Skip to content

Commit

Permalink
Rename Vec2i -> Point and Rect2i -> Rect
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioarnold committed Feb 19, 2023
1 parent 19ae72c commit 83167dd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/Box.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Rect2i = @import("Renderer.zig").Rect2i;
const Rect = @import("Renderer.zig").Rect;

const Box = @This();

Expand All @@ -11,8 +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 toRect2i(self: Box) Rect2i {
return Rect2i.init(self.x, self.y, self.w, self.h);
pub fn toRect(self: Box) Rect {
return Rect.init(self.x, self.y, self.w, self.h);
}

pub fn overlaps(self: Box, other: Box) bool {
Expand Down
6 changes: 3 additions & 3 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 Rect2i = Renderer.Rect2i;
const Rect = Renderer.Rect;

const Self = @This();

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

fn drawGopher(self: Self) void {
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);
var src_rect = Rect.init(self.frame * 24, 0, 24, 24);
const dst_rect = Rect.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;
Expand Down
6 changes: 3 additions & 3 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 Rect2i = Renderer.Rect2i;
const Rect = Renderer.Rect;
const Tile = @import("Tile.zig");
const Room = @import("Room.zig");

Expand Down Expand Up @@ -122,7 +122,7 @@ pub fn draw(self: *Player) void {
return;
}

var src_rect = Rect2i.init(0, 0, 24, 32);
var src_rect = Rect.init(0, 0, 24, 32);
var flip_x = self.face_left;
switch (self.state) {
.idle => {
Expand Down Expand Up @@ -167,7 +167,7 @@ pub fn draw(self: *Player) void {
src_rect.h = 32;
},
}
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);
var dst_rect = Rect.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,
Expand Down
28 changes: 14 additions & 14 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: Vec2i = Vec2i.init(0, 0);
pub var scroll = Point.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 Vec2i = struct {
pub const Point = struct {
x: i32,
y: i32,

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

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

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

Expand Down Expand Up @@ -110,17 +110,17 @@ pub const Texture = struct {

pub const Sprite = struct {
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);
const src_rect = Rect.init(0, 0, @intCast(i32, sprite.width), @intCast(i32, sprite.height));
const dst_rect = Rect.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);
pub fn drawFrame(sprite: Texture, src_rect: Rect, x: i32, y: i32) void {
const dst_rect = Rect.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 {
pub fn drawFromTo(sprite: Texture, src_rect: Rect, dst_rect: Rect) 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);
Expand Down Expand Up @@ -151,7 +151,7 @@ pub const Sprite = struct {
};

pub const Tilemap = struct {
pub fn draw(map: Texture, tiles: Texture, rect: Rect2i) void {
pub fn draw(map: Texture, tiles: Texture, rect: Rect) void {
const x = @intToFloat(f32, rect.x - scroll.x);
const y = @intToFloat(f32, rect.y - scroll.y);
const w = @intToFloat(f32, rect.w);
Expand Down Expand Up @@ -181,7 +181,7 @@ pub const Tilemap = struct {
};

pub const Debug = struct {
pub fn drawRect(rect: Rect2i, color: Color) void {
pub fn drawRect(rect: Rect, 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);
Expand Down
26 changes: 13 additions & 13 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const web = @import("web.zig");
const keys = @import("keys.zig");

const Renderer = @import("Renderer.zig");
const Rect2i = Renderer.Rect2i;
const Rect = Renderer.Rect;
const Sprite = Renderer.Sprite;
const Box = @import("Box.zig");
const Tile = @import("Tile.zig");
Expand Down Expand Up @@ -461,7 +461,7 @@ fn setNextRoom(next_room_index: u8) void {
var death_frame_counter: u32 = 0;
fn drawDeathEffect(x: i32, y: i32) void {
const frame = @intCast(i32, (death_frame_counter / 3) % 6);
const src_rect = Rect2i.init(frame * 24, 0, 24, 24);
const src_rect = Rect.init(frame * 24, 0, 24, 24);

var i: usize = 0;
while (i < 8) : (i += 1) {
Expand All @@ -482,29 +482,29 @@ fn drawTeleportEffect() void {
var y = game_data.player.box.y + game_data.player.box.h;
if (frame <= 10 or frame == 15) {
if (frame != 15) y -= 16 * (10 - frame);
const src_rect = Rect2i.init(8, 0, 8, 8);
const src_rect = Rect.init(8, 0, 8, 8);
var i: i32 = 0;
while (i < 4) : (i += 1) {
Sprite.drawFrame(teleport_tex, src_rect, x - 4, y + i * 8 - 32);
}
} else if (frame <= 12) {
Sprite.drawFrame(teleport_tex, Rect2i.init(0, 16, 24, 16), x - 12, y - 16);
Sprite.drawFrame(teleport_tex, Rect2i.init(0, 16, 24, 8), x - 12, y - 24);
Sprite.drawFrame(teleport_tex, Rect2i.init(8, 8, 8, 8), x - 4, y - 32);
Sprite.drawFrame(teleport_tex, Rect.init(0, 16, 24, 16), x - 12, y - 16);
Sprite.drawFrame(teleport_tex, Rect.init(0, 16, 24, 8), x - 12, y - 24);
Sprite.drawFrame(teleport_tex, Rect.init(8, 8, 8, 8), x - 4, y - 32);
} else if (frame <= 14) {
Sprite.drawFrame(teleport_tex, Rect2i.init(0, 24, 24, 8), x - 12, y - 8);
Sprite.drawFrame(teleport_tex, Rect2i.init(8, 8, 8, 8), x - 4, y - 16);
Sprite.drawFrame(teleport_tex, Rect.init(0, 24, 24, 8), x - 12, y - 8);
Sprite.drawFrame(teleport_tex, Rect.init(8, 8, 8, 8), x - 4, y - 16);
}
}

fn drawTitle() void {
Sprite.drawFrame(title_tex, Rect2i.init(0, 0, 192, 56), 32, 64);
Sprite.drawFrame(title_tex, Rect.init(0, 0, 192, 56), 32, 64);
}

fn drawHealthbar() void {
Sprite.drawFrame(healthbar_tex, Rect2i.init(0, 0, 12, 68), 22, 14);
Sprite.drawFrame(healthbar_tex, Rect.init(0, 0, 12, 68), 22, 14);
const h = 4 + (31 - @as(i32, game_data.player.health)) * 2;
Sprite.drawFrame(healthbar_tex, Rect2i.init(12, 0, 12, h), 22, 14);
Sprite.drawFrame(healthbar_tex, Rect.init(12, 0, 12, h), 22, 14);
}

fn draw() void {
Expand Down Expand Up @@ -547,12 +547,12 @@ fn draw() void {

// text layer
text_tex.updateData(text_buffer[0..]);
const text_rect = Rect2i.init(0, 0, screen_width, screen_height);
const text_rect = Rect.init(0, 0, screen_width, screen_height);
Renderer.Tilemap.draw(text_tex, font_tex, text_rect);
}

fn drawRoom(room: Room, room_tex: Renderer.Texture, door1_h: u8, door2_h: u8) void {
Renderer.Tilemap.draw(room_tex, tiles_tex, room.bounds.toRect2i());
Renderer.Tilemap.draw(room_tex, tiles_tex, room.bounds.toRect());

if (room.door1_y != Room.no_door) {
var i: u8 = 0;
Expand Down

0 comments on commit 83167dd

Please sign in to comment.