c - Allegro 5 Keyboard Interfering on Mouse Position? -
i making simple game using c , allegro 5. hiding mouse , swapping custom mouse icon. cursor icon's position altered whenever mouse movement detected.
what bothers me if, example, have mouse cursor @ coordinates mouse_x = 100 , mouse_y = 200, , press key on keyboard, mouse cursor teleported coordinates mouse_x = 0 , mouse_y = 0 while key being pressed.
i have no clue happens, whenever uninstall keyboard using al_uninstall_keyboard(), error gone.
this code:
#include <stdio.h> #include <stdlib.h> #include <allegro5\allegro.h> #include <allegro5\allegro_image.h> #include <allegro5\allegro_primitives.h> #include <allegro5\allegro_audio.h> #include <allegro5\allegro_font.h> #include <allegro5\allegro_ttf.h> #include <allegro5\allegro_acodec.h> enum gamemode{menu, farmgame, stdend}; enum soil{normal, wet}; enum soil2{unplow, plow}; enum croptype{none, tomato}; typedef struct gdisp //game display { int width; int height; allegro_display *display; }gdisp; typedef struct fcrop //farm crop { croptype crop; allegro_timeout *waterneed; allegro_timeout *growth[4]; //crop's growth time on stages }fcrop; typedef struct ftile //farm tiles { int xpoint; int ypoint; int size; soil ground; soil2 ground2; fcrop crop; }ftile; int main() { //allegro initialization area al_init(); al_init_image_addon(); al_init_primitives_addon(); al_init_font_addon(); al_init_ttf_addon(); al_init_acodec_addon(); //allegro installation area al_install_keyboard(); al_install_mouse(); //allegro variable area allegro_event_queue *queue = al_create_event_queue(); allegro_event ev; allegro_font *font[3]; //allegro variable declaration area //my enums area gamemode gamemode = menu; allegro_bitmap *bmp[30]; //20-30: menu bitmaps allegro_bitmap *ico; //my structs area gdisp disp; disp = {0, 0, null}; //games... here go! if (gamemode == menu) { char *currgamelist[4] = { "play", "tutorial", "credits" }; disp.width = 600, disp.height = 600, disp.display = al_create_display(disp.width, disp.height); ico = al_load_bitmap("tomato_seedbase.png"); bmp[0] = al_load_bitmap("normal-select.png"); bmp[1] = al_load_bitmap("link-select.png"); al_set_window_title(disp.display, "farming hero"); al_set_display_icon(disp.display, ico); al_hide_mouse_cursor(disp.display); font[0] = al_load_font("pressstart2p.ttf", 32, 0); al_clear_to_color(al_map_rgb(0, 0, 0)); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_keyboard_event_source()); int mouse_x; int mouse_y; while (gamemode == menu) { al_wait_for_event(queue, &ev); al_clear_to_color(al_map_rgb(0, 0, 0)); if (ev.type == allegro_event_key_down) if (ev.keyboard.keycode == allegro_key_escape) { al_destroy_display(disp.display); al_destroy_font(font[0]); return 0; } al_draw_text(font[0], al_map_rgb(255, 255, 255), 105, 50, 0, "farming hero"); al_draw_text(font[0], al_map_rgb(255, 255, 255), 30, 200, 0, currgamelist[0]); al_draw_text(font[0], al_map_rgb(255, 255, 255), 30, 300, 0, currgamelist[1]); al_draw_text(font[0], al_map_rgb(255, 255, 255), 30, 400, 0, currgamelist[2]); al_draw_text(font[0], al_map_rgb(255, 255, 255), 30, 500, 0, "exit"); al_draw_text(font[0], al_map_rgb(255, 255, 255), 30, 500, 0, " "); if(ev.type == allegro_event_mouse_button_down) if (ev.mouse.button & 1) { if (ev.mouse.x >= 30 && ev.mouse.y >= 200 && ev.mouse.x <= al_get_text_width(font[0], currgamelist[0]) + 30 && ev.mouse.y <= 232) gamemode = farmgame; if (ev.mouse.x >= 30 && ev.mouse.y >= 300 && ev.mouse.x <= al_get_text_width(font[0], currgamelist[1]) + 30 && ev.mouse.y <= 332) al_destroy_display(disp.display), al_destroy_font(font[0]), exit(0); if (ev.mouse.x >= 30 && ev.mouse.y >= 400 && ev.mouse.x <= al_get_text_width(font[0], currgamelist[2]) + 30 && ev.mouse.y <= 432) al_destroy_display(disp.display), al_destroy_font(font[0]), exit(0); if (ev.mouse.x >= 30 && ev.mouse.y >= 500 && ev.mouse.x <= al_get_text_width(font[0], "exit") + 30 && ev.mouse.y <= 532) al_destroy_display(disp.display), al_destroy_font(font[0]), exit(0); } if (ev.type == allegro_event_mouse_axes) mouse_x = ev.mouse.x, mouse_y = ev.mouse.y; if (mouse_x >= 30 && mouse_x <= al_get_text_width(font[0], currgamelist[0]) + 30 && mouse_y >= 195 && mouse_y <= 235) al_draw_text(font[0], al_map_rgb(255, 239, 0), 30, 200, 0, currgamelist[0]), al_draw_bitmap(bmp[1], mouse_x, mouse_y, 0); else if (mouse_x >= 30 && mouse_x <= al_get_text_width(font[0], currgamelist[1]) + 30 && mouse_y >= 295 && mouse_y <= 335) al_draw_text(font[0], al_map_rgb(255, 239, 0), 30, 300, 0, currgamelist[1]), al_draw_bitmap(bmp[1], mouse_x, mouse_y, 0); else if (mouse_x >= 30 && mouse_x <= al_get_text_width(font[0], currgamelist[2]) + 30 && mouse_y >= 395 && mouse_y <= 435) al_draw_text(font[0], al_map_rgb(255, 239, 0), 30, 400, 0, currgamelist[2]), al_draw_bitmap(bmp[1], mouse_x, mouse_y, 0); else if (mouse_x >= 30 && mouse_x <= al_get_text_width(font[0], "exit") + 30 && mouse_y >= 495 && mouse_y <= 535) al_draw_text(font[0], al_map_rgb(255, 239, 0), 30, 500, 0, "exit"), al_draw_bitmap(bmp[1], mouse_x, mouse_y, 0); else{ al_draw_bitmap(bmp[0], mouse_x, mouse_y, 0); }; al_flip_display(); } } if (gamemode == farmgame) { allegro_timer *fps = al_create_timer(1.00 / 60.00); font[0] = al_load_font("pressstart2p.ttf", 8, 0); font[1] = al_load_font("pressstart2p.ttf", 16, 0); al_register_event_source(queue, al_get_timer_event_source(fps)); int mouse_x, mouse_y; mouse_x = -50, mouse_y = 0; fcrop empty = { none, 0, 0 }; ftile tile[100]; int x_prep, y_prep; //prepare tile's configuration x_prep = 0, y_prep = 0; (int = 0; < 100; i++) { if (x_prep == 10) x_prep = 0, y_prep++; tile[i] = { x_prep, y_prep, 51, normal, unplow, empty}; x_prep++; } al_clear_to_color(al_map_rgb(0, 0, 0)); al_flip_display(); bmp[0] = al_load_bitmap("link-select.png"); bmp[1] = al_load_bitmap("ground_normal.png"); bmp[9] = al_create_bitmap(disp.width, disp.height); bmp[30] = al_load_bitmap("tomato_seedbase.png"); al_uninstall_keyboard(); al_start_timer(fps); al_set_target_bitmap(bmp[9]); al_draw_filled_rectangle(512, 2, disp.width - 2, 508, al_map_rgb(150, 120, 0)); al_draw_filled_rectangle(2, 512, 512, disp.height - 2, al_map_rgb(150, 120, 0)); al_draw_filled_rectangle(512, 512, disp.width - 2, disp.height - 2, al_map_rgb(150, 120, 0)); al_draw_rectangle(512, 2, disp.width - 2, 511, al_map_rgb(70, 50, 0), 5); al_draw_rectangle(2, 512, 512, disp.height - 2, al_map_rgb(70, 50, 0), 5); al_draw_rectangle(512, 512, disp.width - 2, disp.height - 2, al_map_rgb(70, 50, 0), 5); al_set_target_backbuffer(disp.display); while (gamemode == farmgame) { char tileinfo[5][15]; al_clear_to_color(al_map_rgb(0, 0, 0)); al_wait_for_event(queue, &ev); (int = 0; < 100; i++) { if (tile[i].ground == normal && tile[i].ground2 == unplow) al_draw_bitmap(bmp[1], tile[i].xpoint * tile[i].size, tile[i].ypoint * tile[i].size, 0); if (mouse_x >= 0 && mouse_x <= 508 && mouse_y >= 0 && mouse_y <= 508) if (mouse_x >= tile[i].xpoint * tile[i].size && mouse_x <= tile[i].xpoint * tile[i].size + tile[i].size && mouse_y >= tile[i].ypoint * tile[i].size && mouse_y <= tile[i].ypoint * tile[i].size + tile[i].size) { tile[i].crop.crop == none ? strcpy(tileinfo[1], "none") : 0, tile[i].crop.crop == tomato ? strcpy(tileinfo[1], "tomatoes") : 0; tile[i].ground == normal ? strcpy(tileinfo[2], "dry") : 0, tile[i].ground == wet ? strcpy(tileinfo[2], "wet") : 0; tile[i].ground2 == unplow ? strcpy(tileinfo[3], "unplow") : 0, tile[i].ground2 == plow ? strcpy(tileinfo[3], "plow") : 0; } } al_draw_bitmap(bmp[0], ev.mouse.x, ev.mouse.y, 0); if (ev.type == allegro_event_mouse_axes) mouse_x = ev.mouse.x, mouse_y = ev.mouse.y; if (ev.type == allegro_event_key_down) if (ev.keyboard.keycode == allegro_key_escape) gamemode = stdend; al_draw_bitmap(bmp[9], 0, 0, 0); if (mouse_x >= 503 && mouse_x <= disp.height && mouse_y >= 10 && mouse_y <= al_get_bitmap_height(bmp[30])) al_draw_tinted_bitmap(bmp[30], al_map_rgb(125, 255, 37), 503, 10, 0), al_draw_text(font[1], al_map_rgb(125, 255, 37), 525, 100, 0, "shop"); else{ al_draw_bitmap(bmp[30], 503, 10, 0), al_draw_text(font[1], al_map_rgb(255, 255, 255), 525, 100, 0, "shop"); } if (tileinfo[1] != null && tileinfo[2] != null && tileinfo[3] != null) { al_draw_textf(font[0], al_map_rgb(255, 255, 255), 520, 520, 0, "%s", tileinfo[1]); al_draw_textf(font[0], al_map_rgb(255, 255, 255), 520, 550, 0, "%s", tileinfo[2]); al_draw_textf(font[0], al_map_rgb(255, 255, 255), 520, 580, 0, "%s", tileinfo[3]); } al_draw_bitmap(bmp[0], mouse_x, mouse_y, 0); al_flip_display(); } } if (gamemode == stdend) { al_rest(2); al_destroy_display(disp.display); } return 0; }
you never initialized mouse_x
, mouse_y
before loop. means random value, they'd 0. when press key, loop proceeds , mouse cursor drawn @ bottom of loop @ (0, 0). can initialize them proper value al_get_mouse_state
.
Comments
Post a Comment