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. window = glfwCreateWindow(width, height, title, NULL, NULL);
  46. if (!window) {
  47. fprintf(stderr, "Failed to create GLFW window\n");
  48. glfwTerminate();
  49. return;
  50. }
  51. glfwMakeContextCurrent(window);
  52. glfwSwapInterval(1);
  53. startTime = glfwGetTime();
  54. #endif
  55. lastFrameTime = GetTime();
  56. }
  57. float screenWidth;
  58. float screenHeight;
  59. float GetScreenWidth() {
  60. return screenWidth;
  61. }
  62. float GetScreenHeight() {
  63. return screenHeight;
  64. }
  65. float GetFrameTime(void) {
  66. return (float)frameTime;
  67. }
  68. void BeginDrawing() {
  69. glClear(GL_COLOR_BUFFER_BIT);
  70. glMatrixMode(GL_PROJECTION);
  71. glLoadIdentity();
  72. gluOrtho2D(0, screenWidth, 0, screenHeight);
  73. }
  74. void EndDrawing() {
  75. glSwapBuffers();
  76. double currentTime = GetTime();
  77. frameTime = currentTime - lastFrameTime;
  78. lastFrameTime = currentTime;
  79. }
  80. void SetTargetFPS(int targetFPS) {
  81. static clock_t lastTime = 0;
  82. clock_t currentTime = clock();
  83. double frameTime = 1000.0 / targetFPS;
  84. double elapsedTime = (double)(currentTime - lastTime) / CLOCKS_PER_SEC * 1000.0;
  85. if (elapsedTime < frameTime) {
  86. usleep((frameTime - elapsedTime) * 1000);
  87. }
  88. lastTime = currentTime;
  89. }
  90. void ClearBackground(Color color) {
  91. glClearColor(color.r / 255, color.g / 255, color.b / 255, color.a / 255);
  92. }
  93. void InitWindow(int argc, char** argv, int width, int height, const char* title) {
  94. glInit(&argc, argv, width, height, title);
  95. screenWidth = width;
  96. screenHeight = height;
  97. glEnable(GL_BLEND);
  98. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  99. glMatrixMode(GL_PROJECTION);
  100. glLoadIdentity();
  101. gluOrtho2D(0, width, height, 0);
  102. glMatrixMode(GL_MODELVIEW);
  103. }
  104. #ifndef _arch_dreamcast
  105. int WindowShouldClose() {
  106. return glfwWindowShouldClose(window);
  107. }
  108. void PollWindowEvents() {
  109. glfwPollEvents();
  110. }
  111. void CloseWindow() {
  112. glfwDestroyWindow(window);
  113. glfwTerminate();
  114. }
  115. static int keyPreviousState[GLFW_KEY_LAST] = {0};
  116. static int keyCurrentState[GLFW_KEY_LAST] = {0};
  117. void UpdateInput() {
  118. for (int i = 0; i < GLFW_KEY_LAST; i++) {
  119. keyPreviousState[i] = keyCurrentState[i];
  120. keyCurrentState[i] = glfwGetKey(window, i);
  121. }
  122. }
  123. int IsKeyPressed(int key) {
  124. if (key < 0 || key >= GLFW_KEY_LAST) return 0;
  125. return (keyCurrentState[key] == GLFW_PRESS && keyPreviousState[key] == GLFW_RELEASE);
  126. }
  127. int IsKeyDown(int key) {
  128. if (key < 0 || key >= GLFW_KEY_LAST) return 0;
  129. return (keyCurrentState[key] == GLFW_PRESS);
  130. }
  131. int IsKeyReleased(int key) {
  132. if (key < 0 || key >= GLFW_KEY_LAST) return 0;
  133. return (keyCurrentState[key] == GLFW_RELEASE && keyPreviousState[key] == GLFW_PRESS);
  134. }
  135. #endif