-
Save me Allegroites...
Okay, I'm learning Allegro, and I've run into something annoying... this code works like I want it to...
Code:
#include <allegro.h>
int main ()
{
int i;
allegro_init();
install_keyboard();
ureadkey(&i);
allegro_message ("%d ", i);
return 0;
}
and this code doesn't...
Code:
#include <allegro.h>
int main ()
{
int i;
allegro_init();
install_keyboard();
for (;;)
{
ureadkey(&i);
allegro_message ("%d ", i);
}
return 0;
}
Yes, I know that this can be done without Allegro, but learning Allegro's kinda the point here. Basically, I can sit there all day hitting keys on the latter code, and never get any output. Is there any way to make this work? Maybe some way to flush the output buffer? Am using DJGPP, btw.
-
Hmm, that code works absolutely fine in Windows... so here's a couple things you can troubleshoot:
Some OS's (maybe DOS) need you to poll the keyboard. Stick:
poll_keyboard()
in your loop and see if that fixes it.
If not , you can see for sure if the trouble is in ureadkey(&i); or allegro_message ("%d ", i); by eliminating ureadkey. Add the simulate_ukeypress(int key, int scancode); function to send a key right to ureadkey(). If the problem persists, then post again and we'll troubleshoot some more. :)
-Justin
-
Perhaps I should have mentioned, I am running this in a DOS box in Windows 98. Here's the new code (which doesn't work)....
Code:
#include <allegro.h>
#include <stdio.h>
int main ()
{
int i;
allegro_init();
install_keyboard();
printf ("%d ", keyboard_needs_poll()); // Prints 0
for (;;)
{
poll_keyboard();
ureadkey(&i);
allegro_message ("%d ", i);
}
return 0;
}
The problem really seems to be the the output buffer never flushes itself on my computer (thanks Salem, for the wisdom). For example, the following code works...
Code:
#include <allegro.h>
#include <stdio.h>
int main ()
{
int i;
allegro_init();
install_keyboard();
for (;;)
{
ureadkey(&i);
// I'm guessing that the \n dumps the buffer.
allegro_message ("%d\n", i);
}
return 0;
}
The library I'm using is alleg.a, if that helps.
-
Well, it doesn't matter if it is running in a DOS box on 98 or in true DOS mode, DJGPP creates true DOS programs. That's why I mentioned that allegro_message() does work in Windows (mingw32). For DOS, allegro_message() is just wrapping up a standard C function, so you might try something like flush(). Really that function was to make outputting quick and dirty text in Windows apps easy (not that it is hard anyway).
Anyway, that really isn't what Allegro is for. You should really put it into a graphic mode and use any of these:
textout()
textout_centre()
textout_right()
textout_justify()
textprintf()
textprintf_centre()
textprintf_right()
textprintf_justify()
(See docs for more.)
Here is a better test program:
PHP Code:
#include <allegro.h>
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();
// Printing text to the screen.
textout(screen, font, "Hello World!", 1, 1, 10);
textout(screen, font, "Press ESCape to quit.", 1, 12, 11);
// Looping until the ESCape key is pressed.
while(! key[KEY_ESC])
poll_keyboard(); /* This may not be necessary, there is a flag to check. */
// Exit program.
allegro_exit();
return 0;
}
// Some Allegro magic for platform compatibility and timing.
END_OF_MAIN();