event_loop.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <stdint.h>
  2. #include <SDL2/SDL.h>
  3. #include <SDL2/SDL_syswm.h>
  4. #include "render.h"
  5. #include "control_panel.h"
  6. #include "sdl_data.h"
  7. #include "font.h"
  8. #include "parameters.h"
  9. #include "playlist.h"
  10. #include "music.h"
  11. #include "id3.h"
  12. #include <Windows.h>
  13. #define DELAY_TIME 35
  14. void check_music() {
  15. if (!music_playing()) {
  16. const char *song = playlist_next();
  17. retrieve_id3_data(song);
  18. id3_reset_cycle();
  19. play_music(song);
  20. }
  21. }
  22. extern void render_control_panel();
  23. void main_event_loop() {
  24. uint32_t last_ticks = SDL_GetTicks();
  25. for (;;) {
  26. SDL_Event evt;
  27. while (SDL_PollEvent(&evt)) {
  28. if (evt.type == SDL_QUIT) return;
  29. if (evt.type == SDL_MOUSEBUTTONDOWN) {
  30. SDL_Window *win = SDL_GetWindowFromID(evt.button.windowID);
  31. if (win == control_window && (SDL_GetWindowFlags(control_window) & SDL_WINDOW_INPUT_FOCUS)) {
  32. control_mouse_down();
  33. }
  34. }
  35. }
  36. SDL_GL_MakeCurrent(control_window, control_context);
  37. font_set_context(1);
  38. render_control_panel();
  39. control_render();
  40. SDL_GL_SwapWindow(control_window);
  41. SDL_GL_MakeCurrent(main_window, main_context);
  42. font_set_context(0);
  43. render();
  44. SDL_GL_SwapWindow(main_window);
  45. uint32_t cur_ticks = SDL_GetTicks();
  46. uint32_t difference = cur_ticks - last_ticks; /* Time for loop */
  47. uint32_t delay = DELAY_TIME - (difference % DELAY_TIME);
  48. SDL_Delay(delay);
  49. last_ticks = cur_ticks;
  50. check_music();
  51. }
  52. }