Scene rendering native.
This commit is contained in:
@@ -26,6 +26,8 @@
|
|||||||
#include "item/backpack.h"
|
#include "item/backpack.h"
|
||||||
#include "save/save.h"
|
#include "save/save.h"
|
||||||
|
|
||||||
|
#include "scene/initial/initialscene.h"
|
||||||
|
|
||||||
engine_t ENGINE;
|
engine_t ENGINE;
|
||||||
|
|
||||||
errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
||||||
@@ -55,7 +57,7 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
|||||||
|
|
||||||
/* Run the init script. */
|
/* Run the init script. */
|
||||||
consolePrint("Engine initialized");
|
consolePrint("Engine initialized");
|
||||||
// errorChain(scriptManagerExecFile("init.js", NULL));
|
sceneSet(SCENE_TYPE_INITIAL);
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,3 +6,20 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "initialscene.h"
|
#include "initialscene.h"
|
||||||
|
#include "console/console.h"
|
||||||
|
|
||||||
|
void initialSceneInit(void) {
|
||||||
|
consolePrint("Initial scene initialized");
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t initialSceneUpdate(void) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t initialSceneDraw(void) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
void initialSceneDispose(void) {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,16 +6,32 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "scene/scene.h"
|
#include "error/error.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *nothing;
|
||||||
|
} initialscene_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initial scene initializer.
|
||||||
|
*/
|
||||||
void initialSceneInit(void);
|
void initialSceneInit(void);
|
||||||
errorret_t initialSceneUpdate(void);
|
|
||||||
errorret_t initialSceneDraw(void);
|
|
||||||
void initialSceneDispose(void);
|
|
||||||
|
|
||||||
static const scene_t INITIAL_SCENE = {
|
/**
|
||||||
.init = initialSceneInit,
|
* Initial scene updater.
|
||||||
.update = initialSceneUpdate,
|
*
|
||||||
.draw = initialSceneDraw,
|
* @return Any error state that happened.
|
||||||
.dispose = initialSceneDispose
|
*/
|
||||||
};
|
errorret_t initialSceneUpdate(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initial scene drawer.
|
||||||
|
*
|
||||||
|
* @return Any error state that happened.
|
||||||
|
*/
|
||||||
|
errorret_t initialSceneDraw(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initial scene disposer.
|
||||||
|
*/
|
||||||
|
void initialSceneDispose(void);
|
||||||
+63
-122
@@ -18,15 +18,18 @@
|
|||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
#include "ui/ui.h"
|
#include "ui/ui.h"
|
||||||
|
|
||||||
scene_t SCENE_CURRENT;
|
scenefuncs_t SCENE_FUNCTIONS[SCENE_TYPE_COUNT] = {
|
||||||
scene_t SCENE_NEXT;
|
{ 0 },
|
||||||
bool_t SCENE_NEXT_SET;
|
#define X(structName, varName, varNameUpper, initFunc, updateFunc, renderFunc, disposeFunc) \
|
||||||
|
{ initFunc, updateFunc, renderFunc, disposeFunc },
|
||||||
|
#include "scene/scenelist.h"
|
||||||
|
#undef X
|
||||||
|
};
|
||||||
|
|
||||||
|
scene_t SCENE;
|
||||||
|
|
||||||
errorret_t sceneInit(void) {
|
errorret_t sceneInit(void) {
|
||||||
memoryZero(&SCENE_CURRENT, sizeof(scene_t));
|
memoryZero(&SCENE, sizeof(scene_t));
|
||||||
memoryZero(&SCENE_NEXT, sizeof(scene_t));
|
|
||||||
SCENE_NEXT_SET = false;
|
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,14 +40,22 @@ errorret_t sceneUpdate(void) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(SCENE_NEXT_SET) {
|
// Dispose current scene.
|
||||||
if(SCENE_CURRENT.dispose) SCENE_CURRENT.dispose();
|
if(SCENE.type != SCENE_TYPE_NULL) {
|
||||||
SCENE_CURRENT = SCENE_NEXT;
|
if(SCENE_FUNCTIONS[SCENE.type].dispose) {
|
||||||
SCENE_NEXT_SET = false;
|
SCENE_FUNCTIONS[SCENE.type].dispose();
|
||||||
if(SCENE_CURRENT.init) SCENE_CURRENT.init();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(SCENE_CURRENT.update != NULL) errorChain(SCENE_CURRENT.update());
|
// Init new scene
|
||||||
|
SCENE.type = SCENE.nextType;
|
||||||
|
SCENE.nextType = SCENE_TYPE_NULL;
|
||||||
|
|
||||||
|
if(SCENE.type != SCENE_TYPE_NULL) {
|
||||||
|
if(SCENE_FUNCTIONS[SCENE.type].init) {
|
||||||
|
SCENE_FUNCTIONS[SCENE.type].init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -52,131 +63,61 @@ errorret_t sceneUpdate(void) {
|
|||||||
dusktimeepoch_t LAST;
|
dusktimeepoch_t LAST;
|
||||||
|
|
||||||
errorret_t sceneRender(void) {
|
errorret_t sceneRender(void) {
|
||||||
entityid_t camEnts[ENTITY_COUNT_MAX];
|
mat4 view, proj;
|
||||||
componentid_t camComps[ENTITY_COUNT_MAX];
|
|
||||||
entityid_t camCount = componentGetEntitiesWithComponent(
|
|
||||||
COMPONENT_TYPE_CAMERA, camEnts, camComps
|
|
||||||
);
|
|
||||||
|
|
||||||
shader_t *shaderCurrent = NULL;
|
|
||||||
mat4 view, proj, model;
|
|
||||||
|
|
||||||
errorChain(displaySetState((displaystate_t){
|
|
||||||
// .flags = DISPLAY_STATE_FLAG_CULL | DISPLAY_STATE_FLAG_DEPTH_TEST
|
|
||||||
.flags = 0
|
|
||||||
}));
|
|
||||||
|
|
||||||
// For each camera
|
|
||||||
for(entityid_t camIndex = 0; camIndex < camCount; camIndex++) {
|
|
||||||
entityid_t camEnt = camEnts[camIndex];
|
|
||||||
componentid_t camComp = camComps[camIndex];
|
|
||||||
componentid_t camPos = entityGetComponent(camEnt, COMPONENT_TYPE_POSITION);
|
|
||||||
if(camPos == COMPONENT_ID_INVALID) {
|
|
||||||
logError("Camera entity without entity position found\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
entityCameraGetProjection(camEnt, camComp, proj);
|
|
||||||
entityPositionGetTransform(camEnt, camPos, view);
|
|
||||||
|
|
||||||
for(entityid_t entityId = 0; entityId < ENTITY_COUNT_MAX; entityId++) {
|
|
||||||
componentid_t renderComp = entityGetComponent(
|
|
||||||
entityId, COMPONENT_TYPE_RENDERABLE
|
|
||||||
);
|
|
||||||
if(renderComp == COMPONENT_ID_INVALID) continue;
|
|
||||||
|
|
||||||
entityrenderable_t *r = componentGetData(
|
|
||||||
entityId, renderComp, COMPONENT_TYPE_RENDERABLE
|
|
||||||
);
|
|
||||||
|
|
||||||
componentid_t posComp = entityGetComponent(
|
|
||||||
entityId, COMPONENT_TYPE_POSITION
|
|
||||||
);
|
|
||||||
if(posComp == COMPONENT_ID_INVALID) {
|
|
||||||
glm_mat4_identity(model);
|
|
||||||
} else {
|
|
||||||
entityPositionGetTransform(entityId, posComp, model);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch(r->type) {
|
|
||||||
case ENTITY_RENDERABLE_TYPE_CALLBACK: {
|
|
||||||
if(!r->callback) break;
|
|
||||||
errorChain(r->callback(
|
|
||||||
entityId, renderComp, view, proj, model, r->user
|
|
||||||
));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case ENTITY_RENDERABLE_TYPE_MATERIAL: {
|
|
||||||
shader_t *shader = r->shader;
|
|
||||||
if(!shader) break;
|
|
||||||
if(!r->mesh) break;
|
|
||||||
if(shaderCurrent != shader) {
|
|
||||||
shaderCurrent = shader;
|
|
||||||
errorChain(shaderBind(shaderCurrent));
|
|
||||||
errorChain(shaderSetMatrix(shader, SHADER_UNLIT_PROJECTION, proj));
|
|
||||||
errorChain(shaderSetMatrix(shader, SHADER_UNLIT_VIEW, view));
|
|
||||||
}
|
|
||||||
errorChain(shaderSetMatrix(shader, SHADER_UNLIT_MODEL, model));
|
|
||||||
errorChain(shaderSetMaterial(shader, &r->material));
|
|
||||||
errorChain(meshDraw(r->mesh, 0, -1));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case ENTITY_RENDERABLE_TYPE_SPRITEBATCH: {
|
|
||||||
if(r->spritebatch.spriteCount == 0) break;
|
|
||||||
shader_t *shader = r->shader;
|
|
||||||
if(!shader) break;
|
|
||||||
if(shaderCurrent != shader) {
|
|
||||||
shaderCurrent = shader;
|
|
||||||
errorChain(shaderBind(shaderCurrent));
|
|
||||||
errorChain(shaderSetMatrix(shader, SHADER_UNLIT_PROJECTION, proj));
|
|
||||||
errorChain(shaderSetMatrix(shader, SHADER_UNLIT_VIEW, view));
|
|
||||||
}
|
|
||||||
errorChain(shaderSetMatrix(shader, SHADER_UNLIT_MODEL, model));
|
|
||||||
for(uint16_t si = 0; si < r->spritebatch.spriteCount; si++) {
|
|
||||||
errorChain(spriteBatchBufferSprite(&r->spritebatch.sprites[si]));
|
|
||||||
}
|
|
||||||
errorChain(spriteBatchFlush());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// UI Rendering
|
|
||||||
glm_ortho(
|
glm_ortho(
|
||||||
0.0f, SCREEN.width,
|
0.0f, SCREEN.width,
|
||||||
SCREEN.height, 0.0f,
|
SCREEN.height, 0.0f,
|
||||||
0.1f, 100.0f,
|
0.1f, 100.0f,
|
||||||
proj
|
proj
|
||||||
);
|
);
|
||||||
glm_lookat(
|
|
||||||
(vec3){ 0.0f, 0.0f, 1.0f },
|
|
||||||
(vec3){ 0.0f, 0.0f, 0.0f },
|
|
||||||
(vec3){ 0.0f, 1.0f, 0.0f },
|
|
||||||
view
|
|
||||||
);
|
|
||||||
glm_mat4_identity(model);
|
|
||||||
|
|
||||||
errorChain(shaderBind(&SHADER_UNLIT));
|
errorChain(shaderBind(&SHADER_UNLIT));
|
||||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj));
|
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj));
|
||||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view));
|
|
||||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model));
|
|
||||||
errorChain(displaySetState((displaystate_t){
|
|
||||||
.flags = DISPLAY_STATE_FLAG_BLEND
|
|
||||||
}));
|
|
||||||
|
|
||||||
errorChain(uiRender());
|
if(SCENE.type != SCENE_TYPE_NULL && SCENE_FUNCTIONS[SCENE.type].render) {
|
||||||
|
glm_mat4_identity(view);
|
||||||
|
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view));
|
||||||
|
errorChain(SCENE_FUNCTIONS[SCENE.type].render());
|
||||||
|
}
|
||||||
|
|
||||||
|
// UI Rendering
|
||||||
|
{
|
||||||
|
glm_lookat(
|
||||||
|
(vec3){ 0.0f, 0.0f, 1.0f },
|
||||||
|
(vec3){ 0.0f, 0.0f, 0.0f },
|
||||||
|
(vec3){ 0.0f, 1.0f, 0.0f },
|
||||||
|
view
|
||||||
|
);
|
||||||
|
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view));
|
||||||
|
|
||||||
|
mat4 model;
|
||||||
|
glm_mat4_identity(model);
|
||||||
|
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model));
|
||||||
|
|
||||||
|
errorChain(displaySetState((displaystate_t){
|
||||||
|
.flags = DISPLAY_STATE_FLAG_BLEND
|
||||||
|
}));
|
||||||
|
errorChain(uiRender());
|
||||||
|
}
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sceneSet(const scene_t scene) {
|
void sceneSet(const scenetype_t type) {
|
||||||
SCENE_NEXT = scene;
|
assertTrue(
|
||||||
SCENE_NEXT_SET = true;
|
type > SCENE_TYPE_NULL && type < SCENE_TYPE_COUNT,
|
||||||
|
"Invalid scene type"
|
||||||
|
);
|
||||||
|
SCENE.nextType = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t sceneDispose(void) {
|
errorret_t sceneDispose(void) {
|
||||||
|
if(SCENE.type != SCENE_TYPE_NULL) {
|
||||||
|
if(SCENE_FUNCTIONS[SCENE.type].dispose) {
|
||||||
|
SCENE_FUNCTIONS[SCENE.type].dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SCENE.type = SCENE_TYPE_NULL;
|
||||||
|
SCENE.nextType = SCENE_TYPE_NULL;
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|||||||
+28
-5
@@ -7,18 +7,41 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "asset/assetfile.h"
|
#include "asset/assetfile.h"
|
||||||
|
#include "scene/initial/initialscene.h"
|
||||||
|
|
||||||
#define SCENE_EVENT_UPDATE_MAX 16
|
#define SCENE_EVENT_UPDATE_MAX 16
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
SCENE_TYPE_NULL,
|
||||||
|
#define X(structName, varName, varNameUpper, initFunc, updateFunc, renderFunc, disposeFunc) \
|
||||||
|
SCENE_TYPE_##varNameUpper,
|
||||||
|
#include "scene/scenelist.h"
|
||||||
|
#undef X
|
||||||
|
SCENE_TYPE_COUNT
|
||||||
|
} scenetype_t;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
#define X(structName, varName, varNameUpper, initFunc, updateFunc, renderFunc, disposeFunc) \
|
||||||
|
structName varName;
|
||||||
|
#include "scene/scenelist.h"
|
||||||
|
#undef X
|
||||||
|
} scenedata_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
void (*init)(void);
|
void (*init)(void);
|
||||||
errorret_t (*update)(void);
|
errorret_t (*update)(void);
|
||||||
|
errorret_t (*render)(void);
|
||||||
void (*dispose)(void);
|
void (*dispose)(void);
|
||||||
|
} scenefuncs_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
scenedata_t data;
|
||||||
|
scenetype_t type;
|
||||||
|
scenetype_t nextType;
|
||||||
} scene_t;
|
} scene_t;
|
||||||
|
|
||||||
extern scene_t SCENE_CURRENT;
|
extern scenefuncs_t SCENE_FUNCTIONS[SCENE_TYPE_COUNT];
|
||||||
extern scene_t SCENE_NEXT;
|
extern scene_t SCENE;
|
||||||
extern bool_t SCENE_NEXT_SET;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the scene manager.
|
* Initializes the scene manager.
|
||||||
@@ -44,9 +67,9 @@ errorret_t sceneRender(void);
|
|||||||
/**
|
/**
|
||||||
* Requests a scene change on the next safe opportunity.
|
* Requests a scene change on the next safe opportunity.
|
||||||
*
|
*
|
||||||
* @param scene Which scene to set.
|
* @param type The type of scene to change to.
|
||||||
*/
|
*/
|
||||||
void sceneSet(const scene_t scene);
|
void sceneSet(const scenetype_t type);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disposes of the current scene.
|
* Disposes of the current scene.
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef X
|
||||||
|
#define X(structName, varName, varNameUpper, initFunc, updateFunc, renderFunc, disposeFunc) \
|
||||||
|
((void))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
X(initialscene_t, initial, INITIAL, initialSceneInit, initialSceneUpdate, initialSceneDraw, initialSceneDispose)
|
||||||
@@ -207,6 +207,8 @@ void uiTextboxNextPage(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
errorret_t uiTextboxDraw(void) {
|
errorret_t uiTextboxDraw(void) {
|
||||||
|
if(UI_TEXTBOX.lineCount == 0 || UI_TEXTBOX.text[0] == '\0') errorOk();
|
||||||
|
|
||||||
errorChain(uiFrameDraw(
|
errorChain(uiFrameDraw(
|
||||||
&UI_TEXTBOX.frame,
|
&UI_TEXTBOX.frame,
|
||||||
UI_TEXTBOX.x, UI_TEXTBOX.y,
|
UI_TEXTBOX.x, UI_TEXTBOX.y,
|
||||||
@@ -214,8 +216,6 @@ errorret_t uiTextboxDraw(void) {
|
|||||||
));
|
));
|
||||||
errorChain(spriteBatchFlush());
|
errorChain(spriteBatchFlush());
|
||||||
|
|
||||||
if(UI_TEXTBOX.lineCount == 0 || UI_TEXTBOX.text[0] == '\0') errorOk();
|
|
||||||
|
|
||||||
errorChain(shaderSetTexture(
|
errorChain(shaderSetTexture(
|
||||||
&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, &UI_TEXTBOX.font->texture
|
&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, &UI_TEXTBOX.font->texture
|
||||||
));
|
));
|
||||||
|
|||||||
Reference in New Issue
Block a user