Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/desktop/backends/glfw2.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,20 @@ void platformExit(void) {
glfwTerminate();
}

static void platformSetCursor(int32_t cursorType) {
// GLFW2 only supports showing/hiding
if (cursorType == -1) {
glfwDisable(GLFW_MOUSE_CURSOR);
} else {
glfwEnable(GLFW_MOUSE_CURSOR);
}
}

void platformInitFunctions(Runner *runner) {
g_runner = runner;
runner->windowHasFocus = platformGetWindowFocus;
runner->setCursor = platformSetCursor;
runner->currentCursor = 0; // cr_default
#ifdef ENABLE_SW_RENDERER
if (gfx == SOFTWARE)
glfwSetWindowSizeCallback(resizeCallback);
Expand Down
31 changes: 31 additions & 0 deletions src/desktop/backends/glfw3.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,40 @@ void platformExit(void) {
glfwTerminate();
}

static void platformSetCursor(int32_t cursorType) {
if (cursorType == -1) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
return;
}
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);

int glfwShape;
switch (cursorType) {
case -3: glfwShape = GLFW_CROSSHAIR_CURSOR; break;
case -4: glfwShape = GLFW_IBEAM_CURSOR; break;
case -7: glfwShape = GLFW_VRESIZE_CURSOR; break;
case -9: glfwShape = GLFW_HRESIZE_CURSOR; break;
case -12: glfwShape = GLFW_HAND_CURSOR; break;
case -21: glfwShape = GLFW_HAND_CURSOR; break;
#if (GLFW_VERSION_MINOR >= 4)
case -22: glfwShape = GLFW_RESIZE_ALL_CURSOR; break;
case -8: glfwShape = GLFW_RESIZE_NWSE_CURSOR; break;
case -6: glfwShape = GLFW_RESIZE_NESW_CURSOR; break;
#endif
default: glfwShape = GLFW_ARROW_CURSOR; break;
}

GLFWcursor* cursor = glfwCreateStandardCursor(glfwShape);
if (cursor) {
glfwSetCursor(window, cursor);
}
}

void platformInitFunctions(Runner *runner) {
g_runner = runner;
runner->windowHasFocus = platformGetWindowFocus;
runner->setCursor = platformSetCursor;
runner->currentCursor = 0; // cr_default
#ifdef ENABLE_SW_RENDERER
if (gfx == SOFTWARE)
glfwSetWindowSizeCallback(window, resizeCallback);
Expand Down
7 changes: 7 additions & 0 deletions src/desktop/backends/sdl1.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,16 @@ void platformExit(void) {
SDL_Quit();
}

static void platformSetCursor(int32_t cursorType) {
// SDL1.2 only supports showing/hiding
SDL_ShowCursor(cursorType == -1 ? SDL_DISABLE : SDL_ENABLE);
}

void platformInitFunctions(Runner *runner) {
g_runner = runner;
runner->windowHasFocus = platformGetWindowFocus;
runner->setCursor = platformSetCursor;
runner->currentCursor = 0; // cr_default
}

#ifdef ENABLE_SW_RENDERER
Expand Down
31 changes: 31 additions & 0 deletions src/desktop/backends/sdl2.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,40 @@ void platformExit(void) {
SDL_Quit();
}

static void platformSetCursor(int32_t cursorType) {
if (cursorType == -1) {
SDL_ShowCursor(SDL_DISABLE);
return;
}
SDL_ShowCursor(SDL_ENABLE);

SDL_SystemCursor sdlCursor;
switch (cursorType) {
case -3: sdlCursor = SDL_SYSTEM_CURSOR_CROSSHAIR; break;
case -4: sdlCursor = SDL_SYSTEM_CURSOR_IBEAM; break;
case -6: sdlCursor = SDL_SYSTEM_CURSOR_SIZENESW; break;
case -7: sdlCursor = SDL_SYSTEM_CURSOR_SIZENS; break;
case -8: sdlCursor = SDL_SYSTEM_CURSOR_SIZENWSE; break;
case -9: sdlCursor = SDL_SYSTEM_CURSOR_SIZEWE; break;
case -11: sdlCursor = SDL_SYSTEM_CURSOR_WAIT; break;
case -12: sdlCursor = SDL_SYSTEM_CURSOR_SIZEALL; break;
case -19: sdlCursor = SDL_SYSTEM_CURSOR_WAITARROW; break;
case -21: sdlCursor = SDL_SYSTEM_CURSOR_HAND; break;
case -22: sdlCursor = SDL_SYSTEM_CURSOR_SIZEALL; break;
default: sdlCursor = SDL_SYSTEM_CURSOR_ARROW; break;
}

SDL_Cursor* cursor = SDL_CreateSystemCursor(sdlCursor);
if (cursor) {
SDL_SetCursor(cursor);
}
}

void platformInitFunctions(Runner *runner) {
g_runner = runner;
runner->windowHasFocus = platformGetWindowFocus;
runner->setCursor = platformSetCursor;
runner->currentCursor = 0; // cr_default
}

#ifdef ENABLE_SW_RENDERER
Expand Down
2 changes: 2 additions & 0 deletions src/runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ struct Runner {
bool (*getWindowSize)(int32_t* outW, int32_t* outH);
void (*setWindowSize)(int32_t width, int32_t height);
bool (*windowHasFocus)(void);
void (*setCursor)(int32_t cursorType);
int32_t currentCursor; // last value passed to window_set_cursor
TileLayerMapEntry* tileLayerMap; // stb_ds hashmap: depth -> tile layer state
RuntimeLayer* runtimeLayers; // stb_ds array, index-parallel to currentRoom->layers for parsed entries; dynamic entries appended
uint32_t nextLayerId; // counter for IDs of layers/elements created at runtime
Expand Down
20 changes: 20 additions & 0 deletions src/vm_builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -6416,6 +6416,24 @@ static RValue builtin_window_has_focus(VMContext* ctx, MAYBE_UNUSED RValue* args
return RValue_makeBool(true);
}

static RValue builtin_window_set_cursor(VMContext* ctx, RValue* args, int32_t argCount) {
if (argCount < 1) return RValue_makeUndefined();
Runner* runner = ctx->runner;
if (runner == nullptr) return RValue_makeUndefined();
int32_t cursorType = RValue_toInt32(args[0]);
runner->currentCursor = cursorType;
if (runner->setCursor != nullptr) {
runner->setCursor(cursorType);
}
return RValue_makeUndefined();
}

static RValue builtin_window_get_cursor(VMContext* ctx, MAYBE_UNUSED RValue* args, MAYBE_UNUSED int32_t argCount) {
Runner* runner = ctx->runner;
if (runner == nullptr) return RValue_makeReal(0);
return RValue_makeReal(runner->currentCursor);
}

// ===[ Game State Functions ]===
static RValue builtin_game_restart(VMContext* ctx, MAYBE_UNUSED RValue* args, MAYBE_UNUSED int32_t argCount) {
ctx->runner->pendingRoom = ROOM_RESTARTGAME;
Expand Down Expand Up @@ -13385,6 +13403,8 @@ void VMBuiltins_registerAll(VMContext* ctx) {
VM_registerBuiltin(ctx, "window_set_size", builtin_window_set_size);
VM_registerBuiltin(ctx, "window_center", builtin_window_center);
VM_registerBuiltin(ctx, "window_has_focus", builtin_window_has_focus);
VM_registerBuiltin(ctx, "window_set_cursor", builtin_window_set_cursor);
VM_registerBuiltin(ctx, "window_get_cursor", builtin_window_get_cursor);

// Game
VM_registerBuiltin(ctx, "game_restart", builtin_game_restart);
Expand Down
Loading