add BoundingBox and Image

This commit is contained in:
Jeeves 2025-05-28 10:02:30 -06:00
parent 793c4d7211
commit bfca7a4d92

View file

@ -3,47 +3,57 @@ pub fn main() !void {
defer _ = gpa.deinit();
const allocator = gpa.allocator();
raylib.SetConfigFlags(raylib.FLAG_VSYNC_HINT); // | raylib.FLAG_WINDOW_RESIZABLE);
raylib.SetConfigFlags(raylib.FLAG_VSYNC_HINT | raylib.FLAG_WINDOW_RESIZABLE);
raylib.InitWindow(@intFromFloat(screen_width), @intFromFloat(screen_height), "ReX");
defer raylib.CloseWindow();
raylib.SetWindowMinSize(480, 272);
raylib.SetWindowMaxSize(1920, 1080);
scales.recalculate();
// raylib.SetWindowMinSize(480, 272);
// raylib.SetWindowMaxSize(1920, 1080);
// scales.recalculate();
global_font = raylib.LoadFontEx("font/SCE-PS3-RD-R-LATIN.TTF", 32, 0, 250);
raylib.SetTextureFilter(global_font.texture, raylib.TEXTURE_FILTER_TRILINEAR);
// global_font = raylib.LoadFontEx("font/SCE-PS3-RD-R-LATIN.TTF", 32, 0, 250);
// raylib.SetTextureFilter(global_font.texture, raylib.TEXTURE_FILTER_TRILINEAR);
var background = Background.init();
var column = Column.init(
allocator,
raylib.LoadTextureFromImage(raylib.GenImageChecked(64, 64, 8, 8, raylib.BLACK, raylib.WHITE)),
"Game",
);
defer column.deinit();
// var background = Background.init();
// var column = Column.init(
// allocator,
// raylib.LoadTextureFromImage(raylib.GenImageChecked(64, 64, 8, 8, raylib.BLACK, raylib.WHITE)),
// "Game",
// );
// defer column.deinit();
var item = Item.init(
raylib.LoadTexture("menu/game/CometCrash/ICON0.PNG"),
"Comet Crash",
"",
);
try column.appendItem(&item);
// var item = Item.init(
// raylib.LoadTexture("menu/game/CometCrash/ICON0.PNG"),
// "Comet Crash",
// "",
// );
// try column.appendItem(&item);
var image = Image{
.box = .{ .x = 2, .y = 2, .w = screen_width - 4, .h = screen_height - 4 },
.texture = raylib.LoadTextureFromImage(raylib.GenImageChecked(256, 128, 8, 8, raylib.WHITE, raylib.BLACK)),
};
raylib.SetTargetFPS(120);
while (!raylib.WindowShouldClose()) {
if (raylib.IsWindowResized()) {
screen_width = @floatFromInt(raylib.GetScreenWidth());
screen_height = @floatFromInt(raylib.GetScreenHeight());
scales.recalculate();
image.box.w = screen_width - 4;
image.box.h = screen_height - 4;
// scales.recalculate();
}
if (raylib.IsKeyPressed('Z')) item.setBig(!item.large);
// if (raylib.IsKeyPressed('Z')) item.setBig(!item.large);
if (raylib.IsKeyPressed('S')) raylib.TakeScreenshot("screenshot.png");
raylib.BeginDrawing();
defer raylib.EndDrawing();
background.draw();
column.draw();
// background.draw();
// column.draw();
raylib.ClearBackground(raylib.BLACK);
image.draw();
raylib.DrawFPS(1, 1);
@ -276,6 +286,51 @@ pub const Background = struct {
};
};
pub const BoundingBox = struct {
x: f32 = 0,
y: f32 = 0,
w: f32,
h: f32,
};
/// A texture within a bounding box. Positions and scales based on configurable parameters.
pub const Image = struct {
box: BoundingBox,
texture: raylib.Texture2D,
mode: Mode = .fit,
align_w: Align = .center,
align_h: Align = .center,
pub fn draw(self: *Image) void {
const width: f32 = @floatFromInt(self.texture.width);
const height: f32 = @floatFromInt(self.texture.height);
const width_scale: f32 = if (width == 0) 0.0 else self.box.w / width;
const height_scale: f32 = if (height == 0) 0.0 else self.box.h / height;
const scale = switch (self.mode) {
.original => 1.0,
.fit => @min(width_scale, height_scale),
.fill => @max(width_scale, height_scale),
};
const position = raylib.Vector2{
.x = switch (self.align_w) {
.left => 0,
.center => self.box.x + self.box.w / 2.0 - (width * scale) / 2.0,
.right => self.box.x + self.box.w - width * scale,
},
.y = switch (self.align_h) {
.left => 0,
.center => self.box.y + self.box.h / 2.0 - (height * scale) / 2.0,
.right => self.box.y + self.box.h - height * scale,
},
};
raylib.DrawTextureEx(self.texture, position, 0, scale, raylib.WHITE);
}
pub const Mode = enum { original, fit, fill };
pub const Align = enum { left, center, right };
};
/// Create ortho camera looking down at the XY plane, with a fovy of 1 for UV-like positioning.
fn createCamera() raylib.Camera3D {
var camera = raylib.Camera3D{};