Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » What's wront with this (or me)

This thread is locked; no one can reply to it. rss feed Print
What's wront with this (or me)
Ariesnl
Member #2,902
November 2002
avatar

This code was stripped from my game to make a basic Allegro 5 setup...

However for some obscure reason I can't get it to run...
segmentation fault, illegal call etc...

Anyone ?

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_image.h> 4#include <allegro5/allegro_primitives.h> 5#include <allegro5/allegro_font.h> 6#include <allegro5/allegro_ttf.h> 7#include <allegro5/allegro_audio.h> 8#include <allegro5/allegro_acodec.h> 9 10// The display for graphics... 11ALLEGRO_DISPLAY * g_pDisplay = NULL; 12 13// The event queue 14ALLEGRO_EVENT_QUEUE * g_pEventQueue = NULL; 15 16// Global Path 17ALLEGRO_PATH * g_pPath = NULL; 18 19// a font for text 20ALLEGRO_FONT * g_pFont; 21 22 23 24// Other Globals 25bool g_blQuit = false; 26bool g_blRedraw = false; 27bool g_blGameRunning = true; 28double g_nCyclesPerSecond = 60; 29ALLEGRO_COLOR g_clBLACK; 30 31int g_nScreenWidth; 32int g_nScreenHeight; 33 34ALLEGRO_TIMER * g_pTimer =0; 35 36//Forward functions 37void get_desktop_resolution(int a_nAdapter, int *a_pWidth, int * a_pHeighth); 38 39// Setup Allegro 40bool Setup() 41{ 42 if(al_init()) 43 { 44 45#ifdef _DEBUG 46 g_nScreenWidth = 800; 47 g_nScreenHeight = 600; 48#else 49 get_desktop_resolution(0, &g_nScreenWidth, &g_nScreenHeight); 50 al_set_new_display_flags(ALLEGRO_FULLSCREEN); 51#endif 52 53 al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST); 54 g_pDisplay = al_create_display(g_nScreenWidth, g_nScreenHeight); 55 if (g_pDisplay != NULL) 56 { 57 // Install allegro modules 58 al_init_primitives_addon(); 59 al_init_image_addon(); 60 //al_init_font_addon(); 61 //al_init_ttf_addon(); 62 al_install_keyboard(); 63 al_install_mouse(); 64 //al_install_audio(); 65 al_init_acodec_addon(); 66 67 68 g_pTimer=al_create_timer(1.000/g_nCyclesPerSecond); 69 70 // install event queue 71 g_pEventQueue = al_create_event_queue(); 72 al_register_event_source(g_pEventQueue, al_get_keyboard_event_source()); 73 al_register_event_source(g_pEventQueue, al_get_mouse_event_source()); 74 al_register_event_source(g_pEventQueue, al_get_display_event_source(g_pDisplay)); 75 al_register_event_source(g_pEventQueue, al_get_timer_event_source(g_pTimer)); 76 77 78 // switch to correct path 79 g_pPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 80 _chdir(al_path_cstr(g_pPath, '\\')); 81 82 83 g_clBLACK = al_map_rgb(0,0,0); 84 // set quit to false 85 g_blQuit = false; 86 al_hide_mouse_cursor(g_pDisplay); 87 al_start_timer(g_pTimer); 88 } 89 else 90 { 91 return false; 92 } 93 } 94 else 95 { 96 return false; 97 } 98 99 return true; 100} 101 102void get_desktop_resolution(int a_nAdapter, int *a_pWidth, int * a_pHeighth) 103{ 104 ALLEGRO_MONITOR_INFO info; 105 al_get_monitor_info(a_nAdapter, &info); 106 107 *a_pWidth = info.x2 - info.x1; 108 *a_pHeighth = info.y2 - info.y1; 109} 110 111 112// Shutdown Allegro 113void ShutDown() 114{ 115 al_destroy_event_queue(g_pEventQueue); 116 al_destroy_timer(g_pTimer); 117 al_destroy_display(g_pDisplay); 118 //al_destroy_font(g_pFont); 119 al_destroy_path(g_pPath); 120 al_uninstall_system(); 121} 122 123 124// Render the view 125void Render() 126{ 127 al_clear_to_color(m_clBLACK); 128 129 if (g_blGameRunning) 130 { 131 132 } 133 134 al_flip_display(); 135} 136 137 138void GameCycle() 139{ 140 141 142} 143 144 145 146void KeyDown(int a_nKey) 147{ 148 149 switch(a_nKey) 150 { 151 case ALLEGRO_KEY_F1: 152 al_hide_mouse_cursor(g_pDisplay); 153 break; 154 155 case ALLEGRO_KEY_F2: 156 al_show_mouse_cursor(g_pDisplay); 157 break; 158 } 159 160} 161 162void KeyUp(int a_nKey) 163{ 164 switch(a_nKey) 165 { 166 case ALLEGRO_KEY_ESCAPE: 167 g_blQuit = true; 168 break; 169 } 170 171} 172 173void Mouse(ALLEGRO_MOUSE_EVENT * mouse_event) 174{ 175 176} 177 178 179void Timer(ALLEGRO_TIMER_EVENT * timer_event) 180{ 181 if (g_blGameRunning) 182 { 183 GameCycle(); 184 } 185 else 186 { 187 188 189 } 190} 191 192 193int main(int argc, char **argv) 194{ 195 if (Setup()) 196 { 197 while (!g_blQuit) 198 { 199 ALLEGRO_EVENT ev; 200 if (al_get_next_event(g_pEventQueue, &ev)) 201 { 202 203 switch (ev.type) 204 { 205 case ALLEGRO_EVENT_KEY_DOWN: 206 KeyDown(ev.keyboard.keycode); 207 break; 208 case ALLEGRO_EVENT_KEY_UP: 209 KeyUp(ev.keyboard.keycode); 210 break; 211 212 case ALLEGRO_EVENT_DISPLAY_CLOSE: 213 g_blQuit = true; 214 break; 215 216 case ALLEGRO_EVENT_MOUSE_AXES: 217 Mouse(&ev.mouse); 218 break; 219 220 case ALLEGRO_EVENT_TIMER: 221 Timer(&ev.timer); 222 break; 223 224 } 225 226 } 227 else 228 { 229 Render(); 230 231 } 232 } 233 } 234 else 235 { 236 return -1; 237 } 238 ShutDown(); 239 return 0; 240}

EDIT seems like the mt version of the library is causing trouble... with the md version everything works fine......

- Wisdom is the art of using knowledge
- String theory: There's music in everything

Raidho36
Member #14,628
October 2012
avatar

Try debugging it, set breakpoint to the first line of your main and go step by step, jumping into every function and see what exactly is causing your segfaults and other errors.

You should probably instantly return failure when anything fails rather than embracing whole thing into a block.

Polybios
Member #12,293
October 2010

If you're on Linux, you could use valgrind to determine the exact cause of the problem.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Gideon Weems
Member #3,925
October 2003

Polybios said:

If you're on Linux, you could use valgrind to determine the exact cause of the problem.

Thanks for pointing out this lovely program.

Go to: