-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDL tutorial.sln
More file actions
186 lines (149 loc) · 4.16 KB
/
SDL tutorial.sln
File metadata and controls
186 lines (149 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Using SDL and Standard io
# include <SDL.h>
# include <stdio.h>
#include <string>
//Screen Dimensions constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Key press surface constant
enum KeyPressSurfaces {
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_TOTAL,
};
//Starts up SDL and creates window
bool init();
//loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//Loads individual image
SDL_Surface* loadSurface(std::string path);
//The Window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The images that correspond to a keypress
SDL_Surface* gKeyPressSurfaces[KEY_PRESS_SURFACE_TOTAL];
//Current displayed image
SDL_Surface* gCurrentSurface = NULL;
bool init() {
//initialisation flag
bool success = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialise SDL_Error: %s\n", SDL_GetError());
success = false;
}
else {
//Create Window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else {
// Get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
return success;
}
bool loadMedia() {
//Load success flag
bool success = true;
//Load defaut surface
gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("snake_images/snake_default.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] == NULL) {
printf("Failed to load default image!\n");
success = false;
}
//Load up surface
gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] = loadSurface("snake_images/snake_up.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] == NULL) {
printf("Failed to load up image!\n");
success = false;
}
//Load down surface
gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] = loadSurface("snake_images/snake_down.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] == NULL) {
printf("Failed to load down image!\n");
success = false;
}
return success;
}
void close() {
//Deallocate surface
for (int i = 0; i < KEY_PRESS_SURFACE_TOTAL; ++i) {
SDL_FreeSurface(gKeyPressSurfaces[i]);
gKeyPressSurfaces[i] = NULL;
}
//Destroy Window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Close SDL subsystem
SDL_Quit();
}
SDL_Surface* loadSurface(std::string path) {
//Load image at specified path
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
if (loadedSurface == NULL) {
printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
return loadedSurface;
}
int main(int argc, char* args[]) {
// Start up SDL and create Window
if (!init())
{
printf("Failed to initialise\n");
}
else {
//Load media
if (!loadMedia())
{
printf("Failed to load media\n");
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//Set default current surface
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
//While application is running
while (!quit) {
//Handle events on game
while (SDL_PollEvent(&e) != 0) {
//User Requests quit
if (e.type == SDL_QUIT) {
quit = true;
}
//User presses a key
else if (e.type == SDL_KEYDOWN) {
//Select surface based on key press
switch (e.key.keysym.sym) {
case SDLK_UP:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_UP];
break;
case SDLK_DOWN:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN];
break;
default:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
break;
}
}
}
//Apply the image
SDL_BlitSurface(gCurrentSurface, NULL, gScreenSurface, NULL);
//Update the surface
SDL_UpdateWindowSurface(gWindow);
}
}
}
//Free resources and close SDL
close();
return 0;
}