Add inventory.
This commit is contained in:
13
archive/scene/CMakeLists.txt
Normal file
13
archive/scene/CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
scenemanager.c
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(scene)
|
||||
24
archive/scene/scene.h
Normal file
24
archive/scene/scene.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
#include "error/error.h"
|
||||
#include "display/color.h"
|
||||
|
||||
#define SCENE_FLAG_INITIALIZED (1 << 0)
|
||||
|
||||
typedef struct scenedata_s scenedata_t;
|
||||
|
||||
typedef struct {
|
||||
const char_t *name;
|
||||
errorret_t (*init)(scenedata_t *data);
|
||||
void (*update)(scenedata_t *data);
|
||||
void (*render)(scenedata_t *data);
|
||||
void (*dispose)(scenedata_t *data);
|
||||
uint8_t flags;
|
||||
} scene_t;
|
||||
13
archive/scene/scene/CMakeLists.txt
Normal file
13
archive/scene/scene/CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
scenetest.c
|
||||
scenemap.c
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
212
archive/scene/scene/scenemap.c
Normal file
212
archive/scene/scene/scenemap.c
Normal file
@@ -0,0 +1,212 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "scenemap.h"
|
||||
#include "scene/scenedata.h"
|
||||
#include "display/spritebatch.h"
|
||||
#include "assert/assert.h"
|
||||
#include "asset/asset.h"
|
||||
#include "rpg/entity/entity.h"
|
||||
#include "rpg/overworld/map.h"
|
||||
#include "display/screen.h"
|
||||
#include "rpg/rpgcamera.h"
|
||||
#include "util/memory.h"
|
||||
#include "duskdefs.h"
|
||||
|
||||
errorret_t sceneMapInit(scenedata_t *data) {
|
||||
// Init the camera.
|
||||
cameraInitPerspective(&data->sceneMap.camera);
|
||||
data->sceneMap.camera.projType = CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED;
|
||||
data->sceneMap.camera.viewType = CAMERA_VIEW_TYPE_LOOKAT_PIXEL_PERFECT;
|
||||
glm_vec3_zero(data->sceneMap.camera.lookatPixelPerfect.offset);
|
||||
data->sceneMap.camera.lookatPixelPerfect.offset[1] = RPG_CAMERA_Z_OFFSET;
|
||||
glm_vec3_copy(
|
||||
(vec3){ 0.0f, 0.0f, 0.0f },
|
||||
data->sceneMap.camera.lookatPixelPerfect.target
|
||||
);
|
||||
glm_vec3_copy(
|
||||
(vec3){ 0.0f, 1.0f, 0.0f },
|
||||
data->sceneMap.camera.lookatPixelPerfect.up
|
||||
);
|
||||
data->sceneMap.camera.lookatPixelPerfect.pixelsPerUnit = (
|
||||
RPG_CAMERA_PIXELS_PER_UNIT
|
||||
);
|
||||
data->sceneMap.camera.perspective.fov = glm_rad(RPG_CAMERA_FOV);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void sceneMapUpdate(scenedata_t *data) {
|
||||
|
||||
}
|
||||
|
||||
void sceneMapGetWorldPosition(const worldpos_t pos, vec3 outPosition) {
|
||||
assertNotNull(outPosition, "Output position cannot be NULL");
|
||||
|
||||
outPosition[0] = pos.x * TILE_WIDTH;
|
||||
outPosition[1] = pos.y * TILE_HEIGHT;
|
||||
outPosition[2] = pos.z * TILE_DEPTH;
|
||||
|
||||
// Handle stair tiles.
|
||||
tile_t tile = mapGetTile(pos);
|
||||
if(tileIsRamp(tile)) {
|
||||
outPosition[2] += TILE_DEPTH / 2.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void sceneMapEntityGetPosition(const entity_t *entity, vec3 outPosition) {
|
||||
assertNotNull(entity, "Entity cannot be NULL");
|
||||
assertNotNull(outPosition, "Output position cannot be NULL");
|
||||
|
||||
// Get position
|
||||
sceneMapGetWorldPosition(entity->position, outPosition);
|
||||
|
||||
// Add a small offset so we render above the tile
|
||||
outPosition[2] += 0.1f;
|
||||
|
||||
// Add animation offset(s)
|
||||
switch(entity->animation) {
|
||||
case ENTITY_ANIM_WALK: {
|
||||
float_t animPercentage = entity->animTime / ENTITY_ANIM_WALK_DURATION;
|
||||
|
||||
vec3 lastPosition;
|
||||
sceneMapGetWorldPosition(entity->lastPosition, lastPosition);
|
||||
|
||||
vec3 offset;
|
||||
glm_vec3_sub(outPosition, lastPosition, offset);
|
||||
glm_vec3_scale(offset, -animPercentage, offset);
|
||||
glm_vec3_add(outPosition, offset, outPosition);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void sceneMapRender(scenedata_t *data) {
|
||||
if(!mapIsLoaded()) return;
|
||||
|
||||
// Look at target.
|
||||
vec3 cameraTarget;
|
||||
switch(RPG_CAMERA.mode) {
|
||||
case RPG_CAMERA_MODE_FREE:
|
||||
sceneMapGetWorldPosition(RPG_CAMERA.free, cameraTarget);
|
||||
break;
|
||||
|
||||
case RPG_CAMERA_MODE_FOLLOW_ENTITY: {
|
||||
const entity_t *ent = &ENTITIES[RPG_CAMERA.followEntity.followEntityId];
|
||||
sceneMapEntityGetPosition(ent, cameraTarget);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
glm_vec3_zero(cameraTarget);
|
||||
break;
|
||||
}
|
||||
|
||||
glm_vec3_copy(
|
||||
cameraTarget,
|
||||
data->sceneMap.camera.lookatPixelPerfect.target
|
||||
);
|
||||
|
||||
// Push camera
|
||||
cameraPushMatrix(&data->sceneMap.camera);
|
||||
|
||||
// Render map probably.
|
||||
sceneMapRenderMap();
|
||||
|
||||
// Render ents
|
||||
entity_t *ent = ENTITIES;
|
||||
do {
|
||||
sceneMapRenderEntity(ent);
|
||||
} while(++ent, ent < &ENTITIES[ENTITY_COUNT]);
|
||||
spriteBatchFlush();
|
||||
|
||||
// Finished, pop back camera.
|
||||
cameraPopMatrix();
|
||||
}
|
||||
|
||||
void sceneMapRenderEntity(entity_t *entity) {
|
||||
assertNotNull(entity, "Entity cannot be NULL");
|
||||
|
||||
if(entity->type == ENTITY_TYPE_NULL) return;
|
||||
|
||||
vec3 posMin, posMax;
|
||||
vec3 size = { TILE_WIDTH, TILE_HEIGHT, TILE_DEPTH };
|
||||
sceneMapEntityGetPosition(entity, posMin);
|
||||
glm_vec3_add(posMin, size, posMax);
|
||||
|
||||
// TEST: Change color depending on dir.
|
||||
color_t testColor;
|
||||
switch(entity->direction) {
|
||||
case ENTITY_DIR_NORTH:
|
||||
testColor = COLOR_BLUE;
|
||||
break;
|
||||
case ENTITY_DIR_EAST:
|
||||
testColor = COLOR_GREEN;
|
||||
break;
|
||||
case ENTITY_DIR_SOUTH:
|
||||
testColor = COLOR_CYAN;
|
||||
break;
|
||||
case ENTITY_DIR_WEST:
|
||||
testColor = COLOR_YELLOW;
|
||||
break;
|
||||
default:
|
||||
testColor = COLOR_WHITE;
|
||||
break;
|
||||
}
|
||||
|
||||
vec2 uv0 = { 0.0f, 0.0f };
|
||||
vec2 uv1 = { 1.0f, 1.0f };
|
||||
|
||||
spriteBatchPush3D(NULL, posMin, posMax, testColor, uv0, uv1);
|
||||
}
|
||||
|
||||
void sceneMapRenderMap() {
|
||||
assertTrue(mapIsLoaded(), "No map loaded to render");
|
||||
|
||||
// For each chunk.
|
||||
for(uint32_t i = 0; i < MAP_CHUNK_COUNT; i++) {
|
||||
chunk_t *chunk = MAP.chunkOrder[i];
|
||||
|
||||
for(uint8_t j = 0; j < chunk->meshCount; j++) {
|
||||
mesh_t *mesh = &chunk->meshes[j];
|
||||
if(mesh->vertexCount == 0) continue;
|
||||
textureBind(NULL);
|
||||
meshDraw(mesh, -1, -1);
|
||||
}
|
||||
|
||||
// vec3 min, max;
|
||||
// min[0] = chunk->position.x * CHUNK_WIDTH * TILE_WIDTH;
|
||||
// min[1] = chunk->position.y * CHUNK_HEIGHT * TILE_HEIGHT;
|
||||
// min[2] = chunk->position.z * CHUNK_DEPTH * TILE_DEPTH;
|
||||
|
||||
// max[0] = min[0] + (CHUNK_WIDTH * TILE_WIDTH);
|
||||
// max[1] = min[1] + (CHUNK_HEIGHT * TILE_HEIGHT);
|
||||
// max[2] = min[2];
|
||||
|
||||
// color_t color = COLOR_WHITE;
|
||||
// if(chunk->position.x % 2 == 0) {
|
||||
// color = (chunk->position.y % 2 == 0) ? COLOR_BLACK : COLOR_WHITE;
|
||||
// } else {
|
||||
// color = (chunk->position.y % 2 == 0) ? COLOR_WHITE : COLOR_BLACK;
|
||||
// }
|
||||
|
||||
// spriteBatchPush3D(
|
||||
// NULL,
|
||||
// min,
|
||||
// max,
|
||||
// color,
|
||||
// (vec2){ 0.0f, 0.0f },
|
||||
// (vec2){ 1.0f, 1.0f }
|
||||
// );
|
||||
}
|
||||
}
|
||||
|
||||
void sceneMapDispose(scenedata_t *data) {
|
||||
}
|
||||
32
archive/scene/scene/scenemap.h
Normal file
32
archive/scene/scene/scenemap.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "scene/scene.h"
|
||||
#include "rpg/entity/entity.h"
|
||||
#include "display/camera.h"
|
||||
#include "asset/asset.h"
|
||||
|
||||
typedef struct {
|
||||
camera_t camera;
|
||||
} scenemap_t;
|
||||
|
||||
errorret_t sceneMapInit(scenedata_t *data);
|
||||
void sceneMapUpdate(scenedata_t *data);
|
||||
void sceneMapRender(scenedata_t *data);
|
||||
void sceneMapRenderEntity(entity_t *entity);
|
||||
void sceneMapRenderMap();
|
||||
void sceneMapDispose(scenedata_t *data);
|
||||
|
||||
static scene_t SCENE_MAP = {
|
||||
.name = "map",
|
||||
.init = sceneMapInit,
|
||||
.update = sceneMapUpdate,
|
||||
.render = sceneMapRender,
|
||||
.dispose = sceneMapDispose,
|
||||
.flags = 0
|
||||
};
|
||||
26
archive/scene/scene/scenetest.c
Normal file
26
archive/scene/scene/scenetest.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "scenetest.h"
|
||||
#include "scene/scenedata.h"
|
||||
|
||||
errorret_t sceneTestInit(scenedata_t *data) {
|
||||
data->sceneTest.nothing = 0;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void sceneTestUpdate(scenedata_t *data) {
|
||||
|
||||
}
|
||||
|
||||
void sceneTestRender(scenedata_t *data) {
|
||||
|
||||
}
|
||||
|
||||
void sceneTestDispose(scenedata_t *data) {
|
||||
|
||||
}
|
||||
27
archive/scene/scene/scenetest.h
Normal file
27
archive/scene/scene/scenetest.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "scene/scene.h"
|
||||
|
||||
typedef struct {
|
||||
int32_t nothing;
|
||||
} scenetest_t;
|
||||
|
||||
errorret_t sceneTestInit(scenedata_t *data);
|
||||
void sceneTestUpdate(scenedata_t *data);
|
||||
void sceneTestRender(scenedata_t *data);
|
||||
void sceneTestDispose(scenedata_t *data);
|
||||
|
||||
static scene_t SCENE_TEST = {
|
||||
.name = "test",
|
||||
.init = sceneTestInit,
|
||||
.update = sceneTestUpdate,
|
||||
.render = sceneTestRender,
|
||||
.dispose = sceneTestDispose,
|
||||
.flags = 0
|
||||
};
|
||||
12
archive/scene/scenedata.h
Normal file
12
archive/scene/scenedata.h
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "scene/scene.h"
|
||||
|
||||
#include "scene/scene/scenetest.h"
|
||||
#include "scene/scene/scenemap.h"
|
||||
119
archive/scene/scenemanager.c
Normal file
119
archive/scene/scenemanager.c
Normal file
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "scenemanager.h"
|
||||
#include "util/memory.h"
|
||||
#include "assert/assert.h"
|
||||
#include "display/framebuffer.h"
|
||||
#include "util/string.h"
|
||||
#include "asset/asset.h"
|
||||
#include "script/scriptmanager.h"
|
||||
|
||||
scenemanager_t SCENE_MANAGER;
|
||||
|
||||
errorret_t sceneManagerInit(void) {
|
||||
memoryZero(&SCENE_MANAGER, sizeof(scenemanager_t));
|
||||
|
||||
sceneManagerRegisterScene(&SCENE_TEST);
|
||||
sceneManagerRegisterScene(&SCENE_MAP);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
scene_t * sceneManagerGetSceneByName(const char_t *name) {
|
||||
assertNotNull(name, "Name is null");
|
||||
|
||||
for(uint8_t i = 0; i < SCENE_MANAGER.sceneCount; i++) {
|
||||
if(strcmp(SCENE_MANAGER.scenes[i]->name, name) != 0) continue;
|
||||
return SCENE_MANAGER.scenes[i];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void sceneManagerRegisterScene(scene_t *scene) {
|
||||
assertNotNull(scene, "Scene is null");
|
||||
assertTrue(
|
||||
SCENE_MANAGER.sceneCount < SCENE_MANAGER_SCENE_COUNT_MAX,
|
||||
"Scene count exceeded max"
|
||||
);
|
||||
assertNotNull(scene->name, "Scene name is null");
|
||||
assertNull(
|
||||
sceneManagerGetSceneByName(scene->name), "Scene name already registered"
|
||||
);
|
||||
|
||||
SCENE_MANAGER.scenes[SCENE_MANAGER.sceneCount++] = scene;
|
||||
}
|
||||
|
||||
errorret_t sceneManagerSetScene(scene_t *scene) {
|
||||
if(
|
||||
SCENE_MANAGER.current &&
|
||||
(SCENE_MANAGER.current->flags & SCENE_FLAG_INITIALIZED) != 0
|
||||
) {
|
||||
SCENE_MANAGER.current->flags &= ~SCENE_FLAG_INITIALIZED;
|
||||
if(SCENE_MANAGER.current->dispose) {
|
||||
SCENE_MANAGER.current->dispose(&SCENE_MANAGER.sceneData);
|
||||
}
|
||||
}
|
||||
|
||||
SCENE_MANAGER.current = scene;
|
||||
|
||||
if(scene && (scene->flags & SCENE_FLAG_INITIALIZED) == 0) {
|
||||
scene->flags |= SCENE_FLAG_INITIALIZED;
|
||||
|
||||
if(scene->init) errorChain(scene->init(&SCENE_MANAGER.sceneData));
|
||||
|
||||
// Execute scene script if it exists
|
||||
char_t buffer[256];
|
||||
snprintf(buffer, sizeof(buffer), "scene/%s.dsf", scene->name);
|
||||
if(assetFileExists(buffer)) {
|
||||
scriptcontext_t ctx;
|
||||
scriptContextInit(&ctx);
|
||||
errorChain(scriptContextExecFile(&ctx, buffer));
|
||||
scriptContextDispose(&ctx);
|
||||
}
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void sceneManagerUpdate(void) {
|
||||
if(!SCENE_MANAGER.current) return;
|
||||
|
||||
assertTrue(
|
||||
SCENE_MANAGER.current->flags & SCENE_FLAG_INITIALIZED,
|
||||
"Current scene not initialized"
|
||||
);
|
||||
|
||||
if(SCENE_MANAGER.current->update) {
|
||||
SCENE_MANAGER.current->update(&SCENE_MANAGER.sceneData);
|
||||
}
|
||||
}
|
||||
|
||||
void sceneManagerRender(void) {
|
||||
if(!SCENE_MANAGER.current) return;
|
||||
|
||||
assertTrue(
|
||||
SCENE_MANAGER.current->flags & SCENE_FLAG_INITIALIZED,
|
||||
"Current scene not initialized"
|
||||
);
|
||||
|
||||
if(SCENE_MANAGER.current->render) {
|
||||
SCENE_MANAGER.current->render(&SCENE_MANAGER.sceneData);
|
||||
}
|
||||
}
|
||||
|
||||
void sceneManagerDispose(void) {
|
||||
for(uint8_t i = 0; i < SCENE_MANAGER.sceneCount; i++) {
|
||||
scene_t *scene = SCENE_MANAGER.scenes[i];
|
||||
if(scene->flags & SCENE_FLAG_INITIALIZED) {
|
||||
scene->dispose(&SCENE_MANAGER.sceneData);
|
||||
}
|
||||
}
|
||||
|
||||
SCENE_MANAGER.sceneCount = 0;
|
||||
}
|
||||
63
archive/scene/scenemanager.h
Normal file
63
archive/scene/scenemanager.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "scene.h"
|
||||
#include "scenedata.h"
|
||||
|
||||
#define SCENE_MANAGER_SCENE_COUNT_MAX 16
|
||||
|
||||
typedef struct {
|
||||
scene_t *current;
|
||||
scene_t *scenes[SCENE_MANAGER_SCENE_COUNT_MAX];
|
||||
uint8_t sceneCount;
|
||||
} scenemanager_t;
|
||||
|
||||
extern scenemanager_t SCENE_MANAGER;
|
||||
|
||||
/**
|
||||
* Initializes the scene manager and the initial scene.
|
||||
*/
|
||||
errorret_t sceneManagerInit(void);
|
||||
|
||||
/**
|
||||
* Retrieves a registered scene by its name.
|
||||
*
|
||||
* @param name The name of the scene to retrieve.
|
||||
* @return The scene with the specified name, or NULL if not found.
|
||||
*/
|
||||
scene_t * sceneManagerGetSceneByName(const char_t *name);
|
||||
|
||||
/**
|
||||
* Registers a scene with the scene manager.
|
||||
*
|
||||
* @param scene The scene to register.
|
||||
*/
|
||||
void sceneManagerRegisterScene(scene_t *scene);
|
||||
|
||||
/**
|
||||
* Sets the current active scene.
|
||||
*
|
||||
* @param scene The scene to set as current.
|
||||
* @return An error code indicating success or failure.
|
||||
*/
|
||||
errorret_t sceneManagerSetScene(scene_t *scene);
|
||||
|
||||
/**
|
||||
* Updates all active scenes.
|
||||
*/
|
||||
void sceneManagerUpdate(void);
|
||||
|
||||
/**
|
||||
* Renders all visible scenes.
|
||||
*/
|
||||
void sceneManagerRender(void);
|
||||
|
||||
/**
|
||||
* Disposes of all scenes.
|
||||
*/
|
||||
void sceneManagerDispose(void);
|
||||
Reference in New Issue
Block a user