/* closes down the sound system */ void shutdown_sound() { if (!al_is_audio_installed()) return; al_destroy_thread(sound_update_thread); al_uninstall_timer(ping_timer); al_stop_samples(); al_destroy_sample(zap); al_destroy_sample(bang); al_destroy_sample(bigbang); al_destroy_sample(ping); if (!no_music) { al_uninstall_timer(music_timer); al_destroy_sample_instance(part_voice[0]); al_destroy_sample_instance(part_voice[1]); al_destroy_sample_instance(part_voice[2]); al_destroy_sample_instance(part_voice[3]); al_destroy_sample(sine); al_destroy_sample(square); al_destroy_sample(saw); al_destroy_sample(bd); al_destroy_sample(snare); al_destroy_sample(hihat); } }
static void _uninstall_sdl_event_hack(void) { if (thread) { al_set_thread_should_stop(thread); al_join_thread(thread, NULL); al_destroy_thread(thread); } }
void stop_sound_thread(void) { if (started_sound_thread) { // al_join_thread(sound_thread, NULL); al_destroy_thread(sound_thread); } }
static void oss_deallocate_voice(ALLEGRO_VOICE *voice) { OSS_VOICE *oss_voice = voice->extra; oss_voice->quit_poll_thread = true; al_join_thread(oss_voice->poll_thread, NULL); al_destroy_thread(oss_voice->poll_thread); close(oss_voice->fd); free(voice->extra); voice->extra = NULL; }
int main(int argc, const char *argv[]) { ALLEGRO_THREAD *thread[MAX_THREADS]; Background background[MAX_BACKGROUNDS] = { { 1.0, 0.5, 0.5 }, { 0.5, 1.0, 0.5 }, { 0.5, 0.5, 1.0 }, { 1.0, 1.0, 0.5 }, { 0.5, 1.0, 1.0 }, { 1.0, 0.7, 0.5 }, { 0.5, 1.0, 0.7 }, { 0.7, 0.5, 1.0 }, { 1.0, 0.7, 0.5 }, { 0.5, 0.7, 1.0 } }; int num_threads; int i; if (argc > 1) { num_threads = strtol(argv[1], NULL, 10); if (num_threads > MAX_THREADS) num_threads = MAX_THREADS; else if (num_threads < 1) num_threads = 1; } else { num_threads = 3; } if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_init_primitives_addon(); al_install_keyboard(); al_install_mouse(); for (i = 0; i < num_threads; i++) { thread[i] = al_create_thread(thread_func, &background[i % MAX_BACKGROUNDS]); } for (i = 0; i < num_threads; i++) { al_start_thread(thread[i]); } for (i = 0; i < num_threads; i++) { al_join_thread(thread[i], NULL); al_destroy_thread(thread[i]); } return 0; }
/* Destroy the specified player. */ void dumba5_destroy_player(DUMBA5_PLAYER * pp) { if(pp) { al_destroy_thread(pp->thread); al_destroy_mutex(pp->mutex); if(pp->sigrenderer) { duh_end_sigrenderer(pp->sigrenderer); // al_drain_stream(dp->stream); al_destroy_audio_stream(pp->stream); } free(pp); } }
void* get_http_page_threaded( ALLEGRO_THREAD* thread, void* ThreadData ) { DOWNLOAD_THREAD_DATA* threadData = (DOWNLOAD_THREAD_DATA*)ThreadData; CURL* curl_handle; Memory* buffer = new Memory( 0 ); int cTO = 60; curl_handle = curl_easy_init(); curl_easy_setopt( curl_handle, CURLOPT_URL, threadData->URL->c_str() ); curl_easy_setopt( curl_handle, CURLOPT_FOLLOWLOCATION, 1L ); curl_easy_setopt( curl_handle, CURLOPT_NOPROGRESS, 0L ); curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, write_data ); curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, (void*)buffer ); curl_easy_setopt( curl_handle, CURLOPT_PROGRESSFUNCTION, download_progress ); curl_easy_setopt( curl_handle, CURLOPT_PROGRESSDATA, threadData->Information ); cTO = FRAMEWORK->Settings->GetQuickIntegerValue( "Downloads.TimeOut", 60 ); // Default timeout is 60, but get settings curl_easy_setopt( curl_handle, CURLOPT_TIMEOUT, (long)cTO ); curl_easy_setopt( curl_handle, CURLOPT_NOSIGNAL, 1L ); if( FRAMEWORK->Settings->GetQuickBooleanValue( "Downloads.ProxyRequired", false ) ) { curl_easy_setopt( curl_handle, CURLOPT_PROXY, FRAMEWORK->Settings->GetQuickStringValue( "Downloads.ProxyServer", "" )->c_str() ); curl_easy_setopt( curl_handle, CURLOPT_PROXYPORT, FRAMEWORK->Settings->GetQuickInteger64Value( "Downloads.ProxyPort", 8080 ) ); } if( curl_easy_perform( curl_handle ) != 0 ) { buffer->Clear(); } curl_easy_cleanup( curl_handle ); if( !DownloadManager::AbortDownloads ) { Event* fwEvent = new Event(); fwEvent->Type = EVENT_DOWNLOAD_COMPLETE; fwEvent->Data.Download.URL = new std::string( threadData->URL->c_str() ); fwEvent->Data.Download.Contents = buffer; FRAMEWORK->PushEvent( fwEvent ); } if( thread != 0 ) { al_destroy_thread( thread ); } return 0; }
static void pulseaudio_deallocate_voice(ALLEGRO_VOICE *voice) { PULSEAUDIO_VOICE *pv = voice->extra; /* We do NOT hold the voice mutex here, so this does NOT result in a * deadlock when the thread calls _al_voice_update. */ al_set_thread_should_stop(pv->poll_thread); al_join_thread(pv->poll_thread, NULL); al_destroy_thread(pv->poll_thread); al_destroy_mutex(pv->buffer_mutex); pa_simple_free(pv->s); al_free(pv); }
/* To be called when stream is destroyed */ static void ogg_stream_close(ALLEGRO_AUDIO_STREAM *stream) { AL_OV_DATA *extra = (AL_OV_DATA *) stream->extra; ALLEGRO_EVENT quit_event; quit_event.type = _KCM_STREAM_FEEDER_QUIT_EVENT_TYPE; al_emit_user_event(al_get_audio_stream_event_source(stream), &quit_event, NULL); al_join_thread(stream->feed_thread, NULL); al_destroy_thread(stream->feed_thread); ov_clear(extra->vf); _AL_FREE(extra->vf); _AL_FREE(extra); stream->extra = NULL; stream->feed_thread = NULL; }
/* The stop_voice method should stop playback. For non-streaming voices, it should leave the data loaded, and reset the voice position to 0. */ static int _dsound_stop_voice(ALLEGRO_VOICE* voice) { ALLEGRO_DS_DATA *ex_data = (ALLEGRO_DS_DATA *)voice->extra; ALLEGRO_DEBUG("Stopping voice\n"); if (!ex_data->ds8_buffer) { ALLEGRO_ERROR("Trying to stop empty voice buffer\n"); return 1; } /* if playing a sample */ if (!voice->is_streaming) { ALLEGRO_DEBUG("Stopping non-streaming voice\n"); ex_data->ds8_buffer->Stop(); ex_data->ds8_buffer->SetCurrentPosition(0); ALLEGRO_INFO("Non-streaming voice stopped\n"); return 0; } if (ex_data->stop_voice == 0) { ALLEGRO_DEBUG("Joining thread\n"); ex_data->stop_voice = 1; while (ex_data->stop_voice == 1) { al_wait_cond(voice->cond, voice->mutex); } al_join_thread(ex_data->thread, NULL); ALLEGRO_DEBUG("Joined thread\n"); ALLEGRO_DEBUG("Destroying thread\n"); al_destroy_thread(ex_data->thread); ALLEGRO_DEBUG("Thread destroyed\n"); /* This is required to restart the background thread when the voice * restarts. */ ex_data->stop_voice = 1; } ALLEGRO_DEBUG("Releasing buffer\n"); ex_data->ds8_buffer->Release(); ex_data->ds8_buffer = NULL; ALLEGRO_INFO("Voice stopped\n"); return 0; }
static void hapxi_exit_haptic(void) { void *ret_value; ASSERT(hapxi_thread); ASSERT(hapxi_mutex); ASSERT(hapxi_cond); /* Request the event thread to shut down, signal the condition, then join the thread. */ al_set_thread_should_stop(hapxi_thread); al_signal_cond(hapxi_cond); al_join_thread(hapxi_thread, &ret_value); /* clean it all up. */ al_destroy_thread(hapxi_thread); al_destroy_cond(hapxi_cond); al_destroy_mutex(hapxi_mutex); hapxi_mutex = NULL; }
static void pulseaudio_deallocate_voice(ALLEGRO_VOICE *voice) { PULSEAUDIO_VOICE *pv = voice->extra; al_lock_mutex(voice->mutex); pv->status = PV_JOIN; al_broadcast_cond(pv->status_cond); al_unlock_mutex(voice->mutex); /* We do NOT hold the voice mutex here, so this does NOT result in a * deadlock when the thread calls _al_voice_update. */ al_join_thread(pv->poll_thread, NULL); al_destroy_thread(pv->poll_thread); al_destroy_cond(pv->status_cond); al_destroy_mutex(pv->buffer_mutex); pa_simple_free(pv->s); al_free(pv); }
void dumba5_stop_duh(DUMBA5_PLAYER * dp) { if(dp) { al_destroy_thread(dp->thread); if(dp->sigrenderer) { duh_end_sigrenderer(dp->sigrenderer); // al_drain_stream(dp->stream); al_destroy_audio_stream(dp->stream); } free(dp); /* if we are using the internal DUH, free it automatically, you are responsible for freeing your own DUHs */ if(dumba5_duh) { unload_duh(dumba5_duh); dumba5_duh = NULL; } } }
/* Function: al_close_native_text_log */ void al_close_native_text_log(ALLEGRO_TEXTLOG *textlog) { ALLEGRO_NATIVE_DIALOG *dialog = (ALLEGRO_NATIVE_DIALOG *)textlog; if (!dialog) return; if (!dialog->tl_init_error) { dialog->tl_done = false; if (TEXT_LOG_EXTRA_THREAD) { al_lock_mutex(dialog->tl_text_mutex); _al_close_native_text_log(dialog); while (!dialog->tl_done) { al_wait_cond(dialog->tl_text_cond, dialog->tl_text_mutex); } } else { _al_close_native_text_log(dialog); al_lock_mutex(dialog->tl_text_mutex); } _al_unregister_destructor(_al_dtor_list, dialog); } al_ustr_free(dialog->title); al_ustr_free(dialog->tl_pending_text); al_destroy_user_event_source(&dialog->tl_events); al_unlock_mutex(dialog->tl_text_mutex); if (TEXT_LOG_EXTRA_THREAD) { al_destroy_thread(dialog->tl_thread); } al_destroy_cond(dialog->tl_text_cond); al_destroy_mutex(dialog->tl_text_mutex); al_free(dialog); }
int main(int argc, char **argv){ ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *bouncer = NULL; ALLEGRO_THREAD *thread_1 = NULL; ALLEGRO_THREAD *thread_2 = NULL; bool redraw = true; if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(!al_install_mouse()) { fprintf(stderr, "failed to initialize the mouse!\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { fprintf(stderr, "failed to create display!\n"); al_destroy_timer(timer); return -1; } bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE); if(!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_set_target_bitmap(bouncer); al_clear_to_color(al_map_rgb(255, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_mouse_event_source()); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); DATA data; thread_1 = al_create_thread(Func_Thread, &data); al_start_thread(thread_1); al_lock_mutex(data.mutex); while (!data.ready){ al_wait_cond(data.cond, data.mutex); } al_unlock_mutex(data.mutex); al_lock_mutex(data.mutex); data.modi_X = true; data.ready = false; al_unlock_mutex(data.mutex); thread_2 = al_create_thread(Func_Thread, &data); al_start_thread(thread_2); al_lock_mutex(data.mutex); while (!data.ready){ al_wait_cond(data.cond, data.mutex); } al_unlock_mutex(data.mutex); while(1) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) { redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { break; } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_lock_mutex(data.mutex); float X = data.posiX; float Y = data.posiY; al_unlock_mutex(data.mutex); al_draw_bitmap(bouncer, X, Y, 0); al_flip_display(); } } al_destroy_thread(thread_1); al_destroy_thread(thread_2); al_destroy_bitmap(bouncer); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int main(void) { ALLEGRO_THREAD *thread[NUM_THREADS]; ALLEGRO_DISPLAY *display; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; bool need_draw; int i; for (i = 0; i < 256; i++) { sin_lut[i] = 128 + (int) (127.0 * sin(i / 8.0)); } if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_install_keyboard(); al_install_mouse(); display = al_create_display(W * IMAGES_PER_ROW, H * NUM_THREADS / IMAGES_PER_ROW); if (!display) { abort_example("Error creating display\n"); return 1; } timer = al_install_timer(1.0/3); if (!timer) { abort_example("Error creating timer\n"); return 1; } queue = al_create_event_queue(); if (!queue) { abort_example("Error creating event queue\n"); return 1; } al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); /* Note: * Right now, A5 video displays can only be accessed from the thread which * created them (at lesat for OpenGL). To lift this restriction, we could * keep track of the current OpenGL context for each thread and make all * functions accessing the display check for it.. not sure it's worth the * additional complexity though. */ al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_RGB_888); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); for (i = 0; i < NUM_THREADS; i++) { thread_info[i].bitmap = al_create_bitmap(W, H); if (!thread_info[i].bitmap) { goto Error; } thread_info[i].mutex = al_create_mutex(); if (!thread_info[i].mutex) { goto Error; } thread_info[i].cond = al_create_cond(); if (!thread_info[i].cond) { goto Error; } thread_info[i].is_paused = false; thread_info[i].random_seed = i; thread[i] = al_create_thread(thread_func, &thread_info[i]); if (!thread[i]) { goto Error; } } set_target(0, -0.56062033041600878303, -0.56064322926933807256); set_target(1, -0.57798076669230014080, -0.63449861991138123418); set_target(2, 0.36676836392830602929, -0.59081385302214906030); set_target(3, -1.48319283039401317303, -0.00000000200514696273); set_target(4, -0.74052910500707636032, 0.18340899525730713915); set_target(5, 0.25437906525768350097, -0.00046678223345789554); set_target(6, -0.56062033041600878303, 0.56064322926933807256); set_target(7, -0.57798076669230014080, 0.63449861991138123418); set_target(8, 0.36676836392830602929, 0.59081385302214906030); for (i = 0; i < NUM_THREADS; i++) { al_start_thread(thread[i]); } al_start_timer(timer); need_draw = true; while (true) { if (need_draw && al_event_queue_is_empty(queue)) { show_images(); need_draw = false; } al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_TIMER) { need_draw = true; } else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { int n = (event.mouse.y / H) * IMAGES_PER_ROW + (event.mouse.x / W); if (n < NUM_THREADS) { double x = event.mouse.x - (event.mouse.x / W) * W; double y = event.mouse.y - (event.mouse.y / H) * H; /* Center to the mouse click position. */ if (thread_info[n].is_paused) { thread_info[n].target_x = x / W - 0.5; thread_info[n].target_y = y / H - 0.5; } toggle_pausedness(n); } } else if (event.type == ALLEGRO_EVENT_DISPLAY_EXPOSE) { need_draw = true; } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } need_draw = true; } } for (i = 0; i < NUM_THREADS; i++) { /* Set the flag to stop the thread. The thread might be waiting on a * condition variable, so signal the condition to force it to wake up. */ al_set_thread_should_stop(thread[i]); al_lock_mutex(thread_info[i].mutex); al_broadcast_cond(thread_info[i].cond); al_unlock_mutex(thread_info[i].mutex); /* al_destroy_thread() implicitly joins the thread, so this call is not * strictly necessary. */ al_join_thread(thread[i], NULL); al_destroy_thread(thread[i]); } al_destroy_event_queue(queue); al_uninstall_timer(timer); al_destroy_display(display); return 0; Error: return 1; }
void main_loop(ALLEGRO_EVENT_QUEUE *event_queue) { int color = 0, c2 = 0, c3 = 0; bool stlacene_tlacitka[ALLEGRO_KEY_MAX]; memset(stlacene_tlacitka, 0, sizeof(stlacene_tlacitka)); struct Player *player; player = player_new(); struct Objekt * obj = malloc(sizeof(struct Objekt)); list_add(&scena, obj); obj->objekt = player; obj->draw = player_draw; obj->destroy = player_destroy; obj->copy = player_copy; mutex = al_create_mutex(); ALLEGRO_THREAD *thread = al_create_thread(kresliace_vlakno, event_queue); al_start_thread(thread); for (;;) { ALLEGRO_EVENT event; al_wait_for_event(event_queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } if (event.type == ALLEGRO_EVENT_TIMER) { al_lock_mutex(mutex); if (stlacene_tlacitka[ALLEGRO_KEY_LEFT]) { player->x = player->x - 10; if (player->x < 0) player->x = 0; } if (stlacene_tlacitka[ALLEGRO_KEY_RIGHT]) { player->x = player->x + 10; if (player->x > SCREEN_W - 64) player->x = SCREEN_W - 64; } al_unlock_mutex(mutex); } if (event.type == ALLEGRO_EVENT_KEY_DOWN ||event.type == ALLEGRO_EVENT_KEY_UP) { ALLEGRO_KEYBOARD_STATE stav_klavesnice; al_get_keyboard_state(&stav_klavesnice); if (al_key_down(&stav_klavesnice, ALLEGRO_KEY_ESCAPE)) { break; } if (al_key_down(&stav_klavesnice, ALLEGRO_KEY_LEFT)) { stlacene_tlacitka[ALLEGRO_KEY_LEFT] = true; } else { stlacene_tlacitka[ALLEGRO_KEY_LEFT] = false; } if (al_key_down(&stav_klavesnice, ALLEGRO_KEY_RIGHT)) { stlacene_tlacitka[ALLEGRO_KEY_RIGHT] = true; } else { stlacene_tlacitka[ALLEGRO_KEY_RIGHT] = false; } if (al_key_down(&stav_klavesnice, ALLEGRO_KEY_UP)) { stlacene_tlacitka[ALLEGRO_KEY_UP] = true; } else { stlacene_tlacitka[ALLEGRO_KEY_UP] = false; } if (al_key_down(&stav_klavesnice, ALLEGRO_KEY_DOWN)) { stlacene_tlacitka[ALLEGRO_KEY_DOWN] = true; } else { stlacene_tlacitka[ALLEGRO_KEY_DOWN] = false; } } } al_destroy_thread(thread); }