Skip to content
This repository was archived by the owner on May 29, 2024. It is now read-only.

Commit 8fa7e29

Browse files
committed
Center view on player
1 parent 6e21822 commit 8fa7e29

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/main.zig

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Texture = @import("./texture.zig").Texture;
1212
const Map = @import("./map.zig").Map;
1313
const tile = @import("./tile.zig");
1414
const generate = @import("./generate.zig");
15+
const Mat4f = math.Mat4(f32);
1516

1617
// Setup environment
1718
pub const panic = platform.panic;
@@ -109,21 +110,23 @@ pub fn onEvent(event: platform.event.Event) !void {
109110
}
110111

111112
pub fn render(alpha: f64) !void {
112-
const screen_size_int = platform.getScreenSize();
113-
const screen_size = screen_size_int.intToFloat(f32);
114-
115-
flatRenderer.setSize(screen_size);
113+
const screen_size = platform.getScreenSize();
116114

117115
gl.clearColor(0.0, 0.0, 0.0, 1.0);
118116
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
119-
gl.viewport(0, 0, screen_size_int.x, screen_size_int.y);
117+
gl.viewport(0, 0, screen_size.x, screen_size.y);
118+
119+
const cam_size = screen_size.intToFloat(f32);
120+
const cam_pos = playerPos.scale(16).intToFloat(f32).subv(cam_size.scaleDiv(2));
121+
122+
flatRenderer.perspective = Mat4f.orthographic(cam_pos.x, cam_pos.x + cam_size.x, cam_pos.y + cam_size.y, cam_pos.y, -1, 1);
120123

121-
map.render();
122-
render_tile(.{ .pos = 25 }, playerPos);
124+
map.render(&flatRenderer);
125+
render_tile(&flatRenderer, .{ .pos = 25 }, playerPos);
123126
flatRenderer.flush();
124127
}
125128

126-
pub fn render_tile(tid: tile.TID, pos: Vec2i) void {
129+
pub fn render_tile(fr: *FlatRenderer, tid: tile.TID, pos: Vec2i) void {
127130
const id = tid.pos;
128131

129132
const tileposy = id / (tilesetTex.size.x / TILE_W);
@@ -132,5 +135,5 @@ pub fn render_tile(tid: tile.TID, pos: Vec2i) void {
132135
const texpos1 = vec2f(@intToFloat(f32, tileposx) / @intToFloat(f32, tilesetTex.size.x / TILE_W), @intToFloat(f32, tileposy) / @intToFloat(f32, tilesetTex.size.y / TILE_H));
133136
const texpos2 = vec2f(@intToFloat(f32, tileposx + 1) / @intToFloat(f32, tilesetTex.size.x / TILE_W), @intToFloat(f32, tileposy + 1) / @intToFloat(f32, tilesetTex.size.y / TILE_H));
134137

135-
flatRenderer.drawTextureRect(tilesetTex, texpos1, texpos2, pos.scale(16).intToFloat(f32), vec2f(16, 16)) catch unreachable;
138+
fr.drawTextureRect(tilesetTex, texpos1, texpos2, pos.scale(16).intToFloat(f32), vec2f(16, 16)) catch unreachable;
136139
}

src/map.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Vec2i = math.Vec(2, i64);
55
const vec2i = Vec2i.init;
66
const render_tile = @import("./main.zig").render_tile;
77
const Neighbors = tile.Neighbors;
8+
const FlatRenderer = @import("./flat_render.zig").FlatRenderer;
89

910
pub const Map = struct {
1011
allocator: *std.mem.Allocator,
@@ -55,7 +56,7 @@ pub const Map = struct {
5556
return tile.DESCRIPTIONS[@enumToInt(tag)];
5657
}
5758

58-
pub fn render(this: @This()) void {
59+
pub fn render(this: @This(), flatRenderer: *FlatRenderer) void {
5960
var pos = vec2i(0, 0);
6061
while (pos.y < this.size.y) : (pos.y += 1) {
6162
pos.x = 0;
@@ -64,7 +65,7 @@ pub const Map = struct {
6465
const desc = tile.DESCRIPTIONS[@enumToInt(tag)];
6566
switch (desc.render) {
6667
.None => {},
67-
.Static => |tid| render_tile(tid, pos),
68+
.Static => |tid| render_tile(flatRenderer, tid, pos),
6869
.Connected => |tids| {
6970
const neighbors = Neighbors{
7071
.n = this.get(pos.add(0, -1)) == tag,
@@ -85,7 +86,7 @@ pub const Map = struct {
8586
.SouthWest => 9,
8687
.EastWest, .NorthEastWest, .SouthEastWest => 10,
8788
};
88-
render_tile(tids[idx], pos);
89+
render_tile(flatRenderer, tids[idx], pos);
8990
},
9091
}
9192
}

0 commit comments

Comments
 (0)