• entries
    383
  • comments
    1075
  • views
    352190

Writing on the Wall

Sign in to follow this  
Trapper Zoid

80 views


A Big Ol' Screen of Characters


More updates:
  • I've written a simple "sprite set" class which acts as a container of sprite identifiers. It's effectively an array of sprite ids, except with an extra function that allows you to specify a block of sprite names that end in incrementing digits, such as mysprite_000 through to mysprite_584 for example. I'm hoping it will be useful for animation, but it's also useful as a bitmap font class.
  • Text is working now, at least for single lines. It's a heavily modified version of the code I already had in the partially completed Ice Slider code; unfortunately the internals of my sprite sytem meant I had to rewrite a fair bit. The code is pretty scrappy in places but it does the job for now. The screenshot shown shows various test pieces of text spinning around on the screen, including a big counter in the centre.


Next up: timers, then rudimentary input, then audio. And then the prototype is done?
Sign in to follow this  


2 Comments


Recommended Comments

Hrm, word-wrapping isn't too bad if you've got access to the size of each glyph. And once you've got word-wrapping you're basically set for handling newlines and stuff. And it makes displaying any kind of text a lot easier.

Actually, let me show you what I did for an example.

lol notes since my code is a mess
  • MAX_LENGTH: A constant defined in the class, represents the maximum length word (in letters) before the system breaks it and treats it as multiple words which aren't separated by a space. Think it's 32.
  • _map: A std::map<char,Texture> which essentially is used to fetch the dimensions of each glyph, in pixels.
  • drawText( int x, int y, Color, std::string ): Function which draws a line of text normally at the coordinate specified.
  • _maxHeight: Height in pixels of the tallest glyph in _map + 1 (I think).
  • '\t': handled as four spaces.


void Font::drawText( float x, float y, float w, float h, GfxAdapter::ColorType color, std::string text ) {			

// to perform wordwrapping, we basically buffer the calls
// and flush them when we hit a whitespace character.
// if there isn't enough room to flush, then we go to
// the next line.

// append a terminating whitespace character, so that
// the last word is guarenteed to count as a word.
text += ' ';

std::string temp = "";
float curr_x = x;
float curr_y = y;
float curr_w = 0;

for ( std::string::const_iterator i = text.begin();
i != text.end(); ++i )
{
// check for whitespace.
if ( *i == ' ' || *i == '\t' || *i == '\n' || temp.length() > MAX_LENGTH ) {
// check if there is enough room to flush
if ( curr_x + curr_w < w + x ) {
// flush
drawText( curr_x, curr_y, color, temp );
// increment current x value by the width + space
curr_x += curr_w + _map[' '].w;
if ( *i == '\t' ) curr_x += 4 * _map[' '].w;
// reset temp text width
curr_w = 0; temp = "";
}
else {
// not enough space - go to next line
// but only if the line we're on isn't empty
if ( curr_x != x )
curr_y += _maxHeight + 1;
curr_x = x;
// render
drawText( curr_x, curr_y, color, temp );
// increment x, reset temp vars
curr_x += curr_w + _map[' '].w;
if ( *i == '\t' ) curr_x += 4 * _map[' '].w;
curr_w = 0; temp = "";
}

if ( *i == '\n' ) {
curr_x = x;
curr_y += _maxHeight + 1;
}
}
else {
// don't care, just append it to the buffer
temp += *i;
curr_w += _map[*i].w;
}
}
}




lol, well, I didn't remember it being that nasty. Hur hur, /spam.

God I hate font stuff :3

Share this comment


Link to comment
Thanks, that will come in handy when I implement that feature. I haven't yet attempted to implement multi-line text yet because I currently don't need it; single lines of text are fine for simple arcade games. When I get to a game that needs more text I'll have a stab at a proper text system.

P.S. Timers are working now. Just input and audio to go.

Share this comment


Link to comment

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