@@ -12,12 +12,14 @@ 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 ;
15
+ SDL_Surface *square = NULL ;
16
+ SDL_Surface *player = NULL ;
16
17
17
- bool init () ;
18
+ bool init ();
18
19
void close ();
19
- bool loadBMP (SDL_Surface *&surface, string path) ;
20
- bool loadMedia () ;
20
+ bool loadBMP (SDL_Surface *&surface, string path);
21
+ bool loadMedia ();
22
+ bool scaleImage (SDL_Surface *&original, int newWidth, int newHeight);
21
23
22
24
int main (int argc, char **argv) {
23
25
if (!init ()) {
@@ -68,8 +70,7 @@ int main(int argc, char **argv) {
68
70
quit = true ;
69
71
70
72
SDL_FillRect (screen, NULL , SDL_MapRGB (screen->format , 80 , 180 , 130 ));
71
-
72
- SDL_BlitSurface (temp, NULL , screen, &rec);
73
+ SDL_BlitSurface (player, NULL , screen, &rec);
73
74
74
75
SDL_UpdateWindowSurface (window);
75
76
}
@@ -85,6 +86,12 @@ bool loadBMP(SDL_Surface *&surface, string path) {
85
86
printf (SDL_GetError ());
86
87
return false ;
87
88
}
89
+ SDL_Surface *optimized = SDL_ConvertSurface (surface, surface->format , 0 );
90
+ if (!optimized) {
91
+ SDL_FreeSurface (surface);
92
+ printf (SDL_GetError ());
93
+ return false ;
94
+ }
88
95
89
96
return true ;
90
97
}
@@ -110,8 +117,11 @@ void close() {
110
117
SDL_FreeSurface (bmp);
111
118
bmp = NULL ;
112
119
113
- SDL_FreeSurface (temp);
114
- temp = NULL ;
120
+ SDL_FreeSurface (square);
121
+ square = NULL ;
122
+
123
+ SDL_FreeSurface (player);
124
+ player = NULL ;
115
125
116
126
SDL_DestroyWindow (window);
117
127
window = NULL ;
@@ -124,9 +134,39 @@ bool loadMedia() {
124
134
125
135
if (!loadBMP (bmp, " alamakota.bmp" ))
126
136
is_good = false ;
137
+ if (!loadBMP (player, " player.bmp" ))
138
+ is_good = false ;
139
+ if (!scaleImage (player,96 ,48 ))
140
+ is_good=false ;
127
141
128
- temp = SDL_CreateRGBSurface (0 , 40 , 40 , 32 , 0 , 0 , 0 , 0 );
129
- SDL_FillRect (temp , NULL , SDL_MapRGB (temp ->format , 40 , 80 , 120 ));
142
+ square = SDL_CreateRGBSurface (0 , 40 , 40 , 32 , 0 , 0 , 0 , 0 );
143
+ SDL_FillRect (square , NULL , SDL_MapRGB (square ->format , 40 , 80 , 120 ));
130
144
131
145
return is_good;
132
146
}
147
+
148
+ bool scaleImage (SDL_Surface *&og, int newWidth, int newHeight) {
149
+ SDL_Surface *newImage = SDL_CreateRGBSurface (0 , newWidth, newHeight, 32 , og->format ->Rmask , og->format ->Gmask , og->format ->Bmask , og->format ->Amask );
150
+
151
+ if (!newImage) {
152
+ printf (" Image could not be scaled: %s" , SDL_GetError ());
153
+ return false ;
154
+ }
155
+
156
+ SDL_Rect rec;
157
+ rec.x = 0 ;
158
+ rec.y = 0 ;
159
+ rec.w = newWidth;
160
+ rec.h = newHeight;
161
+
162
+ if (SDL_BlitScaled (og, NULL , newImage, &rec)<0 ){
163
+ SDL_FreeSurface (newImage);
164
+ printf (" Image could not be scaled: %s" , SDL_GetError ());
165
+ return false ;
166
+ }
167
+
168
+ SDL_FreeSurface (og);
169
+ og = newImage;
170
+
171
+ return true ;
172
+ }
0 commit comments