Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement rendering of mouse cursors #39

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/render/ogc/SDL_render_ogc.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ static inline void OGC_SetBlendMode(SDL_Renderer *renderer, SDL_BlendMode blend_
{
OGC_RenderData *data = renderer->driverdata;

if (blend_mode == data->current_blend_mode) {
if (data->ops_after_present > 0 &&
blend_mode == data->current_blend_mode) {
/* Nothing to do */
return;
}
Expand Down
480 changes: 480 additions & 0 deletions src/video/ogc/SDL_ogccursors.h

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions src/video/ogc/SDL_ogcevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "../../events/SDL_events_c.h"

#include "SDL_ogcevents_c.h"
#include "SDL_ogcmouse.h"
#include "SDL_ogcvideo.h"

#include <ogc/system.h>
Expand All @@ -48,6 +49,9 @@ static const struct {

static void pump_ir_events(_THIS)
{
SDL_Cursor *cursor;
bool wiimote_pointed_at_screen = false;

if (!_this->windows) return;

if (!SDL_WasInit(SDL_INIT_JOYSTICK)) {
Expand All @@ -60,10 +64,11 @@ static void pump_ir_events(_THIS)
for (int i = 0; i < 4; i++) {
WPADData *data = WPAD_Data(i);

if (!data->ir.smooth_valid) continue;
if (!data->ir.valid) continue;

wiimote_pointed_at_screen = true;
SDL_SendMouseMotion(_this->windows, i,
0, data->ir.sx, data->ir.sy);
0, data->ir.x, data->ir.y);

for (int b = 0; b < MAX_WII_MOUSE_BUTTONS; b++) {
if (data->btns_d & s_mouse_button_map[b].wii) {
Expand All @@ -76,6 +81,17 @@ static void pump_ir_events(_THIS)
}
}
}

/* Unfortunately SDL in practice supports only one mouse, so we are
* consolidating all the wiimotes as a single device.
* Here we check if any wiimote is pointed at the screen, in which case we
* show the default cursor (the Wii hand); if not, then the default cursor
* is hidden. Note that this only affects applications which haven't
* explicitly set a cursor: the others remain in full control of whether a
* cursor should be shown or not. */
cursor = wiimote_pointed_at_screen ?
OGC_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND) : NULL;
SDL_SetDefaultCursor(cursor);
}
#endif

Expand Down
205 changes: 205 additions & 0 deletions src/video/ogc/SDL_ogcmouse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"

#if defined(SDL_VIDEO_DRIVER_OGC) && defined(__wii__)

#include "SDL_surface.h"
#include "SDL_hints.h"

#include "SDL_ogccursors.h"
#include "SDL_ogcgxcommon.h"
#include "SDL_ogcmouse.h"
#include "SDL_ogcpixels.h"

#include "../SDL_sysvideo.h"

#include <malloc.h>
#include <ogc/cache.h>
#include <ogc/gx.h>

typedef struct _OGC_CursorData
{
void *texels;
int hot_x, hot_y;
int w, h;
} OGC_CursorData;

static void draw_cursor_rect(OGC_CursorData *curdata, int x, int y)
{
GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
GX_Position2s16(x, y);
GX_TexCoord2u8(0, 0);
GX_Position2s16(x + curdata->w, y);
GX_TexCoord2u8(1, 0);
GX_Position2s16(x + curdata->w, y + curdata->h);
GX_TexCoord2u8(1, 1);
GX_Position2s16(x, y + curdata->h);
GX_TexCoord2u8(0, 1);
GX_End();
}

/* Create a cursor from a surface */
static SDL_Cursor *OGC_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
{
OGC_CursorData *curdata;
SDL_Cursor *cursor;
u32 texture_size;
SDL_Rect rect;

SDL_assert(surface->pitch == surface->w * 4);

cursor = SDL_calloc(1, sizeof(*cursor));
if (!cursor) {
SDL_OutOfMemory();
return NULL;
}

curdata = SDL_calloc(1, sizeof(*curdata));
if (!curdata) {
SDL_OutOfMemory();
SDL_free(cursor);
return NULL;
}

curdata->hot_x = hot_x;
curdata->hot_y = hot_y;
curdata->w = surface->w;
curdata->h = surface->h;

texture_size = GX_GetTexBufferSize(surface->w, surface->h, GX_TF_RGBA8,
GX_FALSE, 0);
curdata->texels = memalign(32, texture_size);
if (!curdata->texels) {
SDL_OutOfMemory();
SDL_free(curdata);
SDL_free(cursor);
return NULL;
}

rect.x = rect.y = 0;
rect.w = surface->w;
rect.h = surface->h;
OGC_pixels_to_texture(surface->pixels, surface->format->format, &rect,
surface->pitch, curdata->texels, surface->w);
DCStoreRange(curdata->texels, texture_size);
GX_InvalidateTexAll();

cursor->driverdata = curdata;

return cursor;
}

SDL_Cursor *OGC_CreateSystemCursor(SDL_SystemCursor id)
{
const OGC_Cursor *cursor;
SDL_Surface *surface;
SDL_Cursor *c;

switch (id) {
case SDL_SYSTEM_CURSOR_ARROW:
cursor = &OGC_cursor_arrow;
break;
case SDL_SYSTEM_CURSOR_HAND:
cursor = &OGC_cursor_hand;
break;
default:
SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO,
"System cursor %d not implemented", id);
return NULL;
}
surface =
SDL_CreateRGBSurfaceWithFormatFrom((void*)cursor->pixel_data,
cursor->width,
cursor->height,
cursor->bytes_per_pixel * 8,
cursor->width * cursor->bytes_per_pixel,
SDL_PIXELFORMAT_RGBA8888);
c = OGC_CreateCursor(surface, cursor->hot_x, cursor->hot_y);
SDL_FreeSurface(surface);
return c;
}

/* Free a window manager cursor */
static void OGC_FreeCursor(SDL_Cursor *cursor)
{
OGC_CursorData *curdata = cursor->driverdata;

if (curdata) {
if (curdata->texels) {
free(curdata->texels);
}
SDL_free(curdata);
}

SDL_free(cursor);
}

void OGC_InitMouse(_THIS)
{
SDL_Mouse *mouse = SDL_GetMouse();

mouse->CreateCursor = OGC_CreateCursor;
mouse->CreateSystemCursor = OGC_CreateSystemCursor;
mouse->FreeCursor = OGC_FreeCursor;
}

void OGC_QuitMouse(_THIS)
{
}

void OGC_draw_cursor(_THIS)
{
SDL_Mouse *mouse = SDL_GetMouse();
OGC_CursorData *curdata;
int x, y;

if (!mouse || !mouse->cursor_shown ||
!mouse->cur_cursor || !mouse->cur_cursor->driverdata) {
return;
}

curdata = mouse->cur_cursor->driverdata;
x = mouse->x - curdata->hot_x;
y = mouse->y - curdata->hot_y;
OGC_load_texture(curdata->texels, curdata->w, curdata->h, GX_TF_RGBA8,
SDL_ScaleModeNearest);

GX_ClearVtxDesc();
GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XY, GX_S16, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_U8, 0);
GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);

GX_SetTevOp(GX_TEVSTAGE0, GX_REPLACE);
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
GX_SetNumTevStages(1);
GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR);

GX_SetNumTexGens(1);
draw_cursor_rect(curdata, x, y);
GX_DrawDone();
}

#endif /* SDL_VIDEO_DRIVER_OGC */

/* vi: set ts=4 sw=4 expandtab: */
35 changes: 35 additions & 0 deletions src/video/ogc/SDL_ogcmouse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#ifndef SDL_OGC_mouse_h_
#define SDL_OGC_mouse_h_

#include "../SDL_sysvideo.h"
#include "../../events/SDL_mouse_c.h"

void OGC_InitMouse(_THIS);
void OGC_QuitMouse(_THIS);
void OGC_draw_cursor(_THIS);
SDL_Cursor *OGC_CreateSystemCursor(SDL_SystemCursor id);

#endif /* SDL_OGC_mouse_h_ */

/* vi: set ts=4 sw=4 expandtab: */
13 changes: 13 additions & 0 deletions src/video/ogc/SDL_ogcvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "SDL_ogcevents_c.h"
#include "SDL_ogcframebuffer_c.h"
#include "SDL_ogcgxcommon.h"
#include "SDL_ogcmouse.h"
#include "SDL_ogcvideo.h"

#include <malloc.h>
Expand Down Expand Up @@ -155,13 +156,21 @@ int OGC_VideoInit(_THIS)
SDL_AddDisplayMode(&_this->displays[0], &mode);

videodata->vmode = vmode;

#ifdef __wii__
OGC_InitMouse(_this);
#endif
return 0;
}

void OGC_VideoQuit(_THIS)
{
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;

#ifdef __wii__
OGC_QuitMouse(_this);
#endif

SDL_free(videodata->gp_fifo);
if (videodata->xfb[0])
free(MEM_K1_TO_K0(videodata->xfb[0]));
Expand All @@ -179,6 +188,10 @@ void OGC_video_flip(_THIS, bool vsync)
{
SDL_VideoData *videodata = _this->driverdata;
void *xfb = OGC_video_get_xfb(_this);

#ifdef __wii__
OGC_draw_cursor(_this);
#endif
GX_CopyDisp(xfb, GX_TRUE);
GX_DrawDone();
GX_Flush();
Expand Down