Skip to content

Commit 29e4147

Browse files
committed
Funcs moved down
Function definitions have been moved at the end of the file
1 parent 148d23d commit 29e4147

File tree

2 files changed

+73
-56
lines changed

2 files changed

+73
-56
lines changed

SDL2_Project.depend

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# depslib dependency file v1.0
22
8519680
33

4-
1631813965 source:d:\codeblocks\projects\sdl2_project\main.cpp
4+
1631820283 source:d:\codeblocks\projects\sdl2_project\main.cpp
55
<stdio.h>
66
<string>
77
<SDL.h>

main.cpp

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,72 @@ using namespace std;
1212
SDL_Window *window = NULL;
1313
SDL_Surface *screen = NULL;
1414
SDL_Surface *bmp = NULL;
15+
SDL_Surface *temp = NULL;
16+
17+
bool init() ;
18+
void close();
19+
bool loadBMP(SDL_Surface *&surface, string path) ;
20+
bool loadMedia() ;
21+
22+
int main(int argc, char **argv) {
23+
if(!init()) {
24+
getch();
25+
return -1;
26+
}
27+
28+
if(!loadMedia()) {
29+
close();
30+
getch();
31+
return -2;
32+
}
33+
34+
SDL_Event e;
35+
36+
SDL_Rect rec;
37+
rec.x = 40;
38+
rec.y = 40;
39+
40+
bool quit = false;
41+
while(!quit) {
42+
43+
while(SDL_PollEvent(&e) != 0) {
44+
if(e.type == SDL_QUIT)
45+
quit = true;
46+
}
47+
48+
int colorShift = 0;
49+
const Uint8* keys = SDL_GetKeyboardState(NULL);
50+
51+
if(keys[SDL_SCANCODE_LEFT]) {
52+
colorShift -= 30;
53+
rec.x--;
54+
}
55+
if(keys[SDL_SCANCODE_RIGHT]) {
56+
colorShift += 65;
57+
rec.x++;
58+
}
59+
if(keys[SDL_SCANCODE_UP]) {
60+
colorShift -= 50;
61+
rec.y--;
62+
}
63+
if(keys[SDL_SCANCODE_DOWN]) {
64+
colorShift += 90;
65+
rec.y++;
66+
}
67+
if(keys[SDL_SCANCODE_ESCAPE])
68+
quit = true;
69+
70+
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 80, 180, 130));
71+
72+
SDL_BlitSurface(temp, NULL, screen, &rec);
73+
74+
SDL_UpdateWindowSurface(window);
75+
}
76+
77+
close();
78+
return 0;
79+
}
80+
1581

1682
bool loadBMP(SDL_Surface *&surface, string path) {
1783
surface = SDL_LoadBMP(path.c_str());
@@ -44,6 +110,9 @@ void close() {
44110
SDL_FreeSurface(bmp);
45111
bmp = NULL;
46112

113+
SDL_FreeSurface(temp);
114+
temp = NULL;
115+
47116
SDL_DestroyWindow(window);
48117
window = NULL;
49118

@@ -56,60 +125,8 @@ bool loadMedia() {
56125
if(!loadBMP(bmp, "alamakota.bmp"))
57126
is_good = false;
58127

59-
return is_good;
60-
}
61-
62-
int main(int argc, char **argv) {
63-
if(!init()) {
64-
getch();
65-
return -1;
66-
}
67-
68-
if(!loadMedia()) {
69-
close();
70-
getch();
71-
return -2;
72-
}
73-
74-
SDL_Event e;
75-
76-
SDL_Surface *temp = SDL_CreateRGBSurface(0, 40, 40, 32, 0, 0, 0, 0);
77-
78-
bool quit = false;
79-
while(!quit) {
80-
81-
while(SDL_PollEvent(&e) != 0) {
82-
if(e.type == SDL_QUIT)
83-
quit = true;
84-
}
85-
86-
int colorShift = 0;
128+
temp = SDL_CreateRGBSurface(0, 40, 40, 32, 0, 0, 0, 0);
129+
SDL_FillRect(temp, NULL, SDL_MapRGB(temp->format, 40, 80, 120));
87130

88-
const Uint8* keys = SDL_GetKeyboardState(NULL);
89-
if(keys[SDL_SCANCODE_LEFT])
90-
colorShift -= 30;
91-
if(keys[SDL_SCANCODE_RIGHT])
92-
colorShift += 65;
93-
if(keys[SDL_SCANCODE_UP])
94-
colorShift -= 50;
95-
if(keys[SDL_SCANCODE_DOWN])
96-
colorShift += 90;
97-
if(keys[SDL_SCANCODE_ESCAPE])
98-
quit = true;
99-
100-
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 80 + colorShift, 180 - colorShift, 130 + colorShift));
101-
SDL_FillRect(temp, NULL, SDL_MapRGB(temp->format, 40 + colorShift, 80 + colorShift, 120 - colorShift));
102-
103-
if(temp) {
104-
SDL_Rect rec;
105-
rec.x = 40;
106-
rec.y = 40;
107-
SDL_BlitSurface(temp, NULL, screen, &rec);
108-
}
109-
110-
SDL_UpdateWindowSurface(window);
111-
}
112-
113-
close();
114-
return 0;
131+
return is_good;
115132
}

0 commit comments

Comments
 (0)