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 | |
| 3 | char value[5] = "0"; |
| 4 | |
| 5 | int change_value(void *dp3, int d2) |
| 6 | { |
| 7 | itoa(d2, value, 10); |
| 8 | } |
| 9 | |
| 10 | DIALOG 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 | |
| 23 | int 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 | } |
| 36 | END_OF_MAIN() |
What am I doing wrong?
That code looks perfect to me. Maybe see what printf("%d\n", d2); outputs inside of change_value?
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?
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?
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?
Looks like yours isn't a new problem, try reading here.
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?
Also, how do I send another draw message to d_text_proc?
object_message(&the_dialog[2], MSG_DRAW, 0);
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:
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.
No, I removed the (void*) before change_value in the_dialog, but now I get this error:
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.
It works perfectly now.
Awesome8-)