C Board  

Go Back   C Board > General Programming Boards > Game Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-18-2009, 06:51 PM   #1
Registered User
 
Join Date: Sep 2009
Posts: 8
SDL-displaying variable with SDL_ttf

This is my slot machine game with SDL, it is not complete but i have two questions.

How do i display a variable with the extension library SDL_ttf

When you run the program and the welcome text is displayed, even though i call SDL_FreeSurface to remove the text from memory, it still displays on the screen.

Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "SDL.h"
#include "SDL_ttf.h"

using namespace std;

typedef unsigned short int USHORT;

unsigned short int tokens = 24;

bool quitSlot = false;

SDL_Event event;

SDL_Surface* screen = NULL;
TTF_Font *arialFont = NULL;

SDL_Color textColourBlack = {0, 0, 0};

void applySurface( int xCoord, int yCoord, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;

    //Give the offsets to the rectangle
    offset.x = xCoord;
    offset.y = yCoord;

    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}

void cleanUpQuit()
{
	TTF_Quit(); 
	SDL_Quit();
}

void welcome()
{
	SDL_Surface* welcomeLineOne = NULL;
	SDL_Surface* welcomeLineTwo = NULL;
	SDL_Surface* welcomeLineThree = NULL;
	SDL_Surface* welcomeLineFour = NULL;
	welcomeLineOne = TTF_RenderText_Solid(arialFont, "Welcome to the most amazing text-based Slot Machine!", textColourBlack);
	welcomeLineTwo = TTF_RenderText_Solid(arialFont, "Our fine Slot Machines may or maynot grant you wishes!", textColourBlack);
	welcomeLineThree = TTF_RenderText_Solid(arialFont, "Anyways, You have tokens.", textColourBlack);
	welcomeLineFour = TTF_RenderText_Solid (arialFont, "Soooo, have fun with these few tokens as you will have none left very, very, very SOON!", textColourBlack);
	applySurface ( 0, 50, welcomeLineOne, screen);
	applySurface ( 0, 70, welcomeLineTwo, screen);
	applySurface ( 0, 90, welcomeLineThree, screen);
	applySurface ( 0, 110, welcomeLineFour, screen);
	SDL_Flip ( screen);
	SDL_FreeSurface ( welcomeLineOne );
	SDL_FreeSurface ( welcomeLineTwo );
	SDL_FreeSurface ( welcomeLineThree );
	SDL_FreeSurface ( welcomeLineFour );
	TTF_CloseFont ( arialFont );
}

void outcome(int firstNumber, int secondNumber, int thirdNumber)
{

    const USHORT tokensThree = 3;
    const USHORT tokensTwo = 2;
    if(firstNumber == secondNumber && secondNumber == thirdNumber && firstNumber == thirdNumber)
    {
        tokens += tokensThree;
        cout << endl << "Congrats! You won 3 Tokens!" << endl;
        cout << "You have " << tokens << " tokens." << endl;
        return;
    }
    if(firstNumber == secondNumber || secondNumber == thirdNumber || firstNumber == thirdNumber)
    {
        tokens += tokensTwo;
        cout << endl << endl << "Congrats! You won 2 Tokens!" << endl;
        cout << "You have " << tokens << " tokens." << endl;
    }
    else
    {
        tokens -= tokensThree;
        cout << endl << endl << "Sorry! You lost 3 Tokens." << endl;
        cout << "You have " << tokens << " tokens." << endl;
    }
    return;
}

void lever()
{
    SDL_Surface* firstImageNumber = NULL;
	SDL_Surface* secondImageNumber = NULL;
	SDL_Surface* thirdImageNumber = NULL;
	SDL_Surface* fourthImageNumber = NULL;
	SDL_Surface* fifthImageNumber = NULL;
	SDL_Surface* sixthImageNumber = NULL;
	firstImageNumber = SDL_LoadBMP ( "Images/1.bmp" );
	secondImageNumber = SDL_LoadBMP ( "Images/2.bmp" );
	thirdImageNumber = SDL_LoadBMP ( "Images/3.bmp" );
	fourthImageNumber = SDL_LoadBMP ( "Images/4.bmp" );
	fifthImageNumber = SDL_LoadBMP ( "Images/5.bmp" );
	sixthImageNumber = SDL_LoadBMP ( "Images/6.bmp" );
	const USHORT highRandom = 6;
	string imputYes;
	string playYes = ("yes");
	string playY = ("y");
	USHORT randomNumber;
    USHORT randomNumberTwo ;
    USHORT randomNumberThree ;
    time_t clockTime;
    time(&clockTime);
    srand((unsigned int) clockTime);
    randomNumber = rand() % highRandom + 1;
    randomNumberTwo = rand() % highRandom + 1;
    randomNumberThree = rand() % highRandom + 1;
    cout << endl << "Do you want to pull the Lever?" << endl;
	cin >> imputYes;
    if (imputYes == playY || imputYes == playYes)
    {
        if(randomNumber == 1)
		{
			//Apply image to screen
			applySurface ( 180, 100, firstImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( firstImageNumber );
		}
		if(randomNumber == 2)
		{
			//Apply image to screen
			applySurface ( 180, 100, secondImageNumber, screen);;

			//Update Screen
			SDL_Flip( screen );

			
			//Free the loaded image
			SDL_FreeSurface( secondImageNumber );
		}
		if(randomNumber == 3)
		{
			//Apply image to screen
			applySurface ( 180, 100, thirdImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( thirdImageNumber );
		}
		if(randomNumber == 4)
		{
			//Apply image to screen
			applySurface ( 180, 100, fourthImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( fourthImageNumber );
		}
		if(randomNumber == 5)
		{
			//Apply image to screen
			applySurface ( 180, 100, fifthImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( fifthImageNumber );
		}
		if(randomNumber == 6)
		{
			//Apply image to screen
			applySurface ( 180, 100, sixthImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( sixthImageNumber );
		}
		if(randomNumberTwo == 1)
		{
			//Apply image to screen
			applySurface ( 180, 140, firstImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( firstImageNumber );
		}
		if(randomNumberTwo == 2)
		{
			//Apply image to screen
			applySurface ( 180, 140, secondImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( secondImageNumber );
		}
		if(randomNumberTwo == 3)
		{
			//Apply image to screen
			applySurface ( 180, 140, thirdImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( thirdImageNumber );
		}
		if(randomNumberTwo == 4)
		{
			//Apply image to screen
			applySurface ( 180, 140, fourthImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( fourthImageNumber );
		}
		if(randomNumberTwo == 5)
		{
			//Apply image to screen
			applySurface ( 180, 140, fifthImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( fifthImageNumber );
		}
		if(randomNumberTwo == 6)
		{
			//Apply image to screen
			applySurface ( 180, 140, sixthImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( sixthImageNumber );
		}
		if(randomNumberThree == 1)
		{
			//Apply image to screen
			applySurface ( 180, 180, firstImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( firstImageNumber );
		}
		if(randomNumberThree == 2)
		{
			//Apply image to screen
			applySurface ( 180, 180, secondImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( secondImageNumber );
		}
		if(randomNumberThree == 3)
		{
			//Apply image to screen
			applySurface ( 180, 180, thirdImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

		
			
			//Free the loaded image
			SDL_FreeSurface( thirdImageNumber );
		}
		if(randomNumberThree == 4)
		{
			//Apply image to screen
			applySurface ( 180, 180, fourthImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );

			
			
			//Free the loaded image
			SDL_FreeSurface( fourthImageNumber );
		}
		if(randomNumberThree == 5)
		{
			//Apply image to screen
			applySurface ( 180, 180, fifthImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );


			
			//Free the loaded image
			SDL_FreeSurface( fifthImageNumber );
		}
		if(randomNumberThree == 6)
		{
			//Apply image to screen
			applySurface ( 180, 180, sixthImageNumber, screen);

			//Update Screen
			SDL_Flip( screen );


			
			//Free the loaded image
			SDL_FreeSurface( sixthImageNumber );
		}
        outcome(randomNumber, randomNumberTwo, randomNumberThree);
    }
    else
    {
		cout << "Goodbye!" << endl;
		exit(0);
    }
}

int main( int argc, char* args[] )
{
	SDL_Surface* background = NULL;
	SDL_Init( SDL_INIT_EVERYTHING );
	TTF_Init();
	arialFont = TTF_OpenFont ( "Fonts/arial.ttf", 14);

	const USHORT screenHeight = 480;
	const USHORT screenWidth = 640;
	const USHORT screenBpp = 32;
	
	screen = SDL_SetVideoMode ( screenWidth, screenHeight, screenBpp, SDL_SWSURFACE );
		
	SDL_WM_SetCaption ( "Slot Machine", NULL);
	
	background = SDL_LoadBMP ( "Images/BG.bmp" );
	applySurface ( 0, 0, background, screen );
	SDL_Flip (  screen );
	SDL_FreeSurface ( background );
	welcome();
	
	
	while ( quitSlot == false)
	{
		
		while ( SDL_PollEvent (&event));
		{
			
			if(event.type == SDL_QUIT )
			{
				quitSlot = true;
			}
		}
	}

	
	cleanUpQuit();
	return 0;
}
nobo707 is offline   Reply With Quote
Old 11-06-2009, 03:37 PM   #2
Registered User
 
Join Date: Dec 2006
Posts: 1,780
Post the smallest possible code that demonstrates the problem.
cyberfish is offline   Reply With Quote
Old 11-07-2009, 08:14 PM   #3
Registered User
 
Join Date: Nov 2005
Posts: 627
First, very repetitive, and without much reason.

Your problem I believe would be that you are never actually clearing the screen. Just rendering over everything.

Freeing the memory has nothing to do with what is on the screen.
Raigne is offline   Reply With Quote
Old 11-11-2009, 09:44 AM   #4
Registered User
 
rogster001's Avatar
 
Join Date: Aug 2006
Location: Liverpool UK
Posts: 241
Not quite sure whats going on there but i think you should use a sprite sheet instead of all those different image files if you are just wanting to display a number at a time

Lazy Foo' Productions

you would just need one image file and you 'clip' the portion that you require of it, only storing that section in a surface, you then just show this section on the screen, or blit it with another surface then to screen. I think you did something similar in there at one point but hard to tell....

Also yeah, your visible screen is not refreshed by using freesurface call, it just frees the memory, whatever you have drawn on your screen surface there remains as lit pixels until you colour them otherwise.

Last edited by rogster001; 11-12-2009 at 02:17 AM.
rogster001 is offline   Reply With Quote
Old 11-12-2009, 02:43 AM   #5
Registered User
 
rogster001's Avatar
 
Join Date: Aug 2006
Location: Liverpool UK
Posts: 241
Also your code as already mentioned is much too repetitive, only call the function that actually displaysyour image on screen once in your big if block, the rest of your if statements could be a switch and only write the new image into memory ready to display after the correct case has been matched,
If you insist on sticking with a seperate image file for each number picked in your program load the surfaces at the beginning of and free the surfaces at the end of your function, again this will clear up what you are working with, and everyone else has to look at.
rogster001 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
float variable displaying in hexadecimal format using pointers vlrk C Programming 16 10-03-2008 04:35 PM
Static Local Variable vs. Global Variable arpsmack C Programming 7 08-21-2008 03:35 AM
float/double variable storage and precision cjschw C++ Programming 4 07-28-2003 06:23 PM
Beginner question Tride C Programming 30 05-24-2003 08:36 AM
sdl in c++ Klinerr1 C++ Programming 8 07-07-2002 07:46 AM


All times are GMT -6. The time now is 08:58 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22