Ok, I'm making Street Fighter. Some foos from gamedev have written a tutorial about Game States.I used it to make my menu. I made 1 base class GameState and 3-4 inherited classes( class Intro, class Menu, class Stage1 ) and I made another base class Characters and only one inherited class, for now, which is class Ryu. Now I somehow need to say to the game: show Ryu and all his stuff (like movement, handle_keys) only when the Stage is active, I don't want my Ryu to walk in the Intro stepping onto the Street Fighter logo.... I tried to make it work in the main function, ugly stuff.. If someone knows other ways, please tell, and here is my code. My code is split between multiple source files, so I posted only the two classes and I need you to give me some pseudocode so I can know what to do.
Basically, I think I want to call the function logic() from the Ryu class into the logic() function in the Stage1 class and the render() from Ruy into the render() in Stage1.
But I need to initialize an object so I can call the Ryu functions, but I don't want any more globals.....
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include "constants.h"
#include "game_state.h"
#include "stage13.h"
#include "timer.h"
#include "functions.h"
#include "globals.h"
Stage13::Stage13()
{
//Load the backgrounds
background[ 0 ] = load_image( "sprites/Stages/Stage13_part1.png" );
background[ 1 ] = load_image( "sprites/Stages/Stage13_part2.png" );
}
Stage13::~Stage13()
{
//Free the background surfaces
for( int i = 0; i < 2; i ++ )
{
SDL_FreeSurface( background[ i ] );
}
}
void Stage13::handle_events()
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
set_next_state( STATE_EXIT );
}
if( event.type == SDL_KEYDOWN )
{
//If the user pressed enter
if( event.key.keysym.sym == SDLK_ESCAPE )
{
//Move to the title screen
set_next_state( STATE_MENU );
}
}
}
void Stage13::logic()
{
;
}
void Stage13::render()
{
//Show the background
for( int i = 0; i < 2; i ++ )
{
apply_surface( 0, 0, background[ i ], screen );
}
}
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include "constants.h"
#include "timer.h"
#include "character.h"
#include "ryu.h"
#include "functions.h"
#include "globals.h"
Ryu::Ryu()
{
//Load the sprites
ryu[ 0 ] = load_image( "sprites/Characters/Ryu/Walking/ryu_walking1.png" );
ryu[ 1 ] = load_image( "sprites/Characters/Ryu/Walking/ryu_walking2.png" );
ryu[ 2 ] = load_image( "sprites/Characters/Ryu/Walking/ryu_walking3.png" );
ryu[ 3 ] = load_image( "sprites/Characters/Ryu/Walking/ryu_walking4.png" );
ryu[ 4 ] = load_image( "sprites/Characters/Ryu/Walking/ryu_walking5.png" );
//Initialize the velocity
Xvelocity = 0;
Yvelocity = 0;
walkstatus = FOO_RIGHT;
frame = 0;
}
Ryu::~Ryu()
{
//Free the Ryu surfaces
for( int i = 0; i <=4; i ++ )
{
SDL_FreeSurface( ryu[ i ] );
}
}
void Ryu::handle_events()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
if( event.key.keysym.sym == SDLK_UP )
{
Yvelocity -= RYU_SPEED;
}
if( event.key.keysym.sym == SDLK_DOWN )
{
Yvelocity += RYU_SPEED;
}
if( event.key.keysym.sym == SDLK_LEFT )
{
Xvelocity -= RYU_SPEED;
}
if( event.key.keysym.sym == SDLK_RIGHT )
{
Xvelocity += RYU_SPEED;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
if( event.key.keysym.sym == SDLK_UP )
{
Yvelocity += RYU_SPEED;
}
if( event.key.keysym.sym == SDLK_DOWN )
{
Yvelocity -= RYU_SPEED;
}
if( event.key.keysym.sym == SDLK_LEFT )
{
Xvelocity += RYU_SPEED;
}
if( event.key.keysym.sym == SDLK_RIGHT )
{
Xvelocity -= RYU_SPEED;
}
}
}
void Ryu::movement()
{
//Move the Ryu left or right
ryubox.x += Xvelocity;
ryubox.y += Yvelocity;
//If the Ryu went too far to the left or right
if( ( ryubox.x < 0 ) || ( ryubox.x + RYU_WIDTH > SCREEN_WIDTH ) )
{
//move back
ryubox.x -= Xvelocity;
}
//Move the Ryu up or down
//y += Yvelocity;
//If the Ryu went too far up or down
if( ( ryubox.y < 0 ) || ( ryubox.y + RYU_HEIGHT > SCREEN_HEIGHT ) )
{
//move back
ryubox.y -= Yvelocity;
}
}
void Ryu::walk_anim()
{
if( Xvelocity < 0 )
{
//Set the animation to left
walkstatus = FOO_LEFT;
//Move to the next frame in the animation
frame++;
}
//If Ryu is moving forward
else if( Xvelocity > 0 )
{
//Set the animation to forward
walkstatus = FOO_RIGHT;
//Move to the next frame
frame++;
}
//If Ryu is standing
else
{
//Set the animation to standing
walkstatus = FOO_STILL;
}
//Loop the animation
if( frame >= 20 )
{
frame = 0;
}
}
void Ryu::logic()
{
movement();
walk_anim();
}
void Ryu::render()
{
//Show Ryu
if( walkstatus == FOO_STILL )
{
apply_surface( ryubox.x, ryubox.y, ryu[ frame/5 ], screen );
}
else if( walkstatus == FOO_RIGHT )
{
apply_surface( ryubox.x, ryubox.y, ryu[ frame/5 ], screen );
}
else if( walkstatus == FOO_LEFT )
{
apply_surface( ryubox.x, ryubox.y, ryu[ frame/5 ], screen );
}
}