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 ) ?
I remmeber something called gstream being used for text input.
string szInput; if (keypressed()) { szInput += readkey() & 0xff; }
C++:
1 | // edittext.cpp |
2 | #include <allegro.h> |
3 | #include <string> |
4 | using namespace std; |
5 | |
6 | #define WHITE makecol(255, 255, 255) |
7 | |
8 | int 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 | } |
108 | END_OF_MAIN() |
C:
1 | // edittext.c |
2 | #include <allegro.h> |
3 | |
4 | #define BUFFERSIZE 128 |
5 | |
6 | int 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 | } |
57 | END_OF_MAIN() |
And yes, you can use different fonts, but I'll leave that to someone who knows more about how than me
You didn't leave him much work to do
Hey, give me some credit; I completely ignored the second question!
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
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
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?
[plug]Allegro Font Editor[/plug]
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.
1 | In 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: |
10 | D:/DEVCPP/include/wingdi.h:1181: conflicting types for `typedef struct |
11 | |
12 | tagBITMAP BITMAP' |
13 | D:/DEVCPP/include/allegro/gfx.h:234: previous declaration as `typedef struct |
14 | BITMAP BITMAP' |
15 | |
16 | C:/CPP/edittext.cpp: In function `int _mangled_main()': |
17 | C:/CPP/edittext.cpp:16: cannot convert `BITMAP*' to `tagBITMAP*' in |
18 | initialization |
19 | |
20 | C:/CPP/edittext.cpp:80: cannot convert `tagBITMAP*' to `BITMAP*' for argument ` |
21 | 1' to `void clear(BITMAP*)' |
22 | |
23 | C:/CPP/edittext.cpp:83: cannot convert `tagBITMAP*' to `BITMAP*' for argument ` |
24 | 1' to `void textout(BITMAP*, const FONT*, const char*, int, int, int)' |
25 | C:/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 | ...)' |
28 | C:/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 | ...)' |
31 | C:/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 | ...)' |
34 | C:/CPP/edittext.cpp:90: cannot convert `tagBITMAP*' to `BITMAP*' for argument ` |
35 | 1' to `void textout(BITMAP*, const FONT*, const char*, int, int, int)' |
36 | C:/CPP/edittext.cpp:92: cannot convert `tagBITMAP*' to `BITMAP*' for argument ` |
37 | 1' to `void textout(BITMAP*, const FONT*, const char*, int, int, int)' |
38 | C:/CPP/edittext.cpp:95: cannot convert `tagBITMAP*' to `BITMAP*' for argument ` |
39 | |
40 | 1' to `void vline(BITMAP*, int, int, int, int)' |
41 | C:/CPP/edittext.cpp:98: cannot convert `tagBITMAP*' to `BITMAP*' for argument ` |
42 | 1' to `void blit(BITMAP*, BITMAP*, int, int, int, int, int, int)' |
43 | C:/CPP/edittext.cpp:103: cannot convert `tagBITMAP*' to `BITMAP*' for argument |
44 | `1' to `void destroy_bitmap(BITMAP*)' |
45 | |
46 | Execution terminated |
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
edit:
you add that flag to the compiler, in devcpp
project->options->parameters
add it there
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.
flares is right; you need to use that compiler flag.
Ok that's it. I copied the code to new source file but I didn't make a project. 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.
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...
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.
nah. the Allegro gui us uber simple. a quick DIALOG struct with a entry will take barely a few minutes to layout.