Skip to main content
GameDev.net gamedev.net
🔒 Locked

Outputting an Integer and Manipulating it.

Started by Assassin7257 Jan 11, 2012 at 2:49 AM 40 replies 7.5k views
Original Post
Assassin7257
Assassin7257
So I'm making a lottery game, I got the three slots and all. But all I have left, is how to set a certain amount of credit, say 1500 and decrease everytime the user clicks "insert credit". How would I decrease it ?
TheUnbeliever
TheUnbeliever
We need way more information (like language and libraries used) if you want detailed help, but this is basically going to be a case of

- having a 'money' variable stored in an appropriate scope
- when the user clicks the button, decrement this value then update the display
[TheUnbeliever]
Assassin7257
Assassin7257
I'm using SDL, and C++, would I use an interger and how would I output it.
RulerOfNothing
RulerOfNothing
One way is to convert the integer to a string (using the itoa or sprintf functions) then using the SDL_ttf library to write the resulting string to the screen.
boogyman19946
boogyman19946
I take it you're making a graphical user interface. Rather to us an integer or a float depends on if you're going to allow someone to insert fractions of a credit. If you allow only 1 credit at a time, which seems to be most sensible, us an integer. Once you detect that the player clicked insert credit, credits = credits - X (X being number of credits inserted) or credits-- to decrease by one.

As to converting the number, in C++ you can use std::stringstream like so


std::stringstream ss;
ss << credits;
std::string creditString = ss.str(); // returns string representation of credits.
Yo dawg, don't even trip.
Assassin7257
Assassin7257
|error: cannot convert 'std::basic_string, std::allocator >' to 'SDL_Surface*' for argument '3' to 'void apply_surface(int, int, SDL_Surface*, SDL_Surface*, SDL_Rect*)'| I did what you said but all I keep getting this error, How would I show it on the screen ?
RulerOfNothing
RulerOfNothing
As I said, input the string to the SDL_ttf library (it has a function called TTF_WriteText or something like that which takes a character string and produces a SDL_Surface with the string in graphical form). If you are using stringstream, you need to transform the std::string into a char*, which should be possible using a function called c_str() (or something like that)
boogyman19946
boogyman19946
That's because apply_surface is expecting a pointer to an SDL_Surface struct and you're trying to pass it an std::string object. The streamstring only converts the integer into a string. As RulerOfNothing said, you can then use the SDL_ttf library to convert the string into a struct that you can feed to apply_surface.
Yo dawg, don't even trip.
RulerOfNothing
RulerOfNothing
well, you could use the following:

std::stringstream ss;
ss << credits;
std::string creditString = ss.str();
SDL_Surface* output = TTF_RenderText_Solid(font, ss.c_str(), color);

which will allow you to blit the output surface to the screen, provided that you have already called TTF_Init() somewhere and loaded a font into the font variable. Also color is a SDL_Color.
Assassin7257
Assassin7257
std::stringstream ss;
ss << score;
std::string creditString = ss.c_str();

SDL_Surface* output = TTF_RenderText_Solid(font, ss.c_str(), color);

apply_surface( 0, 150, output, screen );



//apply surfaces
apply_surface( 0, 0, background, screen );

Here is the chunck of code I have and I still dont get it
Assassin7257
Assassin7257
'struct std::stringstream' has no member named 'c_str'| is says that
BCullis
BCullis
1) Get used to reading references for the language or library you're using: like this for example. There is no c_str() but there IS a str() function, which is what you're looking for. That reference even has example code.

2) In your TTF_RenderText_Solid call, [s]use creditString, not another call to the str() function, otherwise why bother with the variable at all?[/s] More obvious EDIT: use creditString.c_str(). My previous suggestion was made while unfamiliar with the TTF function's argument list.

EDIT: RulerOfNothing, did your example code mean to use the c_str() call on the string object, not the stream? as in:

SDL_Surface* output = TTF_RenderText_Solid(font, creditString.c_str(), color);

That may be where the confusion is coming from, as Assassin here is just copy-pasting from your example.
Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)
Assassin7257
Assassin7257
Now it says this cannot convert 'std::string' to 'const char*' for argument '2' to 'SDL_Surface* TTF_RenderText_Solid(TTF_Font*, const char*, SDL_Color)
Assassin7257
Assassin7257


//The header
#include"SDL/SDL.h"
#include"SDL/SDL_image.h"
#include"SDL/SDL_ttf.h"
#include<string>
#include<sstream>


//Screen Attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//Surface
SDL_Surface* background = NULL;
SDL_Surface* message = NULL;
SDL_Surface* screen = NULL;

//Event Structure
SDL_Event event;

//int score
int score = 9990;

//The fonts thats going to be used
TTF_Font *font = NULL;

//The colour of the font
SDL_Color textColor = { 255, 255, 255 };

SDL_Surface* load_image( std::string filename )
{
//Temporary hold image
SDL_Surface* loadedImage = NULL;

//Optimized IMage
SDL_Surface* optimizedImage = NULL;

//Load image
loadedImage = IMG_Load( filename.c_str() );

if ( loadedImage != NULL )
{
//Optimize Image now
optimizedImage = SDL_DisplayFormat ( loadedImage );

//Free surface
SDL_FreeSurface ( loadedImage );

//if optimized with no errors
if ( optimizedImage != NULL )
{
//Set colour key
SDL_SetColorKey ( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB ( optimizedImage->format, 0, 0xFF,0xFF) );

}
}

//if no errors
return optimizedImage;
}

bool init()
{
if ( SDL_Init( SDL_INIT_EVERYTHING) == -1)
{
return false;
}

//set up screen
screen = SDL_SetVideoMode ( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

//if there was no errors
if ( screen == NULL )
{
return false;
}

//Initilize TTF_Init()
if ( TTF_Init () == -1)
{
return false;
}

//set window caption
SDL_WM_SetCaption( "Rendering Text", NULL );

//if there was no errors
return true;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Offset
SDL_Rect offset;

//assigning offset
offset.x = x;
offset.y = y;

//BlitSurface
SDL_BlitSurface( source, clip, destination, &offset);
}

bool load_files()
{
//load image
background = load_image ( "background.png" );

if ( background == NULL )
{
return false;
}

font = TTF_OpenFont( "lazy.ttf", 28);

if ( font == NULL )
{
return false;
}

//if no errors at all
return true;
}

void cleanup()
{
//Free up surface
SDL_FreeSurface ( background );
SDL_FreeSurface ( message );

//Close the font being use
TTF_CloseFont ( font );

//Quit font
TTF_Quit();

//SDL Quit
SDL_Quit();
}

int main ( int argc, char* args [] )
{
//event quit
bool quit = false;

if ( init() == false )
{
return 1;
}

if ( load_files() == false)
{
return 1;
}

std::stringstream ss;
ss << score;
std::string creditString = ss.str();

SDL_Surface* output = TTF_RenderText_Solid(font, creditString, textColor);
SDL_BlitSurface( creditString, NULL, screen, NULL);





//apply surfaces
apply_surface( 0, 0, background, screen );

//Update screen
if ( SDL_Flip (screen) == -1 )
{
return 1;
}

//while quit is false
while ( quit == false)
{
//while there event to handle
while ( SDL_PollEvent ( &event ))
{
//if the user hasn't quit
if ( event.type == SDL_QUIT )
{
//if the user hasnt Xed
quit = true;
}
}
}

//end program
cleanup();

return 0;
}
RulerOfNothing
RulerOfNothing
BCullis you are right in that I meant creditString.c_str() not ss.c_str(). Sorry about that Assassin7257.
BCullis
BCullis
Assassin, read my edit. Here's your problem line:

SDL_Surface* output = TTF_RenderText_Solid(font, creditString, textColor);


Which should read:

SDL_Surface* output = TTF_RenderText_Solid(font, creditString.c_str(), textColor);

because the TTF_RenderText_Solid method expects argument 2 to be a c-style string (a pointer) and not a std::string. Thankfully, std::string has a method that converts from string to c-style string. Hooray documentation and reference!
Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)
Assassin7257
Assassin7257
I change it to creditstring.c_str and it still not showing on the screen

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.