joeylib2/src/core/init.c

120 lines
2.7 KiB
C

// JoeyLib lifecycle: init, shutdown, error reporting.
//
// joeyInit stores configuration, allocates the library-owned screen
// surface, and asks the port HAL to set up the display mode.
// joeyShutdown tears those down in reverse order.
#include <stddef.h>
#include <string.h>
#include "joey/core.h"
#include "codegenArenaInternal.h"
#include "hal.h"
#include "surfaceInternal.h"
// 8 KB fits the largest typical sprite working set (~3-4 KB per
// 32x32 sprite at all opaque) and keeps malloc requests small enough
// for IIgs ORCA-C's small-memory-model heap to satisfy them.
#define DEFAULT_CODEGEN_BYTES (8u * 1024u)
// ----- Prototypes -----
static void clearError(void);
static void setError(const char *message);
// ----- Module state -----
static JoeyConfigT gConfig;
static bool gInitialized = false;
static const char *gLastError = NULL;
// ----- Internal helpers (alphabetical) -----
static void clearError(void) {
gLastError = NULL;
}
static void setError(const char *message) {
gLastError = message;
}
// ----- Public API (alphabetical) -----
bool joeyInit(const JoeyConfigT *config) {
clearError();
if (gInitialized) {
setError("joeyInit called while already initialized");
return false;
}
if (config == NULL) {
setError("joeyInit called with NULL config");
return false;
}
memcpy(&gConfig, config, sizeof(gConfig));
// halInit must run before stageAlloc: on IIgs the stage's pixel
// buffer comes from halStageAllocPixels, which depends on shadow /
// SHR setup that halInit performs.
if (!halInit(&gConfig)) {
const char *halMsg = halLastError();
setError(halMsg != NULL ? halMsg : "halInit failed");
return false;
}
if (!stageAlloc()) {
setError("failed to allocate stage surface");
halShutdown();
return false;
}
if (!codegenArenaInit(gConfig.codegenBytes != 0 ? gConfig.codegenBytes
: DEFAULT_CODEGEN_BYTES)) {
setError("failed to allocate codegen arena");
stageFree();
halShutdown();
return false;
}
halInputInit();
gInitialized = true;
return true;
}
const char *joeyLastError(void) {
return gLastError;
}
const char *joeyPlatformName(void) {
return JOEYLIB_PLATFORM_NAME;
}
void joeyShutdown(void) {
if (!gInitialized) {
return;
}
halInputShutdown();
codegenArenaShutdown();
stageFree();
halShutdown();
gInitialized = false;
clearError();
}
const char *joeyVersionString(void) {
return JOEYLIB_VERSION_STRING;
}
void joeyWaitVBL(void) {
halWaitVBL();
}