Fix texture rendering for free cells

This commit is contained in:
Dusk 2023-09-24 22:53:52 +02:00
parent 0a94c68195
commit 8fbcc7e915
1 changed files with 41 additions and 39 deletions

View File

@ -14,6 +14,7 @@ renderer: *Renderer,
grid_cell_size: i32, grid_cell_size: i32,
grid_pos_x: i32, grid_pos_x: i32,
grid_pos_y: i32, grid_pos_y: i32,
cell_texture: Renderer.Texture,
pub fn init(renderer: *Renderer) Self { pub fn init(renderer: *Renderer) Self {
var wsize = renderer.getOutputSize(); var wsize = renderer.getOutputSize();
@ -26,6 +27,7 @@ pub fn init(renderer: *Renderer) Self {
.grid_pos_x = grid_pos_x, .grid_pos_x = grid_pos_x,
.grid_pos_y = grid_pos_y, .grid_pos_y = grid_pos_y,
.grid_cell_size = grid_cell_size, .grid_cell_size = grid_cell_size,
.cell_texture = Renderer.Texture.init("res/cell.png"),
}; };
} }
@ -42,11 +44,10 @@ pub fn renderBag(self: *Self, game: Game) void {
const new_x = pos_x + @as(i32, @intCast(x)) * self.grid_cell_size; const new_x = pos_x + @as(i32, @intCast(x)) * self.grid_cell_size;
const new_y = pos_y + (@as(i32, @intCast(y)) + @as(i32, @intCast(i * piece.structure.len))) * self.grid_cell_size; const new_y = pos_y + (@as(i32, @intCast(y)) + @as(i32, @intCast(i * piece.structure.len))) * self.grid_cell_size;
var alpha: u8 = 50;
if (piece.structure[y][x]) { if (piece.structure[y][x]) {
alpha = 255; self.cell_texture.drawTo(new_x, new_y, self.grid_cell_size, self.grid_cell_size, piece.color);
} } else {
const alpha = 50;
self.renderer.setColor([_]u8{ self.renderer.setColor([_]u8{
piece.color[0] -| 30, // r piece.color[0] -| 30, // r
@ -62,6 +63,7 @@ pub fn renderBag(self: *Self, game: Game) void {
} }
} }
} }
}
} }
pub fn renderHeld(self: *Self, game: Game) void { pub fn renderHeld(self: *Self, game: Game) void {
@ -76,11 +78,10 @@ pub fn renderHeld(self: *Self, game: Game) void {
const new_x = pos_x + @as(i32, @intCast(x)) * self.grid_cell_size; const new_x = pos_x + @as(i32, @intCast(x)) * self.grid_cell_size;
const new_y = pos_y + @as(i32, @intCast(y)) * self.grid_cell_size; const new_y = pos_y + @as(i32, @intCast(y)) * self.grid_cell_size;
var alpha: u8 = 50;
if (held.structure[y][x]) { if (held.structure[y][x]) {
alpha = 255; self.cell_texture.drawTo(new_x, new_y, self.grid_cell_size, self.grid_cell_size, held.color);
} } else {
const alpha = 50;
self.renderer.setColor([_]u8{ self.renderer.setColor([_]u8{
held.color[0] -| 30, // r held.color[0] -| 30, // r
@ -95,6 +96,7 @@ pub fn renderHeld(self: *Self, game: Game) void {
} }
} }
} }
}
} }
pub fn render(self: *Self, game: Game) void { pub fn render(self: *Self, game: Game) void {
@ -119,15 +121,7 @@ pub fn renderPiece(self: *Self, piece: Piece) void {
const new_x = pos_x + (@as(i32, @intCast(x)) + piece.col) * self.grid_cell_size; const new_x = pos_x + (@as(i32, @intCast(x)) + piece.col) * self.grid_cell_size;
const new_y = pos_y + (@as(i32, @intCast(y)) + piece.row) * self.grid_cell_size; const new_y = pos_y + (@as(i32, @intCast(y)) + piece.row) * self.grid_cell_size;
self.renderer.setColor(.{ self.cell_texture.drawTo(new_x, new_y, self.grid_cell_size, self.grid_cell_size, piece.color);
piece.color[0] -| 30,
piece.color[1] -| 30,
piece.color[2] -| 30,
piece.color[3],
});
self.renderer.fillRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size);
self.renderer.setColor(piece.color);
self.renderer.fillRectangle(new_x + 1, new_y + 1, self.grid_cell_size - 2, self.grid_cell_size - 2);
} }
} }
} }
@ -142,10 +136,18 @@ pub fn renderGrid(self: *Self, grid: Grid) void {
const new_x = pos_x + @as(i32, @intCast(x)) * self.grid_cell_size; const new_x = pos_x + @as(i32, @intCast(x)) * self.grid_cell_size;
const new_y = pos_y + (@as(i32, @intCast(y)) + Grid.buffer) * self.grid_cell_size; const new_y = pos_y + (@as(i32, @intCast(y)) + Grid.buffer) * self.grid_cell_size;
// Background
self.renderer.setColor(color.light_grey); self.renderer.setColor(color.light_grey);
self.renderer.fillRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size); self.renderer.fillRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size);
if (visible[y][x].free) {
// Not dropped
self.renderer.setColor(visible[y][x].color); self.renderer.setColor(visible[y][x].color);
self.renderer.fillRectangle(new_x + 1, new_y + 1, self.grid_cell_size - 2, self.grid_cell_size - 2); self.renderer.fillRectangle(new_x + 1, new_y + 1, self.grid_cell_size - 2, self.grid_cell_size - 2);
} else {
// Dropped
self.cell_texture.drawTo(new_x, new_y, self.grid_cell_size, self.grid_cell_size, visible[y][x].color);
}
} }
} }
} }