hey, why doesn't the mouse show up?
1 | #include <allegro.h> |
2 | #include "menu.h" |
3 | |
4 | |
5 | int main(void) |
6 | { |
7 | allegro_init(); |
8 | set_color_depth(24); |
9 | set_gfx_mode( GFX_AUTODETECT , 640, 480, 0, 0 ); |
10 | install_keyboard(); |
11 | install_mouse(); |
12 | install_timer(); |
13 | acquire_screen(); |
14 | |
15 | |
16 | |
17 | BITMAP *buffer = create_bitmap( 640, 480 ); |
18 | clear_bitmap(buffer); |
19 | draw_sprite( screen, buffer, 0, 0); |
20 | show_mouse(buffer); |
21 | menu(); //launch main game menu |
22 | |
23 | readkey(); |
24 | return 0; |
25 | } |
26 | END_OF_MAIN() |
this is the code for main.h
1 | void menu(void) |
2 | { |
3 | |
4 | |
5 | |
6 | |
7 | textout_ex(screen, font, "New Game", 10, 10, (0,0,200), NULL); |
8 | if (mouse_x >= 10 && mouse_x <= 35 & mouse_y >= 10 & mouse_y <= 15) |
9 | { |
10 | textout_ex(screen, font, "New Game", 10, 10, (0,0,255), NULL); |
11 | } |
12 | |
13 | textout_ex(screen, font, "Exit", 22, 25, (0,0,200),NULL); |
14 | if (mouse_x >= 15 && mouse_x <= 25 && mouse_y >= 25 && mouse_y <= 35) |
15 | { |
16 | textout_ex(screen, font, "Exit", 22, 25, (0,0,255),NULL); |
17 | } |
18 | |
19 | |
20 | } |
You put the mouse on the buffer but as far as I can see, at no time after that did you draw the buffer back onto the screen.
For one, you're calling acquire_screen then doing non-screen-related things (and not calling release_screen). Second, you're showing the mouse on an off-screen buffer. Third, you have a non-inlined function in your header.
Unrelated, but important.
The actual problem is that you draw the buffer to the screen once, before showing the mouse on it, then never again. readkey() is blocking, so you can't draw the buffer afterward. You should make a loop that always cycles (eg.
while(1) { /* code here */ }
after initializing everything. When you draw, clear the buffer, draw to the buffer instead of the screen, then use blit()] to copy the buffer to the screen. To show the mouse, use draw_sprite() to draw mouse_sprite onto the buffer right before blitting to the screen.
[EDIT]
I was too slow
[/EDIT]
I don't see why it should. Why don't you update everything to the buffer and display that? For example..
int main( ) { //initialization stuff BITMAP *buffer = create_bitmap( 640, 480 ); while(!key[KEY_ESC]) { //update buffer with menu stuff here show_mouse(buffer); //show buffer to screen here? } destroy_bitmap(buffer); return 0; }END_OF_MAIN()
Also, don't forget to deallocate your buffer. Hope this helps.
Erick
1 | #include <allegro.h> |
2 | #include "menu.h" |
3 | |
4 | |
5 | int main(void) |
6 | { |
7 | allegro_init(); |
8 | set_color_depth(24); |
9 | set_gfx_mode( GFX_AUTODETECT , 640, 480, 0, 0 ); |
10 | install_keyboard(); |
11 | install_mouse(); |
12 | install_timer(); |
13 | acquire_screen(); |
14 | |
15 | while ( !key[KEY_ESC] ) |
16 | { |
17 | BITMAP *buffer = create_bitmap( 640, 480 ); |
18 | clear_bitmap(buffer); |
19 | draw_sprite( screen, buffer, 0, 0); |
20 | show_mouse(buffer); |
21 | menu(); //launch main game menu |
22 | } |
23 | |
24 | release_screen(); |
25 | return 0; |
26 | } |
27 | END_OF_MAIN() |
i got flickering and it still didn't show, the mouse i mean
try this..
1 | #include <allegro.h> |
2 | |
3 | int main( ) { |
4 | allegro_init(); |
5 | install_keyboard(); |
6 | install_timer(); |
7 | install_mouse(); |
8 | set_color_depth(16); |
9 | set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); |
10 | BITMAP *buffer = create_bitmap(640, 480); |
11 | |
12 | while(!key[KEY_ESC]) { |
13 | textprintf_ex(buffer, font, 0, 0, makecol(0, 255, 0), -1, "mx:%d my:%d", mouse_x, mouse_y); |
14 | show_mouse(buffer); |
15 | |
16 | acquire_screen(); |
17 | draw_sprite(screen, buffer, 0, 0); |
18 | release_screen(); |
19 | clear_bitmap(buffer); |
20 | show_mouse(NULL); |
21 | } |
22 | |
23 | destroy_bitmap(buffer); |
24 | return 0; |
25 | }END_OF_MAIN() |
Don't use show_mouse on an off-screen buffer. Especially a memory bitmap. And you don't need to use acquire_release_screen when you're only doing one thing to the screen.
how can i do a quick small menu like that one, but use the keyboard instead of te mouse, just, i want for the user to press down and an action occurs
for a game menu with the keyboard, couldn't i do something like
switch(menu)
{
case 0 = //code for new game
break;
case 1 = //code for exit
break;
}
Yes, you could. You would have to make an int/short Menu() function that returns the user's choice.
well how would i make the menu items correspond to a menu umber, like "new game" would be enter to one or something, i dont really know how to implement this
Simple example:
1 | short Menu() |
2 | { |
3 | short choice; |
4 | |
5 | //code |
6 | std::cout << "Enter a number(0-2): "; std::cin >> choice; |
7 | //more code |
8 | |
9 | return choice; |
10 | } |
11 | int main() |
12 | { |
13 | short Choice = Menu(); |
14 | |
15 | switch(Choice) |
16 | { |
17 | case 0: |
18 | //New Game; |
19 | break; |
20 | case 1: |
21 | //Exit; |
22 | break; |
23 | } |
24 | return 0; |
25 | } |
o, i tried something on my own, but its not working right, i tried drawing to the screen, but still the highlighter wouldn't move, but when i try to use the buffer, either the program crashes or the start game and exdit doens't show
1 | #include <allegro.h> |
2 | |
3 | void menu(void); |
4 | |
5 | //resources |
6 | BITMAP *buffer = create_bitmap(640, 480); |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | int main(void) |
16 | { |
17 | allegro_init(); |
18 | install_keyboard(); |
19 | set_color_depth(24); |
20 | set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); |
21 | |
22 | draw_sprite(screen,buffer,0,0); |
23 | |
24 | |
25 | |
26 | while ( !key[KEY_ESC] ) |
27 | { |
28 | menu(); //display main game menu |
29 | } |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | return 0; |
41 | } |
42 | END_OF_MAIN() |
43 | void menu(void) |
44 | { |
45 | |
46 | textout_ex(buffer, font, "Start Game", (640/2)-45, 480/2, (0,0,200), NULL); |
47 | textout_ex(buffer, font, "Exit", (640/2)-25, (480/2)+25, (0,0,200),NULL); |
48 | |
49 | int menu_place = 0; |
50 | |
51 | if ( key[KEY_DOWN] ) |
52 | { |
53 | menu_place = 1; |
54 | } |
55 | |
56 | if ( menu_place == 0 ) |
57 | { |
58 | textout_ex(buffer, font, "Start Game", (640/2)-45, 480/2, (0,0,255), NULL); |
59 | } |
60 | |
61 | if ( menu_place == 1 ) |
62 | { |
63 | textout_ex(buffer, font, "Exit", (640/2)-25, (480/2)+25, (0,0,200),NULL); |
64 | } |
65 | |
66 | |
67 | } |
You create the bitmap before you initialize that graphics mode. that will not work.
You also never actually draw the buffer to the screen any where in your loop.
And there will be now way to select option 0 once you pressed down once.
It would be better to use readkey (is that the allegro one?) and increase the position on down, decrease it on up and make sure you wrap the selection-variable nicely.
To draw something to the screen, you typically do (once per "round" of your loop):
1. Clear the buffer
2. Draw everything you need to show to the buffer
3. Draw the buffer to the screen
Repeat.
Right now, all you are doing is step 2.
1 | #include <allegro.h> |
2 | |
3 | void menu(void); |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | int main(void) |
12 | { |
13 | allegro_init(); |
14 | install_keyboard(); |
15 | set_color_depth(24); |
16 | set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); |
17 | |
18 | BITMAP *buffer = create_bitmap(640, 480); |
19 | clear_bitmap(buffer); |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | while ( !key[KEY_ESC] ) |
26 | { |
27 | menu(); //display main game menu |
28 | draw_sprite(screen,buffer,0,0); |
29 | } |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | return 0; |
41 | } |
42 | END_OF_MAIN() |
43 | void menu(void) |
44 | { |
45 | |
46 | textout_ex(buffer, font, "Start Game", (640/2)-45, 480/2, (0,0,200), NULL); |
47 | textout_ex(buffer, font, "Exit", (640/2)-25, (480/2)+25, (0,0,200),NULL); |
48 | |
49 | int menu_place = 0; |
50 | |
51 | if ( key[KEY_DOWN] ) |
52 | { |
53 | menu_place = 1; |
54 | } |
55 | |
56 | if ( menu_place == 0 ) |
57 | { |
58 | textout_ex(buffer, font, "Start Game", (640/2)-45, 480/2, (0,0,255), NULL); |
59 | } |
60 | |
61 | if ( menu_place == 1 ) |
62 | { |
63 | textout_ex(buffer, font, "Exit", (640/2)-25, (480/2)+25, (0,0,200),NULL); |
64 | } |
65 | |
66 | |
67 | } |
i did that globally cause it says buffer undeclared, so i declared buffer globally, but you said i coudn't do it
You can declare the buffer globally, but not create a BITMAP.
BITMAP *buffer; int main() { allegro_init(); set_color_depth(desktop_color_depth()); set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0); buffer = create_bitmap(SCREEN_W, SCREEN_H); clear(buffer); allegro_exit(); return 0; }
oh man, i've been sitting here for like an hour tring to figure this out, i got the buttons to work fine, but now i can't get the input part, right, i've tried different things and don't see how to fix it, i think i know what the prob is, i was thinking so hard to fix it, but didn't have a clue, i think it'd be easier to use a switch case in this..."case" but i don't know how to implement that in my code
1 | #include <allegro.h> |
2 | |
3 | void menu(void); |
4 | |
5 | BITMAP *buffer; |
6 | |
7 | int main(void) |
8 | { |
9 | allegro_init(); |
10 | install_keyboard(); |
11 | set_color_depth(24); |
12 | set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); |
13 | |
14 | buffer = create_bitmap(640, 480); |
15 | clear_bitmap(buffer); |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | while ( !key[KEY_ESC] ) |
22 | { |
23 | menu(); //display main game menu |
24 | draw_sprite(screen,buffer,0,0); |
25 | } |
26 | |
27 | return 0; |
28 | } |
29 | END_OF_MAIN() |
30 | void menu(void) |
31 | { |
32 | |
33 | textout_ex(buffer, font, "Start Game", (640/2)-45, 480/2, (0,0,200), NULL); |
34 | textout_ex(buffer, font, "Exit", (640/2)-25, (480/2)+25, (0,0,200),NULL); |
35 | |
36 | int menu_place = 0; |
37 | |
38 | if ( key[KEY_DOWN] ) |
39 | { |
40 | menu_place = 1; |
41 | } |
42 | |
43 | if ( menu_place == 0 ) |
44 | { |
45 | textout_ex(buffer, font, "Start Game", (640/2)-45, 480/2, (0,0,255), NULL); |
46 | } |
47 | |
48 | if ( menu_place == 1 ) |
49 | { |
50 | textout_ex(buffer, font, "Exit", (640/2)-25, (480/2)+25, (0,0,255),NULL); |
51 | |
52 | } |
53 | |
54 | } |
Easier? No. Different.
1 | void menu(void) |
2 | { |
3 | textout_ex(buffer, font, "Start Game", (640/2)-45, 480/2, (0,0,200), NULL); |
4 | textout_ex(buffer, font, "Exit", (640/2)-25, (480/2)+25, (0,0,200),NULL); |
5 | |
6 | int menu_place = 0; |
7 | bool OK = false; |
8 | |
9 | while (!OK) |
10 | { |
11 | if ( key[KEY_UP] ) { menu_place = 0; } |
12 | else if (key[KEY_DOWN]) { menu_place = 1; } |
13 | |
14 | if (key[KEY_ENTER]) |
15 | { |
16 | OK = true; |
17 | if ( menu_place == 0 ) { /*Start Game */ } |
18 | else if ( menu_place == 1 ) { /* Exit */ } |
19 | } |
20 | } |
21 | } |
if you compiled my previous code, you'd see that once ypou press down, it highights exit, but when you let go of down, it goes back to start game, how do i make it so that when you press down it stays there, and i wanted to edit my code, not add in new, cause i kinda don't understand it that way, just tell me what to do and why its doing it like that
Because:
while ( !key[KEY_ESC] ) { menu(); //display main game menu draw_sprite(screen,buffer,0,0); } void menu(void) { int menu_choice = 0; }
Make the menu_choice global, initialize it once outside of the loop, and change the values inside the loop.
i ...do not understand sigh you said change the valuies to what, i just don't understand, stuff like this jsut makes me want to quit progrmaming forever
Sorry, I was focusing on the menu() function and I forgot you're looping it.
1 | #include <allegro.h> |
2 | |
3 | void menu(void); |
4 | |
5 | BITMAP *buffer; |
6 | int menu_place; |
7 | |
8 | int main(void) |
9 | { |
10 | allegro_init(); |
11 | install_keyboard(); |
12 | set_color_depth(24); |
13 | set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); |
14 | |
15 | buffer = create_bitmap(640, 480); |
16 | clear_bitmap(buffer); |
17 | |
18 | menu_place = 0; |
19 | |
20 | while ( !key[KEY_ESC] ) |
21 | { |
22 | menu(); //display main game menu |
23 | draw_sprite(screen,buffer,0,0); |
24 | } |
25 | |
26 | return 0; |
27 | } |
28 | END_OF_MAIN(); |
29 | void menu(void) |
30 | { |
31 | textout_ex(buffer, font, "Start Game", (640/2)-45, 480/2, (0,0,200), NULL); |
32 | textout_ex(buffer, font, "Exit", (640/2)-25, (480/2)+25, (0,0,200),NULL); |
33 | |
34 | if ( key[KEY_UP] ) { menu_place = 0; } |
35 | else if (key[KEY_DOWN]) { menu_place = 1; } |
36 | |
37 | if (key[KEY_ENTER]) |
38 | { |
39 | if ( menu_place == 0 ) { /*Start Game */ } |
40 | else if ( menu_place == 1 ) { /* Exit */ } |
41 | } |
42 | } |
It doesn't get much simple than that.
umm, did you even try to compile that? nothing happens, i don't even see anything hightlighted
that code doens't work
sobs
Because you have to write it yourself. I've written what has to be written, and you can insert the code for starting/exiting/highlighting yourself... but in case you're too lazy:
1 | #include <allegro.h> |
2 | |
3 | void menu(void); |
4 | |
5 | BITMAP *buffer; |
6 | int menu_place; |
7 | |
8 | int main(void) |
9 | { |
10 | allegro_init(); |
11 | install_keyboard(); |
12 | set_color_depth(24); |
13 | set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); |
14 | |
15 | buffer = create_bitmap(640, 480); |
16 | clear_bitmap(buffer); |
17 | |
18 | menu_place = 0; |
19 | |
20 | while ( !key[KEY_ESC] ) |
21 | { |
22 | clear(buffer); |
23 | clear_keybuf(); |
24 | menu(); //display main game menu |
25 | draw_sprite(screen, buffer, 0, 0); |
26 | } |
27 | |
28 | return 0; |
29 | } |
30 | END_OF_MAIN(); |
31 | void menu(void) |
32 | { |
33 | if ( key[KEY_UP] ) { menu_place = 0; } |
34 | else if (key[KEY_DOWN]) { menu_place = 1; } |
35 | |
36 | rectfill(buffer, (SCREEN_W/2)-50, SCREEN_H/2-5+menu_place*25, (SCREEN_W/2)+35, SCREEN_H/2+10+menu_place*25, makecol(100, 100, 255)); |
37 | |
38 | textprintf_ex(buffer, font, (640/2)-45, 480/2, (0,0,200), -1, "Start Game"); |
39 | textprintf_ex(buffer, font, (640/2)-25, (480/2)+25, (0,0,200), -1, "Exit"); |
40 | |
41 | if (key[KEY_ENTER]) |
42 | { |
43 | if ( menu_place == 0 ) { /*Start Game */ } |
44 | else if ( menu_place == 1 ) { exit(0); } |
45 | } |
46 | } |
This time it's tested.
thank you so much for your help, im such a noob, i jsut need a little guidance
"A little" is not a good way to put it. You have a lot to learn. But then again, we all started like that, so no need to discourage yourself... I spent 3 hours today trying to figure out what was wrong in my game, only to find out that it was just a misplaced line of code.