Skip to content

Commit

Permalink
Switch Renderer API from float to int
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioarnold committed Feb 19, 2023
1 parent 32d2ee4 commit 19ae72c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 128 deletions.
11 changes: 3 additions & 8 deletions src/Box.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Rect2 = @import("Renderer.zig").Rect2;
const Rect2i = @import("Renderer.zig").Rect2i;

const Box = @This();

Expand All @@ -11,13 +11,8 @@ pub fn init(x: i32, y: i32, w: i32, h: i32) Box {
return Box{ .x = x, .y = y, .w = w, .h = h };
}

pub fn toRect2(self: Box) Rect2 {
return .{
.x = @intToFloat(f32, self.x),
.y = @intToFloat(f32, self.y),
.w = @intToFloat(f32, self.w),
.h = @intToFloat(f32, self.h),
};
pub fn toRect2i(self: Box) Rect2i {
return Rect2i.init(self.x, self.y, self.w, self.h);
}

pub fn overlaps(self: Box, other: Box) bool {
Expand Down
8 changes: 4 additions & 4 deletions src/Enemy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Box = @import("Box.zig");
const Attrib = @import("Tile.zig").Attrib;
const Room = @import("Room.zig");
const Renderer = @import("Renderer.zig");
const Rect2 = Renderer.Rect2;
const Rect2i = Renderer.Rect2i;

const Self = @This();

Expand Down Expand Up @@ -97,11 +97,11 @@ fn tickGopher(self: *Self, r: std.rand.Random, game: *GameData, attribs: []const
}

fn drawGopher(self: Self) void {
var src_rect = Rect2.init(@intToFloat(f32, self.frame * 24), 0, 24, 24);
var dst_rect = Rect2.init(@intToFloat(f32, self.box.x) - 4, @intToFloat(f32, self.box.y), src_rect.w, src_rect.h);
var src_rect = Rect2i.init(self.frame * 24, 0, 24, 24);
const dst_rect = Rect2i.init(self.box.x - 4, self.box.y, 24, 24);
if (self.flip_x) {
src_rect.x += src_rect.w;
src_rect.w = -src_rect.w;
}
Renderer.Sprite.draw(gopher_sprite, src_rect, dst_rect);
Renderer.Sprite.drawFromTo(gopher_sprite, src_rect, dst_rect);
}
27 changes: 11 additions & 16 deletions src/Player.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const web = @import("web.zig");
const keys = @import("keys.zig");
const Box = @import("Box.zig");
const Renderer = @import("Renderer.zig");
const Rect2 = Renderer.Rect2;
const Rect2i = Renderer.Rect2i;
const Tile = @import("Tile.zig");
const Room = @import("Room.zig");

Expand Down Expand Up @@ -117,15 +117,12 @@ pub fn hurt(self: *Player, damage: u8) void {
pub fn draw(self: *Player) void {
if (self.invincibility_frames % 6 >= 3) {
if (self.state == .hurting) {
const src_rect = Rect2.init(0, 0, 24, 24);
const dst_rect = Rect2.init(@intToFloat(f32, self.box.x) - 4, @intToFloat(f32, self.box.y), 24, 24);
Renderer.Sprite.draw(hurt_fx, src_rect, dst_rect);
Renderer.Sprite.draw(hurt_fx, self.box.x - 4, self.box.y);
}
return;
}

const bigger_sprite = true;
var src_rect = if (bigger_sprite) Rect2.init(0, 0, 24, 32) else Rect2.init(0, 8, 24, 24);
var src_rect = Rect2i.init(0, 0, 24, 32);
var flip_x = self.face_left;
switch (self.state) {
.idle => {
Expand Down Expand Up @@ -170,21 +167,19 @@ pub fn draw(self: *Player) void {
src_rect.h = 32;
},
}
var dst_rect = Rect2.init(@intToFloat(f32, self.box.x + @divTrunc(self.box.w - @floatToInt(i32, src_rect.w), 2)), @intToFloat(f32, self.box.y), src_rect.w, src_rect.h);
if (bigger_sprite) {
dst_rect.y -= 8;
if (self.state == .climbing) dst_rect.y += 4;
if (self.state == .jumping) dst_rect.y += 5;
if (self.state == .hurting) dst_rect.y += 6;
} else {
if (self.state == .sliding) dst_rect.y -= 8;
if (self.state == .climbing) dst_rect.y -= 4;
var dst_rect = Rect2i.init(self.box.x + @divTrunc(self.box.w - src_rect.w, 2), self.box.y - 8, src_rect.w, src_rect.h);
switch (self.state) {
.climbing => dst_rect.y += 4,
.jumping => dst_rect.y += 5,
.hurting => dst_rect.y += 6,
else => {},
}

if (flip_x) {
src_rect.x += src_rect.w;
src_rect.w = -src_rect.w;
}
Renderer.Sprite.draw(sprite, src_rect, dst_rect);
Renderer.Sprite.drawFromTo(sprite, src_rect, dst_rect);
}

pub fn handleInput(self: *Player, room: Room, attribs: []const Tile.Attrib, input: Input, prev_input: Input) void {
Expand Down
93 changes: 55 additions & 38 deletions src/Renderer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fb_width = 256;
const fb_height = 240;
var blit_vbo: gl.GLuint = undefined;

pub var scroll: Vec2 = Vec2.init(0, 0);
pub var scroll: Vec2i = Vec2i.init(0, 0);

const identity_matrix = [16]f32{
1, 0, 0, 0,
Expand Down Expand Up @@ -35,23 +35,23 @@ var colored: struct {
mvp_loc: gl.GLint,
} = undefined;

pub const Vec2 = struct {
x: f32,
y: f32,
pub const Vec2i = struct {
x: i32,
y: i32,

pub fn init(x: f32, y: f32) Vec2 {
return Vec2{ .x = x, .y = y };
pub fn init(x: i32, y: i32) Vec2i {
return Vec2i{ .x = x, .y = y };
}
};

pub const Rect2 = struct {
x: f32,
y: f32,
w: f32,
h: f32,
pub const Rect2i = struct {
x: i32,
y: i32,
w: i32,
h: i32,

pub fn init(x: f32, y: f32, w: f32, h: f32) Rect2 {
return Rect2{ .x = x, .y = y, .w = w, .h = h };
pub fn init(x: i32, y: i32, w: i32, h: i32) Rect2i {
return Rect2i{ .x = x, .y = y, .w = w, .h = h };
}
};

Expand Down Expand Up @@ -109,27 +109,40 @@ pub const Texture = struct {
};

pub const Sprite = struct {
pub fn draw(sprite: Texture, src_rect: Rect2, dst_rect: Rect2) void {
const x = dst_rect.x - scroll.x;
const y = dst_rect.y - scroll.y;
pub fn draw(sprite: Texture, x: i32, y: i32) void {
const src_rect = Rect2i.init(0, 0, @intCast(i32, sprite.width), @intCast(i32, sprite.height));
const dst_rect = Rect2i.init(x, y, src_rect.w, src_rect.h);
drawFromTo(sprite, src_rect, dst_rect);
}

pub fn drawFrame(sprite: Texture, src_rect: Rect2i, x: i32, y: i32) void {
const dst_rect = Rect2i.init(x, y, src_rect.w, src_rect.h);
drawFromTo(sprite, src_rect, dst_rect);
}

pub fn drawFromTo(sprite: Texture, src_rect: Rect2i, dst_rect: Rect2i) void {
const x = @intToFloat(f32, dst_rect.x - scroll.x);
const y = @intToFloat(f32, dst_rect.y - scroll.y);
const w = @intToFloat(f32, dst_rect.w);
const h = @intToFloat(f32, dst_rect.h);
const px: f32 = 2.0 / @as(f32, fb_width);
const py: f32 = 2.0 / @as(f32, fb_height);
const mvp = [16]f32{
px * dst_rect.w, 0, 0, 0,
0, -py * dst_rect.h, 0, 0,
0, 0, 1, 0,
px * x - 1, 1 - py * y, 0, 1,
px * w, 0, 0, 0,
0, -py * h, 0, 0,
0, 0, 1, 0,
px * x - 1, 1 - py * y, 0, 1,
};
gl.glUseProgram(blit2d.program);
gl.glUniformMatrix4fv(blit2d.mvp_loc, 1, gl.GL_FALSE, &mvp);

const sx = 1.0 / @intToFloat(f32, sprite.width);
const sy = 1.0 / @intToFloat(f32, sprite.height);
const texmat = [16]f32{
src_rect.w * sx, 0, 0, 0,
0, src_rect.h * sy, 0, 0,
0, 0, 1, 0,
src_rect.x * sx, src_rect.y * sy, 0, 1,
@intToFloat(f32, src_rect.w) * sx, 0, 0, 0,
0, @intToFloat(f32, src_rect.h) * sy, 0, 0,
0, 0, 1, 0,
@intToFloat(f32, src_rect.x) * sx, @intToFloat(f32, src_rect.y) * sy, 0, 1,
};
gl.glUniformMatrix4fv(blit2d.texmat_loc, 1, gl.GL_FALSE, &texmat);
gl.glBindTexture(gl.GL_TEXTURE_2D, sprite.handle);
Expand All @@ -138,16 +151,18 @@ pub const Sprite = struct {
};

pub const Tilemap = struct {
pub fn draw(map: Texture, tiles: Texture, rect: Rect2) void {
const x = rect.x - scroll.x;
const y = rect.y - scroll.y;
pub fn draw(map: Texture, tiles: Texture, rect: Rect2i) void {
const x = @intToFloat(f32, rect.x - scroll.x);
const y = @intToFloat(f32, rect.y - scroll.y);
const w = @intToFloat(f32, rect.w);
const h = @intToFloat(f32, rect.h);
const px = 2.0 / @as(f32, fb_width);
const py: f32 = 2.0 / @as(f32, fb_height);
const mvp = [16]f32{
px * rect.w, 0, 0, 0,
0, -py * rect.h, 0, 0,
0, 0, 1, 0,
px * x - 1, 1 - py * y, 0, 1,
px * w, 0, 0, 0,
0, -py * h, 0, 0,
0, 0, 1, 0,
px * x - 1, 1 - py * y, 0, 1,
};
gl.glUseProgram(tiled.program);
gl.glUniformMatrix4fv(tiled.mvp_loc, 1, gl.GL_FALSE, &mvp);
Expand All @@ -166,16 +181,18 @@ pub const Tilemap = struct {
};

pub const Debug = struct {
pub fn drawRect(rect: Rect2, color: Color) void {
const x = rect.x - scroll.x;
const y = rect.y - scroll.y;
pub fn drawRect(rect: Rect2i, color: Color) void {
const x = @intToFloat(f32, rect.x - scroll.x);
const y = @intToFloat(f32, rect.y - scroll.y);
const w = @intToFloat(f32, rect.w);
const h = @intToFloat(f32, rect.h);
const px = 2.0 / @as(f32, fb_width);
const py: f32 = 2.0 / @as(f32, fb_height);
const mvp = [16]f32{
px * rect.w, 0, 0, 0,
0, -py * rect.h, 0, 0,
0, 0, 1, 0,
px * x - 1, 1 - py * y, 0, 1,
px * w, 0, 0, 0,
0, -py * h, 0, 0,
0, 0, 1, 0,
px * x - 1, 1 - py * y, 0, 1,
};
gl.glUseProgram(colored.program);
gl.glUniformMatrix4fv(colored.mvp_loc, 1, gl.GL_FALSE, &mvp);
Expand Down
Loading

0 comments on commit 19ae72c

Please sign in to comment.