DVX_GUI/src/libs/kpunch/game/gameBas.c
2026-09-29 17:03:51 -05:00

61 lines
2.3 KiB
C

// The MIT License (MIT)
//
// Copyright (C) 2026 Scott Duensing
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// gameBas.c -- DVX BASIC entry points of the game library
//
// Called through DECLARE LIBRARY (src/include/basic/game.bas), so the
// signatures use only BASIC-marshalable types: LONG in, True (-1) / False
// (0) out. Held keys are only reported while one of the calling
// program's windows has the focus, which needs the GUI context the BASIC
// runtime hands over with gameBasSetContext when it starts a program.
#include "game.h"
#define BAS_TRUE (-1)
#define BAS_FALSE 0
static AppContextT *sBasCtx = NULL;
// ============================================================
// Prototypes
// ============================================================
void gameBasSetContext(AppContextT *ctx);
int32_t GameKeyDown(int32_t rawCode);
void gameBasSetContext(AppContextT *ctx) {
sBasCtx = ctx;
}
// The focused window must belong to the program asking (currentAppId is
// the calling program while its BASIC code runs).
int32_t GameKeyDown(int32_t rawCode) {
if (!sBasCtx || sBasCtx->stack.focusedIdx < 0) {
return BAS_FALSE;
}
const WindowT *win = sBasCtx->stack.windows[sBasCtx->stack.focusedIdx];
return (win->appId == sBasCtx->currentAppId && gameKeyIsDown(win, rawCode)) ? BAS_TRUE : BAS_FALSE;
}