Outputting an Integer and Manipulating it.

Recommended Posts

Assassin7257    105
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 ?

Share on other sites
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

Share on other sites
Assassin7257    105
I'm using SDL, and C++, would I use an interger and how would I output it.

Share on other sites
RulerOfNothing    1466
One way is to convert the integer to a string (using the itoa or sprintf functions) then using the [url=http://www.libsdl.org/projects/SDL_ttf/]SDL_ttf library[/url] to write the resulting string to the screen.

Share on other sites
boogyman19946    1487
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

[code]
std::stringstream ss;
ss << credits;
std::string creditString = ss.str(); // returns string representation of credits.
[/code]

Share on other sites
Assassin7257    105
|error: cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' 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 ?

Share on other sites
RulerOfNothing    1466
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)

Share on other sites
boogyman19946    1487
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.

Share on other sites
Assassin7257    105
Hidden
std::stringstream ss;
ss << score;
std::string creditScore = ss.str();

ss.str();

//apply surfaces
apply_surface( 0, 0, background, screen );
font = TTF_OpenFont( "lazy.ttf", 28);
message = TTF_RenderText_Solid(font, ss.str(), textColor);

apply_surface( 0, 150, message, screen);

Assassin7257    105
Could I get an example ?

Share on other sites
RulerOfNothing    1466
well, you could use the following:
[CODE]
std::stringstream ss;
ss << credits;
std::string creditString = ss.str();
SDL_Surface* output = TTF_RenderText_Solid(font, ss.c_str(), color);
[/CODE]
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.

Share on other sites
Assassin7257    105
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

Share on other sites
Assassin7257    105
'struct std::stringstream' has no member named 'c_str'| is says that

Share on other sites
BCullis    1955
1) Get used to reading references for the language or library you're using: [url="http://www.cplusplus.com/reference/iostream/stringstream/"]like this for example[/url]. There is no c_str() but there IS a str() function, which is what you're looking for. That reference [url="http://www.cplusplus.com/reference/iostream/stringstream/str/"]even has example code[/url].

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:

[CODE]SDL_Surface* output = TTF_RenderText_Solid(font, creditString.c_str(), color);[/CODE]

That may be where the confusion is coming from, as Assassin here is just copy-pasting from your example.

Share on other sites
Assassin7257    105
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)

Share on other sites
Assassin7257    105
[code]

#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 };

{
//Temporary hold image

//Optimized IMage
SDL_Surface* optimizedImage = NULL;

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

//Free surface

//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);
}

{
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;
}

{
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;
}
[/code]

Share on other sites
Assassin7257    105
Idk what i'm doing wrong

Share on other sites
RulerOfNothing    1466
BCullis you are right in that I meant creditString.c_str() not ss.c_str(). Sorry about that Assassin7257.

Share on other sites
BCullis    1955

[CODE]SDL_Surface* output = TTF_RenderText_Solid(font, creditString, textColor);[/CODE]

[CODE]SDL_Surface* output = TTF_RenderText_Solid(font, creditString.c_str(), textColor);[/CODE]

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!

Share on other sites
Assassin7257    105
I change it to creditstring.c_str and it still not showing on the screen

Share on other sites
Assassin7257    105
i blit the surface too

Share on other sites
BCullis    1955
What do you expect this line to do?:
[CODE]SDL_BlitSurface( creditString, NULL, screen, NULL);
[/CODE]

Or more importantly, isn't this throwing an error at you as well? Because when I look at [url="http://www.libsdl.org/docs/html/sdlblitsurface.html"]the SDL reference docs[/url], the first argument of SDL_BlitSurface definitely isn't a std::string...

Share on other sites
Assassin7257    105
I wasn't getting an error, I just want to know how would I apply it to the screen now ?

Share on other sites
BCullis    1955
If you've had success blitting other surfaces to the screen, you have everything you need. The key points:
1) Turn an integer into a string using stringstream
2) Turn a string into an SDL Surface* using the TTF function.

So if your code still looks like it did in the last big code post, I'd suggest changing that SDL_BlitSurface call from (creditString, ...) to (output, ...) as creditString is your std::string from before, while output is a pointer to a surface (which is what SDL_BlitSurface expects as its first argument).

Share on other sites
Assassin7257    105
std::stringstream ss;
ss << score;
std::string creditString = creditString.c_str();

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

apply_surface(150, 150, output, screen);

This is what is looks like and nothing is showing.