base.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "../../include/base.h"
  2. #ifdef _arch_dreamcast
  3. #include "GL/glkos.h"
  4. #include "GL/glu.h"
  5. #include "GL/glext.h"
  6. #include "kos.h"
  7. #else
  8. #include "GLFW/glfw3.h"
  9. #include "GL/glu.h"
  10. GLFWwindow* window = NULL;
  11. #endif
  12. #include <time.h>
  13. #include <unistd.h>
  14. static double frameTime = 0.0;
  15. static double lastFrameTime = 0.0;
  16. static double startTime = 0.0;
  17. void glSwapBuffers() {
  18. #ifdef _arch_dreamcast
  19. glKosSwapBuffers();
  20. #else
  21. glfwSwapBuffers(window);
  22. #endif
  23. }
  24. float GetTime(void) {
  25. #ifdef _arch_dreamcast
  26. return (float)(clock() / (double)CLOCKS_PER_SEC - startTime);
  27. #else
  28. return (float)(glfwGetTime() - startTime);
  29. #endif
  30. }
  31. void glInit(int* pargc, char** argv, int width, int height, const char* title) {
  32. #ifdef _arch_dreamcast
  33. glKosInit(pargc, argv);
  34. startTime = clock() / (double)CLOCKS_PER_SEC;
  35. #else
  36. if (!glfwInit()) {
  37. fprintf(stderr, "Failed to initialize GLFW\n");
  38. return;
  39. }
  40. // Указываем OpenGL 1.1
  41. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  42. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
  43. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
  44. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE);
  45. glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
  46. window = glfwCreateWindow(width, height, title, NULL, NULL);
  47. if (!window) {
  48. fprintf(stderr, "Failed to create GLFW window\n");
  49. glfwTerminate();
  50. return;
  51. }
  52. glfwMakeContextCurrent(window);
  53. glfwSwapInterval(1);
  54. startTime = glfwGetTime();
  55. #endif
  56. lastFrameTime = GetTime();
  57. }
  58. float screenWidth;
  59. float screenHeight;
  60. float GetScreenWidth() {
  61. return screenWidth;
  62. }
  63. float GetScreenHeight() {
  64. return screenHeight;
  65. }
  66. float GetFrameTime(void) {
  67. return (float)frameTime;
  68. }
  69. void BeginDrawing() {
  70. glClear(GL_COLOR_BUFFER_BIT);
  71. glMatrixMode(GL_PROJECTION);
  72. glLoadIdentity();
  73. gluOrtho2D(0, screenWidth, 0, screenHeight);
  74. }
  75. void EndDrawing() {
  76. glSwapBuffers();
  77. double currentTime = GetTime();
  78. frameTime = currentTime - lastFrameTime;
  79. lastFrameTime = currentTime;
  80. }
  81. void SetTargetFPS(int targetFPS) {
  82. static clock_t lastTime = 0;
  83. clock_t currentTime = clock();
  84. double frameTime = 1000.0 / targetFPS;
  85. double elapsedTime = (double)(currentTime - lastTime) / CLOCKS_PER_SEC * 1000.0;
  86. if (elapsedTime < frameTime) {
  87. usleep((frameTime - elapsedTime) * 1000);
  88. }
  89. lastTime = currentTime;
  90. }
  91. void ClearBackground(Color color) {
  92. glClearColor(color.r / 255, color.g / 255, color.b / 255, color.a / 255);
  93. }
  94. void InitWindow(int argc, char** argv, int width, int height, const char* title) {
  95. glInit(&argc, argv, width, height, title);
  96. screenWidth = width;
  97. screenHeight = height;
  98. glEnable(GL_BLEND);
  99. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  100. glMatrixMode(GL_PROJECTION);
  101. glLoadIdentity();
  102. gluOrtho2D(0, width, height, 0);
  103. glMatrixMode(GL_MODELVIEW);
  104. }
  105. #ifndef _arch_dreamcast
  106. int WindowShouldClose() {
  107. return glfwWindowShouldClose(window);
  108. }
  109. void PollWindowEvents() {
  110. glfwPollEvents();
  111. }
  112. void CloseWindow() {
  113. glfwDestroyWindow(window);
  114. glfwTerminate();
  115. }
  116. static int keyPreviousState[GLFW_KEY_LAST] = {0};
  117. static int keyCurrentState[GLFW_KEY_LAST] = {0};
  118. void UpdateInput() {
  119. for (int i = 0; i < GLFW_KEY_LAST; i++) {
  120. keyPreviousState[i] = keyCurrentState[i];
  121. keyCurrentState[i] = glfwGetKey(window, i);
  122. }
  123. }
  124. int IsKeyPressed(int key) {
  125. if (key < 0 || key >= GLFW_KEY_LAST) return 0;
  126. return (keyCurrentState[key] == GLFW_PRESS && keyPreviousState[key] == GLFW_RELEASE);
  127. }
  128. int IsKeyDown(int key) {
  129. if (key < 0 || key >= GLFW_KEY_LAST) return 0;
  130. return (keyCurrentState[key] == GLFW_PRESS);
  131. }
  132. int IsKeyReleased(int key) {
  133. if (key < 0 || key >= GLFW_KEY_LAST) return 0;
  134. return (keyCurrentState[key] == GLFW_RELEASE && keyPreviousState[key] == GLFW_PRESS);
  135. }
  136. #endif