Merge remote-tracking branch 'origin/main' into feature/opengl-port
This commit is contained in:
commit
022afa07b0
Binary file not shown.
|
@ -147,7 +147,7 @@ pub fn tick(self: *Self) State {
|
||||||
if (!self.piece.timer_dropped.started) {
|
if (!self.piece.timer_dropped.started) {
|
||||||
if (!self.timer_gravity.started) {
|
if (!self.timer_gravity.started) {
|
||||||
self.timer_gravity.start();
|
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.action_list[@enumToInt(ActionCode.down)].activate = true;
|
||||||
self.timer_gravity.start();
|
self.timer_gravity.start();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -185,33 +185,3 @@ pub fn rotate(self: Self, dir: Rot) Self {
|
||||||
return new_piece;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -130,9 +130,82 @@ pub fn renderHeld(self: *Self, game: Game) void {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(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);
|
renderGrid(self, game.grid);
|
||||||
game.piece.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
|
renderPiece(self, game.piece);
|
||||||
game.shadow.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
|
renderPiece(self, game.shadow);
|
||||||
renderBag(self, game);
|
renderBag(self, game);
|
||||||
renderHeld(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ const SDL = @import("sdl2");
|
||||||
|
|
||||||
const MenuSelection = @import("MainMenu/MenuSelection.zig");
|
const MenuSelection = @import("MainMenu/MenuSelection.zig");
|
||||||
const MenuTab = @import("MainMenu/MenuTab.zig");
|
const MenuTab = @import("MainMenu/MenuTab.zig");
|
||||||
|
const Renderer = @import("MainMenu/Renderer.zig");
|
||||||
|
|
||||||
const State = @import("flow.zig").State;
|
const State = @import("flow.zig").State;
|
||||||
const Action = @import("Action.zig");
|
const Action = @import("Action.zig");
|
||||||
|
@ -10,11 +11,11 @@ const Action = @import("Action.zig");
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
action_list: [3]Action = .{
|
action_list: [3]Action = .{
|
||||||
// Action.init(SDL.SDL_SCANCODE_RIGHT, actionRight), // Right
|
// Action.init(SDL.SDL_SCANCODE_RIGHT, actionRight), // Tab Right
|
||||||
// Action.init(SDL.SDL_SCANCODE_LEFT, actionLeft), // Left
|
// Action.init(SDL.SDL_SCANCODE_LEFT, actionLeft), // Tab left
|
||||||
Action.init(SDL.SDL_SCANCODE_DOWN, actionSelDown), // Down
|
Action.init(SDL.SDL_SCANCODE_DOWN, actionSelDown), // Go down
|
||||||
Action.init(SDL.SDL_SCANCODE_UP, actionSelUp), // Down
|
Action.init(SDL.SDL_SCANCODE_UP, actionSelUp), // Go up
|
||||||
Action.init(SDL.SDL_SCANCODE_RETURN, actionSelect), // Down
|
Action.init(SDL.SDL_SCANCODE_RETURN, actionSelect), // Select
|
||||||
},
|
},
|
||||||
|
|
||||||
tab_list: [2]MenuTab = [2]MenuTab{
|
tab_list: [2]MenuTab = [2]MenuTab{
|
||||||
|
@ -28,6 +29,8 @@ tab_list: [2]MenuTab = [2]MenuTab{
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
renderer: Renderer,
|
||||||
|
|
||||||
// Current tab and selection index
|
// Current tab and selection index
|
||||||
tab: usize = 0,
|
tab: usize = 0,
|
||||||
sel: usize = 0,
|
sel: usize = 0,
|
||||||
|
@ -37,8 +40,8 @@ state: State = State.main_menu,
|
||||||
holding_down: bool = false,
|
holding_down: bool = false,
|
||||||
holding_enter: bool = false,
|
holding_enter: bool = false,
|
||||||
|
|
||||||
pub fn init() Self {
|
pub fn init(renderer: *SDL.SDL_Renderer) Self {
|
||||||
return Self{};
|
return Self{.renderer = Renderer.init(renderer)};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getSel(self: *Self) MenuSelection {
|
fn getSel(self: *Self) MenuSelection {
|
||||||
|
|
|
@ -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,
|
||||||
|
};
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
const std = @import("std");
|
||||||
const sdl = @import("sdl2");
|
const sdl = @import("sdl2");
|
||||||
const gl = @import("zgl");
|
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 {
|
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.enableVertexAttribArray(0);
|
||||||
gl.vertexAttribPointer(0, 3, .float, false, 0, 0);
|
gl.vertexAttribPointer(0, 3, .float, false, 0, 0);
|
||||||
|
|
||||||
|
std.debug.print("{} {}\n", .{ xf, yf });
|
||||||
const vertex_buffer = [_]f32{
|
const vertex_buffer = [_]f32{
|
||||||
-1.0, -1.0, 0.0,
|
xf, yf, 0.0,
|
||||||
-1.0, 1.0, 0.0,
|
xf + wf, yf, 0.0,
|
||||||
0.0, 1.0, 0.0,
|
xf, yf + hf, 0.0,
|
||||||
1.0, 1.0, 0.0,
|
|
||||||
0.0, -1.0, 0.0,
|
|
||||||
1.0, -1.0, 0.0,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
self.buffer.data(f32, &vertex_buffer, .static_draw);
|
self.buffer.data(f32, &vertex_buffer, .static_draw);
|
||||||
|
|
||||||
gl.uniform4fv(self.color_loc, &.{ self.color });
|
gl.uniform4fv(self.color_loc, &.{ self.color });
|
||||||
gl.drawArrays(.triangles, 0, 3);
|
gl.drawArrays(.triangles, 0, 3);
|
||||||
gl.drawArrays(.triangles, 3, 3);
|
|
||||||
|
|
||||||
gl.disableVertexAttribArray(0);
|
gl.disableVertexAttribArray(0);
|
||||||
|
|
||||||
_ = self;
|
_ = xf;
|
||||||
_ = x;
|
_ = yf;
|
||||||
_ = y;
|
|
||||||
_ = w;
|
|
||||||
_ = h;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue