From fd56a839d33d8d33ff4722550f97465db56b379d Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 20 Apr 2018 22:05:11 +0200 Subject: Replace GLFW3 with SDL2 Replace all of GLFW3 with SDL2 for future benefits. --- Makefile | 2 +- faqe.c | 80 +++++++++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 839b032..fccbf40 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ EPRINTF_PATH ?= ../eprintf PKG_CONFIG ?= pkg-config LN ?= ln -sf -LIBS := glfw3 +LIBS := sdl2 CPPFLAGS += -D__gl_h_ CFLAGS += $(shell $(PKG_CONFIG) --cflags $(LIBS)) -std=c11 -MMD -MP LDFLAGS += -Wl,--as-needed diff --git a/faqe.c b/faqe.c index 0a9ab7d..ddcff69 100644 --- a/faqe.c +++ b/faqe.c @@ -2,7 +2,7 @@ * Copyright (C) 2018 Tomasz Kramkowski * SPDX-License-Identifier: MIT */ -#include +#include #include #include "assets.h" @@ -17,21 +17,11 @@ enum { HEIGHT = 600, }; -void fb_size_cb(GLFWwindow *win, int width, int height) -{ - (void)win; - gl_viewport(0, 0, width, height); -} - -void take_input(GLFWwindow *win) -{ - if (glfwGetKey(win, GLFW_KEY_ESCAPE) == GLFW_PRESS) - glfwSetWindowShouldClose(win, true); -} - int main(int argc, char **argv) { - GLFWwindow *win; + bool running = true; + SDL_Window *win; + SDL_GLContext *glc; GLuint ebo, vbo, vao, prog; GLint ucolor; struct vertex { @@ -51,20 +41,28 @@ int main(int argc, char **argv) setprogname(argc >= 1 && argv[0] != NULL ? argv[0] : "faqe"); - glfwInit(); + if (SDL_Init(SDL_INIT_VIDEO) != 0) + eprintf("Failed to initialise SDL: %s", SDL_GetError()); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - win = glfwCreateWindow(WIDTH, HEIGHT, "faqe", NULL, NULL); + win = SDL_CreateWindow("faqe", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + WIDTH, HEIGHT, + SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); if (win == NULL) - eprintf("Failed to create GLFW window"); - glfwMakeContextCurrent(win); + eprintf("Failed to create window: %s", SDL_GetError()); + + glc = SDL_GL_CreateContext(win); + if (glc == NULL) + eprintf("Failed to create OpenGL context: %s", SDL_GetError()); - gl_load((gl_loadfunc *)glfwGetProcAddress); - glfwSetFramebufferSizeCallback(win, &fb_size_cb); - fb_size_cb(win, WIDTH, HEIGHT); + gl_load((gl_loadfunc *)SDL_GL_GetProcAddress); + + gl_viewport(0, 0, WIDTH, HEIGHT); gl_buf_gen(1, &vbo); gl_buf_gen(1, &ebo); @@ -93,8 +91,30 @@ int main(int argc, char **argv) gl_poly_mode(GL_FRONT_AND_BACK, GL_LINE); - while (!glfwWindowShouldClose(win)) { - take_input(win); + while (running) { + SDL_Event ev; + + while (SDL_PollEvent(&ev)) { + switch (ev.type) { + case SDL_KEYDOWN: + case SDL_KEYUP: + if (ev.key.state != SDL_RELEASED + || ev.key.keysym.sym != SDLK_ESCAPE) + break; + /* fallthrough */ + case SDL_QUIT: + running = false; + break; + case SDL_WINDOWEVENT: + switch (ev.window.event) { + case SDL_WINDOWEVENT_SIZE_CHANGED: + gl_viewport(0, 0, + ev.window.data1, + ev.window.data2); + } + break; + } + } gl_clearcolor(0.2, 0.3, 0.3, 1.0); gl_clear(GL_COLOR_BUFFER_BIT); @@ -103,9 +123,11 @@ int main(int argc, char **argv) gl_draw_elems(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); gl_va_bind(0); - glfwSwapBuffers(win); - glfwPollEvents(); + SDL_GL_SwapWindow(win); } - glfwTerminate(); + SDL_GL_DeleteContext(glc); + SDL_DestroyWindow(win); + + SDL_Quit(); } -- cgit v1.2.3-54-g00ecf