Exemplo n.º 1
0
static bool start_video(ALLEGRO_VIDEO *video)
{
   VideoState *is = video->data;

   is->timer_thread = al_create_thread(timer_thread, is);
   al_start_thread(is->timer_thread);

   is->parse_thread = al_create_thread(decode_thread, is);
   if (!is->parse_thread) {
      return false;
   }
   al_start_thread(is->parse_thread);
   return true;
}
Exemplo n.º 2
0
/* Initializes the XInput haptic system. */
static bool hapxi_init_haptic(void)
{
   int i;

   ASSERT(hapxi_mutex == NULL);
   ASSERT(hapxi_thread == NULL);
   ASSERT(hapxi_cond == NULL);


   /* Create the mutex and a condition vaiable. */
   hapxi_mutex = al_create_mutex_recursive();
   if (!hapxi_mutex)
      return false;
   hapxi_cond = al_create_cond();
   if (!hapxi_cond)
      return false;

   al_lock_mutex(hapxi_mutex);

   for (i = 0; i < HAPTICS_MAX; i++) {
      haptics[i].active = false;
   }

   /* Now start a polling background thread, since XInput is a polled API,
      and also to make it possible for effects to stop running when their
      duration has passed. */
   hapxi_thread = al_create_thread(hapxi_poll_thread, NULL);
   al_unlock_mutex(hapxi_mutex);
   if (hapxi_thread) al_start_thread(hapxi_thread);
   return(hapxi_thread != NULL);
}
Exemplo n.º 3
0
void reloadPosition()
{
    //create handle to dfHack API
    static bool firstLoad = 1;

    if (timeToReloadConfig) {
        contentLoader->Load();
        timeToReloadConfig = false;
    }

    int segmentHeight = ssConfig.single_layer_view ? 2 : ssState.Size.z;
    //load segment
    if(ssConfig.threading_enable) {
        if(!ssConfig.threadmade) {
            ssConfig.readThread = al_create_thread(threadedSegment, NULL);
            ssConfig.threadmade = 1;
        }
    }

    if(ssConfig.threading_enable) {
        al_start_thread(ssConfig.readThread);
    } else {
        read_segment(NULL);
    }

    firstLoad = 0;
}
Exemplo n.º 4
0
void gdp_splash(){
    int i;
    int ncond = 0,nlogo = 1;

    char *buf;
    ALLEGRO_BITMAP *image = NULL;
    ALLEGRO_THREAD *thread = NULL;

    thread = al_create_thread(gdp_splashclose,(void*) &ncond);
    al_start_thread(thread);

    for(i=0;i<nlogo;i++){
        buf = ".//Images//logo0.png";
        image = al_load_bitmap(buf);

        // se fechar a tela
        if(ncond==1){
            al_destroy_bitmap(image);
            break;
        }
        // se precionar qualquer botao
        if(ncond==2){
            al_destroy_bitmap(image);
            break;
        }

        gdp_fadein(image,1);
        al_rest(0.5);
        gdp_fadeout(image,1);

        al_destroy_bitmap(image);
    }
}
Exemplo n.º 5
0
void _al_sdl_event_hack(void)
{
   if (thread)
      return;
   _al_add_exit_func(_uninstall_sdl_event_hack, "uninstall_sdl_event_hack");
   thread = al_create_thread(wakeup_thread, NULL);
   al_start_thread(thread);
}
Exemplo n.º 6
0
static int pulseaudio_allocate_voice(ALLEGRO_VOICE *voice)
{
   PULSEAUDIO_VOICE *pv = al_malloc(sizeof(PULSEAUDIO_VOICE));
   pa_sample_spec ss;
   pa_buffer_attr ba;

   ss.channels = al_get_channel_count(voice->chan_conf);
   ss.rate = voice->frequency;

   if (voice->depth == ALLEGRO_AUDIO_DEPTH_UINT8)
      ss.format = PA_SAMPLE_U8;
   else if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT16)
      ss.format = PA_SAMPLE_S16NE;
#if PA_API_VERSION > 11
   else if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT24)
      ss.format = PA_SAMPLE_S24NE;
#endif
   else if (voice->depth == ALLEGRO_AUDIO_DEPTH_FLOAT32)
      ss.format = PA_SAMPLE_FLOAT32NE;
   else {
      ALLEGRO_ERROR("Unsupported PulseAudio sound format.\n");
      al_free(pv);
      return 1;
   }

   ba.maxlength = 0x10000; // maximum length of buffer
   ba.tlength   = 0x2000;  // target length of buffer
   ba.prebuf    = 0;       // minimum data size required before playback starts
   ba.minreq    = 0;       // minimum size of request 
   ba.fragsize  = -1;      // fragment size (recording)

   pv->s = pa_simple_new(NULL,         // Use the default server.
                   al_get_app_name(),     
                   PA_STREAM_PLAYBACK,
                   NULL,               // Use the default device.
                   "Allegro Voice",    
                   &ss,                
                   NULL,               // Use default channel map
                   &ba,                
                   NULL                // Ignore error code.
   );

   if (!pv->s) {
      al_free(pv);
      return 1;
   }

   voice->extra = pv;

   pv->frame_size = ss.channels * al_get_audio_depth_size(voice->depth);
   pv->status = PV_STOPPED;
   pv->buffer_mutex = al_create_mutex();

   pv->poll_thread = al_create_thread(pulseaudio_update, (void*)voice);
   al_start_thread(pv->poll_thread);

   return 0;
}
Exemplo n.º 7
0
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;
}
Exemplo n.º 8
0
/* ljoy_init_joystick: [primary thread]
 *  Initialise the joystick driver.
 */
static bool ljoy_init_joystick(void)
{
    _al_vector_init(&joysticks, sizeof(ALLEGRO_JOYSTICK_LINUX *));
    num_joysticks = 0;

    if (!(config_mutex = al_create_mutex())) {
        return false;
    }

    // Scan for joysticks
    ljoy_scan(false);
    ljoy_merge();

#ifdef SUPPORT_HOTPLUG
    if (!(hotplug_mutex = al_create_mutex())) {
        al_destroy_mutex(config_mutex);
        return false;
    }
    if (!(hotplug_cond = al_create_cond())) {
        al_destroy_mutex(config_mutex);
        al_destroy_mutex(hotplug_mutex);
        return false;
    }
    if (!(hotplug_thread = al_create_thread(hotplug_proc, NULL))) {
        al_destroy_mutex(config_mutex);
        al_destroy_mutex(hotplug_mutex);
        al_destroy_cond(hotplug_cond);
        return false;
    }

    al_start_thread(hotplug_thread);

    inotify_fd = inotify_init();
    if (inotify_fd != -1) {
        fcntl(inotify_fd, F_SETFL, O_NONBLOCK);
        /* Modern Linux probably only needs to monitor /dev/input. */
        inotify_add_watch(inotify_fd, "/dev/input", IN_CREATE|IN_DELETE);
        _al_unix_start_watching_fd(inotify_fd, ljoy_config_dev_changed, NULL);
        ALLEGRO_INFO("Hotplugging enabled\n");
    }
    else {
        ALLEGRO_WARN("Hotplugging not enabled\n");
        if (inotify_fd != -1) {
            close(inotify_fd);
            inotify_fd = -1;
        }
    }
#endif

    return true;
}
/* Function: al_open_native_text_log
 */
ALLEGRO_TEXTLOG *al_open_native_text_log(char const *title, int flags)
{
   ALLEGRO_NATIVE_DIALOG *textlog = NULL;

   /* Avoid warnings when log windows are unimplemented. */
   (void)title;
   (void)flags;

   textlog = al_calloc(1, sizeof *textlog);
   textlog->title = al_ustr_new(title);
   textlog->flags = flags;
   if (TEXT_LOG_EXTRA_THREAD) {
      textlog->tl_thread = al_create_thread(text_log_thread_proc, textlog);
   }
   textlog->tl_text_cond = al_create_cond();
   textlog->tl_text_mutex = al_create_mutex();
   textlog->tl_pending_text = al_ustr_new("");
   al_init_user_event_source(&textlog->tl_events);

   textlog->tl_init_error = false;
   textlog->tl_done = false;

   if (TEXT_LOG_EXTRA_THREAD) {
      /* Unlike the other dialogs, this one never blocks as the intended
       * use case is a log window running in the background for debugging
       * purposes when no console can be used. Therefore we have it run
       * in a separate thread.
       */
      al_start_thread(textlog->tl_thread);
      al_lock_mutex(textlog->tl_text_mutex);
      while (!textlog->tl_done && !textlog->tl_init_error) {
         al_wait_cond(textlog->tl_text_cond, textlog->tl_text_mutex);
      }
      al_unlock_mutex(textlog->tl_text_mutex);
   }
   else {
      textlog->tl_init_error = !_al_open_native_text_log(textlog);
   }

   if (textlog->tl_init_error) {
      al_close_native_text_log((ALLEGRO_TEXTLOG *)textlog);
      return NULL;
   }

   _al_register_destructor(_al_dtor_list, textlog,
      (void (*)(void *))al_close_native_text_log);

   return (ALLEGRO_TEXTLOG *)textlog;
}
Exemplo n.º 10
0
void DownloadManager::Update()
{
  ALLEGRO_THREAD* t;
  DOWNLOAD_THREAD_DATA* tD;

	while( urlDownloading.size() < urlDownloads && urlList.size() > 0 )
	{
	  DownloadInformation* dlI = new DownloadInformation( urlList.front() );
		urlDownloading.push_back( dlI );
		tD = (DOWNLOAD_THREAD_DATA*)malloc( sizeof(DOWNLOAD_THREAD_DATA) );
		tD->URL = new std::string( urlList.front() );
		tD->Information = dlI;
    t = al_create_thread( get_http_page_threaded, (void*)tD );
    al_start_thread( t );
		urlList.pop_front();
#ifdef WRITE_LOG
		fprintf( FRAMEWORK->LogFile, "DownloadManager: Downloading '%s' (Queue Length: %ld, Download Count: %ld)\n", urlDownloading.back()->URL.c_str(), urlList.size(), urlDownloading.size() );
#endif
	}
}
Exemplo n.º 11
0
void reloadDisplayedSegment()
{
    //create handle to dfHack API
    static bool firstLoad = 1;

    if (timeToReloadConfig) {
        parms.thread_connect = 0;
        contentLoader->Load();
        timeToReloadConfig = false;
    }

    if (firstLoad || ssConfig.follow_DFscreen) {
        ssState.DisplayedSegment.x = parms.x;
        ssState.DisplayedSegment.y = parms.y;
        ssState.DisplayedSegment.z = parms.z;
    }

    int segmentHeight = ssConfig.single_layer_view ? 2 : ssState.SegmentSize.z;
    //load segment
    if(ssConfig.threading_enable) {
        if(!ssConfig.threadmade) {
            ssConfig.readThread = al_create_thread(threadedSegment, NULL);
            ssConfig.threadmade = 1;
        }
    }

    parms.x = ssState.DisplayedSegment.x;
    parms.y = ssState.DisplayedSegment.y;
    parms.z = ssState.DisplayedSegment.z;
    parms.sizex = ssState.SegmentSize.x;
    parms.sizey = ssState.SegmentSize.y;
    parms.sizez = segmentHeight;

    if(ssConfig.threading_enable) {
        al_start_thread(ssConfig.readThread);
    } else {
        read_segment(NULL);
    }

    firstLoad = 0;
}
Exemplo n.º 12
0
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);


}
Exemplo n.º 13
0
static int stream_component_open(VideoState * is, int stream_index)
{

   AVFormatContext *format_context = is->format_context;
   AVCodecContext *codecCtx;
   AVCodec *codec;

   if (stream_index < 0 || stream_index >= (int)format_context->nb_streams) {
      return -1;
   }

   // Get a pointer to the codec context for the video stream
   codecCtx = format_context->streams[stream_index]->codec;


   if (codecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
      // Set audio settings from codec info
      is->video->audio =
          al_create_audio_stream(4, AUDIO_BUFFER_SIZE / 4,
                                 codecCtx->sample_rate,
                                 ALLEGRO_AUDIO_DEPTH_INT16,
                                 ALLEGRO_CHANNEL_CONF_1 + codecCtx->channels -
                                 1);

      if (!is->video->audio) {
         ALLEGRO_ERROR("al_create_audio_stream failed\n");
         return -1;
      }

      is->audio_thread = al_create_thread(stream_audio, is);
      al_start_thread(is->audio_thread);

      is->audio_hw_buf_size = AUDIO_BUFFER_SIZE;
   }
   codec = avcodec_find_decoder(codecCtx->codec_id);
   if (codec) {
      #ifdef FFMPEG_0_8
      if (avcodec_open2(codecCtx, codec, NULL) < 0) codec = NULL;
      #else
      if (avcodec_open(codecCtx, codec) < 0) codec = NULL;
      #endif
   }
   if (!codec) {
      ALLEGRO_ERROR("Unsupported codec!\n");
      return -1;
   }

   switch (codecCtx->codec_type) {
      case AVMEDIA_TYPE_AUDIO:
         is->audioStream = stream_index;
         is->audio_st = format_context->streams[stream_index];
         is->audio_buf_size = 0;
         is->audio_buf_index = 0;

         /* averaging filter for audio sync */
         is->audio_diff_avg_coef = exp(log(0.01 / AUDIO_DIFF_AVG_NB));
         is->audio_diff_avg_count = 0;
         /* Correct audio only if larger error than this */
         is->audio_diff_threshold = 0.1;

         memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
         packet_queue_init(&is->audioq);

         break;
      case AVMEDIA_TYPE_VIDEO:
         is->videoStream = stream_index;
         is->video_st = format_context->streams[stream_index];

         is->frame_timer = (double)av_gettime() / 1000000.0;
         is->frame_last_delay = 40e-3;
         is->video_current_pts_time = av_gettime();

         packet_queue_init(&is->videoq);
         is->video_thread = al_create_thread(video_thread, is);
         al_start_thread(is->video_thread);
         codecCtx->get_buffer = our_get_buffer;
         codecCtx->release_buffer = our_release_buffer;

         break;
      default:
         break;
   }

   return 0;
}
Exemplo n.º 14
0
int main(int argc, char **argv)
{
   ALLEGRO_DISPLAY *display;
   ALLEGRO_TIMER *timer;
   ALLEGRO_EVENT_QUEUE *queue;
   bool redraw = true;
   ALLEGRO_FONT *font;
   ALLEGRO_BITMAP *spin, *spin2;
   int current_bitmap = 0;
   int loaded_bitmap = 0;
   ALLEGRO_THREAD *thread;

   (void)argc;
   (void)argv;

   if (!al_init()) {
      abort_example("Could not init Allegro.\n");
   }
   al_init_image_addon();
   al_init_font_addon();
   al_init_primitives_addon();
   init_platform_specific();

   open_log();

   al_install_mouse();
   al_install_keyboard();

   spin = al_load_bitmap("data/cursor.tga");
   log_printf("default bitmap without display: %p\n", spin);

   al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
   spin2 = al_load_bitmap("data/cursor.tga");
   log_printf("video bitmap without display: %p\n", spin2);
   
   log_printf("%p before create_display: ", spin);
   print_bitmap_flags(spin);
   log_printf("\n");

   display = al_create_display(64, 64);
   if (!display) {
      abort_example("Error creating display\n");
   }
   
   spin2 = al_load_bitmap("data/cursor.tga");
   log_printf("video bitmap with display: %p\n", spin2);
   
   log_printf("%p after create_display: ", spin);
   print_bitmap_flags(spin);
   log_printf("\n");
   
   log_printf("%p after create_display: ", spin2);
   print_bitmap_flags(spin2);
   log_printf("\n");

   al_destroy_display(display);
   
   log_printf("%p after destroy_display: ", spin);
   print_bitmap_flags(spin);
   log_printf("\n");
   
   log_printf("%p after destroy_display: ", spin2);
   print_bitmap_flags(spin2);
   log_printf("\n");

   display = al_create_display(640, 480);
   
   log_printf("%p after create_display: ", spin);
   print_bitmap_flags(spin);
   log_printf("\n");
   
   log_printf("%p after create_display: ", spin2);
   print_bitmap_flags(spin2);
   log_printf("\n");

   font = al_load_font("data/fixed_font.tga", 0, 0);

   mutex = al_create_mutex();
   thread = al_create_thread(loading_thread, NULL);
   al_start_thread(thread);

   timer = al_create_timer(1.0 / 30);
   queue = al_create_event_queue();
   al_register_event_source(queue, al_get_keyboard_event_source());
   al_register_event_source(queue, al_get_display_event_source(display));
   al_register_event_source(queue, al_get_timer_event_source(timer));
   al_start_timer(timer);

   while (1) {
      ALLEGRO_EVENT event;
      al_wait_for_event(queue, &event);

      if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
         break;
      if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
         if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
             break;
         }
      if (event.type == ALLEGRO_EVENT_TIMER)
         redraw = true;

      if (redraw && al_is_event_queue_empty(queue)) {
         float x = 20, y = 320;
         int i;
         ALLEGRO_COLOR color = al_map_rgb_f(0, 0, 0);
         float t = al_current_time();

         redraw = false;
         al_clear_to_color(al_map_rgb_f(0.5, 0.6, 1));
         
         al_draw_textf(font, color, x + 40, y, 0, "Loading %d%%",
            100 * load_count / load_total);

         al_lock_mutex(mutex);
         if (loaded_bitmap < load_count) {
            /* This will convert any video bitmaps without a display
             * (all the bitmaps being loaded in the loading_thread) to
             * video bitmaps we can use in the main thread.
             */
            al_convert_bitmap(bitmaps[loaded_bitmap]);
            loaded_bitmap++;
         }
         al_unlock_mutex(mutex);

         if (current_bitmap < loaded_bitmap) {
            int bw;
            al_draw_bitmap(bitmaps[current_bitmap], 0, 0, 0);
            if (current_bitmap + 1 < loaded_bitmap)
               current_bitmap++;

            for (i = 0; i <= current_bitmap; i++) {
               bw = al_get_bitmap_width(bitmaps[i]);
               al_draw_scaled_rotated_bitmap(bitmaps[i],
                  0, 0, (i % 20) * 640 / 20, 360 + (i / 20) * 24,
                  32.0 / bw, 32.0 / bw, 0, 0);
            }
         }
         
         if (loaded_bitmap < load_total) {
            al_draw_scaled_rotated_bitmap(spin,
               16, 16, x, y, 1.0, 1.0, t * ALLEGRO_PI * 2, 0);
         }
         
         al_flip_display();
      }
   }

   al_join_thread(thread, NULL);
   al_destroy_mutex(mutex);
   al_destroy_font(font); 
   al_destroy_display(display);

   close_log(true);

   return 0;
}
Exemplo n.º 15
0
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;
}
Exemplo n.º 16
0
void gdp_game(){
    gdp_send2server_init();

    if(connecterro == 1){
        gdp_erro("Não foi possível conectar ao servidor! :/");
        return;
    }
    int i,a=-1,d=-1,x=-1,y=-1;

    thsend2server = al_create_thread(gdp_send2server, NULL);
    al_start_thread(thsend2server);

    threcv2server = al_create_thread(gdp_recv2server, NULL);
    al_start_thread(threcv2server);

    while(true){
        listchars[nlocchar]->idmap = opmap;

        al_wait_for_event(event_queue, &evento);

        if(gdp_readclose())
            break;

        if(listchars[nlocchar]->obj.lock == 0 && listchars[nlocchar]->obj.a !=4){
            gdp_readaction(&listchars[nlocchar]->obj,&scale);
            gdp_readdirection(&listchars[nlocchar]->obj);
        }

        for(i=0;i<ntotchars;i++){
            if(listchars[i]!=NULL)
                gdp_statuschar(listchars[i]);
        }

        if (gdp_readtime() && al_is_event_queue_empty(event_queue)) {
		    if (a != listchars[nlocchar]->obj.a || d != listchars[nlocchar]->obj.d2 ||
		    	x != listchars[nlocchar]->obj.x || y != listchars[nlocchar]->obj.y) {
		        al_lock_mutex(musend2server);
		        mudou = true;
		        al_signal_cond(condsend2server);
		        al_unlock_mutex(musend2server);
		    }
            gdp_clear();

            ALLEGRO_TRANSFORM camera;
            al_identity_transform(&camera);
            gdp_update_camera(listchars[nlocchar], height, wigth, scale);

            // imagem de fundo
            al_draw_scaled_bitmap(ambient->image,
                0, 0,
                ambient->w,
                ambient->h,
                0,
                0, //ambient->info->h
                ambient->wd,
                ambient->hd,
                0
            );

            //mostra portoes
            for (i=0;i<(ambient->qt_gates);i++)
            {
                 al_draw_filled_rectangle
                 (
                    (ambient->gates[i].x1),
                    (ambient->gates[i].y1),
                    (ambient->gates[i].x2),
                    (ambient->gates[i].y2),
                    al_map_rgba(255, 98, 100,0)
                  );
            }

            int size = CHARS + LIFELESS;
            LayeredObject *sorted = (LayeredObject*)calloc(size, sizeof(LayeredObject));
            memset(sorted, 0, size * sizeof(LayeredObject));

            for (i = 0; i < CHARS; i++) {
                if (listchars[i] && listchars[i]->idmap == opmap) {
                    sorted[i].type = OBJTYPE_ENEMY_OR_CHAR;
                    sorted[i].y = listchars[i]->obj.y + listchars[i]->obj.h;
                    sorted[i].arr_idx = i;
                }
            }

            for (i = CHARS; i < CHARS + LIFELESS; i++) {
                if (listlifeless[i - CHARS] && listlifeless[i - CHARS]->idmap == opmap) {
                    sorted[i].type = OBJTYPE_LIFELESS;
                    sorted[i].y = listlifeless[i - CHARS]->obj.y + listlifeless[i - CHARS]->obj.h;
                    sorted[i].arr_idx = i - CHARS;
                }
            }

            qsort(sorted, size, sizeof(LayeredObject), (void*)comparar_char);

            for (i = 0; i < size; i++) {
                if (sorted[i].type == OBJTYPE_NONE)
                    break;

                if (sorted[i].type == OBJTYPE_ENEMY_OR_CHAR) {
                    if(listchars[sorted[i].arr_idx] != NULL){
                        if(listchars[sorted[i].arr_idx]->dead==0){
                                gdp_drawchar(listchars[sorted[i].arr_idx]);
                        }
                    }
                    /*
                    if (listchars[sorted[i].arr_idx]->dead==1) {
                        free(listchars[sorted[i].arr_idx]);
                        listchars[sorted[i].arr_idx] = NULL;
                    }
                    */
                } else {
                    if(listlifeless[sorted[i].arr_idx] != NULL){
                        if(listlifeless[sorted[i].arr_idx]->dead==0){
                            gdp_drawlifeless(listlifeless[sorted[i].arr_idx]);
                        }
                    }
                    /*
                    if (listlifeless[sorted[i].arr_idx]->dead==1) {
                        free(listlifeless[sorted[i].arr_idx]);
                        listlifeless[sorted[i].arr_idx] = NULL;
                    }
                    */
                }
            }

            free(sorted);

            al_use_transform(&camera);
            gdp_drawinfo(ambient->info);
            al_flip_display();
        }
    }
}
Exemplo n.º 17
0
void init_sound(int camstate_rand_seed)
{

 settings.sound_on = 1;

//    al_init_acodec_addon(); - shouldn't need this


 if (!al_install_audio()
  || !al_init_acodec_addon())
 {
  fprintf(stdout, "\nAllegro audio installation failed. Starting without sound.");
  settings.sound_on = 0;
  return;
 }


 if (!al_reserve_samples(24))
 {
  fprintf(stdout, "\nCould not set up Allegro audio voice/mixer. Starting without sound.");
  settings.sound_on = 0;
  return;
 }

 if (settings.option [OPTION_VOL_MUSIC] == 0
		&& settings.option [OPTION_VOL_EFFECT] == 0)
	{
  settings.sound_on = 0;
  return;
	}


 load_in_sample(SAMPLE_BLIP1, "data/sound/blip.wav");
 load_in_sample(SAMPLE_BLIP2, "data/sound/blip2.wav");
 load_in_sample(SAMPLE_BLIP3, "data/sound/blip3.wav");
 load_in_sample(SAMPLE_BLIP4, "data/sound/blip4.wav");
 load_in_sample(SAMPLE_KILL, "data/sound/kill.wav");
 load_in_sample(SAMPLE_CHIRP, "data/sound/chirp.wav");
 load_in_sample(SAMPLE_OVER, "data/sound/over.wav");
 load_in_sample(SAMPLE_ALLOC, "data/sound/alloc.wav");
 load_in_sample(SAMPLE_NEW, "data/sound/new.wav");

 load_in_sample(SAMPLE_BANG, "data/sound/bang.wav");
 load_in_sample(SAMPLE_BANG2, "data/sound/bang2.wav");
 load_in_sample(SAMPLE_INT_UP, "data/sound/int_up.wav");
 load_in_sample(SAMPLE_INT_BREAK, "data/sound/int_break.wav");
 load_in_sample(SAMPLE_RESTORE, "data/sound/restore.wav");
 load_in_sample(SAMPLE_STREAM1, "data/sound/stream1.wav");
 load_in_sample(SAMPLE_STREAM2, "data/sound/stream2.wav");
 load_in_sample(SAMPLE_ZAP, "data/sound/zap.wav");
 load_in_sample(SAMPLE_SPIKE, "data/sound/spike.wav");
 load_in_sample(SAMPLE_SLICE, "data/sound/slice.wav");
 load_in_sample(SAMPLE_ULTRA, "data/sound/ultra.wav");
 load_in_sample(SAMPLE_BUBBLE, "data/sound/bubble.wav");

 load_in_sample(SAMPLE_DRUM1, "data/sound/music/drum1.wav");
 load_in_sample(SAMPLE_DRUM2, "data/sound/music/drum2.wav");
 load_in_sample(SAMPLE_DRUM3, "data/sound/music/drum3.wav");
 load_in_sample(SAMPLE_CLICK, "data/sound/music/click.wav");
 load_in_sample(SAMPLE_THUMP, "data/sound/music/thump.wav");

// load_in_msample(MSAMPLE_NOTE, "data/sound/amb/note.wav");
// load_in_msample(MSAMPLE_NOTE2, "data/sound/amb/note_harm.wav");
// load_in_msample(MSAMPLE_NOTE3, "data/sound/amb/note_sine.wav");
// load_in_msample(MSAMPLE_NOTE4, "data/sound/amb/note_sq.wav");

// load_in_amb_sample(AMB_WARBLE, "sound/amb/warble.wav");
// load_in_amb_sample(AMB_NOTE, "sound/amb/note.wav");



 //al_stop_samples();

 build_tone_array();

 sound_config.music_volume = settings.option [OPTION_VOL_MUSIC] * 0.01;
 sound_config.effect_volume = settings.option [OPTION_VOL_EFFECT] * 0.01;

 sound_timer = al_create_timer(0.22); // 0.20
 if (!sound_timer)
 {
    fprintf(stdout, "\nError: failed to create sound timer.");
    safe_exit(-1);
 }
 al_start_timer(sound_timer);
 al_init_user_event_source(&sound_event_source);

 sound_queue = al_create_event_queue();
 al_register_event_source(sound_queue, &sound_event_source);
 al_register_event_source(sound_queue, al_get_timer_event_source(sound_timer));

 sound_event.user.type = ALLEGRO_GET_EVENT_TYPE(1, 0, 4, 0);

 sthread_init_sample_pointers();

 if (settings.option [OPTION_VOL_MUSIC] != 0)
  init_camstate(-1, 1, 0, camstate_rand_seed); // this is usually only called from within the sound thread
   // but can be called here because the sound thread hasn't been started yet.
   // -1 means start new music
    else
     init_camstate(-2, 0, 0, 0); // -2 means turn the music off


 sound_thread = al_create_thread(thread_check_sound_queue, NULL);
 al_start_thread(sound_thread);

 started_sound_thread = 1;

}
Exemplo n.º 18
0
/* The start_voice should, surprise, start the voice. For streaming voices, it
   should start polling the device and call _al_voice_update for audio data.
   For non-streaming voices, it should resume playing from the last set
   position */
static int _openal_start_voice(ALLEGRO_VOICE *voice)
{
   ALLEGRO_AL_DATA *ex_data = voice->extra;
   ALenum openal_err;

   /* playing a sample instead of a stream */
   if (!voice->is_streaming) {
      alSourcePlay(ex_data->source);
      if ((openal_err = alGetError()) != AL_NO_ERROR) {
         ALLEGRO_ERROR("Could not start voice: %s\n",
            openal_get_err_str(openal_err));
         return 1;
      }

      ALLEGRO_INFO("Starting voice\n");
      return 0;
   }

   {
      ex_data->buffer_size = voice->buffer_size;
      if (!ex_data->buffer_size) {
         switch (ex_data->format) {
            case AL_FORMAT_STEREO16:
               ex_data->buffer_size = preferred_frag_size * 4;
               break;
            case AL_FORMAT_STEREO8:
            case AL_FORMAT_MONO16:
               ex_data->buffer_size = preferred_frag_size * 2;
               break;
            default:
               ex_data->buffer_size = preferred_frag_size;
               break;
         }
      }

      ex_data->num_buffers = voice->num_buffers;
      if (!ex_data->num_buffers)
         ex_data->num_buffers = preferred_buf_count;

      alGenSources(1, &ex_data->source);
      if (alGetError() != AL_NO_ERROR)
         return 1;

      ex_data->buffers = al_malloc(sizeof(ALuint) * ex_data->num_buffers);
      if (!ex_data->buffers) {
         alSourcei(ex_data->source, AL_BUFFER, 0);
         alDeleteSources(1, &ex_data->source);
         return 1;
      }

      alGenBuffers(ex_data->num_buffers, ex_data->buffers);
      if (alGetError() != AL_NO_ERROR) {
         alSourcei(ex_data->source, AL_BUFFER, 0);
         alDeleteSources(1, &ex_data->source);
         al_free(ex_data->buffers);
         ex_data->buffers = NULL;
         return 1;
      }

      alSourcef(ex_data->source, AL_GAIN, 1.0f);
      if (alGetError() != AL_NO_ERROR) {
         alSourcei(ex_data->source, AL_BUFFER, 0);
         alDeleteSources(1, &ex_data->source);
         alDeleteBuffers(ex_data->num_buffers, ex_data->buffers);
         al_free(ex_data->buffers);
         ex_data->buffers = NULL;
         return 1;
      }

      ex_data->stopped = false;
      ex_data->thread = al_create_thread(_openal_update, (void *)voice);
      al_start_thread(ex_data->thread);
   }

   ALLEGRO_INFO("Starting voice\n");
   return 0;
}
Exemplo n.º 19
0
static int oss_allocate_voice(ALLEGRO_VOICE *voice)
{
   int format;
   int chan_count;

   OSS_VOICE *ex_data = calloc(1, sizeof(OSS_VOICE));
   if (!ex_data)
      return 1;

   ex_data->fd = open(oss_audio_device, O_WRONLY/*, O_NONBLOCK*/);
   if (ex_data->fd == -1) {
      ALLEGRO_ERROR("Failed to open audio device '%s'.\n",
            oss_audio_device);
      ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
      free(ex_data);
      return 1;
   }

   chan_count = al_get_channel_count(voice->chan_conf);

   ex_data->frame_size = chan_count * al_get_depth_size(voice->depth);
   if (!ex_data->frame_size)
      goto Error;

   ex_data->stop = true;
   ex_data->stopped = true;

   if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT8)
      format = AFMT_S8;
   else if (voice->depth == ALLEGRO_AUDIO_DEPTH_UINT8)
      format = AFMT_U8;
   else if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT16)
      format = AFMT_S16_NE;
   else if (voice->depth == ALLEGRO_AUDIO_DEPTH_UINT16)
      format = AFMT_U16_NE;
#ifdef OSS_VER_4
   else if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT24)
      format = AFMT_S24_NE;
   else if (voice->depth == ALLEGRO_AUDIO_DEPTH_FLOAT32)
      format = AFMT_FLOAT;
#endif
   else {
      ALLEGRO_ERROR("Unsupported OSS sound format.\n");
      goto Error;
   }

   int tmp_format = format;
   int tmp_chan_count = chan_count;
   unsigned int tmp_freq = voice->frequency;
   int tmp_oss_fragsize = oss_fragsize;

   if (using_ver_4) {
#ifdef OSS_VER_4
      int tmp_oss_timing_policy = oss_timing_policy;
      if (ioctl(ex_data->fd, SNDCTL_DSP_POLICY, &tmp_oss_timing_policy) == -1) {
         ALLEGRO_ERROR("Failed to set_timig policity to '%i'.\n",
               tmp_oss_timing_policy);
         ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
         goto Error;
      }
      ALLEGRO_INFO("Accepted timing policy value: %i\n", tmp_oss_timing_policy);
#endif
   }
   else {
      if (ioctl(ex_data->fd, SNDCTL_DSP_SETFRAGMENT, &tmp_oss_fragsize) == -1) {
          ALLEGRO_ERROR("Failed to set fragment size.\n");
          ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
          goto Error;
      }
   }

   if (ioctl(ex_data->fd, SNDCTL_DSP_SETFMT, &tmp_format) == -1) {
      ALLEGRO_ERROR("Failed to set sample format.\n");
      ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
      goto Error;
   }
   if (tmp_format != format) {
      ALLEGRO_ERROR("Sample format not supported by the driver.\n");
      goto Error;
   }

   if (ioctl(ex_data->fd, SNDCTL_DSP_CHANNELS, &tmp_chan_count)) {
      ALLEGRO_ERROR("Failed to set channel count.\n");
      ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
      goto Error;
   }
   if (tmp_chan_count != chan_count) {
      ALLEGRO_ERROR("Requested sample channe count %i, got %i.\n",
            tmp_chan_count, chan_count);
   }

   if (ioctl(ex_data->fd, SNDCTL_DSP_SPEED, &tmp_freq) == -1) {
      ALLEGRO_ERROR("Failed to set sample rate.\n");
      ALLEGRO_ERROR("errno: %i -- %s\n", errno, strerror(errno));
      goto Error;
   }
   if (voice->frequency != tmp_freq) {
      ALLEGRO_ERROR("Requested sample rate %u, got %iu.\n", voice->frequency,
            tmp_freq);
   }

   voice->extra = ex_data;
   ex_data->quit_poll_thread = false;
   ex_data->poll_thread = al_create_thread(oss_update, (void*)voice);
   al_start_thread(ex_data->poll_thread);

   return 0;

Error:
   close(ex_data->fd);
   free(ex_data);
   return 1;
}
Exemplo n.º 20
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;
}
Exemplo n.º 21
0
DUMBA5_PLAYER * dumba5_start_duh_x(DUH *duh, int n_channels, long pos, float volume, long bufsize, int freq)
{
	DUMBA5_PLAYER * dp;
	ALLEGRO_CHANNEL_CONF c_conf;

	/* This restriction is imposed by Allegro. */
	ASSERT(n_channels > 0);
	ASSERT(n_channels <= 2);
	
	if(!duh)
	{
		return NULL;
	}

	dp = (DUMBA5_PLAYER *) malloc(sizeof(DUMBA5_PLAYER));
	if(!dp)
	{
		return NULL;
	}

	dp->flags = ADP_PLAYING;
	dp->bufsize = bufsize;
	dp->freq = freq;
	dp->channels = n_channels;
	if(n_channels == 1)
	{
		c_conf = ALLEGRO_CHANNEL_CONF_1;
	}
	else
	{
		c_conf = ALLEGRO_CHANNEL_CONF_2;
	}

	dp->stream = al_create_audio_stream(4, bufsize, freq, ALLEGRO_AUDIO_DEPTH_INT16, c_conf);

	if (!dp->stream) {
		free(dp);
		return NULL;
	}
	al_attach_audio_stream_to_mixer(dp->stream, al_get_default_mixer());

	dp->sigrenderer = dumb_it_start_at_order(duh, n_channels, pos);

	if (!dp->sigrenderer) {
		al_destroy_audio_stream(dp->stream);
		free(dp);
		return NULL;
	}
	dp->thread = al_create_thread(dumba5_update_thread, dp);
	if(!dp->thread)
	{
		return NULL;
	}

	dp->volume = volume;
	dp->silentcount = 0;
	dp->duh = duh;
	al_start_thread(dp->thread);

	return dp;
}
/* The start_voice should, surprise, start the voice. For streaming voices, it
   should start polling the device and call _al_voice_update for audio data.
   For non-streaming voices, it should resume playing from the last set
   position */
static int _dsound_start_voice(ALLEGRO_VOICE *voice)
{
   ALLEGRO_DS_DATA *ex_data = (ALLEGRO_DS_DATA *)voice->extra;
   HRESULT hr;
   MAKE_UNION(&ex_data->ds8_buffer, LPDIRECTSOUNDBUFFER8 *);

   ALLEGRO_DEBUG("Starting voice\n");

   if (!voice->is_streaming) {
      ex_data->ds8_buffer->SetCurrentPosition(0);
      hr = ex_data->ds8_buffer->Play(0, 0, 0);
      if (FAILED(hr)) {
         ALLEGRO_ERROR("Streaming voice failed to start\n");
         return 1;
      }
      ALLEGRO_INFO("Streaming voice started\n");
      return 0;
   }

   if (ex_data->stop_voice != 0) {
      ex_data->wave_fmt.wFormatTag = WAVE_FORMAT_PCM;
      ex_data->wave_fmt.nChannels = ex_data->channels;
      ex_data->wave_fmt.nSamplesPerSec = voice->frequency;
      ex_data->wave_fmt.nBlockAlign = ex_data->channels * (ex_data->bits_per_sample/8);
      ex_data->wave_fmt.nAvgBytesPerSec = ex_data->wave_fmt.nBlockAlign * voice->frequency;
      ex_data->wave_fmt.wBitsPerSample = ex_data->bits_per_sample;
      ex_data->wave_fmt.cbSize = 0;

      ex_data->desc.dwSize = sizeof(DSBUFFERDESC);
      ex_data->desc.dwFlags = DSBCAPS_LOCSOFTWARE | DSBCAPS_GLOBALFOCUS; /* FIXME: software mixing for now */
      ex_data->desc.dwBufferBytes = buffer_size;
      ex_data->desc.dwReserved = 0;
      ex_data->desc.lpwfxFormat = &ex_data->wave_fmt;
      ex_data->desc.guid3DAlgorithm = DS3DALG_DEFAULT;

      ALLEGRO_DEBUG("CreateSoundBuffer\n");

      hr = device->CreateSoundBuffer(&ex_data->desc, &ex_data->ds_buffer, NULL);
      if (FAILED(hr)) {
         ALLEGRO_ERROR("CreateSoundBuffer failed: %s\n", ds_get_error(hr));
         al_free(ex_data);
         return 1;
      }

      ALLEGRO_DEBUG("CreateSoundBuffer succeeded\n");

      ex_data->ds_buffer->QueryInterface(_al_IID_IDirectSoundBuffer8, u.v);

      ex_data->ds8_buffer->SetVolume(DSBVOLUME_MAX);

      ALLEGRO_DEBUG("Starting _dsound_update thread\n");

      ex_data->stop_voice = 0;
      ex_data->thread = al_create_thread(_dsound_update, (void*) voice);
      al_start_thread(ex_data->thread);
   }
   else {
      ALLEGRO_WARN("stop_voice == 0\n");
   }

   ALLEGRO_INFO("Voice started\n");
   return 0;
}
Exemplo n.º 23
0
/* Start the player. */
void dumba5_start_player(DUMBA5_PLAYER * pp)
{
	pp->flags = ADP_PLAYING;
	al_start_thread(pp->thread);
}
Exemplo n.º 24
0
/* Function: al_load_ogg_vorbis_audio_stream_f
 */
ALLEGRO_AUDIO_STREAM *al_load_ogg_vorbis_audio_stream_f(ALLEGRO_FILE* file,
	size_t buffer_count, unsigned int samples)
{
   const int word_size = 2; /* 1 = 8bit, 2 = 16-bit. nothing else */
   OggVorbis_File* vf;
   vorbis_info* vi;
   int channels;
   long rate;
   long total_samples;
   long total_size;
   AL_OV_DATA* extra;
   ALLEGRO_AUDIO_STREAM* stream;

   extra = _AL_MALLOC(sizeof(AL_OV_DATA));
   if (extra == NULL) {
      ALLEGRO_ERROR("Failed to allocate AL_OV_DATA struct.\n");
      return NULL;
   }
   
   if (file == NULL) {
      ALLEGRO_WARN("File failed to open\n");
      fprintf(stderr, "File failed to open\n");
      return NULL;
   }
   
   extra->file = file;
   
   vf = _AL_MALLOC(sizeof(OggVorbis_File));
   if (ov_open_callbacks(extra, vf, NULL, 0, callbacks) < 0) {
      ALLEGRO_WARN("ogg: Input does not appear to be an Ogg bitstream.\n");
      al_fclose(file);
      return NULL;
   }

   extra->vf = vf;

   vi = ov_info(vf, -1);
   channels = vi->channels;
   rate = vi->rate;
   total_samples = ov_pcm_total(vf,-1);
   total_size = total_samples * channels * word_size;

   extra->vi = vi;

   extra->bitstream = -1;

   ALLEGRO_DEBUG("channels %d\n", channels);
   ALLEGRO_DEBUG("word_size %d\n", word_size);
   ALLEGRO_DEBUG("rate %ld\n", rate);
   ALLEGRO_DEBUG("total_samples %ld\n", total_samples);
   ALLEGRO_DEBUG("total_size %ld\n", total_size);
	
   stream = al_create_audio_stream(buffer_count, samples, rate,
            _al_word_size_to_depth_conf(word_size),
            _al_count_to_channel_conf(channels));
   if (!stream) {
      free(vf);
      return NULL;
   }

   stream->extra = extra;

   extra->loop_start = 0.0;
   extra->loop_end = ogg_stream_get_length(stream);
   stream->feed_thread = al_create_thread(_al_kcm_feed_stream, stream);
   stream->quit_feed_thread = false;
   stream->feeder = ogg_stream_update;
   stream->rewind_feeder = ogg_stream_rewind;
   stream->seek_feeder = ogg_stream_seek;
   stream->get_feeder_position = ogg_stream_get_position;
   stream->get_feeder_length = ogg_stream_get_length;
   stream->set_feeder_loop = ogg_stream_set_loop;
   stream->unload_feeder = ogg_stream_close;
   al_start_thread(stream->feed_thread);
	
   return stream;
}
Exemplo n.º 25
0
/* initialises the sound system */
void init_sound()
{
   float f, osc1, osc2, freq1, freq2, vol, val;
   char *p;
   int len;
   int i;

   if (!al_is_audio_installed())
      return;

   /* zap (firing sound) consists of multiple falling saw waves */
   len = 8192;
   zap = create_sample_u8(22050, len);

   p = (char *)al_get_sample_data(zap);

   osc1 = 0;
   freq1 = 0.02;

   osc2 = 0;
   freq2 = 0.025;

   for (i=0; i<len; i++) {
      vol = (float)(len - i) / (float)len * 127;

      *p = 128 + (fmod(osc1, 1) + fmod(osc2, 1) - 1) * vol;

      osc1 += freq1;
      freq1 -= 0.000001;

      osc2 += freq2;
      freq2 -= 0.00000125;

      p++;
   }

   /* bang (explosion) consists of filtered noise */
   len = 8192;
   bang = create_sample_u8(22050, len);

   p = (char *)al_get_sample_data(bang);

   val = 0;

   for (i=0; i<len; i++) {
      vol = (float)(len - i) / (float)len * 255;
      val = (val * 0.75) + (RAND * 0.25);
      *p = 128 + val * vol;
      p++;
   }

   /* big bang (explosion) consists of noise plus rumble */
   len = 24576;
   bigbang = create_sample_u8(11025, len);

   p = (char *)al_get_sample_data(bigbang);

   val = 0;

   osc1 = 0;
   osc2 = 0;

   for (i=0; i<len; i++) {
      vol = (float)(len - i) / (float)len * 128;

      f = 0.5 + ((float)i / (float)len * 0.4);
      val = (val * f) + (RAND * (1-f));

      *p = 128 + (val + (sin(osc1) + sin(osc2)) / 4) * vol;

      osc1 += 0.03;
      osc2 += 0.04;

      p++;
   }

   /* ping consists of two sine waves */
   len = 8192;
   ping = create_sample_u8(22050, len);

   p = (char *)al_get_sample_data(ping);

   osc1 = 0;
   osc2 = 0;

   for (i=0; i<len; i++) {
      vol = (float)(len - i) / (float)len * 31;

      *p = 128 + (sin(osc1) + sin(osc2) - 1) * vol;

      osc1 += 0.2;
      osc2 += 0.3;

      p++;
   }

   ping_timer = al_install_timer(0.3);

   /* set up my lurvely music player :-) */
   if (!no_music) {
      init_music();
      al_start_timer(music_timer);
   }

   sound_update_thread = al_create_thread(sound_update_proc, NULL);
   al_start_thread(sound_update_thread);
}