2022-06-28 12:42:28 +00:00
|
|
|
const SDL = @import("sdl2");
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
const Color = @import("color.zig");
|
2022-07-05 18:26:57 +00:00
|
|
|
const Grid = @import("grid.zig");
|
2022-06-28 12:42:28 +00:00
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
pub const Type = enum {
|
|
|
|
o,
|
|
|
|
i,
|
|
|
|
l,
|
|
|
|
j,
|
|
|
|
s,
|
|
|
|
z,
|
|
|
|
t,
|
|
|
|
};
|
|
|
|
|
2022-07-05 18:26:57 +00:00
|
|
|
const ncols = 4;
|
|
|
|
const nrows = 4;
|
|
|
|
|
2022-06-28 12:42:28 +00:00
|
|
|
// Positions relative to a grid
|
|
|
|
row: i32,
|
|
|
|
col: i32,
|
2022-06-28 16:42:40 +00:00
|
|
|
|
|
|
|
// 4x4 grid indicating piece form
|
2022-06-28 12:42:28 +00:00
|
|
|
structure: [4][4]bool,
|
2022-06-28 16:42:40 +00:00
|
|
|
|
|
|
|
// Should this be dropped and become part of the grid (flag)
|
|
|
|
dropped: bool = false,
|
2022-07-05 18:26:57 +00:00
|
|
|
swapped: bool = false,
|
|
|
|
piece_type: Type,
|
2022-06-28 12:42:28 +00:00
|
|
|
color: SDL.SDL_Color,
|
|
|
|
|
|
|
|
pub fn init(piece_type: Type) Self {
|
|
|
|
return Self{
|
2022-07-05 18:26:57 +00:00
|
|
|
.row = Grid.buffer - 3,
|
|
|
|
.col = (Grid.ncolumns / 2) - (ncols / 2),
|
|
|
|
.piece_type = piece_type,
|
2022-06-28 12:42:28 +00:00
|
|
|
|
|
|
|
.structure = switch (piece_type) {
|
|
|
|
Type.o => .{
|
|
|
|
.{ false, true, true, false },
|
|
|
|
.{ false, true, true, false },
|
|
|
|
.{ false, false, false, false },
|
2022-07-05 18:26:57 +00:00
|
|
|
.{ false, false, false, false },
|
2022-06-28 12:42:28 +00:00
|
|
|
},
|
|
|
|
Type.i => .{
|
|
|
|
.{ false, false, false, false },
|
|
|
|
.{ true, true, true, true },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
},
|
|
|
|
Type.l => .{
|
|
|
|
.{ false, false, true, false },
|
|
|
|
.{ true, true, true, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
},
|
|
|
|
Type.j => .{
|
|
|
|
.{ true, false, false, false },
|
|
|
|
.{ true, true, true, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
},
|
|
|
|
Type.s => .{
|
|
|
|
.{ false, true, true, false },
|
|
|
|
.{ true, true, false, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
},
|
|
|
|
Type.z => .{
|
|
|
|
.{ true, true, false, false },
|
|
|
|
.{ false, true, true, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
},
|
|
|
|
Type.t => .{
|
|
|
|
.{ false, true, false, false },
|
|
|
|
.{ true, true, true, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
.{ false, false, false, false },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
.color = switch (piece_type) {
|
|
|
|
Type.o => Color.yellow,
|
|
|
|
Type.i => Color.cyan,
|
|
|
|
Type.l => Color.orange,
|
|
|
|
Type.j => Color.blue,
|
|
|
|
Type.s => Color.green,
|
|
|
|
Type.z => Color.red,
|
|
|
|
Type.t => Color.purple,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-05 18:26:57 +00:00
|
|
|
pub fn rotateAW(self: Self) Self {
|
|
|
|
var seq_outer = [4][2]u8{ .{ 0, 0 }, .{ 0, 2 }, .{ 2, 2 }, .{ 2, 0 } };
|
|
|
|
var seq_inner = [4][2]u8{ .{ 0, 1 }, .{ 1, 2 }, .{ 2, 1 }, .{ 1, 0 } };
|
|
|
|
|
|
|
|
var new_piece = self;
|
|
|
|
|
|
|
|
for (seq_outer) |_, i| {
|
|
|
|
new_piece.structure[seq_outer[i][0]][seq_outer[i][1]] = self.structure[seq_outer[(i + 1) % 4][0]][seq_outer[(i + 1) % 4][1]];
|
|
|
|
new_piece.structure[seq_inner[i][0]][seq_inner[i][1]] = self.structure[seq_inner[(i + 1) % 4][0]][seq_inner[(i + 1) % 4][1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
//[0,0][0,1][0,2]
|
|
|
|
//[1,0][1,1][1,2]
|
|
|
|
//[2,0][2,1][2,2]
|
|
|
|
|
|
|
|
return new_piece;
|
|
|
|
}
|
|
|
|
|
2022-06-28 12:42:28 +00:00
|
|
|
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);
|
2022-07-05 18:26:57 +00:00
|
|
|
_ = SDL.SDL_SetRenderDrawColor(renderer, r -| 30, g -| 30, b -| 30, a +| 30);
|
2022-06-28 12:42:28 +00:00
|
|
|
_ = SDL.SDL_RenderDrawRect(renderer, &rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|