Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Sliders and Text in GUI

Credits go to ImLeftFooted for helping out!
This thread is locked; no one can reply to it. rss feed Print
Sliders and Text in GUI
moon_rabbits
Member #8,469
March 2007
avatar

I'm fooling around with the Allegro GUI. I quickly wrote up a program that was supposed to show a slider, and beside the slider it should show the value of the slider. It works fine, except the d_text_proc value remains at 0 no matter where the slider is positioned. Here's the source:

1#include <allegro.h>
2 
3char value[5] = "0";
4 
5int change_value(void *dp3, int d2)
6{
7 itoa(d2, value, 10);
8}
9 
10DIALOG the_dialog[] =
11{
12 /* (dialog proc) (x) (y) (w) (h) (fg) (bg) (key) (flags) (d1) (d2) (dp) (dp2) (dp3) */
13
14 { d_clear_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
15
16 { d_slider_proc, 10, 10, 16, 100, 0, 0, 0, 0, 100, 0, NULL, (void*)change_value, NULL },
17
18 { d_text_proc, 25, 50, 0, 0, 0, 0, 0, 0, 0, 0, (void*)value, NULL, NULL },
19
20 { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL }
21};
22
23int main() {
24 allegro_init();
25 set_color_depth(16);
26 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
27 install_keyboard();
28 install_mouse();
29 gui_fg_color = makecol(0, 0, 0);
30 gui_mg_color = makecol(128, 128, 128);
31 gui_bg_color = makecol(230, 220, 210);
32 set_dialog_color (the_dialog, gui_fg_color, gui_bg_color);
33 do_dialog(the_dialog, -1);
34 return 0;
35}
36END_OF_MAIN()

What am I doing wrong?

ImLeftFooted
Member #3,935
October 2003
avatar

That code looks perfect to me. Maybe see what printf("%d\n", d2); outputs inside of change_value?

moon_rabbits
Member #8,469
March 2007
avatar

I placed a call to printf() inside change_value() and on the console the slider's current value displayed fine, however the graphical representation did not change from 0.

Also, whenever I click and drag the slider, the instant I release the program exits despite the fact that D_EXIT is not a flag.

Help?

ImLeftFooted
Member #3,935
October 2003
avatar

What compiler / IDE are you using? Are you comfortable with gdb?

As for the not updating problem, maybe the d_text_proc needs to receive a new draw message?

moon_rabbits
Member #8,469
March 2007
avatar

I'm using Dev-Cpp, and I've never touched gdb in my life.

Also, how do I send another draw message to d_text_proc?

Rampage
Member #3,035
December 2002
avatar

Looks like yours isn't a new problem, try reading here.

-R

ImLeftFooted
Member #3,935
October 2003
avatar

Rampage said:

Looks like yours isn't a new problem, try reading here.

We've established the function is getting called via printf, maybe this problem is a different one?

You said:

Also, how do I send another draw message to d_text_proc?

object_message(&the_dialog[2], MSG_DRAW, 0);

You said:

I'm using Dev-Cpp, and I've never touched gdb in my life.

You'll have to get mighty comfortable with it eventually.. Its a tool that helps you answer questions like this one:

You said:

Also, whenever I click and drag the slider, the instant I release the program exits despite the fact that D_EXIT is not a flag.

moon_rabbits
Member #8,469
March 2007
avatar

No, I removed the (void*) before change_value in the_dialog, but now I get this error:

Quote:

22 invalid conversion from `int (*)(void*, int)' to `void*'

And so I added the (void*) back to the callback to change_value.

I tried to send a message to redraw the d_text_proc like this:

d_text_proc(D_REDRAW, the_dialog, 2);

But, if I call that inside change_value, it does not work because the_dialog is declared and defined after change_value is defined. If I place the_dialog's definition before change_value, I cannot compile because change_value is not defined before the_dialog, where it is called. :(

EDIT: The problem is fixed, I declared change_value and defined it after the_dialog, and used the object_message() function that Dustin suggested. It works perfectly now.

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

It works perfectly now.

Awesome8-)

Go to: