Lazy Foo SDL tutorial # 23 [help me plz]
Posted: Wed Jul 11, 2012 6:23 pm
While I work on fixing this myself, I figured I would post the code to see if any of you can figure it out.
This doesn't qualify as that forum rule "don't post a long list of code and expect us to debug" does it?
Please note, I have been over this a few times, I don't know what is wrong with it. I will continue looking.
This doesn't qualify as that forum rule "don't post a long list of code and expect us to debug" does it?
Code: Select all
#include"stdafx.h" //Required header for Windows, or MS Visual Studio, don't remember which.
#include"SDL.h"
#include"SDL_ttf.h"
#include"SDL_image.h"
#include<string>
#include<sstream>
#include<cmath>
#include<vector>
TTF_Font *font = NULL;
const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;
const int LEVEL_WIDTH = 1280;
const int LEVEL_HEIGHT = 960;
const int FRAMES_PER_SECOND = 30;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
int frame = 0 ;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface * message = NULL;
SDL_Surface * frametext = NULL;
SDL_Surface *dot = NULL;
SDL_Surface *timesurf = NULL;
bool quit = false;
SDL_Event event;
SDL_Color textcolor = {0,0,255};
void apply_surface (int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL) {
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, clip , screen, &offset); //Ok.
}
class StringInput {
private:
std::string str;
SDL_Surface * text;
public:
StringInput();
~StringInput();
void handle_input();
void show_centered();
} ;
StringInput::StringInput()
{
str = "";
text = NULL;
SDL_EnableUNICODE(SDL_ENABLE);
}
StringInput::~StringInput() {
SDL_FreeSurface(text);
SDL_EnableUNICODE(SDL_DISABLE);
}
void StringInput::show_centered() {
if (text != NULL) {
apply_surface( 20, 100, text, screen); //Changed the location of the text for my own reasons.
}
}
void StringInput::handle_input() {
if (event.type == SDL_KEYDOWN) {
std::string temp = str;
if ( str.length() <= 16) {
if ( event.key.keysym.unicode == (Uint16) ' ' ) {
str += (char)event.key.keysym.unicode;
}
else if ( event.key.keysym.unicode >= (Uint16)'0' && event.key.keysym.unicode <= (Uint16) '9' )
{
str+= (char) event.key.keysym.unicode;
}
else if ( event.key.keysym.unicode >= (Uint16)'A' && event.key.keysym.unicode <= (Uint16) 'Z' )
{
str+= (char) event.key.keysym.unicode;
}
else if ( event.key.keysym.unicode >= (Uint16)'a' && event.key.keysym.unicode <= (Uint16) 'z' )
{
str+= (char) event.key.keysym.unicode;
}
}
if (event.key.keysym.unicode == SDLK_BACKSPACE && str.length() != 0) {
str.erase(str.length() - 1);
}
if (str != temp) {
SDL_FreeSurface (text);
text = TTF_RenderText_Solid(font, str.c_str(), textcolor);
}
}
}
class Timer {
private:
int startticks;
int pausedticks;
bool started;
bool paused;
public:
Timer();
void start();
void stop();
void pause();
void unpause();
int get_ticks();
bool is_started();
bool is_paused();
};
Timer::Timer() {
startticks = 0;
pausedticks = 0;
paused = false;
started = false;
}
void Timer::start() {
started = true;
paused = false;
startticks = SDL_GetTicks();
}
void Timer::stop()
{
started = false;
paused = false;
}
int Timer::get_ticks() {
if (started == true ) {
if (paused == true ) {
return pausedticks;
}
else {
return SDL_GetTicks() - startticks;
}
}
return 0;
}
void Timer::pause() {
if ( started == true && paused == false ) {
paused = true;
pausedticks = SDL_GetTicks() - startticks;
}
}
void Timer::unpause() {
if (paused == true) {
paused = false;
startticks = SDL_GetTicks() - startticks;
pausedticks = 0;
}
}
bool Timer::is_started() {
return started;
}
bool Timer::is_paused() {
return paused;
}
void clean_up()
{
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface (screen);
SDL_FreeSurface( dot );
SDL_FreeSurface (frametext);
SDL_FreeSurface (message);
//Close the font
TTF_CloseFont( font );
TTF_Quit();
//Quit SDL
SDL_Quit();
}
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 = SDL_LoadBMP( 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;
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
if (TTF_Init() == - 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( "Press an Arrow Key", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the background image
background = load_image( "background.bmp" );
dot = load_image( "dot.bmp" );
//Open the font
font = TTF_OpenFont("lazy.ttf", 42);
//If there was a problem in loading the background
if( background == NULL || dot == NULL || font == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
int main ( int argc, char* args[] )
{
bool nameEntered = false;
Timer myTime;
Timer fps;
StringInput name;
if (init() == false ) {
return false;
}
if (load_files() == false) {
return false;
}
myTime.start(); //The time the loop started. Keeps track of the time.
message = TTF_RenderText_Solid( font, "New High Score! Enter Name:", textcolor );
while (quit == false ) //Loop to do stuff within.
{
fps.start(); //Frame timer.
std::stringstream time; //Something to convert the time into a string, along with a message.
while (SDL_PollEvent( &event ) ) { //Gets an event every time the loop passes.
if ( event.type == SDL_QUIT ) {
fps.stop();
myTime.stop();
quit = true; //Duh.
}
if (nameEntered == false)
{
name.handle_input();
if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN ) )
{
nameEntered = true;
SDL_FreeSurface ( message);
message = TTF_RenderText_Solid( font, "Rank: 1St ", textcolor);
}
}
}
apply_surface( 0, 0, background, screen );
apply_surface( 20, 100, message, screen ); //Changed the location of the text for my own reasons.
name.show_centered();
time << "Time: " << myTime.get_ticks() / 1000.f; //Put the phrase "Time: " and the current time since the start of the loop, since its in milliseconds, we divide it by 1000 because there are 1000 milliseconds in one second.
timesurf = TTF_RenderText_Solid(font, time.str().c_str(), textcolor); //Put the time text into timesurf (short for time surface).
apply_surface( 50 , 10 , timesurf , screen);
SDL_FreeSurface(timesurf);
frametext = TTF_RenderText_Solid(font, "Testing frame rate" , textcolor); //Frame message, shows how fast the framerate is in a visual way.
apply_surface(( SCREEN_WIDTH - frametext->w ) / 2, ((SCREEN_HEIGHT + frametext->h * 2 ) / FRAMES_PER_SECOND ) * (frame % FRAMES_PER_SECOND ) - frametext->h, frametext, screen); //Render the text showing the framerate message.
if (SDL_Flip (screen) == - 1) {
return false;
}
frame += 1; //Frame counter. Used to
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
clean_up();
return 0;
}