|
Sound load crash. |
RazorSharpFang
Member #14,438
July 2012
|
So, my game keeps crashing when I attempt to load the sound file. So far, all attempts have been unsuccessful, and it keeps crashing the moment I run the function al_load_sample(). 1#define ALLEGRO_STATICLINK
2#define bufferWidth 1920
3#define bufferHeight 1080
4#define FPS 45
5// ^ needed, below are includes
6#include <stdio.h>
7#include <stdlib.h>
8#include "allegro5/allegro.h"
9#include "allegro5/allegro_primitives.h"
10#include "allegro5/allegro_audio.h"
11#include "allegro5/allegro_acodec.h"
12#include "allegro5/allegro_image.h"
13#include "allegro5/allegro_native_dialog.h"
14#include "allegro5/allegro_font.h"
15#include "allegro5/allegro_ttf.h"
16#include "start.h"
17
18int main(int argc, char **argv)
19{
20 ALLEGRO_SAMPLE *sample;
21 Message("Sample declared\n");
22 AllegroSetUp();
23 Message("Allegro set up right.\n");
24 SetUpGame();
25 Message("Game is ready to play.\n");
26 al_reserve_samples(1);
27 Message("test sample reserved\n");
28 sample = al_load_sample("music.ogg");
29 Message("Sample loaded.\n");
30 al_play_sample(sample, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);
31 Message("Sample is playing...\n");
32 while(1)
33 {
34
35 }
36 ExitGame("End Of Game\n");
37}
And here is "start.h" Not every function in "start.h" is called by my main. 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}
I am seriously confused, because I'm not lacking anything that the tutorial is doing, but mine doesn't work, and theirs does. I don't like copy-pasting, and would prefer to understand WHY this code is malfunctioning, so I can learn from it. |
Cassio Renan
Member #14,189
April 2012
|
What's the output on allegro.log? |
RazorSharpFang
Member #14,438
July 2012
|
I do not get the Message "Sample loaded" in my log, |
Itachihro
Member #12,001
May 2010
|
Not really related to your problem, but is there some particular reason you keep the definitions of your functions in a header file (that doesn't have header guards either)? |
RazorSharpFang
Member #14,438
July 2012
|
Quite personally, I like my main function file, and main loop function files to be as clean as possible. My previous one was a disaster, totally uncluttered, and I couldn't debug my code when it came down to it. |
gnolam
Member #2,030
March 2002
|
Unfortunately, your personal choice is wrong. Now, splitting up code into multiple files is a good thing - but doing it by putting your code in files that you #include is not. Use header files for declarations only, and keep your definitions in .c/.cpp files. There are plenty of reasons for this, which someone with more time than I have right now can fill you in on, so I'll just give you the one you'll notice right away: you won't need to recompile everything whenever you change something. You just recompile the specific source file(s) you've edited. RazorSharpFang said: Also, what's a header guard? A header guard prevents a header from being included more than once. Typically, something like: 1#ifndef FOO_H
2#define FOO_H
3
4// Declarations here
5
6#endif
-- |
RazorSharpFang
Member #14,438
July 2012
|
OH! Is that why we have header files? |
|