New example and small changes.

This commit is contained in:
Kapendev 2024-11-02 22:11:09 +02:00
parent eb07b43af1
commit 8f084526e1
5 changed files with 37 additions and 22 deletions

View file

@ -35,6 +35,10 @@ This example shows how to create an animated character that follows the mouse.
This example shows how to use the timer structure of Parin.
## [Viewport](viewport.d)
This example shows how to use the viewport structure of Parin.
## [Scene](scene.d)
This example shows how to use the scene manager of Parin.

View file

@ -19,7 +19,6 @@ void ready() {
bool update(float dt) {
// The sprite should be updated every frame, regardless of whether it is running.
sprite.update(dt);
// Get some basic info about the mouse.
auto mouseDistance = sprite.position.distanceTo(mouseScreenPosition);
auto mouseDirection = sprite.position.directionTo(mouseScreenPosition);

View file

@ -15,10 +15,7 @@ bool update(float dt) {
// The timer should be updated every frame, regardless of whether it is running.
timer.update(dt);
// Check if the timer has stopped and add 1 to the counter.
if (timer.hasStopped) {
counter += 1;
}
if (timer.hasStopped) counter += 1;
drawDebugText("Counter: {}".format(counter), Vec2(8));
drawDebugText("\nTimer: {}".format(timer.time), Vec2(8));
return false;

27
examples/viewport.d Normal file
View file

@ -0,0 +1,27 @@
/// This example shows how to use the viewport structure of Parin.
import parin;
// The game variables.
auto viewport = Viewport(black);
void ready() {
// Resize the viewport based on the current resolution.
viewport.resize(resolutionWidth / 2, resolutionHeight / 2);
}
bool update(float dt) {
// Resize the viewport when the window is resized.
if (isWindowResized) viewport.resize(resolutionWidth / 2, resolutionHeight / 2);
// Draw the mouse position inside the viewport.
viewport.attach();
drawVec2(mouseScreenPosition - Rect(resolution * Vec2(0.5), viewport.size).centerArea.position, 24);
viewport.detach();
// Draw the viewport and other things inside the window.
drawViewport(viewport, resolution * Vec2(0.5), DrawOptions(Hook.center));
drawDebugText("Move the mouse inside the black box.", Vec2(8), DrawOptions(Vec2(2)));
return false;
}
void finish() { }
mixin runGame!(ready, update, finish);

View file

@ -555,17 +555,16 @@ struct SoundId {
/// Represents the viewing area for rendering.
struct Viewport {
rl.RenderTexture2D data;
Color color = gray;
Color color;
Blend blend;
bool isAttached;
@safe @nogc nothrow:
/// Initializes the viewport with the given size, background color and blend mode.
this(int width, int height, Color color = gray, Blend blend = Blend.alpha) {
this(Color color, Blend blend = Blend.alpha) {
this.color = color;
this.blend = blend;
resize(width, height);
}
/// Checks if the viewport is not loaded.
@ -1956,12 +1955,7 @@ void drawLine(Line area, float size, Color color = white) {
/// Draws a portion of the specified texture at the given position with the specified draw options.
@trusted
void drawTextureArea(Texture texture, Rect area, Vec2 position, DrawOptions options = DrawOptions()) {
if (texture.isEmpty) {
return;
} else if (area.size.x <= 0.0f || area.size.y <= 0.0f) {
return;
}
if (texture.isEmpty || area.size.x <= 0.0f || area.size.y <= 0.0f) return;
auto target = Rect(position, area.size * options.scale.abs());
auto flip = options.flip;
if (options.scale.x < 0.0f && options.scale.y < 0.0f) {
@ -2035,10 +2029,7 @@ void drawViewport(Viewport viewport, Vec2 position, DrawOptions options = DrawOp
/// Draws a single character from the specified font at the given position with the specified draw options.
@trusted
void drawRune(Font font, dchar rune, Vec2 position, DrawOptions options = DrawOptions()) {
if (font.isEmpty) {
return;
}
if (font.isEmpty) return;
auto rect = toParin(rl.GetGlyphAtlasRec(font.data, rune));
auto origin = options.origin == Vec2() ? rect.origin(options.hook) : options.origin;
rl.rlPushMatrix();
@ -2066,10 +2057,7 @@ void drawRune(FontId font, dchar rune, Vec2 position, DrawOptions options = Draw
/// Draws the specified text with the given font at the given position using the provided draw options.
@trusted
void drawText(Font font, IStr text, Vec2 position, DrawOptions options = DrawOptions()) {
if (font.isEmpty || text.length == 0) {
return;
}
if (font.isEmpty || text.length == 0) return;
// TODO: Make it work with negative scale values.
auto origin = Rect(measureTextSize(font, text)).origin(options.hook);
rl.rlPushMatrix();