diff --git a/main.cpp b/main.cpp index 68fd61b..4d554d2 100644 --- a/main.cpp +++ b/main.cpp @@ -60,6 +60,22 @@ void makeRotationZ(float angle, float* m) m[3] = 0; m[7] = 0; m[11] = 0; m[15] = 1; } +void computeLetterbox(int windowW, int windowH, + int logicalW, int logicalH, + int* x, int* y, int* w, int* h) +{ + float scaleX = (float)windowW / logicalW; + float scaleY = (float)windowH / logicalH; + + float scale = (scaleX < scaleY) ? scaleX : scaleY; + + *w = (int)(logicalW * scale); + *h = (int)(logicalH * scale); + + *x = (windowW - *w) / 2; + *y = (windowH - *h) / 2; +} + extern "C" int APP_MAIN(int argc, char **argv) { SDL_SetAppMetadata("Example Renderer Primitives", "1.0", "com.example.renderer-primitives"); SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight"); @@ -145,7 +161,15 @@ extern "C" int APP_MAIN(int argc, char **argv) { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); - glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); + int drawableW, drawableH; + SDL_GetWindowSizeInPixels(window, &drawableW, &drawableH); + + int vx, vy, vw, vh; + computeLetterbox(drawableW, drawableH, + SCREEN_WIDTH, SCREEN_HEIGHT, + &vx, &vy, &vw, &vh); + + glViewport(vx, vy, vw, vh); glEnable(GL_DEPTH_TEST); float transform[16];