[A5]ALLEGRO_SAMPLE_ID return id -1
Matheus Santos

sorry for the english, I'm brazilian and I need help ...
ALLEGRO_SAMPLE_ID always the id is returning -1
I stated the ALLEGRO_SAMPLE_ID in a header and I am trying to access it in another. cpp but it always returns the id - 1
I'll post the part of the code here:

.h
ALLEGRO_SAMPLE_ID teste;

.cpp

al_play_sample(sample_cowboys, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, &teste);
al_stop_sample(&teste); // that part of the runtime error!!

help me please!

SiegeLord

You probably didn't call al_reserve_samples, or called it with too small a number. Also, you should check that al_play_sample returns true before you use the ALLEGRO_SAMPLE_ID for anything.

Matheus Santos

i'm calling al_reserve_samples (1);
al_play_sample is returning true

I tried to increase the number of al_reserve_samples but not solved

code

al_reserve_samples(10);
sample_cowboys = al_load_sample("aud/teste.ogg");

torhu

Did you also call al_install_audio and al_init_acodec_addon?

EDIT: If you declare a variable in a header, you have to put extern in front of it, to avoid getting multiple definitions.

// Declare in a header:
extern ALLEGRO_SAMPLE_ID teste;

// Define in a single cpp:
ALLEGRO_SAMPLE_ID teste;

Then it can be used in every .cpp file that included the header with the declaration.

Matheus Santos

yes, my only problem is the ALLEGRO_SAMPLE_ID the moment I use al_stop_sample.

#SelectExpand
1 2Viewinit.cpp 3viewInit::viewInit(){ // Class only for init al settings 4 5 if(!al_init()) { 6 fprintf(stderr, "Falha ao iniciar o allegro\n"); 7 } 8 if (!al_install_keyboard()) 9 { 10 fprintf(stderr, "Falha ao inicializar o teclado.\n"); 11 } 12 13 if (!al_install_audio()) 14 { 15 fprintf(stderr, "Falha ao inicializar áudio.\n"); 16 17 } 18 19 if (!al_init_acodec_addon()) 20 { 21 fprintf(stderr, "Falha ao inicializar codecs de áudio.\n"); 22 exit(1); 23 } 24 25 al_init_image_addon(); 26 al_init_font_addon(); 27 al_init_ttf_addon(); 28 29 30 //al_rest(10.0); 31 32 //al_destroy_display(display); 33 34} 35 36viewGame.h 37 38ALLEGRO_SAMPLE_ID teste; 39 40viewGame.cpp 41// this in other class and cpp 42#include "viewGame.h" 43 44viewGame::viewGame(){ // 45 al_reserve_samples(1); 46 sample_cowboys = al_load_sample("aud/bingo_1l.ogg"); 47 48} 49 50void viewGame::draw_anim_cowboys(){ 51 al_play_sample(sample_cowboys, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, &teste); 52 al_stop_sample(&teste); 53}

EDIT

the ALLEGRO_SAMPLE_ID this in a class

torhu

And you are sure that viewInit is actually constructed before viewGame? It's hard to tell when you only post snippets of code...

Matheus Santos
#SelectExpand
1//viewGame.h 2#include "libs.h" 3#include "viewGlobal.h" 4#include "controller.h" 5 6class viewGame: public viewGlobal{ 7protected: 8 9 // IMAGENS 10 11 12 ALLEGRO_BITMAP *img_bg[24]; // Imagens bg de acordo com o horário 13 14 ALLEGRO_BITMAP *anim_full_rabo_cavalos; 15 ALLEGRO_BITMAP *anim_rabo_cavalos[35]; 16 17 ALLEGRO_BITMAP *anim_full_trem; 18 ALLEGRO_BITMAP *anim_trem[125]; 19 20 ALLEGRO_BITMAP *anim_full_logo; 21 ALLEGRO_BITMAP *anim_logo[70]; 22 23 ALLEGRO_BITMAP *anim_full_cowboys[4]; 24 ALLEGRO_BITMAP *anim_full_bingo; 25 ALLEGRO_BITMAP *anim_full_acumulado; 26 27 ALLEGRO_BITMAP *anim_cowboys[177]; 28 ALLEGRO_BITMAP *anim_bingo[30]; 29 ALLEGRO_BITMAP *anim_acum[39]; 30 31 ALLEGRO_BITMAP *img_placa_acum; 32 33 34 // FONTES 35 ALLEGRO_FONT *font_display; 36 37 // timer para começar animações 38 ALLEGRO_TIMER *timer_anim_cavalo; 39 ALLEGRO_TIMER *timer_anim_trem; 40 ALLEGRO_TIMER *timer_anim_logo; 41 42 43 // timer para trocar o frame de cada animação 44 ALLEGRO_TIMER *timer_frame_cavalo; 45 ALLEGRO_TIMER *timer_frame_trem; 46 ALLEGRO_TIMER *timer_frame_logo; 47 48 ALLEGRO_TIMER *timer_frame_cowboys; 49 ALLEGRO_TIMER *timer_frame_bingo; 50 ALLEGRO_TIMER *timer_frame_acumulado; 51 52 // Eventos 53 ALLEGRO_EVENT_QUEUE *fila_eventos; 54 55 56 // Frame animações 57 58 int frame_anim[10]; // Controla os frames das animações 59 60 // controle de animações 61 62 int play_anim_cowboys; // 1 se tiver que fazer a animação 63 int play_anim_bingo; 64 int play_anim_acum; 65 66 int status_anim[10]; //1 = começa a animação; 2 = troca o frame 67 68 // Sons 69 70 ALLEGRO_AUDIO_STREAM *music_main; 71 ALLEGRO_AUDIO_STREAM *music_bingo; 72 ALLEGRO_AUDIO_STREAM *music_acumulado; 73 74 ALLEGRO_SAMPLE *sample_tiro; 75 ALLEGRO_SAMPLE *sample_acum; 76 ALLEGRO_SAMPLE *sample_bingo; 77 ALLEGRO_SAMPLE *sample_cowboys; 78 79 ALLEGRO_SAMPLE_ID id_sample_cowboys; 80 81 ALLEGRO_SAMPLE_ID teste; 82 83 // Outros 84 85 char aux[50]; // LoadImage 86 int x; // Loop 87 88 89 char tempo[60]; 90 int i; 91 time_t hora; 92 93 94public: 95 viewGame(); // Carrega todas as imagens e variaveis que ira usar durante a execução do mesmo 96 97 void main(controller refcontroller); // Faz o controle das animações; 98 99 // METODOS DE ANIMAÇÂO 100 void draw_anim_cavalo(); 101 void draw_anim_logo(); 102 void draw_anim_trem(); 103 104 void draw_anim_bingo(); 105 void draw_anim_acum(); 106 107 void draw_anim_cowboys(int status); 108 109 // ---------------------- 110 111 void play_main_music(); 112 113 int get_hour_bg(); 114 // 115 void input(); 116 117 ALLEGRO_EVENT get_event(); 118 119 void get_status(); 120 121 122}; 123 124//viewGame.cpp 125 126#include "viewGame.h" 127 128 129viewGame::viewGame(){ 130 131 //fprintf(stderr, "Teste construtor viewGame\n"); 132 cout << "construtor viewGame OK.\n"; 133 134 135 // ANIMA LOGO 136 for(x = 0; x < 70 ; x++){ // Carregando animação do rabo do cavalo 137 anim_logo[x] = al_create_bitmap(543, 411); 138 al_set_target_bitmap(anim_logo[x]); 139 al_draw_bitmap_region(anim_full_logo, 0 , 411 * x, 543, 411, 0, 0, 0); 140 } 141 142 // ANIMAÇÂO COWBOYS 143 for(x = 0; x < 177; x++){ 144 145 anim_cowboys[x] = al_create_bitmap(750, 294); 146 al_set_target_bitmap(anim_cowboys[x]); 147 148 if(x < 50) al_draw_bitmap_region(anim_full_cowboys[0], 0 , 294 * x, 750, 294, 0, 0, 0); 149 if(x > 49 && x < 100) al_draw_bitmap_region(anim_full_cowboys[1], 0 , 294 *( x - 50 ), 750, 294, 0, 0, 0); 150 if(x > 99 && x < 150) al_draw_bitmap_region(anim_full_cowboys[2], 0 , 294 *( x - 100 ), 750, 294, 0, 0, 0); 151 if(x > 149 && x < 177) al_draw_bitmap_region(anim_full_cowboys[3], 0 , 294 *( x - 150 ), 750, 294, 0, 0, 0); 152 153 al_convert_mask_to_alpha(anim_cowboys[x], al_map_rgb(255,0,255)); 154 155 } 156 157 for(x = 0; x < 30; x++){ 158 anim_bingo[x] = al_create_bitmap(750, 294); 159 al_set_target_bitmap(anim_bingo[x]); 160 161 al_draw_bitmap_region(anim_full_bingo, 0 , 294 * x, 750, 294, 0, 0, 0); 162 al_convert_mask_to_alpha(anim_bingo[x], al_map_rgb(255,0,255)); 163 } 164 165 for(x = 0; x < 39; x++){ 166 anim_acum[x] = al_create_bitmap(750, 294); 167 al_set_target_bitmap(anim_acum[x]); 168 169 al_draw_bitmap_region(anim_full_acumulado, 0 , 294 * x, 750, 294, 0, 0, 0); 170 al_convert_mask_to_alpha(anim_acum[x], al_map_rgb(255,0,255)); 171 } 172 173 al_set_target_bitmap(al_get_backbuffer(display)); 174 175 // FONTES 176 font_display = al_load_font("digital display tfb.ttf", 160, 0); 177 178 // TIMERS 179 180 timer_anim_cavalo = al_create_timer(5.0); 181 timer_frame_cavalo = al_create_timer(0.05); 182 183 timer_anim_trem = al_create_timer(5.0); 184 timer_frame_trem = al_create_timer(0.05); 185 186 timer_anim_logo = al_create_timer(5.0); 187 timer_frame_logo = al_create_timer(0.05); 188 189 timer_frame_cowboys = al_create_timer(0.04); 190 timer_frame_bingo = al_create_timer(0.04); 191 timer_frame_acumulado = al_create_timer(0.04); 192 193 194 // EVENTOS - TIMER 195 fila_eventos = al_create_event_queue(); 196 197 198 al_register_event_source(fila_eventos, al_get_display_event_source(display)); // Registra os eventos da janela 199 200 // Registra evento dos timers 201 al_register_event_source(fila_eventos, al_get_timer_event_source(timer_anim_cavalo)); 202 al_register_event_source(fila_eventos, al_get_timer_event_source(timer_frame_cavalo)); 203 204 al_register_event_source(fila_eventos, al_get_timer_event_source(timer_anim_trem)); 205 al_register_event_source(fila_eventos, al_get_timer_event_source(timer_frame_trem)); 206 207 al_register_event_source(fila_eventos, al_get_timer_event_source(timer_anim_logo)); 208 al_register_event_source(fila_eventos, al_get_timer_event_source(timer_frame_logo)); 209 210 al_register_event_source(fila_eventos, al_get_timer_event_source(timer_frame_cowboys)); 211 al_register_event_source(fila_eventos, al_get_timer_event_source(timer_frame_bingo)); 212 al_register_event_source(fila_eventos, al_get_timer_event_source(timer_frame_acumulado)); 213 214 al_register_event_source(fila_eventos, al_get_keyboard_event_source()); 215 216 217 al_start_timer(timer_anim_cavalo); 218 al_start_timer(timer_anim_trem); 219 al_start_timer(timer_anim_logo); 220 221 // FRAMES 222 for(x = 0; x < 7; x++){ // Zera todos os frames das animações 223 frame_anim[x] = 0; 224 } 225 226 // SONS 227 228 al_reserve_samples(1); 229 230 music_main = al_load_audio_stream("aud/western_theme.ogg", 4, 1024); 231 music_acumulado = al_load_audio_stream("aud/acum_sound.ogg", 4, 1024); 232 music_bingo = al_load_audio_stream("aud/bingo_sound.ogg", 4, 1024); 233 234 sample_cowboys = al_load_sample("aud/bingo_1l.ogg"); 235 236 al_attach_audio_stream_to_mixer(music_main, al_get_default_mixer()); 237 al_attach_audio_stream_to_mixer(music_acumulado, al_get_default_mixer()); 238 al_attach_audio_stream_to_mixer(music_bingo, al_get_default_mixer()); 239 240 241 al_set_audio_stream_playing(music_main, 0); 242 al_set_audio_stream_playing(music_acumulado, 0); 243 al_set_audio_stream_playing(music_bingo, 0); 244 245 246 247 // OUTROS 248 play_anim_cowboys = 0; 249 play_anim_acum = 0; 250 play_anim_bingo = 0; 251 252 ALLEGRO_SAMPLE_ID teste; 253 254} 255// MAIN LOOP 256void viewGame::main(controller controller){ 257 258 al_draw_bitmap(img_bg[ get_hour_bg() ], 0, 0, 0); 259 al_draw_bitmap(img_placa_acum, 0, 0, 0); 260 261 get_status(); 262 263 if(play_anim_cowboys == 0 && play_anim_bingo == 0 && play_anim_acum == 0){ 264 draw_anim_trem(); 265 draw_anim_cavalo(); 266 } 267 268 draw_anim_logo(); 269 270 draw_anim_cowboys(play_anim_cowboys); 271 draw_anim_bingo(); 272 draw_anim_acum(); 273 274 al_draw_text(font_display, al_map_rgb(255, 0, 0), 923, 445, ALLEGRO_ALIGN_CENTER, "888.888,88"); 275 al_draw_text(font_display, al_map_rgb(255, 0, 0), 953, 256, ALLEGRO_ALIGN_CENTER, "88.888,88"); 276 al_draw_text(font_display, al_map_rgb(255, 0, 0), 983, 68, ALLEGRO_ALIGN_CENTER, "8.888,88"); 277 278 al_flip_display(); 279} 280// FUNCTION ERROR 281 282void viewGame::draw_anim_cowboys(int controller_status ){ 283 int retorno_sound; 284 285 if(controller_status == 1 || controller_status == 3){ 286 al_start_timer(timer_frame_cowboys); 287 play_anim_cowboys = controller_status; 288 retorno_sound = al_play_sample(sample_cowboys, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, &teste); 289 cout << retorno_sound << "\n\n"; 290 } 291 292 if(play_anim_cowboys == 1 || play_anim_cowboys == 3){ 293 al_draw_bitmap(anim_cowboys[frame_anim[IDX_COWBOYS]], 0, 474, 0); 294 295 if(status_anim[IDX_COWBOYS] == 2){ 296 frame_anim[IDX_COWBOYS] = (frame_anim[IDX_COWBOYS] + 1) % 177; 297 298 if(frame_anim[IDX_COWBOYS] == 0){ 299 al_stop_timer(timer_frame_cowboys); 300 if (play_anim_cowboys == 3){ 301 al_start_timer(timer_frame_bingo); 302 play_anim_bingo = 1; 303 } 304 if (play_anim_cowboys == 1){ 305 play_anim_acum = 1; 306 al_start_timer(timer_frame_acumulado); 307 } 308 309 al_stop_sample(&teste); // AT THIS LINE ERRO 310 311 play_anim_cowboys = 0; 312 al_rest(1.0); 313 } 314 } 315 } 316}

EDIT

#SelectExpand
1//IN MAIN.cpp 2int main(int argc, char **argv) 3{ 4 5 6 7 int running = 1; 8 9 viewInit viewInit; 10 viewGame viewGame; 11 controller controller; 12 13 while(running){ 14 controller.main(); 15 viewGame.main( controller ); 16 } 17 18 19 20 return 0; 21}

SiegeLord

I'm looking at the source of al_play_sample and I don't see any way that it can return true while at the same time setting the id parameter to -1; Try making a small test case that illustrates the bug I guess.

Thread #612416. Printed from Allegro.cc