Compare commits
62 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 643a8077dd | |||
| 4205899f5a | |||
| 7dd3940770 | |||
| 00d94e3015 | |||
| 7bacb3ee2b | |||
| 8e49be5ac4 | |||
| 3b94598d2c | |||
| bddc9af3b6 | |||
| 2451d73a7c | |||
| 1dd2efa182 | |||
| acea610773 | |||
| 8f2f1fd496 | |||
| 39c775872a | |||
| bdb3cbd109 | |||
| ff84ce2b04 | |||
| 225f405592 | |||
| 715ecffa18 | |||
| e51cdc8992 | |||
| 133685ea37 | |||
| 6aff98d555 | |||
| acdc524284 | |||
| 1ee5ec7b43 | |||
| 46a5403511 | |||
| 87bfb92576 | |||
| c7a3e5601c | |||
| 4009130f6e | |||
| c91243f6e9 | |||
| 0e3871ac26 | |||
| 55baafec8a | |||
| b5a66993ca | |||
| 0b570b5fd6 | |||
| 378227c377 | |||
| 650645eaff | |||
| 62c71f3fe6 | |||
| 041ec3d710 | |||
| 5f2d871bad | |||
| a30b151e4d | |||
| 5a651d2d1f | |||
| 4b3826edd9 | |||
| bae1ff3759 | |||
| fd82486431 | |||
| c9cd91cbd8 | |||
| c8abd374fe | |||
| 2b9ee8f721 | |||
| d02673e04a | |||
| f0117b8e6e | |||
| bb7c41c754 | |||
| efa583c154 | |||
| d16ea13c14 | |||
| 673d8e0a18 | |||
| 37cfdde1ee | |||
| 0778ffb57a | |||
| 42099f7241 | |||
| c52e1d22b7 | |||
| 0d7b0aadd1 | |||
| 4cd3355ef1 | |||
| 98d70b96d1 | |||
| 64735bdf43 | |||
| 7b87347b77 | |||
| b5b29d7061 | |||
| 9ec21f85a0 | |||
| da1a5a3f1b |
@@ -53,6 +53,22 @@ jobs:
|
|||||||
path: ./git-artifcats/Dusk
|
path: ./git-artifcats/Dusk
|
||||||
if-no-files-found: error
|
if-no-files-found: error
|
||||||
|
|
||||||
|
build-vita:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v6
|
||||||
|
- name: Set up Docker
|
||||||
|
uses: docker/setup-docker-action@v5
|
||||||
|
- name: Build Vita
|
||||||
|
run: ./scripts/build-vita-docker.sh
|
||||||
|
- name: Upload Vita binary
|
||||||
|
uses: actions/upload-artifact@v6
|
||||||
|
with:
|
||||||
|
name: dusk-vita
|
||||||
|
path: build-vita/Dusk.vpk
|
||||||
|
if-no-files-found: error
|
||||||
|
|
||||||
build-knulli:
|
build-knulli:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
|
|||||||
+3
-2
@@ -83,7 +83,6 @@ assets/borrowed
|
|||||||
.VSCode*
|
.VSCode*
|
||||||
|
|
||||||
/vita
|
/vita
|
||||||
|
|
||||||
._*
|
._*
|
||||||
|
|
||||||
*~
|
*~
|
||||||
@@ -104,4 +103,6 @@ yarn.lock
|
|||||||
.venv
|
.venv
|
||||||
|
|
||||||
/build2
|
/build2
|
||||||
/build*
|
/build*
|
||||||
|
/assets/test
|
||||||
|
/tools_old
|
||||||
@@ -35,8 +35,8 @@ void cameraInitOrthographic(camera_t *camera) {
|
|||||||
camera->projType = CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC;
|
camera->projType = CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC;
|
||||||
camera->orthographic.left = 0.0f;
|
camera->orthographic.left = 0.0f;
|
||||||
camera->orthographic.right = SCREEN.width;
|
camera->orthographic.right = SCREEN.width;
|
||||||
camera->orthographic.top = 0.0f;
|
camera->orthographic.top = SCREEN.height;
|
||||||
camera->orthographic.bottom = SCREEN.height;
|
camera->orthographic.bottom = 0.0f;
|
||||||
camera->nearClip = 0.1f;
|
camera->nearClip = 0.1f;
|
||||||
camera->farClip = 1.0f;
|
camera->farClip = 1.0f;
|
||||||
|
|
||||||
@@ -14,11 +14,6 @@
|
|||||||
#error "cameraPushMatrixPlatform must be defined"
|
#error "cameraPushMatrixPlatform must be defined"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
CAMERA_PROJECTION_TYPE_PERSPECTIVE,
|
|
||||||
CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED,
|
|
||||||
CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC
|
|
||||||
} cameraprojectiontype_t;
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
CAMERA_VIEW_TYPE_MATRIX,
|
CAMERA_VIEW_TYPE_MATRIX,
|
||||||
@@ -10,6 +10,4 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
|||||||
assettileset.c
|
assettileset.c
|
||||||
assetlanguage.c
|
assetlanguage.c
|
||||||
assetscript.c
|
assetscript.c
|
||||||
assetmap.c
|
|
||||||
assetmapchunk.c
|
|
||||||
)
|
)
|
||||||
+3
-5
@@ -9,8 +9,8 @@ if PSP then
|
|||||||
inputBind("down", INPUT_ACTION_DOWN)
|
inputBind("down", INPUT_ACTION_DOWN)
|
||||||
inputBind("left", INPUT_ACTION_LEFT)
|
inputBind("left", INPUT_ACTION_LEFT)
|
||||||
inputBind("right", INPUT_ACTION_RIGHT)
|
inputBind("right", INPUT_ACTION_RIGHT)
|
||||||
inputBind("circle", INPUT_ACTION_CANCEL)
|
inputBind("accept", INPUT_ACTION_ACCEPT)
|
||||||
inputBind("cross", INPUT_ACTION_ACCEPT)
|
inputBind("cancel", INPUT_ACTION_CANCEL)
|
||||||
inputBind("select", INPUT_ACTION_RAGEQUIT)
|
inputBind("select", INPUT_ACTION_RAGEQUIT)
|
||||||
inputBind("lstick_up", INPUT_ACTION_UP)
|
inputBind("lstick_up", INPUT_ACTION_UP)
|
||||||
inputBind("lstick_down", INPUT_ACTION_DOWN)
|
inputBind("lstick_down", INPUT_ACTION_DOWN)
|
||||||
@@ -73,6 +73,4 @@ elseif LINUX then
|
|||||||
|
|
||||||
else
|
else
|
||||||
print("Unknown platform, no default input bindings set.")
|
print("Unknown platform, no default input bindings set.")
|
||||||
end
|
end
|
||||||
|
|
||||||
sceneSet('scene/minesweeper.lua')
|
|
||||||
+56
-5
@@ -1,9 +1,60 @@
|
|||||||
#
|
|
||||||
msgid ""
|
msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Language: en_US\n"
|
"Project-Id-Version: ExampleApp 1.0\n"
|
||||||
|
"Language: en\n"
|
||||||
"Content-Type: text/plain; charset=UTF-8\n"
|
"Content-Type: text/plain; charset=UTF-8\n"
|
||||||
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
|
"Plural-Forms: nplurals=4; plural=(n==1 ? 0 : n==2 ? 1 : (n<7 ? 2 : 3));\n"
|
||||||
|
|
||||||
msgid "ui.test"
|
#: ui/menu.c:10
|
||||||
msgstr "Hello this is a test."
|
msgid "ui.title"
|
||||||
|
msgstr ""
|
||||||
|
"Welcome"
|
||||||
|
|
||||||
|
#: ui/user.c:22
|
||||||
|
msgid "ui.greeting"
|
||||||
|
msgstr "Hello, %s!"
|
||||||
|
|
||||||
|
#: ui/files.c:40
|
||||||
|
msgid "ui.file_status"
|
||||||
|
msgstr "%s has %d files."
|
||||||
|
|
||||||
|
#: ui/cart.c:55
|
||||||
|
msgid "cart.item_count"
|
||||||
|
msgid_plural "cart.item_count"
|
||||||
|
msgstr[0] "%d item"
|
||||||
|
msgstr[1] "%d items (dual)"
|
||||||
|
msgstr[2] "%d items (few)"
|
||||||
|
msgstr[3] "%d items (many)"
|
||||||
|
|
||||||
|
#: ui/notifications.c:71
|
||||||
|
msgid ""
|
||||||
|
"ui.multiline_help"
|
||||||
|
msgstr ""
|
||||||
|
"Line one of the help text.\n"
|
||||||
|
"Line two continues here.\n"
|
||||||
|
"Line three ends here."
|
||||||
|
|
||||||
|
#: ui/errors.c:90
|
||||||
|
msgid ""
|
||||||
|
"error.upload_failed.long"
|
||||||
|
msgstr ""
|
||||||
|
"Upload failed for file \"%s\".\n"
|
||||||
|
"Please try again later or contact support."
|
||||||
|
|
||||||
|
#: ui/messages.c:110
|
||||||
|
msgid ""
|
||||||
|
"user.invite_status"
|
||||||
|
msgid_plural ""
|
||||||
|
"user.invite_status"
|
||||||
|
msgstr[0] ""
|
||||||
|
"%s invited %d user.\n"
|
||||||
|
"Please review the request."
|
||||||
|
msgstr[1] ""
|
||||||
|
"%s invited %d users (dual).\n"
|
||||||
|
"Please review the requests."
|
||||||
|
msgstr[2] ""
|
||||||
|
"%s invited %d users (few).\n"
|
||||||
|
"Please review the requests."
|
||||||
|
msgstr[3] ""
|
||||||
|
"%s invited %d users (many).\n"
|
||||||
|
"Please review the requests."
|
||||||
Binary file not shown.
@@ -1,264 +0,0 @@
|
|||||||
module('spritebatch')
|
|
||||||
module('camera')
|
|
||||||
module('color')
|
|
||||||
module('ui')
|
|
||||||
module('screen')
|
|
||||||
module('time')
|
|
||||||
module('glm')
|
|
||||||
module('text')
|
|
||||||
module('tileset')
|
|
||||||
module('texture')
|
|
||||||
module('input')
|
|
||||||
module('shader')
|
|
||||||
|
|
||||||
CELL_STATE_DEFAULT = 0
|
|
||||||
CELL_STATE_HOVER = 1
|
|
||||||
CELL_STATE_DOWN = 2
|
|
||||||
CELL_STATE_DISABLED = 3
|
|
||||||
|
|
||||||
screenSetBackground(colorCornflowerBlue())
|
|
||||||
camera = cameraCreate(CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC)
|
|
||||||
|
|
||||||
-- tilesetUi = tilesetGetByName("ui")
|
|
||||||
-- textureUi = textureLoad(tilesetUi.texture)
|
|
||||||
|
|
||||||
-- tilesetBorder = tilesetGetByName("border")
|
|
||||||
-- textureBorder = textureLoad(tilesetBorder.texture)
|
|
||||||
|
|
||||||
-- textureGrid = textureLoad("minesweeper/grid_bg.dpi")
|
|
||||||
|
|
||||||
-- tilesetCell = tilesetGetByName("cell")
|
|
||||||
-- textureCell = textureLoad(tilesetCell.texture)
|
|
||||||
-- cellSliceDefault = tilesetPositionGetUV(tilesetCell, 3, 5)
|
|
||||||
-- cellSliceHover = tilesetPositionGetUV(tilesetCell, 3, 4)
|
|
||||||
-- cellSliceDown = tilesetPositionGetUV(tilesetCell, 3, 6)
|
|
||||||
-- cellSliceDisabled = tilesetPositionGetUV(tilesetCell, 3, 7)
|
|
||||||
|
|
||||||
-- sweepwerCols = 10
|
|
||||||
-- sweeperRows = 14
|
|
||||||
|
|
||||||
-- mouseX = -1
|
|
||||||
-- mouseY = -1
|
|
||||||
-- centerX = 0
|
|
||||||
-- centerY = 0
|
|
||||||
-- boardWidth = sweepwerCols * tilesetCell.tileWidth
|
|
||||||
-- boardHeight = sweeperRows * tilesetCell.tileHeight
|
|
||||||
|
|
||||||
-- i = 0
|
|
||||||
-- cells = {}
|
|
||||||
-- for y = 1, sweeperRows do
|
|
||||||
-- for x = 1, sweepwerCols do
|
|
||||||
-- cells[i] = CELL_STATE_DEFAULT
|
|
||||||
-- i = i + 1
|
|
||||||
-- end
|
|
||||||
-- end
|
|
||||||
|
|
||||||
function cellDraw(x, y, type)
|
|
||||||
local slice = cellSliceDefault
|
|
||||||
if type == CELL_STATE_HOVER then
|
|
||||||
slice = cellSliceHover
|
|
||||||
elseif type == CELL_STATE_DOWN then
|
|
||||||
slice = cellSliceDown
|
|
||||||
elseif type == CELL_STATE_DISABLED then
|
|
||||||
slice = cellSliceDisabled
|
|
||||||
end
|
|
||||||
|
|
||||||
spriteBatchPush(
|
|
||||||
x, y,
|
|
||||||
x + tilesetCell.tileWidth, y + tilesetCell.tileHeight,
|
|
||||||
colorWhite(),
|
|
||||||
slice.u0, slice.v0,
|
|
||||||
slice.u1, slice.v1
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
function backgroundDraw()
|
|
||||||
local t = (TIME.time / 40) % 1
|
|
||||||
local scaleX = screenGetWidth() / textureGrid.width
|
|
||||||
local scaleY = screenGetHeight() / textureGrid.height
|
|
||||||
local u0 = t * scaleX
|
|
||||||
local v0 = t * scaleY
|
|
||||||
local u1 = scaleX + u0
|
|
||||||
local v1 = scaleY + v0
|
|
||||||
|
|
||||||
spriteBatchPush(textureGrid,
|
|
||||||
0, 0,
|
|
||||||
screenGetWidth(), screenGetHeight(),
|
|
||||||
colorWhite(),
|
|
||||||
u0, v0,
|
|
||||||
u1, v1
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
function borderDraw(x, y, innerWidth, innerHeight)
|
|
||||||
-- Top Left
|
|
||||||
local uv = tilesetPositionGetUV(tilesetBorder, 0, 0)
|
|
||||||
spriteBatchPush(textureBorder,
|
|
||||||
x - tilesetBorder.tileWidth, y - tilesetBorder.tileWidth,
|
|
||||||
x, y,
|
|
||||||
colorWhite(),
|
|
||||||
uv.u0, uv.v0,
|
|
||||||
uv.u1, uv.v1
|
|
||||||
)
|
|
||||||
|
|
||||||
-- Top Right
|
|
||||||
uv = tilesetPositionGetUV(tilesetBorder, 10, 0)
|
|
||||||
spriteBatchPush(textureBorder,
|
|
||||||
x + innerWidth, y - tilesetBorder.tileHeight,
|
|
||||||
x + innerWidth + tilesetBorder.tileWidth, y,
|
|
||||||
colorWhite(),
|
|
||||||
uv.u0, uv.v0,
|
|
||||||
uv.u1, uv.v1
|
|
||||||
)
|
|
||||||
|
|
||||||
-- Bottom Left
|
|
||||||
uv = tilesetPositionGetUV(tilesetBorder, 0, 10)
|
|
||||||
spriteBatchPush(textureBorder,
|
|
||||||
x - tilesetBorder.tileWidth, y + innerHeight,
|
|
||||||
x, y + innerHeight + tilesetBorder.tileHeight,
|
|
||||||
colorWhite(),
|
|
||||||
uv.u0, uv.v0,
|
|
||||||
uv.u1, uv.v1
|
|
||||||
)
|
|
||||||
|
|
||||||
-- Bottom Right
|
|
||||||
uv = tilesetPositionGetUV(tilesetBorder, 10, 10)
|
|
||||||
spriteBatchPush(textureBorder,
|
|
||||||
x + innerWidth, y + innerHeight,
|
|
||||||
x + innerWidth + tilesetBorder.tileWidth, y + innerHeight + tilesetBorder.tileHeight,
|
|
||||||
colorWhite(),
|
|
||||||
uv.u0, uv.v0,
|
|
||||||
uv.u1, uv.v1
|
|
||||||
)
|
|
||||||
|
|
||||||
-- Top
|
|
||||||
uv = tilesetPositionGetUV(tilesetBorder, 1, 0)
|
|
||||||
spriteBatchPush(textureBorder,
|
|
||||||
x, y - tilesetBorder.tileHeight,
|
|
||||||
x + innerWidth, y,
|
|
||||||
colorWhite(),
|
|
||||||
uv.u0, uv.v0,
|
|
||||||
uv.u1, uv.v1
|
|
||||||
)
|
|
||||||
|
|
||||||
-- Bottom
|
|
||||||
uv = tilesetPositionGetUV(tilesetBorder, 1, 10)
|
|
||||||
spriteBatchPush(textureBorder,
|
|
||||||
x, y + innerHeight,
|
|
||||||
x + innerWidth, y + innerHeight + tilesetBorder.tileHeight,
|
|
||||||
colorWhite(),
|
|
||||||
uv.u0, uv.v0,
|
|
||||||
uv.u1, uv.v1
|
|
||||||
)
|
|
||||||
|
|
||||||
-- Left
|
|
||||||
uv = tilesetPositionGetUV(tilesetBorder, 0, 1)
|
|
||||||
spriteBatchPush(textureBorder,
|
|
||||||
x - tilesetBorder.tileWidth, y,
|
|
||||||
x, y + innerHeight,
|
|
||||||
colorWhite(),
|
|
||||||
uv.u0, uv.v0,
|
|
||||||
uv.u1, uv.v1
|
|
||||||
)
|
|
||||||
|
|
||||||
-- Right
|
|
||||||
uv = tilesetPositionGetUV(tilesetBorder, 10, 1)
|
|
||||||
spriteBatchPush(textureBorder,
|
|
||||||
x + innerWidth, y,
|
|
||||||
x + innerWidth + tilesetBorder.tileWidth, y + innerHeight,
|
|
||||||
colorWhite(),
|
|
||||||
uv.u0, uv.v0,
|
|
||||||
uv.u1, uv.v1
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
x = 0
|
|
||||||
y = 0
|
|
||||||
|
|
||||||
function sceneDispose()
|
|
||||||
end
|
|
||||||
|
|
||||||
function sceneUpdate()
|
|
||||||
x = x + inputAxis(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT)
|
|
||||||
y = y + inputAxis(INPUT_ACTION_UP, INPUT_ACTION_DOWN)
|
|
||||||
end
|
|
||||||
|
|
||||||
function sceneRender()
|
|
||||||
-- Update camera
|
|
||||||
camera.bottom = 0
|
|
||||||
camera.top = screenGetHeight()
|
|
||||||
camera.right = screenGetWidth()
|
|
||||||
|
|
||||||
shaderBind(SHADER_UNLIT)
|
|
||||||
proj = cameraGetProjectionMatrix(camera)
|
|
||||||
shaderSetMatrix(SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj)
|
|
||||||
view = cameraGetViewMatrix(camera)
|
|
||||||
shaderSetMatrix(SHADER_UNLIT, SHADER_UNLIT_VIEW, view)
|
|
||||||
|
|
||||||
shaderSetTexture(SHADER_UNLIT, SHADER_UNLIT_TEXTURE, nil)
|
|
||||||
spriteBatchPush(
|
|
||||||
x, y,
|
|
||||||
x + 32, y + 32,
|
|
||||||
colorWhite()
|
|
||||||
)
|
|
||||||
spriteBatchFlush()
|
|
||||||
|
|
||||||
textDraw(10, 10, "Hello World\nHow are you?", colorRed())
|
|
||||||
spriteBatchFlush()
|
|
||||||
|
|
||||||
-- Update mouse position
|
|
||||||
-- if INPUT_POINTER then
|
|
||||||
-- mouseX = inputGetValue(INPUT_ACTION_POINTERX) * screenGetWidth()
|
|
||||||
-- mouseY = inputGetValue(INPUT_ACTION_POINTERY) * screenGetHeight()
|
|
||||||
|
|
||||||
-- -- Draw cursor
|
|
||||||
-- spriteBatchPush(
|
|
||||||
-- nil,
|
|
||||||
-- mouseX - 2, mouseY - 2,
|
|
||||||
-- mouseX + 2, mouseY + 2,
|
|
||||||
-- colorRed(),
|
|
||||||
-- 0, 0,
|
|
||||||
-- 1, 1
|
|
||||||
-- )
|
|
||||||
-- end
|
|
||||||
|
|
||||||
|
|
||||||
-- textDraw(10, 10, "Hello World")
|
|
||||||
|
|
||||||
-- centerX = math.floor(screenGetWidth() / 2)
|
|
||||||
-- centerY = math.floor(screenGetHeight() / 2)
|
|
||||||
|
|
||||||
-- Draw elements
|
|
||||||
-- backgroundDraw()
|
|
||||||
-- borderDraw(
|
|
||||||
-- centerX - (boardWidth / 2), centerY - (boardHeight / 2),
|
|
||||||
-- boardWidth, boardHeight
|
|
||||||
-- )
|
|
||||||
|
|
||||||
-- i = 0
|
|
||||||
-- -- Foreach cell
|
|
||||||
-- local offX = centerX - (boardWidth / 2)
|
|
||||||
-- local offY = centerY - (boardHeight / 2)
|
|
||||||
-- for y = 0, sweeperRows - 1 do
|
|
||||||
-- for x = 0, sweepwerCols - 1 do
|
|
||||||
-- i = y * sweepwerCols + x
|
|
||||||
|
|
||||||
-- -- Hovered
|
|
||||||
-- if
|
|
||||||
-- cells[i] == CELL_STATE_DEFAULT and
|
|
||||||
-- mouseX >= x * tilesetCell.tileWidth + offX and mouseX < (x + 1) * tilesetCell.tileWidth + offX and
|
|
||||||
-- mouseY >= y * tilesetCell.tileHeight + offY and mouseY < (y + 1) * tilesetCell.tileHeight + offY
|
|
||||||
-- then
|
|
||||||
-- cells[i] = CELL_STATE_HOVER
|
|
||||||
-- else
|
|
||||||
-- cells[i] = CELL_STATE_DEFAULT
|
|
||||||
-- end
|
|
||||||
|
|
||||||
-- cellDraw(
|
|
||||||
-- x * tilesetCell.tileWidth + offX,
|
|
||||||
-- y * tilesetCell.tileHeight + offY,
|
|
||||||
-- cells[i]
|
|
||||||
-- )
|
|
||||||
-- end
|
|
||||||
-- end
|
|
||||||
end
|
|
||||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 1.2 KiB |
@@ -0,0 +1,31 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
include(FetchContent)
|
||||||
|
FetchContent_Declare(
|
||||||
|
stb
|
||||||
|
GIT_REPOSITORY https://github.com/nothings/stb.git
|
||||||
|
)
|
||||||
|
|
||||||
|
# Fetch stb if not already done
|
||||||
|
FetchContent_MakeAvailable(stb)
|
||||||
|
|
||||||
|
# Find the stb_image.h header
|
||||||
|
set(STB_INCLUDE_DIR "${stb_SOURCE_DIR}")
|
||||||
|
set(STB_IMAGE_HEADER "${stb_SOURCE_DIR}/stb_image.h")
|
||||||
|
|
||||||
|
if(EXISTS "${STB_IMAGE_HEADER}")
|
||||||
|
add_library(stb_image INTERFACE)
|
||||||
|
target_include_directories(stb_image INTERFACE "${STB_INCLUDE_DIR}")
|
||||||
|
set(STB_IMAGE_FOUND TRUE)
|
||||||
|
else()
|
||||||
|
set(STB_IMAGE_FOUND FALSE)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Standard CMake variables
|
||||||
|
set(STB_IMAGE_INCLUDE_DIRS "${STB_INCLUDE_DIR}")
|
||||||
|
set(STB_IMAGE_LIBRARIES stb_image)
|
||||||
|
|
||||||
|
mark_as_advanced(STB_IMAGE_INCLUDE_DIRS STB_IMAGE_LIBRARIES STB_IMAGE_FOUND)
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
include(FetchContent)
|
||||||
|
|
||||||
|
if(NOT TARGET yyjson)
|
||||||
|
FetchContent_Declare(
|
||||||
|
yyjson
|
||||||
|
GIT_REPOSITORY https://github.com/ibireme/yyjson.git
|
||||||
|
GIT_TAG 0.12.0
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(yyjson)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Provide an imported target if not already available
|
||||||
|
if(NOT TARGET yyjson::yyjson)
|
||||||
|
add_library(yyjson::yyjson ALIAS yyjson)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Mark yyjson as found for find_package compatibility
|
||||||
|
set(yyjson_FOUND TRUE)
|
||||||
@@ -2,4 +2,9 @@ include(cmake/targets/dolphin.cmake)
|
|||||||
|
|
||||||
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||||
DUSK_GAMECUBE
|
DUSK_GAMECUBE
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Link libraries
|
||||||
|
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PRIVATE
|
||||||
|
# bba
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
# Find link platform-specific libraries
|
# Find link platform-specific libraries
|
||||||
find_package(SDL2 REQUIRED)
|
find_package(SDL2 REQUIRED)
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
|
# find_package(CURL REQUIRED)
|
||||||
|
|
||||||
# Setup endianess at compile time to optimize.
|
# Setup endianess at compile time to optimize.
|
||||||
include(TestBigEndian)
|
include(TestBigEndian)
|
||||||
@@ -16,12 +17,13 @@ else()
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Link required libraries.
|
# Link required libraries.
|
||||||
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||||
SDL2
|
SDL2
|
||||||
pthread
|
pthread
|
||||||
OpenGL::GL
|
OpenGL::GL
|
||||||
GL
|
GL
|
||||||
m
|
m
|
||||||
|
# CURL::libcurl
|
||||||
)
|
)
|
||||||
|
|
||||||
# Define platform-specific macros.
|
# Define platform-specific macros.
|
||||||
@@ -38,4 +40,6 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
|||||||
DUSK_INPUT_POINTER
|
DUSK_INPUT_POINTER
|
||||||
DUSK_INPUT_GAMEPAD
|
DUSK_INPUT_GAMEPAD
|
||||||
DUSK_TIME_DYNAMIC
|
DUSK_TIME_DYNAMIC
|
||||||
|
DUSK_NETWORK_IPV6
|
||||||
|
THREAD_PTHREAD=1
|
||||||
)
|
)
|
||||||
@@ -24,6 +24,11 @@ target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
|||||||
pspvfpu
|
pspvfpu
|
||||||
pspvram
|
pspvram
|
||||||
psphprm
|
psphprm
|
||||||
|
pspnet
|
||||||
|
pspnet_inet
|
||||||
|
pspnet_apctl
|
||||||
|
psphttp
|
||||||
|
pspssl
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(${DUSK_LIBRARY_TARGET_NAME} PRIVATE
|
target_include_directories(${DUSK_LIBRARY_TARGET_NAME} PRIVATE
|
||||||
@@ -39,6 +44,7 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
|||||||
DUSK_OPENGL_LEGACY
|
DUSK_OPENGL_LEGACY
|
||||||
DUSK_DISPLAY_WIDTH=480
|
DUSK_DISPLAY_WIDTH=480
|
||||||
DUSK_DISPLAY_HEIGHT=272
|
DUSK_DISPLAY_HEIGHT=272
|
||||||
|
THREAD_PTHREAD=1
|
||||||
)
|
)
|
||||||
|
|
||||||
# Postbuild, create .pbp file for PSP.
|
# Postbuild, create .pbp file for PSP.
|
||||||
|
|||||||
@@ -0,0 +1,110 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
if(NOT DEFINED ENV{VITASDK})
|
||||||
|
message(FATAL_ERROR "VITASDK environment variable is not set.")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
include("$ENV{VITASDK}/share/vita.cmake" REQUIRED)
|
||||||
|
|
||||||
|
set(VITA_APP_NAME "Dusk")
|
||||||
|
set(VITA_TITLEID "DUSK00001")
|
||||||
|
set(VITA_VERSION "01.00")
|
||||||
|
|
||||||
|
find_package(SDL2 REQUIRED)
|
||||||
|
|
||||||
|
# Custom flags for cglm
|
||||||
|
set(CGLM_SHARED OFF CACHE BOOL "Build cglm shared" FORCE)
|
||||||
|
set(CGLM_STATIC ON CACHE BOOL "Build cglm static" FORCE)
|
||||||
|
find_package(cglm REQUIRED)
|
||||||
|
|
||||||
|
# Compile lua
|
||||||
|
include(FetchContent)
|
||||||
|
FetchContent_Declare(
|
||||||
|
liblua
|
||||||
|
URL https://www.lua.org/ftp/lua-5.5.0.tar.gz
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(liblua)
|
||||||
|
set(LUA_SRC_DIR "${liblua_SOURCE_DIR}/src")
|
||||||
|
set(LUA_C_FILES
|
||||||
|
lapi.c lauxlib.c lbaselib.c lcode.c lcorolib.c lctype.c ldblib.c ldebug.c
|
||||||
|
ldo.c ldump.c lfunc.c lgc.c linit.c liolib.c llex.c lmathlib.c lmem.c
|
||||||
|
loadlib.c lobject.c lopcodes.c loslib.c lparser.c lstate.c lstring.c
|
||||||
|
lstrlib.c ltable.c ltablib.c ltm.c lundump.c lutf8lib.c lvm.c lzio.c
|
||||||
|
)
|
||||||
|
list(TRANSFORM LUA_C_FILES PREPEND "${LUA_SRC_DIR}/")
|
||||||
|
add_library(liblua STATIC ${LUA_C_FILES})
|
||||||
|
target_include_directories(liblua PUBLIC "${LUA_SRC_DIR}")
|
||||||
|
target_compile_definitions(liblua PRIVATE LUA_USE_C89)
|
||||||
|
add_library(lua::lua ALIAS liblua)
|
||||||
|
set(Lua_FOUND TRUE CACHE BOOL "Lua found" FORCE)
|
||||||
|
|
||||||
|
# Link libraries
|
||||||
|
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||||
|
${SDL2_LIBRARIES}
|
||||||
|
liblua
|
||||||
|
cglm
|
||||||
|
SDL2
|
||||||
|
SDL2main
|
||||||
|
zip
|
||||||
|
bz2
|
||||||
|
z
|
||||||
|
zstd
|
||||||
|
crypto
|
||||||
|
lzma
|
||||||
|
m
|
||||||
|
pthread
|
||||||
|
stdc++
|
||||||
|
vitaGL
|
||||||
|
mathneon
|
||||||
|
vitashark
|
||||||
|
kubridge_stub
|
||||||
|
SceAppMgr_stub
|
||||||
|
SceAudio_stub
|
||||||
|
SceCtrl_stub
|
||||||
|
SceCommonDialog_stub
|
||||||
|
SceDisplay_stub
|
||||||
|
SceKernelDmacMgr_stub
|
||||||
|
SceGxm_stub
|
||||||
|
SceShaccCg_stub
|
||||||
|
SceSysmodule_stub
|
||||||
|
ScePower_stub
|
||||||
|
SceTouch_stub
|
||||||
|
SceVshBridge_stub
|
||||||
|
SceIofilemgr_stub
|
||||||
|
SceShaccCgExt
|
||||||
|
libtaihen_stub.a
|
||||||
|
|
||||||
|
|
||||||
|
# SceKernel_stub
|
||||||
|
SceAppUtil_stub
|
||||||
|
SceHid_stub
|
||||||
|
SceRtc_stub
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(${DUSK_LIBRARY_TARGET_NAME} PRIVATE
|
||||||
|
${SDL2_INCLUDE_DIRS}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||||
|
DUSK_SDL2
|
||||||
|
DUSK_OPENGL
|
||||||
|
DUSK_VITA
|
||||||
|
DUSK_INPUT_GAMEPAD
|
||||||
|
DUSK_PLATFORM_ENDIAN_LITTLE
|
||||||
|
DUSK_OPENGL_LEGACY
|
||||||
|
DUSK_DISPLAY_WIDTH=960
|
||||||
|
DUSK_DISPLAY_HEIGHT=544
|
||||||
|
)
|
||||||
|
|
||||||
|
# Post-build: create SELF from the ELF binary (UNSAFE = homebrew, no signing)
|
||||||
|
vita_create_self(${DUSK_BINARY_TARGET_NAME}.self ${DUSK_BINARY_TARGET_NAME} UNSAFE)
|
||||||
|
|
||||||
|
# Post-build: package SELF + assets into a .vpk installable on the Vita
|
||||||
|
vita_create_vpk(${DUSK_BINARY_TARGET_NAME}.vpk ${VITA_TITLEID} ${DUSK_BINARY_TARGET_NAME}.self
|
||||||
|
VERSION ${VITA_VERSION}
|
||||||
|
NAME ${VITA_APP_NAME}
|
||||||
|
FILE ${DUSK_ASSETS_ZIP} dusk.dsk
|
||||||
|
)
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
FROM vitasdk/vitasdk:latest
|
||||||
|
WORKDIR /workdir
|
||||||
|
|
||||||
|
# Install vitaGL and its dependencies (vitashark, SceShaccCg) via vdpm
|
||||||
|
RUN which vdpm
|
||||||
|
|
||||||
|
# Install Python (needed for Dusk code generation tools)
|
||||||
|
RUN apk add --no-cache \
|
||||||
|
python3 \
|
||||||
|
py3-pip \
|
||||||
|
py3-dotenv
|
||||||
|
|
||||||
|
VOLUME ["/workdir"]
|
||||||
Executable
+3
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
docker build -t dusk-vita -f docker/vita/Dockerfile .
|
||||||
|
docker run --rm -v $(pwd):/workdir dusk-vita /bin/bash -c "./scripts/build-vita.sh"
|
||||||
Executable
+13
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
if [ -z "$VITASDK" ]; then
|
||||||
|
echo "VITASDK environment variable is not set. Please set it to the path of your VitaSDK installation."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p build-vita
|
||||||
|
cd build-vita
|
||||||
|
cmake \
|
||||||
|
-DCMAKE_TOOLCHAIN_FILE=$VITASDK/share/vita.toolchain.cmake \
|
||||||
|
-DDUSK_TARGET_SYSTEM=vita \
|
||||||
|
..
|
||||||
|
make -j$(nproc)
|
||||||
+6
-1
@@ -16,7 +16,12 @@ elseif(DUSK_TARGET_SYSTEM STREQUAL "psp")
|
|||||||
add_subdirectory(dusksdl2)
|
add_subdirectory(dusksdl2)
|
||||||
add_subdirectory(duskgl)
|
add_subdirectory(duskgl)
|
||||||
|
|
||||||
elseif(DUSK_TARGET_SYSTEM STREQUAL "gamecube" OR DUSK_TARGET_SYSTEM STREQUAL "wii")
|
elseif(DUSK_TARGET_SYSTEM STREQUAL "vita")
|
||||||
|
add_subdirectory(duskvita)
|
||||||
|
add_subdirectory(dusksdl2)
|
||||||
|
add_subdirectory(duskgl)
|
||||||
|
|
||||||
|
elseif(DUSK_TARGET_SYSTEM STREQUAL "wii" OR DUSK_TARGET_SYSTEM STREQUAL "gamecube")
|
||||||
add_subdirectory(duskdolphin)
|
add_subdirectory(duskdolphin)
|
||||||
|
|
||||||
endif()
|
endif()
|
||||||
+23
-7
@@ -14,6 +14,24 @@ if(NOT libzip_FOUND)
|
|||||||
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC zip)
|
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC zip)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if(NOT stb_image_FOUND)
|
||||||
|
find_package(stb REQUIRED)
|
||||||
|
if(STB_IMAGE_FOUND)
|
||||||
|
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC stb_image)
|
||||||
|
else()
|
||||||
|
message(FATAL_ERROR "stb_image not found. Please ensure stb is correctly fetched.")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT yyjson_FOUND)
|
||||||
|
find_package(yyjson REQUIRED)
|
||||||
|
if(yyjson_FOUND)
|
||||||
|
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC yyjson::yyjson)
|
||||||
|
else()
|
||||||
|
message(FATAL_ERROR "yyjson not found. Please ensure yyjson is correctly fetched.")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
if(NOT Lua_FOUND)
|
if(NOT Lua_FOUND)
|
||||||
find_package(Lua REQUIRED)
|
find_package(Lua REQUIRED)
|
||||||
if(Lua_FOUND AND NOT TARGET Lua::Lua)
|
if(Lua_FOUND AND NOT TARGET Lua::Lua)
|
||||||
@@ -40,25 +58,23 @@ target_sources(${DUSK_BINARY_TARGET_NAME}
|
|||||||
main.c
|
main.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# Defs
|
|
||||||
dusk_env_to_h(duskdefs.env duskdefs.h)
|
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
add_subdirectory(assert)
|
add_subdirectory(assert)
|
||||||
add_subdirectory(asset)
|
add_subdirectory(asset)
|
||||||
add_subdirectory(log)
|
add_subdirectory(log)
|
||||||
add_subdirectory(display)
|
add_subdirectory(display)
|
||||||
add_subdirectory(engine)
|
add_subdirectory(engine)
|
||||||
|
add_subdirectory(entity)
|
||||||
add_subdirectory(error)
|
add_subdirectory(error)
|
||||||
add_subdirectory(event)
|
add_subdirectory(event)
|
||||||
add_subdirectory(input)
|
add_subdirectory(input)
|
||||||
add_subdirectory(locale)
|
add_subdirectory(locale)
|
||||||
|
add_subdirectory(physics)
|
||||||
add_subdirectory(scene)
|
add_subdirectory(scene)
|
||||||
add_subdirectory(script)
|
add_subdirectory(script)
|
||||||
|
add_subdirectory(system)
|
||||||
add_subdirectory(time)
|
add_subdirectory(time)
|
||||||
add_subdirectory(ui)
|
add_subdirectory(ui)
|
||||||
|
add_subdirectory(network)
|
||||||
add_subdirectory(util)
|
add_subdirectory(util)
|
||||||
|
add_subdirectory(thread)
|
||||||
# if(DUSK_TARGET_SYSTEM STREQUAL "linux" OR DUSK_TARGET_SYSTEM STREQUAL "psp")
|
|
||||||
# add_subdirectory(thread)
|
|
||||||
# endif()
|
|
||||||
@@ -216,4 +216,9 @@
|
|||||||
#define assertStrLenMax(str, len, message) ((void)0)
|
#define assertStrLenMax(str, len, message) ((void)0)
|
||||||
#define assertStrLenMin(str, len, message) ((void)0)
|
#define assertStrLenMin(str, len, message) ((void)0)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define assertStructSize(struct, size) \
|
||||||
|
_Static_assert(sizeof(struct) == size, "Size of " #struct " must be " #size)
|
||||||
|
|
||||||
|
// EOF
|
||||||
|
|||||||
@@ -7,7 +7,8 @@
|
|||||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
PUBLIC
|
PUBLIC
|
||||||
asset.c
|
asset.c
|
||||||
|
assetfile.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
add_subdirectory(type)
|
add_subdirectory(loader)
|
||||||
+11
-99
@@ -9,7 +9,6 @@
|
|||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "asset/assettype.h"
|
|
||||||
#include "engine/engine.h"
|
#include "engine/engine.h"
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
|
|
||||||
@@ -33,107 +32,20 @@ bool_t assetFileExists(const char_t *filename) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t assetLoad(const char_t *filename, void *output) {
|
errorret_t assetLoad(
|
||||||
|
const char_t *filename,
|
||||||
|
assetfileloader_t loader,
|
||||||
|
void *params,
|
||||||
|
void *output
|
||||||
|
) {
|
||||||
assertStrLenMax(filename, FILENAME_MAX, "Filename too long.");
|
assertStrLenMax(filename, FILENAME_MAX, "Filename too long.");
|
||||||
assertNotNull(output, "Output pointer cannot be NULL.");
|
assertNotNull(output, "Output pointer cannot be NULL.");
|
||||||
|
assertNotNull(loader, "Asset file loader cannot be NULL.");
|
||||||
|
|
||||||
// Determine the asset type by reading the extension
|
assetfile_t file;
|
||||||
const assettypedef_t *def = NULL;
|
errorChain(assetFileInit(&file, filename, params, output));
|
||||||
for(uint_fast8_t i = 0; i < ASSET_TYPE_COUNT; i++) {
|
errorChain(loader(&file));
|
||||||
const assettypedef_t *cmp = &ASSET_TYPE_DEFINITIONS[i];
|
errorChain(assetFileDispose(&file));
|
||||||
assertNotNull(cmp, "Asset type definition cannot be NULL.");
|
|
||||||
if(cmp->extension == NULL) continue;
|
|
||||||
if(!stringEndsWithCaseInsensitive(filename, cmp->extension)) continue;
|
|
||||||
def = cmp;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(def == NULL) {
|
|
||||||
errorThrow("Unknown asset type for file: %s", filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get file size of the asset.
|
|
||||||
zip_stat_t st;
|
|
||||||
zip_stat_init(&st);
|
|
||||||
if(!zip_stat(ASSET.zip, filename, 0, &st) == 0) {
|
|
||||||
errorThrow("Failed to stat asset file: %s", filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Minimum file size.
|
|
||||||
zip_int64_t fileSize = (zip_int64_t)st.size;
|
|
||||||
if(fileSize <= 0) {
|
|
||||||
errorThrow("Asset file is empty: %s", filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to open the file
|
|
||||||
zip_file_t *file = zip_fopen(ASSET.zip, filename, 0);
|
|
||||||
if(file == NULL) {
|
|
||||||
errorThrow("Failed to open asset file: %s", filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load the asset data
|
|
||||||
switch(def->loadStrategy) {
|
|
||||||
case ASSET_LOAD_STRAT_ENTIRE:
|
|
||||||
assertNotNull(def->entire, "Asset load function cannot be NULL.");
|
|
||||||
|
|
||||||
// Must have more to read
|
|
||||||
if(fileSize <= 0) {
|
|
||||||
zip_fclose(file);
|
|
||||||
errorThrow("No data remaining to read for asset: %s", filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fileSize > def->dataSize) {
|
|
||||||
zip_fclose(file);
|
|
||||||
errorThrow(
|
|
||||||
"Asset file has too much data remaining after header: %s",
|
|
||||||
filename
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create space to read the entire asset data
|
|
||||||
void *data = memoryAllocate(fileSize);
|
|
||||||
if(!data) {
|
|
||||||
zip_fclose(file);
|
|
||||||
errorThrow(
|
|
||||||
"Failed to allocate memory for asset data of file: %s", filename
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read in the asset data.
|
|
||||||
zip_int64_t bytesRead = zip_fread(file, data, fileSize);
|
|
||||||
if(bytesRead == 0 || bytesRead > fileSize) {
|
|
||||||
memoryFree(data);
|
|
||||||
zip_fclose(file);
|
|
||||||
errorThrow("Failed to read asset data for file: %s", filename);
|
|
||||||
}
|
|
||||||
fileSize -= bytesRead;
|
|
||||||
|
|
||||||
// Close the file now we have the data
|
|
||||||
zip_fclose(file);
|
|
||||||
|
|
||||||
// Pass to the asset type loader
|
|
||||||
assetentire_t entire = {
|
|
||||||
.data = data,
|
|
||||||
.output = output
|
|
||||||
};
|
|
||||||
errorret_t ret = def->entire(entire);
|
|
||||||
memoryFree(data);
|
|
||||||
|
|
||||||
errorChain(ret);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ASSET_LOAD_STRAT_CUSTOM:
|
|
||||||
assertNotNull(def->custom, "Asset load function cannot be NULL.");
|
|
||||||
assetcustom_t customData = {
|
|
||||||
.zipFile = file,
|
|
||||||
.output = output
|
|
||||||
};
|
|
||||||
errorChain(def->custom(customData));
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
assertUnreachable("Unknown asset load strategy.");
|
|
||||||
}
|
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-4
@@ -7,8 +7,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "error/error.h"
|
#include "error/error.h"
|
||||||
#include "assettype.h"
|
|
||||||
#include "asset/assetplatform.h"
|
#include "asset/assetplatform.h"
|
||||||
|
#include "assetfile.h"
|
||||||
|
|
||||||
#ifndef assetInitPlatform
|
#ifndef assetInitPlatform
|
||||||
#error "Platform must define assetInitPlatform function."
|
#error "Platform must define assetInitPlatform function."
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
#define ASSET_FILE_NAME "dusk.dsk"
|
#define ASSET_FILE_NAME "dusk.dsk"
|
||||||
#define ASSET_HEADER_SIZE 3
|
#define ASSET_HEADER_SIZE 3
|
||||||
|
|
||||||
typedef struct {
|
typedef struct asset_s {
|
||||||
zip_t *zip;
|
zip_t *zip;
|
||||||
assetplatform_t platform;
|
assetplatform_t platform;
|
||||||
} asset_t;
|
} asset_t;
|
||||||
@@ -41,13 +41,20 @@ errorret_t assetInit(void);
|
|||||||
bool_t assetFileExists(const char_t *filename);
|
bool_t assetFileExists(const char_t *filename);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads an asset by its filename, the output type depends on the asset type.
|
* Loads an asset by its filename,
|
||||||
*
|
*
|
||||||
* @param filename The filename of the asset to retrieve.
|
* @param filename The filename of the asset to retrieve.
|
||||||
|
* @param loader Loader to use for loading the asset file.
|
||||||
|
* @param params Parameters to pass to the loader.
|
||||||
* @param output The output pointer to store the loaded asset data.
|
* @param output The output pointer to store the loaded asset data.
|
||||||
* @return An error code if the asset could not be loaded.
|
* @return An error code if the asset could not be loaded.
|
||||||
*/
|
*/
|
||||||
errorret_t assetLoad(const char_t *filename, void *output);
|
errorret_t assetLoad(
|
||||||
|
const char_t *filename,
|
||||||
|
assetfileloader_t loader,
|
||||||
|
void *params,
|
||||||
|
void *output
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disposes/cleans up the asset system.
|
* Disposes/cleans up the asset system.
|
||||||
|
|||||||
@@ -0,0 +1,323 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asset/asset.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
#include "util/math.h"
|
||||||
|
|
||||||
|
errorret_t assetFileInit(
|
||||||
|
assetfile_t *file,
|
||||||
|
const char_t *filename,
|
||||||
|
void *params,
|
||||||
|
void *output
|
||||||
|
) {
|
||||||
|
memoryZero(file, sizeof(assetfile_t));
|
||||||
|
|
||||||
|
file->filename = filename;
|
||||||
|
file->params = params;
|
||||||
|
file->output = output;
|
||||||
|
|
||||||
|
// Stat the file
|
||||||
|
zip_stat_init(&file->stat);
|
||||||
|
if(!zip_stat(ASSET.zip, filename, 0, &file->stat) == 0) {
|
||||||
|
errorThrow("Failed to stat asset file: %s", filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Minimum file size.
|
||||||
|
file->size = (zip_int64_t)file->stat.size;
|
||||||
|
if(file->size <= 0) {
|
||||||
|
errorThrow("Invalid asset file size: %s", filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileRewind(assetfile_t *file) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(file->zipFile, "Asset file must be opened before rewinding.");
|
||||||
|
|
||||||
|
if(file->position == 0) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorChain(assetFileClose(file));
|
||||||
|
errorChain(assetFileOpen(file));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileOpen(assetfile_t *file) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(file->filename, "Asset file filename cannot be NULL.");
|
||||||
|
assertNotNull(ASSET.zip, "Asset zip cannot be NULL.");
|
||||||
|
assertNull(file->zipFile, "Asset file already open.");
|
||||||
|
|
||||||
|
file->zipFile = zip_fopen(ASSET.zip, file->filename, 0);
|
||||||
|
if(file->zipFile == NULL) {
|
||||||
|
errorThrow("Failed to open asset file: %s", file->filename);
|
||||||
|
}
|
||||||
|
file->position = 0;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileRead(
|
||||||
|
assetfile_t *file,
|
||||||
|
void *buffer,
|
||||||
|
const size_t bufferSize
|
||||||
|
) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(file->zipFile, "Asset file must be opened before reading.");
|
||||||
|
|
||||||
|
if(buffer == NULL) {
|
||||||
|
size_t bytesRemaining = bufferSize;
|
||||||
|
uint8_t tempBuffer[256];
|
||||||
|
while(bytesRemaining > 0) {
|
||||||
|
size_t chunkSize = mathMin(bytesRemaining, sizeof(tempBuffer));
|
||||||
|
errorChain(assetFileRead(file, tempBuffer, chunkSize));
|
||||||
|
file->position += chunkSize;
|
||||||
|
bytesRemaining -= chunkSize;
|
||||||
|
}
|
||||||
|
file->lastRead = bufferSize;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
// I assume zip_fread takes buffer NULL for skipping?
|
||||||
|
zip_int64_t bytesRead = zip_fread(file->zipFile, buffer, bufferSize);
|
||||||
|
if(bytesRead < 0) {
|
||||||
|
errorThrow("Failed to read from asset file: %s", file->filename);
|
||||||
|
}
|
||||||
|
file->position += bytesRead;
|
||||||
|
file->lastRead = bytesRead;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileClose(assetfile_t *file) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(file->zipFile, "Asset file must be opened before closing.");
|
||||||
|
|
||||||
|
if(zip_fclose(file->zipFile) != 0) {
|
||||||
|
errorThrow("Failed to close asset file: %s", file->filename);
|
||||||
|
}
|
||||||
|
file->zipFile = NULL;
|
||||||
|
file->position = 0;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileDispose(assetfile_t *file) {
|
||||||
|
if(file->zipFile != NULL) {
|
||||||
|
errorChain(assetFileClose(file));
|
||||||
|
}
|
||||||
|
memoryZero(file, sizeof(assetfile_t));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Line Reader;
|
||||||
|
void assetFileLineReaderInit(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
assetfile_t *file,
|
||||||
|
uint8_t *readBuffer,
|
||||||
|
const size_t readBufferSize,
|
||||||
|
uint8_t *outBuffer,
|
||||||
|
const size_t outBufferSize
|
||||||
|
) {
|
||||||
|
assertNotNull(reader, "Line reader cannot be NULL.");
|
||||||
|
assertNotNull(file, " File cannot be NULL.");
|
||||||
|
assertNotNull(readBuffer, "Read buffer cannot be NULL.");
|
||||||
|
assertNotNull(outBuffer, "Output buffer cannot be NULL.");
|
||||||
|
assertTrue(readBufferSize > 0, "Read buffer size must be greater than 0.");
|
||||||
|
assertTrue(outBufferSize > 0, "Output buffer size must be greater than 0.");
|
||||||
|
|
||||||
|
memoryZero(reader, sizeof(assetfilelinereader_t));
|
||||||
|
|
||||||
|
reader->file = file;
|
||||||
|
reader->readBuffer = readBuffer;
|
||||||
|
reader->readBufferSize = readBufferSize;
|
||||||
|
reader->outBuffer = outBuffer;
|
||||||
|
reader->outBufferSize = outBufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t assetFileLineReaderUnreadBytes(const assetfilelinereader_t *reader) {
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertTrue(reader->bufferEnd >= reader->bufferStart, "Invalid buffer state.");
|
||||||
|
return reader->bufferEnd - reader->bufferStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t *assetFileLineReaderUnreadPtr(const assetfilelinereader_t *reader) {
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->readBuffer, "Read buffer cannot be NULL.");
|
||||||
|
return reader->readBuffer + reader->bufferStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
static errorret_t assetFileLineReaderAppend(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
const uint8_t *src,
|
||||||
|
size_t srcLength
|
||||||
|
) {
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->outBuffer, "Out buffer cannot be NULL.");
|
||||||
|
|
||||||
|
if(srcLength == 0) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* reserve room for optional NUL terminator */
|
||||||
|
if(reader->lineLength + srcLength >= reader->outBufferSize) {
|
||||||
|
errorThrow("Line length exceeds output buffer size.");
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryCopy(reader->outBuffer + reader->lineLength, src, srcLength);
|
||||||
|
reader->lineLength += srcLength;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void assetFileLineReaderTerminate(assetfilelinereader_t *reader) {
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->outBuffer, "Out buffer cannot be NULL.");
|
||||||
|
assertTrue(reader->lineLength < reader->outBufferSize, "Line length exceeds out buffer.");
|
||||||
|
reader->outBuffer[reader->lineLength] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t assetFileLineReaderFindNewline(const assetfilelinereader_t *reader) {
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->readBuffer, "Read buffer cannot be NULL.");
|
||||||
|
|
||||||
|
for(i = reader->bufferStart; i < reader->bufferEnd; ++i) {
|
||||||
|
if(reader->readBuffer[i] == '\n') {
|
||||||
|
return (ssize_t)i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileLineReaderFill(assetfilelinereader_t *reader) {
|
||||||
|
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->file, "File cannot be NULL.");
|
||||||
|
assertNotNull(reader->readBuffer, "Read buffer cannot be NULL.");
|
||||||
|
|
||||||
|
if(reader->eof) errorOk();
|
||||||
|
|
||||||
|
errorret_t ret;
|
||||||
|
|
||||||
|
size_t unreadBytes = assetFileLineReaderUnreadBytes(reader);
|
||||||
|
|
||||||
|
/* If buffer is fully consumed, refill from start. */
|
||||||
|
if(unreadBytes == 0) {
|
||||||
|
reader->bufferStart = 0;
|
||||||
|
reader->bufferEnd = 0;
|
||||||
|
|
||||||
|
errorChain(assetFileRead(
|
||||||
|
reader->file,
|
||||||
|
reader->readBuffer,
|
||||||
|
reader->readBufferSize
|
||||||
|
));
|
||||||
|
|
||||||
|
if(reader->file->lastRead == 0) {
|
||||||
|
reader->eof = true;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
reader->bufferStart = 0;
|
||||||
|
reader->bufferEnd = reader->file->lastRead;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* There are unread bytes left but no newline in them.
|
||||||
|
* If bufferStart > 0, slide unread bytes to front so we can read more.
|
||||||
|
* This only happens when necessary to make space.
|
||||||
|
*/
|
||||||
|
if(reader->bufferEnd == reader->readBufferSize) {
|
||||||
|
if(reader->bufferStart == 0) {
|
||||||
|
/*
|
||||||
|
* Entire read buffer is unread and contains no newline.
|
||||||
|
* Caller must have a large enough outBuffer to accumulate across fills,
|
||||||
|
* so we consume these bytes into outBuffer before refilling.
|
||||||
|
*/
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryMove(
|
||||||
|
reader->readBuffer,
|
||||||
|
reader->readBuffer + reader->bufferStart,
|
||||||
|
unreadBytes
|
||||||
|
);
|
||||||
|
reader->bufferStart = 0;
|
||||||
|
reader->bufferEnd = unreadBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorChain(assetFileRead(
|
||||||
|
reader->file,
|
||||||
|
reader->readBuffer + reader->bufferEnd,
|
||||||
|
reader->readBufferSize - reader->bufferEnd
|
||||||
|
));
|
||||||
|
|
||||||
|
if(reader->file->lastRead == 0) {
|
||||||
|
reader->eof = true;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
reader->bufferEnd += reader->file->lastRead;
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetFileLineReaderNext(assetfilelinereader_t *reader) {
|
||||||
|
assertNotNull(reader, "Reader cannot be NULL.");
|
||||||
|
assertNotNull(reader->file, "File cannot be NULL.");
|
||||||
|
assertNotNull(reader->readBuffer, "Read buffer cannot be NULL.");
|
||||||
|
assertNotNull(reader->outBuffer, "Out buffer cannot be NULL.");
|
||||||
|
|
||||||
|
reader->lineLength = 0;
|
||||||
|
|
||||||
|
for(;;) {
|
||||||
|
ssize_t newlineIndex = assetFileLineReaderFindNewline(reader);
|
||||||
|
|
||||||
|
if(newlineIndex >= 0) {
|
||||||
|
size_t chunkLength = (size_t)newlineIndex - reader->bufferStart;
|
||||||
|
errorret_t ret;
|
||||||
|
|
||||||
|
/* strip CR in CRLF */
|
||||||
|
if(chunkLength > 0 && reader->readBuffer[(size_t)newlineIndex - 1] == '\r') {
|
||||||
|
chunkLength--;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorChain(assetFileLineReaderAppend(
|
||||||
|
reader,
|
||||||
|
reader->readBuffer + reader->bufferStart,
|
||||||
|
chunkLength
|
||||||
|
));
|
||||||
|
|
||||||
|
reader->bufferStart = (size_t)newlineIndex + 1;
|
||||||
|
assetFileLineReaderTerminate(reader);
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(assetFileLineReaderUnreadBytes(reader) > 0) {
|
||||||
|
errorChain(assetFileLineReaderAppend(
|
||||||
|
reader,
|
||||||
|
assetFileLineReaderUnreadPtr(reader),
|
||||||
|
assetFileLineReaderUnreadBytes(reader)
|
||||||
|
));
|
||||||
|
|
||||||
|
reader->bufferStart = reader->bufferEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(reader->eof) {
|
||||||
|
if(reader->lineLength > 0) {
|
||||||
|
assetFileLineReaderTerminate(reader);
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorThrow("End of file reached.");
|
||||||
|
}
|
||||||
|
|
||||||
|
errorChain(assetFileLineReaderFill(reader));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "error/error.h"
|
||||||
|
#include <zip.h>
|
||||||
|
|
||||||
|
typedef struct assetfile_s assetfile_t;
|
||||||
|
|
||||||
|
typedef errorret_t (*assetfileloader_t)(assetfile_t *file);
|
||||||
|
|
||||||
|
typedef struct assetfile_s {
|
||||||
|
const char_t *filename;
|
||||||
|
void *params;
|
||||||
|
void *output;
|
||||||
|
|
||||||
|
zip_stat_t stat;
|
||||||
|
zip_int64_t size;
|
||||||
|
zip_int64_t position;
|
||||||
|
zip_int64_t lastRead;
|
||||||
|
zip_file_t *zipFile;
|
||||||
|
} assetfile_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the asset file structure in preparation for loading. This will
|
||||||
|
* stat the file but not open the handle.
|
||||||
|
*
|
||||||
|
* @param file The asset file structure to initialize.
|
||||||
|
* @param filename The name of the asset file to load.
|
||||||
|
* @param params Optional loader params.
|
||||||
|
* @param output Output pointer for the loader.
|
||||||
|
* @return Error indicating success or failure.
|
||||||
|
*/
|
||||||
|
errorret_t assetFileInit(
|
||||||
|
assetfile_t *file,
|
||||||
|
const char_t *filename,
|
||||||
|
void *params,
|
||||||
|
void *output
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens the asset file for reading. After opening the loader is responsible
|
||||||
|
* for closing the file.
|
||||||
|
*
|
||||||
|
* @param file The asset file to open.
|
||||||
|
* @return An error code if the file could not be opened.
|
||||||
|
*/
|
||||||
|
errorret_t assetFileOpen(assetfile_t *file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rewind the file to the initial position.
|
||||||
|
*
|
||||||
|
* @param file The asset file to rewind.
|
||||||
|
*/
|
||||||
|
errorret_t assetFileRewind(assetfile_t *file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read bytes from the asset file. Assumes the file has already been opened
|
||||||
|
* prior to trying to read anything.
|
||||||
|
*
|
||||||
|
* @param file File to read from.
|
||||||
|
* @param buffer Buffer to read the file data into., or NULL to skip bytes.
|
||||||
|
* @param size Size of the buffer to read into.
|
||||||
|
*/
|
||||||
|
errorret_t assetFileRead(
|
||||||
|
assetfile_t *file,
|
||||||
|
void *buffer,
|
||||||
|
const size_t bufferSize
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the asset file and releases any resources associated with it.
|
||||||
|
*
|
||||||
|
* @param file The asset file to close.
|
||||||
|
* @return An error code if the file could not be closed properly.
|
||||||
|
*/
|
||||||
|
errorret_t assetFileClose(assetfile_t *file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes the asset file structure, closing any open handles and zeroing
|
||||||
|
* out the structure.
|
||||||
|
*
|
||||||
|
* @param file The asset file to dispose.
|
||||||
|
* @return An error code if the file could not be disposed properly.
|
||||||
|
*/
|
||||||
|
errorret_t assetFileDispose(assetfile_t *file);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
assetfile_t *file;
|
||||||
|
uint8_t *readBuffer;
|
||||||
|
size_t readBufferSize;
|
||||||
|
uint8_t *outBuffer;
|
||||||
|
size_t outBufferSize;
|
||||||
|
|
||||||
|
// A
|
||||||
|
size_t bufferStart;
|
||||||
|
size_t bufferEnd;
|
||||||
|
bool_t eof;//?
|
||||||
|
|
||||||
|
// Updated each reach:
|
||||||
|
size_t lineLength;
|
||||||
|
} assetfilelinereader_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a line reader for the given asset file. The line reader will read
|
||||||
|
* lines from the file into the provided line buffer, using the provided buffer
|
||||||
|
* for reading chunks of the file.
|
||||||
|
*
|
||||||
|
* @param file The asset file to read from. Must already be opened.
|
||||||
|
* @param readBuffer Buffer to use for reading chunks of the file.
|
||||||
|
* @param readBufferSize Size of the read buffer.
|
||||||
|
* @param outBuffer Buffer to read lines into. Lines will be null-terminated.
|
||||||
|
* @param outBufferSize Size of the output buffer.
|
||||||
|
* @return An initialized line reader structure.
|
||||||
|
*/
|
||||||
|
void assetFileLineReaderInit(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
assetfile_t *file,
|
||||||
|
uint8_t *readBuffer,
|
||||||
|
const size_t readBufferSize,
|
||||||
|
uint8_t *outBuffer,
|
||||||
|
const size_t outBufferSize
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the next line from the asset file into the line buffer. The line
|
||||||
|
* buffer is null-terminated and does not include the newline character.
|
||||||
|
*
|
||||||
|
* @param reader The line reader to read from.
|
||||||
|
* @return An error code if a failure occurs, or errorOk() if a line was read
|
||||||
|
* successfully. If the end of the file is reached, errorEndOfFile() is
|
||||||
|
* returned.
|
||||||
|
*/
|
||||||
|
errorret_t assetFileLineReaderNext(assetfilelinereader_t *reader);
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "type/assettexture.h"
|
|
||||||
// #include "type/assetpalette.h"
|
|
||||||
#include "type/assettileset.h"
|
|
||||||
#include "type/assetlanguage.h"
|
|
||||||
#include "type/assetscript.h"
|
|
||||||
#include "type/assetmap.h"
|
|
||||||
#include "type/assetmapchunk.h"
|
|
||||||
#include <zip.h>
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
ASSET_TYPE_NULL,
|
|
||||||
|
|
||||||
ASSET_TYPE_TEXTURE,
|
|
||||||
ASSET_TYPE_TILESET,
|
|
||||||
ASSET_TYPE_LANGUAGE,
|
|
||||||
ASSET_TYPE_SCRIPT,
|
|
||||||
|
|
||||||
ASSET_TYPE_COUNT,
|
|
||||||
} assettype_t;
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
ASSET_LOAD_STRAT_ENTIRE,
|
|
||||||
ASSET_LOAD_STRAT_CUSTOM
|
|
||||||
} assetloadstrat_t;
|
|
||||||
|
|
||||||
typedef struct assetentire_s {
|
|
||||||
void *data;
|
|
||||||
void *output;
|
|
||||||
} assetentire_t;
|
|
||||||
|
|
||||||
typedef struct assetcustom_s {
|
|
||||||
zip_file_t *zipFile;
|
|
||||||
void *output;
|
|
||||||
} assetcustom_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
const char_t *extension;
|
|
||||||
const size_t dataSize;
|
|
||||||
const assetloadstrat_t loadStrategy;
|
|
||||||
union {
|
|
||||||
errorret_t (*entire)(assetentire_t entire);
|
|
||||||
errorret_t (*custom)(assetcustom_t custom);
|
|
||||||
};
|
|
||||||
} assettypedef_t;
|
|
||||||
|
|
||||||
static const assettypedef_t ASSET_TYPE_DEFINITIONS[ASSET_TYPE_COUNT] = {
|
|
||||||
[ASSET_TYPE_NULL] = {
|
|
||||||
0
|
|
||||||
},
|
|
||||||
|
|
||||||
[ASSET_TYPE_TEXTURE] = {
|
|
||||||
.extension = "DTX",
|
|
||||||
.loadStrategy = ASSET_LOAD_STRAT_ENTIRE,
|
|
||||||
.dataSize = sizeof(assettexture_t),
|
|
||||||
.entire = assetTextureLoad
|
|
||||||
},
|
|
||||||
|
|
||||||
[ASSET_TYPE_TILESET] = {
|
|
||||||
.extension = "DTF",
|
|
||||||
.loadStrategy = ASSET_LOAD_STRAT_ENTIRE,
|
|
||||||
.dataSize = sizeof(assettileset_t),
|
|
||||||
.entire = assetTilesetLoad
|
|
||||||
},
|
|
||||||
|
|
||||||
[ASSET_TYPE_LANGUAGE] = {
|
|
||||||
.extension = "DLF",
|
|
||||||
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
|
||||||
.custom = assetLanguageHandler
|
|
||||||
},
|
|
||||||
|
|
||||||
[ASSET_TYPE_SCRIPT] = {
|
|
||||||
.extension = "lua",
|
|
||||||
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
|
||||||
.custom = assetScriptHandler
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
|
||||||
|
# Subdirs
|
||||||
|
add_subdirectory(display)
|
||||||
|
add_subdirectory(locale)
|
||||||
|
add_subdirectory(script)
|
||||||
|
add_subdirectory(json)
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
assetmeshloader.c
|
||||||
|
assettextureloader.c
|
||||||
|
assettilesetloader.c
|
||||||
|
)
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asset/loader/display/assetmeshloader.h"
|
||||||
|
#include "asset/asset.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "util/endian.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
|
errorret_t assetMeshLoader(assetfile_t *file) {
|
||||||
|
assertNotNull(file, "Asset file cannot be null");
|
||||||
|
|
||||||
|
assetmeshoutput_t *output = (assetmeshoutput_t *)file->output;
|
||||||
|
assertNotNull(output, "Output cannot be null");
|
||||||
|
assertNotNull(output->outMesh, "Output mesh cannot be null");
|
||||||
|
assertNotNull(output->outVertices, "Output vertices cannot be null");
|
||||||
|
|
||||||
|
// STL file loading
|
||||||
|
errorChain(assetFileOpen(file));
|
||||||
|
|
||||||
|
// Skip the 80 byte header
|
||||||
|
errorChain(assetFileRead(file, NULL, 80));
|
||||||
|
if(file->lastRead != 80) errorThrow("Failed to skip STL header");
|
||||||
|
|
||||||
|
uint32_t triangleCount;
|
||||||
|
errorChain(assetFileRead(file, &triangleCount, sizeof(uint32_t)));
|
||||||
|
if(file->lastRead != sizeof(uint32_t)) errorThrow("Failed read tri count");
|
||||||
|
// normalize
|
||||||
|
triangleCount = endianLittleToHost32(triangleCount);
|
||||||
|
|
||||||
|
// Allocate mesh and vertex data
|
||||||
|
errorret_t ret;
|
||||||
|
meshvertex_t *verts = memoryAllocate(
|
||||||
|
sizeof(meshvertex_t) * triangleCount * 3
|
||||||
|
);
|
||||||
|
*output->outVertices = verts;
|
||||||
|
|
||||||
|
// Read triangle data
|
||||||
|
for(uint32_t i = 0; i < triangleCount; i++) {
|
||||||
|
assetmeshstltriangle_t triData;
|
||||||
|
ret = assetFileRead(file, &triData, sizeof(triData));
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
memoryFree(verts);
|
||||||
|
errorChain(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(file->lastRead != sizeof(triData)) {
|
||||||
|
memoryFree(verts);
|
||||||
|
errorThrow("Failed to read triangle data");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip normals, we don't use them
|
||||||
|
|
||||||
|
// Fix endianess of of data
|
||||||
|
for(uint8_t j = 0; j < 3; j++) {
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
verts[i * 3 + j].color.r = (uint8_t)(endianLittleToHostFloat(
|
||||||
|
triData.normal[0]
|
||||||
|
) * 255.0f);
|
||||||
|
verts[i * 3 + j].color.g = (uint8_t)(endianLittleToHostFloat(
|
||||||
|
triData.normal[1]
|
||||||
|
) * 255.0f);
|
||||||
|
verts[i * 3 + j].color.b = (uint8_t)(endianLittleToHostFloat(
|
||||||
|
triData.normal[2]
|
||||||
|
) * 255.0f);
|
||||||
|
verts[i * 3 + j].color.a = 0xFF;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
verts[i * 3 + j].uv[0] = 0.0f; // No UV data in STL, just set to 0
|
||||||
|
verts[i * 3 + j].uv[1] = 0.0f;
|
||||||
|
|
||||||
|
for(uint8_t k = 0; k < 3; k++) {
|
||||||
|
verts[i * 3 + j].pos[k] = endianLittleToHostFloat(
|
||||||
|
triData.positions[j][k]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(output->inputAxis) {
|
||||||
|
case MESH_INPUT_AXIS_Z_UP:
|
||||||
|
// Convert Z-Up to Y-Up
|
||||||
|
{
|
||||||
|
float_t temp = verts[i * 3 + j].pos[1];
|
||||||
|
verts[i * 3 + j].pos[1] = verts[i * 3 + j].pos[2];
|
||||||
|
verts[i * 3 + j].pos[2] = temp;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MESH_INPUT_AXIS_X_UP:
|
||||||
|
// Convert X-Up to Y-Up
|
||||||
|
{
|
||||||
|
float_t temp = verts[i * 3 + j].pos[0];
|
||||||
|
verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1];
|
||||||
|
verts[i * 3 + j].pos[1] = temp;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MESH_INPUT_AXIS_Y_DOWN:
|
||||||
|
// Invert Y axis
|
||||||
|
verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[1];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MESH_INPUT_AXIS_Z_DOWN:
|
||||||
|
// Convert Z-Up to Y-Up and invert Y axis
|
||||||
|
{
|
||||||
|
float_t temp = verts[i * 3 + j].pos[1];
|
||||||
|
verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[2];
|
||||||
|
verts[i * 3 + j].pos[2] = temp;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MESH_INPUT_AXIS_X_DOWN:
|
||||||
|
// Convert X-Up to Y-Up and invert Y axis
|
||||||
|
{
|
||||||
|
float_t temp = verts[i * 3 + j].pos[0];
|
||||||
|
verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1];
|
||||||
|
verts[i * 3 + j].pos[1] = -temp;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MESH_INPUT_AXIS_Y_UP:
|
||||||
|
default:
|
||||||
|
// No covnersion possible / Needed
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, init mesh
|
||||||
|
ret = meshInit(
|
||||||
|
output->outMesh,
|
||||||
|
MESH_PRIMITIVE_TYPE_TRIANGLES,
|
||||||
|
triangleCount * 3,
|
||||||
|
verts
|
||||||
|
);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
memoryFree(verts);
|
||||||
|
errorChain(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = assetFileClose(file);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
errorCatch(errorPrint(meshDispose(output->outMesh)));
|
||||||
|
memoryFree(verts);
|
||||||
|
errorChain(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetMeshLoadToOutput(
|
||||||
|
const char_t *path,
|
||||||
|
assetmeshoutput_t *output
|
||||||
|
) {
|
||||||
|
assertNotNull(path, "Path cannot be null");
|
||||||
|
assertNotNull(output, "Output cannot be null");
|
||||||
|
assertNotNull(output->outMesh, "Output mesh cannot be null");
|
||||||
|
assertNotNull(output->outVertices, "Output vertices cannot be null");
|
||||||
|
|
||||||
|
return assetLoad(path, &assetMeshLoader, NULL, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetMeshLoad(
|
||||||
|
const char_t *path,
|
||||||
|
mesh_t *outMesh,
|
||||||
|
meshvertex_t **outVertices,
|
||||||
|
const assetmeshinputaxis_t inputAxis
|
||||||
|
) {
|
||||||
|
assertNotNull(path, "Path cannot be null");
|
||||||
|
assertNotNull(outMesh, "Output mesh cannot be null");
|
||||||
|
assertNotNull(outVertices, "Output vertices cannot be null");
|
||||||
|
|
||||||
|
assetmeshoutput_t output = {
|
||||||
|
outMesh,
|
||||||
|
outVertices,
|
||||||
|
inputAxis
|
||||||
|
};
|
||||||
|
return assetMeshLoadToOutput(path, &output);
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "asset/asset.h"
|
||||||
|
#include "display/mesh/mesh.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
MESH_INPUT_AXIS_Y_UP,// Default
|
||||||
|
MESH_INPUT_AXIS_Z_UP,
|
||||||
|
MESH_INPUT_AXIS_X_UP,
|
||||||
|
|
||||||
|
MESH_INPUT_AXIS_Y_DOWN,
|
||||||
|
MESH_INPUT_AXIS_Z_DOWN,
|
||||||
|
MESH_INPUT_AXIS_X_DOWN,
|
||||||
|
} assetmeshinputaxis_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
mesh_t *outMesh;
|
||||||
|
meshvertex_t **outVertices;
|
||||||
|
assetmeshinputaxis_t inputAxis;
|
||||||
|
} assetmeshoutput_t;
|
||||||
|
|
||||||
|
#pragma pack(push, 1)
|
||||||
|
typedef struct {
|
||||||
|
vec3 normal;
|
||||||
|
float_t positions[3][3];
|
||||||
|
uint16_t attributeByteCount;
|
||||||
|
} assetmeshstltriangle_t;
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
assertStructSize(assetmeshstltriangle_t, 50);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loader for mesh assets.
|
||||||
|
*
|
||||||
|
* @param file Asset file to load the mesh from.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetMeshLoader(assetfile_t *file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for mesh assets.
|
||||||
|
*
|
||||||
|
* @param file Asset file to load the mesh from.
|
||||||
|
* @param outMesh Output mesh to load the data into.
|
||||||
|
* @param outVertices Output pointer to the vertex data, used for cleanup.
|
||||||
|
* @param inputAxis The axis orientation of the input mesh data.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetMeshLoad(
|
||||||
|
const char_t *path,
|
||||||
|
mesh_t *outMesh,
|
||||||
|
meshvertex_t **outVertices,
|
||||||
|
const assetmeshinputaxis_t inputAxis
|
||||||
|
);
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "assettextureloader.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include "stb_image.h"
|
||||||
|
#include "log/log.h"
|
||||||
|
#include "util/endian.h"
|
||||||
|
|
||||||
|
stbi_io_callbacks ASSET_TEXTURE_STB_CALLBACKS = {
|
||||||
|
.read = assetTextureReader,
|
||||||
|
.skip = assetTextureSkipper,
|
||||||
|
.eof = assetTextureEOF
|
||||||
|
};
|
||||||
|
|
||||||
|
int assetTextureReader(void *user, char *data, int size) {
|
||||||
|
assertNotNull(data, "Data buffer for stb_image callbacks cannot be NULL.");
|
||||||
|
|
||||||
|
assetfile_t *file = (assetfile_t*)user;
|
||||||
|
assertNotNull(file, "Asset file in stb_image callbacks cannot be NULL.");
|
||||||
|
|
||||||
|
errorret_t ret = assetFileRead(file, data, (size_t)size);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
errorCatch(errorPrint(ret));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return file->lastRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
void assetTextureSkipper(void *user, int n) {
|
||||||
|
assetfile_t *file = (assetfile_t*)user;
|
||||||
|
assertNotNull(file, "Asset file in stb_image callbacks cannot be NULL.");
|
||||||
|
|
||||||
|
errorret_t ret = assetFileRead(file, NULL, (size_t)n);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
errorCatch(errorPrint(ret));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int assetTextureEOF(void *user) {
|
||||||
|
assetfile_t *file = (assetfile_t*)user;
|
||||||
|
assertNotNull(file, "Asset file in stb_image callbacks cannot be NULL.");
|
||||||
|
|
||||||
|
return file->position >= file->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetTextureLoader(assetfile_t *file) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(file->params, "Asset file parameters cannot be NULL.");
|
||||||
|
assertNotNull(file->output, "Asset file output cannot be NULL.");
|
||||||
|
|
||||||
|
assettextureloaderparams_t *p = (assettextureloaderparams_t*)file->params;
|
||||||
|
assertNotNull(p, "Asset texture loader parameters cannot be NULL.");
|
||||||
|
|
||||||
|
int channelsDesired;
|
||||||
|
switch(p->format) {
|
||||||
|
case TEXTURE_FORMAT_RGBA:
|
||||||
|
channelsDesired = 4;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
errorThrow("Bad texture format.");
|
||||||
|
}
|
||||||
|
|
||||||
|
int width, height, channels;
|
||||||
|
errorChain(assetFileOpen(file));
|
||||||
|
uint8_t *data = stbi_load_from_callbacks(
|
||||||
|
&ASSET_TEXTURE_STB_CALLBACKS,
|
||||||
|
file,
|
||||||
|
&width,
|
||||||
|
&height,
|
||||||
|
&channels,
|
||||||
|
channelsDesired
|
||||||
|
);
|
||||||
|
errorChain(assetFileClose(file));
|
||||||
|
|
||||||
|
if(data == NULL) {
|
||||||
|
const char_t *errorStr = stbi_failure_reason();
|
||||||
|
errorThrow("Failed to load texture from file %s.", errorStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fixes a specific bug probably with Dolphin but for now just assuming endian
|
||||||
|
if(!isHostLittleEndian()) {
|
||||||
|
stbi__vertical_flip(data, width, height, channelsDesired);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorChain(textureInit(
|
||||||
|
(texture_t*)file->output,
|
||||||
|
(int32_t)width, (int32_t)height,
|
||||||
|
p->format,
|
||||||
|
(texturedata_t){
|
||||||
|
.rgbaColors = (color_t*)data
|
||||||
|
}
|
||||||
|
));
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetTextureLoad(
|
||||||
|
const char_t *path,
|
||||||
|
texture_t *out,
|
||||||
|
const textureformat_t format
|
||||||
|
) {
|
||||||
|
assettextureloaderparams_t params = {
|
||||||
|
.format = format
|
||||||
|
};
|
||||||
|
return assetLoad(path, assetTextureLoader, ¶ms, out);
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "asset/asset.h"
|
||||||
|
#include "display/texture/texture.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
textureformat_t format;
|
||||||
|
} assettextureloaderparams_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* STB image read callback for asset files.
|
||||||
|
*
|
||||||
|
* @param user User data passed to the callback, should be an assetfile_t*.
|
||||||
|
* @param data Buffer to read the file data into.
|
||||||
|
* @param size Size of the buffer to read into.
|
||||||
|
* @return Number of bytes read, or -1 on error.
|
||||||
|
*/
|
||||||
|
int assetTextureReader(void *user, char *data, int size);
|
||||||
|
|
||||||
|
void assetTextureSkipper(void *user, int n);
|
||||||
|
|
||||||
|
int assetTextureEOF(void *user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for texture assets.
|
||||||
|
*
|
||||||
|
* @param file Asset file to load the texture from.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetTextureLoader(assetfile_t *file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a texture from the specified path.
|
||||||
|
*
|
||||||
|
* @param path Path to the texture asset.
|
||||||
|
* @param out Output texture to load into.
|
||||||
|
* @param format Format of the texture to load.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetTextureLoad(
|
||||||
|
const char_t *path,
|
||||||
|
texture_t *out,
|
||||||
|
const textureformat_t format
|
||||||
|
);
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "assettilesetloader.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
#include "util/endian.h"
|
||||||
|
|
||||||
|
errorret_t assetTilesetLoader(assetfile_t *file) {
|
||||||
|
assertNotNull(file, "Asset file pointer for tileset loader is null.");
|
||||||
|
|
||||||
|
tileset_t *out = (tileset_t *)file->output;
|
||||||
|
assertNotNull(out, "Output pointer for tileset loader is null.");
|
||||||
|
|
||||||
|
uint8_t *entire = memoryAllocate(file->size);
|
||||||
|
errorChain(assetFileOpen(file));
|
||||||
|
errorChain(assetFileRead(file, entire, file->size));
|
||||||
|
errorChain(assetFileClose(file));
|
||||||
|
assertTrue(file->lastRead == file->size, "Failed to read entire file.");
|
||||||
|
|
||||||
|
|
||||||
|
if(
|
||||||
|
entire[0] != 'D' ||
|
||||||
|
entire[1] != 'T' ||
|
||||||
|
entire[2] != 'F'
|
||||||
|
) {
|
||||||
|
errorThrow("Invalid tileset header");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(entire[3] != 0x00) {
|
||||||
|
errorThrow("Unsupported tileset version");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fix endianness
|
||||||
|
|
||||||
|
out->tileWidth = endianLittleToHost16(*(uint16_t *)(entire + 4));
|
||||||
|
out->tileHeight = endianLittleToHost16(*(uint16_t *)(entire + 6));
|
||||||
|
out->columns = endianLittleToHost16(*(uint16_t *)(entire + 8));
|
||||||
|
out->rows = endianLittleToHost16(*(uint16_t *)(entire + 10));
|
||||||
|
// out->right = endianLittleToHost16(*(uint16_t *)(entire + 12));
|
||||||
|
// out->bottom = endianLittleToHost16(*(uint16_t *)(entire + 14));
|
||||||
|
|
||||||
|
if(out->tileWidth == 0) {
|
||||||
|
errorThrow("Tile width cannot be 0");
|
||||||
|
}
|
||||||
|
if(out->tileHeight == 0) {
|
||||||
|
errorThrow("Tile height cannot be 0");
|
||||||
|
}
|
||||||
|
if(out->columns == 0) {
|
||||||
|
errorThrow("Column count cannot be 0");
|
||||||
|
}
|
||||||
|
if(out->rows == 0) {
|
||||||
|
errorThrow("Row count cannot be 0");
|
||||||
|
}
|
||||||
|
|
||||||
|
out->uv[0] = endianLittleToHostFloat(*(float *)(entire + 16));
|
||||||
|
out->uv[1] = endianLittleToHostFloat(*(float *)(entire + 20));
|
||||||
|
|
||||||
|
if(out->uv[1] < 0.0f || out->uv[1] > 1.0f) {
|
||||||
|
errorThrow("Invalid v0 value in tileset");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup tileset
|
||||||
|
out->tileCount = out->columns * out->rows;
|
||||||
|
|
||||||
|
memoryFree(entire);
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetTilesetLoad(
|
||||||
|
const char_t *path,
|
||||||
|
tileset_t *out
|
||||||
|
) {
|
||||||
|
return assetLoad(path, assetTilesetLoader, NULL, out);
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "asset/asset.h"
|
||||||
|
#include "display/texture/tileset.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for tileset assets.
|
||||||
|
*
|
||||||
|
* @param file Asset file to load the tileset from.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetTilesetLoader(assetfile_t *file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a tileset from the specified path.
|
||||||
|
*
|
||||||
|
* @param path Path to the tileset asset.
|
||||||
|
* @param out Output tileset to load into.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetTilesetLoad(
|
||||||
|
const char_t *path,
|
||||||
|
tileset_t *out
|
||||||
|
);
|
||||||
+1
-2
@@ -4,8 +4,7 @@
|
|||||||
# https://opensource.org/licenses/MIT
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
# Sources
|
# Sources
|
||||||
|
|
||||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
PUBLIC
|
PUBLIC
|
||||||
cameradolphin.c
|
assetjsonloader.c
|
||||||
)
|
)
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "assetjsonloader.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
errorret_t assetJsonLoadFileToDoc(assetfile_t *file, yyjson_doc **outDoc) {
|
||||||
|
assertNotNull(file, "Asset file pointer for JSON loader is null.");
|
||||||
|
assertNotNull(outDoc, "Output pointer for JSON loader is null.");
|
||||||
|
|
||||||
|
if(file->size > ASSET_JSON_FILE_SIZE_MAX) {
|
||||||
|
errorThrow("JSON exceeds maximum allowed size");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create buffer
|
||||||
|
uint8_t *buffer = memoryAllocate(file->size);
|
||||||
|
|
||||||
|
errorChain(assetFileOpen(file));
|
||||||
|
|
||||||
|
// Read entire file
|
||||||
|
errorChain(assetFileRead(file, buffer, file->size));
|
||||||
|
assertTrue(file->lastRead == file->size, "Failed to read entire JSON file.");
|
||||||
|
|
||||||
|
errorChain(assetFileClose(file));
|
||||||
|
|
||||||
|
*outDoc = yyjson_read(
|
||||||
|
buffer,
|
||||||
|
file->size,
|
||||||
|
YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS
|
||||||
|
);
|
||||||
|
memoryFree(buffer);
|
||||||
|
|
||||||
|
if(!*outDoc) errorThrow("Failed to parse JSON");
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetJsonLoader(assetfile_t *file) {
|
||||||
|
yyjson_doc **outDoc = (yyjson_doc **)file->output;
|
||||||
|
assertNotNull(outDoc, "Output pointer for JSON loader is null.");
|
||||||
|
return assetJsonLoadFileToDoc(file, outDoc);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetJsonLoad(
|
||||||
|
const char_t *path,
|
||||||
|
yyjson_doc **outDoc
|
||||||
|
) {
|
||||||
|
return assetLoad(path, assetJsonLoader, NULL, outDoc);
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "asset/asset.h"
|
||||||
|
#include "yyjson.h"
|
||||||
|
|
||||||
|
#define ASSET_JSON_FILE_SIZE_MAX 1024*256
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *nothing;
|
||||||
|
} assetjsonloaderparams_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a JSON document from the specified asset file.
|
||||||
|
*
|
||||||
|
* @param file Asset file to load the JSON document from.
|
||||||
|
* @param outDoc Pointer to store the loaded JSON document.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetJsonLoadFileToDoc(assetfile_t *file, yyjson_doc **outDoc);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for locale assets.
|
||||||
|
*
|
||||||
|
* @param file Asset file to load the locale from.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetJsonLoader(assetfile_t *file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a locale from the specified path.
|
||||||
|
*
|
||||||
|
* @param path Path to the locale asset.
|
||||||
|
* @param outDoc Pointer to store the loaded JSON document.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetJsonLoad(
|
||||||
|
const char_t *path,
|
||||||
|
yyjson_doc **outDoc
|
||||||
|
);
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
assetlocaleloader.c
|
||||||
|
)
|
||||||
@@ -0,0 +1,807 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "assetlocaleloader.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
#include "util/math.h"
|
||||||
|
#include "util/string.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
errorret_t assetLocaleFileInit(
|
||||||
|
assetlocalefile_t *localeFile,
|
||||||
|
const char_t *path
|
||||||
|
) {
|
||||||
|
assertNotNull(localeFile, "Locale file cannot be NULL.");
|
||||||
|
assertNotNull(path, "Locale file path cannot be NULL.");
|
||||||
|
|
||||||
|
memoryZero(localeFile, sizeof(assetlocalefile_t));
|
||||||
|
|
||||||
|
// Init the asset file.
|
||||||
|
errorChain(assetFileInit(&localeFile->file, path, NULL, NULL));
|
||||||
|
|
||||||
|
// Open the file handle
|
||||||
|
errorChain(assetFileOpen(&localeFile->file));
|
||||||
|
|
||||||
|
// Get the blank key, this is basically the header info for po files
|
||||||
|
char_t buffer[1024];
|
||||||
|
errorChain(assetLocaleGetString(localeFile, "", 0, buffer, sizeof(buffer)));
|
||||||
|
errorChain(assetLocaleParseHeader(localeFile, buffer, sizeof(buffer)));
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleFileDispose(assetlocalefile_t *localeFile) {
|
||||||
|
assertNotNull(localeFile, "Locale file cannot be NULL.");
|
||||||
|
|
||||||
|
errorChain(assetFileClose(&localeFile->file));
|
||||||
|
errorChain(assetFileDispose(&localeFile->file));
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleParseHeader(
|
||||||
|
assetlocalefile_t *localeFile,
|
||||||
|
char_t *headerBuffer,
|
||||||
|
const size_t headerBufferSize
|
||||||
|
) {
|
||||||
|
assertNotNull(localeFile, "Locale file cannot be NULL.");
|
||||||
|
assertNotNull(headerBuffer, "Header buffer cannot be NULL.");
|
||||||
|
assertTrue(headerBufferSize > 0, "Header buffer size must be > 0.");
|
||||||
|
|
||||||
|
// Find "Plural-Forms: " line and parse out plural form info
|
||||||
|
char_t *pluralFormsLine = strstr(headerBuffer, "Plural-Forms:");
|
||||||
|
if(!pluralFormsLine) {
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
pluralFormsLine += strlen("Plural-Forms:");
|
||||||
|
|
||||||
|
// Expect nplurals
|
||||||
|
char_t *npluralsStr = strstr(pluralFormsLine, "nplurals=");
|
||||||
|
if(!npluralsStr) {
|
||||||
|
errorThrow("Failed to find nplurals in Plural-Forms header.");
|
||||||
|
}
|
||||||
|
npluralsStr += strlen("nplurals=");
|
||||||
|
localeFile->pluralStateCount = (uint8_t)atoi(npluralsStr);
|
||||||
|
|
||||||
|
if(localeFile->pluralStateCount == 0) {
|
||||||
|
errorThrow("nplurals must be greater than 0.");
|
||||||
|
}
|
||||||
|
if(localeFile->pluralStateCount > ASSET_LOCALE_FILE_PLURAL_FORM_COUNT) {
|
||||||
|
errorThrow(
|
||||||
|
"nplurals exceeds maximum supported plural forms: %d > %d",
|
||||||
|
localeFile->pluralStateCount,
|
||||||
|
ASSET_LOCALE_FILE_PLURAL_FORM_COUNT
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expect plural=
|
||||||
|
char_t *pluralStr = strstr(pluralFormsLine, "plural=");
|
||||||
|
if(!pluralStr) {
|
||||||
|
errorThrow("Failed to find plural in Plural-Forms header.");
|
||||||
|
}
|
||||||
|
pluralStr += strlen("plural=");
|
||||||
|
|
||||||
|
// Expect ( [expressions] )
|
||||||
|
char_t *openParen = strchr(pluralStr, '(');
|
||||||
|
char_t *closeParen = strrchr(pluralStr, ')');
|
||||||
|
if(!openParen || !closeParen || closeParen < openParen) {
|
||||||
|
errorThrow("Failed to find plural expression in Plural-Forms header.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse:
|
||||||
|
// n [op] value ? index : n [op] value ? index : ... : final_index
|
||||||
|
char_t *ptr = openParen + 1;
|
||||||
|
uint8_t pluralIndex = 0;
|
||||||
|
uint8_t definedCount = 0;
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Allow grouped subexpressions like:
|
||||||
|
// (n<7 ? 2 : 3)
|
||||||
|
// or
|
||||||
|
// (((3)))
|
||||||
|
uint8_t parenDepth = 0;
|
||||||
|
while(*ptr == '(') {
|
||||||
|
parenDepth++;
|
||||||
|
ptr++;
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final fallback: just an integer
|
||||||
|
if(*ptr != 'n') {
|
||||||
|
char_t *endPtr = NULL;
|
||||||
|
int32_t finalIndex = (int32_t)strtol(ptr, &endPtr, 10);
|
||||||
|
if(endPtr == ptr) {
|
||||||
|
errorThrow("Expected final plural index.");
|
||||||
|
}
|
||||||
|
ptr = endPtr;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
while(parenDepth > 0) {
|
||||||
|
if(*ptr != ')') {
|
||||||
|
errorThrow("Expected ')' after final plural index.");
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
parenDepth--;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*ptr != ')') {
|
||||||
|
errorThrow("Expected ')' at end of plural expression.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(finalIndex < 0 || finalIndex >= localeFile->pluralStateCount) {
|
||||||
|
errorThrow(
|
||||||
|
"Final plural expression index out of bounds: %d (nplurals: %d)",
|
||||||
|
finalIndex,
|
||||||
|
localeFile->pluralStateCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
localeFile->pluralDefaultIndex = (uint8_t)finalIndex;
|
||||||
|
definedCount++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pluralIndex >= localeFile->pluralStateCount - 1) {
|
||||||
|
errorThrow(
|
||||||
|
"Too many plural conditions. Expected %d conditional clauses for nplurals=%d.",
|
||||||
|
localeFile->pluralStateCount - 1,
|
||||||
|
localeFile->pluralStateCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr++; // skip 'n'
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Determine operator
|
||||||
|
assetlocalepluraloperation_t op;
|
||||||
|
if(strncmp(ptr, "==", 2) == 0) {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_EQUAL;
|
||||||
|
ptr += 2;
|
||||||
|
} else if(strncmp(ptr, "!=", 2) == 0) {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_NOT_EQUAL;
|
||||||
|
ptr += 2;
|
||||||
|
} else if(strncmp(ptr, "<=", 2) == 0) {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_LESS_EQUAL;
|
||||||
|
ptr += 2;
|
||||||
|
} else if(strncmp(ptr, ">=", 2) == 0) {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_GREATER_EQUAL;
|
||||||
|
ptr += 2;
|
||||||
|
} else if(*ptr == '<') {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_LESS;
|
||||||
|
ptr++;
|
||||||
|
} else if(*ptr == '>') {
|
||||||
|
op = ASSET_LOCALE_PLURAL_OP_GREATER;
|
||||||
|
ptr++;
|
||||||
|
} else {
|
||||||
|
errorThrow("Unsupported plural operator.");
|
||||||
|
}
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Parse the comparitor value
|
||||||
|
char_t *endPtr = NULL;
|
||||||
|
int32_t value = (int32_t)strtol(ptr, &endPtr, 10);
|
||||||
|
if(endPtr == ptr) {
|
||||||
|
errorThrow("Expected value for plural expression.");
|
||||||
|
}
|
||||||
|
ptr = endPtr;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Parse ternary operator
|
||||||
|
if(*ptr != '?') {
|
||||||
|
errorThrow("Expected '?' after plural expression.");
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Parse the indice
|
||||||
|
endPtr = NULL;
|
||||||
|
int32_t index = (int32_t)strtol(ptr, &endPtr, 10);
|
||||||
|
if(endPtr == ptr) {
|
||||||
|
errorThrow("Expected index for plural expression.");
|
||||||
|
}
|
||||||
|
ptr = endPtr;
|
||||||
|
|
||||||
|
if(index < 0 || index >= localeFile->pluralStateCount) {
|
||||||
|
errorThrow(
|
||||||
|
"Plural expression index out of bounds: %d (nplurals: %d)",
|
||||||
|
index,
|
||||||
|
localeFile->pluralStateCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store plural expression.
|
||||||
|
localeFile->pluralIndices[pluralIndex] = (uint8_t)index;
|
||||||
|
localeFile->pluralOps[pluralIndex] = op;
|
||||||
|
localeFile->pluralValues[pluralIndex] = value;
|
||||||
|
pluralIndex++;
|
||||||
|
definedCount++;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
|
||||||
|
// Close any grouping parens that wrapped this conditional branch
|
||||||
|
while(parenDepth > 0) {
|
||||||
|
if(*ptr != ')') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
parenDepth--;
|
||||||
|
|
||||||
|
while(*ptr == ' ') ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*ptr != ':') {
|
||||||
|
errorThrow("Expected ':' after plural expression.");
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Must define exactly nplurals outcomes:
|
||||||
|
// (nplurals - 1) conditional results + 1 final fallback result
|
||||||
|
if(
|
||||||
|
pluralIndex != localeFile->pluralStateCount - 1 ||
|
||||||
|
definedCount != localeFile->pluralStateCount
|
||||||
|
) {
|
||||||
|
errorThrow("Plural expression count does not match nplurals.");
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t assetLocaleEvaluatePlural(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const int32_t pluralCount
|
||||||
|
) {
|
||||||
|
assertNotNull(file, "Locale file cannot be NULL.");
|
||||||
|
assertTrue(pluralCount >= 0, "Plural count cannot be negative.");
|
||||||
|
|
||||||
|
for(uint8_t i = 0; i < file->pluralStateCount - 1; i++) {
|
||||||
|
int32_t value = file->pluralValues[i];
|
||||||
|
switch(file->pluralOps[i]) {
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_EQUAL:
|
||||||
|
if(pluralCount == value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_NOT_EQUAL:
|
||||||
|
if(pluralCount != value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_LESS:
|
||||||
|
if(pluralCount < value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_LESS_EQUAL:
|
||||||
|
if(pluralCount <= value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_GREATER:
|
||||||
|
if(pluralCount > value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ASSET_LOCALE_PLURAL_OP_GREATER_EQUAL:
|
||||||
|
if(pluralCount >= value) return file->pluralIndices[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return file->pluralDefaultIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleLineSkipBlanks(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
uint8_t *lineBuffer
|
||||||
|
) {
|
||||||
|
while(!reader->eof) {
|
||||||
|
// Skip blank lines
|
||||||
|
if(lineBuffer[0] == '\0') {
|
||||||
|
errorChain(assetFileLineReaderNext(reader));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip comment lines
|
||||||
|
if(lineBuffer[0] == '#') {
|
||||||
|
errorChain(assetFileLineReaderNext(reader));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is line only spaces?
|
||||||
|
size_t lineLength = strlen((char_t *)lineBuffer);
|
||||||
|
size_t i;
|
||||||
|
bool_t onlySpaces = true;
|
||||||
|
for(i = 0; i < lineLength; i++) {
|
||||||
|
if(lineBuffer[i] != ' ') {
|
||||||
|
onlySpaces = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(onlySpaces) {
|
||||||
|
errorChain(assetFileLineReaderNext(reader));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleLineUnbuffer(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
uint8_t *lineBuffer,
|
||||||
|
uint8_t *stringBuffer,
|
||||||
|
const size_t stringBufferSize
|
||||||
|
) {
|
||||||
|
stringBuffer[0] = '\0';
|
||||||
|
|
||||||
|
// At the point this funciton is called, we are looking for an opening quote.
|
||||||
|
char_t *start = strchr((char_t *)lineBuffer, '"');
|
||||||
|
if(!start) {
|
||||||
|
errorThrow("Expected open (0) \"");
|
||||||
|
}
|
||||||
|
|
||||||
|
char *end = strchr(start + 1, '"');
|
||||||
|
if(!end) {
|
||||||
|
errorThrow("Expected close (0) \"");
|
||||||
|
}
|
||||||
|
*end = '\0';
|
||||||
|
|
||||||
|
if(strlen(start) >= stringBufferSize) {
|
||||||
|
errorThrow("String buffer overflow");
|
||||||
|
}
|
||||||
|
memoryCopy(stringBuffer, start + 1, strlen(start));
|
||||||
|
|
||||||
|
// Now start buffering lines out
|
||||||
|
while(!reader->eof) {
|
||||||
|
errorChain(assetFileLineReaderNext(reader));
|
||||||
|
|
||||||
|
// Skip blank lines
|
||||||
|
errorChain(assetLocaleLineSkipBlanks(reader, lineBuffer));
|
||||||
|
|
||||||
|
// Skip starting spaces
|
||||||
|
char_t *ptr = (char_t *)lineBuffer;
|
||||||
|
while(*ptr == ' ') {
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only consider lines starting with quote
|
||||||
|
if(*ptr != '"') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr++; // move past first quote
|
||||||
|
|
||||||
|
bool_t escaping = false;
|
||||||
|
char_t *dest = (char_t *)stringBuffer + strlen((char_t *)stringBuffer);
|
||||||
|
while(*ptr) {
|
||||||
|
if(escaping) {
|
||||||
|
// Handle escape sequences
|
||||||
|
switch(*ptr) {
|
||||||
|
case 'n': *dest++ = '\n'; break;
|
||||||
|
case 't': *dest++ = '\t'; break;
|
||||||
|
case '\\': *dest++ = '\\'; break;
|
||||||
|
case '"': *dest++ = '"'; break;
|
||||||
|
default:
|
||||||
|
errorThrow("Unknown escape sequence: \\%c", *ptr);
|
||||||
|
}
|
||||||
|
escaping = false;
|
||||||
|
} else if(*ptr == '\\') {
|
||||||
|
escaping = true;
|
||||||
|
} else if(*ptr == '"') {
|
||||||
|
// End of string
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
// Regular character
|
||||||
|
*dest++ = *ptr;
|
||||||
|
}
|
||||||
|
if((size_t)(dest - (char_t *)stringBuffer) >= stringBufferSize) {
|
||||||
|
errorThrow("String buffer overflow");
|
||||||
|
}
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
*dest = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleGetString(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *messageId,
|
||||||
|
const int32_t pluralCount,
|
||||||
|
char_t *stringBuffer,
|
||||||
|
const size_t stringBufferSize
|
||||||
|
) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(messageId, "Message ID cannot be NULL.");
|
||||||
|
assertTrue(pluralCount >= 0, "Plural index cannot be negative.");
|
||||||
|
assertNotNull(stringBuffer, "String buffer cannot be NULL.");
|
||||||
|
assertTrue(stringBufferSize > 0, "String buffer size must be > 0");
|
||||||
|
assetfilelinereader_t reader;
|
||||||
|
|
||||||
|
bool_t msgidFound = false, msgidPluralFound = false, msgstrFound = false;
|
||||||
|
uint8_t msgidBuffer[256];
|
||||||
|
uint8_t msgidPluralBuffer[256];
|
||||||
|
uint8_t readBuffer[1024];
|
||||||
|
uint8_t lineBuffer[1024];
|
||||||
|
uint8_t pluralIndex = 0xFF;
|
||||||
|
|
||||||
|
msgidBuffer[0] = '\0';
|
||||||
|
msgidPluralBuffer[0] = '\0';
|
||||||
|
stringBuffer[0] = '\0';
|
||||||
|
|
||||||
|
// Rewind and start reading lines.
|
||||||
|
errorChain(assetFileRewind(&file->file));
|
||||||
|
assetFileLineReaderInit(
|
||||||
|
&reader,
|
||||||
|
&file->file,
|
||||||
|
readBuffer,
|
||||||
|
sizeof(readBuffer),
|
||||||
|
lineBuffer,
|
||||||
|
sizeof(lineBuffer)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Skip blanks, comments, etc and start looking for msgid's
|
||||||
|
errorChain(assetLocaleLineSkipBlanks(&reader, lineBuffer));
|
||||||
|
|
||||||
|
while(!reader.eof) {
|
||||||
|
// Is this msgid?
|
||||||
|
if(memoryCompare(lineBuffer, "msgid", 5) != 0) {
|
||||||
|
errorChain(assetFileLineReaderNext(&reader));
|
||||||
|
msgidBuffer[0] = '\0';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unbuffer the msgid
|
||||||
|
assetLocaleLineUnbuffer(
|
||||||
|
&reader, lineBuffer, (uint8_t *)msgidBuffer, sizeof(msgidBuffer)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Is this the needle?
|
||||||
|
if(memoryCompare(msgidBuffer, messageId, strlen(messageId)) != 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
msgidFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(!msgidFound) {
|
||||||
|
errorThrow("Failed to find message ID: %s", messageId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// We are either going to see a msgstr or a msgid_plural
|
||||||
|
while(!reader.eof) {
|
||||||
|
errorChain(assetLocaleLineSkipBlanks(&reader, lineBuffer));
|
||||||
|
|
||||||
|
// Is msgid_plural?
|
||||||
|
if(
|
||||||
|
!msgidPluralFound &&
|
||||||
|
memoryCompare(lineBuffer, "msgid_plural", 12) == 0
|
||||||
|
) {
|
||||||
|
// Yes, start reading plural ID
|
||||||
|
assetLocaleLineUnbuffer(
|
||||||
|
&reader,
|
||||||
|
lineBuffer,
|
||||||
|
(uint8_t *)msgidPluralBuffer,
|
||||||
|
sizeof(msgidPluralBuffer)
|
||||||
|
);
|
||||||
|
msgidPluralFound = true;
|
||||||
|
|
||||||
|
// At this point we determine the plural index to use by running the
|
||||||
|
// plural formula
|
||||||
|
pluralIndex = assetLocaleEvaluatePlural(
|
||||||
|
file,
|
||||||
|
pluralCount
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should be msgstr if not plural.
|
||||||
|
if(memoryCompare(lineBuffer, "msgstr", 6) != 0) {
|
||||||
|
errorThrow("Expected msgstr after msgid, found: %s", lineBuffer);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If plural we need an index
|
||||||
|
if(msgidPluralFound) {
|
||||||
|
// Skip blank chars
|
||||||
|
char_t *ptr = (char_t *)lineBuffer + 6;
|
||||||
|
while(*ptr == ' ') {
|
||||||
|
ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*ptr != '[') {
|
||||||
|
errorThrow("Expected [ for plural form, found: %s", lineBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr++; // move past [
|
||||||
|
|
||||||
|
// Parse until ]
|
||||||
|
char *end = strchr(ptr, ']');
|
||||||
|
if(!end) {
|
||||||
|
errorThrow("Expected ] for plural form, found: %s", lineBuffer);
|
||||||
|
}
|
||||||
|
*end = '\0';
|
||||||
|
|
||||||
|
int32_t index = atoi(ptr);
|
||||||
|
if(index != pluralIndex) {
|
||||||
|
// Not the plural form we want, skip to the next useable line
|
||||||
|
while(!reader.eof) {
|
||||||
|
errorChain(assetFileLineReaderNext(&reader));
|
||||||
|
errorChain(assetLocaleLineSkipBlanks(&reader, lineBuffer));
|
||||||
|
if(
|
||||||
|
lineBuffer[0] == '\"' ||
|
||||||
|
lineBuffer[0] == '\0' ||
|
||||||
|
lineBuffer[0] == '#'
|
||||||
|
) continue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Undo damage to line buffer from unbuffering.
|
||||||
|
*end = ']';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse out msgstr
|
||||||
|
errorChain(assetLocaleLineUnbuffer(
|
||||||
|
&reader, lineBuffer, (uint8_t *)stringBuffer, stringBufferSize
|
||||||
|
));
|
||||||
|
msgstrFound = true;
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
if(!msgstrFound) {
|
||||||
|
errorThrow("Failed to find msgstr for message ID: %s", messageId);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleGetStringWithVA(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *messageId,
|
||||||
|
const int32_t pluralIndex,
|
||||||
|
char_t *buffer,
|
||||||
|
const size_t bufferSize,
|
||||||
|
...
|
||||||
|
) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL.");
|
||||||
|
assertNotNull(messageId, "Message ID cannot be NULL.");
|
||||||
|
assertNotNull(buffer, "Buffer cannot be NULL.");
|
||||||
|
assertTrue(bufferSize > 0, "Buffer size must be > 0.");
|
||||||
|
assertTrue(pluralIndex >= 0, "Plural cannot be negative.");
|
||||||
|
|
||||||
|
char_t *tempBuffer;
|
||||||
|
tempBuffer = memoryAllocate(bufferSize);
|
||||||
|
errorret_t ret = assetLocaleGetString(
|
||||||
|
file,
|
||||||
|
messageId,
|
||||||
|
pluralIndex,
|
||||||
|
tempBuffer,
|
||||||
|
bufferSize
|
||||||
|
);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
memoryFree(tempBuffer);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
va_list args;
|
||||||
|
va_start(args, bufferSize);
|
||||||
|
int result = vsnprintf(buffer, bufferSize, tempBuffer, args);
|
||||||
|
va_end(args);
|
||||||
|
memoryFree(tempBuffer);
|
||||||
|
|
||||||
|
if(result < 0) {
|
||||||
|
errorThrow("Failed to format locale string for ID: %s", messageId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetLocaleGetStringWithArgs(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *id,
|
||||||
|
const int32_t plural,
|
||||||
|
char_t *buffer,
|
||||||
|
const size_t bufferSize,
|
||||||
|
const assetlocalearg_t *args,
|
||||||
|
const size_t argCount
|
||||||
|
) {
|
||||||
|
assertNotNull(id, "Message ID cannot be NULL.");
|
||||||
|
assertNotNull(buffer, "Buffer cannot be NULL.");
|
||||||
|
assertTrue(bufferSize > 0, "Buffer size must be > 0.");
|
||||||
|
assertTrue(plural >= 0, "Plural cannot be negative.");
|
||||||
|
assertTrue(
|
||||||
|
argCount == 0 || args != NULL,
|
||||||
|
"Args cannot be NULL when argCount > 0."
|
||||||
|
);
|
||||||
|
|
||||||
|
char_t *format = memoryAllocate(bufferSize);
|
||||||
|
if(format == NULL) {
|
||||||
|
errorThrow("Failed to allocate format buffer.");
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t ret = assetLocaleGetString(
|
||||||
|
file,
|
||||||
|
id,
|
||||||
|
plural,
|
||||||
|
format,
|
||||||
|
bufferSize
|
||||||
|
);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
memoryFree(format);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t inIndex = 0;
|
||||||
|
size_t outIndex = 0;
|
||||||
|
size_t nextArg = 0;
|
||||||
|
|
||||||
|
buffer[0] = '\0';
|
||||||
|
|
||||||
|
while(format[inIndex] != '\0') {
|
||||||
|
if(format[inIndex] != '%') {
|
||||||
|
if(outIndex + 1 >= bufferSize) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Formatted locale string buffer overflow for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[outIndex++] = format[inIndex++];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
inIndex++;
|
||||||
|
|
||||||
|
/* Escaped percent */
|
||||||
|
if(format[inIndex] == '%') {
|
||||||
|
if(outIndex + 1 >= bufferSize) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Formatted locale string buffer overflow for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[outIndex++] = '%';
|
||||||
|
inIndex++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(nextArg >= argCount) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Not enough locale arguments for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
char_t specBuffer[32];
|
||||||
|
char_t valueBuffer[256];
|
||||||
|
size_t specLength = 0;
|
||||||
|
int written = 0;
|
||||||
|
char_t specifier;
|
||||||
|
|
||||||
|
specBuffer[specLength++] = '%';
|
||||||
|
|
||||||
|
/* Allow flags / width / precision */
|
||||||
|
while(format[inIndex] != '\0') {
|
||||||
|
char_t ch = format[inIndex];
|
||||||
|
|
||||||
|
if(
|
||||||
|
ch == '-' || ch == '+' || ch == ' ' || ch == '#' || ch == '0' ||
|
||||||
|
ch == '.' || (ch >= '0' && ch <= '9')
|
||||||
|
) {
|
||||||
|
if(specLength + 1 >= sizeof(specBuffer)) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Format specifier too long for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
specBuffer[specLength++] = ch;
|
||||||
|
inIndex++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(format[inIndex] == '\0') {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Incomplete format specifier for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
specifier = format[inIndex++];
|
||||||
|
|
||||||
|
if(specifier != 's' && specifier != 'd' && specifier != 'f') {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow(
|
||||||
|
"Unsupported format specifier '%%%c' for ID: %s",
|
||||||
|
specifier,
|
||||||
|
id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
specBuffer[specLength++] = specifier;
|
||||||
|
specBuffer[specLength] = '\0';
|
||||||
|
|
||||||
|
switch(specifier) {
|
||||||
|
case 's':
|
||||||
|
if(args[nextArg].type != ASSET_LOCALE_ARG_STRING) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Expected string locale argument for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
written = snprintf(
|
||||||
|
valueBuffer,
|
||||||
|
sizeof(valueBuffer),
|
||||||
|
specBuffer,
|
||||||
|
args[nextArg].stringValue ? args[nextArg].stringValue : ""
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'd':
|
||||||
|
if(args[nextArg].type != ASSET_LOCALE_ARG_INT) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Expected int locale argument for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
written = snprintf(
|
||||||
|
valueBuffer,
|
||||||
|
sizeof(valueBuffer),
|
||||||
|
specBuffer,
|
||||||
|
args[nextArg].intValue
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'f':
|
||||||
|
if(
|
||||||
|
args[nextArg].type != ASSET_LOCALE_ARG_FLOAT &&
|
||||||
|
args[nextArg].type != ASSET_LOCALE_ARG_INT
|
||||||
|
) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Expected float or int locale argument for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
float_t floatValue = (
|
||||||
|
args[nextArg].type == ASSET_LOCALE_ARG_FLOAT ?
|
||||||
|
args[nextArg].floatValue :
|
||||||
|
(float_t)args[nextArg].intValue
|
||||||
|
);
|
||||||
|
|
||||||
|
written = snprintf(
|
||||||
|
valueBuffer,
|
||||||
|
sizeof(valueBuffer),
|
||||||
|
specBuffer,
|
||||||
|
floatValue
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
nextArg++;
|
||||||
|
|
||||||
|
if(written < 0) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Failed to format locale string for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(outIndex + (size_t)written >= bufferSize) {
|
||||||
|
memoryFree(format);
|
||||||
|
errorThrow("Formatted locale string buffer overflow for ID: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryCopy(buffer + outIndex, valueBuffer, (size_t)written);
|
||||||
|
outIndex += (size_t)written;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[outIndex] = '\0';
|
||||||
|
memoryFree(format);
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "asset/asset.h"
|
||||||
|
|
||||||
|
#define ASSET_LOCALE_FILE_PLURAL_FORM_COUNT 6
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_LOCALE_PLURAL_OP_EQUAL,
|
||||||
|
ASSET_LOCALE_PLURAL_OP_NOT_EQUAL,
|
||||||
|
ASSET_LOCALE_PLURAL_OP_LESS,
|
||||||
|
ASSET_LOCALE_PLURAL_OP_LESS_EQUAL,
|
||||||
|
ASSET_LOCALE_PLURAL_OP_GREATER,
|
||||||
|
ASSET_LOCALE_PLURAL_OP_GREATER_EQUAL
|
||||||
|
} assetlocalepluraloperation_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ASSET_LOCALE_ARG_STRING,
|
||||||
|
ASSET_LOCALE_ARG_INT,
|
||||||
|
ASSET_LOCALE_ARG_FLOAT
|
||||||
|
} assetlocaleargtype_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
assetlocaleargtype_t type;
|
||||||
|
union {
|
||||||
|
const char_t *stringValue;
|
||||||
|
int32_t intValue;
|
||||||
|
float_t floatValue;
|
||||||
|
};
|
||||||
|
} assetlocalearg_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
assetfile_t file;
|
||||||
|
assetlocalepluraloperation_t pluralOps[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||||
|
int32_t pluralValues[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||||
|
int32_t pluralIndices[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||||
|
uint8_t pluralStateCount;
|
||||||
|
uint8_t pluralDefaultIndex;
|
||||||
|
} assetlocalefile_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize a locale asset file.
|
||||||
|
*
|
||||||
|
* @param localeFile The locale file to initialize.
|
||||||
|
* @param path The path to the locale file.
|
||||||
|
* @return An error code if a failure occurs.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleFileInit(
|
||||||
|
assetlocalefile_t *localeFile,
|
||||||
|
const char_t *path
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dispose of a locale asset file.
|
||||||
|
*
|
||||||
|
* @param localeFile The locale file to dispose of.
|
||||||
|
* @return An error code if a failure occurs.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleFileDispose(assetlocalefile_t *localeFile);
|
||||||
|
|
||||||
|
errorret_t assetLocaleParseHeader(
|
||||||
|
assetlocalefile_t *localeFile,
|
||||||
|
char_t *headerBuffer,
|
||||||
|
const size_t headerBufferSize
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Skips blank lines and comment lines in the line reader.
|
||||||
|
*
|
||||||
|
* @param reader Line reader to read from.
|
||||||
|
* @param lineBuffer Buffer to use for reading lines.
|
||||||
|
* @return Any error that occurs during skipping.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleLineSkipBlanks(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
uint8_t *lineBuffer
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unbuffers a potentially multi-line quoted string from the line reader.
|
||||||
|
*
|
||||||
|
* This will read lines until it finds a line that starts with a quote, then
|
||||||
|
* read until the closing quote.
|
||||||
|
*
|
||||||
|
* @param reader Line reader to read from.
|
||||||
|
* @param lineBuffer Buffer to use for reading lines.
|
||||||
|
* @param stringBuffer Buffer to write the unbuffered string to.
|
||||||
|
* @param stringBufferSize Size of the string buffer.
|
||||||
|
* @return Any error that occurs during unbuffering.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleLineUnbuffer(
|
||||||
|
assetfilelinereader_t *reader,
|
||||||
|
uint8_t *lineBuffer,
|
||||||
|
uint8_t *stringBuffer,
|
||||||
|
const size_t stringBufferSize
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test function for locale asset loading.
|
||||||
|
*
|
||||||
|
* @param file Asset file to test loading from.
|
||||||
|
* @param messageId The message ID to retrieve.
|
||||||
|
* @param pluralCount Count for formulating the plural variant.
|
||||||
|
* @param stringBuffer Buffer to write the retrieved string to.
|
||||||
|
* @param stringBufferSize Size of the string buffer.
|
||||||
|
* @return Any error that occurs during testing.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleGetString(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *messageId,
|
||||||
|
const int32_t pluralCount,
|
||||||
|
char_t *stringBuffer,
|
||||||
|
const size_t stringBufferSize
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test function for locale asset loading with a variable argument list.
|
||||||
|
*
|
||||||
|
* @param file Asset file to test loading from.
|
||||||
|
* @param messageId The message ID to retrieve.
|
||||||
|
* @param pluralCount Count for formulating the plural variant.
|
||||||
|
* @param buffer Buffer to write the retrieved string to.
|
||||||
|
* @param bufferSize Size of the buffer.
|
||||||
|
* @param ... Additional arguments for formatting the string.
|
||||||
|
* @return Any error that occurs during testing.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleGetStringWithVA(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *messageId,
|
||||||
|
const int32_t pluralCount,
|
||||||
|
char_t *buffer,
|
||||||
|
const size_t bufferSize,
|
||||||
|
...
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test function for locale asset loading with a list of arguments.
|
||||||
|
*
|
||||||
|
* @param file Asset file to test loading from.
|
||||||
|
* @param messageId The message ID to retrieve.
|
||||||
|
* @param pluralCount Count for formulating the plural variant.
|
||||||
|
* @param buffer Buffer to write the retrieved string to.
|
||||||
|
* @param bufferSize Size of the buffer.
|
||||||
|
* @param args List of arguments for formatting the string.
|
||||||
|
* @param argCount Number of arguments in the list.
|
||||||
|
* @return Any error that occurs during testing.
|
||||||
|
*/
|
||||||
|
errorret_t assetLocaleGetStringWithArgs(
|
||||||
|
assetlocalefile_t *file,
|
||||||
|
const char_t *messageId,
|
||||||
|
const int32_t pluralCount,
|
||||||
|
char_t *buffer,
|
||||||
|
const size_t bufferSize,
|
||||||
|
const assetlocalearg_t *args,
|
||||||
|
const size_t argCount
|
||||||
|
);
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
assetscriptloader.c
|
||||||
|
)
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "assetscriptloader.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
errorret_t assetScriptLoader(assetfile_t *file) {
|
||||||
|
assertNotNull(file, "Asset file cannot be NULL");
|
||||||
|
assertNull(file->zipFile, "Asset file zip handle must be NULL");
|
||||||
|
assertNotNull(file->output, "Asset file output cannot be NULL");
|
||||||
|
|
||||||
|
assetscript_t *script = (assetscript_t *)file->output;
|
||||||
|
|
||||||
|
// Open the asset for buffering
|
||||||
|
errorChain(assetFileOpen(file));
|
||||||
|
|
||||||
|
// Request loading
|
||||||
|
if(!lua_load(
|
||||||
|
script->ctx->luaState,
|
||||||
|
assetScriptReader,
|
||||||
|
file,
|
||||||
|
file->filename,
|
||||||
|
NULL
|
||||||
|
) == LUA_OK) {
|
||||||
|
const char_t *strErr = lua_tostring(script->ctx->luaState, -1);
|
||||||
|
lua_pop(script->ctx->luaState, 1);
|
||||||
|
errorThrow("Failed to load Lua script: %s", strErr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now loaded, exec
|
||||||
|
if(lua_pcall(script->ctx->luaState, 0, LUA_MULTRET, 0) != LUA_OK) {
|
||||||
|
const char_t *strErr = lua_tostring(script->ctx->luaState, -1);
|
||||||
|
lua_pop(script->ctx->luaState, 1);
|
||||||
|
errorThrow("Failed to execute Lua script: %s", strErr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the file
|
||||||
|
return assetFileClose(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorret_t assetScriptLoad(const char_t *path, scriptcontext_t *ctx) {
|
||||||
|
assertNotNull(path, "Script path cannot be NULL");
|
||||||
|
assertNotNull(ctx, "Script context cannot be NULL");
|
||||||
|
|
||||||
|
assetscript_t script;
|
||||||
|
script.ctx = ctx;
|
||||||
|
|
||||||
|
return assetLoad(
|
||||||
|
path,
|
||||||
|
assetScriptLoader,
|
||||||
|
NULL,
|
||||||
|
&script
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char_t * assetScriptReader(lua_State* L, void* data, size_t* size) {
|
||||||
|
assetfile_t *file = (assetfile_t*)data;
|
||||||
|
assertNotNull(file, "Script asset file cannot be NULL");
|
||||||
|
assertNotNull(file->zipFile, "Script asset zip handle cannot be NULL");
|
||||||
|
assertNotNull(file->output, "Script asset output cannot be NULL");
|
||||||
|
|
||||||
|
assetscript_t *script = (assetscript_t *)file->output;
|
||||||
|
assertNotNull(script, "Script asset output cannot be NULL");
|
||||||
|
|
||||||
|
zip_int64_t read = zip_fread(
|
||||||
|
file->zipFile,
|
||||||
|
script->buffer,
|
||||||
|
sizeof(script->buffer)
|
||||||
|
);
|
||||||
|
|
||||||
|
if(read < 0) {
|
||||||
|
*size = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*size = (size_t)read;
|
||||||
|
return script->buffer;
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "asset/asset.h"
|
||||||
|
#include "script/scriptcontext.h"
|
||||||
|
|
||||||
|
#define ASSET_SCRIPT_BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *nothing;
|
||||||
|
} assetscriptloaderparams_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
scriptcontext_t *ctx;
|
||||||
|
char_t buffer[ASSET_SCRIPT_BUFFER_SIZE];
|
||||||
|
} assetscript_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for script assets.
|
||||||
|
*
|
||||||
|
* @param file Asset file to load the script from.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetScriptLoader(assetfile_t *file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a script from the specified path.
|
||||||
|
*
|
||||||
|
* @param path Path to the script asset.
|
||||||
|
* @param ctx Script context to load the script into.
|
||||||
|
* @return Any error that occurs during loading.
|
||||||
|
*/
|
||||||
|
errorret_t assetScriptLoad(const char_t *path, scriptcontext_t *ctx);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reader function for Lua to read script data from the asset.
|
||||||
|
*
|
||||||
|
* @param L Lua state.
|
||||||
|
* @param data Pointer to the scriptcontext_t structure.
|
||||||
|
* @param size Pointer to store the size of the read data.
|
||||||
|
* @return Pointer to the read data buffer.
|
||||||
|
*/
|
||||||
|
const char_t * assetScriptReader(lua_State* L, void* data, size_t* size);
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "asset/asset.h"
|
|
||||||
#include "assert/assert.h"
|
|
||||||
#include "util/memory.h"
|
|
||||||
|
|
||||||
errorret_t assetMapHandler(assetcustom_t custom) {
|
|
||||||
printf("Map Loaded from asset!\n");
|
|
||||||
errorOk();
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "error/error.h"
|
|
||||||
|
|
||||||
typedef struct assetcustom_s assetcustom_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads a map asset from the given data pointer into the output map structure.
|
|
||||||
*
|
|
||||||
* @param data Pointer to the raw assetmap_t data.
|
|
||||||
* @param output Pointer to the map_t to load the map into.
|
|
||||||
* @return An error code.
|
|
||||||
*/
|
|
||||||
errorret_t assetMapHandler(assetcustom_t custom);
|
|
||||||
@@ -1,185 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "asset/asset.h"
|
|
||||||
#include "assert/assert.h"
|
|
||||||
#include "map/mapchunk.h"
|
|
||||||
#include "util/endian.h"
|
|
||||||
|
|
||||||
#pragma pack(push, 1)
|
|
||||||
typedef struct {
|
|
||||||
uint32_t tileCount;
|
|
||||||
uint8_t modelCount;
|
|
||||||
uint8_t entityCount;
|
|
||||||
} assetchunkheader_t;
|
|
||||||
#pragma pack(pop)
|
|
||||||
|
|
||||||
#pragma pack(push, 1)
|
|
||||||
typedef struct {
|
|
||||||
maptile_t tile;
|
|
||||||
} assetchunktiledata_t;
|
|
||||||
#pragma pack(pop)
|
|
||||||
|
|
||||||
#pragma pack(push, 1)
|
|
||||||
typedef struct {
|
|
||||||
uint32_t vertexCount;
|
|
||||||
} assetchunkmodelheader_t;
|
|
||||||
#pragma pack(pop)
|
|
||||||
|
|
||||||
#pragma pack(push, 1)
|
|
||||||
typedef struct {
|
|
||||||
uint8_t entityType;
|
|
||||||
uint8_t localX;
|
|
||||||
uint8_t localY;
|
|
||||||
uint8_t localZ;
|
|
||||||
} assetchunkentityheader_t;
|
|
||||||
#pragma pack(pop)
|
|
||||||
|
|
||||||
errorret_t assetMapChunkHandler(assetcustom_t custom) {
|
|
||||||
assertNotNull(custom.output, "Output pointer cannot be NULL");
|
|
||||||
assertNotNull(custom.zipFile, "Zip file pointer cannot be NULL");
|
|
||||||
|
|
||||||
mapchunk_t *chunk = (mapchunk_t *)custom.output;
|
|
||||||
assertTrue(chunk->meshCount == 0, "Chunk is not in a good state");
|
|
||||||
|
|
||||||
// Read header
|
|
||||||
assetchunkheader_t header;
|
|
||||||
size_t bytesRead = zip_fread(
|
|
||||||
custom.zipFile, &header, sizeof(assetchunkheader_t)
|
|
||||||
);
|
|
||||||
if(bytesRead != sizeof(assetchunkheader_t)) {
|
|
||||||
zip_fclose(custom.zipFile);
|
|
||||||
errorThrow("Failed to read chunk asset header.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fix endianess if necessary
|
|
||||||
header.tileCount = endianLittleToHost32(header.tileCount);
|
|
||||||
|
|
||||||
if(header.tileCount != CHUNK_TILE_COUNT) {
|
|
||||||
zip_fclose(custom.zipFile);
|
|
||||||
errorThrow(
|
|
||||||
"Chunk asset has invalid tile count: %d (expected %d).",
|
|
||||||
header.tileCount,
|
|
||||||
CHUNK_TILE_COUNT
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(header.modelCount > CHUNK_MESH_COUNT_MAX) {
|
|
||||||
zip_fclose(custom.zipFile);
|
|
||||||
errorThrow(
|
|
||||||
"Chunk asset has too many models: %d (max %d).",
|
|
||||||
header.modelCount,
|
|
||||||
CHUNK_MESH_COUNT_MAX
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(header.entityCount > CHUNK_ENTITY_COUNT_MAX) {
|
|
||||||
zip_fclose(custom.zipFile);
|
|
||||||
errorThrow(
|
|
||||||
"Chunk asset has too many entities: %d (max %d).",
|
|
||||||
header.entityCount,
|
|
||||||
CHUNK_ENTITY_COUNT_MAX
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
chunk->meshCount = header.modelCount;
|
|
||||||
|
|
||||||
// Read tile data
|
|
||||||
bytesRead = zip_fread(
|
|
||||||
custom.zipFile,
|
|
||||||
chunk->tiles,
|
|
||||||
sizeof(assetchunktiledata_t) * header.tileCount
|
|
||||||
);
|
|
||||||
if(bytesRead != sizeof(assetchunktiledata_t) * header.tileCount) {
|
|
||||||
zip_fclose(custom.zipFile);
|
|
||||||
errorThrow("Failed to read chunk tile data.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// For each model...
|
|
||||||
uint32_t vertexIndex = 0;
|
|
||||||
for(uint8_t i = 0; i < header.modelCount; i++) {
|
|
||||||
assetchunkmodelheader_t modelHeader;
|
|
||||||
bytesRead = zip_fread(
|
|
||||||
custom.zipFile, &modelHeader, sizeof(assetchunkmodelheader_t)
|
|
||||||
);
|
|
||||||
if(bytesRead != sizeof(assetchunkmodelheader_t)) {
|
|
||||||
zip_fclose(custom.zipFile);
|
|
||||||
errorThrow("Failed to read chunk model header.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fix endianess if necessary
|
|
||||||
modelHeader.vertexCount = endianLittleToHost32(modelHeader.vertexCount);
|
|
||||||
|
|
||||||
if(
|
|
||||||
vertexIndex + modelHeader.vertexCount >
|
|
||||||
CHUNK_VERTEX_COUNT_MAX
|
|
||||||
) {
|
|
||||||
zip_fclose(custom.zipFile);
|
|
||||||
errorThrow("Chunk model vertex count exceeds maximum.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read vertex data.
|
|
||||||
bytesRead = zip_fread(
|
|
||||||
custom.zipFile,
|
|
||||||
&chunk->vertices[vertexIndex],
|
|
||||||
sizeof(meshvertex_t) * modelHeader.vertexCount
|
|
||||||
);
|
|
||||||
if(bytesRead != sizeof(meshvertex_t) * modelHeader.vertexCount) {
|
|
||||||
zip_fclose(custom.zipFile);
|
|
||||||
errorThrow("Failed to read chunk model vertex data.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init the mesh
|
|
||||||
if(modelHeader.vertexCount > 0) {
|
|
||||||
mesh_t *mesh = &chunk->meshes[i];
|
|
||||||
meshInit(
|
|
||||||
mesh,
|
|
||||||
MESH_PRIMITIVE_TYPE_TRIANGLES,
|
|
||||||
modelHeader.vertexCount,
|
|
||||||
&chunk->vertices[vertexIndex]
|
|
||||||
);
|
|
||||||
|
|
||||||
vertexIndex += modelHeader.vertexCount;
|
|
||||||
} else {
|
|
||||||
// chunk->meshes[i].vertexCount = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read entity data
|
|
||||||
// for(uint8_t i = 0; i < header.entityCount; i++) {
|
|
||||||
// assetchunkentityheader_t entityHeader;
|
|
||||||
// bytesRead = zip_fread(
|
|
||||||
// custom.zipFile, &entityHeader, sizeof(assetchunkentityheader_t)
|
|
||||||
// );
|
|
||||||
// if(bytesRead != sizeof(assetchunkentityheader_t)) {
|
|
||||||
// zip_fclose(custom.zipFile);
|
|
||||||
// errorThrow("Failed to read chunk entity header.");
|
|
||||||
// }
|
|
||||||
|
|
||||||
// uint8_t entityIndex = entityGetAvailable();
|
|
||||||
// if(entityIndex == 0xFF) {
|
|
||||||
// zip_fclose(custom.zipFile);
|
|
||||||
// errorThrow("No available entity slots.");
|
|
||||||
// }
|
|
||||||
|
|
||||||
// entity_t *entity = &ENTITIES[entityIndex];
|
|
||||||
// entityInit(entity, (entitytype_t)entityHeader.entityType);
|
|
||||||
// entity->position.x = (
|
|
||||||
// (chunk->position.x * CHUNK_WIDTH) + entityHeader.localX
|
|
||||||
// );
|
|
||||||
// entity->position.y = (
|
|
||||||
// (chunk->position.y * CHUNK_HEIGHT) + entityHeader.localY
|
|
||||||
// );
|
|
||||||
// entity->position.z = (
|
|
||||||
// (chunk->position.z * CHUNK_DEPTH) + entityHeader.localZ
|
|
||||||
// );
|
|
||||||
|
|
||||||
// chunk->entities[i] = entityIndex;
|
|
||||||
// }
|
|
||||||
|
|
||||||
errorOk();
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "error/error.h"
|
|
||||||
|
|
||||||
typedef struct assetcustom_s assetcustom_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles loading of map chunk data from a map chunk asset file.
|
|
||||||
*
|
|
||||||
* @param custom The custom asset loading parameters.
|
|
||||||
* @return An error code.
|
|
||||||
*/
|
|
||||||
errorret_t assetMapChunkHandler(assetcustom_t custom);
|
|
||||||
@@ -10,7 +10,6 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Subdirectories
|
# Subdirectories
|
||||||
add_subdirectory(camera)
|
|
||||||
add_subdirectory(framebuffer)
|
add_subdirectory(framebuffer)
|
||||||
add_subdirectory(mesh)
|
add_subdirectory(mesh)
|
||||||
add_subdirectory(screen)
|
add_subdirectory(screen)
|
||||||
@@ -22,7 +21,7 @@ add_subdirectory(texture)
|
|||||||
# Color definitions
|
# Color definitions
|
||||||
dusk_run_python(
|
dusk_run_python(
|
||||||
dusk_color_defs
|
dusk_color_defs
|
||||||
tools.display.color.csv
|
tools.color.csv
|
||||||
--csv ${CMAKE_CURRENT_SOURCE_DIR}/color.csv
|
--csv ${CMAKE_CURRENT_SOURCE_DIR}/color.csv
|
||||||
--output ${DUSK_GENERATED_HEADERS_DIR}/display/color.h
|
--output ${DUSK_GENERATED_HEADERS_DIR}/display/color.h
|
||||||
)
|
)
|
||||||
|
|||||||
+41
-34
@@ -10,6 +10,11 @@
|
|||||||
#include "scene/scene.h"
|
#include "scene/scene.h"
|
||||||
#include "display/spritebatch/spritebatch.h"
|
#include "display/spritebatch/spritebatch.h"
|
||||||
#include "display/mesh/quad.h"
|
#include "display/mesh/quad.h"
|
||||||
|
#include "display/mesh/cube.h"
|
||||||
|
#include "display/mesh/sphere.h"
|
||||||
|
#include "display/mesh/plane.h"
|
||||||
|
#include "display/mesh/capsule.h"
|
||||||
|
#include "display/mesh/triprism.h"
|
||||||
#include "display/screen/screen.h"
|
#include "display/screen/screen.h"
|
||||||
#include "ui/ui.h"
|
#include "ui/ui.h"
|
||||||
#include "display/text/text.h"
|
#include "display/text/text.h"
|
||||||
@@ -23,24 +28,6 @@
|
|||||||
#include "script/module/display/moduleshader.h"
|
#include "script/module/display/moduleshader.h"
|
||||||
|
|
||||||
display_t DISPLAY = { 0 };
|
display_t DISPLAY = { 0 };
|
||||||
mesh_t mesh;
|
|
||||||
meshvertex_t vertices[3] = {
|
|
||||||
{
|
|
||||||
.color = { 255, 0, 0, 255 },
|
|
||||||
.uv = { 0.0f, 0.0f },
|
|
||||||
.pos = { 0.0f, 0.5f, 0.0f }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.color = { 0, 255, 0, 255 },
|
|
||||||
.uv = { 0.5f, 1.0f },
|
|
||||||
.pos = { -0.5f, -0.5f, 0.0f }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.color = { 0, 0, 255, 255 },
|
|
||||||
.uv = { 1.0f, 0.0f },
|
|
||||||
.pos = { 0.5f, -0.5f, 0.0f }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
errorret_t displayInit(void) {
|
errorret_t displayInit(void) {
|
||||||
memoryZero(&DISPLAY, sizeof(DISPLAY));
|
memoryZero(&DISPLAY, sizeof(DISPLAY));
|
||||||
@@ -48,27 +35,50 @@ errorret_t displayInit(void) {
|
|||||||
#ifdef displayPlatformInit
|
#ifdef displayPlatformInit
|
||||||
errorChain(displayPlatformInit());
|
errorChain(displayPlatformInit());
|
||||||
#endif
|
#endif
|
||||||
|
errorChain(textureInit(
|
||||||
|
&TEXTURE_WHITE, 4, 4,
|
||||||
|
TEXTURE_FORMAT_RGBA, (texturedata_t){ .rgbaColors = TEXTURE_WHITE_PIXELS }
|
||||||
|
));
|
||||||
|
|
||||||
|
// Standard meshes
|
||||||
errorChain(quadInit());
|
errorChain(quadInit());
|
||||||
|
errorChain(cubeInit());
|
||||||
|
errorChain(sphereInit());
|
||||||
|
errorChain(planeInit());
|
||||||
|
errorChain(capsuleInit());
|
||||||
|
errorChain(triPrismInit());
|
||||||
|
|
||||||
errorChain(frameBufferInitBackBuffer());
|
errorChain(frameBufferInitBackBuffer());
|
||||||
errorChain(spriteBatchInit());
|
errorChain(spriteBatchInit());
|
||||||
errorChain(textInit());
|
errorChain(textInit());
|
||||||
errorChain(screenInit());
|
errorChain(screenInit());
|
||||||
|
|
||||||
// Setup initial shader with default values
|
// Setup initial shader with default values
|
||||||
errorChain(shaderInit(&SHADER_UNLIT, &SHADER_UNLIT_DEFINITION));
|
mat4 view, proj, model;
|
||||||
camera_t cam;
|
glm_lookat(
|
||||||
cameraInit(&cam);
|
(vec3){ 0.0f, 0.0f, 1.0f },
|
||||||
mat4 mat;
|
(vec3){ 0.0f, 0.0f, 0.0f },
|
||||||
cameraGetProjectionMatrix(&cam, mat);
|
(vec3){ 0.0f, 1.0f, 0.0f },
|
||||||
errorChain(shaderBind(&SHADER_UNLIT));
|
view
|
||||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, mat));
|
);
|
||||||
cameraGetViewMatrix(&cam, mat);
|
|
||||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, mat));
|
|
||||||
glm_mat4_identity(mat);
|
|
||||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, mat));
|
|
||||||
errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, NULL));
|
|
||||||
|
|
||||||
errorChain(meshInit(&mesh, MESH_PRIMITIVE_TYPE_TRIANGLES, 3, vertices));
|
glm_perspective(
|
||||||
|
glm_rad(45.0f),
|
||||||
|
SCREEN.aspect,
|
||||||
|
0.1f,
|
||||||
|
100.0f,
|
||||||
|
proj
|
||||||
|
);
|
||||||
|
|
||||||
|
glm_mat4_identity(model);
|
||||||
|
|
||||||
|
errorChain(shaderInit(&SHADER_UNLIT, &SHADER_UNLIT_DEFINITION));
|
||||||
|
errorChain(shaderBind(&SHADER_UNLIT));
|
||||||
|
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(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, NULL));
|
||||||
|
errorChain(shaderSetColor(&SHADER_UNLIT, SHADER_UNLIT_COLOR, COLOR_WHITE));
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -91,9 +101,6 @@ errorret_t displayUpdate(void) {
|
|||||||
|
|
||||||
errorChain(sceneRender());
|
errorChain(sceneRender());
|
||||||
|
|
||||||
// Render UI
|
|
||||||
// uiRender();
|
|
||||||
|
|
||||||
// Finish up
|
// Finish up
|
||||||
screenUnbind();
|
screenUnbind();
|
||||||
screenRender();
|
screenRender();
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "display/displayplatform.h"
|
#include "display/displayplatform.h"
|
||||||
#include "display/camera/camera.h"
|
|
||||||
|
|
||||||
// Expecting some definitions to be provided
|
// Expecting some definitions to be provided
|
||||||
#ifndef DUSK_DISPLAY_SIZE_DYNAMIC
|
#ifndef DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
|
|||||||
@@ -40,6 +40,17 @@ uint32_t frameBufferGetHeight(const framebuffer_t *framebuffer) {
|
|||||||
return frameBufferPlatformGetHeight(framebuffer);
|
return frameBufferPlatformGetHeight(framebuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float_t frameBufferGetAspect(const framebuffer_t *framebuffer) {
|
||||||
|
#ifdef frameBufferPlatformGetAspect
|
||||||
|
return frameBufferPlatformGetAspect(framebuffer);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
uint32_t width = frameBufferGetWidth(framebuffer);
|
||||||
|
uint32_t height = frameBufferGetHeight(framebuffer);
|
||||||
|
if(height == 0) return 1.0f; // Avoid divide by zero, just return 1:1 aspect.
|
||||||
|
return (float_t)width / (float_t)height;
|
||||||
|
}
|
||||||
|
|
||||||
void frameBufferClear(const uint8_t flags, const color_t color) {
|
void frameBufferClear(const uint8_t flags, const color_t color) {
|
||||||
frameBufferPlatformClear(flags, color);
|
frameBufferPlatformClear(flags, color);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,16 @@ uint32_t frameBufferGetWidth(const framebuffer_t *framebuffer);
|
|||||||
*/
|
*/
|
||||||
uint32_t frameBufferGetHeight(const framebuffer_t *framebuffer);
|
uint32_t frameBufferGetHeight(const framebuffer_t *framebuffer);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the aspect ratio of the framebuffer. This is ALMOST always just
|
||||||
|
* the width / height, however some platforms may choose to override this if
|
||||||
|
* they have stretched styled back buffers, e.g. 640x480 stretched.
|
||||||
|
*
|
||||||
|
* @param framebuffer The framebuffer to get the aspect ratio of.
|
||||||
|
* @return The aspect ratio of the framebuffer.
|
||||||
|
*/
|
||||||
|
float_t frameBufferGetAspect(const framebuffer_t *framebuffer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Binds the framebuffer for rendering, or the backbuffer if the framebuffer
|
* Binds the framebuffer for rendering, or the backbuffer if the framebuffer
|
||||||
* provided is NULL.
|
* provided is NULL.
|
||||||
|
|||||||
@@ -8,4 +8,9 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
|||||||
PUBLIC
|
PUBLIC
|
||||||
mesh.c
|
mesh.c
|
||||||
quad.c
|
quad.c
|
||||||
|
cube.c
|
||||||
|
sphere.c
|
||||||
|
plane.c
|
||||||
|
capsule.c
|
||||||
|
triprism.c
|
||||||
)
|
)
|
||||||
@@ -0,0 +1,184 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "capsule.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
mesh_t CAPSULE_MESH_SIMPLE;
|
||||||
|
meshvertex_t CAPSULE_MESH_SIMPLE_VERTICES[CAPSULE_VERTEX_COUNT];
|
||||||
|
|
||||||
|
errorret_t capsuleInit() {
|
||||||
|
vec3 center = { 0.0f, 0.0f, 0.0f };
|
||||||
|
capsuleBuffer(
|
||||||
|
CAPSULE_MESH_SIMPLE_VERTICES,
|
||||||
|
center,
|
||||||
|
0.5f,
|
||||||
|
0.5f,
|
||||||
|
CAPSULE_CAP_RINGS,
|
||||||
|
CAPSULE_SECTORS
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, COLOR_WHITE_4B
|
||||||
|
#endif
|
||||||
|
);
|
||||||
|
errorChain(meshInit(
|
||||||
|
&CAPSULE_MESH_SIMPLE,
|
||||||
|
CAPSULE_PRIMITIVE_TYPE,
|
||||||
|
CAPSULE_VERTEX_COUNT,
|
||||||
|
CAPSULE_MESH_SIMPLE_VERTICES
|
||||||
|
));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
void capsuleBuffer(
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const vec3 center,
|
||||||
|
const float_t radius,
|
||||||
|
const float_t halfHeight,
|
||||||
|
const int32_t capRings,
|
||||||
|
const int32_t sectors
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
|
) {
|
||||||
|
assertNotNull(vertices, "Vertices cannot be NULL");
|
||||||
|
assertNotNull(center, "Center vector cannot be NULL");
|
||||||
|
|
||||||
|
const float_t cx = center[0];
|
||||||
|
const float_t cy = center[1];
|
||||||
|
const float_t cz = center[2];
|
||||||
|
const float_t sectorStep = 2.0f * (float_t)GLM_PI / (float_t)sectors;
|
||||||
|
int32_t vi = 0;
|
||||||
|
|
||||||
|
/* Helper macro: write one vertex. */
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
#define CAP_VERT(px, py, pz, u, v) \
|
||||||
|
vertices[vi].color = color; \
|
||||||
|
vertices[vi].pos[0] = (px); \
|
||||||
|
vertices[vi].pos[1] = (py); \
|
||||||
|
vertices[vi].pos[2] = (pz); \
|
||||||
|
vertices[vi].uv[0] = (u); \
|
||||||
|
vertices[vi].uv[1] = (v); \
|
||||||
|
vi++;
|
||||||
|
#else
|
||||||
|
#define CAP_VERT(px, py, pz, u, v) \
|
||||||
|
vertices[vi].pos[0] = (px); \
|
||||||
|
vertices[vi].pos[1] = (py); \
|
||||||
|
vertices[vi].pos[2] = (pz); \
|
||||||
|
vertices[vi].uv[0] = (u); \
|
||||||
|
vertices[vi].uv[1] = (v); \
|
||||||
|
vi++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ---- Top hemisphere ---- */
|
||||||
|
/* phi ranges from PI/2 (top pole) down to 0 (equator). */
|
||||||
|
const float_t capStep = (float_t)GLM_PI_2 / (float_t)capRings;
|
||||||
|
for(int32_t i = 0; i < capRings; i++) {
|
||||||
|
const float_t phi1 = (float_t)GLM_PI_2 - (float_t)i * capStep;
|
||||||
|
const float_t phi2 = (float_t)GLM_PI_2 - (float_t)(i + 1) * capStep;
|
||||||
|
|
||||||
|
const float_t ly1 = radius * sinf(phi1);
|
||||||
|
const float_t ly2 = radius * sinf(phi2);
|
||||||
|
const float_t lxz1 = radius * cosf(phi1);
|
||||||
|
const float_t lxz2 = radius * cosf(phi2);
|
||||||
|
|
||||||
|
/* UV: top cap occupies v in [0.5 + halfHeightFrac .. 1.0]: we use a
|
||||||
|
* simple per-band normalisation against the full height. */
|
||||||
|
const float_t v1 = 1.0f - (float_t)i / (float_t)(2 * capRings + 1);
|
||||||
|
const float_t v2 = 1.0f - (float_t)(i + 1) / (float_t)(2 * capRings + 1);
|
||||||
|
|
||||||
|
for(int32_t j = 0; j < sectors; j++) {
|
||||||
|
const float_t t1 = (float_t)j * sectorStep;
|
||||||
|
const float_t t2 = (float_t)(j + 1) * sectorStep;
|
||||||
|
|
||||||
|
const float_t u1 = (float_t)j / (float_t)sectors;
|
||||||
|
const float_t u2 = (float_t)(j + 1) / (float_t)sectors;
|
||||||
|
|
||||||
|
const float_t x11 = lxz1 * cosf(t1), z11 = lxz1 * sinf(t1);
|
||||||
|
const float_t x12 = lxz1 * cosf(t2), z12 = lxz1 * sinf(t2);
|
||||||
|
const float_t x21 = lxz2 * cosf(t1), z21 = lxz2 * sinf(t1);
|
||||||
|
const float_t x22 = lxz2 * cosf(t2), z22 = lxz2 * sinf(t2);
|
||||||
|
|
||||||
|
const float_t y1off = cy + halfHeight + ly1;
|
||||||
|
const float_t y2off = cy + halfHeight + ly2;
|
||||||
|
|
||||||
|
CAP_VERT(cx+x11, y1off, cz+z11, u1, v1)
|
||||||
|
CAP_VERT(cx+x21, y2off, cz+z21, u1, v2)
|
||||||
|
CAP_VERT(cx+x12, y1off, cz+z12, u2, v1)
|
||||||
|
|
||||||
|
CAP_VERT(cx+x12, y1off, cz+z12, u2, v1)
|
||||||
|
CAP_VERT(cx+x21, y2off, cz+z21, u1, v2)
|
||||||
|
CAP_VERT(cx+x22, y2off, cz+z22, u2, v2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---- Cylindrical body ---- */
|
||||||
|
{
|
||||||
|
const float_t yTop = cy + halfHeight;
|
||||||
|
const float_t yBot = cy - halfHeight;
|
||||||
|
const float_t vTop = 1.0f - (float_t)capRings / (float_t)(2 * capRings + 1);
|
||||||
|
const float_t vBot = 1.0f - (float_t)(capRings + 1) / (float_t)(2 * capRings + 1);
|
||||||
|
|
||||||
|
for(int32_t j = 0; j < sectors; j++) {
|
||||||
|
const float_t t1 = (float_t)j * sectorStep;
|
||||||
|
const float_t t2 = (float_t)(j + 1) * sectorStep;
|
||||||
|
|
||||||
|
const float_t u1 = (float_t)j / (float_t)sectors;
|
||||||
|
const float_t u2 = (float_t)(j + 1) / (float_t)sectors;
|
||||||
|
|
||||||
|
const float_t x1 = radius * cosf(t1), z1 = radius * sinf(t1);
|
||||||
|
const float_t x2 = radius * cosf(t2), z2 = radius * sinf(t2);
|
||||||
|
|
||||||
|
CAP_VERT(cx+x1, yTop, cz+z1, u1, vTop)
|
||||||
|
CAP_VERT(cx+x1, yBot, cz+z1, u1, vBot)
|
||||||
|
CAP_VERT(cx+x2, yTop, cz+z2, u2, vTop)
|
||||||
|
|
||||||
|
CAP_VERT(cx+x2, yTop, cz+z2, u2, vTop)
|
||||||
|
CAP_VERT(cx+x1, yBot, cz+z1, u1, vBot)
|
||||||
|
CAP_VERT(cx+x2, yBot, cz+z2, u2, vBot)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bottom hemisphere
|
||||||
|
for(int32_t i = 0; i < capRings; i++) {
|
||||||
|
const float_t phi1 = -(float_t)i * capStep;
|
||||||
|
const float_t phi2 = -(float_t)(i + 1) * capStep;
|
||||||
|
|
||||||
|
const float_t ly1 = radius * sinf(phi1);
|
||||||
|
const float_t ly2 = radius * sinf(phi2);
|
||||||
|
const float_t lxz1 = radius * cosf(phi1);
|
||||||
|
const float_t lxz2 = radius * cosf(phi2);
|
||||||
|
|
||||||
|
const float_t v1 = 1.0f - (float_t)(capRings + 1 + i) / (float_t)(2 * capRings + 1);
|
||||||
|
const float_t v2 = 1.0f - (float_t)(capRings + 1 + i + 1) / (float_t)(2 * capRings + 1);
|
||||||
|
|
||||||
|
for(int32_t j = 0; j < sectors; j++) {
|
||||||
|
const float_t t1 = (float_t)j * sectorStep;
|
||||||
|
const float_t t2 = (float_t)(j + 1) * sectorStep;
|
||||||
|
|
||||||
|
const float_t u1 = (float_t)j / (float_t)sectors;
|
||||||
|
const float_t u2 = (float_t)(j + 1) / (float_t)sectors;
|
||||||
|
|
||||||
|
const float_t x11 = lxz1 * cosf(t1), z11 = lxz1 * sinf(t1);
|
||||||
|
const float_t x12 = lxz1 * cosf(t2), z12 = lxz1 * sinf(t2);
|
||||||
|
const float_t x21 = lxz2 * cosf(t1), z21 = lxz2 * sinf(t1);
|
||||||
|
const float_t x22 = lxz2 * cosf(t2), z22 = lxz2 * sinf(t2);
|
||||||
|
|
||||||
|
const float_t y1off = cy - halfHeight + ly1;
|
||||||
|
const float_t y2off = cy - halfHeight + ly2;
|
||||||
|
|
||||||
|
CAP_VERT(cx+x11, y1off, cz+z11, u1, v1)
|
||||||
|
CAP_VERT(cx+x21, y2off, cz+z21, u1, v2)
|
||||||
|
CAP_VERT(cx+x12, y1off, cz+z12, u2, v1)
|
||||||
|
|
||||||
|
CAP_VERT(cx+x12, y1off, cz+z12, u2, v1)
|
||||||
|
CAP_VERT(cx+x21, y2off, cz+z21, u1, v2)
|
||||||
|
CAP_VERT(cx+x22, y2off, cz+z22, u2, v2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef CAP_VERT
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/mesh/mesh.h"
|
||||||
|
#include "display/color.h"
|
||||||
|
|
||||||
|
#define CAPSULE_CAP_RINGS 4
|
||||||
|
#define CAPSULE_SECTORS 16
|
||||||
|
#define CAPSULE_VERTEX_COUNT ((2 * CAPSULE_CAP_RINGS + 1) * CAPSULE_SECTORS * 6)
|
||||||
|
#define CAPSULE_PRIMITIVE_TYPE MESH_PRIMITIVE_TYPE_TRIANGLES
|
||||||
|
|
||||||
|
extern mesh_t CAPSULE_MESH_SIMPLE;
|
||||||
|
extern meshvertex_t CAPSULE_MESH_SIMPLE_VERTICES[CAPSULE_VERTEX_COUNT];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the simple unit capsule mesh centered at (0,0,0) with radius 0.5
|
||||||
|
* and a cylindrical half-height of 0.5 (total height 2.0).
|
||||||
|
*
|
||||||
|
* @return Error for initialization of the capsule mesh.
|
||||||
|
*/
|
||||||
|
errorret_t capsuleInit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buffers a capsule (cylinder + two hemisphere caps) into the provided vertex
|
||||||
|
* array. The capsule's long axis is Y. Total vertex count is
|
||||||
|
* (2*capRings + 1) * sectors * 6.
|
||||||
|
*
|
||||||
|
* @param vertices Vertex array to write into.
|
||||||
|
* @param center Center position of the capsule.
|
||||||
|
* @param radius Radius of the cylinder and hemisphere caps.
|
||||||
|
* @param halfHeight Half the height of the cylindrical section only (caps
|
||||||
|
* extend an additional radius above/below).
|
||||||
|
* @param capRings Number of latitude rings per hemisphere cap.
|
||||||
|
* @param sectors Number of longitude segments around the circumference.
|
||||||
|
* @param color Color applied to all vertices.
|
||||||
|
*/
|
||||||
|
void capsuleBuffer(
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const vec3 center,
|
||||||
|
const float_t radius,
|
||||||
|
const float_t halfHeight,
|
||||||
|
const int32_t capRings,
|
||||||
|
const int32_t sectors
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
|
);
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "cube.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
mesh_t CUBE_MESH_SIMPLE;
|
||||||
|
meshvertex_t CUBE_MESH_SIMPLE_VERTICES[CUBE_VERTEX_COUNT];
|
||||||
|
|
||||||
|
errorret_t cubeInit() {
|
||||||
|
vec3 min = { 0.0f, 0.0f, 0.0f };
|
||||||
|
vec3 max = { 1.0f, 1.0f, 1.0f };
|
||||||
|
cubeBuffer(
|
||||||
|
CUBE_MESH_SIMPLE_VERTICES, min, max
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, COLOR_WHITE_4B
|
||||||
|
#endif
|
||||||
|
);
|
||||||
|
errorChain(meshInit(
|
||||||
|
&CUBE_MESH_SIMPLE,
|
||||||
|
CUBE_PRIMITIVE_TYPE,
|
||||||
|
CUBE_VERTEX_COUNT,
|
||||||
|
CUBE_MESH_SIMPLE_VERTICES
|
||||||
|
));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper macro: set one vertex position, UV and color.
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
#define CUBE_VERT(i, px, py, pz, u, v) \
|
||||||
|
vertices[i].color = color; \
|
||||||
|
vertices[i].pos[0] = (px); \
|
||||||
|
vertices[i].pos[1] = (py); \
|
||||||
|
vertices[i].pos[2] = (pz); \
|
||||||
|
vertices[i].uv[0] = (u); \
|
||||||
|
vertices[i].uv[1] = (v);
|
||||||
|
#else
|
||||||
|
#define CUBE_VERT(i, px, py, pz, u, v) \
|
||||||
|
vertices[i].pos[0] = (px); \
|
||||||
|
vertices[i].pos[1] = (py); \
|
||||||
|
vertices[i].pos[2] = (pz); \
|
||||||
|
vertices[i].uv[0] = (u); \
|
||||||
|
vertices[i].uv[1] = (v);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void cubeBuffer(
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const vec3 min,
|
||||||
|
const vec3 max
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
|
) {
|
||||||
|
assertNotNull(vertices, "Vertices cannot be NULL");
|
||||||
|
assertNotNull(min, "Min vector cannot be NULL");
|
||||||
|
assertNotNull(max, "Max vector cannot be NULL");
|
||||||
|
|
||||||
|
const float_t x0 = min[0], y0 = min[1], z0 = min[2];
|
||||||
|
const float_t x1 = max[0], y1 = max[1], z1 = max[2];
|
||||||
|
|
||||||
|
// Front face (+Z normal): CCW when viewed from +Z
|
||||||
|
CUBE_VERT( 0, x0, y0, z1, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT( 1, x1, y0, z1, 1.0f, 0.0f);
|
||||||
|
CUBE_VERT( 2, x1, y1, z1, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT( 3, x0, y0, z1, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT( 4, x1, y1, z1, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT( 5, x0, y1, z1, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// Back face (-Z normal): CCW when viewed from -Z
|
||||||
|
CUBE_VERT( 6, x1, y0, z0, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT( 7, x0, y0, z0, 1.0f, 0.0f);
|
||||||
|
CUBE_VERT( 8, x0, y1, z0, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT( 9, x1, y0, z0, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT(10, x0, y1, z0, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT(11, x1, y1, z0, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// Right face (+X normal): CCW when viewed from +X
|
||||||
|
CUBE_VERT(12, x1, y0, z1, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT(13, x1, y0, z0, 1.0f, 0.0f);
|
||||||
|
CUBE_VERT(14, x1, y1, z0, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT(15, x1, y0, z1, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT(16, x1, y1, z0, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT(17, x1, y1, z1, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// Left face (-X normal): CCW when viewed from -X
|
||||||
|
CUBE_VERT(18, x0, y0, z0, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT(19, x0, y0, z1, 1.0f, 0.0f);
|
||||||
|
CUBE_VERT(20, x0, y1, z1, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT(21, x0, y0, z0, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT(22, x0, y1, z1, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT(23, x0, y1, z0, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// Top face (+Y normal): CCW when viewed from +Y
|
||||||
|
CUBE_VERT(24, x0, y1, z1, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT(25, x1, y1, z1, 1.0f, 0.0f);
|
||||||
|
CUBE_VERT(26, x1, y1, z0, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT(27, x0, y1, z1, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT(28, x1, y1, z0, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT(29, x0, y1, z0, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// Bottom face (-Y normal): CCW when viewed from -Y
|
||||||
|
CUBE_VERT(30, x0, y0, z0, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT(31, x1, y0, z0, 1.0f, 0.0f);
|
||||||
|
CUBE_VERT(32, x1, y0, z1, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT(33, x0, y0, z0, 0.0f, 0.0f);
|
||||||
|
CUBE_VERT(34, x1, y0, z1, 1.0f, 1.0f);
|
||||||
|
CUBE_VERT(35, x0, y0, z1, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
#undef CUBE_VERT
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/mesh/mesh.h"
|
||||||
|
#include "display/color.h"
|
||||||
|
|
||||||
|
#define CUBE_FACE_COUNT 6
|
||||||
|
#define CUBE_VERTICES_PER_FACE 6
|
||||||
|
#define CUBE_VERTEX_COUNT (CUBE_FACE_COUNT * CUBE_VERTICES_PER_FACE)
|
||||||
|
#define CUBE_PRIMITIVE_TYPE MESH_PRIMITIVE_TYPE_TRIANGLES
|
||||||
|
|
||||||
|
extern mesh_t CUBE_MESH_SIMPLE;
|
||||||
|
extern meshvertex_t CUBE_MESH_SIMPLE_VERTICES[CUBE_VERTEX_COUNT];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the simple unit cube mesh (0,0,0) to (1,1,1).
|
||||||
|
*
|
||||||
|
* @return Error for initialization of the cube mesh.
|
||||||
|
*/
|
||||||
|
errorret_t cubeInit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buffers a 3D axis-aligned cube into the provided vertex array.
|
||||||
|
* Writes CUBE_VERTEX_COUNT vertices (6 faces x 6 vertices, CCW winding).
|
||||||
|
*
|
||||||
|
* @param vertices The vertex array to buffer into (must hold CUBE_VERTEX_COUNT).
|
||||||
|
* @param min The minimum XYZ corner of the cube.
|
||||||
|
* @param max The maximum XYZ corner of the cube.
|
||||||
|
* @param color The color applied to all vertices.
|
||||||
|
*/
|
||||||
|
void cubeBuffer(
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const vec3 min,
|
||||||
|
const vec3 max
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
|
);
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
#include "mesh.h"
|
#include "mesh.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
#include "util/math.h"
|
||||||
|
|
||||||
errorret_t meshInit(
|
errorret_t meshInit(
|
||||||
mesh_t *mesh,
|
mesh_t *mesh,
|
||||||
@@ -75,6 +76,29 @@ errorret_t meshDraw(
|
|||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void meshGetBounds(
|
||||||
|
const mesh_t *mesh,
|
||||||
|
vec3 outMin,
|
||||||
|
vec3 outMax
|
||||||
|
) {
|
||||||
|
assertNotNull(mesh, "Mesh cannot be NULL");
|
||||||
|
assertNotNull(outMin, "Output min cannot be NULL");
|
||||||
|
assertNotNull(outMax, "Output max cannot be NULL");
|
||||||
|
|
||||||
|
for(int i = 0; i < 3; i++) {
|
||||||
|
outMin[i] = FLT_MAX;
|
||||||
|
outMax[i] = -FLT_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(uint32_t i = 0; i < mesh->vertexCount; i++) {
|
||||||
|
meshvertex_t vert = mesh->vertices[i];
|
||||||
|
for(int j = 0; j < 3; j++) {
|
||||||
|
outMin[j] = mathMin(outMin[j], vert.pos[j]);
|
||||||
|
outMax[j] = mathMax(outMax[j], vert.pos[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int32_t meshGetVertexCount(const mesh_t *mesh) {
|
int32_t meshGetVertexCount(const mesh_t *mesh) {
|
||||||
assertNotNull(mesh, "Mesh cannot be NULL");
|
assertNotNull(mesh, "Mesh cannot be NULL");
|
||||||
return meshGetVertexCountPlatform(mesh);
|
return meshGetVertexCountPlatform(mesh);
|
||||||
|
|||||||
@@ -70,6 +70,19 @@ errorret_t meshDraw(
|
|||||||
const int32_t vertexCount
|
const int32_t vertexCount
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the axis-aligned bounding box of a mesh.
|
||||||
|
*
|
||||||
|
* @param mesh The mesh to get the bounds of.
|
||||||
|
* @param outMin Output parameter for the minimum corner of the bounding box.
|
||||||
|
* @param outMax Output parameter for the maximum corner of the bounding box.
|
||||||
|
*/
|
||||||
|
void meshGetBounds(
|
||||||
|
const mesh_t *mesh,
|
||||||
|
vec3 outMin,
|
||||||
|
vec3 outMax
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the vertex count of a mesh.
|
* Gets the vertex count of a mesh.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -9,11 +9,18 @@
|
|||||||
#include "dusk.h"
|
#include "dusk.h"
|
||||||
#include "display/color.h"
|
#include "display/color.h"
|
||||||
|
|
||||||
|
#ifndef MESH_ENABLE_COLOR
|
||||||
|
#define MESH_ENABLE_COLOR 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#define MESH_VERTEX_UV_SIZE 2
|
#define MESH_VERTEX_UV_SIZE 2
|
||||||
#define MESH_VERTEX_POS_SIZE 3
|
#define MESH_VERTEX_POS_SIZE 3
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
color_t color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
color_t color;
|
||||||
|
#endif
|
||||||
|
|
||||||
float_t uv[MESH_VERTEX_UV_SIZE];
|
float_t uv[MESH_VERTEX_UV_SIZE];
|
||||||
float_t pos[MESH_VERTEX_POS_SIZE];
|
float_t pos[MESH_VERTEX_POS_SIZE];
|
||||||
} meshvertex_t;
|
} meshvertex_t;
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "plane.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
mesh_t PLANE_MESH_SIMPLE;
|
||||||
|
meshvertex_t PLANE_MESH_SIMPLE_VERTICES[PLANE_VERTEX_COUNT];
|
||||||
|
|
||||||
|
errorret_t planeInit() {
|
||||||
|
vec3 min = { 0.0f, 0.0f, 0.0f };
|
||||||
|
vec3 max = { 1.0f, 0.0f, 1.0f };
|
||||||
|
vec2 uvMin = { 0.0f, 0.0f };
|
||||||
|
vec2 uvMax = { 1.0f, 1.0f };
|
||||||
|
planeBuffer(
|
||||||
|
PLANE_MESH_SIMPLE_VERTICES,
|
||||||
|
PLANE_AXIS_XZ,
|
||||||
|
min,
|
||||||
|
max
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, COLOR_WHITE_4B
|
||||||
|
#endif
|
||||||
|
, uvMin,
|
||||||
|
uvMax
|
||||||
|
);
|
||||||
|
errorChain(meshInit(
|
||||||
|
&PLANE_MESH_SIMPLE,
|
||||||
|
PLANE_PRIMITIVE_TYPE,
|
||||||
|
PLANE_VERTEX_COUNT,
|
||||||
|
PLANE_MESH_SIMPLE_VERTICES
|
||||||
|
));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Helper macro to write one vertex. */
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
#define PLANE_VERT(i, px, py, pz, u, v) \
|
||||||
|
vertices[i].color = color; \
|
||||||
|
vertices[i].pos[0] = (px); \
|
||||||
|
vertices[i].pos[1] = (py); \
|
||||||
|
vertices[i].pos[2] = (pz); \
|
||||||
|
vertices[i].uv[0] = (u); \
|
||||||
|
vertices[i].uv[1] = (v);
|
||||||
|
#else
|
||||||
|
#define PLANE_VERT(i, px, py, pz, u, v) \
|
||||||
|
vertices[i].pos[0] = (px); \
|
||||||
|
vertices[i].pos[1] = (py); \
|
||||||
|
vertices[i].pos[2] = (pz); \
|
||||||
|
vertices[i].uv[0] = (u); \
|
||||||
|
vertices[i].uv[1] = (v);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void planeBuffer(
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const planeaxis_t axis,
|
||||||
|
const vec3 min,
|
||||||
|
const vec3 max
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
|
, const vec2 uvMin,
|
||||||
|
const vec2 uvMax
|
||||||
|
) {
|
||||||
|
assertNotNull(vertices, "Vertices cannot be NULL");
|
||||||
|
assertNotNull(min, "Min vector cannot be NULL");
|
||||||
|
assertNotNull(max, "Max vector cannot be NULL");
|
||||||
|
assertNotNull(uvMin, "uvMin cannot be NULL");
|
||||||
|
assertNotNull(uvMax, "uvMax cannot be NULL");
|
||||||
|
|
||||||
|
const float_t u0 = uvMin[0], u1 = uvMax[0];
|
||||||
|
const float_t v0 = uvMin[1], v1 = uvMax[1];
|
||||||
|
|
||||||
|
switch (axis) {
|
||||||
|
case PLANE_AXIS_XY: {
|
||||||
|
/* Flat in XY at z = min[2]; spans X and Y. */
|
||||||
|
const float_t z = min[2];
|
||||||
|
PLANE_VERT(0, min[0], min[1], z, u0, v0)
|
||||||
|
PLANE_VERT(1, max[0], min[1], z, u1, v0)
|
||||||
|
PLANE_VERT(2, max[0], max[1], z, u1, v1)
|
||||||
|
PLANE_VERT(3, min[0], min[1], z, u0, v0)
|
||||||
|
PLANE_VERT(4, max[0], max[1], z, u1, v1)
|
||||||
|
PLANE_VERT(5, min[0], max[1], z, u0, v1)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PLANE_AXIS_XZ: {
|
||||||
|
/* Flat in XZ at y = min[1]; spans X and Z. */
|
||||||
|
const float_t y = min[1];
|
||||||
|
PLANE_VERT(0, min[0], y, min[2], u0, v0)
|
||||||
|
PLANE_VERT(1, max[0], y, min[2], u1, v0)
|
||||||
|
PLANE_VERT(2, max[0], y, max[2], u1, v1)
|
||||||
|
PLANE_VERT(3, min[0], y, min[2], u0, v0)
|
||||||
|
PLANE_VERT(4, max[0], y, max[2], u1, v1)
|
||||||
|
PLANE_VERT(5, min[0], y, max[2], u0, v1)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case PLANE_AXIS_YZ: {
|
||||||
|
/* Flat in YZ at x = min[0]; spans Y and Z. */
|
||||||
|
const float_t x = min[0];
|
||||||
|
PLANE_VERT(0, x, min[1], min[2], u0, v0)
|
||||||
|
PLANE_VERT(1, x, max[1], min[2], u1, v0)
|
||||||
|
PLANE_VERT(2, x, max[1], max[2], u1, v1)
|
||||||
|
PLANE_VERT(3, x, min[1], min[2], u0, v0)
|
||||||
|
PLANE_VERT(4, x, max[1], max[2], u1, v1)
|
||||||
|
PLANE_VERT(5, x, min[1], max[2], u0, v1)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef PLANE_VERT
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/mesh/mesh.h"
|
||||||
|
#include "display/color.h"
|
||||||
|
|
||||||
|
#define PLANE_VERTEX_COUNT 6
|
||||||
|
#define PLANE_PRIMITIVE_TYPE MESH_PRIMITIVE_TYPE_TRIANGLES
|
||||||
|
|
||||||
|
/** Which axis the plane's normal points along. */
|
||||||
|
typedef enum {
|
||||||
|
PLANE_AXIS_XY = 0, /**< Flat in XY, normal along +Z (billboard/wall face). */
|
||||||
|
PLANE_AXIS_XZ = 1, /**< Flat in XZ, normal along +Y (ground/floor plane). */
|
||||||
|
PLANE_AXIS_YZ = 2, /**< Flat in YZ, normal along +X (side wall). */
|
||||||
|
} planeaxis_t;
|
||||||
|
|
||||||
|
extern mesh_t PLANE_MESH_SIMPLE;
|
||||||
|
extern meshvertex_t PLANE_MESH_SIMPLE_VERTICES[PLANE_VERTEX_COUNT];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the simple unit XZ plane mesh (ground plane) from (0,0,0) to
|
||||||
|
* (1,0,1).
|
||||||
|
*
|
||||||
|
* @return Error for initialization of the plane mesh.
|
||||||
|
*/
|
||||||
|
errorret_t planeInit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buffers an axis-aligned plane into the provided vertex array.
|
||||||
|
* Writes PLANE_VERTEX_COUNT (6) vertices (two triangles, CCW winding).
|
||||||
|
*
|
||||||
|
* The min/max corners fully describe the plane in 3D space. The axis enum
|
||||||
|
* controls which dimension is treated as the "depth" (normal) axis:
|
||||||
|
* PLANE_AXIS_XY: spans X and Y, depth from min[2]/max[2] (uses min[2])
|
||||||
|
* PLANE_AXIS_XZ: spans X and Z, depth from min[1]/max[1] (uses min[1])
|
||||||
|
* PLANE_AXIS_YZ: spans Y and Z, depth from min[0]/max[0] (uses min[0])
|
||||||
|
*
|
||||||
|
* @param vertices Vertex array to write into (must hold PLANE_VERTEX_COUNT).
|
||||||
|
* @param axis Which axis the plane's normal points along.
|
||||||
|
* @param min Minimum XYZ corner.
|
||||||
|
* @param max Maximum XYZ corner.
|
||||||
|
* @param color Color applied to all vertices.
|
||||||
|
* @param uvMin Minimum UV coordinates.
|
||||||
|
* @param uvMax Maximum UV coordinates.
|
||||||
|
*/
|
||||||
|
void planeBuffer(
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const planeaxis_t axis,
|
||||||
|
const vec3 min,
|
||||||
|
const vec3 max
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
|
, const vec2 uvMin,
|
||||||
|
const vec2 uvMax
|
||||||
|
);
|
||||||
@@ -10,13 +10,55 @@
|
|||||||
|
|
||||||
mesh_t QUAD_MESH_SIMPLE;
|
mesh_t QUAD_MESH_SIMPLE;
|
||||||
meshvertex_t QUAD_MESH_SIMPLE_VERTICES[QUAD_VERTEX_COUNT] = {
|
meshvertex_t QUAD_MESH_SIMPLE_VERTICES[QUAD_VERTEX_COUNT] = {
|
||||||
{ .color = COLOR_WHITE_4B, .uv = { 0.0f, 0.0f }, .pos = { 0.0f, 0.0f, 0.0f } },
|
{
|
||||||
{ .color = COLOR_WHITE_4B, .uv = { 1.0f, 0.0f }, .pos = { 1.0f, 0.0f, 0.0f } },
|
#if MESH_ENABLE_COLOR
|
||||||
{ .color = COLOR_WHITE_4B, .uv = { 1.0f, 1.0f }, .pos = { 1.0f, 1.0f, 0.0f } },
|
.color = COLOR_WHITE_4B,
|
||||||
|
#endif
|
||||||
|
|
||||||
{ .color = COLOR_WHITE_4B, .uv = { 0.0f, 0.0f }, .pos = { 0.0f, 0.0f, 0.0f } },
|
.uv = { 0.0f, 0.0f },
|
||||||
{ .color = COLOR_WHITE_4B, .uv = { 1.0f, 1.0f }, .pos = { 1.0f, 1.0f, 0.0f } },
|
.pos = { 0.0f, 0.0f, 0.0f }
|
||||||
{ .color = COLOR_WHITE_4B, .uv = { 0.0f, 1.0f }, .pos = { 0.0f, 1.0f, 0.0f } }
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
.color = COLOR_WHITE_4B,
|
||||||
|
#endif
|
||||||
|
.uv = { 1.0f, 0.0f },
|
||||||
|
.pos = { 1.0f, 0.0f, 0.0f }
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
.color = COLOR_WHITE_4B,
|
||||||
|
#endif
|
||||||
|
.uv = { 1.0f, 1.0f },
|
||||||
|
.pos = { 1.0f, 1.0f, 0.0f }
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
.color = COLOR_WHITE_4B,
|
||||||
|
#endif
|
||||||
|
.uv = { 0.0f, 0.0f },
|
||||||
|
.pos = { 0.0f, 0.0f, 0.0f }
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
.color = COLOR_WHITE_4B,
|
||||||
|
#endif
|
||||||
|
.uv = { 1.0f, 1.0f },
|
||||||
|
.pos = { 1.0f, 1.0f, 0.0f }
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
.color = COLOR_WHITE_4B,
|
||||||
|
#endif
|
||||||
|
.uv = { 0.0f, 1.0f },
|
||||||
|
.pos = { 0.0f, 1.0f, 0.0f }
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
errorret_t quadInit() {
|
errorret_t quadInit() {
|
||||||
@@ -35,31 +77,39 @@ void quadBuffer(
|
|||||||
const float_t minY,
|
const float_t minY,
|
||||||
const float_t maxX,
|
const float_t maxX,
|
||||||
const float_t maxY,
|
const float_t maxY,
|
||||||
const color_t color,
|
|
||||||
const float_t u0,
|
const float_t u0,
|
||||||
const float_t v0,
|
const float_t v0,
|
||||||
const float_t u1,
|
const float_t u1,
|
||||||
const float_t v1
|
const float_t v1
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
) {
|
) {
|
||||||
const float_t z = 0.0f; // Z coordinate for 2D rendering
|
const float_t z = 0.0f; // Z coordinate for 2D rendering
|
||||||
assertNotNull(vertices, "Vertices cannot be NULL");
|
assertNotNull(vertices, "Vertices cannot be NULL");
|
||||||
|
|
||||||
// First triangle
|
// First triangle
|
||||||
vertices[0].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[0].color = color;
|
||||||
|
#endif
|
||||||
vertices[0].uv[0] = u0;
|
vertices[0].uv[0] = u0;
|
||||||
vertices[0].uv[1] = v1;
|
vertices[0].uv[1] = v1;
|
||||||
vertices[0].pos[0] = minX;
|
vertices[0].pos[0] = minX;
|
||||||
vertices[0].pos[1] = maxY;
|
vertices[0].pos[1] = maxY;
|
||||||
vertices[0].pos[2] = z;
|
vertices[0].pos[2] = z;
|
||||||
|
|
||||||
vertices[1].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[1].color = color;
|
||||||
|
#endif
|
||||||
vertices[1].uv[0] = u1;
|
vertices[1].uv[0] = u1;
|
||||||
vertices[1].uv[1] = v0;
|
vertices[1].uv[1] = v0;
|
||||||
vertices[1].pos[0] = maxX;
|
vertices[1].pos[0] = maxX;
|
||||||
vertices[1].pos[1] = minY;
|
vertices[1].pos[1] = minY;
|
||||||
vertices[1].pos[2] = z;
|
vertices[1].pos[2] = z;
|
||||||
|
|
||||||
vertices[2].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[2].color = color;
|
||||||
|
#endif
|
||||||
vertices[2].uv[0] = u0;
|
vertices[2].uv[0] = u0;
|
||||||
vertices[2].uv[1] = v0;
|
vertices[2].uv[1] = v0;
|
||||||
vertices[2].pos[0] = minX;
|
vertices[2].pos[0] = minX;
|
||||||
@@ -67,21 +117,27 @@ void quadBuffer(
|
|||||||
vertices[2].pos[2] = z;
|
vertices[2].pos[2] = z;
|
||||||
|
|
||||||
// Second triangle
|
// Second triangle
|
||||||
vertices[3].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[3].color = color;
|
||||||
|
#endif
|
||||||
vertices[3].uv[0] = u0;
|
vertices[3].uv[0] = u0;
|
||||||
vertices[3].uv[1] = v1;
|
vertices[3].uv[1] = v1;
|
||||||
vertices[3].pos[0] = minX;
|
vertices[3].pos[0] = minX;
|
||||||
vertices[3].pos[1] = maxY;
|
vertices[3].pos[1] = maxY;
|
||||||
vertices[3].pos[2] = z;
|
vertices[3].pos[2] = z;
|
||||||
|
|
||||||
vertices[4].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[4].color = color;
|
||||||
|
#endif
|
||||||
vertices[4].uv[0] = u1;
|
vertices[4].uv[0] = u1;
|
||||||
vertices[4].uv[1] = v1;
|
vertices[4].uv[1] = v1;
|
||||||
vertices[4].pos[0] = maxX;
|
vertices[4].pos[0] = maxX;
|
||||||
vertices[4].pos[1] = maxY;
|
vertices[4].pos[1] = maxY;
|
||||||
vertices[4].pos[2] = z;
|
vertices[4].pos[2] = z;
|
||||||
|
|
||||||
vertices[5].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[5].color = color;
|
||||||
|
#endif
|
||||||
vertices[5].uv[0] = u1;
|
vertices[5].uv[0] = u1;
|
||||||
vertices[5].uv[1] = v0;
|
vertices[5].uv[1] = v0;
|
||||||
vertices[5].pos[0] = maxX;
|
vertices[5].pos[0] = maxX;
|
||||||
@@ -93,9 +149,11 @@ void quadBuffer3D(
|
|||||||
meshvertex_t *vertices,
|
meshvertex_t *vertices,
|
||||||
const vec3 min,
|
const vec3 min,
|
||||||
const vec3 max,
|
const vec3 max,
|
||||||
const color_t color,
|
|
||||||
const vec2 uvMin,
|
const vec2 uvMin,
|
||||||
const vec2 uvMax
|
const vec2 uvMax
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
) {
|
) {
|
||||||
assertNotNull(vertices, "Vertices cannot be NULL");
|
assertNotNull(vertices, "Vertices cannot be NULL");
|
||||||
assertNotNull(min, "Min vector cannot be NULL");
|
assertNotNull(min, "Min vector cannot be NULL");
|
||||||
@@ -104,21 +162,27 @@ void quadBuffer3D(
|
|||||||
assertNotNull(uvMax, "UV Max vector cannot be NULL");
|
assertNotNull(uvMax, "UV Max vector cannot be NULL");
|
||||||
|
|
||||||
// First triangle
|
// First triangle
|
||||||
vertices[0].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[0].color = color;
|
||||||
|
#endif
|
||||||
vertices[0].uv[0] = uvMin[0];
|
vertices[0].uv[0] = uvMin[0];
|
||||||
vertices[0].uv[1] = uvMin[1];
|
vertices[0].uv[1] = uvMin[1];
|
||||||
vertices[0].pos[0] = min[0];
|
vertices[0].pos[0] = min[0];
|
||||||
vertices[0].pos[1] = min[1];
|
vertices[0].pos[1] = min[1];
|
||||||
vertices[0].pos[2] = min[2];
|
vertices[0].pos[2] = min[2];
|
||||||
|
|
||||||
vertices[1].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[1].color = color;
|
||||||
|
#endif
|
||||||
vertices[1].uv[0] = uvMax[0];
|
vertices[1].uv[0] = uvMax[0];
|
||||||
vertices[1].uv[1] = uvMin[1];
|
vertices[1].uv[1] = uvMin[1];
|
||||||
vertices[1].pos[0] = max[0];
|
vertices[1].pos[0] = max[0];
|
||||||
vertices[1].pos[1] = min[1];
|
vertices[1].pos[1] = min[1];
|
||||||
vertices[1].pos[2] = min[2];
|
vertices[1].pos[2] = min[2];
|
||||||
|
|
||||||
vertices[2].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[2].color = color;
|
||||||
|
#endif
|
||||||
vertices[2].uv[0] = uvMax[0];
|
vertices[2].uv[0] = uvMax[0];
|
||||||
vertices[2].uv[1] = uvMax[1];
|
vertices[2].uv[1] = uvMax[1];
|
||||||
vertices[2].pos[0] = max[0];
|
vertices[2].pos[0] = max[0];
|
||||||
@@ -126,21 +190,27 @@ void quadBuffer3D(
|
|||||||
vertices[2].pos[2] = min[2];
|
vertices[2].pos[2] = min[2];
|
||||||
|
|
||||||
// Second triangle
|
// Second triangle
|
||||||
vertices[3].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[3].color = color;
|
||||||
|
#endif
|
||||||
vertices[3].uv[0] = uvMin[0];
|
vertices[3].uv[0] = uvMin[0];
|
||||||
vertices[3].uv[1] = uvMin[1];
|
vertices[3].uv[1] = uvMin[1];
|
||||||
vertices[3].pos[0] = min[0];
|
vertices[3].pos[0] = min[0];
|
||||||
vertices[3].pos[1] = min[1];
|
vertices[3].pos[1] = min[1];
|
||||||
vertices[3].pos[2] = min[2];
|
vertices[3].pos[2] = min[2];
|
||||||
|
|
||||||
vertices[4].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[4].color = color;
|
||||||
|
#endif
|
||||||
vertices[4].uv[0] = uvMax[0];
|
vertices[4].uv[0] = uvMax[0];
|
||||||
vertices[4].uv[1] = uvMax[1];
|
vertices[4].uv[1] = uvMax[1];
|
||||||
vertices[4].pos[0] = max[0];
|
vertices[4].pos[0] = max[0];
|
||||||
vertices[4].pos[1] = max[1];
|
vertices[4].pos[1] = max[1];
|
||||||
vertices[4].pos[2] = min[2];
|
vertices[4].pos[2] = min[2];
|
||||||
|
|
||||||
vertices[5].color = color;
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[5].color = color;
|
||||||
|
#endif
|
||||||
vertices[5].uv[0] = uvMin[0];
|
vertices[5].uv[0] = uvMin[0];
|
||||||
vertices[5].uv[1] = uvMax[1];
|
vertices[5].uv[1] = uvMax[1];
|
||||||
vertices[5].pos[0] = min[0];
|
vertices[5].pos[0] = min[0];
|
||||||
|
|||||||
@@ -42,11 +42,13 @@ void quadBuffer(
|
|||||||
const float_t minY,
|
const float_t minY,
|
||||||
const float_t maxX,
|
const float_t maxX,
|
||||||
const float_t maxY,
|
const float_t maxY,
|
||||||
const color_t color,
|
|
||||||
const float_t u0,
|
const float_t u0,
|
||||||
const float_t v0,
|
const float_t v0,
|
||||||
const float_t u1,
|
const float_t u1,
|
||||||
const float_t v1
|
const float_t v1
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,7 +65,9 @@ void quadBuffer3D(
|
|||||||
meshvertex_t *vertices,
|
meshvertex_t *vertices,
|
||||||
const vec3 min,
|
const vec3 min,
|
||||||
const vec3 max,
|
const vec3 max,
|
||||||
const color_t color,
|
|
||||||
const vec2 uvMin,
|
const vec2 uvMin,
|
||||||
const vec2 uvMax
|
const vec2 uvMax
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
);
|
);
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "sphere.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
mesh_t SPHERE_MESH_SIMPLE;
|
||||||
|
meshvertex_t SPHERE_MESH_SIMPLE_VERTICES[SPHERE_VERTEX_COUNT];
|
||||||
|
|
||||||
|
errorret_t sphereInit() {
|
||||||
|
vec3 center = { 0.0f, 0.0f, 0.0f };
|
||||||
|
sphereBuffer(
|
||||||
|
SPHERE_MESH_SIMPLE_VERTICES,
|
||||||
|
center,
|
||||||
|
0.5f,
|
||||||
|
SPHERE_STACKS,
|
||||||
|
SPHERE_SECTORS
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, COLOR_WHITE_4B
|
||||||
|
#endif
|
||||||
|
);
|
||||||
|
errorChain(meshInit(
|
||||||
|
&SPHERE_MESH_SIMPLE,
|
||||||
|
SPHERE_PRIMITIVE_TYPE,
|
||||||
|
SPHERE_VERTEX_COUNT,
|
||||||
|
SPHERE_MESH_SIMPLE_VERTICES
|
||||||
|
));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
void sphereBuffer(
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const vec3 center,
|
||||||
|
const float_t radius,
|
||||||
|
const int32_t stacks,
|
||||||
|
const int32_t sectors
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
|
) {
|
||||||
|
assertNotNull(vertices, "Vertices cannot be NULL");
|
||||||
|
assertNotNull(center, "Center vector cannot be NULL");
|
||||||
|
|
||||||
|
const float_t stackStep = (float_t)GLM_PI / (float_t)stacks;
|
||||||
|
const float_t sectorStep = 2.0f * (float_t)GLM_PI / (float_t)sectors;
|
||||||
|
int32_t vi = 0;
|
||||||
|
|
||||||
|
for(int32_t i = 0; i < stacks; i++) {
|
||||||
|
/* Latitude angles: top of band -> bottom of band */
|
||||||
|
const float_t phi1 = (float_t)GLM_PI_2 - (float_t)i * stackStep;
|
||||||
|
const float_t phi2 = (float_t)GLM_PI_2 - (float_t)(i + 1) * stackStep;
|
||||||
|
|
||||||
|
const float_t y1 = radius * sinf(phi1);
|
||||||
|
const float_t y2 = radius * sinf(phi2);
|
||||||
|
const float_t xz1 = radius * cosf(phi1);
|
||||||
|
const float_t xz2 = radius * cosf(phi2);
|
||||||
|
|
||||||
|
const float_t v1 = 1.0f - (float_t)i / (float_t)stacks;
|
||||||
|
const float_t v2 = 1.0f - (float_t)(i + 1) / (float_t)stacks;
|
||||||
|
|
||||||
|
for(int32_t j = 0; j < sectors; j++) {
|
||||||
|
const float_t theta1 = (float_t)j * sectorStep;
|
||||||
|
const float_t theta2 = (float_t)(j + 1) * sectorStep;
|
||||||
|
|
||||||
|
const float_t x11 = xz1 * cosf(theta1);
|
||||||
|
const float_t z11 = xz1 * sinf(theta1);
|
||||||
|
const float_t x12 = xz1 * cosf(theta2);
|
||||||
|
const float_t z12 = xz1 * sinf(theta2);
|
||||||
|
|
||||||
|
const float_t x21 = xz2 * cosf(theta1);
|
||||||
|
const float_t z21 = xz2 * sinf(theta1);
|
||||||
|
const float_t x22 = xz2 * cosf(theta2);
|
||||||
|
const float_t z22 = xz2 * sinf(theta2);
|
||||||
|
|
||||||
|
const float_t u1 = (float_t)j / (float_t)sectors;
|
||||||
|
const float_t u2 = (float_t)(j + 1) / (float_t)sectors;
|
||||||
|
|
||||||
|
/* Triangle 1: top-left, bottom-left, top-right */
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[vi].color = color;
|
||||||
|
#endif
|
||||||
|
vertices[vi].pos[0] = center[0] + x11;
|
||||||
|
vertices[vi].pos[1] = center[1] + y1;
|
||||||
|
vertices[vi].pos[2] = center[2] + z11;
|
||||||
|
vertices[vi].uv[0] = u1;
|
||||||
|
vertices[vi].uv[1] = v1;
|
||||||
|
vi++;
|
||||||
|
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[vi].color = color;
|
||||||
|
#endif
|
||||||
|
vertices[vi].pos[0] = center[0] + x21;
|
||||||
|
vertices[vi].pos[1] = center[1] + y2;
|
||||||
|
vertices[vi].pos[2] = center[2] + z21;
|
||||||
|
vertices[vi].uv[0] = u1;
|
||||||
|
vertices[vi].uv[1] = v2;
|
||||||
|
vi++;
|
||||||
|
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[vi].color = color;
|
||||||
|
#endif
|
||||||
|
vertices[vi].pos[0] = center[0] + x12;
|
||||||
|
vertices[vi].pos[1] = center[1] + y1;
|
||||||
|
vertices[vi].pos[2] = center[2] + z12;
|
||||||
|
vertices[vi].uv[0] = u2;
|
||||||
|
vertices[vi].uv[1] = v1;
|
||||||
|
vi++;
|
||||||
|
|
||||||
|
/* Triangle 2: top-right, bottom-left, bottom-right */
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[vi].color = color;
|
||||||
|
#endif
|
||||||
|
vertices[vi].pos[0] = center[0] + x12;
|
||||||
|
vertices[vi].pos[1] = center[1] + y1;
|
||||||
|
vertices[vi].pos[2] = center[2] + z12;
|
||||||
|
vertices[vi].uv[0] = u2;
|
||||||
|
vertices[vi].uv[1] = v1;
|
||||||
|
vi++;
|
||||||
|
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[vi].color = color;
|
||||||
|
#endif
|
||||||
|
vertices[vi].pos[0] = center[0] + x21;
|
||||||
|
vertices[vi].pos[1] = center[1] + y2;
|
||||||
|
vertices[vi].pos[2] = center[2] + z21;
|
||||||
|
vertices[vi].uv[0] = u1;
|
||||||
|
vertices[vi].uv[1] = v2;
|
||||||
|
vi++;
|
||||||
|
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
vertices[vi].color = color;
|
||||||
|
#endif
|
||||||
|
vertices[vi].pos[0] = center[0] + x22;
|
||||||
|
vertices[vi].pos[1] = center[1] + y2;
|
||||||
|
vertices[vi].pos[2] = center[2] + z22;
|
||||||
|
vertices[vi].uv[0] = u2;
|
||||||
|
vertices[vi].uv[1] = v2;
|
||||||
|
vi++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/mesh/mesh.h"
|
||||||
|
#include "display/color.h"
|
||||||
|
|
||||||
|
#define SPHERE_STACKS 8
|
||||||
|
#define SPHERE_SECTORS 16
|
||||||
|
#define SPHERE_VERTEX_COUNT (SPHERE_STACKS * SPHERE_SECTORS * 6)
|
||||||
|
#define SPHERE_PRIMITIVE_TYPE MESH_PRIMITIVE_TYPE_TRIANGLES
|
||||||
|
|
||||||
|
extern mesh_t SPHERE_MESH_SIMPLE;
|
||||||
|
extern meshvertex_t SPHERE_MESH_SIMPLE_VERTICES[SPHERE_VERTEX_COUNT];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the simple unit sphere mesh centered at (0,0,0) with radius 0.5.
|
||||||
|
*
|
||||||
|
* @return Error for initialization of the sphere mesh.
|
||||||
|
*/
|
||||||
|
errorret_t sphereInit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buffers a UV sphere into the provided vertex array.
|
||||||
|
* Writes stacks * sectors * 6 vertices (CCW winding).
|
||||||
|
*
|
||||||
|
* @param vertices Vertex array to write into (must hold stacks*sectors*6).
|
||||||
|
* @param center Center position of the sphere.
|
||||||
|
* @param radius Radius of the sphere.
|
||||||
|
* @param stacks Number of horizontal rings (latitude bands).
|
||||||
|
* @param sectors Number of vertical segments (longitude slices).
|
||||||
|
* @param color Color applied to all vertices.
|
||||||
|
*/
|
||||||
|
void sphereBuffer(
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const vec3 center,
|
||||||
|
const float_t radius,
|
||||||
|
const int32_t stacks,
|
||||||
|
const int32_t sectors
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
|
);
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "triprism.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
mesh_t TRIPRISM_MESH_SIMPLE;
|
||||||
|
meshvertex_t TRIPRISM_MESH_SIMPLE_VERTICES[TRIPRISM_VERTEX_COUNT];
|
||||||
|
|
||||||
|
errorret_t triPrismInit() {
|
||||||
|
triPrismBuffer(
|
||||||
|
TRIPRISM_MESH_SIMPLE_VERTICES,
|
||||||
|
0.0f, 0.0f, /* p0: bottom-left */
|
||||||
|
1.0f, 0.0f, /* p1: bottom-right */
|
||||||
|
0.5f, 1.0f, /* p2: apex */
|
||||||
|
0.0f, 1.0f /* minZ, maxZ */
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, COLOR_WHITE_4B
|
||||||
|
#endif
|
||||||
|
);
|
||||||
|
errorChain(meshInit(
|
||||||
|
&TRIPRISM_MESH_SIMPLE,
|
||||||
|
TRIPRISM_PRIMITIVE_TYPE,
|
||||||
|
TRIPRISM_VERTEX_COUNT,
|
||||||
|
TRIPRISM_MESH_SIMPLE_VERTICES
|
||||||
|
));
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
void triPrismBuffer(
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const float_t x0, const float_t y0,
|
||||||
|
const float_t x1, const float_t y1,
|
||||||
|
const float_t x2, const float_t y2,
|
||||||
|
const float_t minZ,
|
||||||
|
const float_t maxZ
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
|
) {
|
||||||
|
assertNotNull(vertices, "Vertices cannot be NULL");
|
||||||
|
|
||||||
|
/* Helper macro: write one vertex then advance index. */
|
||||||
|
int32_t vi = 0;
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
#define PRISM_VERT(px, py, pz, u, v) \
|
||||||
|
vertices[vi].color = color; \
|
||||||
|
vertices[vi].pos[0] = (px); \
|
||||||
|
vertices[vi].pos[1] = (py); \
|
||||||
|
vertices[vi].pos[2] = (pz); \
|
||||||
|
vertices[vi].uv[0] = (u); \
|
||||||
|
vertices[vi].uv[1] = (v); \
|
||||||
|
vi++;
|
||||||
|
#else
|
||||||
|
#define PRISM_VERT(px, py, pz, u, v) \
|
||||||
|
vertices[vi].pos[0] = (px); \
|
||||||
|
vertices[vi].pos[1] = (py); \
|
||||||
|
vertices[vi].pos[2] = (pz); \
|
||||||
|
vertices[vi].uv[0] = (u); \
|
||||||
|
vertices[vi].uv[1] = (v); \
|
||||||
|
vi++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* --- Front face (z = maxZ), CCW from +Z --- */
|
||||||
|
PRISM_VERT(x0, y0, maxZ, 0.0f, 0.0f)
|
||||||
|
PRISM_VERT(x1, y1, maxZ, 1.0f, 0.0f)
|
||||||
|
PRISM_VERT(x2, y2, maxZ, 0.5f, 1.0f)
|
||||||
|
|
||||||
|
/* --- Back face (z = minZ), CCW from -Z (reverse winding) --- */
|
||||||
|
PRISM_VERT(x2, y2, minZ, 0.5f, 1.0f)
|
||||||
|
PRISM_VERT(x1, y1, minZ, 1.0f, 0.0f)
|
||||||
|
PRISM_VERT(x0, y0, minZ, 0.0f, 0.0f)
|
||||||
|
|
||||||
|
/* --- Side face 0: edge p0->p1 --- */
|
||||||
|
PRISM_VERT(x0, y0, minZ, 0.0f, 0.0f)
|
||||||
|
PRISM_VERT(x1, y1, minZ, 1.0f, 0.0f)
|
||||||
|
PRISM_VERT(x1, y1, maxZ, 1.0f, 1.0f)
|
||||||
|
|
||||||
|
PRISM_VERT(x0, y0, minZ, 0.0f, 0.0f)
|
||||||
|
PRISM_VERT(x1, y1, maxZ, 1.0f, 1.0f)
|
||||||
|
PRISM_VERT(x0, y0, maxZ, 0.0f, 1.0f)
|
||||||
|
|
||||||
|
/* --- Side face 1: edge p1->p2 --- */
|
||||||
|
PRISM_VERT(x1, y1, minZ, 0.0f, 0.0f)
|
||||||
|
PRISM_VERT(x2, y2, minZ, 1.0f, 0.0f)
|
||||||
|
PRISM_VERT(x2, y2, maxZ, 1.0f, 1.0f)
|
||||||
|
|
||||||
|
PRISM_VERT(x1, y1, minZ, 0.0f, 0.0f)
|
||||||
|
PRISM_VERT(x2, y2, maxZ, 1.0f, 1.0f)
|
||||||
|
PRISM_VERT(x1, y1, maxZ, 0.0f, 1.0f)
|
||||||
|
|
||||||
|
/* --- Side face 2: edge p2->p0 --- */
|
||||||
|
PRISM_VERT(x2, y2, minZ, 0.0f, 0.0f)
|
||||||
|
PRISM_VERT(x0, y0, minZ, 1.0f, 0.0f)
|
||||||
|
PRISM_VERT(x0, y0, maxZ, 1.0f, 1.0f)
|
||||||
|
|
||||||
|
PRISM_VERT(x2, y2, minZ, 0.0f, 0.0f)
|
||||||
|
PRISM_VERT(x0, y0, maxZ, 1.0f, 1.0f)
|
||||||
|
PRISM_VERT(x2, y2, maxZ, 0.0f, 1.0f)
|
||||||
|
|
||||||
|
#undef PRISM_VERT
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/mesh/mesh.h"
|
||||||
|
#include "display/color.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vertex layout:
|
||||||
|
* 2 triangular end-caps (3 verts each) = 6
|
||||||
|
* 3 rectangular side faces (6 verts each) = 18
|
||||||
|
* Total = 24
|
||||||
|
*/
|
||||||
|
#define TRIPRISM_VERTEX_COUNT 24
|
||||||
|
#define TRIPRISM_PRIMITIVE_TYPE MESH_PRIMITIVE_TYPE_TRIANGLES
|
||||||
|
|
||||||
|
extern mesh_t TRIPRISM_MESH_SIMPLE;
|
||||||
|
extern meshvertex_t TRIPRISM_MESH_SIMPLE_VERTICES[TRIPRISM_VERTEX_COUNT];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the simple unit triangular prism. The cross-section triangle has
|
||||||
|
* vertices (0,0), (1,0), (0.5,1) in XY, extruded from z=0 to z=1.
|
||||||
|
*
|
||||||
|
* @return Error for initialization of the triangular prism mesh.
|
||||||
|
*/
|
||||||
|
errorret_t triPrismInit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buffers a triangular prism into the provided vertex array.
|
||||||
|
* The triangular cross-section is defined by three 2D points in the XY plane;
|
||||||
|
* the prism is extruded along the Z axis between minZ and maxZ.
|
||||||
|
* Writes TRIPRISM_VERTEX_COUNT (24) vertices (CCW winding).
|
||||||
|
*
|
||||||
|
* @param vertices Vertex array to write into (must hold TRIPRISM_VERTEX_COUNT).
|
||||||
|
* @param x0,y0 First triangle vertex (XY).
|
||||||
|
* @param x1,y1 Second triangle vertex (XY).
|
||||||
|
* @param x2,y2 Third triangle vertex (XY).
|
||||||
|
* @param minZ Near Z extent of the prism.
|
||||||
|
* @param maxZ Far Z extent of the prism.
|
||||||
|
* @param color Color applied to all vertices.
|
||||||
|
*/
|
||||||
|
void triPrismBuffer(
|
||||||
|
meshvertex_t *vertices,
|
||||||
|
const float_t x0, const float_t y0,
|
||||||
|
const float_t x1, const float_t y1,
|
||||||
|
const float_t x2, const float_t y2,
|
||||||
|
const float_t minZ,
|
||||||
|
const float_t maxZ
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, const color_t color
|
||||||
|
#endif
|
||||||
|
);
|
||||||
@@ -22,19 +22,15 @@ errorret_t screenInit() {
|
|||||||
SCREEN.mode = SCREEN_MODE_FIXED_VIEWPORT_HEIGHT;
|
SCREEN.mode = SCREEN_MODE_FIXED_VIEWPORT_HEIGHT;
|
||||||
SCREEN.fixedHeight.height = DUSK_DISPLAY_SCREEN_HEIGHT;
|
SCREEN.fixedHeight.height = DUSK_DISPLAY_SCREEN_HEIGHT;
|
||||||
|
|
||||||
cameraInitOrthographic(&SCREEN.framebufferCamera);
|
|
||||||
SCREEN.framebufferCamera.viewType = CAMERA_VIEW_TYPE_2D;
|
|
||||||
SCREEN.framebufferCamera._2d.position[0] = 0;
|
|
||||||
SCREEN.framebufferCamera._2d.position[1] = 0;
|
|
||||||
SCREEN.framebufferCamera._2d.zoom = 1.0f;
|
|
||||||
|
|
||||||
quadBuffer(
|
quadBuffer(
|
||||||
SCREEN.frameBufferMeshVertices,
|
SCREEN.frameBufferMeshVertices,
|
||||||
0.0f, 0.0f,
|
0.0f, 0.0f,
|
||||||
1.0f, 1.0f,
|
1.0f, 1.0f,
|
||||||
COLOR_WHITE,
|
|
||||||
0.0f, 0.0f,
|
0.0f, 0.0f,
|
||||||
1.0f, 1.0f
|
1.0f, 1.0f
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, COLOR_WHITE
|
||||||
|
#endif
|
||||||
);
|
);
|
||||||
errorChain(meshInit(
|
errorChain(meshInit(
|
||||||
&SCREEN.frameBufferMesh,
|
&SCREEN.frameBufferMesh,
|
||||||
@@ -56,7 +52,7 @@ errorret_t screenBind() {
|
|||||||
// Screen mode backbuffer uses the full display size
|
// Screen mode backbuffer uses the full display size
|
||||||
SCREEN.width = frameBufferGetWidth(FRAMEBUFFER_BOUND);
|
SCREEN.width = frameBufferGetWidth(FRAMEBUFFER_BOUND);
|
||||||
SCREEN.height = frameBufferGetHeight(FRAMEBUFFER_BOUND);
|
SCREEN.height = frameBufferGetHeight(FRAMEBUFFER_BOUND);
|
||||||
SCREEN.aspect = (float_t)SCREEN.width / (float_t)SCREEN.height;
|
SCREEN.aspect = frameBufferGetAspect(FRAMEBUFFER_BOUND);
|
||||||
|
|
||||||
// No needd for a framebuffer.
|
// No needd for a framebuffer.
|
||||||
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
@@ -104,8 +100,7 @@ errorret_t screenBind() {
|
|||||||
int32_t fbWidth, fbHeight;
|
int32_t fbWidth, fbHeight;
|
||||||
fbWidth = frameBufferGetWidth(FRAMEBUFFER_BOUND);
|
fbWidth = frameBufferGetWidth(FRAMEBUFFER_BOUND);
|
||||||
fbHeight = frameBufferGetHeight(FRAMEBUFFER_BOUND);
|
fbHeight = frameBufferGetHeight(FRAMEBUFFER_BOUND);
|
||||||
|
float_t currentAspect = frameBufferGetAspect(FRAMEBUFFER_BOUND);
|
||||||
float_t currentAspect = (float_t)fbWidth / (float_t)fbHeight;
|
|
||||||
if(currentAspect == SCREEN.aspectRatio.ratio) {
|
if(currentAspect == SCREEN.aspectRatio.ratio) {
|
||||||
// No need to use framebuffer.
|
// No need to use framebuffer.
|
||||||
SCREEN.width = fbWidth;
|
SCREEN.width = fbWidth;
|
||||||
@@ -133,13 +128,14 @@ errorret_t screenBind() {
|
|||||||
if(SCREEN.framebufferReady) {
|
if(SCREEN.framebufferReady) {
|
||||||
// Is current framebuffer the correct size?
|
// Is current framebuffer the correct size?
|
||||||
int32_t curFbWidth, curFbHeight;
|
int32_t curFbWidth, curFbHeight;
|
||||||
|
float_t curFbAspect = frameBufferGetAspect(&SCREEN.framebuffer);
|
||||||
curFbWidth = frameBufferGetWidth(&SCREEN.framebuffer);
|
curFbWidth = frameBufferGetWidth(&SCREEN.framebuffer);
|
||||||
curFbHeight = frameBufferGetHeight(&SCREEN.framebuffer);
|
curFbHeight = frameBufferGetHeight(&SCREEN.framebuffer);
|
||||||
if(curFbWidth == newFbWidth && curFbHeight == newFbHeight) {
|
if(curFbWidth == newFbWidth && curFbHeight == newFbHeight) {
|
||||||
// Correct size, nothing to do.
|
// Correct size, nothing to do.
|
||||||
SCREEN.width = newFbWidth;
|
SCREEN.width = newFbWidth;
|
||||||
SCREEN.height = newFbHeight;
|
SCREEN.height = newFbHeight;
|
||||||
SCREEN.aspect = (float_t)SCREEN.width / (float_t)SCREEN.height;
|
SCREEN.aspect = curFbAspect;
|
||||||
errorChain(frameBufferBind(&SCREEN.framebuffer));
|
errorChain(frameBufferBind(&SCREEN.framebuffer));
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -352,20 +348,26 @@ errorret_t screenRender() {
|
|||||||
fbY = (bbHeight - fbHeight) * 0.5f;
|
fbY = (bbHeight - fbHeight) * 0.5f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine back buffer matricies
|
||||||
float_t centerX = bbWidth * 0.5f;
|
float_t centerX = bbWidth * 0.5f;
|
||||||
float_t centerY = bbHeight * 0.5f;
|
float_t centerY = bbHeight * 0.5f;
|
||||||
|
mat4 view, proj, model;
|
||||||
SCREEN.framebufferCamera.orthographic.left = 0.0f;
|
glm_ortho(
|
||||||
SCREEN.framebufferCamera.orthographic.right = bbWidth;
|
0.0f, bbWidth, bbHeight, 0.0f, 0.01f, 1.0f,
|
||||||
SCREEN.framebufferCamera.orthographic.top = 0.0f;
|
proj
|
||||||
SCREEN.framebufferCamera.orthographic.bottom = bbHeight;
|
);
|
||||||
|
glm_mat4_identity(view);
|
||||||
|
glm_mat4_identity(model);
|
||||||
|
|
||||||
quadBuffer(
|
quadBuffer(
|
||||||
SCREEN.frameBufferMeshVertices,
|
SCREEN.frameBufferMeshVertices,
|
||||||
centerX - fbWidth * 0.5f, centerY + fbHeight * 0.5f, // top-left
|
centerX - fbWidth * 0.5f, centerY + fbHeight * 0.5f, // top-left
|
||||||
centerX + fbWidth * 0.5f, centerY - fbHeight * 0.5f, // bottom-right
|
centerX + fbWidth * 0.5f, centerY - fbHeight * 0.5f, // bottom-right
|
||||||
COLOR_WHITE,
|
|
||||||
0.0f, 0.0f,
|
0.0f, 0.0f,
|
||||||
1.0f, 1.0f
|
1.0f, 1.0f
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, COLOR_WHITE
|
||||||
|
#endif
|
||||||
);
|
);
|
||||||
|
|
||||||
frameBufferClear(
|
frameBufferClear(
|
||||||
@@ -374,10 +376,6 @@ errorret_t screenRender() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
shaderBind(&SHADER_UNLIT);
|
shaderBind(&SHADER_UNLIT);
|
||||||
mat4 proj, view, model;
|
|
||||||
cameraGetProjectionMatrix(&SCREEN.framebufferCamera, proj);
|
|
||||||
cameraGetViewMatrix(&SCREEN.framebufferCamera, view);
|
|
||||||
glm_mat4_identity(model);
|
|
||||||
shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj);
|
shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj);
|
||||||
shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view);
|
shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view);
|
||||||
shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model);
|
shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model);
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "dusk.h"
|
#include "dusk.h"
|
||||||
#include "display/framebuffer/framebuffer.h"
|
#include "display/framebuffer/framebuffer.h"
|
||||||
#include "display/camera/camera.h"
|
|
||||||
#include "display/mesh/quad.h"
|
#include "display/mesh/quad.h"
|
||||||
#include "display/color.h"
|
#include "display/color.h"
|
||||||
|
|
||||||
@@ -50,7 +49,7 @@ typedef struct {
|
|||||||
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
||||||
framebuffer_t framebuffer;
|
framebuffer_t framebuffer;
|
||||||
bool_t framebufferReady;
|
bool_t framebufferReady;
|
||||||
camera_t framebufferCamera;
|
// camera_t framebufferCamera;
|
||||||
mesh_t frameBufferMesh;
|
mesh_t frameBufferMesh;
|
||||||
meshvertex_t frameBufferMeshVertices[QUAD_VERTEX_COUNT];
|
meshvertex_t frameBufferMeshVertices[QUAD_VERTEX_COUNT];
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
|
#include "shadermaterial.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
|
||||||
shader_t *bound = NULL;
|
shader_t *bound = NULL;
|
||||||
@@ -49,16 +50,28 @@ errorret_t shaderSetTexture(
|
|||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
// errorret_t shaderSetColor(
|
errorret_t shaderSetColor(
|
||||||
// shader_t *shader,
|
shader_t *shader,
|
||||||
// const char_t *name,
|
const char_t *name,
|
||||||
// color_t color
|
color_t color
|
||||||
// ) {
|
) {
|
||||||
// assertNotNull(shader, "Shader cannot be null");
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
// assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
||||||
// errorChain(shaderSetColorPlatform(shader, name, color));
|
errorChain(shaderSetColorPlatform(shader, name, color));
|
||||||
// errorOk();
|
errorOk();
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
errorret_t shaderSetMaterial(
|
||||||
|
shader_t *shader,
|
||||||
|
const shadermaterial_t *material
|
||||||
|
) {
|
||||||
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
assertNotNull(material, "Material cannot be null");
|
||||||
|
assertTrue(bound == shader, "Shader must be bound.");
|
||||||
|
assertNotNull(shader->definition, "Shader definition cannot be null");
|
||||||
|
assertNotNull(shader->definition->setMaterial, "Def lacks setMaterial");
|
||||||
|
return shader->definition->setMaterial(shader, material);
|
||||||
|
}
|
||||||
|
|
||||||
errorret_t shaderDispose(shader_t *shader) {
|
errorret_t shaderDispose(shader_t *shader) {
|
||||||
assertNotNull(shader, "Shader cannot be null");
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#error "shaderDisposePlatform must be defined to use shader.h"
|
#error "shaderDisposePlatform must be defined to use shader.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef union shadermaterial_u shadermaterial_t;
|
||||||
typedef shaderplatform_t shader_t;
|
typedef shaderplatform_t shader_t;
|
||||||
typedef shaderdefinitionplatform_t shaderdefinition_t;
|
typedef shaderdefinitionplatform_t shaderdefinition_t;
|
||||||
|
|
||||||
@@ -79,15 +80,28 @@ errorret_t shaderSetTexture(
|
|||||||
* @param color Color to set
|
* @param color Color to set
|
||||||
* @return Error if failure, otherwise errorOk
|
* @return Error if failure, otherwise errorOk
|
||||||
*/
|
*/
|
||||||
// errorret_t shaderSetColor(
|
errorret_t shaderSetColor(
|
||||||
// shader_t *shader,
|
shader_t *shader,
|
||||||
// const char_t *name,
|
const char_t *name,
|
||||||
// color_t color
|
color_t color
|
||||||
// );
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a material's properties in the shader. This is platform dependant.
|
||||||
|
* the definition's upload function pointer.
|
||||||
|
*
|
||||||
|
* @param shader The shader to upload material properties to.
|
||||||
|
* @param material The material data to upload.
|
||||||
|
* @return Error if failure, otherwise errorOk.
|
||||||
|
*/
|
||||||
|
errorret_t shaderSetMaterial(
|
||||||
|
shader_t *shader,
|
||||||
|
const shadermaterial_t *material
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disposes of a shader. This is platform dependant.
|
* Disposes of a shader. This is platform dependant.
|
||||||
*
|
*
|
||||||
* @param shader Shader to dispose
|
* @param shader Shader to dispose
|
||||||
* @return Error if failure, otherwise errorOk
|
* @return Error if failure, otherwise errorOk
|
||||||
*/
|
*/
|
||||||
|
|||||||
+5
-4
@@ -1,12 +1,13 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2026 Dominic Masters
|
* Copyright (c) 2026 Dominic Masters
|
||||||
*
|
*
|
||||||
* 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
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "cameragl.h"
|
#include "display/shader/shaderunlit.h"
|
||||||
|
|
||||||
#define cameraPushMatrixPlatform cameraPushMatrixGL
|
typedef union shadermaterial_u {
|
||||||
#define cameraPopMatrixPlatform cameraPopMatrixGL
|
shaderunlitmaterial_t unlit;
|
||||||
|
} shadermaterial_t;
|
||||||
@@ -6,5 +6,27 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "shaderunlit.h"
|
#include "shaderunlit.h"
|
||||||
|
#include "display/shader/shadermaterial.h"
|
||||||
|
|
||||||
shader_t SHADER_UNLIT = { 0 };
|
shader_t SHADER_UNLIT = {
|
||||||
|
.definition = &SHADER_UNLIT_DEFINITION
|
||||||
|
};
|
||||||
|
|
||||||
|
errorret_t shaderUnlitSetMaterial(
|
||||||
|
shader_t *shader,
|
||||||
|
const shadermaterial_t *material
|
||||||
|
) {
|
||||||
|
errorChain(shaderSetTexture(
|
||||||
|
shader,
|
||||||
|
SHADER_UNLIT_TEXTURE,
|
||||||
|
material->unlit.texture
|
||||||
|
));
|
||||||
|
|
||||||
|
errorChain(shaderSetColor(
|
||||||
|
shader,
|
||||||
|
SHADER_UNLIT_COLOR,
|
||||||
|
material->unlit.color
|
||||||
|
));
|
||||||
|
|
||||||
|
errorOk();
|
||||||
|
}
|
||||||
@@ -12,7 +12,24 @@
|
|||||||
#define SHADER_UNLIT_VIEW "u_View"
|
#define SHADER_UNLIT_VIEW "u_View"
|
||||||
#define SHADER_UNLIT_MODEL "u_Model"
|
#define SHADER_UNLIT_MODEL "u_Model"
|
||||||
#define SHADER_UNLIT_TEXTURE "u_Texture"
|
#define SHADER_UNLIT_TEXTURE "u_Texture"
|
||||||
// #define SHADER_UNLIT_COLOR "u_Color"
|
#define SHADER_UNLIT_COLOR "u_Color"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
color_t color;
|
||||||
|
texture_t *texture;
|
||||||
|
} shaderunlitmaterial_t;
|
||||||
|
|
||||||
extern shaderdefinition_t SHADER_UNLIT_DEFINITION;
|
extern shaderdefinition_t SHADER_UNLIT_DEFINITION;
|
||||||
extern shader_t SHADER_UNLIT;
|
extern shader_t SHADER_UNLIT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uploads the unlit material properties to the shader.
|
||||||
|
*
|
||||||
|
* @param shader The shader to upload to.
|
||||||
|
* @param material The material data to upload.
|
||||||
|
* @return Error if failure, otherwise errorOk.
|
||||||
|
*/
|
||||||
|
errorret_t shaderUnlitSetMaterial(
|
||||||
|
shader_t *shader,
|
||||||
|
const shadermaterial_t *material
|
||||||
|
);
|
||||||
@@ -29,7 +29,9 @@ errorret_t spriteBatchPush(
|
|||||||
const float_t minY,
|
const float_t minY,
|
||||||
const float_t maxX,
|
const float_t maxX,
|
||||||
const float_t maxY,
|
const float_t maxY,
|
||||||
const color_t color,
|
#if MESH_ENABLE_COLOR
|
||||||
|
const color_t color,
|
||||||
|
#endif
|
||||||
const float_t u0,
|
const float_t u0,
|
||||||
const float_t v0,
|
const float_t v0,
|
||||||
const float_t u1,
|
const float_t u1,
|
||||||
@@ -38,7 +40,9 @@ errorret_t spriteBatchPush(
|
|||||||
return spriteBatchPush3D(
|
return spriteBatchPush3D(
|
||||||
(vec3){ minX, minY, 0 },
|
(vec3){ minX, minY, 0 },
|
||||||
(vec3){ maxX, maxY, 0 },
|
(vec3){ maxX, maxY, 0 },
|
||||||
color,
|
#if MESH_ENABLE_COLOR
|
||||||
|
color,
|
||||||
|
#endif
|
||||||
(vec2){ u0, v0 },
|
(vec2){ u0, v0 },
|
||||||
(vec2){ u1, v1 }
|
(vec2){ u1, v1 }
|
||||||
);
|
);
|
||||||
@@ -47,7 +51,9 @@ errorret_t spriteBatchPush(
|
|||||||
errorret_t spriteBatchPush3D(
|
errorret_t spriteBatchPush3D(
|
||||||
const vec3 min,
|
const vec3 min,
|
||||||
const vec3 max,
|
const vec3 max,
|
||||||
const color_t color,
|
#if MESH_ENABLE_COLOR
|
||||||
|
const color_t color,
|
||||||
|
#endif
|
||||||
const vec2 uv0,
|
const vec2 uv0,
|
||||||
const vec2 uv1
|
const vec2 uv1
|
||||||
) {
|
) {
|
||||||
@@ -62,7 +68,11 @@ errorret_t spriteBatchPush3D(
|
|||||||
) * QUAD_VERTEX_COUNT;
|
) * QUAD_VERTEX_COUNT;
|
||||||
quadBuffer3D(
|
quadBuffer3D(
|
||||||
&SPRITEBATCH_VERTICES[vertexOffset],
|
&SPRITEBATCH_VERTICES[vertexOffset],
|
||||||
min, max, color, uv0, uv1
|
min, max,
|
||||||
|
uv0, uv1
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
, color
|
||||||
|
#endif
|
||||||
);
|
);
|
||||||
SPRITEBATCH.spriteCount++;
|
SPRITEBATCH.spriteCount++;
|
||||||
errorOk();
|
errorOk();
|
||||||
|
|||||||
@@ -57,7 +57,9 @@ errorret_t spriteBatchPush(
|
|||||||
const float_t minY,
|
const float_t minY,
|
||||||
const float_t maxX,
|
const float_t maxX,
|
||||||
const float_t maxY,
|
const float_t maxY,
|
||||||
const color_t color,
|
#if MESH_ENABLE_COLOR
|
||||||
|
const color_t color,
|
||||||
|
#endif
|
||||||
const float_t u0,
|
const float_t u0,
|
||||||
const float_t v0,
|
const float_t v0,
|
||||||
const float_t u1,
|
const float_t u1,
|
||||||
@@ -78,7 +80,9 @@ errorret_t spriteBatchPush(
|
|||||||
errorret_t spriteBatchPush3D(
|
errorret_t spriteBatchPush3D(
|
||||||
const vec3 min,
|
const vec3 min,
|
||||||
const vec3 max,
|
const vec3 max,
|
||||||
const color_t color,
|
#if MESH_ENABLE_COLOR
|
||||||
|
const color_t color,
|
||||||
|
#endif
|
||||||
const vec2 uvMin,
|
const vec2 uvMin,
|
||||||
const vec2 uvMax
|
const vec2 uvMax
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -9,16 +9,18 @@
|
|||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
#include "display/spritebatch/spritebatch.h"
|
#include "display/spritebatch/spritebatch.h"
|
||||||
#include "asset/asset.h"
|
#include "asset/loader/display/assettextureloader.h"
|
||||||
|
#include "asset/loader/display/assettilesetloader.h"
|
||||||
#include "display/shader/shaderunlit.h"
|
#include "display/shader/shaderunlit.h"
|
||||||
|
|
||||||
texture_t DEFAULT_FONT_TEXTURE;
|
texture_t DEFAULT_FONT_TEXTURE;
|
||||||
tileset_t DEFAULT_FONT_TILESET;
|
tileset_t DEFAULT_FONT_TILESET;
|
||||||
|
|
||||||
errorret_t textInit(void) {
|
errorret_t textInit(void) {
|
||||||
errorChain(assetLoad("ui/minogram.dtx", &DEFAULT_FONT_TEXTURE));
|
errorChain(assetTextureLoad(
|
||||||
errorChain(assetLoad("ui/minogram.dtf", &DEFAULT_FONT_TILESET));
|
"ui/minogram.png", &DEFAULT_FONT_TEXTURE, TEXTURE_FORMAT_RGBA
|
||||||
|
));
|
||||||
|
errorChain(assetTilesetLoad("ui/minogram.dtf", &DEFAULT_FONT_TILESET));
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +33,9 @@ errorret_t textDrawChar(
|
|||||||
const float_t x,
|
const float_t x,
|
||||||
const float_t y,
|
const float_t y,
|
||||||
const char_t c,
|
const char_t c,
|
||||||
const color_t color,
|
#if MESH_ENABLE_COLOR
|
||||||
|
const color_t color,
|
||||||
|
#endif
|
||||||
const tileset_t *tileset,
|
const tileset_t *tileset,
|
||||||
texture_t *texture
|
texture_t *texture
|
||||||
) {
|
) {
|
||||||
@@ -53,7 +57,9 @@ errorret_t textDrawChar(
|
|||||||
x, y,
|
x, y,
|
||||||
x + tileset->tileWidth,
|
x + tileset->tileWidth,
|
||||||
y + tileset->tileHeight,
|
y + tileset->tileHeight,
|
||||||
color,
|
#if MESH_ENABLE_COLOR
|
||||||
|
color,
|
||||||
|
#endif
|
||||||
uv[0], uv[1], uv[2], uv[3]
|
uv[0], uv[1], uv[2], uv[3]
|
||||||
));
|
));
|
||||||
errorOk();
|
errorOk();
|
||||||
@@ -75,6 +81,20 @@ errorret_t textDraw(
|
|||||||
|
|
||||||
errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, texture));
|
errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, texture));
|
||||||
|
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
#else
|
||||||
|
errorChain(shaderSetColor(&SHADER_UNLIT, SHADER_UNLIT_COLOR, color));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// errorChain(spriteBatchPush(
|
||||||
|
// // texture,
|
||||||
|
// posX, posY,
|
||||||
|
// posX + texture->width * 1, posY + texture->height * 1,
|
||||||
|
// color,
|
||||||
|
// 0.0f, 0.0f, 1.0f, 1.0f
|
||||||
|
// ));
|
||||||
|
// errorOk();
|
||||||
|
|
||||||
char_t c;
|
char_t c;
|
||||||
int32_t i = 0;
|
int32_t i = 0;
|
||||||
while((c = text[i++]) != '\0') {
|
while((c = text[i++]) != '\0') {
|
||||||
@@ -89,7 +109,13 @@ errorret_t textDraw(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorChain(textDrawChar(posX, posY, c, color, tileset, texture));
|
errorChain(textDrawChar(
|
||||||
|
posX, posY, c,
|
||||||
|
#if MESH_ENABLE_COLOR
|
||||||
|
color,
|
||||||
|
#endif
|
||||||
|
tileset, texture
|
||||||
|
));
|
||||||
posX += tileset->tileWidth;
|
posX += tileset->tileWidth;
|
||||||
}
|
}
|
||||||
errorOk();
|
errorOk();
|
||||||
|
|||||||
@@ -44,7 +44,9 @@ errorret_t textDrawChar(
|
|||||||
const float_t x,
|
const float_t x,
|
||||||
const float_t y,
|
const float_t y,
|
||||||
const char_t c,
|
const char_t c,
|
||||||
const color_t color,
|
#if MESH_ENABLE_COLOR
|
||||||
|
const color_t color,
|
||||||
|
#endif
|
||||||
const tileset_t *tileset,
|
const tileset_t *tileset,
|
||||||
texture_t *texture
|
texture_t *texture
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -11,6 +11,14 @@
|
|||||||
#include "util/math.h"
|
#include "util/math.h"
|
||||||
#include "display/display.h"
|
#include "display/display.h"
|
||||||
|
|
||||||
|
texture_t TEXTURE_WHITE;
|
||||||
|
color_t TEXTURE_WHITE_PIXELS[4*4] = {
|
||||||
|
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
|
||||||
|
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
|
||||||
|
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
|
||||||
|
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
|
||||||
|
};
|
||||||
|
|
||||||
errorret_t textureInit(
|
errorret_t textureInit(
|
||||||
texture_t *texture,
|
texture_t *texture,
|
||||||
const int32_t width,
|
const int32_t width,
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ typedef union texturedata_u {
|
|||||||
color_t *rgbaColors;
|
color_t *rgbaColors;
|
||||||
} texturedata_t;
|
} texturedata_t;
|
||||||
|
|
||||||
|
extern texture_t TEXTURE_WHITE;
|
||||||
|
extern color_t TEXTURE_WHITE_PIXELS[4*4];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a texture.
|
* Initializes a texture.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
# Copyright (c) 2025 Dominic Masters
|
|
||||||
#
|
|
||||||
# This software is released under the MIT License.
|
|
||||||
# https://opensource.org/licenses/MIT
|
|
||||||
|
|
||||||
ENTITY_DIR_SOUTH = 0
|
|
||||||
ENTITY_DIR_WEST = 1
|
|
||||||
ENTITY_DIR_EAST = 2
|
|
||||||
ENTITY_DIR_NORTH = 3
|
|
||||||
|
|
||||||
ENTITY_COUNT = 128
|
|
||||||
|
|
||||||
ENTITY_TYPE_NULL = 0
|
|
||||||
ENTITY_TYPE_PLAYER = 1
|
|
||||||
ENTITY_TYPE_NPC = 2
|
|
||||||
ENTITY_TYPE_COUNT = 3
|
|
||||||
|
|
||||||
CHUNK_WIDTH = 16
|
|
||||||
CHUNK_HEIGHT = 16
|
|
||||||
CHUNK_DEPTH = 4
|
|
||||||
# CHUNK_VERTEX_COUNT_MAX = QUAD_VERTEXES * CHUNK_WIDTH * CHUNK_HEIGHT * 4
|
|
||||||
CHUNK_VERTEX_COUNT_MAX=6144
|
|
||||||
CHUNK_MESH_COUNT_MAX = 14
|
|
||||||
CHUNK_ENTITY_COUNT_MAX = 8
|
|
||||||
|
|
||||||
TILE_WIDTH = 16.0
|
|
||||||
TILE_HEIGHT = 16.0
|
|
||||||
TILE_DEPTH = 16.0
|
|
||||||
|
|
||||||
TILE_SHAPE_NULL = 0
|
|
||||||
TILE_SHAPE_FLOOR = 1
|
|
||||||
TILE_SHAPE_RAMP_SOUTH = 2
|
|
||||||
TILE_SHAPE_RAMP_WEST = 3
|
|
||||||
TILE_SHAPE_RAMP_EAST = 4
|
|
||||||
TILE_SHAPE_RAMP_NORTH = 5
|
|
||||||
TILE_SHAPE_RAMP_SOUTHWEST = 6
|
|
||||||
TILE_SHAPE_RAMP_SOUTHEAST = 7
|
|
||||||
TILE_SHAPE_RAMP_NORTHWEST = 8
|
|
||||||
TILE_SHAPE_RAMP_NORTHEAST = 9
|
|
||||||
|
|
||||||
RPG_CAMERA_FOV = 70
|
|
||||||
RPG_CAMERA_PIXELS_PER_UNIT = 1.0
|
|
||||||
RPG_CAMERA_Z_OFFSET = 24.0
|
|
||||||
|
|
||||||
ASSET_LANG_CHUNK_CHAR_COUNT = 6144
|
|
||||||
+181
-9
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2025 Dominic Masters
|
* Copyright (c) 2025 Dominic Masters
|
||||||
*
|
*
|
||||||
* 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
|
||||||
*/
|
*/
|
||||||
@@ -14,12 +14,110 @@
|
|||||||
#include "scene/scene.h"
|
#include "scene/scene.h"
|
||||||
#include "asset/asset.h"
|
#include "asset/asset.h"
|
||||||
#include "ui/ui.h"
|
#include "ui/ui.h"
|
||||||
#include "map/map.h"
|
|
||||||
#include "script/scriptmanager.h"
|
#include "script/scriptmanager.h"
|
||||||
#include "item/backpack.h"
|
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
#include "entity/entitymanager.h"
|
||||||
|
#include "entity/component/physics/entityphysics.h"
|
||||||
|
#include "game/game.h"
|
||||||
|
#include "physics/physicsmanager.h"
|
||||||
|
#include "network/network.h"
|
||||||
|
#include "network/networkinfo.h"
|
||||||
|
#include "network/networksocketclient.h"
|
||||||
|
#include "system/system.h"
|
||||||
|
|
||||||
|
#include "display/mesh/cube.h"
|
||||||
|
#include "display/mesh/plane.h"
|
||||||
|
|
||||||
engine_t ENGINE;
|
engine_t ENGINE;
|
||||||
|
entityid_t phBoxEnt;
|
||||||
|
componentid_t phBoxPhys;
|
||||||
|
networksocketclient_t sockClient;
|
||||||
|
|
||||||
|
float_t onlineSwapTime = FLT_MAX;
|
||||||
|
|
||||||
|
void goOnline();
|
||||||
|
void goOffline();
|
||||||
|
|
||||||
|
void onSocketConnected(void *user) {
|
||||||
|
sceneLog("Socket connected.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void onSocketError(errorret_t error, void *user) {
|
||||||
|
sceneLog("Socket error: %s\n", error.state->message);
|
||||||
|
errorCatch(errorPrint(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
void onSocketDisconnected(void *user) {
|
||||||
|
sceneLog("Socket disconnected.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void onNetworkConnected(void *user) {
|
||||||
|
onlineSwapTime = TIME.time + 1.5f;
|
||||||
|
|
||||||
|
networkinfo_t info = networkGetInfo();
|
||||||
|
if(info.type == NETWORK_TYPE_IPV4) {
|
||||||
|
sceneLog(
|
||||||
|
"Connected to network with IPv4 address: " NETWORK_INFO_FORMAT_IPV4 "\n",
|
||||||
|
info.ipv4.ip[0], info.ipv4.ip[1], info.ipv4.ip[2], info.ipv4.ip[3]
|
||||||
|
);
|
||||||
|
#ifdef DUSK_NETWORK_IPV6
|
||||||
|
} else if(info.type == NETWORK_TYPE_IPV6) {
|
||||||
|
sceneLog(
|
||||||
|
"Connected to network with IPv6 address: " NETWORK_INFO_FORMAT_IPV6 "\n",
|
||||||
|
info.ipv6.ip[0], info.ipv6.ip[1], info.ipv6.ip[2], info.ipv6.ip[3],
|
||||||
|
info.ipv6.ip[4], info.ipv6.ip[5], info.ipv6.ip[6], info.ipv6.ip[7],
|
||||||
|
info.ipv6.ip[8], info.ipv6.ip[9], info.ipv6.ip[10], info.ipv6.ip[11],
|
||||||
|
info.ipv6.ip[12], info.ipv6.ip[13], info.ipv6.ip[14], info.ipv6.ip[15]
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
sceneLog("Network connected, opening socket: %.2f1.\n", onlineSwapTime);
|
||||||
|
networkSocketClientInit(
|
||||||
|
&sockClient,
|
||||||
|
"google.com",
|
||||||
|
443,
|
||||||
|
NULL,
|
||||||
|
onSocketConnected,
|
||||||
|
onSocketError,
|
||||||
|
onSocketDisconnected
|
||||||
|
);
|
||||||
|
onlineSwapTime = FLT_MAX;
|
||||||
|
|
||||||
|
// sceneLog("Network connected, I will disconnect at: %.2f1.\n", onlineSwapTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onNetworkFailed(errorret_t error, void *user) {
|
||||||
|
onlineSwapTime = TIME.time + 3.0f;
|
||||||
|
sceneLog("Failed to connect to network, will try again at %.2f1.\n", onlineSwapTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onNetworkDisconnected(errorret_t error, void *user) {
|
||||||
|
onlineSwapTime = TIME.time + 3.0f;
|
||||||
|
sceneLog("Network disconnected, will go online at %.2f1.\n", onlineSwapTime);
|
||||||
|
errorCatch(errorPrint(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
void onNetworkDisconnectFinished(void *user) {
|
||||||
|
onlineSwapTime = TIME.time + 3.0f;
|
||||||
|
sceneLog("Finished disconnecting from network, will go online at %.2f1.\n", onlineSwapTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
void goOnline() {
|
||||||
|
sceneLog("Going online...\n");
|
||||||
|
networkRequestConnection(
|
||||||
|
onNetworkConnected,
|
||||||
|
onNetworkFailed,
|
||||||
|
onNetworkDisconnected,
|
||||||
|
NULL
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void goOffline() {
|
||||||
|
sceneLog("Going offline...\n");
|
||||||
|
networkRequestDisconnection(onNetworkDisconnectFinished, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
||||||
memoryZero(&ENGINE, sizeof(engine_t));
|
memoryZero(&ENGINE, sizeof(engine_t));
|
||||||
@@ -28,6 +126,7 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
|||||||
ENGINE.argv = argv;
|
ENGINE.argv = argv;
|
||||||
|
|
||||||
// Init systems. Order is important.
|
// Init systems. Order is important.
|
||||||
|
errorChain(systemInit());
|
||||||
timeInit();
|
timeInit();
|
||||||
errorChain(inputInit());
|
errorChain(inputInit());
|
||||||
errorChain(assetInit());
|
errorChain(assetInit());
|
||||||
@@ -35,11 +134,60 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
|||||||
errorChain(scriptManagerInit());
|
errorChain(scriptManagerInit());
|
||||||
errorChain(displayInit());
|
errorChain(displayInit());
|
||||||
errorChain(uiInit());
|
errorChain(uiInit());
|
||||||
errorChain(mapInit());
|
|
||||||
errorChain(sceneInit());
|
errorChain(sceneInit());
|
||||||
backpackInit();
|
entityManagerInit();
|
||||||
|
physicsManagerInit();
|
||||||
|
// errorChain(networkInit());
|
||||||
|
errorChain(gameInit());
|
||||||
|
|
||||||
|
onlineSwapTime = TIME.time + 1.0f;
|
||||||
|
sceneLog("Init done, going to queue online at %.2f1.\n", onlineSwapTime);
|
||||||
|
|
||||||
|
// Camera
|
||||||
|
entityid_t cam = entityManagerAdd();
|
||||||
|
componentid_t camPos = entityAddComponent(cam, COMPONENT_TYPE_POSITION);
|
||||||
|
float_t distance = 6.0f;
|
||||||
|
entityPositionLookAt(
|
||||||
|
cam, camPos,
|
||||||
|
(vec3){ 0.0f, 1.0f, 0.0f },
|
||||||
|
(vec3){ 0.0f, 1.0f, 0.0f },
|
||||||
|
(vec3){ distance, distance, distance }
|
||||||
|
);
|
||||||
|
componentid_t camCam = entityAddComponent(cam, COMPONENT_TYPE_CAMERA);
|
||||||
|
entityCameraSetZFar(cam, camCam, 100.0f);
|
||||||
|
|
||||||
|
// Floor
|
||||||
|
entityid_t floorEnt = entityManagerAdd();
|
||||||
|
componentid_t floorPos = entityAddComponent(floorEnt, COMPONENT_TYPE_POSITION);
|
||||||
|
componentid_t floorMesh = entityAddComponent(floorEnt, COMPONENT_TYPE_MESH);
|
||||||
|
componentid_t floorMat = entityAddComponent(floorEnt, COMPONENT_TYPE_MATERIAL);
|
||||||
|
componentid_t floorPhys = entityAddComponent(floorEnt, COMPONENT_TYPE_PHYSICS);
|
||||||
|
|
||||||
|
entityPositionSetPosition(floorEnt, floorPos, (vec3){ -5.0f, 0.0f, -5.0f });
|
||||||
|
entityPositionSetScale(floorEnt, floorPos, (vec3){ 10.0f, 1.0f, 10.0f });
|
||||||
|
entityMeshSetMesh(floorEnt, floorMesh, &PLANE_MESH_SIMPLE);
|
||||||
|
entityMaterialGetShaderMaterial(floorEnt, floorMat)->unlit.color = COLOR_GREEN;
|
||||||
|
|
||||||
|
entityphysics_t *floorPhysData = entityPhysicsGet(floorEnt, floorPhys);
|
||||||
|
floorPhysData->type = PHYSICS_BODY_STATIC;
|
||||||
|
floorPhysData->shape.type = PHYSICS_SHAPE_PLANE;
|
||||||
|
floorPhysData->shape.data.plane.normal[0] = 0.0f;
|
||||||
|
floorPhysData->shape.data.plane.normal[1] = 1.0f;
|
||||||
|
floorPhysData->shape.data.plane.normal[2] = 0.0f;
|
||||||
|
floorPhysData->shape.data.plane.distance = 0.0f;
|
||||||
|
|
||||||
|
// Box
|
||||||
|
phBoxEnt = entityManagerAdd();
|
||||||
|
componentid_t boxPos = entityAddComponent(phBoxEnt, COMPONENT_TYPE_POSITION);
|
||||||
|
componentid_t boxMesh = entityAddComponent(phBoxEnt, COMPONENT_TYPE_MESH);
|
||||||
|
componentid_t boxMat = entityAddComponent(phBoxEnt, COMPONENT_TYPE_MATERIAL);
|
||||||
|
phBoxPhys = entityAddComponent(phBoxEnt, COMPONENT_TYPE_PHYSICS);
|
||||||
|
|
||||||
|
entityMeshSetMesh(phBoxEnt, boxMesh, &CUBE_MESH_SIMPLE);
|
||||||
|
entityMaterialGetShaderMaterial(phBoxEnt, boxMat)->unlit.color = COLOR_RED;
|
||||||
|
entityPositionSetPosition(phBoxEnt, boxPos, (vec3){ 0.0f, 4.0f, 0.0f });
|
||||||
|
|
||||||
// Run the initial script.
|
/* Run the init script. */
|
||||||
scriptcontext_t ctx;
|
scriptcontext_t ctx;
|
||||||
errorChain(scriptContextInit(&ctx));
|
errorChain(scriptContextInit(&ctx));
|
||||||
errorChain(scriptContextExecFile(&ctx, "init.lua"));
|
errorChain(scriptContextExecFile(&ctx, "init.lua"));
|
||||||
@@ -49,16 +197,38 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
errorret_t engineUpdate(void) {
|
errorret_t engineUpdate(void) {
|
||||||
|
// errorChain(networkUpdate());
|
||||||
|
|
||||||
timeUpdate();
|
timeUpdate();
|
||||||
inputUpdate();
|
inputUpdate();
|
||||||
|
|
||||||
uiUpdate();
|
uiUpdate();
|
||||||
errorChain(sceneUpdate());
|
errorChain(sceneUpdate());
|
||||||
mapUpdate();
|
|
||||||
|
/* Reset the box to its start position on demand. */
|
||||||
|
if(inputIsDown(INPUT_ACTION_ACCEPT)) {
|
||||||
|
componentid_t posComp = entityGetComponent(phBoxEnt, COMPONENT_TYPE_POSITION);
|
||||||
|
entityPositionSetPosition(phBoxEnt, posComp, (vec3){ 0.0f, 4.0f, 0.0f });
|
||||||
|
entityPhysicsSetVelocity(phBoxEnt, phBoxPhys, (vec3){ 0.0f, 0.0f, 0.0f });
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Step physics: positions are updated directly on POSITION components. */
|
||||||
|
physicsManagerUpdate();
|
||||||
|
|
||||||
|
errorChain(gameUpdate());
|
||||||
errorChain(displayUpdate());
|
errorChain(displayUpdate());
|
||||||
|
|
||||||
if(inputPressed(INPUT_ACTION_RAGEQUIT)) ENGINE.running = false;
|
if(inputPressed(INPUT_ACTION_RAGEQUIT)) ENGINE.running = false;
|
||||||
|
|
||||||
|
if(TIME.time >= onlineSwapTime) {
|
||||||
|
onlineSwapTime = FLT_MAX;
|
||||||
|
if(NETWORK.state == NETWORK_STATE_CONNECTED) {
|
||||||
|
goOffline();
|
||||||
|
} else {
|
||||||
|
goOnline();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,11 +237,13 @@ void engineExit(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
errorret_t engineDispose(void) {
|
errorret_t engineDispose(void) {
|
||||||
|
// errorChain(networkDispose());
|
||||||
sceneDispose();
|
sceneDispose();
|
||||||
mapDispose();
|
errorChain(gameDispose());
|
||||||
|
entityManagerDispose();
|
||||||
localeManagerDispose();
|
localeManagerDispose();
|
||||||
uiDispose();
|
uiDispose();
|
||||||
errorChain(displayDispose());
|
errorChain(displayDispose());
|
||||||
errorChain(assetDispose());
|
errorChain(assetDispose());
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
entity.c
|
||||||
|
entitymanager.c
|
||||||
|
component.c
|
||||||
|
)
|
||||||
|
|
||||||
|
# Subdirs
|
||||||
|
add_subdirectory(component)
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "entitymanager.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
|
componentdefinition_t COMPONENT_DEFINITIONS[] = {
|
||||||
|
[COMPONENT_TYPE_NULL] = { 0 },
|
||||||
|
|
||||||
|
#define X(enumName, type, field, iMethod, dMethod) \
|
||||||
|
[COMPONENT_TYPE_##enumName] = { .init = iMethod, .dispose = dMethod },
|
||||||
|
#include "componentlist.h"
|
||||||
|
#undef X
|
||||||
|
|
||||||
|
[COMPONENT_TYPE_COUNT] = { 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
void componentInit(
|
||||||
|
const entityid_t entityId,
|
||||||
|
const componentid_t componentId,
|
||||||
|
const componenttype_t type
|
||||||
|
) {
|
||||||
|
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
|
||||||
|
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
|
||||||
|
assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB");
|
||||||
|
assertTrue(type != COMPONENT_TYPE_NULL, "Cannot initialize null component");
|
||||||
|
|
||||||
|
componentindex_t index = componentGetIndex(entityId, componentId);
|
||||||
|
component_t *cmp = &ENTITY_MANAGER.components[index];
|
||||||
|
memoryZero(cmp, sizeof(component_t));
|
||||||
|
|
||||||
|
cmp->type = type;
|
||||||
|
if(COMPONENT_DEFINITIONS[type].init) {
|
||||||
|
COMPONENT_DEFINITIONS[type].init(entityId, componentId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void * componentGetData(
|
||||||
|
const entityid_t entityId,
|
||||||
|
const componentid_t componentId,
|
||||||
|
const componenttype_t type
|
||||||
|
) {
|
||||||
|
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
|
||||||
|
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
|
||||||
|
assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB");
|
||||||
|
assertTrue(type != COMPONENT_TYPE_NULL, "Cannot get data of null component");
|
||||||
|
|
||||||
|
componentindex_t index = componentGetIndex(entityId, componentId);
|
||||||
|
component_t *cmp = &ENTITY_MANAGER.components[index];
|
||||||
|
assertTrue(cmp->type == type, "Component type mismatch");
|
||||||
|
|
||||||
|
return &cmp->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
componentindex_t componentGetIndex(
|
||||||
|
const entityid_t entityId,
|
||||||
|
const componentid_t componentId
|
||||||
|
) {
|
||||||
|
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
|
||||||
|
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
|
||||||
|
return (entityId * ENTITY_COMPONENT_COUNT_MAX) + componentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
entityid_t componentGetEntitiesWithComponent(
|
||||||
|
const componenttype_t type,
|
||||||
|
entityid_t outEntities[ENTITY_COUNT_MAX],
|
||||||
|
componentid_t outComponents[ENTITY_COUNT_MAX]
|
||||||
|
) {
|
||||||
|
assertTrue(type < COMPONENT_TYPE_COUNT, "Component type OOB");
|
||||||
|
assertTrue(type != COMPONENT_TYPE_NULL, "Cannot check NULL type");
|
||||||
|
assertNotNull(outEntities, "Output entities array cannot be null");
|
||||||
|
assertNotNull(outComponents, "Output components array cannot be null");
|
||||||
|
|
||||||
|
entityid_t written = 0;
|
||||||
|
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
|
||||||
|
componentid_t used = ENTITY_MANAGER.entitiesWithComponent[
|
||||||
|
type * ENTITY_COUNT_MAX + i
|
||||||
|
];
|
||||||
|
if(used == 0xFF) continue;
|
||||||
|
assertTrue(
|
||||||
|
ENTITY_MANAGER.components[componentGetIndex(i, used)].type == type,
|
||||||
|
"Component type mismatch in entitiesWithComponent lookup"
|
||||||
|
);
|
||||||
|
assertTrue(
|
||||||
|
(ENTITY_MANAGER.entities[i].state & ENTITY_STATE_ACTIVE) != 0,
|
||||||
|
"Inactive entity in entitiesWithComponent lookup"
|
||||||
|
);
|
||||||
|
assertTrue(
|
||||||
|
used < ENTITY_COMPONENT_COUNT_MAX,
|
||||||
|
"Component ID OOB in entitiesWithComponent lookup"
|
||||||
|
);
|
||||||
|
assertTrue(
|
||||||
|
componentGetIndex(i, used) < ENTITY_COUNT_MAX * ENTITY_COMPONENT_COUNT_MAX,
|
||||||
|
"Component index OOB in entitiesWithComponent lookup"
|
||||||
|
);
|
||||||
|
assertTrue(
|
||||||
|
ENTITY_MANAGER.components[componentGetIndex(i,used)].type == type,
|
||||||
|
"Component type mismatch in entitiesWithComponent lookup"
|
||||||
|
);
|
||||||
|
outComponents[written] = used;
|
||||||
|
outEntities[written++] = i;
|
||||||
|
}
|
||||||
|
return written;
|
||||||
|
}
|
||||||
|
|
||||||
|
void componentDispose(
|
||||||
|
const entityid_t entityId,
|
||||||
|
const componentid_t componentId
|
||||||
|
) {
|
||||||
|
assertTrue(entityId < ENTITY_COUNT_MAX, "Entity ID OOB");
|
||||||
|
assertTrue(componentId < ENTITY_COMPONENT_COUNT_MAX, "Component ID OOB");
|
||||||
|
|
||||||
|
componentindex_t index = componentGetIndex(entityId, componentId);
|
||||||
|
component_t *cmp = &ENTITY_MANAGER.components[index];
|
||||||
|
if(cmp->type == COMPONENT_TYPE_NULL) return;
|
||||||
|
|
||||||
|
if(COMPONENT_DEFINITIONS[cmp->type].dispose) {
|
||||||
|
COMPONENT_DEFINITIONS[cmp->type].dispose(entityId, componentId);
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp->type = COMPONENT_TYPE_NULL;
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "entitybase.h"
|
||||||
|
|
||||||
|
#define X(enumName, type, field, init, dispose) \
|
||||||
|
// do nothing
|
||||||
|
#include "componentlist.h"
|
||||||
|
#undef X
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
#define X(enumName, type, field, init, dispose) type field;
|
||||||
|
#include "componentlist.h"
|
||||||
|
#undef X
|
||||||
|
} componentdata_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void (*init)(const entityid_t, const componentid_t);
|
||||||
|
void (*dispose)(const entityid_t, const componentid_t);
|
||||||
|
} componentdefinition_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
COMPONENT_TYPE_NULL,
|
||||||
|
|
||||||
|
#define X(enumName, type, field, init, dispose) \
|
||||||
|
COMPONENT_TYPE_##enumName,
|
||||||
|
#include "componentlist.h"
|
||||||
|
#undef X
|
||||||
|
|
||||||
|
COMPONENT_TYPE_COUNT
|
||||||
|
} componenttype_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
componenttype_t type;
|
||||||
|
componentdata_t data;
|
||||||
|
} component_t;
|
||||||
|
|
||||||
|
extern componentdefinition_t COMPONENT_DEFINITIONS[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a component of the given type for the entity with component ID.
|
||||||
|
*
|
||||||
|
* @param entityId The entity ID.
|
||||||
|
* @param componentId The component ID.
|
||||||
|
* @param type The type of the component to initialize.
|
||||||
|
*/
|
||||||
|
void componentInit(
|
||||||
|
const entityid_t entityId,
|
||||||
|
const componentid_t componentId,
|
||||||
|
const componenttype_t type
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the pointer to the data of a component for the entity with component ID.
|
||||||
|
*
|
||||||
|
* @param entityId The entity ID.
|
||||||
|
* @param componentId The component ID.
|
||||||
|
* @param type The type of the component to get, only used for assertion.
|
||||||
|
* @return A pointer to the component data.
|
||||||
|
*/
|
||||||
|
void * componentGetData(
|
||||||
|
const entityid_t entityId,
|
||||||
|
const componentid_t componentId,
|
||||||
|
const componenttype_t type
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the index of a component for the entity with component ID.
|
||||||
|
*
|
||||||
|
* @param entityId The entity ID.
|
||||||
|
* @param componentId The component ID.
|
||||||
|
* @return The index of the component in the component array.
|
||||||
|
*/
|
||||||
|
componentindex_t componentGetIndex(
|
||||||
|
const entityid_t entityId,
|
||||||
|
const componentid_t componentId
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the entity IDs of all entities with a component of the given type.
|
||||||
|
*
|
||||||
|
* @param type The type of the component to get entities for.
|
||||||
|
* @param outEntities An array to write the entity IDs to, must be at least
|
||||||
|
* ENTITY_COUNT_MAX in size.
|
||||||
|
* @param outComponents An array to write the component IDs to.
|
||||||
|
* @return The number of entity IDs written to outEntities.
|
||||||
|
*/
|
||||||
|
entityid_t componentGetEntitiesWithComponent(
|
||||||
|
const componenttype_t type,
|
||||||
|
entityid_t outEntities[ENTITY_COUNT_MAX],
|
||||||
|
componentid_t outComponents[ENTITY_COUNT_MAX]
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes of a component for the entity with component ID.
|
||||||
|
*
|
||||||
|
* @param entityId The entity ID.
|
||||||
|
* @param componentId The component ID.
|
||||||
|
*/
|
||||||
|
void componentDispose(
|
||||||
|
const entityid_t entityId,
|
||||||
|
const componentid_t componentId
|
||||||
|
);
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
add_subdirectory(display)
|
||||||
|
add_subdirectory(physics)
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
entityposition.c
|
||||||
|
entitycamera.c
|
||||||
|
entitymesh.c
|
||||||
|
entitymaterial.c
|
||||||
|
)
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user