Setup example shader.
This commit is contained in:
@ -8,6 +8,7 @@
|
||||
#include "matrix.h"
|
||||
|
||||
void matrixIdentity(matrix_t *matrix) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_mat4_identity(matrix->internalMatrix);
|
||||
}
|
||||
|
||||
@ -16,6 +17,7 @@ void matrixLookAt(matrix_t *matrix,
|
||||
float tx,float ty, float tz,
|
||||
float ux, float uy, float uz
|
||||
) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_lookat(
|
||||
(vec3){ x, y, z },
|
||||
(vec3){ tx, ty, tz },
|
||||
@ -29,6 +31,7 @@ void matrixLook(matrix_t *matrix,
|
||||
float pitch, float yaw, float roll,
|
||||
float ux, float uy, float uz
|
||||
) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_look(
|
||||
(vec3){ x, y, z },
|
||||
(vec3){ pitch, yaw, roll },
|
||||
@ -40,23 +43,48 @@ void matrixLook(matrix_t *matrix,
|
||||
void matrixPerspective(matrix_t *matrix,
|
||||
float fov, float aspect, float camNear, float camFar
|
||||
) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_perspective(fov, aspect, camNear, camFar, matrix->internalMatrix);
|
||||
}
|
||||
|
||||
void matrixOrtho(matrix_t *matrix,
|
||||
float left, float right, float bottom, float top, float camNear, float camFar
|
||||
) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_ortho(left, right, bottom, top, camNear, camFar, matrix->internalMatrix);
|
||||
}
|
||||
|
||||
void matrixTranslate(matrix_t *matrix, float x, float y, float z) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_translate(matrix->internalMatrix, (vec3){ x, y, z });
|
||||
}
|
||||
|
||||
void matrixRotate(matrix_t *matrix, float angle, float x, float y, float z) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_rotate(matrix->internalMatrix, angle, (vec3){ x, y, z });
|
||||
}
|
||||
|
||||
void matrixScale(matrix_t *matrix, float x, float y, float z) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_scale(matrix->internalMatrix, (vec3){ x, y, z });
|
||||
}
|
||||
|
||||
matrix_t matrixPositionAndScale(
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll,
|
||||
float scaleX, float scaleY, float scaleZ
|
||||
) {
|
||||
matrix_t matrix;
|
||||
|
||||
matrixIdentity(&matrix);
|
||||
matrixTranslate(&matrix, x, y, z);
|
||||
|
||||
// Rotation (YZX order)
|
||||
matrixRotate(&matrix, yaw, 0, 1, 0);
|
||||
matrixRotate(&matrix, roll, 0, 0, 1);
|
||||
matrixRotate(&matrix, pitch, 1, 0, 0);
|
||||
|
||||
matrixScale(&matrix, scaleX, scaleY, scaleZ);
|
||||
|
||||
return matrix;
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "../assert/assert.h"
|
||||
|
||||
/**
|
||||
* Representation for a matrix. Used as a highlevel wrapper for the math
|
||||
@ -120,4 +121,25 @@ void matrixRotate(matrix_t *matrix, float angle, float x, float y, float z);
|
||||
* @param y Y vector to scale.
|
||||
* @param z Z vector to scale.
|
||||
*/
|
||||
void matrixScale(matrix_t *matrix, float x, float y, float z);
|
||||
void matrixScale(matrix_t *matrix, float x, float y, float z);
|
||||
|
||||
/**
|
||||
* Calculates a fairly standard position and scale matrix. Note that the matrix
|
||||
* is calculated in the order of Translate, Yaw, Pitch, Roll, Scale.
|
||||
*
|
||||
* @param x X translation to apply.
|
||||
* @param y Y translation to apply.
|
||||
* @param z Z translation to apply.
|
||||
* @param pitch Pitch rotation to apply.
|
||||
* @param yaw Yaw rotation to apply.
|
||||
* @param roll Roll rotation to apply.
|
||||
* @param scaleX X scale factor.
|
||||
* @param scaleY Y scale factor.
|
||||
* @param scaleZ Z scale factor.
|
||||
* @return A specially created transformation matrix.
|
||||
*/
|
||||
matrix_t matrixPositionAndScale(
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll,
|
||||
float scaleX, float scaleY, float scaleZ
|
||||
);
|
@ -50,6 +50,7 @@ void renderListRender(renderlist_t *list, renderlistbackshader_t *backShader) {
|
||||
camera_t camera;
|
||||
int32_t i;
|
||||
renderpass_t *pass;
|
||||
matrix_t matrix;
|
||||
|
||||
// Setup the camera
|
||||
cameraLookAt(&camera, 0,0,1, 0,0,0);
|
||||
@ -64,9 +65,9 @@ void renderListRender(renderlist_t *list, renderlistbackshader_t *backShader) {
|
||||
backShader->program, backShader->uniformView,
|
||||
backShader->uniformProjection, &camera
|
||||
);
|
||||
shaderUsePosition(backShader->program, backShader->uniformPosition,
|
||||
0,0,0, 0,0,0
|
||||
);
|
||||
|
||||
matrix = matrixPositionAndScale(0,0,0, 0,0,0, 1,1,1);
|
||||
shaderUseMatrix(backShader->program, backShader->uniformPosition, &matrix);
|
||||
|
||||
// Render each pass.
|
||||
for(i = 0; i < list->passCount; i++) {
|
||||
|
@ -103,42 +103,6 @@ void shaderUseMatrix(
|
||||
glUniformMatrix4fv(uniform, 1, GL_FALSE, matrix->internalMatrix[0]);
|
||||
}
|
||||
|
||||
void shaderUsePosition(shaderprogram_t *shader, shaderuniform_t uniform,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll
|
||||
) {
|
||||
matrix_t matrix;
|
||||
|
||||
matrixIdentity(&matrix);
|
||||
matrixTranslate(&matrix, x, y, z);
|
||||
|
||||
// Rotation (YZX order)
|
||||
matrixRotate(&matrix, yaw, 0, 1, 0);
|
||||
matrixRotate(&matrix, roll, 0, 0, 1);
|
||||
matrixRotate(&matrix, pitch, 1, 0, 0);
|
||||
shaderUseMatrix(shader, uniform, &matrix);
|
||||
}
|
||||
|
||||
void shaderUsePositionAndScale(shaderprogram_t *shader, shaderuniform_t uniform,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll,
|
||||
float scaleX, float scaleY, float scaleZ
|
||||
) {
|
||||
matrix_t matrix;
|
||||
|
||||
matrixIdentity(&matrix);
|
||||
matrixTranslate(&matrix, x, y, z);
|
||||
|
||||
// Rotation (YZX order)
|
||||
matrixRotate(&matrix, yaw, 0, 1, 0);
|
||||
matrixRotate(&matrix, roll, 0, 0, 1);
|
||||
matrixRotate(&matrix, pitch, 1, 0, 0);
|
||||
|
||||
matrixScale(&matrix, scaleX, scaleY, scaleZ);
|
||||
|
||||
shaderUseMatrix(shader, uniform, &matrix);
|
||||
}
|
||||
|
||||
void shaderUseColor(
|
||||
shaderprogram_t *shader, shaderuniform_t uniform, pixel_t color
|
||||
) {
|
||||
|
@ -89,46 +89,6 @@ void shaderUseMatrix(
|
||||
shaderprogram_t *shader, shaderuniform_t uniform, matrix_t *matrix
|
||||
);
|
||||
|
||||
/**
|
||||
* Set's the current translation matrix onto the shader for the next
|
||||
* render to use. Rotation order is set to YZX.
|
||||
* @param shader Shader to attach to.
|
||||
* @param uniform Uniform to set the position on to.
|
||||
* @param x X coordinate (world space).
|
||||
* @param y Y coordinate (world space).
|
||||
* @param z Z coordinate (world space).
|
||||
* @param pitch Pitch of the object (local space).
|
||||
* @param yaw Yaw of the object (local space).
|
||||
* @param roll Roll of the object (local space).
|
||||
*/
|
||||
void shaderUsePosition(shaderprogram_t *shader, shaderuniform_t uniform,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Set's the current translation matrix onto the shader for the next
|
||||
* render to use. Also provides scaling controls.
|
||||
*
|
||||
* @param shader Shader to attach to.
|
||||
* @param uniform Uniform to set the position on to.
|
||||
* @param x X coordinate (world space).
|
||||
* @param y Y coordinate (world space).
|
||||
* @param z Z coordinate (world space).
|
||||
* @param pitch Pitch of the object (local space).
|
||||
* @param yaw Yaw of the object (local space).
|
||||
* @param roll Roll of the object (local space).
|
||||
* @param scaleX X scale of model (1 being 100% scaled).
|
||||
* @param scaleY Y scale of model (1 being 100% scaled).
|
||||
* @param scaleZ Z scale of model (1 being 100% scaled).
|
||||
*/
|
||||
void shaderUsePositionAndScale(shaderprogram_t *shader, shaderuniform_t uniform,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll,
|
||||
float scaleX, float scaleY, float scaleZ
|
||||
);
|
||||
|
||||
/**
|
||||
* Set a color on to the shader.
|
||||
*
|
||||
|
@ -1,11 +1,10 @@
|
||||
# Copyright (c) 2021 Dominic Msters
|
||||
# Copyright (c) 2022 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
# target_sources(${PROJECT_NAME}
|
||||
# PRIVATE
|
||||
# standardshader.c
|
||||
# shaderui.c
|
||||
# )
|
||||
target_sources(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
standardshader.c
|
||||
)
|
@ -1,12 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "shaderui.h"
|
||||
|
||||
void shaderUiInit(shaderprogram_t *program) {
|
||||
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../shaderprogram.h"
|
||||
#include "../texture.h"
|
||||
|
||||
typedef struct {
|
||||
shaderprogram_t program;
|
||||
texture_t texture;
|
||||
|
||||
shaderuniform_t uniProjection;
|
||||
shaderuniform_t uniModel;
|
||||
shaderuniform_t uniTexture;
|
||||
shaderuniform_t uniColor;
|
||||
} uishader_t;
|
||||
|
||||
void shaderUiInit(shaderprogram_t *program);
|
@ -7,5 +7,107 @@
|
||||
|
||||
#include "standardshader.h"
|
||||
|
||||
void standardShaderInit(standardshader_t *stds, char *vert, char *frag) {
|
||||
void _standardShaderUpdateUniforms(standardshader_t *stds) {
|
||||
shaderprogram_t *sp;
|
||||
ASSERT_NOT_NULL(stds);
|
||||
|
||||
sp = STANDARD_SHADER_SHADER(stds);
|
||||
stds->uniformView = shaderGetUniformByName(
|
||||
sp, STANDARD_SHADER_UNIFORM_NAME_VIEW
|
||||
);
|
||||
stds->uniformProjection = shaderGetUniformByName(
|
||||
sp, STANDARD_SHADER_UNIFORM_NAME_PROJ
|
||||
);
|
||||
stds->uniformTexture = shaderGetUniformByName(
|
||||
sp, STANDARD_SHADER_UNIFORM_NAME_TEXT
|
||||
);
|
||||
stds->uniformColor = shaderGetUniformByName(
|
||||
sp, STANDARD_SHADER_UNIFORM_NAME_COLOR
|
||||
);
|
||||
stds->uniformPosition = shaderGetUniformByName(
|
||||
sp, STANDARD_SHADER_UNIFORM_NAME_MODEL
|
||||
);
|
||||
}
|
||||
|
||||
bool _standardShaderOnAssetItemLoaded(void *u, event_t e, void *s[], int32_t c){
|
||||
standardshader_t *ss = (standardshader_t *)u;
|
||||
|
||||
ASSERT_NOT_NULL(ss);
|
||||
ASSERT_EQUAL(e, ASSET_MANAGER_EVENT_ITEM_LOADED);
|
||||
ASSERT_NOT_NULL(s);
|
||||
ASSERT_EQUAL(c, 2);
|
||||
|
||||
if(s[1] != ss->shaderProgram) return true;
|
||||
|
||||
eventManagerUnsubscribe(&((assetmanager_t *)s[0])->events, ss->onItemLoaded);
|
||||
_standardShaderUpdateUniforms(ss);
|
||||
return true;
|
||||
}
|
||||
|
||||
void standardShaderInit(standardshader_t *stds, assetmanager_t *assetManager) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
ASSERT_NOT_NULL(assetManager);
|
||||
|
||||
// Begin loading the shader.
|
||||
stds->holder = assetManagerHolderCreate(assetManager);
|
||||
stds->shaderProgram = assetManagerLoadShader(
|
||||
assetManager, stds->holder,
|
||||
STANDARD_SHARER_FILE_VERTEX, STANDARD_SHADER_FILE_FRAGMENT
|
||||
);
|
||||
|
||||
// Update uniforms
|
||||
if(assetManagerItemIsFinished(stds->shaderProgram)) {
|
||||
_standardShaderUpdateUniforms(stds);
|
||||
} else {
|
||||
stds->onItemLoaded = eventManagerSubscribe(
|
||||
&assetManager->events, ASSET_MANAGER_EVENT_ITEM_LOADED,
|
||||
stds, &_standardShaderOnAssetItemLoaded
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void standardShaderUse(standardshader_t *stds) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
|
||||
shaderUse(STANDARD_SHADER_SHADER(stds));
|
||||
}
|
||||
|
||||
void standardShaderSetCamera(standardshader_t *stds, camera_t *camera) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
ASSERT_NOT_NULL(camera);
|
||||
if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
|
||||
|
||||
shaderUseCamera(STANDARD_SHADER_SHADER(stds),
|
||||
stds->uniformView, stds->uniformProjection, camera
|
||||
);
|
||||
}
|
||||
|
||||
void standardShaderSetTexture(standardshader_t *stds, texture_t *texture) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
ASSERT_NOT_NULL(texture);
|
||||
if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
|
||||
|
||||
shaderUseTexture(STANDARD_SHADER_SHADER(stds), stds->uniformTexture, 0);
|
||||
}
|
||||
|
||||
void standardShaderSetColor(standardshader_t *stds, pixel_t color) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
|
||||
|
||||
shaderUseColor(STANDARD_SHADER_SHADER(stds), stds->uniformColor, color);
|
||||
}
|
||||
|
||||
void standardShaderSetPosition(standardshader_t *stds, matrix_t *matrix) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
|
||||
|
||||
shaderUseMatrix(STANDARD_SHADER_SHADER(stds), stds->uniformPosition, matrix);
|
||||
}
|
||||
|
||||
void standardShaderDispose(standardshader_t *stds, assetmanager_t *assMan) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
ASSERT_NOT_NULL(assMan);
|
||||
|
||||
assetManagerHolderRelease(assMan, stds->holder);
|
||||
}
|
@ -7,7 +7,10 @@
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "../../assert/assert.h"
|
||||
#include "../../engine/event.h"
|
||||
#include "../shaderprogram.h"
|
||||
#include "../../file/assetmanager.h"
|
||||
|
||||
#define STANDARD_SHADER_UNIFORM_NAME_VIEW "u_View"
|
||||
#define STANDARD_SHADER_UNIFORM_NAME_PROJ "u_Proj"
|
||||
@ -15,8 +18,15 @@
|
||||
#define STANDARD_SHADER_UNIFORM_NAME_MODEL "u_Model"
|
||||
#define STANDARD_SHADER_UNIFORM_NAME_COLOR "u_Color"
|
||||
|
||||
#define STANDARD_SHARER_FILE_VERTEX "shaders/textured.vert"
|
||||
#define STANDARD_SHADER_FILE_FRAGMENT "shaders/textured.frag"
|
||||
|
||||
#define STANDARD_SHADER_SHADER(ss) (&ss->shaderProgram->data.shader.shader)
|
||||
|
||||
typedef struct {
|
||||
shaderprogram_t shader;
|
||||
assetmanageritem_t *shaderProgram;
|
||||
assetmanagerholder_t holder;
|
||||
eventlistener_t *onItemLoaded;
|
||||
|
||||
shaderuniform_t uniformView;
|
||||
shaderuniform_t uniformProjection;
|
||||
@ -25,4 +35,10 @@ typedef struct {
|
||||
shaderuniform_t uniformColor;
|
||||
} standardshader_t;
|
||||
|
||||
void standardShaderInit(standardshader_t *stds, char *vert, char *frag);
|
||||
void standardShaderInit(standardshader_t *stds, assetmanager_t *assetManager);
|
||||
void standardShaderUse(standardshader_t *stds);
|
||||
void standardShaderSetCamera(standardshader_t *stds, camera_t *camera);
|
||||
void standardShaderSetTexture(standardshader_t *stds, texture_t *texture);
|
||||
void standardShaderSetColor(standardshader_t *stds, pixel_t color);
|
||||
void standardShaderSetPosition(standardshader_t *stds, matrix_t *matrix);
|
||||
void standardShaderDispose(standardshader_t *stds, assetmanager_t *assMan);
|
@ -11,38 +11,50 @@ assetmanagerloaderdefinition_t ASSET_MANAGER_LOADERS[] = {
|
||||
{
|
||||
&_assetManagerLoaderTextureAsync,
|
||||
&_assetManagerLoaderTextureSync,
|
||||
&_assetManagerLoaderTextureDispose,
|
||||
NULL
|
||||
&_assetManagerLoaderTextureDispose
|
||||
},
|
||||
{
|
||||
&_assetManagerLoaderFontAsync,
|
||||
&_assetManagerLoaderFontSync,
|
||||
&_assetManagerLoaderFontDispose,
|
||||
NULL
|
||||
&_assetManagerLoaderFontDispose
|
||||
},
|
||||
{
|
||||
&_assetManagerLoaderShaderAsync,
|
||||
&_assetManagerLoaderShaderSync,
|
||||
&_assetManagerLoaderShaderDispose,
|
||||
NULL
|
||||
&_assetManagerLoaderShaderDispose
|
||||
},
|
||||
{
|
||||
&_assetManagerLoaderScaledTextureAsync,
|
||||
&_assetManagerLoaderScaledTextureSync,
|
||||
&_assetManagerLoaderScaledTextureDispose,
|
||||
NULL
|
||||
&_assetManagerLoaderScaledTextureDispose
|
||||
}
|
||||
};
|
||||
|
||||
bool _assetManagerOnSaveManagerValueChange(
|
||||
void *usr, event_t evt, void *args[], int32_t argc
|
||||
) {
|
||||
ASSERT_EQUAL(argc, 3);
|
||||
|
||||
printf("Hello World\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void assetManagerInit(assetmanager_t *manager, savemanager_t *save) {
|
||||
threadInit(&manager->thread, &_assetManagerThread);
|
||||
eventManagerInit(&manager->events);
|
||||
|
||||
manager->thread.user = manager;
|
||||
manager->itemCount = 0;
|
||||
manager->finished = false;
|
||||
manager->holderCount = 0;
|
||||
manager->running = false;
|
||||
manager->save = save;
|
||||
|
||||
manager->listenerOnSaveValueChange = eventManagerSubscribe(
|
||||
&save->events, SAVE_EVENT_VALUE_CHANGED,
|
||||
manager, &_assetManagerOnSaveManagerValueChange
|
||||
);
|
||||
}
|
||||
|
||||
float assetManagerProgressGet(assetmanager_t *manager) {
|
||||
@ -53,15 +65,13 @@ float assetManagerProgressGet(assetmanager_t *manager) {
|
||||
done = 0.0f;
|
||||
for(i = 0; i < manager->itemCount; i++) {
|
||||
item = manager->items + i;
|
||||
done += assetManagerItemIsFinished(
|
||||
item, ASSET_MANAGER_LOADERS + item->type
|
||||
) ? 1 : 0;
|
||||
done += assetManagerItemIsFinished(item) ? 1 : 0;
|
||||
}
|
||||
return done / ((float)manager->itemCount);
|
||||
}
|
||||
|
||||
float assetManagerProgressGetForHolder(
|
||||
assetmanager_t *manager, assetmanagerowner_t hold
|
||||
assetmanager_t *manager, assetmanagerholder_t hold
|
||||
) {
|
||||
float done;
|
||||
uint8_t i, j, c;
|
||||
@ -75,9 +85,7 @@ float assetManagerProgressGetForHolder(
|
||||
for(j = 0; j < item->holderCount; j++) {
|
||||
if(item->holders[j] != hold) continue;
|
||||
c++;
|
||||
done += assetManagerItemIsFinished(
|
||||
item, ASSET_MANAGER_LOADERS + item->type
|
||||
) ? 1 : 0;
|
||||
done += assetManagerItemIsFinished(item) ? 1 : 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -86,13 +94,13 @@ float assetManagerProgressGetForHolder(
|
||||
return done / (float)c;
|
||||
}
|
||||
|
||||
assetmanagerowner_t assetManagerHolderCreate(assetmanager_t *man) {
|
||||
assetmanagerholder_t assetManagerHolderCreate(assetmanager_t *man) {
|
||||
uint8_t i;
|
||||
|
||||
// Find first available number.
|
||||
for(i = 0; i < 0xFF; i++) {
|
||||
if(arrayFind(
|
||||
sizeof(assetmanagerowner_t), man->holders, man->holderCount, &i
|
||||
sizeof(assetmanagerholder_t), man->holders, man->holderCount, &i
|
||||
) == -1) break;
|
||||
}
|
||||
|
||||
@ -100,13 +108,13 @@ assetmanagerowner_t assetManagerHolderCreate(assetmanager_t *man) {
|
||||
return i;// No slots left.
|
||||
}
|
||||
|
||||
void assetManagerHolderRelease(assetmanager_t *man, assetmanagerowner_t hold) {
|
||||
void assetManagerHolderRelease(assetmanager_t *man, assetmanagerholder_t hold) {
|
||||
int32_t i;
|
||||
uint8_t j;
|
||||
assetmanageritem_t *item;
|
||||
size_t s;
|
||||
|
||||
s = sizeof(assetmanagerowner_t);
|
||||
s = sizeof(assetmanagerholder_t);
|
||||
i = arrayFind(s, man->holders,man->holderCount,&hold);
|
||||
if(i == -1) return;
|
||||
|
||||
@ -142,6 +150,8 @@ void assetManagerDispose(assetmanager_t *man) {
|
||||
assetmanagerloader_t *disp;
|
||||
assetmanageritem_t *item;
|
||||
|
||||
eventManagerUnsubscribe(&man->save->events, man->listenerOnSaveValueChange);
|
||||
|
||||
for(i = 0; i < man->itemCount; i++) {
|
||||
item = man->items + i;
|
||||
disp = ASSET_MANAGER_LOADERS[item->type].dispose;
|
||||
@ -201,8 +211,11 @@ void assetManagerUpdate(assetmanager_t *manager) {
|
||||
uint8_t i;
|
||||
assetmanageritem_t *item;
|
||||
assetmanagerloaderdefinition_t *definition;
|
||||
void *args[2];
|
||||
bool result;
|
||||
|
||||
args[0] = manager;
|
||||
|
||||
// Autostart
|
||||
if(assetManagerProgressGet(manager) < 1.0f && !manager->running) {
|
||||
assetManagerStart(manager);
|
||||
@ -233,11 +246,16 @@ void assetManagerUpdate(assetmanager_t *manager) {
|
||||
item->state = ASSET_MANAGER_STATE_SYNC_LOADING;
|
||||
result = definition->loadSync(item);
|
||||
|
||||
// Finish loading
|
||||
// Finish loading, fire events
|
||||
if(!result) {
|
||||
item->state = ASSET_MANAGER_STATE_SYNC_ERROR;
|
||||
} else {
|
||||
item->state = ASSET_MANAGER_STATE_SYNC_DONE;
|
||||
|
||||
args[1] = item;
|
||||
eventManagerTrigger(
|
||||
&manager->events, ASSET_MANAGER_EVENT_ITEM_LOADED, args, 2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,9 +14,12 @@
|
||||
#include "loaders/texture.h"
|
||||
#include "asset.h"
|
||||
#include "../save/save.h"
|
||||
#include "../engine/event.h"
|
||||
|
||||
// Constants
|
||||
extern assetmanagerloaderdefinition_t ASSET_MANAGER_LOADERS[];
|
||||
/** Callback Listener for save manager value changes */
|
||||
bool _assetManagerOnSaveManagerValueChange(
|
||||
void *usr, event_t evt, void *args[], int32_t argc
|
||||
);
|
||||
|
||||
/**
|
||||
* Initialize the asset manager
|
||||
@ -42,7 +45,7 @@ float assetManagerProgressGet(assetmanager_t *manager);
|
||||
* @return The percentage (0-1) of the loaded assets.
|
||||
*/
|
||||
float assetManagerProgressGetForHolder(
|
||||
assetmanager_t *manager, assetmanagerowner_t hold
|
||||
assetmanager_t *manager, assetmanagerholder_t hold
|
||||
);
|
||||
|
||||
/**
|
||||
@ -54,7 +57,7 @@ float assetManagerProgressGetForHolder(
|
||||
* @param man Asset manager in question
|
||||
* @return An asset manager owner ID.
|
||||
*/
|
||||
assetmanagerowner_t assetManagerHolderCreate(assetmanager_t *man);
|
||||
assetmanagerholder_t assetManagerHolderCreate(assetmanager_t *man);
|
||||
|
||||
/**
|
||||
* Release a previously reserved asset manager holder. This will (in turn) cause
|
||||
@ -63,7 +66,7 @@ assetmanagerowner_t assetManagerHolderCreate(assetmanager_t *man);
|
||||
* @param man Asset manager holder to release for.
|
||||
* @param hold Holder to release.
|
||||
*/
|
||||
void assetManagerHolderRelease(assetmanager_t *man, assetmanagerowner_t hold);
|
||||
void assetManagerHolderRelease(assetmanager_t *man, assetmanagerholder_t hold);
|
||||
|
||||
/**
|
||||
* Disposes all assets that are not currently held (released assets). This can
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include "font.h"
|
||||
|
||||
assetmanageritem_t * assetManagerLoadFont(
|
||||
assetmanager_t *manager, assetmanagerowner_t owner, const char fileName[]
|
||||
assetmanager_t *manager, assetmanagerholder_t owner, const char fileName[]
|
||||
) {
|
||||
assetmanageritem_t *item;
|
||||
item = assetManagerItemGet(manager, fileName);
|
||||
|
@ -19,7 +19,7 @@
|
||||
* @return A pointer to the asset manager item for tracking.
|
||||
*/
|
||||
assetmanageritem_t * assetManagerLoadFont(
|
||||
assetmanager_t *manager, assetmanagerowner_t owner, const char fileName[]
|
||||
assetmanager_t *manager, assetmanagerholder_t owner, const char fileName[]
|
||||
);
|
||||
|
||||
bool _assetManagerLoaderFontAsync(assetmanageritem_t *item);
|
||||
|
@ -33,7 +33,7 @@ assetmanageritem_t * assetManagerItemAdd(
|
||||
}
|
||||
|
||||
uint8_t assetManagerItemGetOrAddHolder(
|
||||
assetmanageritem_t *item, assetmanagerowner_t owner
|
||||
assetmanageritem_t *item, assetmanagerholder_t owner
|
||||
) {
|
||||
uint8_t i, firstEmpty;
|
||||
firstEmpty = 0xFF;
|
||||
@ -50,7 +50,13 @@ uint8_t assetManagerItemGetOrAddHolder(
|
||||
return firstEmpty;
|
||||
}
|
||||
|
||||
bool assetManagerItemIsFinished(assetmanageritem_t *item, assetmanagerloaderdefinition_t *def) {
|
||||
bool assetManagerItemIsFinished(assetmanageritem_t *item) {
|
||||
assetmanagerloaderdefinition_t *def;
|
||||
|
||||
ASSERT_NOT_NULL(item);
|
||||
|
||||
def = ASSET_MANAGER_LOADERS +item->type;
|
||||
|
||||
// Sync done is always done
|
||||
if(item->state == ASSET_MANAGER_STATE_SYNC_DONE) return true;
|
||||
// Only check if ASYNC is done.
|
||||
|
@ -36,16 +36,13 @@ assetmanageritem_t * assetManagerItemAdd(
|
||||
* @return The index within the item that the owner is at.
|
||||
*/
|
||||
uint8_t assetManagerItemGetOrAddHolder(
|
||||
assetmanageritem_t *i, assetmanagerowner_t o
|
||||
assetmanageritem_t *i, assetmanagerholder_t o
|
||||
);
|
||||
|
||||
/**
|
||||
* Checks if a given asset item is finished or not.
|
||||
* Checks if a given asset item is finished loading or not.
|
||||
*
|
||||
* @param item Item to check.
|
||||
* @param def Item type definition used for loading.
|
||||
* @return True if finished, otherwise false.
|
||||
*/
|
||||
bool assetManagerItemIsFinished(
|
||||
assetmanageritem_t *item, assetmanagerloaderdefinition_t *def
|
||||
);
|
||||
bool assetManagerItemIsFinished(assetmanageritem_t *item);
|
@ -8,7 +8,7 @@
|
||||
#include "scaledtexture.h"
|
||||
|
||||
assetmanageritem_t * assetManagerLoadScaledTexture(
|
||||
assetmanager_t *manager, assetmanagerowner_t owner,
|
||||
assetmanager_t *manager, assetmanagerholder_t owner,
|
||||
const char path[], const char file[]
|
||||
) {
|
||||
assetmanageritem_t *item;
|
||||
@ -17,7 +17,7 @@ assetmanageritem_t * assetManagerLoadScaledTexture(
|
||||
char buffer[ASSET_MANAGER_ITEM_NAME_MAX];
|
||||
|
||||
// Get the scale
|
||||
scale = saveManagerGetUint8(manager->save, SAVE_KEY_TEXTURE_SCALE);
|
||||
scale = saveManagerGetU8(manager->save, SAVE_KEY_TEXTURE_SCALE);
|
||||
|
||||
// Generate a key
|
||||
sprintf(buffer, "%s/%s", path, file);
|
||||
@ -167,7 +167,7 @@ void assetManagerScaledTextureRescaleAll(assetmanager_t *manager) {
|
||||
uint8_t i, scale;
|
||||
|
||||
// Get the new scale
|
||||
scale = saveManagerGetUint8(manager->save, SAVE_KEY_TEXTURE_SCALE);
|
||||
scale = saveManagerGetU8(manager->save, SAVE_KEY_TEXTURE_SCALE);
|
||||
|
||||
// Rescale each texture. This is, unfortunately, blocking for now.
|
||||
for(i = 0; i < manager->itemCount; i++) {
|
||||
|
@ -21,7 +21,7 @@
|
||||
* @return A pointer to the asset manager item for tracking.
|
||||
*/
|
||||
assetmanageritem_t * assetManagerLoadScaledTexture(
|
||||
assetmanager_t *manager, assetmanagerowner_t owner,
|
||||
assetmanager_t *manager, assetmanagerholder_t owner,
|
||||
const char path[], const char file[]
|
||||
);
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include "shader.h"
|
||||
|
||||
assetmanageritem_t * assetManagerLoadShader(
|
||||
assetmanager_t *manager, assetmanagerowner_t owner,
|
||||
assetmanager_t *manager, assetmanagerholder_t owner,
|
||||
const char fileVert[], const char fileFrag[]
|
||||
) {
|
||||
assetmanageritem_t *item;
|
||||
|
@ -20,7 +20,7 @@
|
||||
* @return A pointer to the asset manager item for tracking.
|
||||
*/
|
||||
assetmanageritem_t * assetManagerLoadShader(
|
||||
assetmanager_t *manager, assetmanagerowner_t owner,
|
||||
assetmanager_t *manager, assetmanagerholder_t owner,
|
||||
const char fileVert[], const char fileFrag[]
|
||||
);
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include "texture.h"
|
||||
|
||||
assetmanageritem_t * assetManagerLoadTexture(
|
||||
assetmanager_t *manager, assetmanagerowner_t owner, const char fileName[]
|
||||
assetmanager_t *manager, assetmanagerholder_t owner, const char fileName[]
|
||||
) {
|
||||
assetmanageritem_t *item;
|
||||
item = assetManagerItemGet(manager, fileName);
|
||||
|
@ -19,7 +19,7 @@
|
||||
* @return A pointer to the asset manager item for tracking.
|
||||
*/
|
||||
assetmanageritem_t * assetManagerLoadTexture(
|
||||
assetmanager_t *manager, assetmanagerowner_t owner, const char fileName[]
|
||||
assetmanager_t *manager, assetmanagerholder_t owner, const char fileName[]
|
||||
);
|
||||
|
||||
bool _assetManagerLoaderTextureAsync(assetmanageritem_t *item);
|
||||
|
@ -8,13 +8,13 @@
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "../../engine/thread.h"
|
||||
#include "../../engine/event.h"
|
||||
#include "../../save/save.h"
|
||||
#include "texture.h"
|
||||
#include "font.h"
|
||||
#include "shader.h"
|
||||
#include "scaledtexture.h"
|
||||
|
||||
|
||||
#define ASSET_MANAGER_ITEMS_MAX 64
|
||||
#define ASSET_MANAGER_ITEM_NAME_MAX 96
|
||||
#define ASSET_MANAGER_HOLDERS_MAX 8
|
||||
@ -35,8 +35,11 @@
|
||||
#define ASSET_MANAGER_TYPE_SHADER 0x02
|
||||
#define ASSET_MANAGER_TYPE_SCALED_TEXTURE 0x03
|
||||
|
||||
/** Events */
|
||||
#define ASSET_MANAGER_EVENT_ITEM_LOADED 0x00
|
||||
|
||||
// Owner
|
||||
typedef uint8_t assetmanagerowner_t;
|
||||
typedef uint8_t assetmanagerholder_t;
|
||||
|
||||
// Union of all the manager types
|
||||
typedef union {
|
||||
@ -52,7 +55,7 @@ typedef struct {
|
||||
uint8_t state;
|
||||
const char *key;
|
||||
assetmanagerassetdata_t data;
|
||||
assetmanagerowner_t holders[ASSET_MANAGER_HOLDERS_MAX];
|
||||
assetmanagerholder_t holders[ASSET_MANAGER_HOLDERS_MAX];
|
||||
uint8_t holderCount;
|
||||
} assetmanageritem_t;
|
||||
|
||||
@ -64,15 +67,14 @@ typedef struct {
|
||||
assetmanagerloader_t *loadAsync;
|
||||
assetmanagerloader_t *loadSync;
|
||||
assetmanagerloader_t *dispose;
|
||||
assetmanagerloader_t *update;
|
||||
} assetmanagerloaderdefinition_t;
|
||||
|
||||
|
||||
|
||||
// Manager
|
||||
typedef struct {
|
||||
thread_t thread;
|
||||
savemanager_t *save;
|
||||
eventlistener_t *listenerOnSaveValueChange;
|
||||
eventmanager_t events;
|
||||
|
||||
bool finished;
|
||||
bool running;
|
||||
@ -80,6 +82,9 @@ typedef struct {
|
||||
assetmanageritem_t items[ASSET_MANAGER_ITEMS_MAX];
|
||||
uint8_t itemCount;
|
||||
|
||||
assetmanagerowner_t holders[ASSET_MANAGER_HOLDERS_MAX];
|
||||
assetmanagerholder_t holders[ASSET_MANAGER_HOLDERS_MAX];
|
||||
uint8_t holderCount;
|
||||
} assetmanager_t;
|
||||
} assetmanager_t;
|
||||
|
||||
// Constants
|
||||
extern assetmanagerloaderdefinition_t ASSET_MANAGER_LOADERS[];
|
@ -17,10 +17,7 @@ bool gameInit(game_t *game) {
|
||||
cameraPerspective(&game->camera, 45.0f, 16.0f/9.0f, 0.001f, 1000.0f);
|
||||
quadInit(&game->quad, 0.0f, 0.0f,0.0f,0.0f,0.0f, 1.0f,1.0f,1.0f,1.0f);
|
||||
|
||||
shaderInit(&game->shader,
|
||||
assetStringLoad("shaders/textured.vert"),
|
||||
assetStringLoad("shaders/textured.frag")
|
||||
);
|
||||
standardShaderInit(&game->shader, &game->engine.assetManager);
|
||||
|
||||
game->owner = assetManagerHolderCreate(&game->engine.assetManager);
|
||||
game->item = assetManagerLoadScaledTexture(
|
||||
@ -43,12 +40,17 @@ bool gameUpdate(game_t *game, float delta) {
|
||||
doneResize = true;
|
||||
}
|
||||
|
||||
matrix_t pos = matrixPositionAndScale(0,0,0, 0,0,0, 1,1,1);
|
||||
standardShaderUse(&game->shader);
|
||||
standardShaderSetCamera(&game->shader, &game->camera);
|
||||
standardShaderSetPosition(&game->shader, &pos);
|
||||
standardShaderSetColor(&game->shader, PIXEL_COLOR_WHITE);
|
||||
|
||||
if(game->item->state == ASSET_MANAGER_STATE_SYNC_DONE) {
|
||||
shaderUse(&game->shader);
|
||||
// shaderUseCamera(&game->shader, &game->camera);
|
||||
// shaderUsePosition(&game->shader, 0,0,0, 0,0,0);
|
||||
// shaderUseTexture(&game->shader, &game->item->data.scaledTexture.texture);
|
||||
// primitiveDraw(&game->quad, 0, -1);
|
||||
standardShaderSetTexture(
|
||||
&game->shader, &game->item->data.scaledTexture.texture
|
||||
);
|
||||
primitiveDraw(&game->quad, 0, -1);
|
||||
}
|
||||
|
||||
|
||||
|
@ -13,14 +13,14 @@
|
||||
#include "../../display/primitive/primitive.h"
|
||||
#include "../../display/primitive/quad.h"
|
||||
#include "../../display/primitive/cube.h"
|
||||
#include "../../display/shaderprogram.h"
|
||||
#include "../../display/shaders/standardshader.h"
|
||||
|
||||
typedef struct {
|
||||
engine_t engine;
|
||||
camera_t camera;
|
||||
shaderprogram_t shader;
|
||||
standardshader_t shader;
|
||||
primitive_t quad;
|
||||
assetmanagerowner_t owner;
|
||||
assetmanagerholder_t owner;
|
||||
assetmanageritem_t *item;
|
||||
} game_t;
|
||||
|
||||
|
@ -11,6 +11,7 @@ void saveManagerInit(savemanager_t *man) {
|
||||
ASSERT_NOT_NULL(man);
|
||||
|
||||
man->count = 0;
|
||||
eventManagerInit(&man->events);
|
||||
|
||||
// Set up defaults
|
||||
saveManagerSetU8(man, SAVE_KEY_TEXTURE_SCALE, 0x00);
|
||||
@ -20,8 +21,11 @@ savevalue_t * saveManagerAddOrGet(savemanager_t *man, const char key[]) {
|
||||
uint8_t i, j;
|
||||
|
||||
ASSERT_NOT_NULL(man);
|
||||
ASSERT_NOT_NULL(key);
|
||||
|
||||
j = saveManagerGetKey(man, key);
|
||||
ASSERT_IF(j >= SAVE_VALUES_MAX, 0xFF);
|
||||
ASSERT_IF(j < SAVE_VALUES_MAX, ASSERT_LESS_THAN(j, SAVE_VALUES_MAX));
|
||||
if(j != 0xFF) return man->values + j;
|
||||
|
||||
// Find empty key
|
||||
@ -38,14 +42,8 @@ savevalue_t * saveManagerAddOrGet(savemanager_t *man, const char key[]) {
|
||||
}
|
||||
|
||||
uint8_t saveManagerGetKey(savemanager_t *man, const char key[]) {
|
||||
uint8_t i;
|
||||
|
||||
ASSERT_NOT_NULL(man);
|
||||
|
||||
for(i = 0; i < man->count; i++) {
|
||||
if(strcmp(man->keys[i], key) == 0) return i;
|
||||
}
|
||||
return 0xFF;
|
||||
return (uint8_t) arrayFindString(man->keys, man->count, key);
|
||||
}
|
||||
|
||||
void saveManagerRemove(savemanager_t *man, const char key[]) {
|
||||
@ -60,13 +58,17 @@ void saveManagerRemove(savemanager_t *man, const char key[]) {
|
||||
man->keys[i] = NULL;
|
||||
}
|
||||
|
||||
/////////////////////////////////// Setters ////////////////////////////////////
|
||||
|
||||
// Setters
|
||||
savevalue_t * saveManagerSetBool(
|
||||
savemanager_t *man, const char key[], bool val
|
||||
) {
|
||||
savevalue_t *v = saveManagerAddOrGet(man, key);
|
||||
v->b = val;
|
||||
|
||||
void *evtArgs[3] = { man, key, &val };
|
||||
eventManagerTrigger(&man->events, SAVE_EVENT_VALUE_CHANGED, evtArgs, 3);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@ -75,10 +77,15 @@ savevalue_t * saveManagerSetU8(
|
||||
) {
|
||||
savevalue_t *v = saveManagerAddOrGet(man, key);
|
||||
v->u8 = val;
|
||||
|
||||
void *evtArgs[3] = { man, key, &val };
|
||||
eventManagerTrigger(&man->events, SAVE_EVENT_VALUE_CHANGED, evtArgs, 3);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
// Getters
|
||||
/////////////////////////////////// Getters ////////////////////////////////////
|
||||
|
||||
bool saveManagerGetBool(savemanager_t *man, const char key[]) {
|
||||
return saveManagerAddOrGet(man, key)->b;
|
||||
}
|
||||
|
@ -9,11 +9,14 @@
|
||||
#include "../libs.h"
|
||||
#include "../assert/assert.h"
|
||||
#include "../util/array.h"
|
||||
#include "../engine/event.h"
|
||||
|
||||
#define SAVE_VALUES_MAX 200
|
||||
|
||||
#define SAVE_KEY_TEXTURE_SCALE "TEXTURE_SCALE"
|
||||
|
||||
#define SAVE_EVENT_VALUE_CHANGED 0x00
|
||||
|
||||
typedef union {
|
||||
bool b;
|
||||
uint8_t u8;
|
||||
@ -26,19 +29,64 @@ typedef struct {
|
||||
const char *keys[SAVE_VALUES_MAX];
|
||||
savevalue_t values[SAVE_VALUES_MAX];
|
||||
uint8_t count;
|
||||
eventmanager_t events;
|
||||
} savemanager_t;
|
||||
|
||||
/**
|
||||
* Initialize the save manager.
|
||||
*
|
||||
* @param man Save manager to initialize.
|
||||
*/
|
||||
void saveManagerInit(savemanager_t *man);
|
||||
|
||||
/**
|
||||
* Either add, or return, a pointer to a specific save manager value.
|
||||
*
|
||||
* @param man Save manager instance.
|
||||
* @param key Key to get (or add).
|
||||
* @return A pointer to the save value.
|
||||
*/
|
||||
savevalue_t * saveManagerAddOrGet(savemanager_t *man, const char key[]);
|
||||
|
||||
/**
|
||||
* Get the index within the save manager that the given key is at. Returns 0xFF
|
||||
* if the key is not present within the save manager.
|
||||
*
|
||||
* @param man Save manager to check.
|
||||
* @param key Key to check for.
|
||||
* @return The index within the save manager that the key is at.
|
||||
*/
|
||||
uint8_t saveManagerGetKey(savemanager_t *man, const char key[]);
|
||||
|
||||
/**
|
||||
* Remove a key from the save manager.
|
||||
*
|
||||
* @param man Save manager to remove the key from.
|
||||
* @param key Key to remove.
|
||||
*/
|
||||
void saveManagerRemove(savemanager_t *man, const char key[]);
|
||||
|
||||
savevalue_t * saveManagerSetBool(savemanager_t *man,const char key[],bool val);
|
||||
/////////////////////////////////// Setters ////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Setter for Save Manager.
|
||||
*
|
||||
* @param man Save Manager instance.
|
||||
* @param key Key to set.
|
||||
* @param val Value to set.
|
||||
* @return The save value pointer.
|
||||
*/
|
||||
savevalue_t * saveManagerSetBool(savemanager_t *man,const char key[],bool val);
|
||||
savevalue_t * saveManagerSetU8(savemanager_t *man,const char key[],uint8_t val);
|
||||
|
||||
bool saveManagerGetBool(savemanager_t *man, char *key);
|
||||
uint8_t saveManagerGetU8(savemanager_t *man, char *key);
|
||||
/////////////////////////////////// Getters ////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Getter for the Save Manager.
|
||||
*
|
||||
* @param man Save Manager to get from.
|
||||
* @param key Key to get.
|
||||
* @return The value for that key, may be garbage if not previously set.
|
||||
*/
|
||||
bool saveManagerGetBool(savemanager_t *man, const char key[]);
|
||||
uint8_t saveManagerGetU8(savemanager_t *man, const char key[]);
|
@ -21,7 +21,7 @@ typedef void scenecallback_t(scene_t *scene);
|
||||
// Scene Definition
|
||||
typedef struct _scene_t {
|
||||
engine_t *engine;
|
||||
assetmanagerowner_t assetOwner;
|
||||
assetmanagerholder_t assetOwner;
|
||||
void *user;
|
||||
|
||||
/** Whether or not the assets that this scene depends upon have loaded */
|
||||
|
Reference in New Issue
Block a user