Loop ran out of time from drawing
Bingocat

I've been making a program, and for some reason it crashes if I have draw too much stuff. I've tested it, and if I draw one less thing, it doesn't crash. When I started using ENet and Allegro together, they didn't work because of timing so I asked why on here I think and someone said to change it to that. Someone also said something about multithreading or something when I asked about it. How would I add that to my program? I need the ENet timing thing and Allegro timing in different threads. How could I do this? I can't really find anything on google. Should I use multithreading or something else? Here is the code for the server.

#SelectExpand
1#define WIN32_LEAN_AND_MEAN 2 3#include <allegro5\allegro.h> 4#include <allegro5\allegro_primitives.h> 5#include <allegro5\allegro_font.h> 6#include <allegro5\allegro_ttf.h> 7#include <enet\enet.h> 8#include <iostream> 9#include <sstream> 10#include <string> 11#include <Windows.h> 12#include <vector> 13 14using namespace std; 15 16const int screenWidth = 640, screenHeight = 480; 17 18struct Button 19{ 20 POINT pos; 21 bool active; 22 string text; 23}; 24 25struct Menu 26{ 27 POINT pos; 28 int width, height; 29 vector<Button> buttons; 30 bool active; 31 string text; 32}; 33 34void InitButtons(vector<Menu> &menus); 35void DrawButtons(vector<Menu> menus, ALLEGRO_FONT *font18); 36 37int main() { 38 bool done = false; 39 bool redraw = true; 40 bool input = false; 41 bool connected = false; 42 POINT mouse; 43 POINT clientMouse; 44 vector<Menu> menus; 45 46 system("title Server"); 47 48 al_init(); 49 al_init_primitives_addon(); 50 al_init_font_addon(); 51 al_init_ttf_addon(); 52 al_install_mouse(); 53 al_install_keyboard(); 54 if (!enet_initialize()) 55 cout << "Initialized ENet\n"; 56 57 ENetAddress address; 58 ENetHost * server; 59 ENetEvent event; 60 61 address.host = ENET_HOST_ANY; 62 address.port = 12345; 63 server = enet_host_create(&address, 32, 1, 0, 0); 64 65 if (address.port != NULL) 66 cout << "Binded to port " << address.port << endl; 67 if (server != NULL) 68 cout << "Successfully created ENet Server\n"; 69 70 if (server == NULL) { 71 cout << "ERROR: Could not create ENet Server " << endl << "Press any key to exit...\n"; 72 system("pause>nul"); 73 exit(EXIT_FAILURE); 74 } 75 76 ALLEGRO_DISPLAY *display = NULL; 77 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 78 ALLEGRO_TIMER *timer = NULL; 79 ALLEGRO_FONT *font18 = al_load_font("arial.ttf", 18, 0); 80 81 display = al_create_display(screenWidth, screenHeight); 82 event_queue = al_create_event_queue(); 83 timer = al_create_timer(1.0 / 60.0); 84 85 al_register_event_source(event_queue, al_get_keyboard_event_source()); 86 al_register_event_source(event_queue, al_get_mouse_event_source()); 87 al_register_event_source(event_queue, al_get_display_event_source(display)); 88 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 89 90 InitButtons(menus); 91 92 al_start_timer(timer); 93 94 while (!done) { 95 96 97 ALLEGRO_EVENT ev; 98 al_wait_for_event(event_queue, &ev); 99 100 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 101 done = true; 102 } 103 else if (ev.type == ALLEGRO_EVENT_TIMER) { 104 redraw = true; 105 106 for (unsigned int i = 0; i < menus.size(); i++) { 107 for (unsigned int j = 0; j < menus[i].buttons.size(); j++) { 108 if (menus[i].buttons[j].active) { 109 menus[i].buttons[j].active = false; 110 111 if (j == 0) 112 input = true; 113 } 114 } 115 } 116 } 117 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { 118 switch (ev.keyboard.keycode) { 119 case ALLEGRO_KEY_ESCAPE: 120 done = true; 121 break; 122 case ALLEGRO_KEY_LSHIFT: 123 input = !input; 124 if (input) { 125 al_grab_mouse(display); 126 al_set_mouse_xy(display, screenWidth / 2, screenHeight / 2); 127 } 128 else 129 al_ungrab_mouse(); 130 break; 131 } 132 } 133 else if (ev.type == ALLEGRO_EVENT_KEY_UP) { 134 /*switch (ev.keyboard.keycode) { 135 136 }*/ 137 } 138 else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) { 139 mouse.x = ev.mouse.x; 140 mouse.y = ev.mouse.y; 141 142 if (input) { 143 int dirX, dirY; 144 dirX = -(screenWidth / 2 - mouse.x); 145 dirY = -(screenHeight / 2 - mouse.y); 146 147 char packet[256]; 148 sprintf_s(packet, sizeof(packet), "M,%i,%i", dirX, dirY); 149 ENetPacket *p = enet_packet_create((char*)packet, strlen(packet)+1, NULL); 150 //printf("Sent a packet to client containing %s\n", packet2); 151 enet_host_broadcast(server, 0, p); 152 153 al_set_mouse_xy(display, screenWidth / 2, screenHeight / 2); 154 } 155 else { 156 for (unsigned int i = 0; i < menus.size(); i++) { 157 if (mouse.x > menus[i].pos.x && mouse.x < menus[i].pos.x + menus[i].width && 158 mouse.y > menus[i].pos.y && mouse.y < menus[i].pos.y + menus[i].height) { 159 menus[i].active = true; 160 } 161 if (mouse.x < menus[i].pos.x || mouse.x > menus[i].pos.x + menus[i].width || 162 mouse.y < menus[i].pos.y || mouse.y > menus[i].pos.y + menus[i].height + menus[i].buttons.size() * 30) { 163 menus[i].active = false; 164 } 165 } 166 } 167 } 168 else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { 169 if (input) { 170 if (ev.mouse.button == 1) { 171 char packet[256]; 172 sprintf_s(packet, sizeof(packet), "CD,%i", 15); 173 ENetPacket *p = enet_packet_create((char*)packet, strlen(packet)+1, NULL); 174 enet_host_broadcast(server, 0, p); 175 } 176 else if (ev.mouse.button == 2) { 177 char packet[256]; 178 sprintf_s(packet, sizeof(packet), "RD,%i", 15); 179 ENetPacket *p = enet_packet_create((char*)packet, strlen(packet)+1, NULL); 180 enet_host_broadcast(server, 0, p); 181 } 182 } 183 else { 184 for (unsigned int i = 0; i < menus.size(); i++) { 185 for (unsigned int j = 0; j < menus[i].buttons.size(); j++) { 186 if (mouse.x > menus[i].buttons[j].pos.x && mouse.x < menus[i].buttons[j].pos.x && 187 mouse.y > menus[i].buttons[j].pos.y && mouse.y < menus[i].buttons[j].pos.y && 188 menus[i].active) { 189 menus[i].buttons[j].active = true; 190 } 191 } 192 } 193 } 194 } 195 else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 196 if (input) { 197 if (ev.mouse.button == 1) { 198 char packet[256]; 199 sprintf_s(packet, sizeof(packet), "CU,%i", 15); 200 ENetPacket *p = enet_packet_create((char*)packet, strlen(packet)+1, NULL); 201 enet_host_broadcast(server, 0, p); 202 } 203 else if (ev.mouse.button == 2) { 204 char packet[256]; 205 sprintf_s(packet, sizeof(packet), "RU,%i", 15); 206 ENetPacket *p = enet_packet_create((char*)packet, strlen(packet)+1, NULL); 207 enet_host_broadcast(server, 0, p); 208 } 209 } 210 } 211 212 if (redraw && al_is_event_queue_empty(event_queue)) { 213 redraw = false; 214 215 al_clear_to_color(al_map_rgb(0,89,255)); 216 DrawButtons(menus, font18); 217 if (connected) 218 al_draw_textf(font18, al_map_rgb(255, 255, 255), screenWidth / 2, screenHeight / 2, ALLEGRO_ALIGN_CENTRE, "Client Mouse X: %i Y: %i", clientMouse.x, clientMouse.y); 219 else 220 al_draw_text(font18, al_map_rgb(255, 255, 255), screenWidth / 2, screenHeight / 2, ALLEGRO_ALIGN_CENTRE, "Waiting for connection..."); 221 al_flip_display(); 222 } 223 } 224} 225 226void InitButtons(vector<Menu> &menus) { 227 Menu newMenu; 228 newMenu.active = false; 229 newMenu.width = 216; 230 newMenu.height = 30; 231 newMenu.pos.x = 0; 232 newMenu.pos.y = 0; 233 newMenu.text = "Mouse"; 234 235 Button newButton; 236 newButton.active = false; 237 newButton.pos.x = 0; 238 newButton.pos.y = 30; 239 newButton.text = "Control Mouse"; 240 newMenu.buttons.push_back(newButton); 241 242 menus.push_back(newMenu); 243 244} 245 246void DrawButtons(vector<Menu> menus, ALLEGRO_FONT *font18) { 247 for (unsigned int i = 0; i < menus.size(); i++) { 248 al_draw_filled_rectangle(menus[i].pos.x, menus[i].pos.y, menus[i].pos.x + menus[i].width, menus[i].pos.y + menus[i].height, al_map_rgb(0, 0, 255)); 249 al_draw_rectangle(menus[i].pos.x, menus[i].pos.y, menus[i].pos.x + menus[i].width, menus[i].pos.y + menus[i].height, al_map_rgb(0, 0, 0), 2); 250 al_draw_text(font18, al_map_rgb(255, 255, 255), menus[i].pos.x + menus[i].width / 2, menus[i].pos.y + menus[i].height / 2 - 10, ALLEGRO_ALIGN_CENTRE, menus[i].text.c_str()); 251 if (menus[i].active) { 252 for (unsigned int j = 0; j < menus[j].buttons.size(); j++) { 253 al_draw_filled_rectangle(menus[i].buttons[j].pos.x, menus[i].buttons[j].pos.y, menus[i].buttons[j].pos.x + menus[i].width, menus[i].buttons[j].pos.y + menus[i].height, al_map_rgb(0, 0, 255)); 254 al_draw_rectangle(menus[i].buttons[j].pos.x, menus[i].buttons[j].pos.y, menus[i].buttons[j].pos.x + menus[i].width, menus[i].buttons[j].pos.y + menus[i].height, al_map_rgb(0, 0, 0), 2); 255 al_draw_text(font18, al_map_rgb(255, 255, 255), menus[i].buttons[j].pos.x + menus[i].width / 2, menus[i].buttons[j].pos.y + menus[i].height / 2 - 10, ALLEGRO_ALIGN_CENTRE, menus[i].buttons[j].text.c_str()); 256 } 257 } 258 } 259}

Dizzy Egg
for (unsigned int j = 0; j < menus[j].buttons.size(); j++) {

Shouldn't that be menus [i].buttons.size()

Thread #614239. Printed from Allegro.cc