Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » mouse problems

This thread is locked; no one can reply to it. rss feed Print
mouse problems
Money
Member #6,730
December 2005
avatar

why won't the mouse move??

#include <allegro.h>

int main(){

allegro_init();
install_keyboard();
install_mouse();
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

acquire_screen();

show_mouse(screen);
while ( !key[KEY_ESC] )
{
textout_ex( screen, font, "New Game", 0, 0, makecol( 0, 155, 0), makecol( 0, 0, 0) );
textout_ex( screen, font, "Load Game", 0, 10, makecol( 0, 155, 0),makecol(0, 0, 0) );
textout_ex( screen, font, "Options", 0, 20, makecol( 0, 155, 0), makecol(0,0,0) );
textout_ex( screen, font, "Quit", 0, 30, makecol(0, 155, 0), makecol(0, 0, 0) );

}

release_screen();

return 0;

}
END_OF_MAIN();

miran
Member #2,407
June 2002

Throw out acquire_screen() and it will work.

--
sig used to be here

Money
Member #6,730
December 2005
avatar

thanks, i thought i need aquare screen so i can draw to the screen

also, the mouse goes under the text, i need it to overlap the text

ReyBrujo
Moderator
January 2001
avatar

Use a double buffer.

1#include <allegro.h>
2 
3int main(){
4 BITMAP *bmp;
5 allegro_init();
6 install_keyboard();
7 install_mouse();
8 set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
9 
10 bmp = create_bitmap(640, 480);
11 while ( !key[KEY_ESC] ) {
12 clear_bitmap(bmp);
13 
14 textout_ex( bmp, font, "New Game", 0, 0, makecol( 0, 155, 0), makecol( 0, 0, 0) );
15 textout_ex( bmp, font, "Load Game", 0, 10, makecol( 0, 155, 0),makecol(0, 0, 0) );
16 textout_ex( bmp, font, "Options", 0, 20, makecol( 0, 155, 0), makecol(0,0,0) );
17 textout_ex( bmp, font, "Quit", 0, 30, makecol(0, 155, 0), makecol(0, 0, 0) );
18 
19 show_mouse(bmp);
20 blit(bmp, screen, 0, 0, 0, 0, 640, 480);
21 }
22 
23 show_mouse(NULL);
24 destroy_bitmap(bmp);
25 allegro_exit();
26 return 0;
27}
28END_OF_MAIN()

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

miran
Member #2,407
June 2002

Try this code:

1#include <allegro.h>
2 
3 
4int main(){
5 
6allegro_init();
7set_color_depth(desktop_color_depth());
8set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
9install_keyboard();
10install_mouse();
11 
12BITMAP *canvas = create_bitmap(SCREEN_W, SCREEN_H);
13 
14while ( !key[KEY_ESC] )
15{
16clear_to_color(canvas, 0);
17textout_ex( canvas, font, "New Game", 0, 0, makecol( 0, 155, 0), makecol( 0, 0, 0) );
18textout_ex( canvas, font, "Load Game", 0, 10, makecol( 0, 155, 0),makecol(0, 0, 0) );
19textout_ex( canvas, font, "Options", 0, 20, makecol( 0, 155, 0), makecol(0,0,0) );
20textout_ex( canvas, font, "Quit", 0, 30, makecol(0, 155, 0), makecol(0, 0, 0) );
21show_mouse(canvas);
22blit(canvas, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
23}
24show_mouse(0);
25destroy_bitmap(canvas);
26return 0;
27}
28END_OF_MAIN()

EDIT: Göddämnít Rey, you type too fast.

--
sig used to be here

ReyBrujo
Moderator
January 2001
avatar

And Money, begin adding checks. Your program may crash and you will never find out why.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Money
Member #6,730
December 2005
avatar

new game doesn't get highlightsed

#include <allegro.h>

int main(){
BITMAP *bmp;
allegro_init();
install_keyboard();
install_mouse();
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

bmp = create_bitmap(640, 480);
while ( !key[KEY_ESC] ) {
clear_bitmap(bmp);

textout_ex( bmp, font, "New Game", 0, 0, makecol( 0, 155, 0), makecol( 0, 0, 0) );
textout_ex( bmp, font, "Load Game", 0, 10, makecol( 0, 155, 0),makecol(0, 0, 0) );
textout_ex( bmp, font, "Options", 0, 20, makecol( 0, 155, 0), makecol(0,0,0) );
textout_ex( bmp, font, "Quit", 0, 30, makecol(0, 155, 0), makecol(0, 0, 0) );

show_mouse(bmp);
blit(bmp, screen, 0, 0, 0, 0, 640, 480);

if ( mouse_x >= 0 && mouse_x <= 50 && mouse_y >= 0 and mouse_y <= 20 )
{

textout_ex( bmp, font, "New Game", 0, 0, makecol( 0, 255, 0), makecol( 0, 0, 0) );

}

}

show_mouse(NULL);
destroy_bitmap(bmp);
allegro_exit();
return 0;
}
END_OF_MAIN()

miran
Member #2,407
June 2002

Think a little bit about what your code does.

--
sig used to be here

ReyBrujo
Moderator
January 2001
avatar

Hehehe :) miran is right, you need to understand what you are writing. Note that you blit the bitmap on the screen and then highlight it. You should highlight it before blitting the bitmap on the screen.

Go one line at a time, understanding what each does.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Money
Member #6,730
December 2005
avatar

ok, got it thanks

what does blitting a bitmap mean?

ReyBrujo
Moderator
January 2001
avatar

Blit is basically copying. You copy every pixel of a memory bitmap onto the screen so that you can see it. Until you blit the bitmap, you won't see anything you have drawn on that determined bitmap.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Money
Member #6,730
December 2005
avatar

oh, ok, thNks

miran
Member #2,407
June 2002

Blit stands for Block Image Transfer. In simple terms it means copying a bitmap somewhere. The blit() function copies (blits) one bitmap on top of another. In your code you should draw all your graphics to the canvas buffer and then copy (blit) it to the visible screen. What you actually did was draw some of the gfx to the canvas, copy it to the screen, then draw some more. Which is wrong.

EDIT: Yes, I type slower. Or maybe I just type more?

--
sig used to be here

Andrei Ellman
Member #3,434
April 2003

Also, don't forget to call install_timer() before you call install_mouse(). Allegro needs this if you want to display the pointer.

AE.

--
Don't let the illegitimates turn you into carbon.

Don Freeman
Member #5,110
October 2004
avatar

You don't need to call install_timer() first because the manual states that the install_mouse() function calls it if not installed. I do suggest the following order though:

allegro_init();
install_timer();
install_keyboard();
install_mouse();
set_color_depth(BPP);
set_gfx_mode(CARD,Width,Height,XWidth,XHeight);
set_color_conversion(COLORCONV_TOTAL);

I install timer before keyboard even, because the keyboard was an interrupt driven device...I don't beleive it matters with allegro, but I've stuck with this all the same.

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Tobias Dammers
Member #2,604
August 2002
avatar

Don: I believe that this can get you into trouble on some windows platforms, since the w32 port needs a window (created by set_gfx_mode()) to process the keyboard and mouse messages. I could be wrong though.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

miran
Member #2,407
June 2002

The proper order should be something like this:

allegro_init();
set_color_depth(BPP);
set_gfx_mode(CARD,Width,Height,XWidth,XHeight);
set_color_conversion(COLORCONV_TOTAL);
install_keyboard();
install_mouse();

--
sig used to be here

Evert
Member #794
November 2000
avatar

Quote:

I believe that this can get you into trouble on some windows platforms, since the w32 port needs a window (created by set_gfx_mode()) to process the keyboard and mouse messages. I could be wrong though.

Allegro already creates a window before you call set_gfx_mode(). It's somewhere outside the desktop area of the viewport though, so you never get to see it.

That said, I think the Windows port delays the `real' initialisation of the keyboard and mouse code until after set_gfx_mode() is called. But calling the initialisation routines before then should be fine (I do it all the time and have never seen a problem myself).

Matthew Leverton
Supreme Loser
January 1999
avatar

It is fine to initialize them before the graphics mode is set, but you cannot use them until after.

The docs are quite clear on this, especially the example.

Go to: