summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c37
1 files changed, 34 insertions, 3 deletions
diff --git a/main.c b/main.c
index 76811ec..fa61427 100644
--- a/main.c
+++ b/main.c
@@ -116,8 +116,22 @@ link_compute_program(
}
#endif
+static void
+update_viewport(
+ SDL_Window *win
+) {
+ int w, h;
+
+ // get window drawable size
+ SDL_GL_GetDrawableSize(win, &w, &h);
+
+ // set viewport size
+ glViewport(0, 0, w, h);
+}
+
static SDL_Window *
init(void) {
+ // init sdl
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
SDL_Log("SDL_Init() failed: %s", SDL_GetError());
exit(EXIT_FAILURE);
@@ -168,7 +182,7 @@ init(void) {
}
// init glew
- // (do this immediately after setting context)
+ // (do this immediately after GL context init)
{
GLenum err = glewInit();
if (err != GLEW_OK) {
@@ -177,6 +191,10 @@ init(void) {
}
}
+ // set viewport size
+ // (after GLEW init)
+ update_viewport(win);
+
// set swap interval
// (after context init)
if (SDL_GL_SetSwapInterval(1) < 0) {
@@ -195,12 +213,14 @@ static Uint32 timer_cb(
SDL_Event ev;
UNUSED(arg);
+ // build synthetic event
ev.type = SDL_USEREVENT;
ev.user.type = SDL_USEREVENT;
ev.user.code = EVENT_CODE_TIMER;
ev.user.data1 = NULL;
ev.user.data2 = NULL;
+ // add event to queue
SDL_PushEvent(&ev);
// return interval
@@ -287,6 +307,7 @@ int main(int argc, char *argv[]) {
break;
case SDL_KEYDOWN:
switch (ev.key.keysym.sym) {
+ case SDLK_q:
case SDLK_ESCAPE:
done = true;
@@ -310,8 +331,18 @@ int main(int argc, char *argv[]) {
case SDL_WINDOWEVENT:
switch (ev.window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
- // update viewport
- glViewport(0, 0, ev.window.data1, ev.window.data2);
+ {
+ // map event to window
+ SDL_Window *win = SDL_GetWindowFromID(ev.window.windowID);
+
+ if (win) {
+ // update viewport
+ update_viewport(win);
+ } else {
+ SDL_Log("ignoring SIZE_CHANGED event from unknown window");
+ }
+ }
+
break;
}