#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
//The attributes of the window
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 20;
//The dimensions of the dot
const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;
//The dimensions of the level
const int LEVEL_WIDTH = 1280;
const int LEVEL_HEIGHT = 960;
//The surfaces that will be used
SDL_Surface *dot = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
//The event structure that will be used
SDL_Event event;
//The camera (duh)
SDL_Rect camera;
//The dot that will move around on the screen
class Dot
{
private:
//The X and Y offsets of the dot
int x, y;
//The velocity of the dot
int xVel, yVel;
int ballvx,ballvy;
public:
//Initializes the variables
Dot();
//Takes key presses and adjusts the dot's velocity
void handle_input( SDL_Event &event );
//Moves the dot
void move();
//Shows the dot on the screen
void show( SDL_Surface *screen );
//Sets the camera over the dot
void set_camera();
};
//The timer class
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();
//Get the number of ticks since the timer started
//Or gets the number of ticks when the timer was paused
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
SDL_Surface *load_image( std::string filename )
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If nothing went wrong in loading the image
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
//If the image was optimized just fine
if( optimizedImage != NULL )
{
//Set all pixels of color 0x00FFFF to be transparent
SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, 0x00FFFF );
}
}
//Return the optimized image
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Make a temporary rect to hold the offsets
SDL_Rect Offset;
//Give the offsets to the rect
Offset.x = x;
Offset.y = y;
//Blit the surface
SDL_BlitSurface( source, clip, destination, &Offset );
}
bool init()
{
//Initialize all SDL sub systems
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 in error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Move the Dot", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the dot image
dot = load_image( "dot.bmp" );
//Load the background
background = load_image( "bg.png" );
//If there was a problem in loading the dot
if( dot == NULL )
{
return false;
}
//If there was a problem in loading the background
if( background == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surfaces
SDL_FreeSurface( dot );
SDL_FreeSurface( background );
//Quit SDL
SDL_Quit();
}
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::pause()
{
//If the timer is running and isn't 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 the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isn't running return 0
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
Dot::Dot()
{
//Initialize the offsets
x = LEVEL_WIDTH /2;
y = LEVEL_HEIGHT /2;
camera.x = (x + DOT_WIDTH) - SCREEN_WIDTH /2;
camera.y = (y + DOT_HEIGHT) - SCREEN_HEIGHT /2;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Dot::handle_input( SDL_Event &event )
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_w: ballvy -= 20;break;
case SDLK_s: ballvy += 20;break;
case SDLK_a: ballvx -= 20;break;
case SDLK_d: ballvx += 20;break;
case SDLK_UP: yVel -= 20; break;
case SDLK_DOWN: yVel += 20; break;
case SDLK_LEFT: xVel -= 20; break;
case SDLK_RIGHT: xVel += 20; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_w: ballvy += 20;break;
case SDLK_s: ballvy -= 20;break;
case SDLK_a: ballvx += 20;break;
case SDLK_d: ballvx -= 20;break;
case SDLK_UP: yVel += 20; break;
case SDLK_DOWN: yVel -= 20; break;
case SDLK_LEFT: xVel += 20; break;
case SDLK_RIGHT: xVel -= 20; break;
}
}
}
void Dot::show( SDL_Surface *screen )
{
//Show the dot
apply_surface( x - camera.x, y - camera.y, dot, screen );
}
void Dot::move()
{
x += ballvx;
if( ( x < 0 ) || ( x + DOT_WIDTH > LEVEL_WIDTH ) )
{
x -= ballvx;
}
y += ballvy;
if( ( y < 0 ) || ( y + DOT_HEIGHT > LEVEL_HEIGHT ) )
{
y -= ballvy;
}
}
void Dot::set_camera()
{
//Center the camera over the dot
camera.x += xVel;
camera.y += yVel;
//Keep the camera in bounds.
if( camera.x < 0 )
{
camera.x = 0;
}
if( camera.y < 0 )
{
camera.y = 0;
}
if( camera.x > LEVEL_WIDTH - camera.w )
{
camera.x = LEVEL_WIDTH - camera.w;
}
if( camera.y > LEVEL_HEIGHT - camera.h )
{
camera.y = LEVEL_HEIGHT - camera.h;
}
if(camera.x > x)
{
camera.x -= xVel;
}
if(camera.x + SCREEN_WIDTH < x + DOT_WIDTH)
{
camera.x -= xVel;
}
if(camera.y > y)
{
camera.y -= yVel;
}
if(camera.y + SCREEN_HEIGHT < y + DOT_HEIGHT)
{
camera.y -= yVel;
}
}
int main( int argc, char* args[] )
{
//Make sure the program waits for a quit
bool quit = false;
//Make the dot
Dot myDot;
//The frames rate regulator
Timer fps;
//Do the initialization
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Set the camera dimensions
camera.w = SCREEN_WIDTH;
camera.h = SCREEN_HEIGHT;
//While the user hasn't quit
while( quit == false )
{
//Start the frame timer
fps.start();
//While there's an event to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the dot
myDot.handle_input( event );
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Move the dot
myDot.move();
//Set the camera
myDot.set_camera();
//Show the background
apply_surface( 0, 0, background, screen, &camera );
//Show the dot on the screen
myDot.show( screen );
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
while( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
//wait
}
}
//Clean up any uneeded data
clean_up();
return 0;
}
Stupid dot not moving!
Run the code through a debugger by using a conditional breakpoint when you press a key, find out if the velocity is changed by the key press, if the move function is called and does what it should, and if the rendering function does take the change of position into account when rendering.
Whichever of these points fails should tell you what is going on.
Whichever of these points fails should tell you what is going on.
okay i think i did what you said, i checked the lines in bloodshed then ran debug and it did nothing, i dont think the cases are being called?
I didnt look over the code but if the camera is keeping the dot in the middle and you dont have an enviroment around the dot really...how are you gonna tell if the dot has moved?
the camera doesnt follow the dot....the camera actually freely moves up to this point, and so does the dot. they have no relation whats so ever
Quote:Original post by willthiswork89
the camera doesnt follow the dot....the camera actually freely moves up to this point, and so does the dot. they have no relation whats so ever
I think we fail to understand what you mean...
What is your program supposed to do? Move the camera after the dot? Move the dot after the camera? Just move the camera? Just move the dot?
C++
Ok so I made a project with your code and tested it out and it was very easy to see what was missing, here's a big hint, what do you initialize ballvx and ballvy to in your Dot constructor? Once you take care of that, then it will work fine, it did for me.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement