Entity positions.
This commit is contained in:
		| @@ -5,4 +5,7 @@ | ||||
|  * https://opensource.org/licenses/MIT | ||||
|  */ | ||||
|  | ||||
| #define OVERWORLD_ENTITY_WIDTH 32 | ||||
| #define OVERWORLD_ENTITY_HEIGHT 32 | ||||
|  | ||||
| #define OVERWORLD_ENTITY_COUNT_MAX 32 | ||||
| @@ -7,6 +7,7 @@ | ||||
|  | ||||
| #include "entity.h" | ||||
| #include "assert/assert.h" | ||||
| #include "input.h" | ||||
|  | ||||
| void playerInit(entity_t *ent) { | ||||
|   assertNotNull(ent, "Entity cannot be NULL"); | ||||
| @@ -16,4 +17,14 @@ void playerInit(entity_t *ent) { | ||||
|  | ||||
| void playerUpdate(entity_t *ent) { | ||||
|   assertNotNull(ent, "Entity cannot be NULL"); | ||||
|  | ||||
|   if(inputWasPressed(INPUT_RIGHT)) { | ||||
|     ent->x++; | ||||
|   } else if(inputWasPressed(INPUT_LEFT)) { | ||||
|     ent->x--; | ||||
|   } else if(inputWasPressed(INPUT_UP)) { | ||||
|     ent->y--; | ||||
|   } else if(inputWasPressed(INPUT_DOWN)) { | ||||
|     ent->y++; | ||||
|   } | ||||
| } | ||||
| @@ -7,4 +7,5 @@ | ||||
| target_sources(${DUSK_TARGET_NAME} | ||||
|   PRIVATE | ||||
|     transforms.c | ||||
|     entities.c | ||||
| ) | ||||
							
								
								
									
										40
									
								
								src/duskgl/display/shader/data/entities.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								src/duskgl/display/shader/data/entities.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| /** | ||||
|  * Copyright (c) 2025 Dominic Masters | ||||
|  *  | ||||
|  * This software is released under the MIT License. | ||||
|  * https://opensource.org/licenses/MIT | ||||
|  */ | ||||
|  | ||||
| #include "entities.h" | ||||
| #include "overworld/overworld.h" | ||||
| #include "assert/assert.h" | ||||
|  | ||||
| shaderbuffer_t ENTITIES_BUFFER; | ||||
| entitiesdata_t ENTITIES_DATA; | ||||
|  | ||||
| void entitiesInit() { | ||||
|   memset(&ENTITIES_DATA, 0, sizeof(entitiesdata_t)); | ||||
|   shaderBufferInit(&ENTITIES_BUFFER, sizeof(overworld_t)); | ||||
| } | ||||
|  | ||||
| void entitiesUpdate() { | ||||
|   shaderBufferBind(&ENTITIES_BUFFER); | ||||
|   shaderBufferSetData(&ENTITIES_BUFFER, &OVERWORLD); | ||||
|  | ||||
|   for(uint8_t i = 0; i < OVERWORLD.entityCount; i++) { | ||||
|     // Pack the entity data into the buffer | ||||
|     entity_t *src = &OVERWORLD.entities[i]; | ||||
|     entitiesdataent_t *dst = &ENTITIES_DATA.entities[i]; | ||||
|  | ||||
|     // Copy position data. | ||||
|     assertTrue( | ||||
|       (&src->subY - &src->x) + sizeof(uint8_t) == sizeof(uint_t), | ||||
|       "Entity data is not packed correctly any more." | ||||
|     ); | ||||
|     memcpy(&dst->position, &src->x, sizeof(uint_t)); | ||||
|   } | ||||
| } | ||||
|  | ||||
| void entitiesDispose() { | ||||
|   shaderBufferDispose(&ENTITIES_BUFFER); | ||||
| } | ||||
							
								
								
									
										41
									
								
								src/duskgl/display/shader/data/entities.glsl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/duskgl/display/shader/data/entities.glsl
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| // Copyright (c) 2025 Dominic Masters | ||||
| //  | ||||
| // This software is released under the MIT License. | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #include "../../../../dusk/overworld/overworlddefs.h" | ||||
| #include "../fragments/packed.glsl" | ||||
| #include "../fragments/quad.glsl" | ||||
|  | ||||
| struct Entity { | ||||
|   uint position; | ||||
|   uvec3 _padding0; | ||||
| }; | ||||
|  | ||||
| layout(std140) uniform b_Entities { | ||||
|   Entity entities[OVERWORLD_ENTITY_COUNT_MAX]; | ||||
| }; | ||||
|  | ||||
| vec2 entityGetSize() { | ||||
|   return vec2(float(OVERWORLD_ENTITY_WIDTH), float(OVERWORLD_ENTITY_HEIGHT)); | ||||
| } | ||||
|  | ||||
| vec2 entityGetVertice(uint instanceIndex, uint indiceIndex) { | ||||
|   // Get base quad vertice | ||||
|   vec2 vert = quadGetVertice(indiceIndex); | ||||
|  | ||||
|   uint x = packedGetU8(0u, entities[instanceIndex].position); | ||||
|   uint y = packedGetU8(1u, entities[instanceIndex].position); | ||||
|   uint subX = packedGetU8(2u, entities[instanceIndex].position); | ||||
|   uint subY = packedGetU8(3u, entities[instanceIndex].position); | ||||
|  | ||||
|   vert.x += float(x); | ||||
|   vert.y += float(y); | ||||
|  | ||||
|   vert *= entityGetSize(); | ||||
|  | ||||
|   vert.x += float(subX); | ||||
|   vert.y += float(subY); | ||||
|  | ||||
|   return vert; | ||||
| } | ||||
| @@ -5,3 +5,35 @@ | ||||
|  * https://opensource.org/licenses/MIT | ||||
|  */ | ||||
|  | ||||
| #pragma once | ||||
| #include "display/shader/shaderbuffer.h" | ||||
| #include "overworld/overworld.h" | ||||
|  | ||||
| #define ENTITIES_BLOCK_NAME "b_Entities" | ||||
|  | ||||
| typedef struct { | ||||
|   uint_t position; | ||||
|   uvec3_t _padding0; | ||||
| } entitiesdataent_t; | ||||
|  | ||||
| typedef struct { | ||||
|   entitiesdataent_t entities[OVERWORLD_ENTITY_COUNT_MAX]; | ||||
| } entitiesdata_t; | ||||
|  | ||||
| extern shaderbuffer_t ENTITIES_BUFFER; | ||||
| extern entitiesdata_t ENTITIES_DATA; | ||||
|  | ||||
| /** | ||||
|  * Initializes the entities buffer and data. | ||||
|  */ | ||||
| void entitiesInit(); | ||||
|  | ||||
| /** | ||||
|  * Updates the entities buffer with the current data. | ||||
|  */ | ||||
| void entitiesUpdate(); | ||||
|  | ||||
| /** | ||||
|  * Destroys the entities buffer. | ||||
|  */ | ||||
| void entitiesDispose(); | ||||
| @@ -29,13 +29,13 @@ void transformsUpdate() { | ||||
|   glm_perspective( | ||||
|     glm_rad(45.0f), | ||||
|     TRANSFORMS_DATA.resolution[0] / TRANSFORMS_DATA.resolution[1], | ||||
|     1.0f, | ||||
|     100.0f, | ||||
|     0.5f, | ||||
|     1000.0f, | ||||
|     TRANSFORMS_DATA.projection | ||||
|   ); | ||||
|  | ||||
|   glm_lookat( | ||||
|     (vec3){ 3, 3, 3 }, | ||||
|     (vec3){ 300, 300, 300 }, | ||||
|     (vec3){ 0, 0, 0 }, | ||||
|     (vec3){ 0, 1, 0 }, | ||||
|     TRANSFORMS_DATA.view | ||||
|   | ||||
| @@ -1,16 +0,0 @@ | ||||
| // Copyright (c) 2025 Dominic Masters | ||||
| //  | ||||
| // This software is released under the MIT License. | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #include "../fragments/quad.glsl" | ||||
| #include "../../../../dusk/overworld/overworlddefs.h" | ||||
|  | ||||
| struct Entity { | ||||
|   vec2 position; | ||||
| }; | ||||
|  | ||||
| layout(std140) uniform b_Entities { | ||||
|   Entity entities[OVERWORLD_ENTITY_COUNT_MAX]; | ||||
|   uint entityCount; | ||||
| }; | ||||
| @@ -4,7 +4,7 @@ | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #include "../fragments/header.glsl" | ||||
| #include "entity.glsl" | ||||
| #include "../data/entities.glsl" | ||||
|  | ||||
| // Inputs from vertex shader | ||||
| in vec2 v_TextureCoord; | ||||
|   | ||||
| @@ -4,9 +4,8 @@ | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #include "../fragments/header.glsl" | ||||
| #include "../fragments/transforms.glsl" | ||||
| #include "../fragments/quad.glsl" | ||||
| #include "entity.glsl" | ||||
| #include "../data/transforms.glsl" | ||||
| #include "../data/entities.glsl" | ||||
|  | ||||
| // Outputs to fragment shader | ||||
| out vec2 v_TextureCoord; | ||||
| @@ -15,7 +14,7 @@ void main() { | ||||
|   uint instanceIndex = uint(gl_InstanceID); | ||||
|   uint indiceIndex = uint(gl_VertexID % 6); | ||||
|  | ||||
|   vec2 vert = quadGetVertice(indiceIndex); | ||||
|   vec2 vert = entityGetVertice(instanceIndex, indiceIndex); | ||||
|   vec2 uv = quadGetTextureCoordinate(indiceIndex); | ||||
|  | ||||
|   gl_Position = transforms.projection * transforms.view * vec4(vert, 0.0, 1.0); | ||||
|   | ||||
| @@ -11,6 +11,7 @@ | ||||
| #include "entity_vert.glsl.h" | ||||
| #include "entity_frag.glsl.h" | ||||
| #include "display/shader/data/transforms.h" | ||||
| #include "display/shader/data/entities.h" | ||||
|  | ||||
| entityshader_t ENTITY_SHADER; | ||||
|  | ||||
| @@ -29,10 +30,16 @@ void entityShaderInit() { | ||||
|     TRANSFORMS_BLOCK_NAME | ||||
|   ); | ||||
|  | ||||
|   ENTITY_SHADER.entitiesBlock = shaderGetBlock( | ||||
|     &ENTITY_SHADER.shader, | ||||
|     ENTITIES_BLOCK_NAME | ||||
|   ); | ||||
|  | ||||
|   // Uniforms | ||||
|  | ||||
|   // Bind | ||||
|   shaderBufferBindToBlock(&TRANSFORMS_BUFFER, ENTITY_SHADER.transformsBlock); | ||||
|   shaderBufferBindToBlock(&ENTITIES_BUFFER, ENTITY_SHADER.entitiesBlock); | ||||
| } | ||||
|  | ||||
| void entityShaderUse() { | ||||
|   | ||||
| @@ -10,12 +10,8 @@ | ||||
|  | ||||
| typedef struct { | ||||
|   shader_t shader; | ||||
|    | ||||
|   GLuint entitiesBlock; | ||||
|   GLuint transformsBlock; | ||||
|   // GLuint tilesetsBlock; | ||||
|    | ||||
|   // GLuint uniformTilesetTextures; | ||||
| } entityshader_t; | ||||
|  | ||||
| extern entityshader_t ENTITY_SHADER; | ||||
|   | ||||
							
								
								
									
										8
									
								
								src/duskgl/display/shader/fragments/packed.glsl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/duskgl/display/shader/fragments/packed.glsl
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| // Copyright (c) 2025 Dominic Masters | ||||
| //  | ||||
| // This software is released under the MIT License. | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| uint packedGetU8(uint position, uint data) { | ||||
|   return (data >> (position * 8u)) & 0xFFu; | ||||
| } | ||||
| @@ -8,10 +8,12 @@ | ||||
| #include "shadermanager.h" | ||||
| #include "assert/assert.h" | ||||
| #include "display/shader/data/transforms.h" | ||||
| #include "display/shader/data/entities.h" | ||||
| #include "display/shader/entityshader/entityshader.h" | ||||
|  | ||||
| shadermanagerdatacallback_t SHADER_MANAGER_DATA_CALLBACKS[] = { | ||||
|   { transformsInit, transformsUpdate, transformsDispose } | ||||
|   { transformsInit, transformsUpdate, transformsDispose }, | ||||
|   { entitiesInit, entitiesUpdate, entitiesDispose } | ||||
| }; | ||||
|  | ||||
|  | ||||
| @@ -30,6 +32,7 @@ void shaderManagerInit() { | ||||
|     "No Data Callbacks Defined" | ||||
|   ); | ||||
|  | ||||
|   // Init Shader Datas (before the shaders) | ||||
|   i = 0; | ||||
|   do { | ||||
|     assertNotNull( | ||||
| @@ -39,6 +42,7 @@ void shaderManagerInit() { | ||||
|     SHADER_MANAGER_DATA_CALLBACKS[i++].init(); | ||||
|   } while(i < SHADER_MANAGER_DATA_CALLBACKS_SIZE); | ||||
|  | ||||
|   // Init the shaders | ||||
|   i = 0; | ||||
|   do { | ||||
|     assertNotNull( | ||||
| @@ -55,6 +59,8 @@ void shaderManagerUpdate() { | ||||
|     "No Data Callbacks Defined" | ||||
|   ); | ||||
|  | ||||
|  | ||||
|   // Update all the data | ||||
|   size_t i = 0; | ||||
|   do { | ||||
|     assertNotNull( | ||||
| @@ -75,6 +81,7 @@ void shaderManagerDispose() { | ||||
|     "No Data Callbacks Defined" | ||||
|   ); | ||||
|  | ||||
|   // Cleanup the shaders | ||||
|   size_t i = 0; | ||||
|   do { | ||||
|     assertNotNull( | ||||
| @@ -83,4 +90,14 @@ void shaderManagerDispose() { | ||||
|     ); | ||||
|     SHADER_MANAGER_SHADER_CALLBACKS[i++].dispose(); | ||||
|   } while(i < SHADER_MANAGER_SHADER_CALLBACKS_SIZE); | ||||
|  | ||||
|   // Cleanup the data | ||||
|   i = 0; | ||||
|   do { | ||||
|     assertNotNull( | ||||
|       SHADER_MANAGER_DATA_CALLBACKS[i].dispose, | ||||
|       "Data Callback is NULL" | ||||
|     ); | ||||
|     SHADER_MANAGER_DATA_CALLBACKS[i++].dispose(); | ||||
|   } while(i < SHADER_MANAGER_DATA_CALLBACKS_SIZE); | ||||
| } | ||||
| @@ -20,3 +20,8 @@ typedef double double_t; | ||||
|  | ||||
| extern char_t EXECUTABLE_PATH[]; | ||||
| extern char_t EXECUTABLE_DIRECTORY[]; | ||||
|  | ||||
| typedef uint32_t uvec4_t[4]; | ||||
| typedef uint32_t uvec3_t[3]; | ||||
| typedef uint32_t uvec2_t[2]; | ||||
| typedef uint32_t uint_t; | ||||
		Reference in New Issue
	
	Block a user