Outputting an Integer and Manipulating it.

Started by
40 comments, last by Assassin7257 12 years, 3 months ago
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 ?
Advertisement
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]
I'm using SDL, and C++, would I use an interger and how would I output it.
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.
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.

|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 ?
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)
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.

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);
Could I get an example ?

This topic is closed to new replies.

Advertisement