Welcome to the Parin game engine tour. This page will go over each feature of the engine and provide examples of how to use them. If you notice anything missing or would like to contribute, feel free to create an issue!
This guide shows how to install Parin and its dependencies using DUB. To begin, make a new folder and run inside the following commands to create a new project:
-n
dub init dub run parin:setup
If everything is set up correctly, there should be some new files inside the folder. Three of them are particularly important:
Additionally, an app.d file is inside the source folder that looks like this:
import parin;
void ready() {
(320, 180);
lockResolution}
bool update(float dt) {
("Hello world!", Vec2(8));
drawDebugTextreturn false;
}
void finish() { }
mixin runGame!(ready, update, finish);
This code will create a window that displays the message “Hello world!”. Here is a breakdown of how it works:
The Ready Function
void ready() {
(320, 180);
lockResolution}
This function is the starting point of the game. It is called once when the game starts and, in this example, locks the game resolution to 320x180.
The Update Function
bool update(float dt) {
("Hello world!", Vec2(8));
drawDebugTextreturn false;
}
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 (8, 8). The return false
statement at
the end indicates that the game should continue running. If
true
were returned, then the game would stop
running.
The Finish Function
void finish() { }
This function is the ending point of the game. It is called once when the game ends and, in this example, does nothing.
The Mixin
mixin runGame!(ready, update, finish);
This mixin sets up a main function that opens a window and calls the ready, update and finish functions. By default, the window has a size of 960x540.
In essence, a Parin game typically relies on three functions:
To run the game, use the following command:
dub run
Parin consists of the following modules:
The parin.engine
module is the only mandatory module for
creating a game. All other modules are optional and can be included as
needed. The import parin;
statement in the example above is
a convenience import that includes all modules.
Parin provides a set of input functions inside the
parin.engine
module. These include:
bool isDown(char key);
bool isDown(Keyboard key);
bool isDown(Mouse key);
bool isDown(Gamepad key, int id = 0);
bool isPressed(char key);
bool isPressed(Keyboard key);
bool isPressed(Mouse key);
bool isPressed(Gamepad key, int id = 0);
bool isReleased(char key);
bool isReleased(Keyboard key);
bool isReleased(Mouse key);
bool isReleased(Gamepad key, int id = 0);
();
Keyboard dequeuePressedKeydchar dequeuePressedRune();
();
Vec2 wasd();
Vec2 wasdPressed();
Vec2 wasdReleased
();
Vec2 mouse();
Vec2 deltaMousefloat deltaWheel();
Below are examples showing how to use these input functions to move text.
Using the Mouse
bool update(float dt) {
("Text", mouse);
drawDebugTextreturn false;
}
Using the Arrow Keys
auto position = Vec2(8);
bool update(float dt) {
+= Keyboard.right.isDown - Keyboard.left.isDown;
position.x += Keyboard.down.isDown - Keyboard.up.isDown;
position.y ("Text", position);
drawDebugTextreturn false;
}
Using the WASD Keys
auto position = Vec2(8);
bool update(float dt) {
+= 'd'.isDown - 'a'.isDown;
position.x += 's'.isDown - 'w'.isDown;
position.y ("Text", position);
drawDebugTextreturn false;
}
Using the WASD or Arrow Keys
auto position = Vec2(8);
bool update(float dt) {
+= wasd;
position ("Text", position);
drawDebugTextreturn false;
}
Parin provides a set of drawing functions inside the
parin.engine
module. These include:
void drawRect(Rect area, Color color = white);
void drawHollowRect(Rect area, float thickness, Color color = white);
void drawCirc(Circ area, Color color = white);
void drawHollowCirc(Circ area, float thickness, Color color = white);
void drawVec2(Vec2 point, float size, Color color = white);
void drawLine(Line area, float size, Color color = white);
void drawTexture(Texture texture, Vec2 position, DrawOptions options = DrawOptions());
void drawTextureArea(Texture texture, Rect area, Vec2 position, DrawOptions options = DrawOptions());
void drawTexturePatch(Texture texture, Rect area, Rect target, bool isTiled, DrawOptions options = DrawOptions());
void drawViewport(Viewport viewport, Vec2 position, DrawOptions options = DrawOptions());
void drawViewportArea(Viewport viewport, Rect area, Vec2 position, DrawOptions options = DrawOptions());
void drawRune(Font font, dchar rune, Vec2 position, DrawOptions options = DrawOptions());
void drawText(Font font, IStr text, Vec2 position, DrawOptions options = DrawOptions());
void drawDebugText(IStr text, Vec2 position, DrawOptions options = DrawOptions());
Draw options are used for configuring drawing parameters. The data structure looks like this:
struct DrawOptions {
= Vec2(0.0f);
Vec2 origin = Vec2(1.0f);
Vec2 scale float rotation = 0.0f;
= white;
Color color = Hook.topLeft;
Hook hook = Flip.none;
Flip flip = Alignment.left;
Alignment alignment int alignmentWidth = 0;
float visibilityRatio = 1.0f;
bool isRightToLeft = false;
}
Here is a breakdown of what every option is:
Below are examples showing how to use these options to change how text looks.
Changing the Alignment
bool update(float dt) {
auto options = DrawOptions(Alignment.right);
("Hello.\nThis is some text.", Vec2(8), options);
drawDebugTextreturn false;
}
Changing the Size and Origin
bool update(float dt) {
auto options = DrawOptions(Hook.center);
= Vec2(4 + sin(elapsedTime * 4));
options.scale ("Text", resolution * Vec2(0.5), options);
drawDebugTextreturn false;
}
Changing the Visibility Ratio and Origin
bool update(float dt) {
auto options = DrawOptions(Hook.center);
= fmod(elapsedTime, 2.0);
options.visibilityRatio ("Hello.\nThis is some text.", resolution * Vec2(0.5), options);
drawDebugTextreturn false;
}
Parin provides a set of sound functions inside the
parin.engine
module.
void playSound(Sound sound);
void stopSound(Sound sound);
void pauseSound(Sound sound);
void resumeSound(Sound sound);
void updateSound(Sound sound);
Parin provides a set of loading and saving functions inside the
parin.engine
module. These include:
(IStr path);
TextureId loadTexture(IStr path, int size, int runeSpacing, int lineSpacing, IStr32 runes = "");
FontId loadFont(IStr path, int tileWidth, int tileHeight);
FontId loadFontFromTexture(IStr path, float volume, float pitch);
SoundId loadSound
!Texture loadRawTexture(IStr path);
Result!Font loadRawFont(IStr path, int size, int runeSpacing, int lineSpacing, IStr32 runes = "");
Result!Font loadRawFontFromTexture(IStr path, int tileWidth, int tileHeight);
Result!Sound loadRawSound(IStr path, float volume, float pitch);
Result
(IStr path, ref LStr buffer);
Fault loadRawTextIntoBuffer!LStr loadRawText(IStr path);
Result!IStr loadTempText(IStr path);
Result(IStr path, IStr text); Fault saveText
Functions that start with the word load or save will always try to read/write resources from/to the assets folder. They handle both forward slashes and backslashes in file paths, ensuring compatibility across operating systems. Additionally, resources are separated into three groups. Managed, raw and temporary.
Managed resources are cached by their path 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 are managed directly by the user and are not cached. They must be freed manually when no longer needed.
Temporary resources are only valid until the function that provided them is called again. They don’t need to be freed manually.
Sprites and tile maps can be implemented in various ways. To avoid
enforcing a specific approach, Parin provides optional modules for these
features, allowing users to include or omit them as needed. Parin
provides a sprite type inside the parin.sprite
module and a
tile map type inside the parin.map
module.