Better preview rendering

This commit is contained in:
Dusk 2022-07-07 16:23:16 +02:00
parent 7eb6f2b9b4
commit 3cca4ced3f
5 changed files with 114 additions and 40 deletions

View File

@ -1,9 +1,9 @@
const SDL = @import("sdl2");
pub const yellow = SDL.SDL_Color{
.r = 245,
.g = 215,
.b = 145,
.r = 232,
.g = 216,
.b = 165,
.a = 255,
};
pub const brown = SDL.SDL_Color{
@ -13,39 +13,39 @@ pub const brown = SDL.SDL_Color{
.a = 255,
};
pub const cyan = SDL.SDL_Color{
.r = 120,
.g = 190,
.b = 185,
.r = 138,
.g = 167,
.b = 172,
.a = 255,
};
pub const orange = SDL.SDL_Color{
.r = 255,
.g = 170,
.r = 222,
.g = 154,
.b = 40,
.a = 255,
};
pub const blue = SDL.SDL_Color{
.r = 105,
.g = 160,
.b = 255,
.r = 112,
.g = 123,
.b = 136,
.a = 255,
};
pub const green = SDL.SDL_Color{
.r = 120,
.g = 175,
.b = 135,
.r = 142,
.g = 146,
.b = 87,
.a = 255,
};
pub const red = SDL.SDL_Color{
.r = 255,
.g = 105,
.b = 115,
.r = 229,
.g = 93,
.b = 77,
.a = 255,
};
pub const purple = SDL.SDL_Color{
.r = 210,
.g = 180,
.b = 210,
.r = 180,
.g = 171,
.b = 189,
.a = 255,
};
pub const pink = SDL.SDL_Color{

View File

@ -36,7 +36,7 @@ pub fn init(_renderer: *SDL.SDL_Renderer) Self {
.grid = Grid.init(),
.grid_cell_size = 16,
.grid_pos_x = 200,
.grid_pos_y = 0,
.grid_pos_y = -50,
.bag = Bag.init(),
.piece = undefined,
@ -58,7 +58,6 @@ pub fn init(_renderer: *SDL.SDL_Renderer) Self {
// }
// }
pub fn tick(self: *Self) void {
var key_state = SDL.SDL_GetKeyboardState(null);
// MOVEMENT

View File

@ -45,10 +45,7 @@ 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 {

View File

@ -39,7 +39,7 @@ pub fn main() !void {
break :mainLoop;
}
_ = SDL.SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
_ = SDL.SDL_SetRenderDrawColor(renderer, 231, 247, 255, 0xFF);
_ = SDL.SDL_RenderClear(renderer);
game.tick();

View File

@ -4,30 +4,108 @@ const Game = @import("game.zig");
const Bag = @import("bag.zig");
const Grid = @import("grid.zig");
const Piece = @import("piece.zig");
const Color = @import("color.zig");
pub fn renderBag(renderer: *SDL.SDL_Renderer, bag: Bag, cell_size: i32, pos_x: i32, pos_y: i32) void {
var trans: u8 = 0;
for (bag.contents[0..5]) |_, i| {
if (bag.contents[i]) |piece_type| {
pub fn renderBag(game: Game) void {
const pos_x = game.grid_pos_x + ((Grid.ncolumns + 1) * game.grid_cell_size);
const pos_y = game.grid_pos_y + (Grid.buffer * game.grid_cell_size);
const cell_size = game.grid_cell_size;
var rect = SDL.SDL_Rect{
.x = pos_x,
.y = pos_y,
.w = cell_size,
.h = cell_size,
};
//var trans: u8 = 0;
var r: u8 = 0;
var g: u8 = 0;
var b: u8 = 0;
var a: u8 = 255;
for (game.bag.contents[0..5]) |_, i| {
if (game.bag.contents[i]) |piece_type| {
var piece = Piece.init(piece_type);
piece.color.a -|= trans;
trans +|= 55;
piece.render(renderer, cell_size, pos_x, pos_y + @intCast(i32, i) * cell_size * 4);
for (piece.structure) |_, y| {
for (piece.structure[y]) |_, x| {
if (piece.structure[y][x]) {
r = piece.color.r;
g = piece.color.g;
b = piece.color.b;
a = piece.color.a;
} else {
r = piece.color.r;
g = piece.color.g;
b = piece.color.b;
a = 50;
}
rect.x = pos_x + @intCast(i32, x) * cell_size;
rect.y = pos_y + (@intCast(i32, y) + @intCast(i32, i * piece.structure.len)) * cell_size;
_ = SDL.SDL_SetRenderDrawColor(game.renderer, r, g, b, a);
_ = SDL.SDL_RenderFillRect(game.renderer, &rect);
_ = SDL.SDL_SetRenderDrawColor(game.renderer, r -| 30, g -| 30, b -| 30, a);
_ = SDL.SDL_RenderDrawRect(game.renderer, &rect);
}
}
}
}
}
pub fn renderHeld(renderer: *SDL.SDL_Renderer, held: Piece.Type, cell_size: i32, pos_x: i32, pos_y: i32) void {
Piece.init(held).render(renderer, cell_size, pos_x, pos_y);
pub fn renderHeld(game: Game) void {
const pos_x = game.grid_pos_x - (5 * game.grid_cell_size);
const pos_y = game.grid_pos_y + (Grid.buffer * game.grid_cell_size);
const cell_size = game.grid_cell_size;
var rect = SDL.SDL_Rect{
.x = pos_x,
.y = pos_y,
.w = cell_size,
.h = cell_size,
};
//var trans: u8 = 0;
var r: u8 = 0;
var g: u8 = 0;
var b: u8 = 0;
var a: u8 = 255;
if (game.held) |held_type| {
var held = Piece.init(held_type);
for (held.structure) |_, y| {
for (held.structure[y]) |_, x| {
if (held.structure[y][x]) {
r = held.color.r;
g = held.color.g;
b = held.color.b;
a = held.color.a;
} else {
r = held.color.r;
g = held.color.g;
b = held.color.b;
a = 50;
}
rect.x = pos_x + @intCast(i32, x) * cell_size;
rect.y = pos_y + @intCast(i32, y) * cell_size;
_ = SDL.SDL_SetRenderDrawColor(game.renderer, r, g, b, a);
_ = SDL.SDL_RenderFillRect(game.renderer, &rect);
_ = SDL.SDL_SetRenderDrawColor(game.renderer, r -| 30, g -| 30, b -| 30, a);
_ = SDL.SDL_RenderDrawRect(game.renderer, &rect);
}
}
}
}
pub fn render(game: Game) void {
game.grid.render(game.renderer, game.grid_cell_size, game.grid_pos_x, game.grid_pos_y);
game.piece.render(game.renderer, game.grid_cell_size, game.grid_pos_x, game.grid_pos_y);
game.shadow.render(game.renderer, game.grid_cell_size, game.grid_pos_x, game.grid_pos_y);
renderBag(game.renderer, game.bag, game.grid_cell_size, game.grid_pos_x + game.grid_cell_size * (Grid.ncolumns + 1), game.grid_pos_y);
if (game.held) |held_piece| {
renderHeld(game.renderer, held_piece, game.grid_cell_size, game.grid_pos_x - (game.grid_cell_size * 5), game.grid_pos_y);
}
renderBag(game);
renderHeld(game);
}