• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Assassin7257

Outputting an Integer and Manipulating it.

41 posts in this topic

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

Share this post


Link to post
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
|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 ?
0

Share this post


Link to post
Share on other sites
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)
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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);
0

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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)
0

Share this post


Link to post
Share on other sites
[code]

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

Share this post


Link to post
Share on other sites
Assassin, read my edit. Here's your problem line:

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


Which should read:

[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!
0

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0