1FILE
*log; //log file. Write messages to this for debugging
2ALLEGRO_DISPLAY *display
; //the main display.
3ALLEGRO_BITMAP *screenBuffer
; //screen buffer
4ALLEGRO_TIMER *timer
; //The time for game, operates at 45fps
5ALLEGRO_EVENT_QUEUE *eventQueue
; //event queue, where events will be held.
6int screenWidth, screenHeight
;//global scaling variables.
7////////////////////// now functions used to set up stuff.
8int AllegroSetUp
();
9//sets up ALL allegro needed functions. Should be first line of code...
10void SetUpGame
();
11//calls other set up functions to clean up code
12void AbortGame
(const char* message
);
13//avoids redundancy, abort game after message display
14ALLEGRO_DISPLAY* SetUpDisplay
();
15//set the display to fullscreen, no res change
16//returns the display
17ALLEGRO_TIMER* SetUpTimer
();
18//sets up the timer, ticks 45 times each second...
19FILE
* SetUpLog
();
20//prepares the log file for logging...
21void Message
(const char *input
);
22//standard message, output to terminal, and out to the log file as well. Useful for debugging...
23ALLEGRO_BITMAP* SetUpScreenBuffer
();
24// Returns the screen buffer which will be used for resolution independace. Write to this, then write this
25// to the screen after finished drawing. Scale as needed.
26void ExitGame
(const char *message
);
27//Cleans up buffer, destroys display and writes logs. Use this to close.
28void DisplayBuffer
();
29//closes the log, destroys buffer and screen and exits.
30void SetUpEvents
();
31//sets up the event queue. Affects the global eventQueue variable.
32int AllegroSetUp
()
33{
34 if(!al_init()) {
35 AbortGame
("Allegro Error, fatal.\n");
36 }
37 Message
("Allegro Initialized Successfully\n");
38 if (!al_install_keyboard()) {
39 AbortGame
("Failed to install keyboard\n");
40 }
41 Message
("Keyboard initialized successfully\n");
42 if (!al_install_mouse()) {
43 AbortGame
("Failure to install mouse\n");
44 }
45 Message
("Mouse installed successfully!\n");
46 if (!al_init_primitives_addon()) {
47 AbortGame
("Failure to load primitives!\n");
48 }
49 Message
("Primatives addon loaded successfully\n");
50 if(!al_init_image_addon()) {
51 AbortGame
("Failure to initialize image handler\n");
52 }
53 Message
("Image addon initialized successfully!\n");
54 al_init_font_addon();
55 Message
("Font addon load completed. Hope it worked.\n");
56 if (!al_init_ttf_addon()) {
57 AbortGame
("Failure to load font reader!\n");
58 }
59 Message
("True Type Font addon initialized successfully\n");
60 if(!al_install_audio()) {
61 AbortGame
("Failure to load sound\n");
62 }
63 Message
("Audio installed successfully\n");
64 if(!al_init_acodec_addon()) {
65 AbortGame
("Audio Codec Failure!\n");
66 }
67 Message
("Audio Codec (LibVorbis) loaded successfully\n");
68 return 0;
69}
70
71void SetUpGame
()
72{
73 log = SetUpLog
();
74 display
= SetUpDisplay
();
75 timer
= SetUpTimer
();
76 screenWidth
= al_get_display_width(display
);
77 screenHeight
= al_get_display_height(display
);
78 screenBuffer
= SetUpScreenBuffer
();
79 al_flip_display();
80 SetUpEvents
();
81 //al_hide_mouse_cursor(display);
82}
83
84ALLEGRO_TIMER* SetUpTimer
()
85{
86 ALLEGRO_TIMER* time = al_create_timer(1.
0 / FPS
);
87 if (!time) {
88 AbortGame
("Failed to create timer\n");
89 }
90 Message
("Timer created successfully\n");
91 return time;
92}
93
94ALLEGRO_DISPLAY* SetUpDisplay
()
95{
96 ALLEGRO_DISPLAY *disp
= NULL
;
97 ALLEGRO_DISPLAY_MODE disp_data
;
98 al_get_display_mode(al_get_num_display_modes() - 1,
&disp_data
);
99 Message
("Display resolution enunciated successfully: width:");
100 char buffer
[30];
101 sprintf(buffer,
"%d height: %d\n",disp_data.width,disp_data.height
);
102 Message
(buffer
);
103 al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW
);
104 disp
= al_create_display(disp_data.width, disp_data.height
);
105 if (!disp
) {
106 AbortGame
("Failed to create display");
107 }
108 Message
("Display created successfully, width:");
109 sprintf(buffer,
"%d height: %d\n",
al_get_display_width(disp
),
al_get_display_height(disp
));
110 Message
(buffer
);
111 al_clear_to_color(al_map_rgb(0,
0,
0));
112 return disp
;
113}
114
115void AbortGame
(const char* message
)
116{
117 printf("%s \n", message
);
118 fprintf(log,
"%s \n", message
);
119 al_show_native_message_box(display,
"Error!",
"Error! :O ", message, NULL, ALLEGRO_MESSAGEBOX_ERROR
);
120 fclose(log);
121 exit(1);
122}
123
124FILE
* SetUpLog
()
125{
126 FILE
*fp
= NULL
;
127 fp
= fopen("log.log",
"w");
128 fprintf(fp,
"Game started\n");
129 printf("Game started\n");
130 return fp
;
131}
132
133void Message
(const char* message
)
134{
135 printf(message
);
136 fprintf(log,message
);
137 return;
138}
139
140ALLEGRO_BITMAP* SetUpScreenBuffer
()
141{
142 ALLEGRO_BITMAP *buffer
= al_create_bitmap(bufferWidth,bufferHeight
);
143 al_set_target_bitmap(buffer
);
144 if(!buffer
) {
145 AbortGame
("Error setting up screen buffer\n");
146 }
147 Message
("Buffer created successfully, 1920x1080 used.\n");
148 if((al_get_bitmap_flags(buffer
))&(ALLEGRO_VIDEO_BITMAP
)) {
149 Message
("Buffer has video accelleration - Good.\n");
150 } else {
151 Message
("Buffer does NOT have video accelleration - BAD, but not fatal.\n");
152 }
153 return buffer
;
154}
155
156void ExitGame
(const char *message
)
157{
158 char buffer
[20];
159 sprintf(buffer,
"Game is closing...%s\n",message
);
160 Message
(buffer
);
161 Message
("Destroying buffer...\n");
162 al_destroy_bitmap(screenBuffer
);
163 Message
("Buffer Destroyed\n");
164 Message
("Destroying display...\n");
165 al_destroy_display(display
);
166 Message
("Display and buffer gone, exiting...\n");
167 fclose(log);
168 exit(0);
169}
170
171void DisplayBuffer
()
172{
173 al_set_target_backbuffer(display
);
174 //al_clear_to_color(al_map_rgb(0,0,0));
175 //al_draw_scaled_bitmap(screenBuffer, 0, 0, bufferWidth, bufferHeight, scaleX, scaleY, scaleW, scaleH, 0);
176 al_draw_scaled_bitmap(screenBuffer,
0,
0,bufferWidth,bufferHeight,
0,
0,screenWidth,screenHeight,
0);
177 al_flip_display();
178 al_set_target_bitmap(screenBuffer
);
179 al_clear_to_color(al_map_rgb(0,
0,
0));
180}
181
182void SetUpEvents
()
183{
184 eventQueue
= al_create_event_queue();
185 al_register_event_source(eventQueue,
al_get_keyboard_event_source());
186 al_register_event_source(eventQueue,
al_get_display_event_source(display
));
187 al_register_event_source(eventQueue,
al_get_timer_event_source(timer
));
188 al_register_event_source(eventQueue,
al_get_mouse_event_source());
189 //This ^ line can be commented out to prevent INSANE lag
190}