Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » allegro and message boxes

This thread is locked; no one can reply to it. rss feed Print
allegro and message boxes
jimmy james
Member #6,105
August 2005

I just started using allegro,and this is my first step into game/graphical programming. I never used c, only c++. Ok now that thats out of the way, im trying to learn the ins and outs of allegro and basic graphics stuff in general. I just got allegro a couple weeks ago, and im just trying out everything. My first game im building is just a simple game i made in c++ that will stay pretty much textual. I learned to use algif to add a gif to my game(as an icon for each player). What im looking for is how to make a message/dialog box pop up and ask for the players name. I have absolutely no clue how to do this, as ive never used visual c++ or anything like that. Is this even possible, and is it worth it? If its overly difficult let me know, if not can someone post some code of how to do this? Thanks.

Also is there a way to make my gif (animated) run in the background. Right now i have to choose between my gif moving or my game continuing. It waits for the gif animation to complete then continues. Anything i can do about that? Thanks

Frank Drebin
Member #2,987
December 2002
avatar

- you know that you don't have to use c with allegro you can also stay with c++
- allegro has some dialog-functions (see manual/example) but i never used them and i think it's easier to make your own ones but perhaps you give them a try
- i'd store the animation in different image files and animate them on my own

Derezo
Member #1,666
April 2001
avatar

Here is 23yrold3yrold's input box code from this thread. There is also a C version there.

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()

"He who controls the stuffing controls the Universe"

jimmy james
Member #6,105
August 2005

ok i guess i wasnt clear enough. I have no problem getting input from the user. I use gstream in place of my cin and cout. I wanted to accomplish the input in a different means tho, from a popup box. A box that pops up and says "Enter Name of Player 1:" then has a textbox to take the input, you know what im sayin? Many games have them to input the name of the player if he/she get the hi score etc etc... Something like that, not just getting basic input in the window, i want a SEPERATE window to popup and get the input and have that input placed into my name string.

ReyBrujo
Moderator
January 2001
avatar

I don't remember any straight function. You would need to create your own resource with the popup, the buttons and the place where the user would write the test, and handle the different events.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

jimmy james
Member #6,105
August 2005

but how do i make the popup window? If someone could tell me how to make the popup window, i could just use the allegro stuff and gstream to get the input, and figure out the textbox in the window later, baby steps. But i have no clue how to make a popup allegro window from my original allegro window.

Derezo
Member #1,666
April 2001
avatar

You really answered you own question. Just use the allegro stuff and gstream to get the input.

1char *Popup(BITMAP *target, int x, int y, const char *text, char *var, int limit)
2 {
3 // draw the box
4 rectfill(target,x,y,x+300,y+60,makecol(255,255,100));
5 textprintf(target,font,x+10,y+10,0,"%s",text); // note: not 4.2.x friendly
6 
7 while(!key[KEY_ENTER])
8 {
9 // text input code here, goes into char *var
10 }
11 
12 return var;
13 }
14 
15// then used like so:
16char name[20];
17Popup(screen,SCREEN_W/2-150,SCREEN_H/2-30,"Enter your name:",name,20); // replace 'screen' with the target bitmap for the popup.

"He who controls the stuffing controls the Universe"

jimmy james
Member #6,105
August 2005

your code doesnt make a popup window, after working with it for a while i got a window to appear, but i had to put allegro_init() and set_gfx_mode in the popup function, but how do i destroy the window? After the window opens everything freezes. Does anyone know how to do this effectively?

Jonny Cook
Member #4,055
November 2003

Quote:

your code doesnt make a popup window, after working with it
for a while i got a window to appear, but i had to put
allegro_init() and set_gfx_mode in the popup function, but how do i
destroy the window?

You probably should have already called them at the beggining of the program. Not sure if this was what you are trying to accomplish, but I've made an example.

1#include <allegro.h>
2 
3int main() {
4 allegro_init();
5 install_keyboard();
6
7 set_color_depth(16);
8 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
9
10 BITMAP *buffer = create_bitmap(SCREEN_W, SCREEN_H);
11
12 bool done = false;
13 bool prompt = true;
14 bool pressed = false;
15
16 const int length = 256;
17 int prompt_x = SCREEN_W/2 - 150;
18 int prompt_y = SCREEN_H/2 - 25;
19 int letter_width = text_length(font, " "); // width of the caret
20 
21 char str[length];
22 int caret = 0;
23
24 while (!done) {
25 // logic
26 if (key[KEY_ESC]) {
27 done = true;
28 }
29
30 if (key[KEY_TAB]) {
31 if (!pressed) {
32 prompt ^= 1; // toggle the prompt box
33 pressed = true;
34 }
35 }
36 else {
37 pressed = false;
38 }
39 
40 if (prompt) {
41 if (keypressed()) {
42 int k = readkey();
43 char letter = k & 0xFF;
44 int code = k >> 8;
45 
46 if (code == KEY_BACKSPACE) {
47 str[caret - 1] = 0;
48 caret = MAX(0, caret - 1);
49 }
50 else if (code != KEY_TAB) {
51 str[caret] = letter;
52 str[caret + 1] = 0;
53 caret ++;
54 }
55 }
56 }
57 
58
59 // drawing
60 clear_to_color(buffer, makecol(255, 255, 255));
61 if (prompt) {
62 rectfill(buffer, prompt_x, prompt_y, prompt_x + 300, prompt_y + 50, makecol(255, 200, 200));
63 textout_ex(buffer, font, "Please enter your name.", prompt_x + 10, prompt_y + 50/2 - 20, makecol(0, 0, 0), -1);
64 textout_ex(buffer, font, str, prompt_x + 10, prompt_y + 50/2, makecol(0, 0, 0), -1);
65 
66 hline(buffer, prompt_x + 10 + caret*letter_width, prompt_y + 50/2 + 10, prompt_x + 10 + letter_width + caret*letter_width, makecol(0, 0, 0));
67 }
68
69 blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
70 }
71 
72 destroy_bitmap(buffer);
73 return 0;
74}
75END_OF_MAIN();

Hope that helps.

The face of a child can say it all, especially the mouth part of the face.

jimmy james
Member #6,105
August 2005

i attatched a photo of my screen. This is what im looking for, just want it to work. Mine wont work, after it gets to where its at it freezes and i have to go to program reset. If anyone has done this can you post a how to on how to make this second window work. Thanks

Jonny Cook
Member #4,055
November 2003

Ohhh, you want it to open in a new window? I wasn't even aware you could do something like that. But really, is that even necessary? I think it just complicates things. Plus that could never work in DOS.

[edit]
Can I see your code? I want to know what you're doing.

The face of a child can say it all, especially the mouth part of the face.

jimmy james
Member #6,105
August 2005

ok heres the code. Ive been messing with it since ive been home and i got it to work. If you want to run my code you need gstream. The only problem i have is for this to work the initial window is useless after i call the new window. So i have to close the window, open the new one, then open another one after that one. So it starts with a small window, it dissapears and goes to a small one that asks your name, and places it into a string. then that one closes and a window identical in size and position to the first window opens and displays the name you entered. Id like to find out how to do this but not have to close the first window, a way to go back to the original window. Does anyone know or ever done this before? Anyway heres my code

[CODE]
#include <allegro.h>
#include <gstream>

string Popup();
int main()
{
// Initialize Allegro.
allegro_init();
// Set the resolution to 640 by 480 with SAFE autodetection.
set_gfx_mode(GFX_SAFE, 640, 480, 0, 0);

// Installing the keyboard handler.
install_keyboard();

textout(screen, font, "This is the initial window!", 1, 1, 10);
textout(screen, font, "Press any key to continue...", 1, 12, 11);

readkey();
remove_keyboard();
allegro_exit();
string p1;
p1 = Popup();
allegro_init();
install_keyboard();
set_gfx_mode(GFX_SAFE, 640, 480, 0, 0);
text_mode(-1);
gstream gs;
textout(screen, font, "Hey look at that, this stuff worked!", 1, 1, 10);
gs.goto_xy(1,11);
gs << "Your name is " << p1 << endl;
gs << "press ESC to quit";
gs << flush;


//Looping until the ESCape key is pressed.
while(! key[KEY_ESC])
poll_keyboard(); // This shouldn't be necessary in Windows.
readkey();
// Exit program.
allegro_exit();

return 0;
}

// Some Allegro magic to deal with WinMain().
END_OF_MAIN();

string Popup()
{
// Initialize Allegro.
allegro_init();

// Set the resolution to 640 by 480 with SAFE autodetection.
set_gfx_mode(GFX_SAFE, 300, 200, 0, 0);

install_keyboard();
gstream gs;
string name;
// Printing text to the screen.
gs << "Enter Your Name: ";
getline(gs, name);
gs.goto_xy(0,190);
gs << "Press any key to continue...";
gs << flush;
readkey();
clear_keybuf();
remove_keyboard();
allegro_exit();

return name;
}[/CODE]

Jonny Cook
Member #4,055
November 2003

Quote:

Id like to find out how to do this but not have to close the first window, a way to go back to the original window

I don't think Allegro supports that, as it would not be possible under DOS.

The face of a child can say it all, especially the mouth part of the face.

Kitty Cat
Member #2,815
October 2002
avatar

1char player_name[64];
2 
3DIALOG name_getter[] = {
4 { d_box_proc, 0, 0, 320, 64, 0, ~0, 0, 0, 0, 0, NULL },
5 { d_text_proc, 4, 4, 0, 0, 0, ~0, 0, 0, 0, 0, "Enter Player Name:" },
6 { d_edit_proc, 4, 16, 312, 12, 0, ~0, 0, D_EXIT, sizeof(player_name), 0, player_name },
7 { d_yield_proc },
8 { NULL }
9};
10 
11int funct()
12{
13 ...
14 center_dialog(name_getter);
15 popup_dialog(name_getter, 2);
16 ...
17}

More or less...

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Rick
Member #3,572
June 2003
avatar

You can only create the one window with allegro. You will have to create you own "window". This isn't like MS Windows programming where you can create multiple windows. You can use allegro's gui code, or create your own "window" system.

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

jimmy james
Member #6,105
August 2005

i got it all to work great, except the i had to exit the old window, so it starts with a welcome screen window thingy, then closes and opens a new window and asks for name of player one, then closes and opens another window that asks for player 2's name, then closes and opens another window where the actual game takes place, i like it better this way than creating a bitmap inside my window and using that as a "window", but for most apps my way isnt at all usefull.

Go to: