Nuke error.hpp
This commit is contained in:
@@ -48,7 +48,6 @@ add_subdirectory(asset)
|
|||||||
add_subdirectory(debug)
|
add_subdirectory(debug)
|
||||||
add_subdirectory(display)
|
add_subdirectory(display)
|
||||||
add_subdirectory(engine)
|
add_subdirectory(engine)
|
||||||
add_subdirectory(error)
|
|
||||||
add_subdirectory(input)
|
add_subdirectory(input)
|
||||||
add_subdirectory(locale)
|
add_subdirectory(locale)
|
||||||
add_subdirectory(rpg)
|
add_subdirectory(rpg)
|
||||||
|
|||||||
@@ -10,14 +10,16 @@
|
|||||||
#include "util/string.hpp"
|
#include "util/string.hpp"
|
||||||
#include "assert/assert.hpp"
|
#include "assert/assert.hpp"
|
||||||
#include "asset/assettype.h"
|
#include "asset/assettype.h"
|
||||||
#include "engine/engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
#include "debug/debug.hpp"
|
#include "debug/debug.hpp"
|
||||||
|
|
||||||
errorret_t assetInit(void) {
|
using namespace Dusk;
|
||||||
|
|
||||||
|
void assetInit(void) {
|
||||||
memoryZero(&ASSET, sizeof(asset_t));
|
memoryZero(&ASSET, sizeof(asset_t));
|
||||||
|
|
||||||
// Engine may have been provided the launch path
|
// Engine may have been provided the launch path
|
||||||
if(ENGINE.argc > 0) {
|
if(Engine::ENGINE.argc > 0) {
|
||||||
// This first arg is the executable, so on most platforms it is say
|
// This first arg is the executable, so on most platforms it is say
|
||||||
// "/path/file" or "C:\Path\file.exe". On PSP this would be something
|
// "/path/file" or "C:\Path\file.exe". On PSP this would be something
|
||||||
// like "ms0:/PSP/GAME/DUSK/EBOOT.PBP" or if we are debugging it is
|
// like "ms0:/PSP/GAME/DUSK/EBOOT.PBP" or if we are debugging it is
|
||||||
@@ -25,7 +27,7 @@ errorret_t assetInit(void) {
|
|||||||
|
|
||||||
// Get the directory of the executable
|
// Get the directory of the executable
|
||||||
char_t buffer[FILENAME_MAX];
|
char_t buffer[FILENAME_MAX];
|
||||||
stringCopy(buffer, ENGINE.argv[0], FILENAME_MAX);
|
stringCopy(buffer, Engine::ENGINE.argv[0], FILENAME_MAX);
|
||||||
size_t len = strlen(buffer);
|
size_t len = strlen(buffer);
|
||||||
|
|
||||||
// Normalize slashes
|
// Normalize slashes
|
||||||
@@ -69,20 +71,26 @@ errorret_t assetInit(void) {
|
|||||||
);
|
);
|
||||||
ASSET.pbpFile = fopen(pbpPath, "rb");
|
ASSET.pbpFile = fopen(pbpPath, "rb");
|
||||||
if(ASSET.pbpFile == NULL) {
|
if(ASSET.pbpFile == NULL) {
|
||||||
errorThrow("Failed to open PBP file: %s", pbpPath);
|
throw std::runtime_error(
|
||||||
|
"Failed to open PBP file: " + std::string(pbpPath)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get size of PBP file.
|
// Get size of PBP file.
|
||||||
if(fseek(ASSET.pbpFile, 0, SEEK_END) != 0) {
|
if(fseek(ASSET.pbpFile, 0, SEEK_END) != 0) {
|
||||||
fclose(ASSET.pbpFile);
|
fclose(ASSET.pbpFile);
|
||||||
errorThrow("Failed to seek to end of PBP file : %s", pbpPath);
|
throw std::runtime_error(
|
||||||
|
"Failed to seek to end of PBP file : " + std::string(pbpPath)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
size_t pbpSize = ftell(ASSET.pbpFile);
|
size_t pbpSize = ftell(ASSET.pbpFile);
|
||||||
|
|
||||||
// Rewind to start
|
// Rewind to start
|
||||||
if(fseek(ASSET.pbpFile, 0, SEEK_SET) != 0) {
|
if(fseek(ASSET.pbpFile, 0, SEEK_SET) != 0) {
|
||||||
fclose(ASSET.pbpFile);
|
fclose(ASSET.pbpFile);
|
||||||
errorThrow("Failed to seek to start of PBP file : %s", pbpPath);
|
throw std::runtime_error(
|
||||||
|
"Failed to seek to start of PBP file : " + std::string(pbpPath)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the PBP header
|
// Read the PBP header
|
||||||
@@ -94,7 +102,9 @@ errorret_t assetInit(void) {
|
|||||||
);
|
);
|
||||||
if(read != sizeof(assetpbp_t)) {
|
if(read != sizeof(assetpbp_t)) {
|
||||||
fclose(ASSET.pbpFile);
|
fclose(ASSET.pbpFile);
|
||||||
errorThrow("Failed to read PBP header", pbpPath);
|
throw std::runtime_error(
|
||||||
|
"Failed to read PBP header: " + std::string(pbpPath)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(memoryCompare(
|
if(memoryCompare(
|
||||||
@@ -103,13 +113,17 @@ errorret_t assetInit(void) {
|
|||||||
sizeof(ASSET_PBP_SIGNATURE)
|
sizeof(ASSET_PBP_SIGNATURE)
|
||||||
) != 0) {
|
) != 0) {
|
||||||
fclose(ASSET.pbpFile);
|
fclose(ASSET.pbpFile);
|
||||||
errorThrow("Invalid PBP signature in file: %s", pbpPath);
|
throw std::runtime_error(
|
||||||
|
"Invalid PBP signature in file: " + std::string(pbpPath)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we seek to the PSAR offset, we can read the WAD file from there
|
// If we seek to the PSAR offset, we can read the WAD file from there
|
||||||
if(fseek(ASSET.pbpFile, ASSET.pbpHeader.psarOffset, SEEK_SET) != 0) {
|
if(fseek(ASSET.pbpFile, ASSET.pbpHeader.psarOffset, SEEK_SET) != 0) {
|
||||||
fclose(ASSET.pbpFile);
|
fclose(ASSET.pbpFile);
|
||||||
errorThrow("Failed to seek to PSAR offset in PBP file: %s", pbpPath);
|
throw std::runtime_error(
|
||||||
|
"Failed to seek to PSAR offset in PBP file: " + std::string(pbpPath)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
zip_uint64_t zipPsarOffset = (zip_uint64_t)ASSET.pbpHeader.psarOffset;
|
zip_uint64_t zipPsarOffset = (zip_uint64_t)ASSET.pbpHeader.psarOffset;
|
||||||
@@ -125,7 +139,9 @@ errorret_t assetInit(void) {
|
|||||||
);
|
);
|
||||||
if(psarSource == NULL) {
|
if(psarSource == NULL) {
|
||||||
fclose(ASSET.pbpFile);
|
fclose(ASSET.pbpFile);
|
||||||
errorThrow("Failed to create zip source in PBP file: %s", pbpPath);
|
throw std::runtime_error(
|
||||||
|
"Failed to create zip source in PBP file: " + std::string(pbpPath)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
ASSET.zip = zip_open_from_source(
|
ASSET.zip = zip_open_from_source(
|
||||||
@@ -136,7 +152,9 @@ errorret_t assetInit(void) {
|
|||||||
if(ASSET.zip == NULL) {
|
if(ASSET.zip == NULL) {
|
||||||
zip_source_free(psarSource);
|
zip_source_free(psarSource);
|
||||||
fclose(ASSET.pbpFile);
|
fclose(ASSET.pbpFile);
|
||||||
errorThrow("Failed to open zip from PBP file: %s", pbpPath);
|
throw std::runtime_error(
|
||||||
|
"Failed to open zip from PBP file: " + std::string(pbpPath)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
@@ -162,9 +180,7 @@ errorret_t assetInit(void) {
|
|||||||
} while(*(++path) != NULL);
|
} while(*(++path) != NULL);
|
||||||
|
|
||||||
// Did we open the asset?
|
// Did we open the asset?
|
||||||
if(ASSET.zip == NULL) errorThrow("Failed to open asset file.");
|
if(ASSET.zip == NULL) throw std::runtime_error("Failed to open asset file.");
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool_t assetFileExists(const char_t *filename) {
|
bool_t assetFileExists(const char_t *filename) {
|
||||||
@@ -175,14 +191,16 @@ bool_t assetFileExists(const char_t *filename) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetLoad(const char_t *filename, void *output) {
|
void assetLoad(const char_t *filename, void *output) {
|
||||||
assertStrLenMax(filename, FILENAME_MAX, "Filename too long.");
|
assertStrLenMax(filename, FILENAME_MAX, "Filename too long.");
|
||||||
assertNotNull(output, "Output pointer cannot be NULL.");
|
assertNotNull(output, "Output pointer cannot be NULL.");
|
||||||
|
|
||||||
// Try to open the file
|
// Try to open the file
|
||||||
zip_file_t *file = zip_fopen(ASSET.zip, filename, 0);
|
zip_file_t *file = zip_fopen(ASSET.zip, filename, 0);
|
||||||
if(file == NULL) {
|
if(file == NULL) {
|
||||||
errorThrow("Failed to open asset file: %s", filename);
|
throw std::runtime_error(
|
||||||
|
"Failed to open asset file: " + std::string(filename)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the header.
|
// Read the header.
|
||||||
@@ -191,7 +209,9 @@ errorret_t assetLoad(const char_t *filename, void *output) {
|
|||||||
zip_int64_t bytesRead = zip_fread(file, &header, sizeof(assetheader_t));
|
zip_int64_t bytesRead = zip_fread(file, &header, sizeof(assetheader_t));
|
||||||
if(bytesRead != sizeof(assetheader_t)) {
|
if(bytesRead != sizeof(assetheader_t)) {
|
||||||
zip_fclose(file);
|
zip_fclose(file);
|
||||||
errorThrow("Failed to read asset header for: %s", filename);
|
throw std::runtime_error(
|
||||||
|
"Failed to read asset header for: " + std::string(filename)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the asset type based on the header
|
// Find the asset type based on the header
|
||||||
@@ -215,7 +235,9 @@ errorret_t assetLoad(const char_t *filename, void *output) {
|
|||||||
}
|
}
|
||||||
if(def == NULL) {
|
if(def == NULL) {
|
||||||
zip_fclose(file);
|
zip_fclose(file);
|
||||||
errorThrow("Unknown asset type for file: %s", filename);
|
throw std::runtime_error(
|
||||||
|
"Unknown asset type for file: " + std::string(filename)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We found the asset type, now load the asset data
|
// We found the asset type, now load the asset data
|
||||||
@@ -227,16 +249,24 @@ errorret_t assetLoad(const char_t *filename, void *output) {
|
|||||||
if(bytesRead == 0 || bytesRead > def->dataSize) {
|
if(bytesRead == 0 || bytesRead > def->dataSize) {
|
||||||
memoryFree(data);
|
memoryFree(data);
|
||||||
zip_fclose(file);
|
zip_fclose(file);
|
||||||
errorThrow("Failed to read asset data for file: %s", filename);
|
throw std::runtime_error(
|
||||||
|
"Failed to read entire asset data for file: " +
|
||||||
|
std::string(filename)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the file now we have the data
|
// Close the file now we have the data
|
||||||
zip_fclose(file);
|
zip_fclose(file);
|
||||||
|
|
||||||
// Pass to the asset type loader
|
// Pass to the asset type loader
|
||||||
errorret_t ret = def->entire(data, output);
|
try {
|
||||||
|
def->entire(data, output);
|
||||||
|
} catch(...) {
|
||||||
|
memoryFree(data);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
memoryFree(data);
|
memoryFree(data);
|
||||||
errorChain(ret);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,15 +276,13 @@ errorret_t assetLoad(const char_t *filename, void *output) {
|
|||||||
.zipFile = file,
|
.zipFile = file,
|
||||||
.output = output
|
.output = output
|
||||||
};
|
};
|
||||||
errorChain(def->custom(customData));
|
def->custom(customData);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
assertUnreachable("Unknown asset load strategy.");
|
assertUnreachable("Unknown asset load strategy.");
|
||||||
}
|
}
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void assetDispose(void) {
|
void assetDispose(void) {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.hpp"
|
|
||||||
#include "assettype.h"
|
#include "assettype.h"
|
||||||
|
|
||||||
#if ASSET_TYPE == wad
|
#if ASSET_TYPE == wad
|
||||||
@@ -67,7 +66,7 @@ static asset_t ASSET;
|
|||||||
/**
|
/**
|
||||||
* Initializes the asset system.
|
* Initializes the asset system.
|
||||||
*/
|
*/
|
||||||
errorret_t assetInit(void);
|
void assetInit(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if an asset file exists.
|
* Checks if an asset file exists.
|
||||||
@@ -84,7 +83,7 @@ bool_t assetFileExists(const char_t *filename);
|
|||||||
* @param output The output pointer to store the loaded asset data.
|
* @param output The output pointer to store the loaded asset data.
|
||||||
* @return An error code if the asset could not be loaded.
|
* @return An error code if the asset could not be loaded.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLoad(const char_t *filename, void *output);
|
void assetLoad(const char_t *filename, void *output);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disposes/cleans up the asset system.
|
* Disposes/cleans up the asset system.
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ typedef struct {
|
|||||||
const assetloadstrat_t loadStrategy;
|
const assetloadstrat_t loadStrategy;
|
||||||
const size_t dataSize;
|
const size_t dataSize;
|
||||||
union {
|
union {
|
||||||
errorret_t (*entire)(void *data, void *output);
|
void (*entire)(void *data, void *output);
|
||||||
errorret_t (*custom)(assetcustom_t custom);
|
void (*custom)(assetcustom_t custom);
|
||||||
};
|
};
|
||||||
} assettypedef_t;
|
} assettypedef_t;
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#include "assert/assert.hpp"
|
#include "assert/assert.hpp"
|
||||||
#include "display/texture.hpp"
|
#include "display/texture.hpp"
|
||||||
|
|
||||||
errorret_t assetAlphaImageLoad(void *data, void *output) {
|
void assetAlphaImageLoad(void *data, void *output) {
|
||||||
assertNotNull(data, "Data pointer cannot be NULL.");
|
assertNotNull(data, "Data pointer cannot be NULL.");
|
||||||
assertNotNull(output, "Output pointer cannot be NULL.");
|
assertNotNull(output, "Output pointer cannot be NULL.");
|
||||||
|
|
||||||
@@ -23,6 +23,4 @@ errorret_t assetAlphaImageLoad(void *data, void *output) {
|
|||||||
TEXTURE_FORMAT_ALPHA,
|
TEXTURE_FORMAT_ALPHA,
|
||||||
(texturedata_t){ .alpha = { .data = dataPtr->pixels } }
|
(texturedata_t){ .alpha = { .data = dataPtr->pixels } }
|
||||||
);
|
);
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.hpp"
|
#include "dusk.hpp"
|
||||||
|
|
||||||
#define ASSET_ALPHA_IMAGE_WIDTH_MAX 256
|
#define ASSET_ALPHA_IMAGE_WIDTH_MAX 256
|
||||||
#define ASSET_ALPHA_IMAGE_HEIGHT_MAX 256
|
#define ASSET_ALPHA_IMAGE_HEIGHT_MAX 256
|
||||||
@@ -27,6 +27,6 @@ typedef struct {
|
|||||||
* be of type ASSET_TYPE_ALPHA_IMAGE and must be loaded.
|
* be of type ASSET_TYPE_ALPHA_IMAGE and must be loaded.
|
||||||
*
|
*
|
||||||
* @param asset The asset to load the alpha image from.
|
* @param asset The asset to load the alpha image from.
|
||||||
* @return An error code.
|
* @throws std::runtime_error if loading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t assetAlphaImageLoad(void *data, void *output);
|
void assetAlphaImageLoad(void *data, void *output);
|
||||||
@@ -38,7 +38,7 @@ typedef struct {
|
|||||||
} assetchunkentityheader_t;
|
} assetchunkentityheader_t;
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
errorret_t assetChunkLoad(assetcustom_t custom) {
|
void assetChunkLoad(assetcustom_t custom) {
|
||||||
assertNotNull(custom.output, "Output pointer cannot be NULL");
|
assertNotNull(custom.output, "Output pointer cannot be NULL");
|
||||||
assertNotNull(custom.zipFile, "Zip file pointer cannot be NULL");
|
assertNotNull(custom.zipFile, "Zip file pointer cannot be NULL");
|
||||||
|
|
||||||
@@ -52,33 +52,33 @@ errorret_t assetChunkLoad(assetcustom_t custom) {
|
|||||||
);
|
);
|
||||||
if(bytesRead != sizeof(assetchunkheader_t)) {
|
if(bytesRead != sizeof(assetchunkheader_t)) {
|
||||||
zip_fclose(custom.zipFile);
|
zip_fclose(custom.zipFile);
|
||||||
errorThrow("Failed to read chunk asset header.");
|
throw std::runtime_error("Failed to read chunk asset header.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(header.tileCount != CHUNK_TILE_COUNT) {
|
if(header.tileCount != CHUNK_TILE_COUNT) {
|
||||||
zip_fclose(custom.zipFile);
|
zip_fclose(custom.zipFile);
|
||||||
errorThrow(
|
throw std::runtime_error(
|
||||||
"Chunk asset has invalid tile count: %d (expected %d).",
|
"Chunk asset has invalid tile count: " +
|
||||||
header.tileCount,
|
std::to_string(header.tileCount) +
|
||||||
CHUNK_TILE_COUNT
|
" (expected " + std::to_string(CHUNK_TILE_COUNT) + ")."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(header.modelCount > CHUNK_MESH_COUNT_MAX) {
|
if(header.modelCount > CHUNK_MESH_COUNT_MAX) {
|
||||||
zip_fclose(custom.zipFile);
|
zip_fclose(custom.zipFile);
|
||||||
errorThrow(
|
throw std::runtime_error(
|
||||||
"Chunk asset has too many models: %d (max %d).",
|
"Chunk asset has too many models: " +
|
||||||
header.modelCount,
|
std::to_string(header.modelCount) +
|
||||||
CHUNK_MESH_COUNT_MAX
|
" (max " + std::to_string(CHUNK_MESH_COUNT_MAX) + ")."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(header.entityCount > CHUNK_ENTITY_COUNT_MAX) {
|
if(header.entityCount > CHUNK_ENTITY_COUNT_MAX) {
|
||||||
zip_fclose(custom.zipFile);
|
zip_fclose(custom.zipFile);
|
||||||
errorThrow(
|
throw std::runtime_error(
|
||||||
"Chunk asset has too many entities: %d (max %d).",
|
"Chunk asset has too many entities: " +
|
||||||
header.entityCount,
|
std::to_string(header.entityCount) +
|
||||||
CHUNK_ENTITY_COUNT_MAX
|
" (max " + std::to_string(CHUNK_ENTITY_COUNT_MAX) + ")."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +92,7 @@ errorret_t assetChunkLoad(assetcustom_t custom) {
|
|||||||
);
|
);
|
||||||
if(bytesRead != sizeof(assetchunktiledata_t) * header.tileCount) {
|
if(bytesRead != sizeof(assetchunktiledata_t) * header.tileCount) {
|
||||||
zip_fclose(custom.zipFile);
|
zip_fclose(custom.zipFile);
|
||||||
errorThrow("Failed to read chunk tile data.");
|
throw std::runtime_error("Failed to read chunk tile data.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// For each model...
|
// For each model...
|
||||||
@@ -104,7 +104,7 @@ errorret_t assetChunkLoad(assetcustom_t custom) {
|
|||||||
);
|
);
|
||||||
if(bytesRead != sizeof(assetchunkmodelheader_t)) {
|
if(bytesRead != sizeof(assetchunkmodelheader_t)) {
|
||||||
zip_fclose(custom.zipFile);
|
zip_fclose(custom.zipFile);
|
||||||
errorThrow("Failed to read chunk model header.");
|
throw std::runtime_error("Failed to read chunk model header.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(
|
if(
|
||||||
@@ -112,7 +112,7 @@ errorret_t assetChunkLoad(assetcustom_t custom) {
|
|||||||
CHUNK_VERTEX_COUNT_MAX
|
CHUNK_VERTEX_COUNT_MAX
|
||||||
) {
|
) {
|
||||||
zip_fclose(custom.zipFile);
|
zip_fclose(custom.zipFile);
|
||||||
errorThrow("Chunk model vertex count exceeds maximum.");
|
throw std::runtime_error("Chunk model vertex count exceeds maximum.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read vertex data.
|
// Read vertex data.
|
||||||
@@ -123,7 +123,7 @@ errorret_t assetChunkLoad(assetcustom_t custom) {
|
|||||||
);
|
);
|
||||||
if(bytesRead != sizeof(meshvertex_t) * modelHeader.vertexCount) {
|
if(bytesRead != sizeof(meshvertex_t) * modelHeader.vertexCount) {
|
||||||
zip_fclose(custom.zipFile);
|
zip_fclose(custom.zipFile);
|
||||||
errorThrow("Failed to read chunk model vertex data.");
|
throw std::runtime_error("Failed to read chunk model vertex data.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init the mesh
|
// Init the mesh
|
||||||
@@ -150,13 +150,13 @@ errorret_t assetChunkLoad(assetcustom_t custom) {
|
|||||||
);
|
);
|
||||||
if(bytesRead != sizeof(assetchunkentityheader_t)) {
|
if(bytesRead != sizeof(assetchunkentityheader_t)) {
|
||||||
zip_fclose(custom.zipFile);
|
zip_fclose(custom.zipFile);
|
||||||
errorThrow("Failed to read chunk entity header.");
|
throw std::runtime_error("Failed to read chunk entity header.");
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t entityIndex = entityGetAvailable();
|
uint8_t entityIndex = entityGetAvailable();
|
||||||
if(entityIndex == 0xFF) {
|
if(entityIndex == 0xFF) {
|
||||||
zip_fclose(custom.zipFile);
|
zip_fclose(custom.zipFile);
|
||||||
errorThrow("No available entity slots.");
|
throw std::runtime_error("No available entity slots.");
|
||||||
}
|
}
|
||||||
|
|
||||||
entity_t *entity = &ENTITIES[entityIndex];
|
entity_t *entity = &ENTITIES[entityIndex];
|
||||||
@@ -173,6 +173,4 @@ errorret_t assetChunkLoad(assetcustom_t custom) {
|
|||||||
|
|
||||||
chunk->entities[i] = entityIndex;
|
chunk->entities[i] = entityIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.hpp"
|
|
||||||
#include "rpg/world/chunk.hpp"
|
#include "rpg/world/chunk.hpp"
|
||||||
|
|
||||||
typedef struct assetcustom_s assetcustom_t;
|
typedef struct assetcustom_s assetcustom_t;
|
||||||
@@ -15,6 +14,6 @@ typedef struct assetcustom_s assetcustom_t;
|
|||||||
* Handles loading of chunk data from a chunk asset file.
|
* Handles loading of chunk data from a chunk asset file.
|
||||||
*
|
*
|
||||||
* @param custom The custom asset loading parameters.
|
* @param custom The custom asset loading parameters.
|
||||||
* @return An error code.
|
* @throws std::runtime_error if loading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t assetChunkLoad(assetcustom_t custom);
|
void assetChunkLoad(assetcustom_t custom);
|
||||||
@@ -9,17 +9,15 @@
|
|||||||
#include "assert/assert.hpp"
|
#include "assert/assert.hpp"
|
||||||
#include "locale/localemanager.hpp"
|
#include "locale/localemanager.hpp"
|
||||||
|
|
||||||
errorret_t assetLanguageHandler(assetcustom_t custom) {
|
void assetLanguageHandler(assetcustom_t custom) {
|
||||||
assertNotNull(custom.zipFile, "Custom asset zip file cannot be NULL");
|
assertNotNull(custom.zipFile, "Custom asset zip file cannot be NULL");
|
||||||
assertNotNull(custom.output, "Custom asset output cannot be NULL");
|
assertNotNull(custom.output, "Custom asset output cannot be NULL");
|
||||||
|
|
||||||
assetlanguage_t *lang = (assetlanguage_t *)custom.output;
|
assetlanguage_t *lang = (assetlanguage_t *)custom.output;
|
||||||
errorChain(assetLanguageInit(lang, custom.zipFile));
|
assetLanguageInit(lang, custom.zipFile);
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetLanguageInit(
|
void assetLanguageInit(
|
||||||
assetlanguage_t *lang,
|
assetlanguage_t *lang,
|
||||||
zip_file_t *zipFile
|
zip_file_t *zipFile
|
||||||
) {
|
) {
|
||||||
@@ -40,19 +38,17 @@ errorret_t assetLanguageInit(
|
|||||||
);
|
);
|
||||||
if(bytesRead != sizeof(assetlanguageheader_t)) {
|
if(bytesRead != sizeof(assetlanguageheader_t)) {
|
||||||
zip_fclose(lang->zip);
|
zip_fclose(lang->zip);
|
||||||
errorThrow("Failed to read language asset header.");
|
throw std::runtime_error("Failed to read language asset header.");
|
||||||
}
|
}
|
||||||
|
|
||||||
lang->chunksOffset = zip_ftell(lang->zip);
|
lang->chunksOffset = zip_ftell(lang->zip);
|
||||||
if(lang->chunksOffset <= 0) {
|
if(lang->chunksOffset <= 0) {
|
||||||
zip_fclose(lang->zip);
|
zip_fclose(lang->zip);
|
||||||
errorThrow("Failed to get language asset chunks offset.");
|
throw std::runtime_error("Failed to get language asset chunks offset.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
errorOk();
|
void assetLanguageRead(
|
||||||
}
|
|
||||||
|
|
||||||
errorret_t assetLanguageRead(
|
|
||||||
assetlanguage_t *lang,
|
assetlanguage_t *lang,
|
||||||
const uint32_t key,
|
const uint32_t key,
|
||||||
char_t *buffer,
|
char_t *buffer,
|
||||||
@@ -70,7 +66,7 @@ errorret_t assetLanguageRead(
|
|||||||
if(buffer == NULL) {
|
if(buffer == NULL) {
|
||||||
assertNotNull(outLength, "Output length pointer cannot be NULL.");
|
assertNotNull(outLength, "Output length pointer cannot be NULL.");
|
||||||
*outLength = str->length;
|
*outLength = str->length;
|
||||||
errorOk();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure buffer is large enough
|
// Ensure buffer is large enough
|
||||||
@@ -87,19 +83,19 @@ errorret_t assetLanguageRead(
|
|||||||
// Seek
|
// Seek
|
||||||
zip_int64_t result = zip_fseek(lang->zip, seekTo, SEEK_SET);
|
zip_int64_t result = zip_fseek(lang->zip, seekTo, SEEK_SET);
|
||||||
if(result != 0) {
|
if(result != 0) {
|
||||||
errorThrow("Failed to seek to language string in asset.");
|
throw std::runtime_error("Failed to seek to language string in asset.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read
|
// Read
|
||||||
zip_int64_t readTest = zip_fread(lang->zip, buffer, str->length);
|
zip_int64_t readTest = zip_fread(lang->zip, buffer, str->length);
|
||||||
if(readTest != str->length) {
|
if(readTest != str->length) {
|
||||||
errorThrow("Failed to read test string from language asset.");
|
throw std::runtime_error("Failed to read test string from language asset.");
|
||||||
}
|
}
|
||||||
buffer[str->length] = '\0';
|
buffer[str->length] = '\0';
|
||||||
|
|
||||||
// Set str length if requested
|
// Set str length if requested
|
||||||
if(outLength != NULL) *outLength = str->length;
|
if(outLength != NULL) *outLength = str->length;
|
||||||
errorOk();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void assetLanguageDispose(assetlanguage_t *lang) {
|
void assetLanguageDispose(assetlanguage_t *lang) {
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "locale/language/keys.hpp"
|
#include "locale/language/keys.hpp"
|
||||||
#include "error/error.hpp"
|
|
||||||
#include "duskdefs.hpp"
|
#include "duskdefs.hpp"
|
||||||
#include <zip.h>
|
#include <zip.h>
|
||||||
|
|
||||||
@@ -47,18 +46,18 @@ typedef struct assetcustom_s assetcustom_t;
|
|||||||
* Receiving function from the asset manager to handle language assets.
|
* Receiving function from the asset manager to handle language assets.
|
||||||
*
|
*
|
||||||
* @param custom Custom asset loading data.
|
* @param custom Custom asset loading data.
|
||||||
* @return Error code.
|
* @throws std::runtime_error if loading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLanguageHandler(assetcustom_t custom);
|
void assetLanguageHandler(assetcustom_t custom);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a language asset and loads the header data into memory.
|
* Initializes a language asset and loads the header data into memory.
|
||||||
*
|
*
|
||||||
* @param lang Language asset to initialize.
|
* @param lang Language asset to initialize.
|
||||||
* @param zipFile Zip file handle for the language asset.
|
* @param zipFile Zip file handle for the language asset.
|
||||||
* @return Error code.
|
* @throws std::runtime_error if loading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLanguageInit(assetlanguage_t *lang, zip_file_t *zipFile);
|
void assetLanguageInit(assetlanguage_t *lang, zip_file_t *zipFile);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a string from the language asset into the provided buffer.
|
* Reads a string from the language asset into the provided buffer.
|
||||||
@@ -68,9 +67,9 @@ errorret_t assetLanguageInit(assetlanguage_t *lang, zip_file_t *zipFile);
|
|||||||
* @param buffer Buffer to read the string into.
|
* @param buffer Buffer to read the string into.
|
||||||
* @param bufferSize Size of the provided buffer.
|
* @param bufferSize Size of the provided buffer.
|
||||||
* @param outLength Pointer to store the length of the string read.
|
* @param outLength Pointer to store the length of the string read.
|
||||||
* @return Error code.
|
* @throws std::runtime_error if loading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLanguageRead(
|
void assetLanguageRead(
|
||||||
assetlanguage_t *lang,
|
assetlanguage_t *lang,
|
||||||
const uint32_t key,
|
const uint32_t key,
|
||||||
char_t *buffer,
|
char_t *buffer,
|
||||||
@@ -82,6 +81,6 @@ errorret_t assetLanguageRead(
|
|||||||
* Disposes of language asset resources.
|
* Disposes of language asset resources.
|
||||||
*
|
*
|
||||||
* @param custom Custom asset loading data.
|
* @param custom Custom asset loading data.
|
||||||
* @return Error code.
|
* @throws std::runtime_error if loading fails.
|
||||||
*/
|
*/
|
||||||
void assetLanguageDispose(assetlanguage_t *lang);
|
void assetLanguageDispose(assetlanguage_t *lang);
|
||||||
@@ -9,11 +9,9 @@
|
|||||||
#include "assert/assert.hpp"
|
#include "assert/assert.hpp"
|
||||||
#include "util/memory.hpp"
|
#include "util/memory.hpp"
|
||||||
|
|
||||||
errorret_t assetMapLoad(void *data, void *output) {
|
void assetMapLoad(void *data, void *output) {
|
||||||
assertNotNull(data, "Data cannot be NULL");
|
assertNotNull(data, "Data cannot be NULL");
|
||||||
assertNotNull(output, "Output cannot be NULL");
|
assertNotNull(output, "Output cannot be NULL");
|
||||||
|
|
||||||
assertUnreachable("map not finished");
|
assertUnreachable("map not finished");
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.hpp"
|
|
||||||
#include "rpg/world/map.hpp"
|
#include "rpg/world/map.hpp"
|
||||||
#include "display/mesh/mesh.hpp"
|
#include "display/mesh/mesh.hpp"
|
||||||
|
|
||||||
@@ -15,6 +14,6 @@
|
|||||||
*
|
*
|
||||||
* @param data Pointer to the raw assetmap_t data.
|
* @param data Pointer to the raw assetmap_t data.
|
||||||
* @param output Pointer to the map_t to load the map into.
|
* @param output Pointer to the map_t to load the map into.
|
||||||
* @return An error code.
|
* @throws std::runtime_error if loading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t assetMapLoad(void *data, void *output);
|
void assetMapLoad(void *data, void *output);
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
#include "assert/assert.hpp"
|
#include "assert/assert.hpp"
|
||||||
#include "display/texture.hpp"
|
#include "display/texture.hpp"
|
||||||
|
|
||||||
errorret_t assetPaletteImageLoad(void *data, void *output) {
|
void assetPaletteImageLoad(void *data, void *output) {
|
||||||
assertNotNull(data, "Data pointer cannot be NULL.");
|
assertNotNull(data, "Data pointer cannot be NULL.");
|
||||||
assertNotNull(output, "Output pointer cannot be NULL.");
|
assertNotNull(output, "Output pointer cannot be NULL.");
|
||||||
|
|
||||||
@@ -28,6 +28,4 @@ errorret_t assetPaletteImageLoad(void *data, void *output) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.hpp"
|
#include "dusk.hpp"
|
||||||
|
|
||||||
#define ASSET_PALETTE_IMAGE_WIDTH_MAX 128
|
#define ASSET_PALETTE_IMAGE_WIDTH_MAX 128
|
||||||
#define ASSET_PALETTE_IMAGE_HEIGHT_MAX 128
|
#define ASSET_PALETTE_IMAGE_HEIGHT_MAX 128
|
||||||
@@ -29,6 +29,6 @@ typedef struct {
|
|||||||
*
|
*
|
||||||
* @param data Pointer to the raw assetpaletteimage_t data.
|
* @param data Pointer to the raw assetpaletteimage_t data.
|
||||||
* @param output Pointer to the texture_t to load the image into.
|
* @param output Pointer to the texture_t to load the image into.
|
||||||
* @return An error code.
|
* @throws std::runtime_error if loading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t assetPaletteImageLoad(void *data, void *output);
|
void assetPaletteImageLoad(void *data, void *output);
|
||||||
@@ -8,17 +8,15 @@
|
|||||||
#include "asset/asset.hpp"
|
#include "asset/asset.hpp"
|
||||||
#include "assert/assert.hpp"
|
#include "assert/assert.hpp"
|
||||||
|
|
||||||
errorret_t assetScriptHandler(assetcustom_t custom) {
|
void assetScriptHandler(assetcustom_t custom) {
|
||||||
assertNotNull(custom.zipFile, "Custom asset zip file cannot be NULL");
|
assertNotNull(custom.zipFile, "Custom asset zip file cannot be NULL");
|
||||||
assertNotNull(custom.output, "Custom asset output cannot be NULL");
|
assertNotNull(custom.output, "Custom asset output cannot be NULL");
|
||||||
|
|
||||||
assetscript_t *script = (assetscript_t *)custom.output;
|
assetscript_t *script = (assetscript_t *)custom.output;
|
||||||
errorChain(assetScriptInit(script, custom.zipFile));
|
assetScriptInit(script, custom.zipFile);
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetScriptInit(
|
void assetScriptInit(
|
||||||
assetscript_t *script,
|
assetscript_t *script,
|
||||||
zip_file_t *zipFile
|
zip_file_t *zipFile
|
||||||
) {
|
) {
|
||||||
@@ -27,8 +25,6 @@ errorret_t assetScriptInit(
|
|||||||
|
|
||||||
// We now own the zip file handle.
|
// We now own the zip file handle.
|
||||||
script->zip = zipFile;
|
script->zip = zipFile;
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char_t * assetScriptReader(lua_State* lState, void* data, size_t* size) {
|
const char_t * assetScriptReader(lua_State* lState, void* data, size_t* size) {
|
||||||
@@ -46,13 +42,11 @@ const char_t * assetScriptReader(lua_State* lState, void* data, size_t* size) {
|
|||||||
return script->buffer;
|
return script->buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetScriptDispose(assetscript_t *script) {
|
void assetScriptDispose(assetscript_t *script) {
|
||||||
assertNotNull(script, "Script asset cannot be NULL");
|
assertNotNull(script, "Script asset cannot be NULL");
|
||||||
|
|
||||||
if(script->zip != NULL) {
|
if(script->zip != NULL) {
|
||||||
zip_fclose(script->zip);
|
zip_fclose(script->zip);
|
||||||
script->zip = NULL;
|
script->zip = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.hpp"
|
#include "dusk.hpp"
|
||||||
#include "duskdefs.hpp"
|
#include "duskdefs.hpp"
|
||||||
#include <zip.h>
|
#include <zip.h>
|
||||||
#include "script/scriptcontext.hpp"
|
#include "script/scriptcontext.hpp"
|
||||||
@@ -24,18 +24,18 @@ typedef struct assetcustom_s assetcustom_t;
|
|||||||
* Receiving function from the asset manager to handle script assets.
|
* Receiving function from the asset manager to handle script assets.
|
||||||
*
|
*
|
||||||
* @param custom Custom asset loading data.
|
* @param custom Custom asset loading data.
|
||||||
* @return Error code.
|
* @throws std::runtime_error on failure.
|
||||||
*/
|
*/
|
||||||
errorret_t assetScriptHandler(assetcustom_t custom);
|
void assetScriptHandler(assetcustom_t custom);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a script asset.
|
* Initializes a script asset.
|
||||||
*
|
*
|
||||||
* @param script Script asset to initialize.
|
* @param script Script asset to initialize.
|
||||||
* @param zipFile Zip file handle for the script asset.
|
* @param zipFile Zip file handle for the script asset.
|
||||||
* @return Error code.
|
* @throws std::runtime_error on failure.
|
||||||
*/
|
*/
|
||||||
errorret_t assetScriptInit(assetscript_t *script, zip_file_t *zipFile);
|
void assetScriptInit(assetscript_t *script, zip_file_t *zipFile);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reader function for Lua to read script data from the asset.
|
* Reader function for Lua to read script data from the asset.
|
||||||
@@ -51,7 +51,7 @@ const char_t * assetScriptReader(lua_State* L, void* data, size_t* size);
|
|||||||
* Disposes of a script asset, freeing any allocated resources.
|
* Disposes of a script asset, freeing any allocated resources.
|
||||||
*
|
*
|
||||||
* @param script Script asset to dispose of.
|
* @param script Script asset to dispose of.
|
||||||
* @return Error code.
|
* @throws std::runtime_error on failure.
|
||||||
*/
|
*/
|
||||||
errorret_t assetScriptDispose(assetscript_t *script);
|
void assetScriptDispose(assetscript_t *script);
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "display/display.hpp"
|
#include "display/display.hpp"
|
||||||
#include "engine/engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
#include "display/framebuffer.hpp"
|
#include "display/framebuffer.hpp"
|
||||||
#include "scene/scenemanager.hpp"
|
#include "scene/scenemanager.hpp"
|
||||||
#include "display/spritebatch.hpp"
|
#include "display/spritebatch.hpp"
|
||||||
@@ -15,16 +15,20 @@
|
|||||||
#include "ui/ui.hpp"
|
#include "ui/ui.hpp"
|
||||||
#include "debug/debug.hpp"
|
#include "debug/debug.hpp"
|
||||||
|
|
||||||
|
using namespace Dusk;
|
||||||
|
|
||||||
display_t DISPLAY;
|
display_t DISPLAY;
|
||||||
|
|
||||||
errorret_t displayInit(void) {
|
void displayInit(void) {
|
||||||
#if DISPLAY_SDL2
|
#if DISPLAY_SDL2
|
||||||
uint32_t flags = SDL_INIT_VIDEO;
|
uint32_t flags = SDL_INIT_VIDEO;
|
||||||
#if INPUT_GAMEPAD == 1
|
#if INPUT_GAMEPAD == 1
|
||||||
flags |= SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK;
|
flags |= SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK;
|
||||||
#endif
|
#endif
|
||||||
if(SDL_Init(flags) != 0) {
|
if(SDL_Init(flags) != 0) {
|
||||||
errorThrow("SDL Failed to Initialize: %s", SDL_GetError());
|
throw std::runtime_error(
|
||||||
|
"SDL Failed to Initialize " + std::string(SDL_GetError())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set OpenGL attributes (Needs to be done now or later?)
|
// Set OpenGL attributes (Needs to be done now or later?)
|
||||||
@@ -41,13 +45,17 @@ errorret_t displayInit(void) {
|
|||||||
SDL_WINDOW_OPENGL
|
SDL_WINDOW_OPENGL
|
||||||
);
|
);
|
||||||
if(!DISPLAY.window) {
|
if(!DISPLAY.window) {
|
||||||
errorThrow("SDL_CreateWindow failed: %s", SDL_GetError());
|
throw std::runtime_error(
|
||||||
|
"SDL_CreateWindow failed: " + std::string(SDL_GetError())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create OpenGL context
|
// Create OpenGL context
|
||||||
DISPLAY.glContext = SDL_GL_CreateContext(DISPLAY.window);
|
DISPLAY.glContext = SDL_GL_CreateContext(DISPLAY.window);
|
||||||
if(!DISPLAY.glContext) {
|
if(!DISPLAY.glContext) {
|
||||||
errorThrow("SDL_GL_CreateContext failed: %s", SDL_GetError());
|
throw std::runtime_error(
|
||||||
|
"SDL_GL_CreateContext failed: " + std::string(SDL_GetError())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_GL_SetSwapInterval(1);
|
SDL_GL_SetSwapInterval(1);
|
||||||
@@ -74,24 +82,22 @@ errorret_t displayInit(void) {
|
|||||||
frameBufferInitBackbuffer();
|
frameBufferInitBackbuffer();
|
||||||
spriteBatchInit();
|
spriteBatchInit();
|
||||||
screenInit();
|
screenInit();
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t displayUpdate(void) {
|
void displayUpdate(void) {
|
||||||
#if DISPLAY_SDL2
|
#if DISPLAY_SDL2
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while(SDL_PollEvent(&event)) {
|
while(SDL_PollEvent(&event)) {
|
||||||
switch(event.type) {
|
switch(event.type) {
|
||||||
case SDL_QUIT: {
|
case SDL_QUIT: {
|
||||||
ENGINE.running = false;
|
Engine::ENGINE.exit();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case SDL_WINDOWEVENT: {
|
case SDL_WINDOWEVENT: {
|
||||||
switch(event.window.event) {
|
switch(event.window.event) {
|
||||||
case SDL_WINDOWEVENT_CLOSE: {
|
case SDL_WINDOWEVENT_CLOSE: {
|
||||||
ENGINE.running = false;
|
Engine::ENGINE.exit();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,12 +142,9 @@ errorret_t displayUpdate(void) {
|
|||||||
while((err = glGetError()) != GL_NO_ERROR) {
|
while((err = glGetError()) != GL_NO_ERROR) {
|
||||||
debugPrint("GL Error: %d\n", err);
|
debugPrint("GL Error: %d\n", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For now, we just return an OK error.
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t displayDispose(void) {
|
void displayDispose(void) {
|
||||||
spriteBatchDispose();
|
spriteBatchDispose();
|
||||||
screenDispose();
|
screenDispose();
|
||||||
|
|
||||||
@@ -156,7 +159,4 @@ errorret_t displayDispose(void) {
|
|||||||
}
|
}
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// For now, we just return an OK error.
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "displaydefs.hpp"
|
#include "displaydefs.hpp"
|
||||||
#include "error/error.hpp"
|
|
||||||
#include "display/camera.hpp"
|
#include "display/camera.hpp"
|
||||||
#include "display/framebuffer.hpp"
|
#include "display/framebuffer.hpp"
|
||||||
|
|
||||||
@@ -23,14 +22,14 @@ extern display_t DISPLAY;
|
|||||||
/**
|
/**
|
||||||
* Initializes the display system.
|
* Initializes the display system.
|
||||||
*/
|
*/
|
||||||
errorret_t displayInit(void);
|
void displayInit(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tells the display system to actually draw the frame.
|
* Tells the display system to actually draw the frame.
|
||||||
*/
|
*/
|
||||||
errorret_t displayUpdate(void);
|
void displayUpdate(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disposes of the display system.
|
* Disposes of the display system.
|
||||||
*/
|
*/
|
||||||
errorret_t displayDispose(void);
|
void displayDispose(void);
|
||||||
@@ -36,3 +36,10 @@ extern "C" {
|
|||||||
typedef float float_t;
|
typedef float float_t;
|
||||||
typedef char char_t;
|
typedef char char_t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
@@ -6,5 +6,5 @@
|
|||||||
# Sources
|
# Sources
|
||||||
target_sources(${DUSK_TARGET_NAME}
|
target_sources(${DUSK_TARGET_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
engine.cpp
|
Engine.cpp
|
||||||
)
|
)
|
||||||
83
src/engine/Engine.cpp
Normal file
83
src/engine/Engine.cpp
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Engine.hpp"
|
||||||
|
#include "util/memory.hpp"
|
||||||
|
#include "time/time.hpp"
|
||||||
|
#include "input/input.hpp"
|
||||||
|
#include "locale/localemanager.hpp"
|
||||||
|
#include "display/display.hpp"
|
||||||
|
#include "scene/scenemanager.hpp"
|
||||||
|
#include "asset/asset.hpp"
|
||||||
|
#include "ui/ui.hpp"
|
||||||
|
#include "rpg/rpg.hpp"
|
||||||
|
#include "script/scriptmanager.hpp"
|
||||||
|
#include "debug/debug.hpp"
|
||||||
|
#include "script/scriptcontext.hpp"
|
||||||
|
|
||||||
|
using namespace Dusk;
|
||||||
|
|
||||||
|
Engine Engine::ENGINE;
|
||||||
|
|
||||||
|
Engine::Engine() :
|
||||||
|
running(false),
|
||||||
|
argc(0),
|
||||||
|
argv(nullptr)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::init(const int32_t argc, const char_t **argv) {
|
||||||
|
this->running = true;
|
||||||
|
this->argc = argc;
|
||||||
|
this->argv = argv;
|
||||||
|
|
||||||
|
// Init systems. Order is important.
|
||||||
|
timeInit();
|
||||||
|
inputInit();
|
||||||
|
assetInit();
|
||||||
|
localeManagerInit();
|
||||||
|
scriptManagerInit();
|
||||||
|
displayInit();
|
||||||
|
uiInit();
|
||||||
|
rpgInit();
|
||||||
|
sceneManagerInit();
|
||||||
|
|
||||||
|
// Run the initial script.
|
||||||
|
scriptcontext_t testCtx;
|
||||||
|
scriptContextInit(&testCtx);
|
||||||
|
scriptContextExecFile(&testCtx, "script/test.dsf");
|
||||||
|
scriptContextDispose(&testCtx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::update() {
|
||||||
|
timeUpdate();
|
||||||
|
inputUpdate();
|
||||||
|
|
||||||
|
rpgUpdate();
|
||||||
|
uiUpdate();
|
||||||
|
sceneManagerUpdate();
|
||||||
|
displayUpdate();
|
||||||
|
|
||||||
|
if(inputPressed(INPUT_ACTION_RAGEQUIT)) this->running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::dispose() {
|
||||||
|
localeManagerDispose();
|
||||||
|
sceneManagerDispose();
|
||||||
|
rpgDispose();
|
||||||
|
uiDispose();
|
||||||
|
displayDispose();
|
||||||
|
assetDispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::exit() {
|
||||||
|
this->running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t Engine::isRunning() {
|
||||||
|
return this->running;
|
||||||
|
}
|
||||||
56
src/engine/Engine.hpp
Normal file
56
src/engine/Engine.hpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/display.hpp"// Important to be included first.
|
||||||
|
|
||||||
|
namespace Dusk {
|
||||||
|
struct Engine {
|
||||||
|
private:
|
||||||
|
bool running;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static Engine ENGINE;
|
||||||
|
int32_t argc;
|
||||||
|
const char_t **argv;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the engine.
|
||||||
|
*/
|
||||||
|
Engine();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the engine.
|
||||||
|
*
|
||||||
|
* @param argc The argument count from main().
|
||||||
|
* @param argv The argument vector from main().
|
||||||
|
*/
|
||||||
|
void init(const int32_t argc, const char_t **argv);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the engine.
|
||||||
|
*/
|
||||||
|
void update();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes of the engine.
|
||||||
|
*/
|
||||||
|
void dispose();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requests the engine to exit.
|
||||||
|
*/
|
||||||
|
void exit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the engine is exiting or running.
|
||||||
|
*
|
||||||
|
* @return True if the engine is running, false otherwise.
|
||||||
|
*/
|
||||||
|
bool_t isRunning();
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "engine.hpp"
|
|
||||||
#include "util/memory.hpp"
|
|
||||||
#include "time/time.hpp"
|
|
||||||
#include "input/input.hpp"
|
|
||||||
#include "locale/localemanager.hpp"
|
|
||||||
#include "display/display.hpp"
|
|
||||||
#include "scene/scenemanager.hpp"
|
|
||||||
#include "asset/asset.hpp"
|
|
||||||
#include "ui/ui.hpp"
|
|
||||||
#include "rpg/rpg.hpp"
|
|
||||||
#include "script/scriptmanager.hpp"
|
|
||||||
#include "debug/debug.hpp"
|
|
||||||
|
|
||||||
#include "script/scriptcontext.hpp"
|
|
||||||
|
|
||||||
engine_t ENGINE;
|
|
||||||
|
|
||||||
errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
|
||||||
memoryZero(&ENGINE, sizeof(engine_t));
|
|
||||||
ENGINE.running = true;
|
|
||||||
ENGINE.argc = argc;
|
|
||||||
ENGINE.argv = argv;
|
|
||||||
|
|
||||||
|
|
||||||
// Init systems. Order is important.
|
|
||||||
timeInit();
|
|
||||||
inputInit();
|
|
||||||
errorChain(assetInit());
|
|
||||||
errorChain(localeManagerInit());
|
|
||||||
errorChain(scriptManagerInit());
|
|
||||||
errorChain(displayInit());
|
|
||||||
errorChain(uiInit());
|
|
||||||
errorChain(rpgInit());
|
|
||||||
errorChain(sceneManagerInit());
|
|
||||||
|
|
||||||
// Run the initial script.
|
|
||||||
|
|
||||||
|
|
||||||
scriptcontext_t testCtx;
|
|
||||||
errorChain(scriptContextInit(&testCtx));
|
|
||||||
errorChain(scriptContextExecFile(&testCtx, "script/test.dsf"));
|
|
||||||
scriptContextDispose(&testCtx);
|
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
errorret_t engineUpdate(void) {
|
|
||||||
timeUpdate();
|
|
||||||
inputUpdate();
|
|
||||||
|
|
||||||
errorChain(rpgUpdate());
|
|
||||||
uiUpdate();
|
|
||||||
sceneManagerUpdate();
|
|
||||||
errorChain(displayUpdate());
|
|
||||||
|
|
||||||
if(inputPressed(INPUT_ACTION_RAGEQUIT)) ENGINE.running = false;
|
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
void engineExit(void) {
|
|
||||||
ENGINE.running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
errorret_t engineDispose(void) {
|
|
||||||
localeManagerDispose();
|
|
||||||
sceneManagerDispose();
|
|
||||||
rpgDispose();
|
|
||||||
uiDispose();
|
|
||||||
errorChain(displayDispose());
|
|
||||||
assetDispose();
|
|
||||||
errorOk();
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "display/display.hpp"// Important to be included first.
|
|
||||||
#include "error/error.hpp"
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
bool_t running;
|
|
||||||
int32_t argc;
|
|
||||||
const char_t **argv;
|
|
||||||
} engine_t;
|
|
||||||
|
|
||||||
extern engine_t ENGINE;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the engine.
|
|
||||||
*
|
|
||||||
* @param argc The argument count from main().
|
|
||||||
* @param argv The argument vector from main().
|
|
||||||
*/
|
|
||||||
errorret_t engineInit(const int32_t argc, const char_t **argv);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the engine.
|
|
||||||
*/
|
|
||||||
errorret_t engineUpdate(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shuts down the engine.
|
|
||||||
*/
|
|
||||||
errorret_t engineDispose(void);
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
# Copyright (c) 2025 Dominic Masters
|
|
||||||
#
|
|
||||||
# This software is released under the MIT License.
|
|
||||||
# https://opensource.org/licenses/MIT
|
|
||||||
|
|
||||||
# Sources
|
|
||||||
target_sources(${DUSK_TARGET_NAME}
|
|
||||||
PRIVATE
|
|
||||||
error.cpp
|
|
||||||
)
|
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "assert/assert.hpp"
|
|
||||||
#include "error.hpp"
|
|
||||||
#include "util/memory.hpp"
|
|
||||||
#include "util/string.hpp"
|
|
||||||
#include "debug/debug.hpp"
|
|
||||||
|
|
||||||
errorstate_t ERROR_STATE = { 0 };
|
|
||||||
|
|
||||||
errorret_t errorThrowImpl(
|
|
||||||
errorstate_t *state,
|
|
||||||
errorcode_t code,
|
|
||||||
const char_t *file,
|
|
||||||
const char_t *function,
|
|
||||||
const int32_t line,
|
|
||||||
const char_t *message,
|
|
||||||
...
|
|
||||||
) {
|
|
||||||
assertNotNull(state, "Error state cannot be NULL");
|
|
||||||
assertTrue(code != ERROR_OK, "Error code must not be OK");
|
|
||||||
assertNotNull(file, "File cannot be NULL");
|
|
||||||
assertNotNull(function, "Function cannot be NULL");
|
|
||||||
assertTrue(line >= 0, "File pointer must be valid");
|
|
||||||
assertNotNull(message, "Message cannot be NULL");
|
|
||||||
|
|
||||||
memoryZero(state, sizeof(errorstate_t));
|
|
||||||
state->code = code;
|
|
||||||
|
|
||||||
// Format args.
|
|
||||||
va_list args;
|
|
||||||
va_start(args, message);
|
|
||||||
|
|
||||||
// Get length of formatted message
|
|
||||||
va_list argsCopy;
|
|
||||||
va_copy(argsCopy, args);
|
|
||||||
int32_t len = stringFormatVA(NULL, 0, message, argsCopy);
|
|
||||||
va_end(argsCopy);
|
|
||||||
|
|
||||||
// Create string to hold the formatted message
|
|
||||||
state->message = (char_t *)memoryAllocate(len + 1);
|
|
||||||
stringFormatVA(state->message, len + 1, message, args);
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
// Format lines
|
|
||||||
len = stringFormat(NULL, 0, ERROR_LINE_FORMAT, file, line, function);
|
|
||||||
assertTrue(len >= 0, "Line formatting failed");
|
|
||||||
state->lines = (char_t *)memoryAllocate(len + 1);
|
|
||||||
stringFormat(state->lines, len + 1, ERROR_LINE_FORMAT, file, line, function);
|
|
||||||
|
|
||||||
return (errorret_t) {
|
|
||||||
.code = code,
|
|
||||||
.state = state
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
errorret_t errorOkImpl() {
|
|
||||||
assertTrue(
|
|
||||||
ERROR_STATE.code == ERROR_OK,
|
|
||||||
"Global error state is not OK (Likely missing errorCatch)"
|
|
||||||
);
|
|
||||||
|
|
||||||
return (errorret_t) {
|
|
||||||
.code = ERROR_OK,
|
|
||||||
.state = NULL
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
errorret_t errorChainImpl(
|
|
||||||
const errorret_t retval,
|
|
||||||
const char_t *file,
|
|
||||||
const char_t *function,
|
|
||||||
const int32_t line
|
|
||||||
) {
|
|
||||||
if(retval.code == ERROR_OK) return retval;
|
|
||||||
|
|
||||||
assertNotNull(retval.state, "Error state NULL (Likely missing errorOk)");
|
|
||||||
assertNotNull(retval.state->message, "Message cannot be NULL");
|
|
||||||
|
|
||||||
// Create a new line string.
|
|
||||||
int32_t newLineLen = snprintf(NULL, 0, ERROR_LINE_FORMAT, file, line, function);
|
|
||||||
assertTrue(newLineLen >= 0, "Line formatting failed");
|
|
||||||
char_t *newLine = (char_t *)memoryAllocate(newLineLen + 1);
|
|
||||||
snprintf(newLine, newLineLen + 1, ERROR_LINE_FORMAT, file, line, function);
|
|
||||||
|
|
||||||
// Resize the existing lines to accommodate the new line
|
|
||||||
size_t existingLen = strlen(retval.state->lines);
|
|
||||||
memoryResize(
|
|
||||||
(void**)&retval.state->lines,
|
|
||||||
existingLen,
|
|
||||||
existingLen + newLineLen + 1
|
|
||||||
);
|
|
||||||
|
|
||||||
// Now append the new line to the existing lines
|
|
||||||
memoryCopy(
|
|
||||||
retval.state->lines + existingLen,
|
|
||||||
newLine,
|
|
||||||
newLineLen + 1
|
|
||||||
);
|
|
||||||
|
|
||||||
// Cleanup the temporary new line
|
|
||||||
memoryFree(newLine);
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
void errorCatch(const errorret_t retval) {
|
|
||||||
if(retval.code == ERROR_OK) return;
|
|
||||||
|
|
||||||
assertNotNull(retval.state, "Error state cannot be NULL");
|
|
||||||
assertNotNull(retval.state->message, "Message cannot be NULL");
|
|
||||||
|
|
||||||
memoryFree((void*)retval.state->message);
|
|
||||||
}
|
|
||||||
|
|
||||||
errorret_t errorPrint(const errorret_t retval) {
|
|
||||||
if(retval.code == ERROR_OK) return retval;
|
|
||||||
|
|
||||||
assertNotNull(retval.state, "Error state cannot be NULL");
|
|
||||||
assertNotNull(retval.state->message, "Message cannot be NULL");
|
|
||||||
|
|
||||||
debugPrint(
|
|
||||||
ERROR_PRINT_FORMAT,
|
|
||||||
retval.state->code,
|
|
||||||
retval.state->message,
|
|
||||||
retval.state->lines
|
|
||||||
);
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
@@ -1,165 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "dusk.hpp"
|
|
||||||
|
|
||||||
typedef uint8_t errorcode_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
errorcode_t code;
|
|
||||||
char_t *message;
|
|
||||||
char_t *lines;
|
|
||||||
} errorstate_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
errorcode_t code;
|
|
||||||
errorstate_t *state;
|
|
||||||
} errorret_t;
|
|
||||||
|
|
||||||
static const errorcode_t ERROR_OK = 0;
|
|
||||||
static const errorcode_t ERROR_NOT_OK = 1;
|
|
||||||
|
|
||||||
static const char_t *ERROR_PRINT_FORMAT = "Error (%d): %s\n%s";
|
|
||||||
static const char_t *ERROR_LINE_FORMAT = " at %s:%d in function %s\n";
|
|
||||||
extern errorstate_t ERROR_STATE;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the error state with the provided code and message.
|
|
||||||
*
|
|
||||||
* @param state The error state to initialize.
|
|
||||||
* @param code The error code to set.
|
|
||||||
* @param file The file where the error occurred.
|
|
||||||
* @param function The function where the error occurred.
|
|
||||||
* @param line The line number where the error occurred.
|
|
||||||
* @param message The error message.
|
|
||||||
* @param args The arguments for the error message.
|
|
||||||
* @return The error code.
|
|
||||||
*/
|
|
||||||
errorret_t errorThrowImpl(
|
|
||||||
errorstate_t *state,
|
|
||||||
errorcode_t code,
|
|
||||||
const char_t *file,
|
|
||||||
const char_t *function,
|
|
||||||
const int32_t line,
|
|
||||||
const char_t *message,
|
|
||||||
...
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an error state with no error.
|
|
||||||
*
|
|
||||||
* @return An error state with code ERROR_OK.
|
|
||||||
*/
|
|
||||||
errorret_t errorOkImpl();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Chains an error state, allowing for error propagation.
|
|
||||||
*
|
|
||||||
* @param retval The return value containing the error state.
|
|
||||||
* @param file The file where the error occurred.
|
|
||||||
* @param function The function where the error occurred.
|
|
||||||
* @param line The line number where the error occurred.
|
|
||||||
* @return The error code if an error occurred, otherwise continues execution.
|
|
||||||
*/
|
|
||||||
errorret_t errorChainImpl(
|
|
||||||
const errorret_t retval,
|
|
||||||
const char_t *file,
|
|
||||||
const char_t *function,
|
|
||||||
const int32_t line
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Catches an error and handles it.
|
|
||||||
*
|
|
||||||
* @param retval The return value containing the error state.
|
|
||||||
*/
|
|
||||||
void errorCatch(const errorret_t retval);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints the error state to the console.
|
|
||||||
*
|
|
||||||
* @param retval The return value containing the error state.
|
|
||||||
* @return Passed retval for chaining.
|
|
||||||
*/
|
|
||||||
errorret_t errorPrint(const errorret_t retval);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates an error with a specific error state.
|
|
||||||
*
|
|
||||||
* @param state The error state to set.
|
|
||||||
* @param message The format string for the error message.
|
|
||||||
* @param ... Additional arguments for the format string.
|
|
||||||
* @return The error code.
|
|
||||||
*/
|
|
||||||
#define errorCreate(state, message, ... ) \
|
|
||||||
errorThrowImpl(\
|
|
||||||
(state), ERROR_NOT_OK, __FILE__, __func__, __LINE__, (message), \
|
|
||||||
##__VA_ARGS__ \
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Throws an error with a specific error state.
|
|
||||||
*
|
|
||||||
* @param state The error state to set.
|
|
||||||
* @param message The format string for the error message.
|
|
||||||
* @param ... Additional arguments for the format string.
|
|
||||||
* @return The error code.
|
|
||||||
*/
|
|
||||||
#define errorThrowState(state, message, ... ) \
|
|
||||||
return errorThrowImpl(\
|
|
||||||
(state), ERROR_NOT_OK, __FILE__, __func__, __LINE__, (message), \
|
|
||||||
##__VA_ARGS__ \
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Throws an error with a formatted message.
|
|
||||||
*
|
|
||||||
* @param code The error code to throw.
|
|
||||||
* @param message The format string for the error message.
|
|
||||||
* @param ... Additional arguments for the format string.
|
|
||||||
* @return The error code.
|
|
||||||
*/
|
|
||||||
#define errorThrowWithCode(code, message, ... ) \
|
|
||||||
return errorThrowImpl(\
|
|
||||||
&ERROR_STATE, (code), __FILE__, __func__, __LINE__, (message), \
|
|
||||||
__VA_ARGS__ \
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Throws an error with a default error code of ERROR_NOT_OK.
|
|
||||||
*
|
|
||||||
* @param message The format string for the error message.
|
|
||||||
* @param ... Additional arguments for the format string.
|
|
||||||
* @return The error code.
|
|
||||||
*/
|
|
||||||
#define errorThrow(message, ...) \
|
|
||||||
return errorThrowImpl(\
|
|
||||||
&ERROR_STATE, ERROR_NOT_OK, __FILE__, __func__, __LINE__, (message), \
|
|
||||||
##__VA_ARGS__ \
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a child method errored, and if it did, then send it up the chain.
|
|
||||||
* @param retval The return value containing the error state.
|
|
||||||
* @return The error code if an error occurred, otherwise continues execution.
|
|
||||||
*/
|
|
||||||
#define errorChain(retval) { \
|
|
||||||
errorret_t errorChainRetval = (retval); \
|
|
||||||
if(errorChainRetval.code != ERROR_OK) { \
|
|
||||||
return errorChainImpl(errorChainRetval, __FILE__, __func__, __LINE__); \
|
|
||||||
} \
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns without an error.
|
|
||||||
*/
|
|
||||||
#define errorOk() \
|
|
||||||
return errorOkImpl()
|
|
||||||
|
|
||||||
// EOF
|
|
||||||
@@ -12,13 +12,12 @@
|
|||||||
|
|
||||||
localemanager_t LOCALE;
|
localemanager_t LOCALE;
|
||||||
|
|
||||||
errorret_t localeManagerInit() {
|
void localeManagerInit() {
|
||||||
memoryZero(&LOCALE, sizeof(localemanager_t));
|
memoryZero(&LOCALE, sizeof(localemanager_t));
|
||||||
errorChain(localeManagerSetLocale(DUSK_LOCALE_EN_US));
|
localeManagerSetLocale(DUSK_LOCALE_EN_US);
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t localeManagerSetLocale(const dusklocale_t locale) {
|
void localeManagerSetLocale(const dusklocale_t locale) {
|
||||||
assertTrue(locale < DUSK_LOCALE_COUNT, "Invalid locale.");
|
assertTrue(locale < DUSK_LOCALE_COUNT, "Invalid locale.");
|
||||||
LOCALE.locale = locale;
|
LOCALE.locale = locale;
|
||||||
char_t languageFile[FILENAME_MAX];
|
char_t languageFile[FILENAME_MAX];
|
||||||
@@ -27,8 +26,7 @@ errorret_t localeManagerSetLocale(const dusklocale_t locale) {
|
|||||||
);
|
);
|
||||||
assetLanguageDispose(&LOCALE.language);
|
assetLanguageDispose(&LOCALE.language);
|
||||||
memoryZero(&LOCALE.language, sizeof(assetlanguage_t));
|
memoryZero(&LOCALE.language, sizeof(assetlanguage_t));
|
||||||
errorChain(assetLoad(languageFile, &LOCALE.language));
|
assetLoad(languageFile, &LOCALE.language);
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void localeManagerDispose() {
|
void localeManagerDispose() {
|
||||||
|
|||||||
@@ -35,17 +35,17 @@ extern localemanager_t LOCALE;
|
|||||||
/**
|
/**
|
||||||
* Initialize the locale system.
|
* Initialize the locale system.
|
||||||
*
|
*
|
||||||
* @return An error code if a failure occurs.
|
* @throws std::runtime_error if initialization fails.
|
||||||
*/
|
*/
|
||||||
errorret_t localeManagerInit();
|
void localeManagerInit();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the current locale.
|
* Set the current locale.
|
||||||
*
|
*
|
||||||
* @param locale The locale to set.
|
* @param locale The locale to set.
|
||||||
* @return An error code if a failure occurs.
|
* @throws std::runtime_error if setting the locale fails.
|
||||||
*/
|
*/
|
||||||
errorret_t localeManagerSetLocale(const dusklocale_t locale);
|
void localeManagerSetLocale(const dusklocale_t locale);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispose of the locale system.
|
* Dispose of the locale system.
|
||||||
|
|||||||
39
src/main.cpp
39
src/main.cpp
@@ -5,35 +5,34 @@
|
|||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "engine/engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
#include "asset/asset.hpp"
|
|
||||||
#include "util/string.hpp"
|
using namespace Dusk;
|
||||||
#include "input/input.hpp"
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
// Main applet
|
|
||||||
errorret_t ret;
|
|
||||||
|
|
||||||
// Init engine
|
// Init engine
|
||||||
ret = engineInit(argc, (const char_t **)argv);
|
try {
|
||||||
if(ret.code != ERROR_OK) {
|
Engine::ENGINE.init(argc, (const char_t **)argv);
|
||||||
errorCatch(errorPrint(ret));
|
} catch (const std::runtime_error &e) {
|
||||||
return ret.code;
|
std::fprintf(stderr, "Fatal error during engine initialization: %s\n", e.what());
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin main loop
|
// Begin main loop
|
||||||
|
try {
|
||||||
do {
|
do {
|
||||||
ret = engineUpdate();
|
Engine::ENGINE.update();
|
||||||
if(ret.code != ERROR_OK) {
|
} while(Engine::ENGINE.isRunning());
|
||||||
errorCatch(errorPrint(ret));
|
} catch (const std::runtime_error &e) {
|
||||||
return ret.code;
|
std::fprintf(stderr, "Fatal error during engine update: %s\n", e.what());
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
} while(ENGINE.running);
|
|
||||||
|
|
||||||
ret = engineDispose();
|
try {
|
||||||
if(ret.code != ERROR_OK) {
|
Engine::ENGINE.exit();
|
||||||
errorCatch(errorPrint(ret));
|
} catch (const std::runtime_error &e) {
|
||||||
return ret.code;
|
std::fprintf(stderr, "Fatal error during engine exit: %s\n", e.what());
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -15,13 +15,13 @@
|
|||||||
#include "util/memory.hpp"
|
#include "util/memory.hpp"
|
||||||
#include "assert/assert.hpp"
|
#include "assert/assert.hpp"
|
||||||
|
|
||||||
errorret_t rpgInit(void) {
|
void rpgInit(void) {
|
||||||
memoryZero(ENTITIES, sizeof(ENTITIES));
|
memoryZero(ENTITIES, sizeof(ENTITIES));
|
||||||
|
|
||||||
// Init cutscene subsystem
|
// Init cutscene subsystem
|
||||||
cutsceneSystemInit();
|
cutsceneSystemInit();
|
||||||
|
|
||||||
errorChain(mapInit());
|
mapInit();
|
||||||
|
|
||||||
rpgCameraInit();
|
rpgCameraInit();
|
||||||
rpgTextboxInit();
|
rpgTextboxInit();
|
||||||
@@ -34,16 +34,11 @@ errorret_t rpgInit(void) {
|
|||||||
// RPG_CAMERA.mode = RPG_CAMERA_MODE_FOLLOW_ENTITY;
|
// RPG_CAMERA.mode = RPG_CAMERA_MODE_FOLLOW_ENTITY;
|
||||||
// RPG_CAMERA.followEntity.followEntityId = ent->id;
|
// RPG_CAMERA.followEntity.followEntityId = ent->id;
|
||||||
// ent->position.x = 2, ent->position.y = 2;
|
// ent->position.x = 2, ent->position.y = 2;
|
||||||
|
|
||||||
// All Good!
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t rpgUpdate(void) {
|
void rpgUpdate(void) {
|
||||||
#if TIME_FIXED == 0
|
#if TIME_FIXED == 0
|
||||||
if(TIME.dynamicUpdate) {
|
if(TIME.dynamicUpdate) return;
|
||||||
errorOk();
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// TODO: Do not update if the scene is not the map scene?
|
// TODO: Do not update if the scene is not the map scene?
|
||||||
@@ -57,8 +52,7 @@ errorret_t rpgUpdate(void) {
|
|||||||
} while(++ent < &ENTITIES[ENTITY_COUNT]);
|
} while(++ent < &ENTITIES[ENTITY_COUNT]);
|
||||||
|
|
||||||
cutsceneSystemUpdate();
|
cutsceneSystemUpdate();
|
||||||
errorChain(rpgCameraUpdate());
|
rpgCameraUpdate();
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void rpgDispose(void) {
|
void rpgDispose(void) {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.hpp"
|
#include "dusk.hpp"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int32_t nothing;
|
int32_t nothing;
|
||||||
@@ -17,14 +17,14 @@ typedef struct {
|
|||||||
*
|
*
|
||||||
* @return An error code and state.
|
* @return An error code and state.
|
||||||
*/
|
*/
|
||||||
errorret_t rpgInit(void);
|
void rpgInit(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the RPG subsystem.
|
* Update the RPG subsystem.
|
||||||
*
|
*
|
||||||
* @return An error code.
|
* @return An error code.
|
||||||
*/
|
*/
|
||||||
errorret_t rpgUpdate(void);
|
void rpgUpdate(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispose of the RPG subsystem.
|
* Dispose of the RPG subsystem.
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ void rpgCameraInit(void) {
|
|||||||
memoryZero(&RPG_CAMERA, sizeof(rpgcamera_t));
|
memoryZero(&RPG_CAMERA, sizeof(rpgcamera_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t rpgCameraUpdate(void) {
|
void rpgCameraUpdate(void) {
|
||||||
chunkpos_t chunkPos;
|
chunkpos_t chunkPos;
|
||||||
|
|
||||||
switch(RPG_CAMERA.mode) {
|
switch(RPG_CAMERA.mode) {
|
||||||
@@ -27,9 +27,7 @@ errorret_t rpgCameraUpdate(void) {
|
|||||||
|
|
||||||
case RPG_CAMERA_MODE_FOLLOW_ENTITY: {
|
case RPG_CAMERA_MODE_FOLLOW_ENTITY: {
|
||||||
entity_t *entity = &ENTITIES[RPG_CAMERA.followEntity.followEntityId];
|
entity_t *entity = &ENTITIES[RPG_CAMERA.followEntity.followEntityId];
|
||||||
if(entity->type == ENTITY_TYPE_NULL) {
|
if(entity->type == ENTITY_TYPE_NULL) return;
|
||||||
errorOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update map position to match camera. By default map wants to know the
|
// Update map position to match camera. By default map wants to know the
|
||||||
// top left but we want to set the center, so we need to sub half map size
|
// top left but we want to set the center, so we need to sub half map size
|
||||||
@@ -41,10 +39,9 @@ errorret_t rpgCameraUpdate(void) {
|
|||||||
assertUnreachable("Invalid RPG camera mode");
|
assertUnreachable("Invalid RPG camera mode");
|
||||||
}
|
}
|
||||||
|
|
||||||
errorChain(mapPositionSet((chunkpos_t){
|
mapPositionSet((chunkpos_t){
|
||||||
.x = chunkPos.x - (MAP_CHUNK_WIDTH / 2),
|
.x = chunkPos.x - (MAP_CHUNK_WIDTH / 2),
|
||||||
.y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2),
|
.y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2),
|
||||||
.z = chunkPos.z - (MAP_CHUNK_DEPTH / 2)
|
.z = chunkPos.z - (MAP_CHUNK_DEPTH / 2)
|
||||||
}));
|
});
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "rpg/world/worldpos.hpp"
|
#include "rpg/world/worldpos.hpp"
|
||||||
#include "error/error.hpp"
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
RPG_CAMERA_MODE_FREE,
|
RPG_CAMERA_MODE_FREE,
|
||||||
@@ -35,6 +34,6 @@ void rpgCameraInit(void);
|
|||||||
/**
|
/**
|
||||||
* Updates the RPG camera.
|
* Updates the RPG camera.
|
||||||
*
|
*
|
||||||
* @return An error code.
|
* @throws std::runtime_error if map position setting fails.
|
||||||
*/
|
*/
|
||||||
errorret_t rpgCameraUpdate(void);
|
void rpgCameraUpdate(void);
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
map_t MAP;
|
map_t MAP;
|
||||||
|
|
||||||
errorret_t mapInit() {
|
void mapInit() {
|
||||||
memoryZero(&MAP, sizeof(map_t));
|
memoryZero(&MAP, sizeof(map_t));
|
||||||
|
|
||||||
// Init the default chunks. In future I'll probably make this based on where
|
// Init the default chunks. In future I'll probably make this based on where
|
||||||
@@ -27,20 +27,16 @@ errorret_t mapInit() {
|
|||||||
chunk->position.y = y;
|
chunk->position.y = y;
|
||||||
chunk->position.z = z;
|
chunk->position.z = z;
|
||||||
MAP.chunkOrder[index] = chunk;
|
MAP.chunkOrder[index] = chunk;
|
||||||
errorChain(mapChunkLoad(chunk));
|
mapChunkLoad(chunk);
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t mapPositionSet(const chunkpos_t newPos) {
|
void mapPositionSet(const chunkpos_t newPos) {
|
||||||
const chunkpos_t curPos = MAP.chunkPosition;
|
const chunkpos_t curPos = MAP.chunkPosition;
|
||||||
if(chunkPositionIsEqual(curPos, newPos)) {
|
if(chunkPositionIsEqual(curPos, newPos)) return;
|
||||||
errorOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine which chunks remain loaded
|
// Determine which chunks remain loaded
|
||||||
chunkindex_t chunksRemaining[MAP_CHUNK_COUNT] = {0};
|
chunkindex_t chunksRemaining[MAP_CHUNK_COUNT] = {0};
|
||||||
@@ -102,7 +98,7 @@ errorret_t mapPositionSet(const chunkpos_t newPos) {
|
|||||||
chunkIndex = chunksFreed[--freedCount];
|
chunkIndex = chunksFreed[--freedCount];
|
||||||
chunk_t *chunk = &MAP.chunks[chunkIndex];
|
chunk_t *chunk = &MAP.chunks[chunkIndex];
|
||||||
chunk->position = newChunkPos;
|
chunk->position = newChunkPos;
|
||||||
errorChain(mapChunkLoad(chunk));
|
mapChunkLoad(chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
MAP.chunkOrder[orderIndex++] = &MAP.chunks[chunkIndex];
|
MAP.chunkOrder[orderIndex++] = &MAP.chunks[chunkIndex];
|
||||||
@@ -112,8 +108,6 @@ errorret_t mapPositionSet(const chunkpos_t newPos) {
|
|||||||
|
|
||||||
// Update map position
|
// Update map position
|
||||||
MAP.chunkPosition = newPos;
|
MAP.chunkPosition = newPos;
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mapUpdate() {
|
void mapUpdate() {
|
||||||
@@ -138,7 +132,7 @@ void mapChunkUnload(chunk_t* chunk) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t mapChunkLoad(chunk_t* chunk) {
|
void mapChunkLoad(chunk_t* chunk) {
|
||||||
char_t buffer[64];
|
char_t buffer[64];
|
||||||
|
|
||||||
chunk->meshCount = 0;
|
chunk->meshCount = 0;
|
||||||
@@ -153,11 +147,10 @@ errorret_t mapChunkLoad(chunk_t* chunk) {
|
|||||||
|
|
||||||
if(!assetFileExists(buffer)) {
|
if(!assetFileExists(buffer)) {
|
||||||
memoryZero(chunk->tiles, sizeof(chunk->tiles));
|
memoryZero(chunk->tiles, sizeof(chunk->tiles));
|
||||||
errorOk();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorChain(assetLoad(buffer, chunk));
|
assetLoad(buffer, chunk);
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chunkindex_t mapGetChunkIndexAt(const chunkpos_t position) {
|
chunkindex_t mapGetChunkIndexAt(const chunkpos_t position) {
|
||||||
|
|||||||
@@ -19,9 +19,9 @@ extern map_t MAP;
|
|||||||
/**
|
/**
|
||||||
* Initializes the map.
|
* Initializes the map.
|
||||||
*
|
*
|
||||||
* @return An error code.
|
* @throws std::runtime_error if chunk loading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t mapInit();
|
void mapInit();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the map.
|
* Updates the map.
|
||||||
@@ -37,9 +37,9 @@ void mapDispose();
|
|||||||
* Sets the map position and updates chunks accordingly.
|
* Sets the map position and updates chunks accordingly.
|
||||||
*
|
*
|
||||||
* @param newPos The new chunk position.
|
* @param newPos The new chunk position.
|
||||||
* @return An error code.
|
* @throws std::runtime_error if chunk loading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t mapPositionSet(const chunkpos_t newPos);
|
void mapPositionSet(const chunkpos_t newPos);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unloads a chunk.
|
* Unloads a chunk.
|
||||||
@@ -52,9 +52,9 @@ void mapChunkUnload(chunk_t* chunk);
|
|||||||
* Loads a chunk.
|
* Loads a chunk.
|
||||||
*
|
*
|
||||||
* @param chunk The chunk to load.
|
* @param chunk The chunk to load.
|
||||||
* @return An error code.
|
* @throws std::runtime_error if chunk loading fails.
|
||||||
*/
|
*/
|
||||||
errorret_t mapChunkLoad(chunk_t* chunk);
|
void mapChunkLoad(chunk_t* chunk);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the index of a chunk, within the world, at the given position.
|
* Gets the index of a chunk, within the world, at the given position.
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "dusk.hpp"
|
#include "dusk.hpp"
|
||||||
#include "error/error.hpp"
|
|
||||||
#include "display/color.hpp"
|
#include "display/color.hpp"
|
||||||
|
|
||||||
#define SCENE_FLAG_INITIALIZED (1 << 0)
|
#define SCENE_FLAG_INITIALIZED (1 << 0)
|
||||||
@@ -16,7 +15,7 @@ typedef struct scenedata_s scenedata_t;
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char_t *name;
|
const char_t *name;
|
||||||
errorret_t (*init)(scenedata_t *data);
|
void (*init)(scenedata_t *data);
|
||||||
void (*update)(scenedata_t *data);
|
void (*update)(scenedata_t *data);
|
||||||
void (*render)(scenedata_t *data);
|
void (*render)(scenedata_t *data);
|
||||||
void (*dispose)(scenedata_t *data);
|
void (*dispose)(scenedata_t *data);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
#include "util/memory.hpp"
|
#include "util/memory.hpp"
|
||||||
#include "duskdefs.hpp"
|
#include "duskdefs.hpp"
|
||||||
|
|
||||||
errorret_t sceneMapInit(scenedata_t *data) {
|
void sceneMapInit(scenedata_t *data) {
|
||||||
// Init the camera.
|
// Init the camera.
|
||||||
cameraInitPerspective(&data->sceneMap.camera);
|
cameraInitPerspective(&data->sceneMap.camera);
|
||||||
data->sceneMap.camera.projType = CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED;
|
data->sceneMap.camera.projType = CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED;
|
||||||
@@ -36,8 +36,6 @@ errorret_t sceneMapInit(scenedata_t *data) {
|
|||||||
RPG_CAMERA_PIXELS_PER_UNIT
|
RPG_CAMERA_PIXELS_PER_UNIT
|
||||||
);
|
);
|
||||||
data->sceneMap.camera.perspective.fov = glm_rad(RPG_CAMERA_FOV);
|
data->sceneMap.camera.perspective.fov = glm_rad(RPG_CAMERA_FOV);
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sceneMapUpdate(scenedata_t *data) {
|
void sceneMapUpdate(scenedata_t *data) {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ typedef struct {
|
|||||||
camera_t camera;
|
camera_t camera;
|
||||||
} scenemap_t;
|
} scenemap_t;
|
||||||
|
|
||||||
errorret_t sceneMapInit(scenedata_t *data);
|
void sceneMapInit(scenedata_t *data);
|
||||||
void sceneMapUpdate(scenedata_t *data);
|
void sceneMapUpdate(scenedata_t *data);
|
||||||
void sceneMapRender(scenedata_t *data);
|
void sceneMapRender(scenedata_t *data);
|
||||||
void sceneMapRenderEntity(entity_t *entity);
|
void sceneMapRenderEntity(entity_t *entity);
|
||||||
|
|||||||
@@ -8,9 +8,8 @@
|
|||||||
#include "scenetest.hpp"
|
#include "scenetest.hpp"
|
||||||
#include "scene/scenedata.hpp"
|
#include "scene/scenedata.hpp"
|
||||||
|
|
||||||
errorret_t sceneTestInit(scenedata_t *data) {
|
void sceneTestInit(scenedata_t *data) {
|
||||||
data->sceneTest.nothing = 0;
|
data->sceneTest.nothing = 0;
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sceneTestUpdate(scenedata_t *data) {
|
void sceneTestUpdate(scenedata_t *data) {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ typedef struct {
|
|||||||
int32_t nothing;
|
int32_t nothing;
|
||||||
} scenetest_t;
|
} scenetest_t;
|
||||||
|
|
||||||
errorret_t sceneTestInit(scenedata_t *data);
|
void sceneTestInit(scenedata_t *data);
|
||||||
void sceneTestUpdate(scenedata_t *data);
|
void sceneTestUpdate(scenedata_t *data);
|
||||||
void sceneTestRender(scenedata_t *data);
|
void sceneTestRender(scenedata_t *data);
|
||||||
void sceneTestDispose(scenedata_t *data);
|
void sceneTestDispose(scenedata_t *data);
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
scenemanager_t SCENE_MANAGER;
|
scenemanager_t SCENE_MANAGER;
|
||||||
|
|
||||||
errorret_t sceneManagerInit(void) {
|
void sceneManagerInit(void) {
|
||||||
memoryZero(&SCENE_MANAGER, sizeof(scenemanager_t));
|
memoryZero(&SCENE_MANAGER, sizeof(scenemanager_t));
|
||||||
|
|
||||||
sceneManagerRegisterScene(&SCENE_TEST);
|
sceneManagerRegisterScene(&SCENE_TEST);
|
||||||
@@ -22,10 +22,8 @@ errorret_t sceneManagerInit(void) {
|
|||||||
// Initial scene
|
// Initial scene
|
||||||
scene_t *initial = sceneManagerGetSceneByName("map");
|
scene_t *initial = sceneManagerGetSceneByName("map");
|
||||||
sceneManagerSetScene(initial);
|
sceneManagerSetScene(initial);
|
||||||
if(initial->init) errorChain(initial->init(&SCENE_MANAGER.sceneData));
|
if(initial->init) initial->init(&SCENE_MANAGER.sceneData);
|
||||||
initial->flags |= SCENE_FLAG_INITIALIZED;
|
initial->flags |= SCENE_FLAG_INITIALIZED;
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scene_t * sceneManagerGetSceneByName(const char_t *name) {
|
scene_t * sceneManagerGetSceneByName(const char_t *name) {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ extern scenemanager_t SCENE_MANAGER;
|
|||||||
/**
|
/**
|
||||||
* Initializes the scene manager and the initial scene.
|
* Initializes the scene manager and the initial scene.
|
||||||
*/
|
*/
|
||||||
errorret_t sceneManagerInit(void);
|
void sceneManagerInit(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a registered scene by its name.
|
* Retrieves a registered scene by its name.
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
#include "script/func/scriptfuncentity.hpp"
|
#include "script/func/scriptfuncentity.hpp"
|
||||||
#include "script/func/scriptfuncsystem.hpp"
|
#include "script/func/scriptfuncsystem.hpp"
|
||||||
|
|
||||||
errorret_t scriptContextInit(scriptcontext_t *context) {
|
void scriptContextInit(scriptcontext_t *context) {
|
||||||
assertNotNull(context, "Script context cannot be NULL");
|
assertNotNull(context, "Script context cannot be NULL");
|
||||||
|
|
||||||
memoryZero(context, sizeof(scriptcontext_t));
|
memoryZero(context, sizeof(scriptcontext_t));
|
||||||
@@ -23,15 +23,13 @@ errorret_t scriptContextInit(scriptcontext_t *context) {
|
|||||||
// Create a new Lua state for this context.
|
// Create a new Lua state for this context.
|
||||||
context->luaState = luaL_newstate();
|
context->luaState = luaL_newstate();
|
||||||
if(context->luaState == NULL) {
|
if(context->luaState == NULL) {
|
||||||
errorThrow("Failed to init Lua state");
|
throw std::runtime_error("Failed to init Lua state");
|
||||||
}
|
}
|
||||||
luaL_openlibs(context->luaState);
|
luaL_openlibs(context->luaState);
|
||||||
|
|
||||||
// Register functions
|
// Register functions
|
||||||
scriptFuncSystem(context);
|
scriptFuncSystem(context);
|
||||||
scriptFuncEntity(context);
|
scriptFuncEntity(context);
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void scriptContextRegFunc(
|
void scriptContextRegFunc(
|
||||||
@@ -46,7 +44,7 @@ void scriptContextRegFunc(
|
|||||||
lua_register(context->luaState, fnName, function);
|
lua_register(context->luaState, fnName, function);
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t scriptContextCallFunc(
|
void scriptContextCallFunc(
|
||||||
scriptcontext_t *context,
|
scriptcontext_t *context,
|
||||||
const char_t *fnName,
|
const char_t *fnName,
|
||||||
const scriptvalue_t *args,
|
const scriptvalue_t *args,
|
||||||
@@ -60,7 +58,9 @@ errorret_t scriptContextCallFunc(
|
|||||||
// Get func
|
// Get func
|
||||||
lua_getglobal(context->luaState, fnName);
|
lua_getglobal(context->luaState, fnName);
|
||||||
if(!lua_isfunction(context->luaState, -1)) {
|
if(!lua_isfunction(context->luaState, -1)) {
|
||||||
errorThrow("Function '%s' not found in script context", fnName);
|
throw std::runtime_error(
|
||||||
|
"Function '" + std::string(fnName) + "' not found in script context"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push args
|
// Push args
|
||||||
@@ -80,7 +80,9 @@ errorret_t scriptContextCallFunc(
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
errorThrow("Unsupported argument type %d", arg->type);
|
throw std::runtime_error(
|
||||||
|
"Unsupported argument type " + std::to_string(arg->type)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,33 +95,48 @@ errorret_t scriptContextCallFunc(
|
|||||||
) != LUA_OK) {
|
) != LUA_OK) {
|
||||||
const char_t *strErr = lua_tostring(context->luaState, -1);
|
const char_t *strErr = lua_tostring(context->luaState, -1);
|
||||||
lua_pop(context->luaState, 1);
|
lua_pop(context->luaState, 1);
|
||||||
errorThrow("Failed to call function '%s': %s", fnName, strErr);
|
throw std::runtime_error(
|
||||||
|
"Failed to call Lua function '" +
|
||||||
|
std::string(fnName) +
|
||||||
|
"': " +
|
||||||
|
std::string(strErr)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Was there a ret value?
|
// Was there a ret value?
|
||||||
if(retValue == NULL) {
|
if(retValue == NULL) return;
|
||||||
errorOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get ret value
|
// Get ret value
|
||||||
switch(retValue->type) {
|
switch(retValue->type) {
|
||||||
case SCRIPT_VALUE_TYPE_INT:
|
case SCRIPT_VALUE_TYPE_INT:
|
||||||
if(!lua_isinteger(context->luaState, -1)) {
|
if(!lua_isinteger(context->luaState, -1)) {
|
||||||
errorThrow("Expected integer return value from '%s'", fnName);
|
throw std::runtime_error(
|
||||||
|
"Expected integer return value from '" +
|
||||||
|
std::string(fnName) +
|
||||||
|
"'"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
retValue->value.intValue = (int32_t)lua_tointeger(context->luaState, -1);
|
retValue->value.intValue = (int32_t)lua_tointeger(context->luaState, -1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SCRIPT_VALUE_TYPE_FLOAT:
|
case SCRIPT_VALUE_TYPE_FLOAT:
|
||||||
if(!lua_isnumber(context->luaState, -1)) {
|
if(!lua_isnumber(context->luaState, -1)) {
|
||||||
errorThrow("Expected float return value from '%s'", fnName);
|
throw std::runtime_error(
|
||||||
|
"Expected float return value from '" +
|
||||||
|
std::string(fnName) +
|
||||||
|
"'"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
retValue->value.floatValue = (float)lua_tonumber(context->luaState, -1);
|
retValue->value.floatValue = (float)lua_tonumber(context->luaState, -1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SCRIPT_VALUE_TYPE_BOOL:
|
case SCRIPT_VALUE_TYPE_BOOL:
|
||||||
if(!lua_isboolean(context->luaState, -1)) {
|
if(!lua_isboolean(context->luaState, -1)) {
|
||||||
errorThrow("Expected boolean return value from '%s'", fnName);
|
throw std::runtime_error(
|
||||||
|
"Expected boolean return value from '" +
|
||||||
|
std::string(fnName) +
|
||||||
|
"'"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
retValue->value.boolValue = lua_toboolean(context->luaState, -1);
|
retValue->value.boolValue = lua_toboolean(context->luaState, -1);
|
||||||
break;
|
break;
|
||||||
@@ -132,48 +149,49 @@ errorret_t scriptContextCallFunc(
|
|||||||
// break;
|
// break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
errorThrow("Unsupported return value type %d", retValue->type);
|
throw std::runtime_error(
|
||||||
|
"Unsupported return value type " + std::to_string(retValue->type)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
errorOk();
|
void scriptContextExec(scriptcontext_t *context, const char_t *script) {
|
||||||
}
|
|
||||||
|
|
||||||
errorret_t scriptContextExec(scriptcontext_t *context, const char_t *script) {
|
|
||||||
assertNotNull(context, "Script context cannot be NULL");
|
assertNotNull(context, "Script context cannot be NULL");
|
||||||
assertNotNull(script, "Script cannot be NULL");
|
assertNotNull(script, "Script cannot be NULL");
|
||||||
|
|
||||||
if(luaL_dostring(context->luaState, script) != LUA_OK) {
|
if(luaL_dostring(context->luaState, script) != LUA_OK) {
|
||||||
const char_t *strErr = lua_tostring(context->luaState, -1);
|
const char_t *strErr = lua_tostring(context->luaState, -1);
|
||||||
lua_pop(context->luaState, 1);
|
lua_pop(context->luaState, 1);
|
||||||
errorThrow("Failed to execute Lua: ", strErr);
|
throw std::runtime_error("Failed to execute Lua: " + std::string(strErr));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
errorOk();
|
void scriptContextExecFile(scriptcontext_t *ctx, const char_t *fname) {
|
||||||
}
|
|
||||||
|
|
||||||
errorret_t scriptContextExecFile(scriptcontext_t *ctx, const char_t *fname) {
|
|
||||||
assertNotNull(ctx, "Script context cannot be NULL");
|
assertNotNull(ctx, "Script context cannot be NULL");
|
||||||
assertNotNull(fname, "Filename cannot be NULL");
|
assertNotNull(fname, "Filename cannot be NULL");
|
||||||
|
|
||||||
assetscript_t script;
|
assetscript_t script;
|
||||||
errorChain(assetLoad(fname, &script));
|
assetLoad(fname, &script);
|
||||||
|
|
||||||
if(lua_load(
|
if(lua_load(
|
||||||
ctx->luaState, assetScriptReader, &script, fname, NULL
|
ctx->luaState, assetScriptReader, &script, fname, NULL
|
||||||
) != LUA_OK) {
|
) != LUA_OK) {
|
||||||
const char_t *strErr = lua_tostring(ctx->luaState, -1);
|
const char_t *strErr = lua_tostring(ctx->luaState, -1);
|
||||||
lua_pop(ctx->luaState, 1);
|
lua_pop(ctx->luaState, 1);
|
||||||
errorThrow("Failed to load Lua script: %s", strErr);
|
throw std::runtime_error(
|
||||||
|
"Failed to load Lua script: " + std::string(strErr)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(lua_pcall(ctx->luaState, 0, LUA_MULTRET, 0) != LUA_OK) {
|
if(lua_pcall(ctx->luaState, 0, LUA_MULTRET, 0) != LUA_OK) {
|
||||||
const char_t *strErr = lua_tostring(ctx->luaState, -1);
|
const char_t *strErr = lua_tostring(ctx->luaState, -1);
|
||||||
lua_pop(ctx->luaState, 1);
|
lua_pop(ctx->luaState, 1);
|
||||||
errorThrow("Failed to execute Lua script: %s", strErr);
|
throw std::runtime_error(
|
||||||
|
"Failed to execute Lua script: " + std::string(strErr)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
errorChain(assetScriptDispose(&script));
|
assetScriptDispose(&script);
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void scriptContextDispose(scriptcontext_t *context) {
|
void scriptContextDispose(scriptcontext_t *context) {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.hpp"
|
|
||||||
#include "scriptvalue.hpp"
|
#include "scriptvalue.hpp"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -23,9 +22,9 @@ typedef struct scriptcontext_s {
|
|||||||
* Initialize a script context.
|
* Initialize a script context.
|
||||||
*
|
*
|
||||||
* @param context The script context to initialize.
|
* @param context The script context to initialize.
|
||||||
* @return The error return value.
|
* @throws std::runtime_error on failure.
|
||||||
*/
|
*/
|
||||||
errorret_t scriptContextInit(scriptcontext_t *context);
|
void scriptContextInit(scriptcontext_t *context);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a C function within a script context.
|
* Register a C function within a script context.
|
||||||
@@ -48,10 +47,10 @@ void scriptContextRegFunc(
|
|||||||
* @param args Array of args to pass to the function (or NULL for no args)
|
* @param args Array of args to pass to the function (or NULL for no args)
|
||||||
* @param argCount The number of arguments in the args array (omitable).
|
* @param argCount The number of arguments in the args array (omitable).
|
||||||
* @param retValue Output to store returned value (or NULL for no return value).
|
* @param retValue Output to store returned value (or NULL for no return value).
|
||||||
* @return The error return value.
|
* @throws std::runtime_error on failure.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
errorret_t scriptContextCallFunc(
|
void scriptContextCallFunc(
|
||||||
scriptcontext_t *context,
|
scriptcontext_t *context,
|
||||||
const char_t *fnName,
|
const char_t *fnName,
|
||||||
const scriptvalue_t *args,
|
const scriptvalue_t *args,
|
||||||
@@ -64,18 +63,18 @@ errorret_t scriptContextCallFunc(
|
|||||||
*
|
*
|
||||||
* @param context The script context to use.
|
* @param context The script context to use.
|
||||||
* @param script The script to execute.
|
* @param script The script to execute.
|
||||||
* @return The error return value.
|
* @throws std::runtime_error on failure.
|
||||||
*/
|
*/
|
||||||
errorret_t scriptContextExec(scriptcontext_t *context, const char_t *script);
|
void scriptContextExec(scriptcontext_t *context, const char_t *script);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute a script from a file within a script context.
|
* Execute a script from a file within a script context.
|
||||||
*
|
*
|
||||||
* @param ctx The script context to use.
|
* @param ctx The script context to use.
|
||||||
* @param fname The filename of the script to execute.
|
* @param fname The filename of the script to execute.
|
||||||
* @return The error return value.
|
* @throws std::runtime_error on failure.
|
||||||
*/
|
*/
|
||||||
errorret_t scriptContextExecFile(scriptcontext_t *ctx, const char_t *fname);
|
void scriptContextExecFile(scriptcontext_t *ctx, const char_t *fname);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispose of a script context.
|
* Dispose of a script context.
|
||||||
|
|||||||
@@ -13,11 +13,9 @@
|
|||||||
|
|
||||||
scriptmanager_t SCRIPT_MANAGER;
|
scriptmanager_t SCRIPT_MANAGER;
|
||||||
|
|
||||||
errorret_t scriptManagerInit() {
|
void scriptManagerInit() {
|
||||||
memoryZero(&SCRIPT_MANAGER, sizeof(scriptmanager_t));
|
memoryZero(&SCRIPT_MANAGER, sizeof(scriptmanager_t));
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t scriptManagerDispose() {
|
void scriptManagerDispose() {
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.hpp"
|
|
||||||
#include "scriptcontext.hpp"
|
#include "scriptcontext.hpp"
|
||||||
|
|
||||||
typedef struct scriptmanager_s {
|
typedef struct scriptmanager_s {
|
||||||
@@ -18,13 +17,13 @@ extern scriptmanager_t SCRIPT_MANAGER;
|
|||||||
/**
|
/**
|
||||||
* Initialize the script manager.
|
* Initialize the script manager.
|
||||||
*
|
*
|
||||||
* @return The error return value.
|
* @throws Any exceptions if initialization fails.
|
||||||
*/
|
*/
|
||||||
errorret_t scriptManagerInit();
|
void scriptManagerInit();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispose of the script manager.
|
* Dispose of the script manager.
|
||||||
*
|
*
|
||||||
* @return The error return value.
|
* @throws Any exceptions if disposal fails.
|
||||||
*/
|
*/
|
||||||
errorret_t scriptManagerDispose();
|
void scriptManagerDispose();
|
||||||
@@ -15,15 +15,13 @@
|
|||||||
|
|
||||||
ui_t UI;
|
ui_t UI;
|
||||||
|
|
||||||
errorret_t uiInit(void) {
|
void uiInit(void) {
|
||||||
memoryZero(&UI, sizeof(ui_t));
|
memoryZero(&UI, sizeof(ui_t));
|
||||||
|
|
||||||
cameraInitOrthographic(&UI.camera);
|
cameraInitOrthographic(&UI.camera);
|
||||||
|
|
||||||
// Initialize UI components here
|
// Initialize UI components here
|
||||||
uiSetFont(&TILESET_MINOGRAM);
|
uiSetFont(&TILESET_MINOGRAM);
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void uiUpdate(void) {
|
void uiUpdate(void) {
|
||||||
@@ -47,7 +45,7 @@ void uiRender(void) {
|
|||||||
cameraPopMatrix();
|
cameraPopMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t uiSetFont(const tileset_t *fontTileset) {
|
void uiSetFont(const tileset_t *fontTileset) {
|
||||||
if(UI.fontTexture.width > 0) {
|
if(UI.fontTexture.width > 0) {
|
||||||
textureDispose(&UI.fontTexture);
|
textureDispose(&UI.fontTexture);
|
||||||
UI.fontTexture.width = -1;
|
UI.fontTexture.width = -1;
|
||||||
@@ -56,8 +54,7 @@ errorret_t uiSetFont(const tileset_t *fontTileset) {
|
|||||||
assertNotNull(fontTileset, "Font tileset cannot be NULL.");
|
assertNotNull(fontTileset, "Font tileset cannot be NULL.");
|
||||||
|
|
||||||
UI.fontTileset = fontTileset;
|
UI.fontTileset = fontTileset;
|
||||||
errorChain(assetLoad(UI.fontTileset->image, &UI.fontTexture));
|
assetLoad(UI.fontTileset->image, &UI.fontTexture);
|
||||||
errorOk();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void uiDispose(void) {
|
void uiDispose(void) {
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ extern ui_t UI;
|
|||||||
/**
|
/**
|
||||||
* Initializes the UI system, loading necessary resources.
|
* Initializes the UI system, loading necessary resources.
|
||||||
*
|
*
|
||||||
* @return An errorret_t indicating success or failure.
|
* @throws Any exceptions if initialization fails.
|
||||||
*/
|
*/
|
||||||
errorret_t uiInit(void);
|
void uiInit(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the UI state, handling user interactions and animations.
|
* Updates the UI state, handling user interactions and animations.
|
||||||
@@ -40,10 +40,9 @@ void uiRender(void);
|
|||||||
* Sets the font tileset for UI text rendering.
|
* Sets the font tileset for UI text rendering.
|
||||||
*
|
*
|
||||||
* @param fontTileset Pointer to the tileset to use for UI fonts.
|
* @param fontTileset Pointer to the tileset to use for UI fonts.
|
||||||
*
|
* @throws Any exceptions if setting the font fails.
|
||||||
* @return An errorret_t indicating success or failure.
|
|
||||||
*/
|
*/
|
||||||
errorret_t uiSetFont(const tileset_t *fontTileset);
|
void uiSetFont(const tileset_t *fontTileset);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cleans up and frees all UI resources.
|
* Cleans up and frees all UI resources.
|
||||||
|
|||||||
Reference in New Issue
Block a user