Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Fonts and text input

This thread is locked; no one can reply to it. rss feed Print
Fonts and text input
OICW
Member #4,069
November 2003
avatar

I have some questions:
1) Can I do some type of text input ( if I want to get names of players to hall off fame or get name of save game ) ?

2) Can be the fonts larger ( in resolution 800 x 600 are the fonts 8 x 8 very small ) ?

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

SonShadowCat
Member #1,548
September 2001
avatar

I remmeber something called gstream being used for text input.

mEmO
Member #1,124
March 2001
avatar

 string szInput;

 if (keypressed())
 {
  szInput += readkey() & 0xff;
 }

---------------------------------------------
There is only one God, and Connor is his son!
http://www.memocomputers.com
Happy birthday!

23yrold3yrold
Member #1,134
March 2001
avatar

C++:

1// edittext.cpp
2#include <allegro.h>
3#include <string>
4using namespace std;
5 
6#define WHITE makecol(255, 255, 255)
7 
8int main()
9{
10 // typical Allegro initialization
11 allegro_init();
12 install_keyboard();
13 set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0);
14 
15 // all variables are here
16 BITMAP* buffer = create_bitmap(320, 240); // initialize the double buffer
17 string edittext; // an empty string for editting
18 string::iterator iter = edittext.begin(); // string iterator
19 int caret = 0; // tracks the text caret
20 bool insert = true; // true of should text be inserted
21
22 // the game loop
23 do
24 {
25 while(keypressed())
26 {
27 int newkey = readkey();
28 char ASCII = newkey & 0xff;
29 char scancode = newkey >> 8;
30 
31 // a character key was pressed; add it to the string
32 if(ASCII >= 32 && ASCII <= 126)
33 {
34 // add the new char, inserting or replacing as need be
35 if(insert || iter == edittext.end())
36 iter = edittext.insert(iter, ASCII);
37 else
38 edittext.replace(caret, 1, 1, ASCII);
39 
40 // increment both the caret and the iterator
41 caret++;
42 iter++;
43 }
44 // some other, "special" key was pressed; handle it here
45 else
46 switch(scancode)
47 {
48 case KEY_DEL:
49 if(iter != edittext.end()) iter = edittext.erase(iter);
50 break;
51 
52 case KEY_BACKSPACE:
53 if(iter != edittext.begin())
54 {
55 caret--;
56 iter--;
57 iter = edittext.erase(iter);
58 }
59 break;
60
61 case KEY_RIGHT:
62 if(iter != edittext.end()) caret++, iter++;
63 break;
64
65 case KEY_LEFT:
66 if(iter != edittext.begin()) caret--, iter--;
67 break;
68
69 case KEY_INSERT:
70 if(insert) insert = 0; else insert = 1;
71 break;
72 
73 default:
74 
75 break;
76 }
77 }
78
79 // clear screen
80 clear(buffer);
81 
82 // output the string to the screen
83 textout(buffer, font, edittext.c_str(), 0, 10, WHITE);
84 
85 // output some stats using Allegro's printf functions
86 textprintf(buffer, font, 0, 20, WHITE, "length: %d", edittext.length());
87 textprintf(buffer, font, 0, 30, WHITE, "capacity: %d", edittext.capacity());
88 textprintf(buffer, font, 0, 40, WHITE, "empty?: %d", edittext.empty());
89 if(insert)
90 textout(buffer, font, "Inserting", 0, 50, WHITE);
91 else
92 textout(buffer, font, "Replacing", 0, 50, WHITE);
93 
94 // draw the caret
95 vline(buffer, caret * 8, 8, 18, WHITE);
96 
97 // blit to screen
98 blit(buffer, screen, 0, 0, 0, 0, 320, 240);
99 
100 }while(!key[KEY_ESC]); // end of game loop
101
102 // clean up
103 destroy_bitmap(buffer);
104 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
105
106 return 0;
107}
108END_OF_MAIN()

C:

1// edittext.c
2#include <allegro.h>
3 
4#define BUFFERSIZE 128
5 
6int main()
7{
8 BITMAP* buffer = NULL;
9 char edittext[BUFFERSIZE];
10 int caret = 0;
11 
12 /* typical Allegro initialization */
13 allegro_init();
14 install_keyboard();
15 set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0);
16 
17 buffer = create_bitmap(320, 240);
18 
19 do
20 {
21 if(keypressed())
22 {
23 int newkey = readkey();
24 char ASCII = newkey & 0xff;
25 char scancode = newkey >> 8;
26 
27 /* a character key was pressed; add it to the string */
28 if(ASCII >= 32 && ASCII <= 126)
29 {
30 if(caret < BUFFERSIZE - 1)
31 {
32 edittext[caret] = ASCII;
33 caret++;
34 edittext[caret] = '\0';
35 }
36 }
37 else if(scancode == KEY_BACKSPACE)
38 {
39 if (caret > 0) caret--;
40 edittext[caret] = '\0';
41 }
42 }
43
44 /* all drawing goes here */
45 clear(buffer);
46 textout(buffer, font, edittext, 0, 10, makecol(255, 255, 255));
47 vline(buffer, caret * 8, 8, 18, makecol(255, 255, 255));
48 blit(buffer, screen, 0, 0, 0, 0, 320, 240);
49 
50 }
51 while(!key[KEY_ESC]);
52
53 destroy_bitmap(buffer);
54 
55 return 0;
56}
57END_OF_MAIN()

And yes, you can use different fonts, but I'll leave that to someone who knows more about how than me :P

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

mEmO
Member #1,124
March 2001
avatar

You didn't leave him much work to do ::):P;)

---------------------------------------------
There is only one God, and Connor is his son!
http://www.memocomputers.com
Happy birthday!

23yrold3yrold
Member #1,134
March 2001
avatar

Hey, give me some credit; I completely ignored the second question! ;)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Maverick
Member #2,337
May 2002

Quote:

2) Can be the fonts larger ( in resolution 800 x 600 are the fonts 8 x 8 very small ) ?

You could create and use your own font. However, if you don't have the time/inclination/artistic ability, you could just draw the system 8x8 font to a temporary bitmap (with a color 0, or "magic pink" background), then stretch_sprite() it onto the screen (or your backbuffer) to whatever size you need it.

-Maverick

-----
"the polls here don't change as much because I believe so much in free speakin' that I want everyone a chance to vote at least once, and possibly a few dozen times, that way they are really heard." -Matthew Leverton

gnolam
Member #2,030
March 2002
avatar

Quote:

You could create and use your own font. However, if you don't have the time/inclination/artistic ability, you could just draw the system 8x8 font to a temporary bitmap (with a color 0, or "magic pink" background), then stretch_sprite() it onto the screen (or your backbuffer) to whatever size you need it.

Or just use ttf2pcx ;)

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

OICW
Member #4,069
November 2003
avatar

Thanks for posibilities, I have another question, if I draw my own font in one bmp, how can I select one character of 40. Or I had to draw it in many bmps?

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

miran
Member #2,407
June 2002

[plug]Allegro Font Editor[/plug]

--
sig used to be here

Basix 9
Member #3,994
October 2003

23yrold3yrold here are the errors i get when i compile your code =(. I am using MingW32 with DevC++
BTW Martin, you can use allegro font library for TTF fonts.

1In file included from D:/DEVCPP/include/windows.h:52,
2 from D:/DEVCPP/include/c++/mingw32/bits/gthr-default.h:462,
3 from D:/DEVCPP/include/c++/mingw32/bits/gthr.h:98,
4 from D:/DEVCPP/include/c++/mingw32/bits/c++io.h:37,
5 
6 from D:/DEVCPP/include/c++/bits/fpos.h:44,
7 from D:/DEVCPP/include/c++/bits/char_traits.h:46,
8 from D:/DEVCPP/include/c++/string:47,
9 from C:/CPP/edittext.cpp:3:
10D:/DEVCPP/include/wingdi.h:1181: conflicting types for `typedef struct
11 
12 tagBITMAP BITMAP'
13D:/DEVCPP/include/allegro/gfx.h:234: previous declaration as `typedef struct
14 BITMAP BITMAP'
15 
16C:/CPP/edittext.cpp: In function `int _mangled_main()':
17C:/CPP/edittext.cpp:16: cannot convert `BITMAP*' to `tagBITMAP*' in
18 initialization
19
20C:/CPP/edittext.cpp:80: cannot convert `tagBITMAP*' to `BITMAP*' for argument `
21 1' to `void clear(BITMAP*)'
22
23C:/CPP/edittext.cpp:83: cannot convert `tagBITMAP*' to `BITMAP*' for argument `
24 1' to `void textout(BITMAP*, const FONT*, const char*, int, int, int)'
25C:/CPP/edittext.cpp:86: cannot convert `tagBITMAP*' to `BITMAP*' for argument `
26 1' to `void textprintf(BITMAP*, const FONT*, int, int, int, const char*,
27 ...)'
28C:/CPP/edittext.cpp:87: cannot convert `tagBITMAP*' to `BITMAP*' for argument `
29 1' to `void textprintf(BITMAP*, const FONT*, int, int, int, const char*,
30 ...)'
31C:/CPP/edittext.cpp:88: cannot convert `tagBITMAP*' to `BITMAP*' for argument `
32 1' to `void textprintf(BITMAP*, const FONT*, int, int, int, const char*,
33 ...)'
34C:/CPP/edittext.cpp:90: cannot convert `tagBITMAP*' to `BITMAP*' for argument `
35 1' to `void textout(BITMAP*, const FONT*, const char*, int, int, int)'
36C:/CPP/edittext.cpp:92: cannot convert `tagBITMAP*' to `BITMAP*' for argument `
37 1' to `void textout(BITMAP*, const FONT*, const char*, int, int, int)'
38C:/CPP/edittext.cpp:95: cannot convert `tagBITMAP*' to `BITMAP*' for argument `
39
40 1' to `void vline(BITMAP*, int, int, int, int)'
41C:/CPP/edittext.cpp:98: cannot convert `tagBITMAP*' to `BITMAP*' for argument `
42 1' to `void blit(BITMAP*, BITMAP*, int, int, int, int, int, int)'
43C:/CPP/edittext.cpp:103: cannot convert `tagBITMAP*' to `BITMAP*' for argument
44 `1' to `void destroy_bitmap(BITMAP*)'
45
46Execution terminated

flares
Member #3,463
April 2003
avatar

by the tagBitmap stuff, i think that you need to hide the windows headers from the compiler using some flag define in the FAQ, hold on
...
(getting info)
...
-D__GTHREAD_HIDE_WIN32API

i think that should work :D

edit:
you add that flag to the compiler, in devcpp
project->options->parameters

add it there

[nonnus29]Plus the api is crap ... I'd rather chew broken glass then code with those.

OICW
Member #4,069
November 2003
avatar

Thank you Miran for your utility, I probably use it.

2 Basix 9: I've got same errors when I wanted to compile it, it needs repair, I think, that I'll try to write my own algorythm.

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

23yrold3yrold
Member #1,134
March 2001
avatar

flares is right; you need to use that compiler flag.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

OICW
Member #4,069
November 2003
avatar

Ok that's it. I copied the code to new source file but I didn't make a project. ;D I'll try to implement it in my game (it's done from 40%) and when I have first beta I'll post it here.

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

Tobias Dammers
Member #2,604
August 2002
avatar

For making your own fonts: Get ttf2pcx, create a font (any font), and closely observe the resulting pcx bitmap. Your font should then look the same, you can draw it onto one big pcx. Note that fonts are always 8 bpp, the border around the glyphs is color #255, and the mask color for within glyphs is #0. Glyphs are variable-width, but should all be the same height. Every color from #1 to #254 is free for your use, and will be replaced by the text foreground color, except when that is set to something < 0, in which case the original colors are used.
And of course, fonts are palette dependend, and you need to save the palette separately (as with all 8bpp bitmaps).
--- EDIT ---
Oh yes, and for text input: Why not use the gui functions? I'm sure you could bash out a simple text input box within 15 minutes...

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

mEmO
Member #1,124
March 2001
avatar

Quote:

Oh yes, and for text input: Why not use the gui functions? I'm sure you could bash out a simple text input box within 15 minutes...

You mean the allegro standard GUI? I'm pretty sure it's faster to write a custom made function, and better too. Unless you use one of the many addon libs out there, of course.

---------------------------------------------
There is only one God, and Connor is his son!
http://www.memocomputers.com
Happy birthday!

Thomas Fjellstrom
Member #476
June 2000
avatar

nah. the Allegro gui us uber simple. a quick DIALOG struct with a entry will take barely a few minutes to layout.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: