summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2017-05-11 14:21:39 -0400
committerPaul Duncan <pabs@pablotron.org>2017-05-11 14:21:39 -0400
commit62055dc7863169ad5522bfc2b9fb4fa3ac6c6d1f (patch)
tree670cf6d1c7c1aa0f5d9b6146362d5fcf67b0262a
parent02dbb120fa4e1d1ad47d98807552d7c036fd2907 (diff)
downloadcompute-test-62055dc7863169ad5522bfc2b9fb4fa3ac6c6d1f.tar.bz2
compute-test-62055dc7863169ad5522bfc2b9fb4fa3ac6c6d1f.zip
switch from glfw to sdl2
-rw-r--r--Makefile2
-rw-r--r--main.c184
2 files changed, 103 insertions, 83 deletions
diff --git a/Makefile b/Makefile
index cb1ae8e..46cebbd 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
CFLAGS=-std=c11 -W -Wall -pedantic -O2
APP=compute-test
-LIBS=-lglfw -lGLEW -lGL
+LIBS=-lSDL2 -lGLEW -lGL
OBJS=main.o
.PHONY=all clean test
diff --git a/main.c b/main.c
index 22266db..79a9d58 100644
--- a/main.c
+++ b/main.c
@@ -1,8 +1,9 @@
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
-#include <GLFW/glfw3.h>
+#include <SDL2/SDL.h>
#define UNUSED(a) ((void) (a))
@@ -35,54 +36,6 @@ fs_src =
" color = vec4(0.5f + 0.5 * sin(time * 3.141), 0, 0, 1);\n"
"}\n";
-static void
-error_cb(
- const int code,
- const char *text
-) {
- UNUSED(code);
- fprintf(stderr, "GLFW Error: %s\n", text);
-}
-
-static void
-key_cb(
- GLFWwindow *win,
- int key,
- int code,
- int action,
- int mods
-) {
- UNUSED(code);
- UNUSED(mods);
-
- if (action == GLFW_RELEASE && key == GLFW_KEY_ESCAPE)
- glfwSetWindowShouldClose(win, GLFW_TRUE);
-}
-
-static void
-framebuffer_size_cb(
- GLFWwindow *win,
- const int width,
- const int height
-) {
- UNUSED(win);
-
- fprintf(stderr, "width = %d, height = %d\n", width, height);
- glViewport(0, 0, width, height);
-}
-
-static void drop_cb(
- GLFWwindow *win,
- const int num_paths,
- const char ** paths
-) {
- UNUSED(win);
-
- for (int i = 0; i < num_paths; i++) {
- fprintf(stderr, "drop path: %s\n", paths[i]);
- }
-}
-
static GLuint
compile_shader(
const GLenum type,
@@ -157,44 +110,73 @@ link_compute_program(
}
#endif
-static GLFWwindow *
+static SDL_Window *
init(void) {
- // init glfw
- glfwSetErrorCallback(error_cb);
- if (!glfwInit())
+ if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+ SDL_Log("SDL_Init() failed: %s", SDL_GetError());
exit(EXIT_FAILURE);
-
- // set api to GLES 3.2
- glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
+ }
// create window
- GLFWwindow *win = glfwCreateWindow(640, 480, "Compute Test", NULL, NULL);
+ SDL_Window *win = SDL_CreateWindow(
+ "Compute Test",
+ SDL_WINDOWPOS_UNDEFINED,
+ SDL_WINDOWPOS_UNDEFINED,
+ 640,
+ 480,
+ SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
+ );
+
+ // check for error
if (!win) {
+ SDL_Log("SDL_CreateWindow() failed: %s", SDL_GetError());
+ exit(EXIT_FAILURE);
+ }
+
+ // set gl context majory version hint
+ if (SDL_GL_SetAttribute(
+ SDL_GL_CONTEXT_PROFILE_MASK,
+ SDL_GL_CONTEXT_PROFILE_ES
+ ) < 0) {
+ SDL_Log("SDL_GL_SetAttribute() failed: %s", SDL_GetError());
+ exit(EXIT_FAILURE);
+ }
+
+ // set gl context majory version hint
+ if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) {
+ SDL_Log("SDL_GL_SetAttribute() failed: %s", SDL_GetError());
+ exit(EXIT_FAILURE);
+ }
+
+ // set gl context minor version hint
+ if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2) < 0) {
+ SDL_Log("SDL_GL_SetAttribute() failed: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
- // set gl context
- glfwMakeContextCurrent(win);
+ // create GL context
+ SDL_GLContext context = SDL_GL_CreateContext(win);
+ if (!context) {
+ SDL_Log("SDL_GL_CreateContext() failed: %s", SDL_GetError());
+ exit(EXIT_FAILURE);
+ }
// init glew
// (do this immediately after setting context)
- GLenum err = glewInit();
- if (err != GLEW_OK) {
- fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
- exit(EXIT_FAILURE);
+ {
+ GLenum err = glewInit();
+ if (err != GLEW_OK) {
+ SDL_Log("glewInit() failed: %s", glewGetErrorString(err));
+ exit(EXIT_FAILURE);
+ }
}
// set swap interval
// (after context init)
- glfwSwapInterval(1);
-
- // bind event handlers
- // (after context init)
- glfwSetKeyCallback(win, key_cb);
- glfwSetFramebufferSizeCallback(win, framebuffer_size_cb);
- glfwSetDropCallback(win, drop_cb);
+ if (SDL_GL_SetSwapInterval(1) < 0) {
+ SDL_Log("SDL_GL_SetSwapInterval() failed: %s", SDL_GetError());
+ exit(EXIT_FAILURE);
+ }
// return window
return win;
@@ -204,7 +186,7 @@ int main(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);
- GLFWwindow *win = init();
+ SDL_Window *win = init();
// generate vertex array
GLuint vao;
@@ -228,14 +210,15 @@ int main(int argc, char *argv[]) {
GLint u_time = glGetUniformLocation(prog, "time");
// main loop
- while (!glfwWindowShouldClose(win)) {
+ bool done = false, fullscreen = false;
+ while (!done) {
// clear screen
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
// use program, set uniform
glUseProgram(prog);
- glUniform1f(u_time, glfwGetTime());
+ glUniform1f(u_time, SDL_GetTicks() / 1000.0);
// draw
glBindVertexArray(vao);
@@ -243,18 +226,55 @@ int main(int argc, char *argv[]) {
glBindVertexArray(0);
// swap buffer
- glfwSwapBuffers(win);
+ SDL_GL_SwapWindow(win);
// handle events
- glfwPollEvents();
- // glfwWaitEvents();
+ {
+ SDL_Event ev;
+
+ while (SDL_PollEvent(&ev)) {
+ switch (ev.type) {
+ case SDL_QUIT:
+ done = true;
+ break;
+ case SDL_KEYDOWN:
+ switch (ev.key.keysym.sym) {
+ case SDLK_ESCAPE:
+ done = true;
+
+ break;
+ case SDLK_F11:
+ // toggle fullscreen
+ fullscreen = !fullscreen;
+
+ if (SDL_SetWindowFullscreen(
+ win,
+ fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0
+ ) < 0) {
+ SDL_Log("SDL_SetWindowFullscreen() failed: %s", SDL_GetError());
+ fullscreen = !fullscreen;
+ }
+
+ break;
+ }
+
+ break;
+ case SDL_WINDOWEVENT:
+ switch (ev.window.event) {
+ case SDL_WINDOWEVENT_SIZE_CHANGED:
+ // update viewport
+ glViewport(0, 0, ev.window.data1, ev.window.data2);
+ break;
+ }
+
+ break;
+ }
+ }
+ }
}
// destroy window
- glfwDestroyWindow(win);
-
- // fini glfw
- glfwTerminate();
+ SDL_DestroyWindow(win);
// return success
return EXIT_SUCCESS;