rex/src/main.zig
2025-05-28 08:44:41 -06:00

307 lines
9.6 KiB
Zig

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
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();
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 item = Item.init(
raylib.LoadTexture("menu/game/CometCrash/ICON0.PNG"),
"Comet Crash",
"",
);
try column.appendItem(&item);
raylib.SetTargetFPS(120);
while (!raylib.WindowShouldClose()) {
if (raylib.IsWindowResized()) {
screen_width = @floatFromInt(raylib.GetScreenWidth());
screen_height = @floatFromInt(raylib.GetScreenHeight());
scales.recalculate();
}
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();
raylib.DrawFPS(1, 1);
const debug_text = try std.fmt.allocPrint(allocator, "screen size = {d}x{d}", .{ screen_width, screen_height });
defer allocator.free(debug_text);
raylib.DrawText(@ptrCast(debug_text), 80, 2, 8, raylib.GREEN);
}
}
var global_font: raylib.Font = undefined;
var screen_width: f32 = 480;
var screen_height: f32 = 272;
var scales: Scales = undefined;
/// Cached scaling and positioning values for dynamic window resizing.
pub const Scales = struct {
item_icon_small_scale: f32,
item_icon_large_scale: f32,
item_title_font_size: f32,
item_subtitle_font_size: f32,
column_icon_scale: f32,
column_title_font_size: f32,
column_position_center: raylib.Vector2,
column_position_spacing: f32,
/// Recalculate scales after screen resize.
pub fn recalculate(self: *Scales) void {
self.item_icon_small_scale = 0.272727;
self.item_icon_large_scale = 0.272727;
self.item_title_font_size = 16;
self.column_icon_scale = 0.75;
self.column_title_font_size = 13;
self.column_position_center = .{
.x = std.math.lerp(0.0, screen_width, 0.18),
.y = std.math.lerp(0.0, screen_height, 0.15),
};
self.column_position_spacing = 64;
}
};
pub const Column = struct {
icon: raylib.Texture2D,
title: []const u8,
items: std.ArrayList(*Item),
pub fn init(allocator: Allocator, icon: raylib.Texture2D, title: []const u8) Column {
raylib.SetTextureFilter(icon, raylib.TEXTURE_FILTER_BILINEAR);
return .{
.icon = icon,
.title = title,
.items = .init(allocator),
};
}
pub fn deinit(self: *Column) void {
self.items.deinit();
}
pub fn draw(self: *Column) void {
const icon_position = scales.column_position_center;
const icon_scale = scales.column_icon_scale;
const icon_width = @as(f32, @floatFromInt(self.icon.width)) * icon_scale;
const icon_height = @as(f32, @floatFromInt(self.icon.height)) * icon_scale;
const title_font_size = scales.column_title_font_size;
const title_font_spacing = 1.0;
const title_size = raylib.MeasureTextEx(global_font, @ptrCast(self.title), title_font_size, title_font_spacing);
const title_position = raylib.Vector2{
.x = icon_position.x + icon_width / 2.0 - title_size.x / 2.0,
.y = icon_position.y + icon_height + 6,
};
var y: f32 = scales.column_position_center.y + icon_height + title_size.y + 24;
for (self.items.items) |item| {
item.position = .{ .x = scales.column_position_center.x, .y = y };
item.draw();
y += 64;
}
raylib.DrawTextureEx(self.icon, icon_position, 0, icon_scale, raylib.WHITE);
raylib.DrawTextEx(global_font, @ptrCast(self.title), title_position, title_font_size, title_font_spacing, raylib.WHITE);
}
pub fn appendItem(self: *Column, item: *Item) !void {
try self.items.append(item);
}
pub fn insertItem(self: *Column, idx: usize, item: *Item) !void {
try self.items.insert(idx, item);
}
pub fn removeItem(self: *Column, idx: usize) void {
_ = try self.items.orderedRemove(idx);
}
};
pub const Item = struct {
position: raylib.Vector2 = .{ .x = 0, .y = 0 },
icon_scale: f32,
start_scale: f32,
time: f32 = 0.0,
icon: raylib.Texture2D,
title: []const u8,
subtitle: []const u8,
large: bool = true,
pub fn init(texture: raylib.Texture2D, title: []const u8, subtitle: []const u8) Item {
raylib.SetTextureFilter(texture, raylib.TEXTURE_FILTER_BILINEAR);
return .{
.icon = texture,
.title = title,
.subtitle = subtitle,
.icon_scale = scales.item_icon_small_scale,
.start_scale = scales.item_icon_small_scale,
};
}
pub fn draw(self: *Item) void {
self.time += raylib.GetFrameTime();
self.icon_scale = std.math.lerp(
self.start_scale,
if (self.large) scales.item_icon_large_scale else scales.item_icon_small_scale,
easeOutExpo(self.time / 0.333),
);
const icon_position = raylib.Vector2{
.x = self.position.x - (@as(f32, @floatFromInt(self.icon.width)) * self.icon_scale - 64),
.y = self.position.y,
};
const title_size = raylib.MeasureTextEx(global_font, @ptrCast(self.title), scales.item_title_font_size, 1);
const title_position = raylib.Vector2{
.x = icon_position.x + 8 + @as(f32, @floatFromInt(self.icon.width)) * self.icon_scale,
.y = icon_position.y + (@as(f32, @floatFromInt(self.icon.height)) * self.icon_scale) / 2.0 - title_size.y / 2.0,
};
raylib.DrawTextureEx(self.icon, icon_position, 0, self.icon_scale, raylib.WHITE);
raylib.DrawTextEx(global_font, @ptrCast(self.title), title_position, scales.item_title_font_size, 1, raylib.WHITE);
}
pub fn setBig(self: *Item, big: bool) void {
self.large = big;
self.time = 0;
self.start_scale = self.icon_scale;
}
fn easeOutExpo(x: f32) f32 {
return 1.0 - std.math.pow(f32, 2, -10 * std.math.clamp(x, 0.0, 1.0));
}
};
/// Draws the dynamic gradient background.
// TODO shift based on time of day
// TODO image wallpaper
// TODO slideshow wallpaper
// TODO animated image wallpaper
pub const Background = struct {
top_left: Color,
top_right: Color,
bottom_right: Color,
bottom_left: Color,
pub fn init() Background {
var self: Background = undefined;
self.setColors(NIGHT_08);
return self;
}
pub fn setColors(self: *Background, colors: [4]Color) void {
self.top_left = colors[0];
self.bottom_right = colors[1];
self.top_right = colors[2];
self.bottom_left = colors[3];
}
pub fn draw(self: *Background) void {
raylib.DrawRectangleGradientEx(
.{
.x = 0,
.y = 0,
.width = screen_width,
.height = screen_height,
},
self.top_left.toRaylib(),
self.bottom_left.toRaylib(),
self.top_right.toRaylib(),
self.bottom_right.toRaylib(),
);
}
pub const Color = struct {
r: f32,
g: f32,
b: f32,
fn toRaylib(self: Color) raylib.Color {
return .{
.r = @intFromFloat(self.r * 255.0),
.g = @intFromFloat(self.g * 255.0),
.b = @intFromFloat(self.b * 255.0),
.a = 255,
};
}
};
pub const DAY_06 = [4]Color{
.{ .r = 0.408, .g = 0.333, .b = 0.643 },
.{ .r = 0.518, .g = 0.365, .b = 0.855 },
.{ .r = 0.761, .g = 0.510, .b = 0.851 },
.{ .r = 0.569, .g = 0.325, .b = 0.620 },
};
pub const DAY_08 = [4]Color{
.{ .r = 0.243, .g = 0.608, .b = 0.831 },
.{ .r = 0.039, .g = 0.690, .b = 0.878 },
.{ .r = 0.016, .g = 0.306, .b = 0.694 },
.{ .r = 0.000, .g = 0.027, .b = 0.310 },
};
pub const NIGHT_08 = [4]Color{
.{ .r = 0.000, .g = 0.145, .b = 0.349 },
.{ .r = 0.000, .g = 0.008, .b = 0.106 },
.{ .r = 0.251, .g = 0.494, .b = 0.576 },
.{ .r = 0.008, .g = 0.537, .b = 0.612 },
};
};
/// 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{};
camera.position.x = 0;
camera.position.y = 10;
camera.position.z = 0;
// camera pointing down, oriented correctly
// TODO this works but looks weird. do better.
camera.target.x = 0;
camera.target.y = 0;
camera.target.z = -0.000000000000001;
camera.up.x = 0;
camera.up.y = 1;
camera.up.z = 0;
camera.fovy = 1;
camera.projection = raylib.CAMERA_ORTHOGRAPHIC;
return camera;
}
const std = @import("std");
const Allocator = std.mem.Allocator;
const c = @import("c.zig");
const raylib = c.raylib;