Having trouble with my code, when I compile and open .exe
zeph173

So I have a problem with my program, its slow and laggy. I'm trying to make a simple GUI barely starting it off so here is the code(PS:sorry didn't upload this, it would take a while but heres what I have:

MyMain(main.cpp)

#SelectExpand
1// Main here 2#include "allegro5/allegro.h" 3#include "allegro5/allegro_primitives.h" 4#include "allegro5/allegro_image.h" 5#include "allegro5/allegro_native_dialog.h" 6 7#include "GLOBALStoALL.h" 8#include "MouseCursor.h" 9#include "Backgrounds.h" 10#include "Buttons.h" 11 12//PROTOTYPE FUNCTIONS 13 14//-----Button related----- 15void InitButton(Button &object1, Button &object2, Button &object3, Button &object4, Button &object5, 16 ALLEGRO_BITMAP *image1, ALLEGRO_BITMAP *image2, ALLEGRO_BITMAP *image3, ALLEGRO_BITMAP *image4, ALLEGRO_BITMAP *image5); 17void DrawButton(Button &StartB, Button &OptiB, Button &CredB, Button &StageB, Button &ExitB); 18 19int main(int nArgc, char *pszArgs[]) 20{ 21 //VARIABLES GAME 22 int frames = 0; 23 int GAMEfps = 0; 24 float gametime = 0; 25 26 bool Done = false; 27 bool redraw = true; 28 29 //ALLEGRO POINTER VARIABLES 30 ALLEGRO_PATH *path = NULL; 31 32 ALLEGRO_DISPLAY *display = NULL; 33 ALLEGRO_EVENT_QUEUE *eventqueue = NULL; 34 ALLEGRO_TIMER *timer = NULL; 35 36 ALLEGRO_BITMAP *mouseImage = NULL; 37 ALLEGRO_BITMAP *backgroundImage = NULL; 38 39 ALLEGRO_BITMAP *sImage = NULL; 40 ALLEGRO_BITMAP *oImage = NULL; 41 ALLEGRO_BITMAP *cImage = NULL; 42 ALLEGRO_BITMAP *stagesImage = NULL; 43 ALLEGRO_BITMAP *exitImage = NULL; 44 45 //CHECK FOR ERRORS WITH DISPLAY AND INIT 46 if(!al_init()) 47 return -1; 48 49 display = al_create_display(ScreenWidth, ScreenHeight); 50 51 if(!display) 52 return -1; 53 54 //INIT ADDONS 55 al_init_primitives_addon(); 56 al_install_mouse(); 57 al_init_image_addon(); 58 59 path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 60 //Assigning values and loading bitmaps 61 eventqueue = al_create_event_queue(); 62 timer = al_create_timer(1.0 / 60); 63 64 al_append_path_component(path, "resources"); 65 al_change_directory(al_path_cstr(path, '/')); 66 67 //Objects 68 MouseCursor mousecursor; 69 Background mainmenu; 70 Button buttonS, buttonO, buttonC, buttonStage, buttonE; 71 72 // LOAD IMAGES 73 mouseImage = al_load_bitmap("bitmaps/MyMouseClickObject.bmp"); 74 75 if(mouseImage == NULL) 76 { 77 al_show_native_message_box(display, "ERROR", "ERROR WITH LOADING BMP", "Could not load mymouseclickobject bmp file", NULL, NULL); 78 return -1; 79 } 80 81 al_convert_mask_to_alpha(mouseImage, al_map_rgb(255,0,255)); 82 83 backgroundImage = al_load_bitmap("bitmaps/MainMenuBackground.bmp"); 84 85 if(backgroundImage == NULL) 86 { 87 return -1; 88 } 89 90 sImage = al_load_bitmap("bitmaps/ButtonStart.bmp"); 91 92 oImage = al_load_bitmap("bitmaps/ButtonOptions.bmp"); 93 94 cImage = al_load_bitmap("bitmaps/ButtonCredits.bmp"); 95 96 stagesImage = al_load_bitmap("bitmaps/ButtonStages.bmp"); 97 98 exitImage = al_load_bitmap("bitmaps/ButtonExit.bmp"); 99 100 if(sImage == NULL) 101 { 102 al_show_native_message_box(display, "ERROR", "ERROR WITH LOADING BMPS", "Could not load one of the button bmp files", NULL , NULL); 103 return -1; 104 } 105 106 al_convert_mask_to_alpha(sImage, al_map_rgb(255,0,255)); 107 al_convert_mask_to_alpha(oImage, al_map_rgb(255,0,255)); 108 al_convert_mask_to_alpha(cImage, al_map_rgb(255,0,255)); 109 al_convert_mask_to_alpha(stagesImage, al_map_rgb(255,0,255)); 110 al_convert_mask_to_alpha(exitImage, al_map_rgb(255,0,255)); 111 // Initialize objects 112 mousecursor.InitMouseCursor(ScreenWidth, ScreenHeight, mouseImage); 113 mainmenu.InitBackground(backgroundImage); 114 115 InitButton(buttonS, buttonO, buttonC, buttonStage, buttonE, sImage, oImage, cImage, stagesImage, exitImage); 116 117 // Initialize fonts 118 119 // Register events 120 al_register_event_source(eventqueue, al_get_display_event_source(display)); 121 al_register_event_source(eventqueue, al_get_timer_event_source(timer)); 122 al_register_event_source(eventqueue, al_get_mouse_event_source()); 123 124 125 // start checking for input 126 al_start_timer(timer); 127 gametime = al_current_time(); 128 al_hide_mouse_cursor(display); 129 while(!Done) 130 { 131 ALLEGRO_EVENT ev; 132 al_wait_for_event(eventqueue, &ev); 133 134 if(ev.type == ALLEGRO_EVENT_MOUSE_AXES) 135 { 136 mousecursor.pos_x = ev.mouse.x; 137 mousecursor.pos_y = ev.mouse.y; 138 } 139 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 140 { 141 Done = true; 142 } 143 else if(ev.type == ALLEGRO_EVENT_TIMER) 144 { 145 frames++; 146 if(al_current_time() - gametime >= 1) 147 { 148 gametime = al_current_time(); 149 GAMEfps = frames; 150 frames = 0; 151 } 152 153 redraw = true; 154 } 155 156 157 if(redraw && al_is_event_queue_empty(eventqueue)) 158 { 159 redraw = false; 160 161 mainmenu.DrawBackground(); 162 163 if(!redraw) 164 { 165 redraw = true; 166 167 DrawButton(buttonS, buttonO, buttonC, buttonStage, buttonE); 168 169 mousecursor.DrawMouseCursor(); 170 171 } 172 173 174 al_flip_display(); 175 al_clear_to_color(al_map_rgb(0,0,0)); 176 } 177 178 } 179 180 181 //DESTROY ALL POINTER ALLEGRO VARIABLES 182 183 al_destroy_path(path); 184 al_destroy_display(display); 185 al_destroy_bitmap(exitImage); 186 al_destroy_bitmap(stagesImage); 187 al_destroy_bitmap(cImage); 188 al_destroy_bitmap(oImage); 189 al_destroy_bitmap(sImage); 190 al_destroy_bitmap(backgroundImage); 191 al_destroy_bitmap(mouseImage); 192 al_destroy_event_queue(eventqueue); 193 al_destroy_timer(timer); 194 195 196 197 return 0; 198}

Note that Buttons.h where I declare everything is just a struct with simple x,y and image with and height values along with an ALLEGRO_BITMAP *image;

Buttons.cpp

#SelectExpand
1// here everything is defined for objects that are of the button class 2 3#include "allegro5/allegro.h" 4#include "Buttons.h" 5 6 7void InitButton(Button &start, Button &Options, Button &Credits, Button &Stages, Button &Exit, 8 ALLEGRO_BITMAP *imageS, ALLEGRO_BITMAP *imageO, ALLEGRO_BITMAP *imageC, ALLEGRO_BITMAP *imageSt, ALLEGRO_BITMAP *imageE) 9{ 10 // start button 11 start.x = 5; 12 start.y = 70; 13 14 start.buttonW = 81; 15 start.buttonH = 42; 16 17 start.image = imageS; 18 // options button 19 Options.x = 5; 20 Options.y = 125; 21 22 Options.buttonW = 81; 23 Options.buttonH = 42; 24 25 Options.image = imageO; 26 //credits button 27 Credits.x = 5; 28 Credits.y = 180; 29 30 Credits.buttonW = 81; 31 Credits.buttonH = 42; 32 33 Credits.image = imageC; 34 // choose stage button 35 Stages.x = 5; 36 Stages.y = 237; 37 38 Stages.buttonW = 81; 39 Stages.buttonH = 42; 40 41 Stages.image = imageSt; 42 // exit button 43 Exit.x = 5; 44 Exit.y = 295; 45 46 Exit.buttonW = 81; 47 Exit.buttonH = 42; 48 49 Exit.image = imageE; 50} 51 52 53void DrawButton(Button &buttonS,Button &buttonO, Button &buttonC, Button &buttonStage, Button &buttonE) 54{ 55 // start button 56 al_draw_bitmap_region(buttonS.image, 0,0, buttonS.buttonW, buttonS.buttonH, buttonS.x, buttonS.y, 0); 57 // options button 58 al_draw_bitmap_region(buttonO.image, 0,0, buttonO.buttonW, buttonO.buttonH, buttonO.x, buttonO.y, 0); 59 //credits button 60 al_draw_bitmap_region(buttonC.image, 0,0, buttonC.buttonW, buttonC.buttonH, buttonC.x, buttonC.y, 0); 61 // choose stage button 62 al_draw_bitmap_region(buttonStage.image, 0,0, buttonStage.buttonW, buttonStage.buttonH, buttonStage.x, buttonStage.y, 0); 63 // exit button 64 al_draw_bitmap_region(buttonE.image, 0,0, buttonE.buttonW, buttonE.buttonH, buttonE.x, buttonE.y, 0); 65}

after putting in Button.h and Button.cpp the program started to get laggy so thats why I only put these two, I would really appreciate help, would hate to mess up and have to rewrite everything again although I'm thinking about it. Whats wrong with the code that makes it lag and go slow, could it be that I load all the bitmaps at once so sudden?

someone972

Your issue lays in this part of the code:

#SelectExpand
1 if(redraw && al_is_event_queue_empty(eventqueue)) 2 { 3 redraw = false; 4 5 mainmenu.DrawBackground(); 6 7 if(!redraw) 8 { 9 redraw = true; 10 11 DrawButton(buttonS, buttonO, buttonC, buttonStage, buttonE); 12 13 mousecursor.DrawMouseCursor(); 14 15 } 16 17 18 al_flip_display(); 19 al_clear_to_color(al_map_rgb(0,0,0)); 20 }

The problem is the if(!redraw) part. Redraw will always be false there (since you set it to false) and so it will always be set to true again in that block. This means it is constantly redrawing even if the timer has not gone off. Remove the if statement and the redraw = true part and it should work fine.

zeph173

Thank you for the help, I did as you said and its running good now and compiles fine. I feel stupid about that but the reason I added the If(!redraw) was because for some reason the mouse wouldn't move or update but now all of the sudden it does when I removed it. Thanks a ton though, this was making me think I got a memory leak or something but I'm glad I didn't.

Thread #609978. Printed from Allegro.cc