Save me Allegroites...

This is a discussion on Save me Allegroites... within the Game Programming forums, part of the General Programming Boards category; Okay, I'm learning Allegro, and I've run into something annoying... this code works like I want it to... Code: #include ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    752

    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.
    Callou collei we'll code the way
    Of prime numbers and pings!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    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
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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.
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    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_SAFE64048000);

     
    // Installing the keyboard handler.
     
    install_keyboard();

     
    // Printing text to the screen.
     
    textout(screenfont"Hello World!"1110);
     
    textout(screenfont"Press ESCape to quit."11211);

     
    // 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(); 
    Last edited by Justin W; 12-31-2001 at 12:46 PM.
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save vs Save As.... OPENFILENAME flags
    By csonx_p in forum Windows Programming
    Replies: 16
    Last Post: 06-01-2008, 03:42 PM
  2. save and save as functions
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 06-29-2005, 03:49 AM
  3. save webpage? some save some not
    By kryptkat in forum Tech Board
    Replies: 3
    Last Post: 06-07-2005, 10:21 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Major glitch losing source files with MSVC++ and WinXP
    By JasonD in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2003, 01:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21