Skip to content

Commit

Permalink
Fix inverted pen Y tilt on macOS and add tilt display to example
Browse files Browse the repository at this point in the history
  • Loading branch information
AkiSakurai authored and slouken committed Jan 30, 2025
1 parent 3136447 commit 943579a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions examples/pen/01-drawing-lines/drawing-lines.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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;
static float tilt_x = 0.0f;
static float tilt_y = 0.0f;

/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
Expand Down Expand Up @@ -84,6 +86,10 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
} 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. */
} else if(event->paxis.axis == SDL_PEN_AXIS_XTILT) {
tilt_x = event->paxis.value;
} else if(event->paxis.axis == SDL_PEN_AXIS_YTILT) {
tilt_y = event->paxis.value;
}
}

Expand All @@ -93,11 +99,15 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
char debug_text[1024] = {};

/* make sure we're drawing to the window and not the render target */
SDL_SetRenderTarget(renderer, NULL);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer); /* just in case. */
SDL_RenderTexture(renderer, render_target, NULL, NULL);
SDL_snprintf(debug_text, sizeof(debug_text), "Tilt: %f %f", tilt_x, tilt_y);
SDL_RenderDebugText(renderer, 0, 8, debug_text);
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
Expand Down
2 changes: 1 addition & 1 deletion src/video/cocoa/SDL_cocoapen.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static void Cocoa_HandlePenPointEvent(SDL_CocoaWindowData *_data, NSEvent *event
SDL_SendPenAxis(timestamp, pen, window, SDL_PEN_AXIS_PRESSURE, [event pressure]);
SDL_SendPenAxis(timestamp, pen, window, SDL_PEN_AXIS_ROTATION, [event rotation]);
SDL_SendPenAxis(timestamp, pen, window, SDL_PEN_AXIS_XTILT, ((float) tilt.x) * 90.0f);
SDL_SendPenAxis(timestamp, pen, window, SDL_PEN_AXIS_YTILT, ((float) tilt.y) * 90.0f);
SDL_SendPenAxis(timestamp, pen, window, SDL_PEN_AXIS_YTILT, ((float) -tilt.y) * 90.0f);
SDL_SendPenAxis(timestamp, pen, window, SDL_PEN_AXIS_TANGENTIAL_PRESSURE, event.tangentialPressure);
}

Expand Down

0 comments on commit 943579a

Please sign in to comment.