41 replies to this topic
Sponsor:
#2 Members - Reputation: 952
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
- having a 'money' variable stored in an appropriate scope
- when the user clicks the button, decrement this value then update the display
[TheUnbeliever]
#4 Members - Reputation: 812
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.
#5 Members - Reputation: 406
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
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.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ
My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^
My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^
#6 Members - Reputation: 105
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 ?
#7 Members - Reputation: 812
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)
#8 Members - Reputation: 406
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.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ
My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^
My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^
#10 Members - Reputation: 812
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.
#11 Members - Reputation: 105
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
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
#13 Members - Reputation: 1672
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:
That may be where the confusion is coming from, as Assassin here is just copy-pasting from your example.
2) In your TTF_RenderText_Solid call,
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.
#15 Members - Reputation: 105
Posted 10 January 2012 - 11:13 PM
//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;
}
#18 Members - Reputation: 1672
Posted 10 January 2012 - 11:19 PM
Assassin, read my edit. Here's your problem line:
Which should read:
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!
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!






