• Create Account

## Outputting an Integer and Manipulating it.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

41 replies to this topic

### #1Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 08:49 PM

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 ?

### #2TheUnbeliever  Members

962
Like
0Likes
Like

Posted 10 January 2012 - 08:53 PM

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]

### #3Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 08:55 PM

I'm using SDL, and C++, would I use an interger and how would I output it.

### #4RulerOfNothing  Members

1369
Like
0Likes
Like

Posted 10 January 2012 - 09:28 PM

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.

### #5boogyman19946  Members

1479
Like
0Likes
Like

Posted 10 January 2012 - 09:28 PM

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.


- Java API Documentation - For all your Java info needs
- C++ Standard Library Reference - For some of your C++ needs ^.^

### #6Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 10:16 PM

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

### #7RulerOfNothing  Members

1369
Like
0Likes
Like

Posted 10 January 2012 - 10:26 PM

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)

### #8boogyman19946  Members

1479
Like
0Likes
Like

Posted 10 January 2012 - 10:27 PM

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.

- Java API Documentation - For all your Java info needs
- C++ Standard Library Reference - For some of your C++ needs ^.^

### #9Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 10:41 PM

Could I get an example ?

### #10RulerOfNothing  Members

1369
Like
0Likes
Like

Posted 10 January 2012 - 10:46 PM

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.

### #11Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 10:52 PM

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

### #12Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 10:56 PM

'struct std::stringstream' has no member named 'c_str'| is says that

### #13BCullis  Members

1951
Like
0Likes
Like

Posted 10 January 2012 - 11:04 PM

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, use creditString, not another call to the str() function, otherwise why bother with the variable at all? 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)

### #14Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 11:12 PM

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)

### #15Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 11:13 PM



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

}



### #16Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 11:13 PM

Idk what i'm doing wrong

### #17RulerOfNothing  Members

1369
Like
0Likes
Like

Posted 10 January 2012 - 11:14 PM

BCullis you are right in that I meant creditString.c_str() not ss.c_str(). Sorry about that Assassin7257.

### #18BCullis  Members

1951
Like
0Likes
Like

Posted 10 January 2012 - 11:19 PM

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

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)

### #19Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 11:21 PM

I change it to creditstring.c_str and it still not showing on the screen

### #20Assassin7257  Members

105
Like
0Likes
Like

Posted 10 January 2012 - 11:24 PM

i blit the surface too

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.