EDN Admin
Well-known member
Im using SDL with this, but. I was up all night trying to split this up into .h and .cpp files (aside from main.cpp, which i basically want to just cant the main process). I had no luck and my friend wouldnt help me. So I can understand later on, how does
one split this up to work? It works altogether but I couldnt get it to work in peices. My friend told me something about the .h tells what everything is and a .cpp is what they do, but that makes no sense, I need to see code. Thanks
<pre class="prettyprint //The headers
#include <string>
#include <iostream>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
//Screen attributes
const int SCREEN_WIDTH = 320;
const int SCREEN_HEIGHT = 240;
const int SCREEN_BPP = 32;
//The frames per second
const int FRAMES_PER_SECOND = 60;
//The dimenstions of the player
const int PLAYER_WIDTH = 32;
const int PLAYER_HEIGHT = 32;
//The constant speed of the player (when moving)
const int PLAYER_SPEED = 3.14;
//The direction status of the player
const int PLAYER_RIGHT = 0;
const int PLAYER_LEFT = 1;
//The surfaces
SDL_Surface *player = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//The areas of the sprite sheet
SDL_Rect clipsRight[ 5 ];
SDL_Rect clipsLeft[ 5 ];
//The player
class Player
{
private:
//The offset
int offSet;
//Its rate of movement
int velocity;
//Its current frame
int frame;
//Its animation status
int status;
public:
//Initializes the variables
Player();
//Handles input
void handle_events();
//Moves the player
void move();
//Shows the player
void show();
};
//The timer
class Timer
{
private:
//The clock time when the timer started
int startTicks;
//The ticks stored when the timer was paused
int pausedTicks;
//The timer status
bool paused;
bool started;
public:
//Initializes variables
Timer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timers time
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
void set_clips()
{
//Clip the sprites
clipsRight[ 0 ].x = 0;
clipsRight[ 0 ].y = 0;
clipsRight[ 0 ].w = PLAYER_WIDTH;
clipsRight[ 0 ].h = PLAYER_HEIGHT;
clipsRight[ 1 ].x = PLAYER_WIDTH;
clipsRight[ 1 ].y = 0;
clipsRight[ 1 ].w = PLAYER_WIDTH;
clipsRight[ 1 ].h = PLAYER_HEIGHT;
clipsRight[ 2 ].x = PLAYER_WIDTH * 2;
clipsRight[ 2 ].y = 0;
clipsRight[ 2 ].w = PLAYER_WIDTH;
clipsRight[ 2 ].h = PLAYER_HEIGHT;
clipsRight[ 3 ].x = PLAYER_WIDTH * 3;
clipsRight[ 3 ].y = 0;
clipsRight[ 3 ].w = PLAYER_WIDTH;
clipsRight[ 3 ].h = PLAYER_HEIGHT;
clipsRight[ 4 ] = clipsRight[ 2 ];
clipsLeft[ 0 ].x = 0;
clipsLeft[ 0 ].y = PLAYER_HEIGHT;
clipsLeft[ 0 ].w = PLAYER_WIDTH;
clipsLeft[ 0 ].h = PLAYER_HEIGHT;
clipsLeft[ 1 ].x = PLAYER_WIDTH;
clipsLeft[ 1 ].y = PLAYER_HEIGHT;
clipsLeft[ 1 ].w = PLAYER_WIDTH;
clipsLeft[ 1 ].h = PLAYER_HEIGHT;
clipsLeft[ 2 ].x = PLAYER_WIDTH * 2;
clipsLeft[ 2 ].y = PLAYER_HEIGHT;
clipsLeft[ 2 ].w = PLAYER_WIDTH;
clipsLeft[ 2 ].h = PLAYER_HEIGHT;
clipsLeft[ 3 ].x = PLAYER_WIDTH * 3;
clipsLeft[ 3 ].y = PLAYER_HEIGHT;
clipsLeft[ 3 ].w = PLAYER_WIDTH;
clipsLeft[ 3 ].h = PLAYER_HEIGHT;
clipsLeft[ 4 ] = clipsLeft[ 2 ];
}
SDL_Surface *load_image( std::string filename )
{
//The image thats loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Platform Engine", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the sprite sheet
player = load_image( "player.png" );
//If there was a problem in loading the sprite
if( player == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface( player );
//Quit SDL
SDL_Quit();
}
Player:layer()
{
//Initialize movement variables
offSet = 0;
velocity = 0;
//Initialize animation variables
frame = 0;
status = PLAYER_RIGHT;
}
void Player::handle_events()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the velocity
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: velocity += PLAYER_SPEED; break;
case SDLK_LEFT: velocity -= PLAYER_SPEED; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Set the velocity
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: velocity -= PLAYER_SPEED; break;
case SDLK_LEFT: velocity += PLAYER_SPEED; break;
}
}
}
void Player::move()
{
//Move
offSet += velocity;
//Keep the player in bounds
if( ( offSet < 0 ) || ( offSet + PLAYER_WIDTH > SCREEN_WIDTH ) )
{
offSet -= velocity;
}
}
void Player::show()
{
//If Player is moving left
if( velocity < 0 )
{
//Set the animation to left
status = PLAYER_LEFT;
//Move to the next frame in the animation
frame++;
}
//If Player is moving right
else if( velocity > 0 )
{
//Set the animation to right
status = PLAYER_RIGHT;
//Move to the next frame in the animation
frame++;
}
//If Player standing
else
{
//Restart the animation
frame = 0;
}
//Loop the animation
if( frame >= 5 )
{
frame = 1;
}
//Show the player
if( status == PLAYER_RIGHT )
{
apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsRight[ frame ] );
}
else if( status == PLAYER_LEFT )
{
apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsLeft[ frame ] );
}
}
Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//Start the timer
started = true;
//Unpause the timer
paused = false;
//Get the current clock time
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
//Stop the timer
started = false;
//Unpause the timer
paused = false;
}
void Timer:ause()
{
//If the timer is running and isnt already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;
//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;
//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isnt running
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Clip the sprite sheet
set_clips();
//The frame rate regulator
Timer fps;
//The player
Player walk;
//While the user hasnt quit
while( quit == false )
{
//Start the frame timer
fps.start();
//While theres events to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the player
walk.handle_events();
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Move the player
walk.move();
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
//Show the player on the screen
walk.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 6000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 6000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
//Clean up
clean_up();
return 0;
}
[/code]
<br/>
View the full article
one split this up to work? It works altogether but I couldnt get it to work in peices. My friend told me something about the .h tells what everything is and a .cpp is what they do, but that makes no sense, I need to see code. Thanks
<pre class="prettyprint //The headers
#include <string>
#include <iostream>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
//Screen attributes
const int SCREEN_WIDTH = 320;
const int SCREEN_HEIGHT = 240;
const int SCREEN_BPP = 32;
//The frames per second
const int FRAMES_PER_SECOND = 60;
//The dimenstions of the player
const int PLAYER_WIDTH = 32;
const int PLAYER_HEIGHT = 32;
//The constant speed of the player (when moving)
const int PLAYER_SPEED = 3.14;
//The direction status of the player
const int PLAYER_RIGHT = 0;
const int PLAYER_LEFT = 1;
//The surfaces
SDL_Surface *player = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//The areas of the sprite sheet
SDL_Rect clipsRight[ 5 ];
SDL_Rect clipsLeft[ 5 ];
//The player
class Player
{
private:
//The offset
int offSet;
//Its rate of movement
int velocity;
//Its current frame
int frame;
//Its animation status
int status;
public:
//Initializes the variables
Player();
//Handles input
void handle_events();
//Moves the player
void move();
//Shows the player
void show();
};
//The timer
class Timer
{
private:
//The clock time when the timer started
int startTicks;
//The ticks stored when the timer was paused
int pausedTicks;
//The timer status
bool paused;
bool started;
public:
//Initializes variables
Timer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timers time
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
void set_clips()
{
//Clip the sprites
clipsRight[ 0 ].x = 0;
clipsRight[ 0 ].y = 0;
clipsRight[ 0 ].w = PLAYER_WIDTH;
clipsRight[ 0 ].h = PLAYER_HEIGHT;
clipsRight[ 1 ].x = PLAYER_WIDTH;
clipsRight[ 1 ].y = 0;
clipsRight[ 1 ].w = PLAYER_WIDTH;
clipsRight[ 1 ].h = PLAYER_HEIGHT;
clipsRight[ 2 ].x = PLAYER_WIDTH * 2;
clipsRight[ 2 ].y = 0;
clipsRight[ 2 ].w = PLAYER_WIDTH;
clipsRight[ 2 ].h = PLAYER_HEIGHT;
clipsRight[ 3 ].x = PLAYER_WIDTH * 3;
clipsRight[ 3 ].y = 0;
clipsRight[ 3 ].w = PLAYER_WIDTH;
clipsRight[ 3 ].h = PLAYER_HEIGHT;
clipsRight[ 4 ] = clipsRight[ 2 ];
clipsLeft[ 0 ].x = 0;
clipsLeft[ 0 ].y = PLAYER_HEIGHT;
clipsLeft[ 0 ].w = PLAYER_WIDTH;
clipsLeft[ 0 ].h = PLAYER_HEIGHT;
clipsLeft[ 1 ].x = PLAYER_WIDTH;
clipsLeft[ 1 ].y = PLAYER_HEIGHT;
clipsLeft[ 1 ].w = PLAYER_WIDTH;
clipsLeft[ 1 ].h = PLAYER_HEIGHT;
clipsLeft[ 2 ].x = PLAYER_WIDTH * 2;
clipsLeft[ 2 ].y = PLAYER_HEIGHT;
clipsLeft[ 2 ].w = PLAYER_WIDTH;
clipsLeft[ 2 ].h = PLAYER_HEIGHT;
clipsLeft[ 3 ].x = PLAYER_WIDTH * 3;
clipsLeft[ 3 ].y = PLAYER_HEIGHT;
clipsLeft[ 3 ].w = PLAYER_WIDTH;
clipsLeft[ 3 ].h = PLAYER_HEIGHT;
clipsLeft[ 4 ] = clipsLeft[ 2 ];
}
SDL_Surface *load_image( std::string filename )
{
//The image thats loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Platform Engine", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the sprite sheet
player = load_image( "player.png" );
//If there was a problem in loading the sprite
if( player == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface( player );
//Quit SDL
SDL_Quit();
}
Player:layer()
{
//Initialize movement variables
offSet = 0;
velocity = 0;
//Initialize animation variables
frame = 0;
status = PLAYER_RIGHT;
}
void Player::handle_events()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the velocity
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: velocity += PLAYER_SPEED; break;
case SDLK_LEFT: velocity -= PLAYER_SPEED; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Set the velocity
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: velocity -= PLAYER_SPEED; break;
case SDLK_LEFT: velocity += PLAYER_SPEED; break;
}
}
}
void Player::move()
{
//Move
offSet += velocity;
//Keep the player in bounds
if( ( offSet < 0 ) || ( offSet + PLAYER_WIDTH > SCREEN_WIDTH ) )
{
offSet -= velocity;
}
}
void Player::show()
{
//If Player is moving left
if( velocity < 0 )
{
//Set the animation to left
status = PLAYER_LEFT;
//Move to the next frame in the animation
frame++;
}
//If Player is moving right
else if( velocity > 0 )
{
//Set the animation to right
status = PLAYER_RIGHT;
//Move to the next frame in the animation
frame++;
}
//If Player standing
else
{
//Restart the animation
frame = 0;
}
//Loop the animation
if( frame >= 5 )
{
frame = 1;
}
//Show the player
if( status == PLAYER_RIGHT )
{
apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsRight[ frame ] );
}
else if( status == PLAYER_LEFT )
{
apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsLeft[ frame ] );
}
}
Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//Start the timer
started = true;
//Unpause the timer
paused = false;
//Get the current clock time
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
//Stop the timer
started = false;
//Unpause the timer
paused = false;
}
void Timer:ause()
{
//If the timer is running and isnt already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;
//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;
//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isnt running
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Clip the sprite sheet
set_clips();
//The frame rate regulator
Timer fps;
//The player
Player walk;
//While the user hasnt quit
while( quit == false )
{
//Start the frame timer
fps.start();
//While theres events to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the player
walk.handle_events();
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Move the player
walk.move();
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
//Show the player on the screen
walk.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 6000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 6000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
//Clean up
clean_up();
return 0;
}
[/code]
<br/>
View the full article