Skip to content

Commit a698509

Browse files
committed
Added scale function
+ converting loaded images to optimize
1 parent 29e4147 commit a698509

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030
*.exe
3131
*.out
3232
*.app
33+
SDL2_Project.layout

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-
1631820283 source:d:\codeblocks\projects\sdl2_project\main.cpp
4+
1631823966 source:d:\codeblocks\projects\sdl2_project\main.cpp
55
<stdio.h>
66
<string>
77
<SDL.h>

SDL2_Project.layout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<ActiveTarget name="Release" />
55
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
66
<Cursor>
7-
<Cursor1 position="1410" topLine="74" />
7+
<Cursor1 position="1410" topLine="90" />
88
</Cursor>
99
</File>
1010
</CodeBlocks_layout_file>

main.cpp

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ using namespace std;
1212
SDL_Window *window = NULL;
1313
SDL_Surface *screen = NULL;
1414
SDL_Surface *bmp = NULL;
15-
SDL_Surface *temp = NULL;
15+
SDL_Surface *square = NULL;
16+
SDL_Surface *player = NULL;
1617

17-
bool init() ;
18+
bool init();
1819
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);
2123

2224
int main(int argc, char **argv) {
2325
if(!init()) {
@@ -68,8 +70,7 @@ int main(int argc, char **argv) {
6870
quit = true;
6971

7072
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);
7374

7475
SDL_UpdateWindowSurface(window);
7576
}
@@ -85,6 +86,12 @@ bool loadBMP(SDL_Surface *&surface, string path) {
8586
printf(SDL_GetError());
8687
return false;
8788
}
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+
}
8895

8996
return true;
9097
}
@@ -110,8 +117,11 @@ void close() {
110117
SDL_FreeSurface(bmp);
111118
bmp = NULL;
112119

113-
SDL_FreeSurface(temp);
114-
temp = NULL;
120+
SDL_FreeSurface(square);
121+
square = NULL;
122+
123+
SDL_FreeSurface(player);
124+
player = NULL;
115125

116126
SDL_DestroyWindow(window);
117127
window = NULL;
@@ -124,9 +134,39 @@ bool loadMedia() {
124134

125135
if(!loadBMP(bmp, "alamakota.bmp"))
126136
is_good = false;
137+
if(!loadBMP(player, "player.bmp"))
138+
is_good = false;
139+
if(!scaleImage(player,96,48))
140+
is_good=false;
127141

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));
130144

131145
return is_good;
132146
}
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+
}

player.bmp

3.43 KB
Binary file not shown.

0 commit comments

Comments
 (0)