From 0c96ee0becb7aad08e7798b54a71a4609dd6c62c Mon Sep 17 00:00:00 2001 From: Kapendev Date: Tue, 3 Sep 2024 14:10:01 +0300 Subject: [PATCH] Some docs and some setup fixes. --- TOUR.md | 41 +++++++++++++++++++++++++++++++++++------ setup/source/app.d | 6 ++++++ source/popka/engine.d | 35 ++++++++++++++++++----------------- source/popka/sprite.d | 2 +- 4 files changed, 60 insertions(+), 24 deletions(-) diff --git a/TOUR.md b/TOUR.md index 44a6788..800bb0d 100644 --- a/TOUR.md +++ b/TOUR.md @@ -2,7 +2,7 @@ ## Understanding the Code -To begin, open the main file of your project and copy-paste the following code: +To begin, open the main project file and copy-paste the following code: ```d import popka; @@ -45,7 +45,7 @@ Here is a breakdown of how this code works: ``` This function is the main loop of the game. - It is called every frame while the game is running and, in this example, draws the message "Hello world!" at position `Vec2(8.0)`. + It is called every frame while the game is running and, in this example, draws the message "Hello world!" at position `Vec2(8)`. The `return false` statement indicates that the game should continue running. If `true` were returned, the game would stop running. @@ -84,14 +84,18 @@ void drawCirc(Circ area, Color color = white); void drawLine(Line area, float size, Color color = white); void drawTexture(Texture texture, Vec2 position, DrawOptions options = DrawOptions()); +void drawTexture(TextureId texture, Vec2 position, DrawOptions options = DrawOptions()); void drawTextureArea(Texture texture, Rect area, Vec2 position, DrawOptions options = DrawOptions()); +void drawTextureArea(TextureId texture, Rect area, Vec2 position, DrawOptions options = DrawOptions()); void drawRune(Font font, dchar rune, Vec2 position, DrawOptions options = DrawOptions()); +void drawRune(FontId font, dchar rune, Vec2 position, DrawOptions options = DrawOptions()); void drawText(Font font, IStr text, Vec2 position, DrawOptions options = DrawOptions()); +void drawText(FontId font, IStr text, Vec2 position, DrawOptions options = DrawOptions()); void drawDebugText(IStr text, Vec2 position, DrawOptions options = DrawOptions()); ``` -Additional drawing functions can be found in other modules, such as `popka.tilemap`. +Additional drawing functions can be found in other modules, such as `popka.sprite`. ## Loading and Saving Resources @@ -99,8 +103,33 @@ Functions that start with the word load/save will always try to read/write resou These functions handle both forward slashes and backslashes in file paths, ensuring compatibility across operating systems. ```d -loadText("levels/level5.txt"); // Works on Windows, Linux and MacOS. -loadText("levels\\level5.txt"); // Works on Windows, Linux and MacOS. +Result!TextId loadText(IStr path, Sz tag = 0); +Result!TextureId loadTexture(IStr path, Sz tag = 0); +Result!FontId loadFont(IStr path, int size, int runeSpacing, int lineSpacing, const(dchar)[] runes = [], Sz tag = 0); +Result!SoundId loadSound(IStr path, float volume, float pitch, Sz tag = 0); + +Result!LStr loadRawText(IStr path); +Result!Texture loadRawTexture(IStr path); +Result!Font loadRawFont(IStr path, int size, int runeSpacing, int lineSpacing, const(dchar)[] runes = []); +Result!Sound loadRawSound(IStr path, float volume, float pitch); + +Result!IStr loadTempText(IStr path); + +Fault saveText(IStr path, IStr text); ``` -If text is needed for only a single frame, use the `loadTempText` function. +### Managed Resources + +Managed resources are cached by their path and grouped based on the tag they were loaded with. +To free these resources, use the `freeResources` function or the `free` method on the resource identifier. +The resource identifier is automatically invalidated when the resource is freed. + +### Raw Resources + +Raw resources are managed directly by the user and are not cached or grouped. +They must be freed manually when no longer needed. + +### Temporary Resources + +Temporary resources are only valid until the function that provided them is called again. +They don’t need to be freed manually. diff --git a/setup/source/app.d b/setup/source/app.d index 198e98b..4ce3a13 100755 --- a/setup/source/app.d +++ b/setup/source/app.d @@ -104,6 +104,12 @@ int main(string[] args) { auto isDubProject = exists(dubFile); auto isFirstRun = !exists(assetsDir); + // Sometimes I remove the app.d file and this makes a new one lol. + // Also raylib-d:install does not like it when you don't have one. + if (isDubProject) { + if (!exists(appFile)) std.file.write(appFile, appFileContent); + } + // Use the raylib-d script to download the raylib library files. if (isDubProject) { writeln("\n Simply say \"yes\" to all prompts. \n"); diff --git a/source/popka/engine.d b/source/popka/engine.d index e70e64e..8c398b6 100644 --- a/source/popka/engine.d +++ b/source/popka/engine.d @@ -701,93 +701,93 @@ struct EngineState { } } -/// Converts a raylib color to a Popka color. +private Color toPopka(rl.Color from) { return Color(from.r, from.g, from.b, from.a); } -/// Converts a raylib vector to a Popka vector. +private Vec2 toPopka(rl.Vector2 from) { return Vec2(from.x, from.y); } -/// Converts a raylib vector to a Popka vector. +private Vec3 toPopka(rl.Vector3 from) { return Vec3(from.x, from.y, from.z); } -/// Converts a raylib vector to a Popka vector. +private Vec4 toPopka(rl.Vector4 from) { return Vec4(from.x, from.y, from.z, from.w); } -/// Converts a raylib rectangle to a Popka rectangle. +private Rect toPopka(rl.Rectangle from) { return Rect(from.x, from.y, from.width, from.height); } -/// Converts a raylib texture to a Popka texture. +private Texture toPopka(rl.Texture2D from) { auto result = Texture(); result.data = from; return result; } -/// Converts a raylib font to a Popka font. +private Font toPopka(rl.Font from) { auto result = Font(); result.data = from; return result; } -/// Converts a raylib render texture to a Popka viewport. +private Viewport toPopka(rl.RenderTexture2D from) { auto result = Viewport(); result.data = from; return result; } -/// Converts a Popka color to a raylib color. +private rl.Color toRl(Color from) { return rl.Color(from.r, from.g, from.b, from.a); } -/// Converts a Popka vector to a raylib vector. +private rl.Vector2 toRl(Vec2 from) { return rl.Vector2(from.x, from.y); } -/// Converts a Popka vector to a raylib vector. +private rl.Vector3 toRl(Vec3 from) { return rl.Vector3(from.x, from.y, from.z); } -/// Converts a Popka vector to a raylib vector. +private rl.Vector4 toRl(Vec4 from) { return rl.Vector4(from.x, from.y, from.z, from.w); } -/// Converts a Popka rectangle to a raylib rectangle. +private rl.Rectangle toRl(Rect from) { return rl.Rectangle(from.position.x, from.position.y, from.size.x, from.size.y); } -/// Converts a Popka texture to a raylib texture. +private rl.Texture2D toRl(Texture from) { return from.data; } -/// Converts a Popka font to a raylib font. +private rl.Font toRl(Font from) { return from.data; } -/// Converts a Popka viewport to a raylib render texture. +private rl.RenderTexture2D toRl(Viewport from) { return from.data; } -/// Converts a Popka filter to a raylib filter. +private int toRl(Filter filter) { final switch (filter) { case Filter.nearest: return rl.TEXTURE_FILTER_POINT; @@ -795,6 +795,7 @@ int toRl(Filter filter) { } } +private rl.Camera2D toRl(Camera camera) { return rl.Camera2D( Rect(resolution).origin(camera.isCentered ? Hook.center : Hook.topLeft).toRl(), diff --git a/source/popka/sprite.d b/source/popka/sprite.d index 3c42788..a4c0d2a 100644 --- a/source/popka/sprite.d +++ b/source/popka/sprite.d @@ -6,7 +6,7 @@ // Version: v0.0.18 // --- -/// The `sprite` module provides a simple and extensible sprite. +/// The `sprite` module provides a simple and flexible sprite. module popka.sprite; import popka.engine;