summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRadioactivity <85001747+voidwyrm-2@users.noreply.github.com>2025-07-25 14:28:47 -1000
committerRadioactivity <85001747+voidwyrm-2@users.noreply.github.com>2025-07-25 14:29:01 -1000
commit6e0c4c1884fb1d19f21b3372d1cf8dbb86bb1712 (patch)
tree69c99ce06f03d6b63c0ae06a4fd7c875b5352127
parent5cb9f2c6274718b452a342b3ed76e8f0b3c31d45 (diff)
Added a camera to allow the main menu to scale when resizing
-rw-r--r--src/main.zig49
1 files changed, 40 insertions, 9 deletions
diff --git a/src/main.zig b/src/main.zig
index 225e2fd..b71b5f8 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -481,11 +481,22 @@ const Game = struct {
};
const ScreenMainMenu = struct {
+ camera: rl.Camera2D,
pressed_start: bool = false,
pressed_options: bool = false,
fn init() ScreenMainMenu {
- return .{};
+ return .{
+ .camera = .{
+ .target = .{ .x = 128, .y = 128 },
+ .offset = .{
+ .x = 0,
+ .y = 0,
+ },
+ .rotation = 0,
+ .zoom = @as(f32, @floatFromInt(screenHeight)) / world_height,
+ },
+ };
}
fn update(self: *ScreenMainMenu, game: *Game, dt: f32) void {
@@ -495,18 +506,39 @@ const ScreenMainMenu = struct {
const battle = ScreenBattle.init();
game.screen_state = .{ .battle = battle };
}
+
+ self.updateCamera();
}
fn render(self: *ScreenMainMenu, game: *Game) void {
+ rl.beginMode2D(self.camera);
+ defer rl.endMode2D();
+
// what are we actually calling this game?
- rl.drawTextEx(game.font_title, "Bug Defenders", rl.Vector2.init(480, 30), 40, 4, rl.Color.white);
+ rl.drawTextEx(game.font_title, "Bug Defenders", rl.Vector2.init(495, 160), 40, 4, rl.Color.white);
- // TODO: the play button and the title text don't scale with window resizing
- // textures do, though, weirdly?
- self.pressed_start = rlg.button(.{ .x = 285, .y = 280, .width = 300, .height = 100 }, "Play");
+ self.pressed_start = rlg.button(.{ .x = 540, .y = 410, .width = 200, .height = 75 }, "Play");
+ // TODO: after the hackathon, add an options menu
//self.pressed_options = rlg.button(.{ .x = 285, .y = 280, .width = 300, .height = 100 }, "Options");
}
+
+ fn updateCamera(self: *ScreenMainMenu) void {
+ screenWidth = rl.getScreenWidth();
+ screenHeight = rl.getScreenHeight();
+
+ self.camera.target = .{
+ .x = world_width / 2.0,
+ .y = world_height / 2.0,
+ };
+
+ // Take the average between the ratios
+ // This avoids "cheating" by changing the ratio to an extreme value
+ // in order to see more terrain in a certain axis
+ const width_ratio = @as(f32, @floatFromInt(screenWidth)) / world_width;
+ const height_ratio = @as(f32, @floatFromInt(screenHeight)) / world_height;
+ self.camera.zoom = (width_ratio + height_ratio) / 3;
+ }
};
const ScreenBattle = struct {
@@ -525,7 +557,7 @@ const ScreenBattle = struct {
.y = @as(f32, @floatFromInt(screenHeight)) / 2,
},
.rotation = 0,
- .zoom = @as(f32, @floatFromInt(screenHeight)) / world_height,
+ .zoom = 0,
},
.ram = max_ram,
};
@@ -558,16 +590,15 @@ const ScreenBattle = struct {
// in order to see more terrain in a certain axis
const width_ratio = @as(f32, @floatFromInt(screenWidth)) / world_width;
const height_ratio = @as(f32, @floatFromInt(screenHeight)) / world_height;
- self.camera.zoom = (width_ratio + height_ratio) / 2;
+ self.camera.zoom = -(width_ratio + height_ratio);
}
fn render(self: *ScreenBattle, game: *Game) void {
const a = game.frame_arena.allocator();
const map = self.wave.map;
- const camera = self.camera;
{
- rl.beginMode2D(camera);
+ rl.beginMode2D(self.camera);
defer rl.endMode2D();
// TODO: make bugs render between (on top of) lanes and (under) AI