| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include "../../include/base.h"
- #ifdef _arch_dreamcast
- #include "GL/glkos.h"
- #include "GL/glu.h"
- #include "GL/glext.h"
- #include "kos.h"
- #else
- #include "GLFW/glfw3.h"
- #include "GL/glu.h"
- GLFWwindow* window = NULL;
- #endif
- #include <time.h>
- #include <unistd.h>
- static double frameTime = 0.0;
- static double lastFrameTime = 0.0;
- static double startTime = 0.0;
- void glSwapBuffers() {
- #ifdef _arch_dreamcast
- glKosSwapBuffers();
- #else
- glfwSwapBuffers(window);
- #endif
- }
- float GetTime(void) {
- #ifdef _arch_dreamcast
- return (float)(clock() / (double)CLOCKS_PER_SEC - startTime);
- #else
- return (float)(glfwGetTime() - startTime);
- #endif
- }
- void glInit(int* pargc, char** argv, int width, int height, const char* title) {
- #ifdef _arch_dreamcast
- glKosInit(pargc, argv);
- startTime = clock() / (double)CLOCKS_PER_SEC;
- #else
- if (!glfwInit()) {
- fprintf(stderr, "Failed to initialize GLFW\n");
- return;
- }
-
- // Указываем OpenGL 1.1
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE);
-
- window = glfwCreateWindow(width, height, title, NULL, NULL);
- if (!window) {
- fprintf(stderr, "Failed to create GLFW window\n");
- glfwTerminate();
- return;
- }
-
- glfwMakeContextCurrent(window);
- glfwSwapInterval(1);
-
- startTime = glfwGetTime();
- #endif
-
- lastFrameTime = GetTime();
- }
- float screenWidth;
- float screenHeight;
- float GetScreenWidth() {
- return screenWidth;
- }
- float GetScreenHeight() {
- return screenHeight;
- }
- float GetFrameTime(void) {
- return (float)frameTime;
- }
- void BeginDrawing() {
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0, screenWidth, 0, screenHeight);
- }
- void EndDrawing() {
- glSwapBuffers();
-
- double currentTime = GetTime();
- frameTime = currentTime - lastFrameTime;
- lastFrameTime = currentTime;
- }
- void SetTargetFPS(int targetFPS) {
- static clock_t lastTime = 0;
- clock_t currentTime = clock();
-
- double frameTime = 1000.0 / targetFPS;
- double elapsedTime = (double)(currentTime - lastTime) / CLOCKS_PER_SEC * 1000.0;
-
- if (elapsedTime < frameTime) {
- usleep((frameTime - elapsedTime) * 1000);
- }
-
- lastTime = currentTime;
- }
- void ClearBackground(Color color) {
- glClearColor(color.r / 255, color.g / 255, color.b / 255, color.a / 255);
- }
- void InitWindow(int argc, char** argv, int width, int height, const char* title) {
- glInit(&argc, argv, width, height, title);
- screenWidth = width;
- screenHeight = height;
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0, width, height, 0);
- glMatrixMode(GL_MODELVIEW);
- }
- #ifndef _arch_dreamcast
- int WindowShouldClose() {
- return glfwWindowShouldClose(window);
- }
- void PollWindowEvents() {
- glfwPollEvents();
- }
- void CloseWindow() {
- glfwDestroyWindow(window);
- glfwTerminate();
- }
- static int keyPreviousState[GLFW_KEY_LAST] = {0};
- static int keyCurrentState[GLFW_KEY_LAST] = {0};
- void UpdateInput() {
- for (int i = 0; i < GLFW_KEY_LAST; i++) {
- keyPreviousState[i] = keyCurrentState[i];
- keyCurrentState[i] = glfwGetKey(window, i);
- }
- }
- int IsKeyPressed(int key) {
- if (key < 0 || key >= GLFW_KEY_LAST) return 0;
- return (keyCurrentState[key] == GLFW_PRESS && keyPreviousState[key] == GLFW_RELEASE);
- }
- int IsKeyDown(int key) {
- if (key < 0 || key >= GLFW_KEY_LAST) return 0;
- return (keyCurrentState[key] == GLFW_PRESS);
- }
- int IsKeyReleased(int key) {
- if (key < 0 || key >= GLFW_KEY_LAST) return 0;
- return (keyCurrentState[key] == GLFW_RELEASE && keyPreviousState[key] == GLFW_PRESS);
- }
- #endif
|