diff --git a/res/fonts/MarginaliaRegular-8XlZ.ttf b/res/fonts/MarginaliaRegular-8XlZ.ttf new file mode 100644 index 0000000..27dfc90 Binary files /dev/null and b/res/fonts/MarginaliaRegular-8XlZ.ttf differ diff --git a/src/Game.zig b/src/Game.zig index 012b11e..57d7a77 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -147,7 +147,7 @@ pub fn tick(self: *Self) State { if (!self.piece.timer_dropped.started) { if (!self.timer_gravity.started) { self.timer_gravity.start(); - } else if (self.timer_gravity.finished()){ + } else if (self.timer_gravity.finished()) { self.action_list[@enumToInt(ActionCode.down)].activate = true; self.timer_gravity.start(); } diff --git a/src/Game/Grid.zig b/src/Game/Grid.zig index c81c4fc..f2598d1 100644 --- a/src/Game/Grid.zig +++ b/src/Game/Grid.zig @@ -48,29 +48,3 @@ pub fn clearLines(self: *Self) void { } } -pub fn render(self: Self, renderer: *SDL.SDL_Renderer, cell_size: i32, pos_x: i32, pos_y: i32) void { - var rect = SDL.SDL_Rect{ - .x = pos_x, - .y = pos_y, - .w = cell_size, - .h = cell_size, - }; - - var visible = self.cells[buffer..]; - for (visible) |_, y| { - for (visible[y]) |_, x| { - var r = visible[y][x].color.r; - var g = visible[y][x].color.g; - var b = visible[y][x].color.b; - var a = visible[y][x].color.a; - - rect.x = pos_x + @intCast(i32, x) * cell_size; - rect.y = pos_y + (@intCast(i32, y) + buffer) * cell_size; - - _ = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a); - _ = SDL.SDL_RenderFillRect(renderer, &rect); - _ = SDL.SDL_SetRenderDrawColor(renderer, color.light_grey.r, color.light_grey.g, color.light_grey.b, color.light_grey.a); - _ = SDL.SDL_RenderDrawRect(renderer, &rect); - } - } -} diff --git a/src/Game/Piece.zig b/src/Game/Piece.zig index c345498..05cba27 100644 --- a/src/Game/Piece.zig +++ b/src/Game/Piece.zig @@ -185,33 +185,3 @@ pub fn rotate(self: Self, dir: Rot) Self { return new_piece; } -pub fn render(self: Self, renderer: *SDL.SDL_Renderer, cell_size: i32, pos_x: i32, pos_y: i32) void { - var rect = SDL.SDL_Rect{ - .x = pos_x, - .y = pos_y, - .w = cell_size, - .h = cell_size, - }; - - for (self.structure) |_, y| { - for (self.structure[y]) |_, x| { - // We don't want to paint void cells - if (self.structure[y][x] == false) { - continue; - } - - var r = self.color.r; - var g = self.color.g; - var b = self.color.b; - var a = self.color.a; - - rect.x = pos_x + (@intCast(i32, x) + self.col) * cell_size; - rect.y = pos_y + (@intCast(i32, y) + self.row) * cell_size; - - _ = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a); - _ = SDL.SDL_RenderFillRect(renderer, &rect); - _ = SDL.SDL_SetRenderDrawColor(renderer, r -| 30, g -| 30, b -| 30, a +| 30); - _ = SDL.SDL_RenderDrawRect(renderer, &rect); - } - } -} diff --git a/src/Game/Renderer.zig b/src/Game/Renderer.zig index e3e41ac..aa63254 100644 --- a/src/Game/Renderer.zig +++ b/src/Game/Renderer.zig @@ -130,9 +130,82 @@ pub fn renderHeld(self: *Self, game: Game) void { } pub fn render(self: *Self, game: Game) void { - game.grid.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y); - game.piece.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y); - game.shadow.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y); + renderGrid(self, game.grid); + renderPiece(self, game.piece); + renderPiece(self, game.shadow); renderBag(self, game); renderHeld(self, game); } + +pub fn renderPiece(self: *Self, piece: Piece) void { + + const cell_size = self.grid_cell_size; + const pos_x = self.grid_pos_x; + const pos_y = self.grid_pos_y; + const renderer = self.renderer; + + + var rect = SDL.SDL_Rect{ + .x = pos_x, + .y = pos_y, + .w = cell_size, + .h = cell_size, + }; + + for (piece.structure) |_, y| { + for (piece.structure[y]) |_, x| { + // We don't want to paint void cells + if (piece.structure[y][x] == false) { + continue; + } + + var r = piece.color.r; + var g = piece.color.g; + var b = piece.color.b; + var a = piece.color.a; + + rect.x = pos_x + (@intCast(i32, x) + piece.col) * cell_size; + rect.y = pos_y + (@intCast(i32, y) + piece.row) * cell_size; + + _ = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a); + _ = SDL.SDL_RenderFillRect(renderer, &rect); + _ = SDL.SDL_SetRenderDrawColor(renderer, r -| 30, g -| 30, b -| 30, a +| 30); + _ = SDL.SDL_RenderDrawRect(renderer, &rect); + } + } +} + +pub fn renderGrid(self: *Self, grid: Grid) void { + + const cell_size = self.grid_cell_size; + const pos_x = self.grid_pos_x; + const pos_y = self.grid_pos_y; + const renderer = self.renderer; + + var rect = SDL.SDL_Rect{ + .x = pos_x, + .y = pos_y, + .w = cell_size, + .h = cell_size, + }; + + const buffer = Grid.buffer; + + var visible = grid.cells[buffer..]; + for (visible) |_, y| { + for (visible[y]) |_, x| { + var r = visible[y][x].color.r; + var g = visible[y][x].color.g; + var b = visible[y][x].color.b; + var a = visible[y][x].color.a; + + rect.x = pos_x + @intCast(i32, x) * cell_size; + rect.y = pos_y + (@intCast(i32, y) + buffer) * cell_size; + + _ = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a); + _ = SDL.SDL_RenderFillRect(renderer, &rect); + _ = SDL.SDL_SetRenderDrawColor(renderer, color.light_grey.r, color.light_grey.g, color.light_grey.b, color.light_grey.a); + _ = SDL.SDL_RenderDrawRect(renderer, &rect); + } + } +} diff --git a/src/MainMenu.zig b/src/MainMenu.zig index 7e4f136..a7d3939 100644 --- a/src/MainMenu.zig +++ b/src/MainMenu.zig @@ -3,6 +3,7 @@ const SDL = @import("sdl2"); const MenuSelection = @import("MainMenu/MenuSelection.zig"); const MenuTab = @import("MainMenu/MenuTab.zig"); +const Renderer = @import("MainMenu/Renderer.zig"); const State = @import("flow.zig").State; const Action = @import("Action.zig"); @@ -10,11 +11,11 @@ const Action = @import("Action.zig"); const Self = @This(); action_list: [3]Action = .{ - // Action.init(SDL.SDL_SCANCODE_RIGHT, actionRight), // Right - // Action.init(SDL.SDL_SCANCODE_LEFT, actionLeft), // Left - Action.init(SDL.SDL_SCANCODE_DOWN, actionSelDown), // Down - Action.init(SDL.SDL_SCANCODE_UP, actionSelUp), // Down - Action.init(SDL.SDL_SCANCODE_RETURN, actionSelect), // Down + // Action.init(SDL.SDL_SCANCODE_RIGHT, actionRight), // Tab Right + // Action.init(SDL.SDL_SCANCODE_LEFT, actionLeft), // Tab left + Action.init(SDL.SDL_SCANCODE_DOWN, actionSelDown), // Go down + Action.init(SDL.SDL_SCANCODE_UP, actionSelUp), // Go up + Action.init(SDL.SDL_SCANCODE_RETURN, actionSelect), // Select }, tab_list: [2]MenuTab = [2]MenuTab{ @@ -28,6 +29,8 @@ tab_list: [2]MenuTab = [2]MenuTab{ }), }, +renderer: Renderer, + // Current tab and selection index tab: usize = 0, sel: usize = 0, @@ -37,8 +40,8 @@ state: State = State.main_menu, holding_down: bool = false, holding_enter: bool = false, -pub fn init() Self { - return Self{}; +pub fn init(renderer: *SDL.SDL_Renderer) Self { + return Self{.renderer = Renderer.init(renderer)}; } fn getSel(self: *Self) MenuSelection { diff --git a/src/MainMenu/Renderer.zig b/src/MainMenu/Renderer.zig new file mode 100644 index 0000000..5d5ed00 --- /dev/null +++ b/src/MainMenu/Renderer.zig @@ -0,0 +1,15 @@ +const SDL = @import("sdl2"); + +const MainMenu = @import("../MainMenu.zig"); + +const color = @import("../color.zig"); + +const Self = @This(); + +renderer: *SDL.SDL_Renderer, + +pub fn init(renderer: *SDL.SDL_Renderer) Self { + return Self { + .renderer = renderer, + }; +} diff --git a/src/Renderer.zig b/src/Renderer.zig index 8f949f6..88b0941 100644 --- a/src/Renderer.zig +++ b/src/Renderer.zig @@ -1,3 +1,4 @@ +const std = @import("std"); const sdl = @import("sdl2"); const gl = @import("zgl"); @@ -86,28 +87,33 @@ pub fn setColor(self: *Self, r: u8, g: u32, b: u32, a: u32) void { } pub fn drawRectangle(self: *Self, x: u32, y: u32, w: u32, h: u32) void { + var wsize = self.window.getSize(); + + var xunit = 2 / @intToFloat(f32, wsize.width); + var yunit = 2 / @intToFloat(f32, wsize.height); + + var xf = @intToFloat(f32, x) * xunit - 1; + var yf = 1 - @intToFloat(f32, y) * yunit; + var wf = @intToFloat(f32, w) * xunit; + var hf = @intToFloat(f32, h) * yunit; + gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 3, .float, false, 0, 0); + std.debug.print("{} {}\n", .{ xf, yf }); const vertex_buffer = [_]f32{ - -1.0, -1.0, 0.0, - -1.0, 1.0, 0.0, - 0.0, 1.0, 0.0, - 1.0, 1.0, 0.0, - 0.0, -1.0, 0.0, - 1.0, -1.0, 0.0, + xf, yf, 0.0, + xf + wf, yf, 0.0, + xf, yf + hf, 0.0, }; + self.buffer.data(f32, &vertex_buffer, .static_draw); gl.uniform4fv(self.color_loc, &.{ self.color }); gl.drawArrays(.triangles, 0, 3); - gl.drawArrays(.triangles, 3, 3); gl.disableVertexAttribArray(0); - _ = self; - _ = x; - _ = y; - _ = w; - _ = h; + _ = xf; + _ = yf; }