#include"SDL/SDL.h"
#include"SDL/SDL_image.h"
#include<string>
using namespace std;
//screen attributes
const int screenWidth = 640;
const int screenHeight = 480;
const int screenBPP = 32;
//surfaces
SDL_Surface* screen = NULL;
//event
SDL_Event event;
//portion being clipped
SDL_Rect clip [ 2 ];
//pokemonclip
SDL_Rect pokemonClip[3];
SDL_Surface* load_image ( string filename )
{
//temporarily store image
SDL_Surface* loadedImage = NULL;
//store optimizedimage
SDL_Surface* optimizedImage = NULL;
//load the image
loadedImage = IMG_Load( filename.c_str() );
//if no errors
if(loadedImage != NULL)
{
//optimize Image now
optimizedImage = SDL_DisplayFormat( loadedImage );
//free up old surface
SDL_FreeSurface(loadedImage);
//if no errors loading optimizedImage
if(optimizedImage != NULL)
{
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB (optimizedImage->format, 0, 0xFF, 0xFF));
}
}
//if no errrors
return optimizedImage;
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
//create rectangle to represent screen
SDL_Rect offset;
//set offset values
offset.x = x;
offset.y = y;
//blit surface
SDL_BlitSurface(source, clip, destination, &offset);
}
bool init()
{
//load everything
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
//set sscreen
screen = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_SWSURFACE);
//if there was an error
if(screen == NULL)
{
return false;
}
//windows caption
SDL_WM_SetCaption("Choose your DECISION", NULL);
//if no errors
return true;
}
void clean_up()
{
//quit
SDL_Quit();
}
//gender class
class Gender
{
private:
SDL_Surface* genderChoice; //genderChoice
SDL_Rect* choice;
public:
bool GenderEvents(); //handles the gender events choice
void showGenderChoice(); //display what gender they choose
void clipGender();
Gender();
~Gender();
};
Gender::Gender()
{
clipGender();
genderChoice = load_image("Characters.png");
choice = NULL;
}
Gender::~Gender()
{
SDL_FreeSurface(genderChoice);
}
void Gender::clipGender()
{
clip[0].x = 83;
clip[0].y = 43;
clip[0].w = 197;
clip[0].h = 385;
clip[1].x = 320;
clip[1].y = 15;
clip[1].w = 410;
clip[1].h = 460;
}
bool Gender::GenderEvents()
{
//get mouse position
int x = 0, y = 0;
apply_surface(0, 0, genderChoice, screen);
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
//get mouse offsets
x = event.motion.x;
y = event.motion.y;
if( (x > clip[0].x) && (x < clip[0].x + clip[0].w) && (y > clip[0].y) && (y < clip[0].y + clip[0].h) )
{
choice = &clip[0];
return true;
}
if( (x > clip[1].x ) && (x < clip[1].x + clip[1].w) && (y > clip[1].y) && (y < clip[1].y + clip[1].h) )
{
choice = &clip[1];
return true;
}
}
}
}
void Gender::showGenderChoice()
{
apply_surface(0, 0, genderChoice, screen, choice);
}
class PokemonChoice
{
private:
SDL_Surface* Pokemon;
SDL_Rect* PokeChoice; //the choice they make
public:
void setPokemonClip();
void PokemonEvents();
void showPokemon();
PokemonChoice();
~PokemonChoice();
};
PokemonChoice::PokemonChoice()
{
Pokemon = load_image("pokemon.png");
setPokemonClip();
PokeChoice = NULL;
}
PokemonChoice::~PokemonChoice()
{
SDL_FreeSurface(Pokemon);
}
void PokemonChoice::setPokemonClip()
{
pokemonClip[0].x = 25;
pokemonClip[0].y = 140;
pokemonClip[0].w = 145;
pokemonClip[0].h = 185;
pokemonClip[1].x = 275;
pokemonClip[1].y = 140;
pokemonClip[1].w = 145;
pokemonClip[1].h = 185;
pokemonClip[2].x = 475;
pokemonClip[2].y = 150;
pokemonClip[2].w = 145;
pokemonClip[2].h = 185;
}
void PokemonChoice::PokemonEvents()
{
//mouse offsets
int x = 0, y = 0;
apply_surface(0, 0, Pokemon, screen);
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
x = event.motion.x;
y = event.motion.y;
if( (x > pokemonClip[0].x) && (x < pokemonClip[0].x + pokemonClip[0].w) && (y > pokemonClip[0].y) && (y < pokemonClip[0].y + pokemonClip[0].w))
{
PokeChoice = &pokemonClip[0];
}
if( (x > pokemonClip[1].x) && (x < pokemonClip[1].x + pokemonClip[1].w) && (y > pokemonClip[1].y) && (y < pokemonClip[1].y + pokemonClip[1].w))
{
PokeChoice = &pokemonClip[1];
}
if( (x > pokemonClip[2].x) && (x < pokemonClip[2].x + pokemonClip[2].w) && (y > pokemonClip[2].y) && (y < pokemonClip[2].y + pokemonClip[2].w))
{
PokeChoice = &pokemonClip[2];
}
}
}
showPokemon();
}
void PokemonChoice::showPokemon()
{
apply_surface(0, 0, Pokemon, screen, PokeChoice);
}
void runGame(PokemonChoice& aPokemon, Gender& aGender)
{
bool Gender = aGender.GenderEvents();
if(Gender == true)
{
SDL_Delay(1000);
SDL_Flip(screen);
aPokemon.PokemonEvents();
}
}
int main(int argc, char* args[])
{
//quit flag
bool quit = false;
//if init failed
if(init() == false)
{
return 1;
}
PokemonChoice myPokemon;
Gender myGender;
myPokemon.PokemonEvents();
while(quit == false)
{
while(SDL_PollEvent(&event))
{
runGame(myPokemon, myGender);
if(event.type == SDL_QUIT)
{
quit = true;
}
}
if(SDL_Flip(screen) == -1)
{
return 1;
}
}
clean_up();
return 0;
}
What I'm trying to do is sort of recreate the begginning of a pokemon game, but I don't seem to know how to load the different events. Does anyone have any idea ? They both load at the same time btw, and not just one at a time.






