From 547ffedb6daeecc1fd38d1d645c1a226b5150554 Mon Sep 17 00:00:00 2001 From: Ki Rill Date: Fri, 21 Apr 2023 10:47:14 +0600 Subject: [PATCH] lesson#27 - in, out, inout type qualifiers added --- .../ourGame/source/game/menu.d | 34 --------- .../main.d | 66 ++++++++++++++++++ .../ourGame/.gitignore | 0 .../ourGame/dub.json | 0 .../ourGame/dub.selections.json | 0 .../ourGame/raylib.dll | Bin .../ourGame/raylib.lib | Bin .../ourGame/source/app.d | 0 .../source/game/animation}/animation.d | 6 +- .../ourGame/source/game/animation/manager.d | 10 +-- .../ourGame/source/game/data.d | 4 +- .../ourGame/source/game/gstatemanager.d | 0 .../ourGame/source/game/menu.d | 34 +++++++++ .../ourGame/source/game/play.d | 0 .../ourGame/source/game/player.d | 6 +- 15 files changed, 113 insertions(+), 47 deletions(-) delete mode 100644 lesson#27 - Player movement and animation/ourGame/source/game/menu.d create mode 100644 lesson#27 - in, out, inout type qualifiers/main.d rename {lesson#27 - Player movement and animation => lesson#28 - Player movement and animation}/ourGame/.gitignore (100%) rename {lesson#27 - Player movement and animation => lesson#28 - Player movement and animation}/ourGame/dub.json (100%) rename {lesson#27 - Player movement and animation => lesson#28 - Player movement and animation}/ourGame/dub.selections.json (100%) rename {lesson#27 - Player movement and animation => lesson#28 - Player movement and animation}/ourGame/raylib.dll (100%) rename {lesson#27 - Player movement and animation => lesson#28 - Player movement and animation}/ourGame/raylib.lib (100%) rename {lesson#27 - Player movement and animation => lesson#28 - Player movement and animation}/ourGame/source/app.d (100%) rename {lesson#27 - Player movement and animation/ourGame/source/game => lesson#28 - Player movement and animation/ourGame/source/game/animation}/animation.d (87%) rename lesson#27 - Player movement and animation/ourGame/source/game/animationmanager.d => lesson#28 - Player movement and animation/ourGame/source/game/animation/manager.d (70%) rename {lesson#27 - Player movement and animation => lesson#28 - Player movement and animation}/ourGame/source/game/data.d (84%) rename {lesson#27 - Player movement and animation => lesson#28 - Player movement and animation}/ourGame/source/game/gstatemanager.d (100%) create mode 100644 lesson#28 - Player movement and animation/ourGame/source/game/menu.d rename {lesson#27 - Player movement and animation => lesson#28 - Player movement and animation}/ourGame/source/game/play.d (100%) rename {lesson#27 - Player movement and animation => lesson#28 - Player movement and animation}/ourGame/source/game/player.d (89%) diff --git a/lesson#27 - Player movement and animation/ourGame/source/game/menu.d b/lesson#27 - Player movement and animation/ourGame/source/game/menu.d deleted file mode 100644 index 9fbc5a3..0000000 --- a/lesson#27 - Player movement and animation/ourGame/source/game/menu.d +++ /dev/null @@ -1,34 +0,0 @@ -module game.menu; - -import game.data; -import game.gstatemanager; - -class Menu: IState { - this() {} - - // inherited from IState interface - void run() { - update(); - processEvents(); - render(); - } - - void update() {} - - void processEvents() { - if(IsKeyPressed(KeyboardKey.KEY_P)) { - GStateManager.getInstance.setState(GameState.Play); - } - } - - void render() { - // enable drawing - BeginDrawing(); scope(exit) { EndDrawing(); } - - // clear background - ClearBackground(Colors.GREEN); - - // draw - // ... - } -} diff --git a/lesson#27 - in, out, inout type qualifiers/main.d b/lesson#27 - in, out, inout type qualifiers/main.d new file mode 100644 index 0000000..79c0feb --- /dev/null +++ b/lesson#27 - in, out, inout type qualifiers/main.d @@ -0,0 +1,66 @@ +module main; + +import std.stdio; + +void main() { + int kg = 5; + int g = kg2g(kg); + + writeln("kg: ", kg); + writeln(" g: ", g); + + // --- --- --- + writeln("--- --- ---"); + + int remainder = 0; + int result = divide(10, 4, remainder); + + writeln(" result: ", result); + writeln("remainder: ", remainder); + + // --- --- --- + writeln("--- --- ---"); + + int[] array = [1, 2, 3, 4]; + const int[] constArray = [5, 6, 7, 8]; + immutable int[] immutableArray = [9, 10, 11, 12]; + + int[] arrayResult = doSomething(array); + const int[] constArrayResult = doSomething(constArray); + immutable int[] immutableArrayResult = doSomething(immutableArray); + + int[] arr = doSomething(constArray); // error! cannot do implicit conversion between type qualifiers + // Error: cannot implicitly convert expression `doSomething(constArray)` of type `const(int)[]` to `int[]` + + writeln(" a: ", arrayResult); + writeln("ca: ", constArrayResult); + writeln("ia: ", immutableArrayResult); +} + +/// kilogram to gram converter +int kg2g(in int kg) { + // kg = 2; // error, cannot modify, read-only + + int g = kg * 1000; + return g; +} + +/// division and calculates remainder +int divide(in int value, in int divisor, out int remainder) { + assert(remainder == int.init); // remainder is for output, it is initialized to 0 + + // calculate + remainder = value % divisor; + int result = value / divisor; + + return result; +} + +/// imagine it does something useful +inout(int)[] doSomething(inout(int)[] values) { + // do some work + return values; +} + + + diff --git a/lesson#27 - Player movement and animation/ourGame/.gitignore b/lesson#28 - Player movement and animation/ourGame/.gitignore similarity index 100% rename from lesson#27 - Player movement and animation/ourGame/.gitignore rename to lesson#28 - Player movement and animation/ourGame/.gitignore diff --git a/lesson#27 - Player movement and animation/ourGame/dub.json b/lesson#28 - Player movement and animation/ourGame/dub.json similarity index 100% rename from lesson#27 - Player movement and animation/ourGame/dub.json rename to lesson#28 - Player movement and animation/ourGame/dub.json diff --git a/lesson#27 - Player movement and animation/ourGame/dub.selections.json b/lesson#28 - Player movement and animation/ourGame/dub.selections.json similarity index 100% rename from lesson#27 - Player movement and animation/ourGame/dub.selections.json rename to lesson#28 - Player movement and animation/ourGame/dub.selections.json diff --git a/lesson#27 - Player movement and animation/ourGame/raylib.dll b/lesson#28 - Player movement and animation/ourGame/raylib.dll similarity index 100% rename from lesson#27 - Player movement and animation/ourGame/raylib.dll rename to lesson#28 - Player movement and animation/ourGame/raylib.dll diff --git a/lesson#27 - Player movement and animation/ourGame/raylib.lib b/lesson#28 - Player movement and animation/ourGame/raylib.lib similarity index 100% rename from lesson#27 - Player movement and animation/ourGame/raylib.lib rename to lesson#28 - Player movement and animation/ourGame/raylib.lib diff --git a/lesson#27 - Player movement and animation/ourGame/source/app.d b/lesson#28 - Player movement and animation/ourGame/source/app.d similarity index 100% rename from lesson#27 - Player movement and animation/ourGame/source/app.d rename to lesson#28 - Player movement and animation/ourGame/source/app.d diff --git a/lesson#27 - Player movement and animation/ourGame/source/game/animation.d b/lesson#28 - Player movement and animation/ourGame/source/game/animation/animation.d similarity index 87% rename from lesson#27 - Player movement and animation/ourGame/source/game/animation.d rename to lesson#28 - Player movement and animation/ourGame/source/game/animation/animation.d index cdfba37..35925e1 100644 --- a/lesson#27 - Player movement and animation/ourGame/source/game/animation.d +++ b/lesson#28 - Player movement and animation/ourGame/source/game/animation/animation.d @@ -1,4 +1,4 @@ -module game.animation; +module game.animation.animation; import raylib; @@ -13,7 +13,7 @@ struct Animation { bool isActive; } - this(const ref Texture2D texture, const int numFrames, const int numVerticalFrames, const float frameTimeSecs, const int row = 1) { + this(in Texture2D texture, in int numFrames, in int numVerticalFrames, in float frameTimeSecs, in int row = 1) { this.texture = texture; this.numFrames = numFrames; this.frameTimeSecs = frameTimeSecs; @@ -54,7 +54,7 @@ struct Animation { } } - void draw(const Vector2 position) { + void draw(in Vector2 position) { DrawTextureRec(texture, rectFrames[currentFrame], position, Colors.WHITE); } } diff --git a/lesson#27 - Player movement and animation/ourGame/source/game/animationmanager.d b/lesson#28 - Player movement and animation/ourGame/source/game/animation/manager.d similarity index 70% rename from lesson#27 - Player movement and animation/ourGame/source/game/animationmanager.d rename to lesson#28 - Player movement and animation/ourGame/source/game/animation/manager.d index b63793d..2655f0f 100644 --- a/lesson#27 - Player movement and animation/ourGame/source/game/animationmanager.d +++ b/lesson#28 - Player movement and animation/ourGame/source/game/animation/manager.d @@ -1,18 +1,18 @@ -module game.animationmanager; +module game.animation.manager; import raylib; -import game.animation; +import game.animation.animation; struct AnimationManager { private Animation[KeyboardKey] animations; private KeyboardKey lastKey; - void addAnimation(KeyboardKey key, Animation animation) { + void addAnimation(in KeyboardKey key, Animation animation) { animations[key] = animation; lastKey = key; } - void update(KeyboardKey key) { + void update(in KeyboardKey key) { if(key in animations) { animations[key].start(); animations[key].update(); @@ -23,7 +23,7 @@ struct AnimationManager { } } - void draw(const Vector2 position) { + void draw(in Vector2 position) { animations[lastKey].draw(position); } } \ No newline at end of file diff --git a/lesson#27 - Player movement and animation/ourGame/source/game/data.d b/lesson#28 - Player movement and animation/ourGame/source/game/data.d similarity index 84% rename from lesson#27 - Player movement and animation/ourGame/source/game/data.d rename to lesson#28 - Player movement and animation/ourGame/source/game/data.d index 2c5e484..2858fd8 100644 --- a/lesson#27 - Player movement and animation/ourGame/source/game/data.d +++ b/lesson#28 - Player movement and animation/ourGame/source/game/data.d @@ -26,13 +26,13 @@ class Entity { Rectangle frame; Vector2 position; - this(const ref Texture2D texture, const Rectangle frame, const Vector2 Position) { + this(in Texture2D texture, in Rectangle frame, in Vector2 Position) { this.texture = texture; this.frame = frame; this.position = position; } - void move(const float x, const float y) { + void move(in float x, in float y) { position.x += x; position.y += y; } diff --git a/lesson#27 - Player movement and animation/ourGame/source/game/gstatemanager.d b/lesson#28 - Player movement and animation/ourGame/source/game/gstatemanager.d similarity index 100% rename from lesson#27 - Player movement and animation/ourGame/source/game/gstatemanager.d rename to lesson#28 - Player movement and animation/ourGame/source/game/gstatemanager.d diff --git a/lesson#28 - Player movement and animation/ourGame/source/game/menu.d b/lesson#28 - Player movement and animation/ourGame/source/game/menu.d new file mode 100644 index 0000000..6ccf0d9 --- /dev/null +++ b/lesson#28 - Player movement and animation/ourGame/source/game/menu.d @@ -0,0 +1,34 @@ +module game.menu; + +import game.data; +import game.gstatemanager; + +class Menu: IState { + this() {} + + // inherited from IState interface + void run() { + update(); + processEvents(); + render(); + } + + void update() {} + + void processEvents() { + if(IsKeyPressed(KeyboardKey.KEY_P)) { + GStateManager.getInstance.setState(GameState.Play); + } + } + + void render() { + // enable drawing + BeginDrawing(); scope(exit) { EndDrawing(); } + + // clear background + ClearBackground(Colors.GREEN); + + // draw + // ... + } +} diff --git a/lesson#27 - Player movement and animation/ourGame/source/game/play.d b/lesson#28 - Player movement and animation/ourGame/source/game/play.d similarity index 100% rename from lesson#27 - Player movement and animation/ourGame/source/game/play.d rename to lesson#28 - Player movement and animation/ourGame/source/game/play.d diff --git a/lesson#27 - Player movement and animation/ourGame/source/game/player.d b/lesson#28 - Player movement and animation/ourGame/source/game/player.d similarity index 89% rename from lesson#27 - Player movement and animation/ourGame/source/game/player.d rename to lesson#28 - Player movement and animation/ourGame/source/game/player.d index 72b3f42..0212019 100644 --- a/lesson#27 - Player movement and animation/ourGame/source/game/player.d +++ b/lesson#28 - Player movement and animation/ourGame/source/game/player.d @@ -1,14 +1,14 @@ module game.player; import game.data; -import game.animation; -import game.animationmanager; +import game.animation.animation; +import game.animation.manager; class Player: Entity { private int playerSpeed; private AnimationManager animation; - this(const ref Texture2D texture, const Rectangle frame, const Vector2 position, const int playerSpeed) { + this(in Texture2D texture, in Rectangle frame, in Vector2 position, in int playerSpeed) { super(texture, frame, position); this.playerSpeed = playerSpeed;