Map shader base, add memory util.
This commit is contained in:
@ -82,43 +82,43 @@ void assertDeprecatedImpl(
|
|||||||
assertUnreachableImpl(file, line, message);
|
assertUnreachableImpl(file, line, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void assertMemoryRangeMatchesImpl(
|
// void assertMemoryRangeMatchesImpl(
|
||||||
const char *file,
|
// const char *file,
|
||||||
const int32_t line,
|
// const int32_t line,
|
||||||
const void *start,
|
// const void *start,
|
||||||
const void *end,
|
// const void *end,
|
||||||
const size_t size,
|
// const size_t size,
|
||||||
const char *message
|
// const char *message
|
||||||
) {
|
// ) {
|
||||||
assertTrueImpl(
|
// assertTrueImpl(
|
||||||
file,
|
// file,
|
||||||
line,
|
// line,
|
||||||
start != NULL,
|
// start != NULL,
|
||||||
"Start pointer is NULL"
|
// "Start pointer is NULL"
|
||||||
);
|
// );
|
||||||
assertTrueImpl(
|
// assertTrueImpl(
|
||||||
file,
|
// file,
|
||||||
line,
|
// line,
|
||||||
end != NULL,
|
// end != NULL,
|
||||||
"End pointer is NULL"
|
// "End pointer is NULL"
|
||||||
);
|
// );
|
||||||
assertTrueImpl(
|
// assertTrueImpl(
|
||||||
file,
|
// file,
|
||||||
line,
|
// line,
|
||||||
size > 0,
|
// size > 0,
|
||||||
"Size is 0"
|
// "Size is 0"
|
||||||
);
|
// );
|
||||||
assertTrueImpl(
|
// assertTrueImpl(
|
||||||
file,
|
// file,
|
||||||
line,
|
// line,
|
||||||
start < end,
|
// start < end,
|
||||||
"Start pointer is not before end pointer"
|
// "Start pointer is not before end pointer"
|
||||||
);
|
// );
|
||||||
|
|
||||||
assertTrueImpl(
|
// assertTrueImpl(
|
||||||
file,
|
// file,
|
||||||
line,
|
// line,
|
||||||
((uintptr_t)end - (uintptr_t)start) == size,
|
// ((uintptr_t)end - (uintptr_t)start) == size,
|
||||||
message
|
// message
|
||||||
);
|
// );
|
||||||
}
|
// }
|
@ -127,6 +127,3 @@ void assertMemoryRangeMatchesImpl(
|
|||||||
|
|
||||||
#define assertStrLenMin(str, len, message) \
|
#define assertStrLenMin(str, len, message) \
|
||||||
assertTrue(strlen(str) >= len, message)
|
assertTrue(strlen(str) >= len, message)
|
||||||
|
|
||||||
#define assertMemoryRangeMatches(start, end, size, message) \
|
|
||||||
assertMemoryRangeMatchesImpl(__FILE__, __LINE__, start, end, size, message)
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "scene.h"
|
#include "scene.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
#include "overworld/overworld.h"
|
#include "overworld/overworld.h"
|
||||||
|
|
||||||
scenetypecallback_t SCENE_CALLBACKS[] = {
|
scenetypecallback_t SCENE_CALLBACKS[] = {
|
||||||
@ -26,7 +27,7 @@ scenetypecallback_t SCENE_CALLBACKS[] = {
|
|||||||
scene_t SCENE;
|
scene_t SCENE;
|
||||||
|
|
||||||
void sceneInit() {
|
void sceneInit() {
|
||||||
memset(&SCENE, 0, sizeof(scene_t));
|
memoryZero(&SCENE, sizeof(scene_t));
|
||||||
|
|
||||||
for(uint8_t i = 0; i < SCENE_TYPE_COUNT; i++) {
|
for(uint8_t i = 0; i < SCENE_TYPE_COUNT; i++) {
|
||||||
if(SCENE_CALLBACKS[i].onInit) SCENE_CALLBACKS[i].onInit();
|
if(SCENE_CALLBACKS[i].onInit) SCENE_CALLBACKS[i].onInit();
|
||||||
|
@ -8,11 +8,12 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "display/scene.h"
|
#include "display/scene.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
game_t GAME;
|
game_t GAME;
|
||||||
|
|
||||||
void gameInit() {
|
void gameInit() {
|
||||||
memset(&GAME, 0, sizeof(game_t));
|
memoryZero(&GAME, sizeof(game_t));
|
||||||
|
|
||||||
inputInit();
|
inputInit();
|
||||||
sceneInit();
|
sceneInit();
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "entity.h"
|
#include "entity.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
#include "overworlddefs.h"
|
#include "overworlddefs.h"
|
||||||
|
|
||||||
entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = {
|
entitycallback_t ENTITY_CALLBACKS[ENTITY_TYPE_COUNT] = {
|
||||||
@ -24,7 +25,7 @@ void entityInit(
|
|||||||
assertTrue(type < ENTITY_TYPE_COUNT, "Invalid entity type");
|
assertTrue(type < ENTITY_TYPE_COUNT, "Invalid entity type");
|
||||||
|
|
||||||
// Init values
|
// Init values
|
||||||
memset(ent, 0, sizeof(entity_t));
|
memoryZero(ent, sizeof(entity_t));
|
||||||
ent->type = type;
|
ent->type = type;
|
||||||
|
|
||||||
// Call init
|
// Call init
|
||||||
@ -35,7 +36,6 @@ void entityInit(
|
|||||||
void entityUpdate(entity_t *ent) {
|
void entityUpdate(entity_t *ent) {
|
||||||
assertNotNull(ent, "Entity cannot be NULL");
|
assertNotNull(ent, "Entity cannot be NULL");
|
||||||
assertTrue(ent->type < ENTITY_TYPE_COUNT, "Invalid entity type");
|
assertTrue(ent->type < ENTITY_TYPE_COUNT, "Invalid entity type");
|
||||||
|
|
||||||
assertNotNull(ENTITY_CALLBACKS[ent->type].update, "Entity type update err.");
|
assertNotNull(ENTITY_CALLBACKS[ent->type].update, "Entity type update err.");
|
||||||
ENTITY_CALLBACKS[ent->type].update(ent);
|
ENTITY_CALLBACKS[ent->type].update(ent);
|
||||||
|
|
||||||
@ -64,6 +64,7 @@ void entityTurn(entity_t *ent, const facingdir_t dir) {
|
|||||||
|
|
||||||
void entityMove(entity_t *ent, const facingdir_t dir) {
|
void entityMove(entity_t *ent, const facingdir_t dir) {
|
||||||
assertNotNull(ent, "Entity cannot be NULL");
|
assertNotNull(ent, "Entity cannot be NULL");
|
||||||
|
if(entityIsMoving(ent)) return;
|
||||||
|
|
||||||
entityTurn(ent, dir);
|
entityTurn(ent, dir);
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ typedef struct _entity_t {
|
|||||||
};
|
};
|
||||||
} entity_t;
|
} entity_t;
|
||||||
|
|
||||||
#define ENTITY_MOVE_SPEED 4
|
#define ENTITY_MOVE_SPEED 3
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes an entity.
|
* Initializes an entity.
|
||||||
|
@ -6,13 +6,22 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "overworld.h"
|
#include "overworld.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
overworld_t OVERWORLD;
|
overworld_t OVERWORLD;
|
||||||
|
|
||||||
void overworldInit() {
|
void overworldInit() {
|
||||||
memset(&OVERWORLD, 0, sizeof(overworld_t));
|
memoryZero(&OVERWORLD, sizeof(overworld_t));
|
||||||
|
|
||||||
// Test
|
// Test
|
||||||
|
OVERWORLD.mapWidth = 16;
|
||||||
|
OVERWORLD.mapHeight = 16;
|
||||||
|
assertTrue(
|
||||||
|
OVERWORLD.mapWidth * OVERWORLD.mapHeight <= OVERWORLD_TILE_COUNT_MAX,
|
||||||
|
"Map size exceeds tile count."
|
||||||
|
);
|
||||||
|
|
||||||
entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_PLAYER);
|
entityInit(OVERWORLD.entities + OVERWORLD.entityCount++, ENTITY_TYPE_PLAYER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,12 +6,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "entity.h"
|
|
||||||
#include "overworlddefs.h"
|
#include "overworlddefs.h"
|
||||||
|
#include "entity.h"
|
||||||
|
#include "tile.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
entity_t entities[OVERWORLD_ENTITY_COUNT_MAX];
|
entity_t entities[OVERWORLD_ENTITY_COUNT_MAX];
|
||||||
uint8_t entityCount;
|
uint8_t entityCount;
|
||||||
|
|
||||||
|
tileid_t tileIds[OVERWORLD_TILE_COUNT_MAX];
|
||||||
|
tiledata_t tileData[OVERWORLD_TILE_COUNT_MAX];
|
||||||
|
uint8_t mapWidth, mapHeight;
|
||||||
} overworld_t;
|
} overworld_t;
|
||||||
|
|
||||||
extern overworld_t OVERWORLD;
|
extern overworld_t OVERWORLD;
|
||||||
|
@ -5,7 +5,10 @@
|
|||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define OVERWORLD_ENTITY_COUNT_MAX 32
|
||||||
#define OVERWORLD_ENTITY_WIDTH 32
|
#define OVERWORLD_ENTITY_WIDTH 32
|
||||||
#define OVERWORLD_ENTITY_HEIGHT 32
|
#define OVERWORLD_ENTITY_HEIGHT 32
|
||||||
|
|
||||||
#define OVERWORLD_ENTITY_COUNT_MAX 32
|
#define OVERWORLD_TILE_COUNT_MAX 256
|
||||||
|
#define OVERWORLD_TILE_WIDTH OVERWORLD_ENTITY_WIDTH
|
||||||
|
#define OVERWORLD_TILE_HEIGHT OVERWORLD_ENTITY_HEIGHT
|
@ -18,13 +18,13 @@ void playerInit(entity_t *ent) {
|
|||||||
void playerUpdate(entity_t *ent) {
|
void playerUpdate(entity_t *ent) {
|
||||||
assertNotNull(ent, "Entity cannot be NULL");
|
assertNotNull(ent, "Entity cannot be NULL");
|
||||||
|
|
||||||
if(inputWasPressed(INPUT_RIGHT)) {
|
if(inputIsDown(INPUT_RIGHT)) {
|
||||||
entityMove(ent, FACING_DIRECTION_EAST);
|
entityMove(ent, FACING_DIRECTION_EAST);
|
||||||
} else if(inputWasPressed(INPUT_LEFT)) {
|
} else if(inputIsDown(INPUT_LEFT)) {
|
||||||
entityMove(ent, FACING_DIRECTION_WEST);
|
entityMove(ent, FACING_DIRECTION_WEST);
|
||||||
} else if(inputWasPressed(INPUT_UP)) {
|
} else if(inputIsDown(INPUT_UP)) {
|
||||||
entityMove(ent, FACING_DIRECTION_NORTH);
|
entityMove(ent, FACING_DIRECTION_NORTH);
|
||||||
} else if(inputWasPressed(INPUT_DOWN)) {
|
} else if(inputIsDown(INPUT_DOWN)) {
|
||||||
entityMove(ent, FACING_DIRECTION_SOUTH);
|
entityMove(ent, FACING_DIRECTION_SOUTH);
|
||||||
}
|
}
|
||||||
}
|
}
|
15
src/dusk/overworld/tile.h
Normal file
15
src/dusk/overworld/tile.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "dusk.h"
|
||||||
|
|
||||||
|
typedef uint8_t tileid_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t nothing;
|
||||||
|
} tiledata_t;
|
@ -8,4 +8,5 @@ target_sources(${DUSK_TARGET_NAME}
|
|||||||
PRIVATE
|
PRIVATE
|
||||||
math.c
|
math.c
|
||||||
random.c
|
random.c
|
||||||
|
memory.c
|
||||||
)
|
)
|
52
src/dusk/util/memory.c
Normal file
52
src/dusk/util/memory.c
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
void * memoryAllocate(const size_t size) {
|
||||||
|
void *ptr = malloc(size);
|
||||||
|
assertNotNull(ptr, "Failed to allocate memory.");
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void memoryFree(void *ptr) {
|
||||||
|
assertNotNull(ptr, "Cannot free NULL memory.");
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memoryCopy(void *dest, const void *src, const size_t size) {
|
||||||
|
assertNotNull(dest, "Cannot copy to NULL memory.");
|
||||||
|
assertNotNull(src, "Cannot copy from NULL memory.");
|
||||||
|
assertTrue(size > 0, "Cannot copy 0 bytes of memory.");
|
||||||
|
assertTrue(dest != src, "Cannot copy memory to itself.");
|
||||||
|
memcpy(dest, src, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memorySet(void *dest, const uint8_t value, const size_t size) {
|
||||||
|
assertNotNull(dest, "Cannot set NULL memory.");
|
||||||
|
assertTrue(size > 0, "Cannot set 0 bytes of memory.");
|
||||||
|
memset(dest, value, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memoryZero(void *dest, const size_t size) {
|
||||||
|
memorySet(dest, 0, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memoryCopyRangeSafe(
|
||||||
|
void *dest,
|
||||||
|
const void *start,
|
||||||
|
const void *end,
|
||||||
|
const size_t sizeMax
|
||||||
|
) {
|
||||||
|
assertFalse(start == end, "Start and end pointers are the same.");
|
||||||
|
assertTrue(end > start, "End pointer is not after start pointer.");
|
||||||
|
|
||||||
|
size_t copy = (size_t)end - (size_t)start;
|
||||||
|
assertTrue(copy <= sizeMax, "Size of memory to copy is too large.");
|
||||||
|
memoryCopy(dest, start, copy);
|
||||||
|
}
|
67
src/dusk/util/memory.h
Normal file
67
src/dusk/util/memory.h
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "dusk.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocates memory.
|
||||||
|
*
|
||||||
|
* @param size The size of the memory to allocate.
|
||||||
|
* @return The allocated memory.
|
||||||
|
*/
|
||||||
|
void * memoryAllocate(const size_t size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frees memory.
|
||||||
|
*
|
||||||
|
* @param ptr The memory to free.
|
||||||
|
*/
|
||||||
|
void memoryFree(void *ptr);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies memory.
|
||||||
|
*
|
||||||
|
* @param dest The destination to copy to.
|
||||||
|
* @param src The source to copy from.
|
||||||
|
* @param size The size of the memory to copy.
|
||||||
|
*/
|
||||||
|
void memoryCopy(void *dest, const void *src, const size_t size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets memory.
|
||||||
|
*
|
||||||
|
* @param dest The destination to set.
|
||||||
|
* @param value The value to set.
|
||||||
|
* @param size The size of the memory to set.
|
||||||
|
*/
|
||||||
|
void memorySet(void *dest, const uint8_t value, const size_t size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zeroes memory.
|
||||||
|
*
|
||||||
|
* @param dest The destination to zero.
|
||||||
|
* @param size The size of the memory to zero.
|
||||||
|
*/
|
||||||
|
void memoryZero(void *dest, const size_t size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies memory, ensuring that the memory range is as expected, typically this
|
||||||
|
* is done if you're trying to reshape data in a buffer. Extremely useful in
|
||||||
|
* copying data to a shader buffer.
|
||||||
|
*
|
||||||
|
* @param dest The destination to copy to.
|
||||||
|
* @param start The start of the source to copy from.
|
||||||
|
* @param end The end of the source to copy from.
|
||||||
|
* @param sizeMax The maximum size of the memory to copy.
|
||||||
|
*/
|
||||||
|
void memoryCopyRangeSafe(
|
||||||
|
void *dest,
|
||||||
|
const void *start,
|
||||||
|
const void *end,
|
||||||
|
const size_t sizeMax
|
||||||
|
);
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "assert/assertgl.h"
|
#include "assert/assertgl.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
void assertNotGLErrorCheck(const char_t *file, const int32_t line) {
|
void assertNotGLErrorCheck(const char_t *file, const int32_t line) {
|
||||||
assertNotNull(file, "File is an invlaid string");
|
assertNotNull(file, "File is an invlaid string");
|
||||||
@ -18,7 +19,7 @@ void assertNotGLErrorCheck(const char_t *file, const int32_t line) {
|
|||||||
|
|
||||||
while((errorCode = glGetError()) != GL_NO_ERROR) {
|
while((errorCode = glGetError()) != GL_NO_ERROR) {
|
||||||
if(errorCount == 0) {
|
if(errorCount == 0) {
|
||||||
buffer = malloc(sizeof(char_t) * 4096);
|
buffer = memoryAllocate(sizeof(char_t) * 4096);
|
||||||
sprintf(buffer, "assertNotGLErrorCheck: %s:%d\n", file, line);
|
sprintf(buffer, "assertNotGLErrorCheck: %s:%d\n", file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include "asset.h"
|
#include "asset.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "util/math.h"
|
#include "util/math.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
asset_t ASSET;
|
asset_t ASSET;
|
||||||
|
|
||||||
@ -18,7 +19,7 @@ void assetInit() {
|
|||||||
EXECUTABLE_DIRECTORY
|
EXECUTABLE_DIRECTORY
|
||||||
};
|
};
|
||||||
|
|
||||||
memset(&ASSET, 0, sizeof(asset_t));
|
memoryZero(&ASSET, sizeof(asset_t));
|
||||||
|
|
||||||
// Try and find the asset file.
|
// Try and find the asset file.
|
||||||
for(int32_t i = 0; i < sizeof(scanLocations) / sizeof(char_t*); i++) {
|
for(int32_t i = 0; i < sizeof(scanLocations) / sizeof(char_t*); i++) {
|
||||||
@ -171,7 +172,7 @@ void assetDispose() {
|
|||||||
assertNotNull(ASSET.file, "Asset disposing but file is NULL?");
|
assertNotNull(ASSET.file, "Asset disposing but file is NULL?");
|
||||||
|
|
||||||
fclose(ASSET.file);
|
fclose(ASSET.file);
|
||||||
memset(&ASSET, 0, sizeof(asset_t));
|
memoryZero(&ASSET, sizeof(asset_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Libarchive callbacks
|
// Libarchive callbacks
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
# Sources
|
# Sources
|
||||||
target_sources(${DUSK_TARGET_NAME}
|
target_sources(${DUSK_TARGET_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
framebuffer.c
|
|
||||||
render.c
|
render.c
|
||||||
quad.c
|
quad.c
|
||||||
texture.c
|
texture.c
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "assert/assertgl.h"
|
#include "assert/assertgl.h"
|
||||||
|
#include "util/memory.h"
|
||||||
#include "render.h"
|
#include "render.h"
|
||||||
#include "display/scene.h"
|
#include "display/scene.h"
|
||||||
#include "display/shader/shadermanager.h"
|
#include "display/shader/shadermanager.h"
|
||||||
@ -15,7 +16,7 @@
|
|||||||
render_t RENDER;
|
render_t RENDER;
|
||||||
|
|
||||||
void renderInit() {
|
void renderInit() {
|
||||||
memset(&RENDER, 0, sizeof(render_t));
|
memoryZero(&RENDER, sizeof(render_t));
|
||||||
|
|
||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
assertNoGLError();
|
assertNoGLError();
|
||||||
@ -26,6 +27,7 @@ void renderInit() {
|
|||||||
|
|
||||||
void renderUpdate() {
|
void renderUpdate() {
|
||||||
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||||
|
assertNoGLError();
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
assertNoGLError();
|
assertNoGLError();
|
||||||
|
|
||||||
|
@ -14,4 +14,5 @@ target_sources(${DUSK_TARGET_NAME}
|
|||||||
# Subdirs
|
# Subdirs
|
||||||
add_subdirectory(data)
|
add_subdirectory(data)
|
||||||
add_subdirectory(entityshader)
|
add_subdirectory(entityshader)
|
||||||
|
add_subdirectory(mapshader)
|
||||||
add_subdirectory(fragments)
|
add_subdirectory(fragments)
|
@ -8,4 +8,5 @@ target_sources(${DUSK_TARGET_NAME}
|
|||||||
PRIVATE
|
PRIVATE
|
||||||
transforms.c
|
transforms.c
|
||||||
entities.c
|
entities.c
|
||||||
|
map.c
|
||||||
)
|
)
|
@ -8,6 +8,7 @@
|
|||||||
#include "entities.h"
|
#include "entities.h"
|
||||||
#include "overworld/overworld.h"
|
#include "overworld/overworld.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
shaderbuffer_t ENTITIES_BUFFER;
|
shaderbuffer_t ENTITIES_BUFFER;
|
||||||
entitiesdata_t ENTITIES_DATA;
|
entitiesdata_t ENTITIES_DATA;
|
||||||
@ -27,13 +28,12 @@ void entitiesUpdate() {
|
|||||||
entitiesdataent_t *dst = &ENTITIES_DATA.entities[i];
|
entitiesdataent_t *dst = &ENTITIES_DATA.entities[i];
|
||||||
|
|
||||||
// Copy position data.
|
// Copy position data.
|
||||||
assertMemoryRangeMatches(
|
memoryCopyRangeSafe(
|
||||||
|
&dst->position,
|
||||||
&src->x,
|
&src->x,
|
||||||
&src->direction,
|
&src->subY + sizeof(uint8_t),
|
||||||
sizeof(uint_t),
|
sizeof(uint_t)
|
||||||
"Entity data is not packed correctly any more."
|
|
||||||
);
|
);
|
||||||
memcpy(&dst->position, &src->x, sizeof(uint_t));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
43
src/duskgl/display/shader/data/map.c
Normal file
43
src/duskgl/display/shader/data/map.c
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "map.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
|
shaderbuffer_t MAP_BUFFER;
|
||||||
|
mapdata_t MAP_DATA;
|
||||||
|
|
||||||
|
void mapInit() {
|
||||||
|
memset(&MAP_DATA, 0, sizeof(mapdata_t));
|
||||||
|
shaderBufferInit(&MAP_BUFFER, sizeof(mapdata_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
void mapUpdate() {
|
||||||
|
|
||||||
|
// Copy map size.
|
||||||
|
memoryCopyRangeSafe(
|
||||||
|
&MAP_DATA.mapSize,
|
||||||
|
&OVERWORLD.mapWidth,
|
||||||
|
&OVERWORLD.mapHeight + sizeof(uint8_t),
|
||||||
|
sizeof(uint_t)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Copy tile ids.
|
||||||
|
memoryCopyRangeSafe(
|
||||||
|
&MAP_DATA.tileIds,
|
||||||
|
&OVERWORLD.tileIds[0],
|
||||||
|
&OVERWORLD.tileIds[OVERWORLD.mapWidth * OVERWORLD.mapHeight],
|
||||||
|
OVERWORLD_TILE_COUNT_MAX * sizeof(tileid_t)
|
||||||
|
);
|
||||||
|
|
||||||
|
shaderBufferBind(&MAP_BUFFER);
|
||||||
|
shaderBufferSetData(&MAP_BUFFER, &MAP_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mapDispose() {
|
||||||
|
shaderBufferDispose(&MAP_BUFFER);
|
||||||
|
}
|
42
src/duskgl/display/shader/data/map.glsl
Normal file
42
src/duskgl/display/shader/data/map.glsl
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// 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"
|
||||||
|
|
||||||
|
#define MAP_PACKED_SIZE 4
|
||||||
|
|
||||||
|
struct TileData {
|
||||||
|
uint nothing;
|
||||||
|
uvec3 _padding0;
|
||||||
|
};
|
||||||
|
|
||||||
|
layout(std140) uniform b_Map {
|
||||||
|
uint mapSize;
|
||||||
|
uint mapTileIds[OVERWORLD_TILE_COUNT_MAX / MAP_PACKED_SIZE];
|
||||||
|
TileData mapTileData[OVERWORLD_TILE_COUNT_MAX];
|
||||||
|
uvec3 _padding0;
|
||||||
|
};
|
||||||
|
|
||||||
|
vec2 mapTileGetSize() {
|
||||||
|
return vec2(float(OVERWORLD_TILE_WIDTH), float(OVERWORLD_TILE_HEIGHT));
|
||||||
|
}
|
||||||
|
|
||||||
|
vec2 mapGetVertice(uint instanceIndex, uint indiceIndex) {
|
||||||
|
vec2 quad = quadGetVertice(indiceIndex);
|
||||||
|
|
||||||
|
uint mapWidth = packedGetU8(0u, mapSize);
|
||||||
|
uint mapHeight = packedGetU8(1u, mapSize);
|
||||||
|
|
||||||
|
quad += vec2(
|
||||||
|
float(instanceIndex % mapWidth),
|
||||||
|
float(instanceIndex / mapWidth)
|
||||||
|
);
|
||||||
|
|
||||||
|
quad *= mapTileGetSize();
|
||||||
|
|
||||||
|
return quad;
|
||||||
|
}
|
44
src/duskgl/display/shader/data/map.h
Normal file
44
src/duskgl/display/shader/data/map.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma ocne
|
||||||
|
#include "display/shader/shaderbuffer.h"
|
||||||
|
#include "overworld/overworld.h"
|
||||||
|
|
||||||
|
#define MAP_BLOCK_NAME "b_Map"
|
||||||
|
#define MAP_PACKED_SIZE 4
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t nothing;
|
||||||
|
uvec3_t _padding0;
|
||||||
|
} maptiledata_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint_t mapSize;
|
||||||
|
uvec3_t _padding0;
|
||||||
|
|
||||||
|
uint32_t tileIds[OVERWORLD_TILE_COUNT_MAX / MAP_PACKED_SIZE];
|
||||||
|
maptiledata_t tileData[OVERWORLD_TILE_COUNT_MAX];
|
||||||
|
} mapdata_t;
|
||||||
|
|
||||||
|
extern shaderbuffer_t MAP_BUFFER;
|
||||||
|
extern mapdata_t MAP_DATA;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the map buffer and data.
|
||||||
|
*/
|
||||||
|
void mapInit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the map buffer with the current data.
|
||||||
|
*/
|
||||||
|
void mapUpdate();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys the map buffer.
|
||||||
|
*/
|
||||||
|
void mapDispose();
|
@ -12,7 +12,7 @@ out vec2 v_TextureCoord;
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
uint instanceIndex = uint(gl_InstanceID);
|
uint instanceIndex = uint(gl_InstanceID);
|
||||||
uint indiceIndex = uint(gl_VertexID % 6);
|
uint indiceIndex = quadGetIndiceIndex(gl_VertexID);
|
||||||
|
|
||||||
vec2 vert = entityGetVertice(instanceIndex, indiceIndex);
|
vec2 vert = entityGetVertice(instanceIndex, indiceIndex);
|
||||||
vec2 uv = quadGetTextureCoordinate(indiceIndex);
|
vec2 uv = quadGetTextureCoordinate(indiceIndex);
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "assert/assertgl.h"
|
#include "assert/assertgl.h"
|
||||||
|
#include "util/memory.h"
|
||||||
#include "entityshader.h"
|
#include "entityshader.h"
|
||||||
#include "entity_vert.glsl.h"
|
#include "entity_vert.glsl.h"
|
||||||
#include "entity_frag.glsl.h"
|
#include "entity_frag.glsl.h"
|
||||||
@ -16,7 +17,7 @@
|
|||||||
entityshader_t ENTITY_SHADER;
|
entityshader_t ENTITY_SHADER;
|
||||||
|
|
||||||
void entityShaderInit() {
|
void entityShaderInit() {
|
||||||
memset(&ENTITY_SHADER, 0, sizeof(entityshader_t));
|
memoryZero(&ENTITY_SHADER, sizeof(entityshader_t));
|
||||||
|
|
||||||
shaderInit(
|
shaderInit(
|
||||||
&ENTITY_SHADER.shader,
|
&ENTITY_SHADER.shader,
|
||||||
|
@ -3,6 +3,14 @@
|
|||||||
// This software is released under the MIT License.
|
// This software is released under the MIT License.
|
||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
uint quadGetIndiceIndex(uint vertexId) {
|
||||||
|
return vertexId % 6u;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint quadGetIndiceIndex(int vertexId) {
|
||||||
|
return quadGetIndiceIndex(uint(vertexId));
|
||||||
|
}
|
||||||
|
|
||||||
vec2 quadGetVertice(uint indiceIndex) {
|
vec2 quadGetVertice(uint indiceIndex) {
|
||||||
vec2 vert = vec2(0, 0);
|
vec2 vert = vec2(0, 0);
|
||||||
|
|
||||||
|
14
src/duskgl/display/shader/mapshader/CMakeLists.txt
Normal file
14
src/duskgl/display/shader/mapshader/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# Copyright (c) 2025 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Shaders
|
||||||
|
glsltool(map_vert.glsl)
|
||||||
|
glsltool(map_frag.glsl)
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(${DUSK_TARGET_NAME}
|
||||||
|
PRIVATE
|
||||||
|
mapshader.c
|
||||||
|
)
|
16
src/duskgl/display/shader/mapshader/map_frag.glsl
Normal file
16
src/duskgl/display/shader/mapshader/map_frag.glsl
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// Copyright (c) 2025 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#include "../fragments/header.glsl"
|
||||||
|
|
||||||
|
// Inputs from vertex shader
|
||||||
|
in vec2 v_TextureCoord;
|
||||||
|
|
||||||
|
// Frag pixel color
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
FragColor = vec4(1, 0, 0, 1);
|
||||||
|
}
|
22
src/duskgl/display/shader/mapshader/map_vert.glsl
Normal file
22
src/duskgl/display/shader/mapshader/map_vert.glsl
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Copyright (c) 2025 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#include "../fragments/header.glsl"
|
||||||
|
#include "../data/transforms.glsl"
|
||||||
|
#include "../data/map.glsl"
|
||||||
|
|
||||||
|
// Outputs to fragment shader
|
||||||
|
out vec2 v_TextureCoord;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
uint instanceIndex = uint(gl_InstanceID);
|
||||||
|
uint indiceIndex = quadGetIndiceIndex(gl_VertexID);
|
||||||
|
|
||||||
|
vec2 vert = mapGetVertice(instanceIndex, indiceIndex);
|
||||||
|
vec2 uv = quadGetTextureCoordinate(indiceIndex);
|
||||||
|
|
||||||
|
gl_Position = transforms.projection * transforms.view * vec4(vert, 0.0, 1.0);
|
||||||
|
v_TextureCoord = uv;
|
||||||
|
}
|
50
src/duskgl/display/shader/mapshader/mapshader.c
Normal file
50
src/duskgl/display/shader/mapshader/mapshader.c
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "mapshader.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
#include "display/shader/data/map.h"
|
||||||
|
#include "display/shader/data/transforms.h"
|
||||||
|
#include "map_vert.glsl.h"
|
||||||
|
#include "map_frag.glsl.h"
|
||||||
|
|
||||||
|
mapshader_t MAP_SHADER;
|
||||||
|
|
||||||
|
void mapShaderInit() {
|
||||||
|
memoryZero(&MAP_SHADER, sizeof(mapshader_t));
|
||||||
|
|
||||||
|
shaderInit(
|
||||||
|
&MAP_SHADER.shader,
|
||||||
|
map_vertShaderSource,
|
||||||
|
map_fragShaderSource
|
||||||
|
);
|
||||||
|
|
||||||
|
// Uniform blocks
|
||||||
|
MAP_SHADER.mapBlock = shaderGetBlock(
|
||||||
|
&MAP_SHADER.shader,
|
||||||
|
MAP_BLOCK_NAME
|
||||||
|
);
|
||||||
|
|
||||||
|
MAP_SHADER.transformsBlock = shaderGetBlock(
|
||||||
|
&MAP_SHADER.shader,
|
||||||
|
TRANSFORMS_BLOCK_NAME
|
||||||
|
);
|
||||||
|
|
||||||
|
// Bind
|
||||||
|
shaderBufferBindToBlock(&MAP_BUFFER, MAP_SHADER.mapBlock);
|
||||||
|
shaderBufferBindToBlock(&TRANSFORMS_BUFFER, MAP_SHADER.transformsBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mapShaderUse() {
|
||||||
|
shaderUse(&MAP_SHADER.shader);
|
||||||
|
shaderBufferBindToBlock(&MAP_BUFFER, MAP_SHADER.mapBlock);
|
||||||
|
shaderBufferBindToBlock(&TRANSFORMS_BUFFER, MAP_SHADER.transformsBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mapShaderDispose() {
|
||||||
|
shaderDispose(&MAP_SHADER.shader);
|
||||||
|
}
|
32
src/duskgl/display/shader/mapshader/mapshader.h
Normal file
32
src/duskgl/display/shader/mapshader/mapshader.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 "display/shader/shader.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
shader_t shader;
|
||||||
|
GLuint mapBlock;
|
||||||
|
GLuint transformsBlock;
|
||||||
|
} mapshader_t;
|
||||||
|
|
||||||
|
extern mapshader_t MAP_SHADER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the map shader.
|
||||||
|
*/
|
||||||
|
void mapShaderInit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses the map shader.
|
||||||
|
*/
|
||||||
|
void mapShaderUse();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys the map shader.
|
||||||
|
*/
|
||||||
|
void mapShaderDispose();
|
@ -9,16 +9,20 @@
|
|||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "display/shader/data/transforms.h"
|
#include "display/shader/data/transforms.h"
|
||||||
#include "display/shader/data/entities.h"
|
#include "display/shader/data/entities.h"
|
||||||
|
#include "display/shader/data/map.h"
|
||||||
#include "display/shader/entityshader/entityshader.h"
|
#include "display/shader/entityshader/entityshader.h"
|
||||||
|
#include "display/shader/mapshader/mapshader.h"
|
||||||
|
|
||||||
shadermanagerdatacallback_t SHADER_MANAGER_DATA_CALLBACKS[] = {
|
shadermanagerdatacallback_t SHADER_MANAGER_DATA_CALLBACKS[] = {
|
||||||
{ transformsInit, transformsUpdate, transformsDispose },
|
{ transformsInit, transformsUpdate, transformsDispose },
|
||||||
{ entitiesInit, entitiesUpdate, entitiesDispose }
|
{ entitiesInit, entitiesUpdate, entitiesDispose },
|
||||||
|
{ mapInit, mapUpdate, mapDispose }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
shadermanagershadercallback_t SHADER_MANAGER_SHADER_CALLBACKS[] = {
|
shadermanagershadercallback_t SHADER_MANAGER_SHADER_CALLBACKS[] = {
|
||||||
{ entityShaderInit, entityShaderDispose }
|
{ entityShaderInit, entityShaderDispose },
|
||||||
|
{ mapShaderInit, mapShaderDispose }
|
||||||
};
|
};
|
||||||
|
|
||||||
void shaderManagerInit() {
|
void shaderManagerInit() {
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include "assert/assertgl.h"
|
#include "assert/assertgl.h"
|
||||||
#include "asset.h"
|
#include "asset.h"
|
||||||
#include "util/math.h"
|
#include "util/math.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
#define TEXTURE_BUFFER_SIZE 32768
|
#define TEXTURE_BUFFER_SIZE 32768
|
||||||
|
|
||||||
@ -37,7 +38,7 @@ void textureLoad(
|
|||||||
|
|
||||||
// Decode the image. I can probably stream this in the future.
|
// Decode the image. I can probably stream this in the future.
|
||||||
size_t dataSize = ihdr.width * ihdr.height * 4;// 4 for RGBA
|
size_t dataSize = ihdr.width * ihdr.height * 4;// 4 for RGBA
|
||||||
uint8_t *data = (uint8_t *)malloc(dataSize);
|
uint8_t *data = (uint8_t *)memoryAllocate(dataSize);
|
||||||
assertNotNull(data, "Failed to allocate memory for texture data.");
|
assertNotNull(data, "Failed to allocate memory for texture data.");
|
||||||
spng_decode_image(ctx, data, dataSize, SPNG_FMT_RGBA8, 0);
|
spng_decode_image(ctx, data, dataSize, SPNG_FMT_RGBA8, 0);
|
||||||
|
|
||||||
@ -59,7 +60,7 @@ void textureLoad(
|
|||||||
0, GL_RGBA, GL_UNSIGNED_BYTE, data
|
0, GL_RGBA, GL_UNSIGNED_BYTE, data
|
||||||
);
|
);
|
||||||
assertNoGLError();
|
assertNoGLError();
|
||||||
free(data);
|
memoryFree(data);
|
||||||
|
|
||||||
// Setup texture parameters
|
// Setup texture parameters
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
@ -8,10 +8,12 @@
|
|||||||
#include "overworld/overworld.h"
|
#include "overworld/overworld.h"
|
||||||
#include "display/quad.h"
|
#include "display/quad.h"
|
||||||
#include "display/shader/entityshader/entityshader.h"
|
#include "display/shader/entityshader/entityshader.h"
|
||||||
|
#include "display/shader/mapshader/mapshader.h"
|
||||||
|
|
||||||
void overworldRender() {
|
void overworldRender() {
|
||||||
entityShaderUse();
|
mapShaderUse();
|
||||||
|
quadRender(OVERWORLD.mapWidth * OVERWORLD.mapHeight);
|
||||||
|
|
||||||
// Set entity data.
|
// entityShaderUse();
|
||||||
quadRender(1);
|
// quadRender(OVERWORLD.entityCount);
|
||||||
}
|
}
|
Reference in New Issue
Block a user