transparent-cube

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

commit 5f8ccf83f029fb5ac896eb6fdb31d4e8ecb907ba
parent 1db4c4cfb547beacfa0dcf5481e277b6ac7eefcd
Author: amin <dev@aminmesbah.com>
Date:   Sun,  3 Feb 2019 04:50:23 +0000

Get it running on Windows

This is buttery smooth on Windows. That suggests that the cause of the
stuttering I was seeing is not my code. It's likely a problem with the
Linux mesa driver, the driver configuration (vsync?), or the compositor.

FossilOrigin-Name: 60318e8b0c61ff543f9db5d89ef16c13f7c18def7855e814b7b242f39763e81c
Diffstat:
Abuild.bat | 15+++++++++++++++
Asrc/platform_windows.c | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/shader.c | 27+++++++++++++++++++++++++--
3 files changed, 145 insertions(+), 2 deletions(-)

diff --git a/build.bat b/build.bat @@ -0,0 +1,15 @@ +@echo off + +set CFLAGS=-std=c99 -nostdlib -Ilib -I../include/glfw/include -Wall -Wextra -Wshadow -Wswitch-enum -Wno-unused-parameter -Wno-missing-braces +set LDFLAGS=-luser32 -lgdi32 -lwinmm -lopengl32 -lshell32 +set RELCFLAGS=-O2 -Os + +IF NOT EXIST ".\build" mkdir ".\build" +clang %CFLAGS% %RELCFLAGS%^ + src\platform_windows.c^ + src\game.c^ + src\glad.c^ + src\glmth.c^ + src\shader.c^ + ..\include\glfw\lib-vc2015\glfw3.lib^ + -o .\build\quaternion-demo.exe %LDFLAGS% diff --git a/src/platform_windows.c b/src/platform_windows.c @@ -0,0 +1,105 @@ +#include "platform_linux.h" + +#include <inttypes.h> +#include <stdint.h> +#include <stdio.h> +#include <time.h> + +#include <glad/glad.h> +#include <GLFW/glfw3.h> + + +void error_callback(int error, const char* description); +void framebuffer_size_callback(GLFWwindow* window, int width, int height); + +#ifdef GLAD_DEBUG +void pre_gl_callback(const char *func_name, void *func_ptr, int len_args, ...) +{ + printf("Calling: %s (%d arguments)\n", func_name, len_args); +} +#endif // GLAD_DEBUG + + +int main(void) +{ + glfwSetErrorCallback(error_callback); + + if (!glfwInit()) + { + fprintf(stderr, "GLFW initialization failed\n"); + return -1; + } + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); + glfwWindowHint(GLFW_SAMPLES, 4); + + GLFWwindow* window = glfwCreateWindow(PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT, "Quaternion Demo", NULL, NULL); + if (!window) + { + fprintf(stderr, "GLFW window creation failed\n"); + glfwTerminate(); + return -1; + } + glfwMakeContextCurrent(window); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + glfwSwapInterval(1); + + if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) + { + fprintf(stderr, "glad initialization failed\n"); + glfwDestroyWindow(window); + glfwTerminate(); + return -1; + } + +#ifdef USE_TEST_SEED + srand((uint32_t)0); +#else + srand((uint32_t)time(NULL)); +#endif // USE_TEST_SEED + + struct GameState game_state = {0}; + game_init(&game_state, PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT); + + uint64_t lag = 0; + uint64_t previous_ms = (glfwGetTimerValue() * PLATFORM_SECOND) / glfwGetTimerFrequency(); + + while (!glfwWindowShouldClose(window)) + { + uint64_t current_ms = (glfwGetTimerValue() * PLATFORM_SECOND) / glfwGetTimerFrequency(); + uint64_t elapsed_ms = current_ms - previous_ms; + previous_ms = current_ms; + lag += elapsed_ms; + //printf("%" PRIu64 ", %" PRIu64 ", %f\n", elapsed_ms, lag, PLATFORM_MS_PER_UPDATE); + + int32_t framebuffer_width = PLATFORM_SCR_WIDTH; + int32_t framebuffer_height = PLATFORM_SCR_HEIGHT; + glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height); + + game_update_and_render(&game_state, lag/PLATFORM_SECOND, framebuffer_width, framebuffer_height); + + glfwSwapBuffers(window); + glfwPollEvents(); + } + + game_cleanup(&game_state); + + glfwDestroyWindow(window); + glfwTerminate(); + return 0; +} + + +void error_callback(int error, const char* description) +{ + fprintf(stderr, "Error: %s\n", description); +} + + +void framebuffer_size_callback(GLFWwindow* window, int width, int height) +{ + glViewport(0, 0, width, height); +} diff --git a/src/shader.c b/src/shader.c @@ -44,8 +44,31 @@ char *read_file(char *file_path) struct Shader shader_compile(char *vertex_path, char *fragment_path) { - const GLchar *vertex_shader_source = read_file(vertex_path); - const GLchar *fragment_shader_source = read_file(fragment_path); + const GLchar *vertex_shader_source = + "#version 300 es\n" + "precision highp float;\n" + "layout (location = 0) in vec3 a_position;\n" + "layout (location = 1) in vec3 a_color;\n" + "uniform mat4 model;\n" + "uniform mat4 view;\n" + "uniform mat4 projection;\n" + "out vec3 color;\n" + "void main()\n" + "{\n" + " gl_Position = projection * view * model * vec4(a_position, 1.0f);\n" + " color = a_color;\n" + "}\n"; + + const GLchar *fragment_shader_source = + "#version 300 es\n" + "precision highp float;\n" + "uniform float alpha;\n" + "in vec3 color;\n" + "out vec4 frag_color;\n" + "void main()\n" + "{\n" + " frag_color = vec4(color, alpha);\n" + "}\n"; GLint success; GLchar info_log[512];