transparent-cube

Minimal cross-platform native/wasm graphics example.
git clone git://git.amin.space/transparent-cube.git
Log | Files | Refs | README | LICENSE

platform_windows.c (4367B)


      1 #include <inttypes.h>
      2 #include <stdarg.h>
      3 #include <stdint.h>
      4 #include <stdio.h>
      5 #include <time.h>
      6 
      7 #include <glad/glad.h>
      8 #include <GLFW/glfw3.h>
      9 
     10 // LOL, Windef.h
     11 #undef near
     12 #undef far
     13 
     14 #include "game.c"
     15 #include "platform.h"
     16 #include "platform_windows.h"
     17 
     18 void error_callback(int error, const char* description);
     19 void framebuffer_size_callback(GLFWwindow* window, int width, int height);
     20 
     21 #ifdef GLAD_DEBUG
     22 void pre_gl_callback(const char *func_name, void *func_ptr, int len_args, ...)
     23 {
     24     printf("Calling: %s (%d arguments)\n", func_name, len_args);
     25 }
     26 #endif // GLAD_DEBUG
     27 
     28 
     29 int main(void)
     30 {
     31     glfwSetErrorCallback(error_callback);
     32 
     33     if (!glfwInit())
     34     {
     35         fprintf(stderr, "GLFW initialization failed\n");
     36         return -1;
     37     }
     38 
     39     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
     40     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
     41     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
     42     glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
     43     glfwWindowHint(GLFW_SAMPLES, 4);
     44 
     45     GLFWwindow* window = glfwCreateWindow(PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT, "Quaternion Demo", NULL, NULL);
     46     if (!window)
     47     {
     48         fprintf(stderr, "GLFW window creation failed\n");
     49         glfwTerminate();
     50         return -1;
     51     }
     52     glfwMakeContextCurrent(window);
     53     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
     54     glfwSwapInterval(1);
     55 
     56     if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
     57     {
     58         fprintf(stderr, "glad initialization failed\n");
     59         glfwDestroyWindow(window);
     60         glfwTerminate();
     61         return -1;
     62     }
     63 
     64 #ifdef USE_TEST_SEED
     65     srand((uint32_t)0);
     66 #else
     67     srand((uint32_t)time(NULL));
     68 #endif // USE_TEST_SEED
     69 
     70     struct GameState game_state = {0};
     71     game_state.platform.platform_read_entire_file = &windows_read_entire_file;
     72     game_state.platform.platform_print = &windows_print;
     73     game_state.platform.platform_memory_free = &windows_memory_free;
     74 
     75     game_init(&game_state, PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT);
     76 
     77     uint64_t lag = 0;
     78     uint64_t previous_ms = (glfwGetTimerValue() * PLATFORM_SECOND) / glfwGetTimerFrequency();
     79 
     80     while (!glfwWindowShouldClose(window))
     81     {
     82         uint64_t current_ms = (glfwGetTimerValue() * PLATFORM_SECOND) / glfwGetTimerFrequency();
     83         uint64_t elapsed_ms = current_ms - previous_ms;
     84         previous_ms = current_ms;
     85         lag += elapsed_ms;
     86         //printf("%" PRIu64 ", %" PRIu64 ", %f\n", elapsed_ms, lag, PLATFORM_MS_PER_UPDATE);
     87 
     88         int32_t framebuffer_width = PLATFORM_SCR_WIDTH;
     89         int32_t framebuffer_height = PLATFORM_SCR_HEIGHT;
     90         glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height);
     91 
     92         game_update_and_render(&game_state, lag/PLATFORM_SECOND, framebuffer_width, framebuffer_height);
     93 
     94         glfwSwapBuffers(window);
     95         glfwPollEvents();
     96     }
     97 
     98     game_cleanup(&game_state);
     99 
    100     glfwDestroyWindow(window);
    101     glfwTerminate();
    102     return 0;
    103 }
    104 
    105 
    106 void error_callback(int error, const char* description)
    107 {
    108     fprintf(stderr, "Error: %s\n", description);
    109 }
    110 
    111 
    112 void framebuffer_size_callback(GLFWwindow* window, int width, int height)
    113 {
    114     glViewport(0, 0, width, height);
    115 }
    116 
    117 PLATFORM_MEMORY_FREE(windows_memory_free)
    118 {
    119     free(ptr);
    120 }
    121 
    122 PLATFORM_PRINT(windows_print)
    123 {
    124     va_list args;
    125     va_start(args, format);
    126     int num_chars_printed = vprintf(format, args);
    127     va_end(args);
    128     return num_chars_printed;
    129 }
    130 
    131 PLATFORM_READ_ENTIRE_FILE(windows_read_entire_file)
    132 {
    133     FILE *handle = fopen(file_path, "rb");
    134     char *buffer = NULL;
    135 
    136     if (handle)
    137     {
    138         // get file size
    139         fseek(handle, 0, SEEK_END);
    140         u32 num_bytes_in_file = ftell(handle);
    141         rewind(handle);
    142 
    143         // TODO: replace malloc with own allocator so I stop having nightmares
    144         buffer = (char*) malloc(sizeof(char) * (num_bytes_in_file + 1) );
    145 
    146         u32 bytes_read = fread(buffer, sizeof(char), num_bytes_in_file, handle);
    147         // IMPORTANT! fread() doesn't add the '\0'
    148         buffer[num_bytes_in_file] = '\0';
    149 
    150         if (num_bytes_in_file != bytes_read)
    151         {
    152             free(buffer);
    153             buffer = NULL;
    154         }
    155 
    156         fclose(handle);
    157     }
    158     else
    159     {
    160         printf("Error: Couldn't open file at path: %s", file_path);
    161         // TODO: handle errors here in a better way
    162         exit(1);
    163     }
    164 
    165     return buffer;
    166 }