Browse Source

Hopefully fixed SDL rendering for High DPI display

vurtun 9 years ago
parent
commit
154ffb1133
2 changed files with 18 additions and 12 deletions
  1. 4 3
      demo/sdl_opengl3/main.c
  2. 14 9
      demo/sdl_opengl3/nuklear_sdl.c

+ 4 - 3
demo/sdl_opengl3/main.c

@@ -163,9 +163,10 @@ main(int argc, char* argv[])
         glClear(GL_COLOR_BUFFER_BIT);
         glClearColor(bg[0], bg[1], bg[2], bg[3]);
         /* IMPORTANT: `nk_sdl_render` modifies some global OpenGL state
-         * with blending, scissor, face culling and depth test and defaults everything
-         * back into a default state. Make sure to either save and restore or
-         * reset your own state after drawing rendering the UI. */
+         * with blending, scissor, face culling, depth test and viewport and
+         * defaults everything back into a default state.
+         * Make sure to either a.) save and restore or b.) reset your own state after
+         * rendering the UI. */
         nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY);
         SDL_GL_SwapWindow(win);}
     }

+ 14 - 9
demo/sdl_opengl3/nuklear_sdl.c

@@ -145,9 +145,10 @@ nk_sdl_device_destroy(void)
 NK_API void
 nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
 {
-
     struct nk_sdl_device *dev = &sdl.ogl;
     int width, height;
+    int display_width, display_height;
+    struct nk_vec2 scale;
     GLfloat ortho[4][4] = {
         {2.0f, 0.0f, 0.0f, 0.0f},
         {0.0f,-2.0f, 0.0f, 0.0f},
@@ -155,10 +156,15 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
         {-1.0f,1.0f, 0.0f, 1.0f},
     };
     SDL_GetWindowSize(sdl.win, &width, &height);
+    SDL_GL_GetDrawableSize(sdl.win, &display_width, &display_height);
     ortho[0][0] /= (GLfloat)width;
     ortho[1][1] /= (GLfloat)height;
 
+    scale.x = (float)display_width/(float)width;
+    scale.y = (float)display_height/(float)height;
+
     /* setup global state */
+    glViewport(0,0,display_width,display_height);
     glEnable(GL_BLEND);
     glBlendEquation(GL_FUNC_ADD);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -213,9 +219,12 @@ nk_sdl_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_b
         nk_draw_foreach(cmd, &sdl.ctx, &dev->cmds) {
             if (!cmd->elem_count) continue;
             glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
-            glScissor((GLint)cmd->clip_rect.x,
-                height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h),
-                (GLint)cmd->clip_rect.w, (GLint)cmd->clip_rect.h);
+            glScissor(
+                (GLint)(cmd->clip_rect.x * scale.x),
+                (GLint)((height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * scale.y),
+                (GLint)(cmd->clip_rect.w * scale.x),
+                (GLint)(cmd->clip_rect.h * scale.y));
+
             glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
             offset += cmd->elem_count;
         }
@@ -288,11 +297,7 @@ NK_API void
 nk_sdl_handle_event(SDL_Event *evt)
 {
     struct nk_context *ctx = &sdl.ctx;
-    if (evt->type == SDL_WINDOWEVENT) {
-        /* handle window resizing */
-        if (evt->window.event != SDL_WINDOWEVENT_RESIZED) return;
-        glViewport(0, 0, evt->window.data1, evt->window.data2);
-    } else if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
+    if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
         /* key events */
         int down = evt->type == SDL_KEYDOWN;
         const Uint8* state = SDL_GetKeyboardState(0);