116 lines
3.3 KiB
Zig
116 lines
3.3 KiB
Zig
|
const SDL = @import("sdl2");
|
||
|
const std = @import("std");
|
||
|
|
||
|
const Color = @import("color.zig");
|
||
|
|
||
|
const Self = @This();
|
||
|
|
||
|
pub const Type = enum {
|
||
|
o,
|
||
|
i,
|
||
|
l,
|
||
|
j,
|
||
|
s,
|
||
|
z,
|
||
|
t,
|
||
|
};
|
||
|
|
||
|
// Positions relative to a grid
|
||
|
row: i32,
|
||
|
col: i32,
|
||
|
structure: [4][4]bool,
|
||
|
color: SDL.SDL_Color,
|
||
|
|
||
|
pub fn init(piece_type: Type) Self {
|
||
|
return Self{
|
||
|
.row = 0,
|
||
|
.col = 0,
|
||
|
|
||
|
.structure = switch (piece_type) {
|
||
|
Type.o => .{
|
||
|
.{ false, false, false, false },
|
||
|
.{ false, true, true, false },
|
||
|
.{ false, true, true, false },
|
||
|
.{ false, false, false, false },
|
||
|
},
|
||
|
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,
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
|
||
|
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, 0xFF);
|
||
|
_ = SDL.SDL_RenderDrawRect(renderer, &rect);
|
||
|
}
|
||
|
}
|
||
|
}
|