I found a good tutorial and added a few things to it such as text on screen using the TTF dll, the code worked fine before setting up the font and attempting to display it. I still get no errors or warnings it just compiles then closes instantly :(, no idea whats going on. Ive tried google.
My code:
/*This source code copyrighted by Lazy Foo' Productions (2004-2012)
and may not be redestributed without written permission.*/
//The headers
#include "SDL.h"
#include "SDL_image.h"
#include <string>
#include "SDL_ttf.h"
//The screen sttributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The attributes of the dot
const int PLAYER_WIDTH = 20;
const int PLAYER_HEIGHT = 20;
const int PLAYER_VEL = 200;
//The surfaces
SDL_Surface *dot = NULL;
SDL_Surface *screen = NULL;
//TTF_Font
SDL_Surface *message = NULL;
TTF_Font *font = NULL;
SDL_Color textColor = { 255, 255, 255 };
//The event structure
SDL_Event event;
//The wall
SDL_Rect wall;
//The dot
class Dot
{
private:
//The X and Y offsets of the dot
SDL_Rect player; //float player.x, player.y;
//The velocity of the dot
float xVel, yVel;
public:
//Initializes the variables
Dot();
//Takes key presses and adjusts the dot's velocity
void handle_input();
//Moves the dot
void move( Uint32 deltaTicks );
//Shows the dot on the screen
void show();
};
bool check_collision( SDL_Rect A, SDL_Rect B )
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
//Calculate the sides of rect B
leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;
}
//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 timer's time
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
SDL_Surface *load_image( std::string filename )
{
//The image that's 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( screen == NULL )
{
return false;
}
if( !TTF_Init() )
{
return false;
}
SDL_WM_SetCaption( "Move the Dot", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the dot image and check if there was a problem loading
dot = load_image( "dot.bmp" );
//Open the font
font = TTF_OpenFont( "ARIAL.ttf", 18 );
if( dot == NULL )
{
return false;
}
if( font == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface( dot );
SDL_FreeSurface( message );
//Close the font that was used
TTF_CloseFont( font );
//Quit SDL_ttf
TTF_Quit();
//Quit SDL
SDL_Quit();
}
Dot::Dot()
{
//Initialize the offsets
player.x = 400;
player.y = 400;
//Set the square's dimensions
player.w = PLAYER_WIDTH;
player.h = PLAYER_HEIGHT;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Dot::handle_input()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= PLAYER_VEL; break;
case SDLK_DOWN: yVel += PLAYER_VEL; break;
case SDLK_LEFT: xVel -= PLAYER_VEL; break;
case SDLK_RIGHT: xVel += PLAYER_VEL; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += PLAYER_VEL; break;
case SDLK_DOWN: yVel -= PLAYER_VEL; break;
case SDLK_LEFT: xVel += PLAYER_VEL; break;
case SDLK_RIGHT: xVel -= PLAYER_VEL; break;
}
}
}
void Dot::move( Uint32 deltaTicks )
{
//Move the dot left or right
player.x += xVel * ( deltaTicks / 1000.f );
//Collision check with screen edges or wall
if( ( player.x < 0 ) || ( player.x + PLAYER_WIDTH > SCREEN_WIDTH ) || ( check_collision( player, wall ) ) )
{
//Move back
player.x -= xVel * ( deltaTicks / 1000.f );
}
//Move the dot up or down
player.y += yVel * ( deltaTicks / 1000.f );
//If the square went too far up or down or has collided with the wall
if( ( player.y < 0 ) || ( player.y + PLAYER_HEIGHT > SCREEN_HEIGHT ) || ( check_collision( player, wall ) ) )
{
//Move back
player.y -= yVel * ( deltaTicks / 1000.f );
}
}
void Dot::show()
{
//Show the dot
apply_surface( (int)player.x, (int)player.y, dot, screen );
}
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 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;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
//Show info on screen
void showInfo() {
//Render the text
message = TTF_RenderText_Solid( font, "Player.x: ", textColor );
apply_surface( 20, 20, message, screen );
}
//--------------MAIN FUNCTION------------//
int main( int argc, char* args[] )
{
TTF_Init();
//Quit flag
bool quit = false;
//The dot that will be used
Dot myDot;
//Keeps track of time since last rendering
Timer delta;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Wall attributes
wall.x = 300;
wall.y = 40;
wall.w = 40;
wall.h = 400;
//Start delta timer
delta.start();
//While the user hasn't quit -ALL CODE GOES WITHIN THIS LOOP-
while( quit == false )
{
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the dot
myDot.handle_input();
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Move the dot
myDot.move( delta.get_ticks() );
//Restart delta timer
delta.start();
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
//Show the wall
SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 0x77, 0x77, 0x77 ) );
//Show the dot on the screen
myDot.show();
//Show message
showInfo();
//Load message
if( message == NULL )
{
return 1;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
//Clean up
clean_up();
return 0;
}