/
core.cpp
245 lines (196 loc) · 5.11 KB
/
core.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "main.h"
using namespace std;
namespace scrnfunk
{
// Optimize image on load to reduce processing.
SDL_Surface *load_image(const char *file)
{
SDL_Surface *image_format;
SDL_Surface *screen_format;
// Load image
image_format = IMG_Load(file);
/*
DOES THE FILE EVEN EXIST?
*/
if(image_format != 0)
{
// Optimize
screen_format = SDL_DisplayFormat(image_format);
// Release old image
SDL_FreeSurface(image_format);
}
return screen_format;
}
void apply_image(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip)
{
SDL_Rect pos;
pos.x = x;
pos.y = y;
SDL_BlitSurface(source, clip, destination, &pos);
}
// Removes color from an image.
bool RemoveColor(SDL_Surface* image, Uint8 R, Uint8 G, Uint8 B)
{
int result;
// Map color on the image
Uint32 color = SDL_MapRGB(image->format, R, G, B);
// Remove color from the image.
result = SDL_SetColorKey(image, SDL_SRCCOLORKEY, color);
if(!result)
return true;
else
return false;
}
}
namespace scrns
{
// Returns a pointer on success, NULL on an error
SDL_Surface *initiateSDL(int x, int y)
{
SDL_Surface *screen = NULL;
SDL_Surface *vid_init;
int init;//, ttf_ret;
// Initiate SDL and check
init = SDL_Init(SDL_INIT_VIDEO);
if(init < 0)
{
fprintf(stderr, "Unable to initiate SDL: %s\n", SDL_GetError());
return NULL;
}
// Set video and check
vid_init = SDL_SetVideoMode(x, y, 32, SDL_SWSURFACE);
if(vid_init == 0)
{
fprintf(stderr, "Video initialization failed: %s", SDL_GetError());
return NULL;
}
// Get video surface and check
screen = SDL_GetVideoSurface();
if(screen == NULL)
{
fprintf(stderr, "Failed to get video surface: %s", SDL_GetError());
return NULL;
}
// Set window title
SDL_WM_SetCaption(WINDOW_TITLE " " VERSION, 0);
/*
// Initialize SDL_ttf and check
ttf_ret = TTF_Init();
if(ttf_ret == -1)
{
fprintf(stderr, "True Type Font initialization failed: %s", SDL_GetError());
return NULL;
}
*/
SDL_FreeSurface(vid_init);
// Success
return screen;
}
/*SDL_Surface *MainMenu()
{
}*/
SDL_Surface *Stage1()
{
// Screen
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
// Image filenames
const char *background_image = "bin/stage.bmp";
screen = initiateSDL(900, 646);
if(screen == NULL)
{
fprintf(stderr, "Start up failure.\n");
return NULL;
}
else
{
// Load images
background = scrnfunk::load_image(background_image);
// Blit background image
scrnfunk::apply_image(0, 0, background, screen);
// Update screen with all applied images
if(screen != 0)
SDL_Flip(screen);
else
fprintf(stderr, "Couldn't update screen.\n");
}
// Free old surfaces
SDL_FreeSurface(background);
SDL_FreeSurface(screen);
return screen;
}
SDL_Surface *Movement(SDL_Surface *screen)
{
// Movement
// D -- Mario move LEFT
// A -- Mario move RIGHT
SDL_Surface *background = NULL;
SDL_Surface *mario0 = NULL;
//SDL_Surface *mario1 = NULL;
//SDL_Surface *mario2 = NULL;
SDL_Surface *mario3 = NULL;
//SDL_Surface *mario4 = NULL;
//SDL_Surface *mario5 = NULL;
int x,y;
x = 125;
y = 520;
// Image filenames
const char *background_image = "bin/stage.bmp";
const char *mario_image0 = "bin/IMG0.bmp";
//const char *mario_image1 = "bin/IMG1.bmp";
//const char *mario_image2 = "bin/IMG2.bmp";
const char *mario_image3 = "bin/IMG3.bmp";
//const char *mario_image4 = "bin/IMG4.bmp";
//const char *mario_image5 = "bin/IMG5.bmp";
background = scrnfunk::load_image(background_image);
mario0 = scrnfunk::load_image(mario_image0);
//mario1 = scrnfunk::load_image(mario_image1);
//mario2 = scrnfunk::load_image(mario_image2);
mario3 = scrnfunk::load_image(mario_image3);
//mario4 = scrnfunk::load_image(mario_image4);
//mario5 = scrnfunk::load_image(mario_image5);
SDL_Rect clip;
clip.x = x;
clip.y = y;
clip.w = 100;
clip.h = 100;
scrnfunk::RemoveColor(mario0, 0x00, 0x00, 0xFF); // Blue
//scrnfunk::RemoveColor(mario1, 0x00, 0x00, 0xFF); // Blue
//scrnfunk::RemoveColor(mario2, 0x00, 0x00, 0xFF); // Blue
scrnfunk::RemoveColor(mario3, 0x00, 0x00, 0xFF); // Blue
scrnfunk::apply_image(x, y, mario0, screen);
if(screen != 0)
SDL_Flip(screen);
SDL_Event event;
while(1)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
return NULL;
else if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_d) // RIGHT
{
scrnfunk::apply_image(x, y, background, screen, &clip); // Cover current Mario
x=x+10;
clip.x = x;
scrnfunk::apply_image(x, y, mario3, screen); // Place new Mario
}
else if(event.key.keysym.sym == SDLK_a) // LEFT
{
scrnfunk::apply_image(x, y, background, screen, &clip); // Cover current Mario
x=x-10;
clip.x = x;
scrnfunk::apply_image(x, y, mario3, screen); // Place new Mario
}
}
}
if(screen != 0)
SDL_Flip(screen);
else
fprintf(stderr, "Couldn't update screen.\n");
}
return screen;
}
}