@@ -12,6 +12,72 @@ using namespace std;
12
12
SDL_Window *window = NULL ;
13
13
SDL_Surface *screen = NULL ;
14
14
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
+
15
81
16
82
bool loadBMP (SDL_Surface *&surface, string path) {
17
83
surface = SDL_LoadBMP (path.c_str ());
@@ -44,6 +110,9 @@ void close() {
44
110
SDL_FreeSurface (bmp);
45
111
bmp = NULL ;
46
112
113
+ SDL_FreeSurface (temp);
114
+ temp = NULL ;
115
+
47
116
SDL_DestroyWindow (window);
48
117
window = NULL ;
49
118
@@ -56,60 +125,8 @@ bool loadMedia() {
56
125
if (!loadBMP (bmp, " alamakota.bmp" ))
57
126
is_good = false ;
58
127
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 ));
87
130
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;
115
132
}
0 commit comments