SDL: examples/pen/01-drawing-lines: Match render target size to renderer output.

From c030e6f782d8641e7bd8c1dc2d58788cab4f2e4d Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Sat, 28 Dec 2024 14:58:53 -0500
Subject: [PATCH] examples/pen/01-drawing-lines: Match render target size to
 renderer output.

Otherwise, on HiDPI displays (like a retina iPad), the lines you draw don't
match where the pen is touching.
---
 examples/pen/01-drawing-lines/drawing-lines.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/examples/pen/01-drawing-lines/drawing-lines.c b/examples/pen/01-drawing-lines/drawing-lines.c
index 6d2430acd07f3..a530ab60fde2a 100644
--- a/examples/pen/01-drawing-lines/drawing-lines.c
+++ b/examples/pen/01-drawing-lines/drawing-lines.c
@@ -23,6 +23,8 @@ static float previous_touch_y = -1.0f;
 /* This function runs once at startup. */
 SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
 {
+    int w, h;
+
     SDL_SetAppMetadata("Example Pen Drawing Lines", "1.0", "com.example.pen-drawing-lines");
 
     if (!SDL_Init(SDL_INIT_VIDEO)) {
@@ -37,7 +39,10 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
 
     /* we make a render target so we can draw lines to it and not have to record and redraw every pen stroke each frame.
        Instead rendering a frame for us is a single texture draw. */
-    render_target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 640, 480);
+
+    /* make sure the render target matches output size (for hidpi displays, etc) so drawing matches the pen's position on a tablet display. */
+    SDL_GetRenderOutputSize(renderer, &w, &h);
+    render_target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
     if (!render_target) {
         SDL_Log("Couldn't create render target: %s", SDL_GetError());
         return SDL_APP_FAILURE;