Hello everyone!
I'm working on a little 2D Game, and I managed to make a 32x32 fill out my whole window :) and I also managed to override the whole 32x32 grass terrain with a 40x40 desert/sand terrain. But I can't figure out how to remove the images I applied on the grass surface.. 'SDL_FreeSurface(######);' just exists the program..
Here's my complete source code:
#include "SDL/SDL_2D.h"
int main(int argc, char * argv[])
{
if(!init())
{
return 1;
}
sprite.x = 150;
sprite.y = 150;
src.x = 128;
src.y = 0;
src.w = SPRITE_SIZE;
src.h = SPRITE_SIZE;
while(!game_over)
{
SDL_Event event;
if(SDL_PollEvent(& event))
{
handle_event(event);
}
if(sprite.x <= 0)
sprite.x = 0;
if(sprite.x >= SCREEN_WIDTH - SPRITE_SIZE)
sprite.x = SCREEN_WIDTH - SPRITE_SIZE;
if(sprite.y <= 0)
sprite.y = 0;
if(sprite.y >= SCREEN_HEIGHT - SPRITE_SIZE)
sprite.y = SCREEN_HEIGHT - SPRITE_SIZE;
for(int x = 0; x < SCREEN_WIDTH / SPRITE_SIZE; x++)
{
for(int y = 0; y < SCREEN_HEIGHT / SPRITE_SIZE; y++)
{
grass.x = x * SPRITE_SIZE;
grass.y = y * SPRITE_SIZE;
apply_grass(grass_area, screen);
if(check_for_collision())
{
sand.x = x * SPRITE_SIZE;
sand.y = y * SPRITE_SIZE;
apply_sand(desert_area, grass_area);
}
}
}
apply_sprites(sprites, screen);
apply_object_01(230, 190, teapot, screen);
apply_go_to_message(10, 10, go_to, screen);
if(SDL_Flip(screen) != 0)
{
return 1;
}
}
}
#include <iostream>
#include "SDL.h"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 450
#define SPRITE_SIZE 32
using namespace std;
void handle_event(SDL_Event event);
bool init();
bool load_files();
SDL_Surface * load_image(string file);
SDL_Surface * load_color_keying(string file);
void apply_sprites(SDL_Surface * source, SDL_Surface * dest);
void apply_grass(SDL_Surface * source, SDL_Surface * dest);
void apply_object_01(int x, int y, SDL_Surface * source, SDL_Surface * dest);
bool check_for_collision();
void apply_go_to_message(int x, int y, SDL_Surface * source, SDL_Surface * dest);
void apply_object_02(int x, int y, SDL_Surface * source, SDL_Surface * dest);
void apply_sand(SDL_Surface * source, SDL_Surface * dest);
bool game_over = false;
int color_key;
SDL_Rect src, sprite, grass, object_01, message_01, object_02, sand;
SDL_Surface * screen, * sprites, * grass_area, * teapot, * go_to,
* teapot_2, * desert_area;
void handle_event(SDL_Event event)
{
switch(event.type)
{
case SDL_QUIT:
game_over = true;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
game_over = 1;
break;
case SDLK_LEFT:
if(src.x == 192)
src.x = 224;
else
src.x = 192;
sprite.x -= 5;
break;
case SDLK_RIGHT:
if(src.x == 64)
src.x = 96;
else
src.x = 64;
sprite.x += 5;
break;
case SDLK_UP:
if(src.x == 0)
src.x = 32;
else
src.x = 0;
sprite.y -= 5;
break;
case SDLK_DOWN:
if(src.x == 128)
src.x = 160;
else
src.x = 128;
sprite.y += 5;
break;
}
break;
}
}
bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
return false;
}
SDL_WM_SetCaption("2D Game", NULL);
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SPRITE_SIZE, SDL_SWSURFACE);
SDL_EnableKeyRepeat(70, 70);
if(!load_files())
{
return false;
}
return true;
}
bool load_files()
{
sprites = load_color_keying("GFX/sprite.bmp");
grass_area = load_image("GFX/grass.bmp");
teapot = load_color_keying("GFX/teapot.bmp");
go_to = load_color_keying("GFX/enter_teapot.bmp");
teapot_2 = load_color_keying("GFX/teapot_2.bmp");
desert_area = load_image("GFX/desert.bmp");
if(sprites == NULL)
{
return false;
}
if(grass_area == NULL)
{
return false;
}
if(teapot == NULL)
{
return false;
}
if(go_to == NULL)
{
return false;
}
if(teapot_2 == NULL)
{
return false;
}
if(desert_area == NULL)
{
return false;
}
return true;
}
SDL_Surface * load_image(string file)
{
SDL_Surface * loaded_image = NULL;
SDL_Surface * optimized_image = NULL;
loaded_image = SDL_LoadBMP(file.c_str());
if(loaded_image != NULL)
{
optimized_image = SDL_DisplayFormat(loaded_image);
SDL_FreeSurface(loaded_image);
}
return optimized_image;
}
SDL_Surface * load_color_keying(string file)
{
SDL_Surface * loaded_image = NULL;
SDL_Surface * optimized_image = NULL;
loaded_image = IMG_Load(file.c_str());
if(loaded_image != NULL)
{
optimized_image = SDL_DisplayFormat(loaded_image);
SDL_FreeSurface(loaded_image);
if(optimized_image != NULL)
{
Uint32 color_key = SDL_MapRGB(optimized_image->format, 0, 0xFF, 0xFF);
SDL_SetColorKey(optimized_image, SDL_SRCCOLORKEY, color_key);
}
}
return optimized_image;
}
void apply_sprites(SDL_Surface * source, SDL_Surface * dest)
{
SDL_BlitSurface(source, & src, dest, & sprite);
}
void apply_grass(SDL_Surface * source, SDL_Surface * dest)
{
SDL_BlitSurface(source, NULL, dest, & grass);
}
void apply_object_01(int x, int y, SDL_Surface * source, SDL_Surface * dest)
{
object_01.x = x;
object_01.y = y;
SDL_BlitSurface(source, NULL, dest, & object_01);
}
bool check_for_collision()
{
if(sprite.x > 470 + 5 && sprite.y > 320 + 5)
{
return true;
}
return false;
}
void apply_go_to_message(int x, int y, SDL_Surface * source, SDL_Surface * dest)
{
message_01.x = x;
message_01.y = y;
SDL_BlitSurface(source, NULL, dest, & message_01);
}
void apply_object_02(int x, int y, SDL_Surface * source, SDL_Surface * dest)
{
object_02.x = x;
object_02.y = y;
SDL_BlitSurface(source, NULL, dest, & object_02);
}
void apply_sand(SDL_Surface * source, SDL_Surface * dest)
{
SDL_BlitSurface(source, NULL, dest, & sand);
}
Please help me?..