#Help structuring isometric rendering code

1 messages · Page 1 of 1 (latest)

glossy palm
#

Code in comments because too many characters
I have this simple test renderer for isometric tiles, but I hate that I have to convert from int to floats every time I want to transform the position of a tile to its isometric position in the screen. Also I don't know if the loop is correctly done, since some raylib functions take floats, and others take ints to do the same things like drawing a rectangle, which is confusing.

Can someone just look over the code and see if it's fine, or if there is a better way to structure it in the future? Thanks 🙂

#
const std = @import("std");
const rl = @import("rl.zig");
const utils = @import("utils.zig");

const tileWidth: comptime_int = 32;
const tileHeight: comptime_int = 16;

const block = struct {
    const tile: u16 = undefined;
    const solid: bool = undefined;
};

const chunk = struct {
    const blocks: [16 * 16]block = undefined;
};

const world = struct {
    const chunks: [16 * 16]chunk = undefined;
};

inline fn isoTransform(x: i32, y: i32) rl.Vector2 {
    const isoX = (x - y) * (tileWidth / 2);
    const isoY = (x + y) * (tileHeight / 2);
    return rl.Vector2{ .x = @floatFromInt(isoX), .y = @floatFromInt(isoY) };
}

pub fn main() anyerror!void {
    // Initialization
    //--------------------------------------------------------------------------------------
    const screenWidth = 800;
    const screenHeight = 450;

    rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - sprite sheet rendering");
    defer rl.CloseWindow();


#
    // Load the tileset texture
    const tilesetTexture = rl.LoadTexture("src/resources/green_tile.png");
    defer rl.UnloadTexture(tilesetTexture);

    // Main game loop
    while (!rl.WindowShouldClose()) {
        // Update
        //----------------------------------------------------------------------------------
        // TODO: Update game logic here
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        rl.BeginDrawing();
        defer rl.EndDrawing();

        rl.ClearBackground(rl.RAYWHITE);

        for (comptime utils.i32Range(0, 16)) |x| {
            for (comptime utils.i32Range(0, 3)) |y| {
                // Define the source rectangle for the top face
                const sourceRect = rl.Rectangle{
                    .x = 0,
                    .y = 0,
                    .width = tileWidth,
                    .height = tileHeight,
                };

                const isoCoords: rl.Vector2 = isoTransform(x, y);

                // Define the destination rectangle for the top face (where to draw on screen)
                const destRect = rl.Rectangle{
                    .x = isoCoords.x,
                    .y = isoCoords.y,
                    .width = tileWidth,
                    .height = tileHeight,
                };

                // Render the top face
                rl.DrawTexturePro(tilesetTexture, sourceRect, destRect, rl.Vector2{ .x = 0, .y = 0 }, 0, rl.WHITE);
            }
        }
    }
}```