Qbasicnews.com

Full Version: Problems with the Allegro mouse routines.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using FreeBASIC 0.11 for MS-DOS.

Install_Mouse fails (returns -1) whenever I try using it. I've tried it from within Windows, and from plain, vanilla MS-DOS with the CuteMouse mouse driver. It also causes a segment fault sometimes (seems to be random. I don't quite understand it.)

The specs of the system I'm programming on... hmm, AMD K6 200 MHz; GeForce 4 MX440 (found it in a pile o' junk - wow); 64 MB SIMM RAM; PS/2 mouse port, but an AT-style keyboard plug. Weird computer.

Any help would be very... uh... helpful. :-)
I have no idea... Is your mouse wireless? Just a thought.
Hmm... I got Install_Mouse working fine (returns > 0 now, which should indicate the number of buttons available) -- apparently, calling Install_Timer first solved my problem. But now Show_Mouse doesn't work, and randomly crashes (for example, it'll run fine once, but not again, until I recompile the program for some reason. It's a little bit weird.)

Can anyone type up a quick mouse test in Allegro (for FB-DOS) and see if it works? If so/if not, send me the test and I'll compare it to what I'm doing -- maybe I broke it :wink:

Edit: no, my mouse isn't wireless -- it's a cheap, Microsoft PS/2 mouse that I stole from school. Shhhh, I was broke. :lol:
No clue, it could be an argument wrongly declared (the headers are W.I.P.), or then, if i remember, Allegro for DOS needed those LOCK() something with vars updated inside ISR's, i never used Allegro so, where is DrV?!? heh..

I will try to port an Allegro mouse test, if there's one, er..

Edit: here is the mouse test, it worked here, both for Windows and DOS (inside XP):

Code:
''
''    Example program for the Allegro library, by Shawn Hargreaves.
''
''    This program demonstrates how to get mouse input.
''

option explicit

#include "allegro.bi"


       dim mickeyx as integer
       dim mickeyy as integer
       dim custom_cursor as BITMAP ptr
       dim c as integer

       allegro_init
       install_keyboard
       install_timer
    if (set_gfx_mode(GFX_SAFE, 320, 240, 0, 0) <> 0) then
        end 1
    end if
  
    'set_palette( desktop_palette )
       text_mode(makecol(255, 255, 255))
       clear_to_color(al_screen, makecol(255, 255, 255))

       '' Detect mouse presence
       if( install_mouse() < 0 ) then
        textout_centre(al_screen, font, "No mouse detected, but you need one!", _
                        SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0))
          readkey()
          end 1
       end if
  
       do
          '' On most platforms (eg. DOS) things will still work correctly
          '' without this call, but it is a good idea to include it in any
          '' programs that you want to be portable, because on some platforms
          '' you may not be able to get any mouse input without it.
          ''
          poll_mouse()

          acquire_screen()

          '' the mouse position is stored in the variables mouse_x and mouse_y
          textout(al_screen, font, "mouse_x =" + str$(mouse_x), 16, 48, makecol(0, 0, 0))

          textout(al_screen, font, "mouse_y =" + str$(mouse_y), 16, 64, makecol(0, 0, 0))

          '' or you can use this function to measure the speed of movement.
          '' Note that we only call it every fourth time round the loop:
          '' there's no need for that other than to slow the numbers down
          '' a bit so that you will have time to read them...
          ''
        c = c + 1
          if ((c and 3) = 0) then
             get_mouse_mickeys(@mickeyx, @mickeyy)
        end if

          textout(al_screen, font, "mickey_x =" + str$(mickeyx), 16, 88, makecol(0, 0, 0))

          textout(al_screen, font, "mickey_y =" + str$(mickeyy), 16, 104, makecol(0, 0, 0))

          '' the mouse button state is stored in the variable mouse_b
          if (mouse_b and 1) then
             textout(al_screen, font, "left button is pressed ", 16, 128, makecol(0, 0, 0))
          else
             textout(al_screen, font, "left button not pressed", 16, 128, makecol(0, 0, 0))
         end if

          if (mouse_b and 2) then
             textout(al_screen, font, "right button is pressed ", 16, 144, makecol(0, 0, 0))
          else
             textout(al_screen, font, "right button not pressed", 16, 144, makecol(0, 0, 0))
         end if

          if (mouse_b and 4) then
             textout(al_screen, font, "middle button is pressed ", 16, 160, makecol(0, 0, 0))
          else
             textout(al_screen, font, "middle button not pressed", 16, 160, makecol(0, 0, 0))
         end if

          '' the wheel position is stored in the variable mouse_z
          textout(al_screen, font, "mouse_z =" + str$(mouse_z), 16, 184, makecol(0, 0, 0))

          release_screen()

          vsync()

       loop while( keypressed() = 0 )

       clear_keybuf()

       ''  To display a mouse pointer, call show_mouse(). There are several
    ''  things you should be aware of before you do this, though. For one,
    ''  it won't work unless you call install_timer() first. For another,
    ''  you must never draw anything onto the al_screen while the mouse
    ''  pointer is visible. So before you draw anything, be sure to turn
    ''  the mouse off with show_mouse(NULL), and turn it back on again when
    ''  you are done.
    ''
       clear_to_color(al_screen, makecol(255, 255, 255))
       textout_centre(al_screen, font, "Press a key to change cursor", _
          SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0))
       show_mouse(al_screen)
       readkey()
       show_mouse(NULL)

       '' create a custom mouse cursor bitmap...
       custom_cursor = create_bitmap(32, 32)
       clear_to_color(custom_cursor, bitmap_mask_color(al_screen))
       for c= 0 to 8-1
        draw_circle(custom_cursor, 16, 16, c*2, palette_color[c])
    next c

       '' select the custom cursor and set the focus point to the middle of it
       set_mouse_sprite(custom_cursor)
       set_mouse_sprite_focus(16, 16)

       clear_to_color(al_screen, makecol(255, 255, 255))
       textout_centre(al_screen, font, "Press a key to quit", SCREEN_W/2, _
                     SCREEN_H/2, makecol(0, 0, 0))
       show_mouse(al_screen)
       readkey()
       show_mouse(NULL)

       destroy_bitmap(custom_cursor)

    end 0
To v3cz0r (and also, to DrV as well! Big Grin ):

To start off, v3cz0r and DrV, according to the “allegro.bi” file in the Windows FB 0.11b distribution, it had an error in it which one of the routines is incorrectly coded which was this: during the get_mouse_mickeys statement in the Declare Sub, there was supposed to be a quote mark ( " ) there.

Let me show you what I am talking about: in the “allegro.bi” file, be sure to find this:

Code:
Declare Sub get_mouse_mickeys CDecl Alias "get_mouse_mickeys) (ByRef mickeyx As Integer, ByRef mickeyy As Integer)

.......and replace with this:

Code:
Declare Sub get_mouse_mickeys CDecl Alias "get_mouse_mickeys" (ByRef mickeyx As Integer, ByRef mickeyy As Integer)

That way, the little stumbling block will be clear and out of the way for you! Big Grin

And v3cz0r, when I just today tested your ported code that was presented right here in this thread, I found some bugs: 1) the numbers of the mouse coordinates did not have a space mark or two inputted in the code, causing some confusion in the reading of them at runtime; and 2) you forgot by mistake to call allegro_exit right near the end of your code, causing an apparent lock-up of the keyboard and worse. Sad Gotta remember those little things, man. Smile ! With that in mind, here now is the fixed version of your ported code and which I so joyfully corrected the problems for you!


[syntax="FreeBasic"]''
'' Example program for the Allegro library, by Shawn Hargreaves.
''
'' This program demonstrates how to get mouse input.
''

'' Ported to FreeBASIC by v1ctor, with minor corrections by
'' Adigun A. Polack.

option explicit

'$include: "allegro.bi"


dim mickeyx as integer
dim mickeyy as integer
dim custom_cursor as BITMAP ptr
dim c as integer

allegro_init
install_keyboard
install_timer
if (set_gfx_mode(GFX_SAFE, 320, 240, 0, 0) <> 0) then
end 1
end if

'set_palette( desktop_palette )
text_mode(makecol(255, 255, 255))
clear_to_color(al_screen, makecol(255, 255, 255))

'' Detect mouse presence
if( install_mouse() < 0 ) then
textout_centre(al_screen, font, "No mouse detected, but you need one!", SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0))
readkey()
end 1
end if

do
'' On most platforms (eg. DOS) things will still work correctly
'' without this call, but it is a good idea to include it in any
'' programs that you want to be portable, because on some platforms
'' you may not be able to get any mouse input without it.
''
poll_mouse()

acquire_screen()

'' the mouse position is stored in the variables mouse_x and mouse_y
textout(al_screen, font, "mouse_x =" + str$(mouse_x) + " ", 16, 48, makecol(0, 0, 0))

textout(al_screen, font, "mouse_y =" + str$(mouse_y) + " ", 16, 64, makecol(0, 0, 0))

'' or you can use this function to measure the speed of movement.
'' Note that we only call it every fourth time round the loop:
'' there's no need for that other than to slow the numbers down
'' a bit so that you will have time to read them...
''
c = c + 1
if ((c and 3) = 0) then
get_mouse_mickeys(mickeyx, mickeyy)
end if

textout(al_screen, font, "mickey_x =" + str$(mickeyx) + " ", 16, 88, makecol(0, 0, 0))

textout(al_screen, font, "mickey_y =" + str$(mickeyy) + " ", 16, 104, makecol(0, 0, 0))

'' the mouse button state is stored in the variable mouse_b
if (mouse_b and 1) then
textout(al_screen, font, "left button is pressed ", 16, 128, makecol(0, 0, 0))
else
textout(al_screen, font, "left button not pressed", 16, 128, makecol(0, 0, 0))
end if

if (mouse_b and 2) then
textout(al_screen, font, "right button is pressed ", 16, 144, makecol(0, 0, 0))
else
textout(al_screen, font, "right button not pressed", 16, 144, makecol(0, 0, 0))
end if

if (mouse_b and 4) then
textout(al_screen, font, "middle button is pressed ", 16, 160, makecol(0, 0, 0))
else
textout(al_screen, font, "middle button not pressed", 16, 160, makecol(0, 0, 0))
end if

'' the wheel position is stored in the variable mouse_z
textout(al_screen, font, "mouse_z =" + str$(mouse_z) + " ", 16, 184, makecol(0, 0, 0))

release_screen()

vsync()

loop while( keypressed() = 0 )

clear_keybuf()

'' To display a mouse pointer, call show_mouse(). There are several
'' things you should be aware of before you do this, though. For one,
'' it won't work unless you call install_timer() first. For another,
'' you must never draw anything onto the al_screen while the mouse
'' pointer is visible. So before you draw anything, be sure to turn
'' the mouse off with show_mouse(NULL), and turn it back on again when
'' you are done.
''
clear_to_color(al_screen, makecol(255, 255, 255))
textout_centre(al_screen, font, "Press a key to change cursor", SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0))
show_mouse(al_screen)
readkey()
show_mouse(NULL)

'' create a custom mouse cursor bitmap...
custom_cursor = create_bitmap(32, 32)
clear_to_color(custom_cursor, bitmap_mask_color(al_screen))
for c= 0 to 8-1
draw_circle(custom_cursor, 16, 16, c*2, palette_color[c])
next c

'' select the custom cursor and set the focus point to the middle of it
set_mouse_sprite(custom_cursor)
set_mouse_sprite_focus(16, 16)

clear_to_color(al_screen, makecol(255, 255, 255))
textout_centre(al_screen, font, "Press a key to quit", SCREEN_W/2, SCREEN_H/2, makecol(0, 0, 0))
show_mouse(al_screen)
readkey()
show_mouse(NULL)

destroy_bitmap(custom_cursor)

'' This command *must* be called before the end of your program to avoid
'' problems and other worse stuff! ;*)

allegro_exit

'' Now, let's wrap it up!! ^_-=b !

end 0[/syntax]

I have tested this newly-adjusted code on both FB Windows and FBdos, v3cz0r, and found that the code really compiled and ran *so* smoothly well, mouse coordinates, mickeys, and all (truly so too for Windows so long as the little error that I told you and DrV about in the “allegro.bi” file is first of all fixed in the way that I instructed from near the top of this post)! Plus, the coordinates should now be more readable, too!! Cool=b

With all of that said and covered, I thank you so richly once again, v3cz0r and DrV, for your wonderful time well spent. Catch ‘ya now!! ^_- !



ALWAYS A JOY TO HELP YOU OUT AND STUFF LIKE THAT,

[Image: file.php?id=32]
Adigun Azikiwe Polack
One of the Founders of “Aura Flow”
Continuing Developer of “Frantic Journey”
Current Developer of “Star Angelic Slugger”
Webmaster of the “AAP Official Projects Squad”
:oops: My mistake... I'm someday going to port some more of the Allegro examples and tests, but I'm busy bashing my head on the DOS gfxlib port for now. Smile
hehe, I reported these quote problems to v!c on the first day he released 0.11 :wink:

Big Grin

And I don't think you can accuse Dr V. -Allegro port was a huge amount of work I guess... Good work btw!
Thanks a hell of a lot, v1c. The program you posted ran perfectly, with no errors at all (well, after I fixed the bug in allegro.bi - it's been mentioned above. No need to repeat it.)

Anyway, I still can't seem to get the mouse working in my program. It's pissing me off, but it's fun playing around with it. :-D I imagine I'll figure it out sooner or later.

Thanks again, dude. :-D
Hahaha... I finally got the mouse routines working. I was just messing around and I said to myself, "Hmm, maybe I should call Install_Mouse after Set_Gfx_Mode..." So I did. And it worked. Worked great.

I'm dumb. :-D