From 6140f567ba1ad4cacb5fdc99e0d7807b29a4190b Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 12 Aug 2024 00:25:10 -0400
Subject: [PATCH] examples: added a basic pen example.
---
examples/CMakeLists.txt | 1 +
examples/pen/01-pen-drawing-lines/README.txt | 3 +
.../01-pen-drawing-lines/pen-drawing-lines.c | 104 ++++++++++++++++++
3 files changed, 108 insertions(+)
create mode 100644 examples/pen/01-pen-drawing-lines/README.txt
create mode 100644 examples/pen/01-pen-drawing-lines/pen-drawing-lines.c
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 298596feda716..36a80721b3f3d 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -186,6 +186,7 @@ add_sdl_example_executable(renderer-primitives SOURCES renderer/02-primitives/re
add_sdl_example_executable(audio-simple-playback SOURCES audio/01-simple-playback/simple-playback.c)
add_sdl_example_executable(audio-simple-playback-callback SOURCES audio/02-simple-playback-callback/simple-playback-callback.c)
add_sdl_example_executable(audio-load-wav SOURCES audio/03-load-wav/load-wav.c DATAFILES ${CMAKE_CURRENT_SOURCE_DIR}/../test/sample.wav)
+add_sdl_example_executable(pen-drawing-lines SOURCES pen/01-pen-drawing-lines/pen-drawing-lines.c)
add_sdl_example_executable(game-snake SOURCES game/01-snake/main.c game/01-snake/snake.c)
diff --git a/examples/pen/01-pen-drawing-lines/README.txt b/examples/pen/01-pen-drawing-lines/README.txt
new file mode 100644
index 0000000000000..693cda8f28647
--- /dev/null
+++ b/examples/pen/01-pen-drawing-lines/README.txt
@@ -0,0 +1,3 @@
+You need something with a pen/stylus for this to work.
+
+This takes pen input and draws lines. Lines are darker when you press harder.
diff --git a/examples/pen/01-pen-drawing-lines/pen-drawing-lines.c b/examples/pen/01-pen-drawing-lines/pen-drawing-lines.c
new file mode 100644
index 0000000000000..ef9aa6e6b267d
--- /dev/null
+++ b/examples/pen/01-pen-drawing-lines/pen-drawing-lines.c
@@ -0,0 +1,104 @@
+/*
+ * This example code reads pen/stylus input and draws lines. Darker lines
+ * for harder pressure.
+ *
+ * SDL can track multiple pens, but for simplicity here, this assumes any
+ * pen input we see was from one device.
+ *
+ * This code is public domain. Feel free to use it for any purpose!
+ */
+
+#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
+#include <SDL3/SDL.h>
+#include <SDL3/SDL_main.h>
+
+/* We will use this renderer to draw into this window every frame. */
+static SDL_Window *window = NULL;
+static SDL_Renderer *renderer = NULL;
+static SDL_Texture *render_target = NULL;
+static float pressure = 0.0f;
+static float previous_touch_x = -1.0f;
+static float previous_touch_y = -1.0f;
+
+/* This function runs once at startup. */
+int SDL_AppInit(void **appstate, int argc, char *argv[])
+{
+ if (SDL_Init(SDL_INIT_VIDEO) == -1) {
+ SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
+ return SDL_APP_FAILURE;
+ }
+
+ if (SDL_CreateWindowAndRenderer("examples/pen/drawing-lines", 640, 480, 0, &window, &renderer) == -1) {
+ SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
+ return SDL_APP_FAILURE;
+ }
+
+ /* 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);
+ if (!render_target) {
+ SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create render target!", SDL_GetError(), NULL);
+ return SDL_APP_FAILURE;
+ }
+
+ /* just blank the render target to gray to start. */
+ SDL_SetRenderTarget(renderer, render_target);
+ SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255);
+ SDL_RenderClear(renderer);
+ SDL_SetRenderTarget(renderer, NULL);
+ SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
+
+ return SDL_APP_CONTINUE; /* carry on with the program! */
+}
+
+/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
+int SDL_AppEvent(void *appstate, const SDL_Event *event)
+{
+ if (event->type == SDL_EVENT_QUIT) {
+ return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
+ }
+
+ /* There are several events that track the specific stages of pen activity,
+ but we're only going to look for motion and pressure, for simplicity. */
+ if (event->type == SDL_EVENT_PEN_MOTION) {
+ /* you can check for when the pen is touching, but if pressure > 0.0f, it's definitely touching! */
+ if (pressure > 0.0f) {
+ if (previous_touch_x >= 0.0f) { /* only draw if we're moving while touching */
+ /* draw with the alpha set to the pressure, so you effectively get a fainter line for lighter presses. */
+ SDL_SetRenderTarget(renderer, render_target);
+ SDL_SetRenderDrawColorFloat(renderer, 0, 0, 0, pressure);
+ SDL_RenderLine(renderer, previous_touch_x, previous_touch_y, event->pmotion.x, event->pmotion.y);
+ }
+ previous_touch_x = event->pmotion.x;
+ previous_touch_y = event->pmotion.y;
+ } else {
+ previous_touch_x = previous_touch_y = -1.0f;
+ }
+ } else if (event->type == SDL_EVENT_PEN_AXIS) {
+ if (event->paxis.axis == SDL_PEN_AXIS_PRESSURE) {
+ pressure = event->paxis.value; /* remember new pressure for later draws. */
+ }
+ }
+
+ return SDL_APP_CONTINUE; /* carry on with the program! */
+}
+
+/* This function runs once per frame, and is the heart of the program. */
+int SDL_AppIterate(void *appstate)
+{
+ /* make sure we're drawing to the window and not the render target */
+ SDL_SetRenderTarget(renderer, NULL);
+ SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
+ SDL_RenderClear(renderer); /* just in case. */
+ SDL_RenderTexture(renderer, render_target, NULL, NULL);
+ SDL_RenderPresent(renderer);
+ return SDL_APP_CONTINUE; /* carry on with the program! */
+}
+
+/* This function runs once at shutdown. */
+void SDL_AppQuit(void *appstate)
+{
+ SDL_DestroyTexture(render_target);
+ /* SDL will clean up the window/renderer for us. */
+}
+