From 967d52ea6ac288e50e284477a8f9c55125ecf46b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 1 Aug 2024 19:51:21 +1000 Subject: [PATCH] Virtual cursor: Implement in-game too --- src/VirtualCursor.h | 59 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/src/VirtualCursor.h b/src/VirtualCursor.h index 261e698ce..ad960166f 100644 --- a/src/VirtualCursor.h +++ b/src/VirtualCursor.h @@ -3,24 +3,66 @@ #include "Drawer2D.h" #include "Input.h" #include "LBackend.h" +#include "Graphics.h" +#include "Game.h" + static cc_bool vc_hooked; +static struct Texture vc_texture; +static GfxResourceID vc_vb; #define CURSOR_SIZE 1 #define CURSOR_EXTENT 5 -static void VirtualCursor_Display2D(struct Context2D* ctx) { - int x, y; - LBackend_MarkAllDirty(); - - x = Pointers[0].x; - y = Pointers[0].y; - +static void VirtualCursor_Draw(struct Context2D* ctx, int x, int y) { Context2D_Clear(ctx, BITMAPCOLOR_WHITE, x - CURSOR_EXTENT, y - CURSOR_SIZE, CURSOR_EXTENT * 2, CURSOR_SIZE * 3); Context2D_Clear(ctx, BITMAPCOLOR_WHITE, x - CURSOR_SIZE, y - CURSOR_EXTENT, CURSOR_SIZE * 3, CURSOR_EXTENT * 2); } +static void VirtualCursor_Display2D(struct Context2D* ctx) { + LBackend_MarkAllDirty(); + VirtualCursor_Draw(ctx, Pointers[0].x, Pointers[0].y); +} + +static void VirtualCursor_MakeTexture(void) { + struct Context2D ctx; + Context2D_Alloc(&ctx, CURSOR_EXTENT * 2, CURSOR_EXTENT * 2); + { + VirtualCursor_Draw(&ctx, CURSOR_EXTENT, CURSOR_EXTENT); + Context2D_MakeTexture(&vc_texture, &ctx); + } + Context2D_Free(&ctx); +} + +/* TODO hook into context lost etc */ +static void VirtualCursor_Display3D(float delta) { + if (!DisplayInfo.CursorVisible) return; + + if (!vc_vb) { + vc_vb = Gfx_CreateDynamicVb(VERTEX_FORMAT_TEXTURED, 4); + if (!vc_vb) return; + } + + if (!vc_texture.ID) { + VirtualCursor_MakeTexture(); + if (!vc_texture.ID) return; + } + + vc_texture.x = Pointers[0].x - CURSOR_EXTENT; + vc_texture.y = Pointers[0].y - CURSOR_EXTENT; + int X = vc_texture.x, Y = vc_texture.y; + + Gfx_SetVertexFormat(VERTEX_FORMAT_TEXTURED); + Gfx_BindTexture(vc_texture.ID); + + struct VertexTextured* data = (struct VertexTextured*)Gfx_LockDynamicVb(vc_vb, VERTEX_FORMAT_TEXTURED, 4); + struct VertexTextured** ptr = &data; + Gfx_Make2DQuad(&vc_texture, PACKEDCOL_WHITE, ptr); + Gfx_UnlockDynamicVb(vc_vb); + Gfx_DrawVb_IndexedTris(4); +} + static void VirtualCursor_SetPosition(int x, int y) { x = max(0, min(x, Window_Main.Width - 1)); y = max(0, min(y, Window_Main.Height - 1)); @@ -28,7 +70,8 @@ static void VirtualCursor_SetPosition(int x, int y) { if (x == Pointers[0].x && y == Pointers[0].y) return; Pointer_SetPosition(0, x, y); - LBackend_Hooks[3] = VirtualCursor_Display2D; + LBackend_Hooks[3] = VirtualCursor_Display2D; + Game.Draw2DHooks[3] = VirtualCursor_Display3D; /* TODO better dirty region tracking */ if (launcherMode) LBackend_Redraw();