• 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
war3cadu12

Problem Textbox

13 posts in this topic

I'm trying to make an insert input so you can type your nickname. I made a box to limit the number of characters but the problem is that you can type unlimitedly and the box won't stop you. I want as soon as the character reach the end of the box I won't let the user type more or push the last characters back, like it would be normally in other games. I hope you understand me [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img]

[img]http://i43.tinypic.com/34qkc4o.jpg[/img]
0

Share this post


Link to post
Share on other sites
Like you said, you have added a limit to the number of characters right?

Then, I assume the following possibilities:
1) The max number of characters are less than the width of the box, therefore no adjustment is needed
2) The max number of characters goes beyond the size of the box, then it is needed to bring the text back.

In order to fix number 2, I did something like the following on mine GUI:
[code]
class TextBox : public UIElement
{
private:
std::string *text;
std::string *view_text;
int maxChar;
(...)

public:
void EnterText(char c); /*Adds or removes a character from the string text*/
void UpdateText(); /*Updates the string that is being viewed (view_text)*/
void Render(...); /*Renders the box + the view_text string*/
(...)
}

void TextBox::UpdateText()
{
int max = (this->width / CHAR_WIDTH); /*Calculates how many characters can be viewed on the box*/
int i = text->size - max; /*Calculates the first character from the string to be printed*/

if(i < 0)
i = 0;
else
this->view_text->assign(this->text, i, max);

/*Reallocates the surface that will have the text blitted*/
SDL_FreeSurface(srf_Text);
srf_Text = TTF_RenderText_Solid(view->text, color, ...);
}

void TextBox::Render(...)
{
(...)
DrawImage(srf_Text, srf_Screen, ...);
}
[/code]

It is something like that... you just have to calculate the first character position to be drawn, and the last will be always (textbox->width / char_width) where char_width is the width of your font character.
1

Share this post


Link to post
Share on other sites
Define a fixed size for the username. When the user is entering text, simply drop characters when the text is at its greatest length. You should make a little beep sound or give some kind of visual feedback that a limit is reached.

Doing what kuramayoko10 suggests, where you keep the additional characters in the buffer but only render the ones that fix, is brittle because it is all too easy to save or send the longer username. Plus you do not want your display logic being responsible for maintaining such an invariant.

If you're using a [url="http://en.wikipedia.org/wiki/Monospaced_font"]fixed width font[/url], then you could probably set 20 characters as your max size. If you're using a [url="http://en.wikipedia.org/wiki/Typeface#Proportion"]variable width font[/url], you might need to cut it down a bit (e.g. 15 characters). If you find that this is too small for the kinds of user names you wish to support, consider making the text smaller.

Don't forget it is becoming the standard to use the user's email address as their primary login name. Their "username" is more of a nickname/ anonymity token.
1

Share this post


Link to post
Share on other sites
Thanks you for helping me. I have another problem here, could you help me? The thing is that when I draw a text, after something like 4 min the text just disappears. It's really weird. Any ideas?
0

Share this post


Link to post
Share on other sites
Sounds like a bug. Have you broken into your application using a debugger when this is happening?

We're not psychic unfortunately, we cannot debug code we cannot see =]
0

Share this post


Link to post
Share on other sites
Here is code:
[code]´bool drawText(int red, int green, int blue, int alpha, string text, string filenamefont, string align, int size, int x, int y)
{
SDLCFont = TTF_OpenFont(filenamefont.c_str(), size);
if (!SDLCFont == NULL)
{
if (red > 255) { red = 255; }
if (green > 255) { green = 255; }
if (blue > 255) { blue = 255; }
if (alpha > 255) { alpha = 255; }
SDL_Color scolors;
SDL_Rect offset;
int width = 0;
int height = 0;
scolors.r = red;
scolors.g = green;
scolors.b = blue;
if (TTF_SizeText(SDLCFont, text.c_str(), &width, &height) == 0)
{
if (align == "left") { offset.x = x-width; }
else if (align == "center") { offset.x = x-width/2; }
else if (align == "right") { offset.x = x; }
offset.y = y;
SDLMessages = TTF_RenderText_Blended( SDLCFont, text.c_str(), scolors );
//SDL_SetAlpha(SDLMessages, SDL_SRCALPHA, alpha);
if ( SDL_BlitSurface(SDLMessages, NULL, SDLFunctionsScreenGame, &offset) )
{
//SDL_Flip(SDLMessages);
return true;
}
}
}
else
{
cout << "Error TTF: " << TTF_GetError() << endl;
return false;
}
TTF_CloseFont(SDLCFont);
return false;
}[/code]

when I got an error with the last code, I created my own class for it:
cpp
[code]
#include "include.h"

using namespace std;

SDL_Surface *TTFScreenGame = NULL;

void onTextStart(SDL_Surface* SceenGame)
{
TTFScreenGame = SceenGame;
}

Text::Text(string filename, int size)
{
TTF_Init();
ttffont = TTF_OpenFont(filename.c_str(), size);
if (!filename.length() == 0 && size)
{
ifstream CheckFile(filename);
if ( CheckFile.is_open() )
{
ttffont = TTF_OpenFont(filename.c_str(), size);
if (!ttffont == NULL)
{
cout << "Loaded TTF: \"" << filename << "\"" << endl;
ttfloaded = true;
}
else
{
cout << "Error: " << TTF_GetError() << endl;
ttfloaded = false;
}
CheckFile.close();
}
else
{
cout << "Error: Can't find \"" << filename << "\" file." << endl;
ttfloaded = false;
}
}
else
{
cout << "Error: You did not enter filename and/or size" << endl;
ttfloaded = false;
}
}

Text::~Text()
{
delete ttffont;
delete messages;
ttffont = NULL;
messages = NULL;
TTF_CloseFont(ttffont);
SDL_FreeSurface(messages);
}

void Text::Render(int red, int green, int blue, int alpha, string text, string align, int size, int x, int y)
{
if (ttfloaded == false) { return; }
if (red > 255) { red = 255; }
if (green > 255) { green = 255; }
if (blue > 255) { blue = 255; }
if (alpha > 255) { alpha = 255; }
SDL_Color scolors;
SDL_Rect offset;
int width = 0;
int height = 0;
scolors.r = red;
scolors.g = green;
scolors.b = blue;
if (TTF_SizeText(ttffont, text.c_str(), &width, &height) == 0)
{
if (align == "left") { offset.x = x-width; }
else if (align == "center") { offset.x = x-width/2; }
else if (align == "right") { offset.x = x; }
offset.y = y;
messages = TTF_RenderText_Blended(ttffont, text.c_str(), scolors );
SDL_SetAlpha(Messages, SDL_SRCALPHA, alpha);
SDL_BlitSurface(messages, NULL, TTFScreenGame, &offset);
}
}

int Text::GetFontWidth(string text)
{
int width = 0;
int height = 0;
if (TTF_SizeText(ttffont, text.c_str(), &width, &height) == 0)
{
return width;
}
return 0;
}
[/code]

h
[code]#ifndef TEXT_H
#define TEXT_H

#include "include.h"

using namespace std;

void onTextStart(SDL_Surface* SDLTTFScreeGame);

class Text
{
private:
SDL_Surface *messages;
TTF_Font *ttffont;
bool ttfloaded;

public:
Text(string, int);
~Text();
void Render(int, int, int, int, string, string, int, int, int);
int GetFontWidth(string);
};

#endif[/code]
0

Share this post


Link to post
Share on other sites
Thanks for helping, it is working now, I tested my game for an hour. There is a bug in your alignment, it's an easy-to-fix bug. One more thing, can I add a setFont function?
-1

Share this post


Link to post
Share on other sites
[quote]
There is a bug in your alignment, it's an easy-to-fix bug.
[/quote]
As warned, I didn't test the code.

[quote]
One more thing, can I add a setFont function?
[/quote]
No.

Of course you can, if you want. I didn't deal with ownership in that program, but one option would be to use std::shared_ptr<> to hold the Font instance.
1

Share this post


Link to post
Share on other sites
[quote name='Cadu' timestamp='1321554425' post='4885040']
Thanks for helping, it is working now, I tested my game for an hour. There is a bug in your alignment, it's an easy-to-fix bug. One more thing, can I add a setFont function?
[/quote]
So, what was the bug that you fixed?
0

Share this post


Link to post
Share on other sites
It appears the bug is that the formulae for "Right" and "Centre" are swapped. I Still haven't tested the code though!
0

Share this post


Link to post
Share on other sites
I tried to fix that formula, but still it doesn't work. When I compile your code, I get this error:

/home/joga/Documents/projektni_zadatak/test/src/test.cpp:35: error: 'Text::Alignment' is not a class or namespace
0

Share this post


Link to post
Share on other sites
Yes. The alignment enum does not introduce a new name space, so the correct version would be Text::Centre rather that Text::Alignment::Centre.

I've actually gone and tested it this time:

text.hpp
[code]

#ifndef TEXT_HPP
#define TEXT_HPP

#include <string>
#include "SDL.h"
#include "SDL_ttf.h"

// Usually in a separate utility header
class noncopyable
{
protected:
noncopyable() {}
private:
// Deliberately private and unimplemented!
noncopyable(const noncopyable &);
noncopyable &operator=(const noncopyable &);
};

class Font : noncopyable
{
public:
Font(const std::string &font, int size);

~Font();

SDL_Surface *renderText(const std::string &text, const SDL_Color &colour) const;
bool sizeText(const std::string &text, int &width, int &height) const;

private:
TTF_Font *font;
};

class Text : noncopyable
{
public:
enum Alignment
{
Left, Right, Centre
};

Text(Font *font);
~Text();

void setText(const std::string &text);
void setAlignment(Alignment alignment);
void setColour(int r, int g, int b);

void draw(SDL_Surface *screen, int x, int y);

private:
Font *font;
SDL_Surface *message;
Alignment alignment;
SDL_Color colour;
std::string string;
};

#endif

[/code]

text.cpp
[code]

#include "text.hpp"
#include <stdexcept>

Font::Font(const std::string &filename, int size)
{
font = TTF_OpenFont(filename.c_str(), size);
if(!font)
{
// Error handling
throw std::runtime_error(std::string("Failed to open font: ") + filename);
}
}

Font::~Font()
{
TTF_CloseFont(font);
}

SDL_Surface *Font::renderText(const std::string &text, const SDL_Color &colour) const
{
SDL_Surface *result = TTF_RenderText_Blended(font, text.c_str(), colour);
if(result == NULL)
{
// Error handling
throw std::runtime_error(std::string("Failed to render blended text: ") + text);
}
return result;
}

bool Font::sizeText(const std::string &text, int &width, int &height) const
{
int result = TTF_SizeText(font, text.c_str(), &width, &height);
return (result == 0);
}

Text::Text(Font *font)
:
font(font),
message(0),
alignment(alignment)
{
if(font == NULL)
{
// Error handling
throw std::runtime_error("Cannot construct Text will NULL Font");
}
colour.r = 0;
colour.g = 0;
colour.b = 0;
}

Text::~Text()
{
if(message)
{
SDL_FreeSurface(message);
}
}

Uint8 sanitiseColour(int component)
{
if(component < 0)
{
return 0;
}
else if(component > 0xff)
{
return 0xff;
}
return component;
}

void Text::setColour(int r, int g, int b)
{
colour.r = sanitiseColour(r);
colour.g = sanitiseColour(g);
colour.b = sanitiseColour(b);
}

void Text::setText(const std::string &text)
{
if(string == text)
{
return;
}
string = text;

SDL_Surface *temp = font->renderText(string, colour);
if(temp)
{
std::swap(temp, message);
SDL_FreeSurface(temp);
}
else
{
throw std::runtime_error(std::string("Failed to render text: ") + string);
// Error handling
}
}

void Text::setAlignment(Alignment alignment)
{
this->alignment = alignment;
}

void Text::draw(SDL_Surface *screen, int x, int y)
{
if(!message)
{
return;
}

int width = 0;
int height = 0;
bool result = font->sizeText(string, width, height);
if(!result)
{
// Error handling...
throw std::runtime_error(std::string("Failed to size text: ") + string);
}
SDL_Rect destination = { x, y };
switch(alignment)
{
case Left:
destination.x = x - width;
break;
case Right:
destination.x = x;
break;
case Centre:
destination.x = x - width / 2;
break;
default:
// Error handling...
throw std::runtime_error("Unknown alignment");
break;
}

if(SDL_BlitSurface(message, NULL, screen, &destination) < 0)
{
// Error handling
throw std::runtime_error("Failed to blit text");
}
}

[/code]

main.cpp
[code]

#include <sstream>
#include <cstdlib>

#include "SDL.h"
#include "text.hpp"

int main( int argc, char* args[] )
{
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
return 1;
}

std::atexit(&SDL_Quit);

if( TTF_Init() < 0)
{
return 1;
}

std::atexit(&TTF_Quit);

SDL_Surface *screen = SDL_SetVideoMode( 800, 600, 0, SDL_SWSURFACE);
if(!screen)
{
return 1;
}

Font font("lazy.ttf", 16);
Text text(&font);

text.setColour(0xff, 0x00, 0x00);
text.setAlignment(Text::Centre);
text.setText("FPS: ?");

int frames = 0;
Uint32 previousTime = SDL_GetTicks();

bool running = true;
while(running)
{
SDL_Event event;
while( SDL_PollEvent(&event) )
{
if( event.type == SDL_QUIT )
{
running = false;
}
else if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_DOWN:
case SDLK_UP:
text.setAlignment(Text::Centre);
break;
case SDLK_LEFT:
text.setAlignment(Text::Left);
break;
case SDLK_RIGHT:
text.setAlignment(Text::Right);
break;
case SDLK_ESCAPE:
running = false;
break;
}
}
}

++frames;
Uint32 now = SDL_GetTicks();
if(now - previousTime > 1000)
{
std::stringstream stream;
stream << "FPS: " << frames;
text.setText(stream.str().c_str());
frames = 0;
previousTime = now;
}

SDL_FillRect(screen, 0, 0);
text.draw(screen, screen->w / 2, screen->h / 2);

if( SDL_Flip( screen ) < 0 )
{
return 1;
}
}

return 0;
}

[/code]

Not exhaustively, mind you. I threw in some very rudimentary error handling. I'm using lazy.ttf from [url="http://lazyfoo.net/SDL_tutorials/lesson07/index.php"]here[/url]. I've also added a way to dynamically change the alignment, using the cursor keys.
1

Share this post


Link to post
Share on other sites
Works like a charm. Thanks for help.

Edit: But the code you made is not and insert input textbox. I mean it does take inputs from key, but i need to get string from that textbox. Like login on websites or something like scanf function in c. I'll try use your code it and experiment with it. Edited by bobojoga
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