-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_hello_SDL.cpp
170 lines (148 loc) · 4.64 KB
/
01_hello_SDL.cpp
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
#include <SDL.h>
#include <stdio.h>
#include <string>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
enum KeyPressSurfaces
{
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
bool init();
bool loadMedia();
void close();
SDL_Surface* loadSurface( std::string path );
SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gKeyPressSurfaces[ KEY_PRESS_SURFACE_TOTAL ];
SDL_Surface* gCurrentSurface = NULL;
bool init()
{
bool success = true;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
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
{
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
return success;
}
bool loadMedia()
{
bool success = true;
gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("images/press.bmp");
if(gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] == NULL){
printf("Failed to load default image!\n");
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] = loadSurface("images/up.bmp");
if(gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] == NULL){
printf("Failed to load up image!\n");
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] = loadSurface("images/down.bmp");
if(gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] == NULL){
printf("Failed to load down image!\n");
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] = loadSurface("images/left.bmp");
if(gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] == NULL){
printf("Failed to load left image!\n");
success = false;
}
gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] = loadSurface("images/right.bmp");
if(gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] == NULL){
printf("Failed to load right image!\n");
success = false;
}
return success;
}
void close()
{
for(int i = 0; i < KEY_PRESS_SURFACE_TOTAL; i++){
SDL_FreeSurface(gKeyPressSurfaces[i]);
gKeyPressSurfaces[i] = NULL;
}
//SDL_FreeSurface(gCurrentSurface);
//gCurrentSurface = NULL;
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
}
SDL_Surface* loadSurface(std::string 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 main2( int argc, char* args[] )
{
if(!init())
{
printf("initialize faile\n");
}
else
{
if(!loadMedia())
{
printf("fail to load media!\n");
}
else
{
bool quit = false;
SDL_Event e;
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
while(!quit)
{
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
{
quit = true;
}else if(e.type == SDL_KEYDOWN){
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;
case SDLK_LEFT:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT];
break;
case SDLK_RIGHT:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT];
break;
default:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
break;
}
}
}
SDL_BlitSurface(gCurrentSurface,NULL,gScreenSurface,NULL);
SDL_UpdateWindowSurface(gWindow);
}
}
}
close();
return 0;
}