lalacomun wrote:EddieRingle wrote:Looks like a neat start, but I heavily suggest you don't re-load the textures in the Dot class on every key press (dunno what your load_image function does, but I can only assume the worst).

My load image, load's a bmp image then optimize it to a png image, after that it free the previous image (the bmp one)
so as you saw on the screenshot:
load_image(c:\...player.bmp);
i load that image , then optimize it, and then free the bmp image
But when you already have loaded the image once to memory, why remove it and load it back in again?
You should just load the image once, when you init your engine, and reuse that image throughout the whole game, then you free the image when the game ends.
Think about it. Now, each and every frame, the program opens the image, read every single pixels from the image, and stores in to memory, and frees the surface.
Code: Select all
void initDot() {
SDL_Surface * dot_img_up = load_image(dot_up.png);
SDL_Surface * dot_img_down = load_image(dot_down.png);
SDL_Surface * dot_img_right = load_image(dot_right.png);
SDL_Surface * dot_img_left = load_image(dot_left.png);
int dot_facing = 0; //0 = up, 1 = down, 2 = right, 3 = left
}
void drawDot() {
if(dot facing == 0) {
draw_surface(dot_img_up, dot_x, dot_y);
}
else if(dot facing == 1) {
draw_surface(dot_img_down, dot_x, dot_y);
}
else if(dot facing == 2) {
draw_surface(dot_img_right, dot_x, dot_y);
}
else if(dot facing == 3) {
draw_surface(dot_img_left, dot_x, dot_y);
}
}
void moveDot() {
/* your movement stuff here */
case SDLK_UP:
facing = 0;
break;
case SDLK_DOWN:
facing = 1;
break;
case SDLK_RIGHT:
facing = 2;
break;
case SDLK_LEFT:
facing = 3;
break;
}
void cleanUpDot() {
SDL_FreeSurface(dot_img_up);
SDL_FreeSurface(dot_img_down);
SDL_FreeSurface(dot_img_right);
SDL_FreeSurface(dot_img_left);
}
I wrote this code fast and dirty, just so it could be easily understandable. But I hope you got the drill.