pong.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "cglm/types.h"
  2. #include "scufeng/engine.h"
  3. #include "scufeng/text.h"
  4. #include "screen.h"
  5. #include "MainMenu.h"
  6. #include "SinglePlayer.h"
  7. #include "MultiPlayer.h"
  8. static const unsigned int screen_w = 1200;
  9. static const unsigned int screen_h = 600;
  10. static const float paddles_w = 0.01f;
  11. static const float paddles_h = 0.10f;
  12. static const float paddle_l_x = (1.0f / 16);
  13. static const float ball_w = paddles_w * 0.5f;
  14. static vec2 ball_pos = { 0.5f, 0.5f };
  15. static const float paddle_speed = 0.7f;
  16. static const float ball_speed = 0.7f;
  17. int main() {
  18. se_init(true);
  19. se_window_create(screen_w, screen_h, "Pong", -1);
  20. se_swap_interval(0);
  21. se_fps_lock_set_fps(60);
  22. se_font_load(FONT_PATH, 0, 48);
  23. se_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
  24. MainMenu mm = MainMenu_alloc(screen_w, screen_h);
  25. SinglePlayer sp;
  26. double last_frame_time = 0.0;
  27. while (!se_window_should_close()) {
  28. const double current_time = se_get_time();
  29. const double delta = current_time - last_frame_time;
  30. last_frame_time = current_time;
  31. se_fps_lock_frame_start_s(current_time);
  32. se_clear();
  33. se_handle_events();
  34. switch (current_screen) {
  35. case PONG_MAIN_MENU:
  36. current_screen = MainMenu_tick(&mm);
  37. if (current_screen == PONG_SINGLEPLAYER) {
  38. sp = SinglePlayer_alloc(
  39. screen_w,
  40. screen_h,
  41. paddles_w,
  42. paddles_h,
  43. paddle_l_x,
  44. paddle_speed,
  45. ball_w,
  46. ball_pos,
  47. ball_speed
  48. );
  49. }
  50. break;
  51. case PONG_SINGLEPLAYER:
  52. current_screen = SinglePlayer_tick(&sp, delta);
  53. if (current_screen == PONG_MAIN_MENU) {
  54. SinglePlayer_free(&sp);
  55. }
  56. break;
  57. case PONG_MULTIPLAYER:
  58. MultiPlayer_tick();
  59. break;
  60. }
  61. // printf("%u %u\n", sp.state.score_l, sp.state.score_r);
  62. se_fps_lock_wait();
  63. se_swap_buffers();
  64. // printf("FT: %lf FPS: %lf\n", se_get_time() - current_time, 1.0 / (se_get_time() - current_time));
  65. }
  66. return 0;
  67. }