Reimplement game rendering

This commit is contained in:
Dusk 2023-09-22 19:58:47 +02:00
parent e21e184c58
commit a86fda392d
6 changed files with 92 additions and 185 deletions

View File

@ -38,10 +38,10 @@ shadow: Piece,
action_list: ActionList = .{ action_list: ActionList = .{
.right = Action.init(input.menu_left, actionRight), // Right .right = Action.init(input.menu_left, actionRight), // Right
.left = Action.init(input.menu_left, actionLeft), // Left .left = Action.init(input.menu_left, actionLeft), // Left
.down = Action.init(input.menu_left, actionDown), // Down .down = Action.init(input.menu_left, actionDown), // Down
.hard = Action.init(input.menu_left, actionHard), // Instant Drop .hard = Action.init(input.menu_left, actionHard), // Instant Drop
.swap = Action.init(input.menu_left, actionSwap), // Swap Piece .swap = Action.init(input.menu_left, actionSwap), // Swap Piece
.rot_r = Action.init(input.menu_left, actionRotR), // Rotate .rot_r = Action.init(input.menu_left, actionRotR), // Rotate
.rot_l = Action.init(input.menu_left, actionRotL), // Rotate .rot_l = Action.init(input.menu_left, actionRotL), // Rotate
}, },
@ -192,7 +192,7 @@ pub fn tick(self: *Self) State {
// Update Shadow // Update Shadow
{ {
self.shadow = movement.shadow(self.grid, self.piece); self.shadow = movement.shadow(self.grid, self.piece);
//self.shadow.color.a /= 4; self.shadow.color[3] /= 4;
} }
// Check for a top out // Check for a top out

View File

@ -1,15 +1,15 @@
const std = @import("std"); const std = @import("std");
const Color = @import("../color.zig"); const color = @import("../color.zig");
const Self = @This(); const Self = @This();
free: bool, free: bool,
//color: SDL.Color, color: color.Color,
pub fn init() Self { pub fn init() Self {
return Self{ return Self{
.free = true, .free = true,
//.color = Color.dark_grey, .color = color.dark_grey,
}; };
} }

View File

@ -50,7 +50,7 @@ dropped: bool = false,
swapped: bool = false, swapped: bool = false,
piece_type: Type, piece_type: Type,
//color: SDL.Color, color: color.Color,
pub fn init(piece_type: Type) Self { pub fn init(piece_type: Type) Self {
return Self{ return Self{
@ -105,15 +105,15 @@ pub fn init(piece_type: Type) Self {
}, },
}, },
//.color = switch (piece_type) { .color = switch (piece_type) {
// Type.o => color.yellow, Type.o => color.yellow,
// Type.i => color.cyan, Type.i => color.cyan,
// Type.l => color.orange, Type.l => color.orange,
// Type.j => color.blue, Type.j => color.blue,
// Type.s => color.green, Type.s => color.green,
// Type.z => color.red, Type.z => color.red,
// Type.t => color.purple, Type.t => color.purple,
//}, },
}; };
} }

View File

@ -16,11 +16,11 @@ grid_pos_x: i32,
grid_pos_y: i32, grid_pos_y: i32,
pub fn init(renderer: *Renderer) Self { pub fn init(renderer: *Renderer) Self {
//var wsize = renderer.getOutputSize(); var wsize = renderer.getOutputSize();
const grid_cell_size = @divFloor(@min(640, 480), 32); const grid_cell_size = @divFloor(@min(wsize.width, wsize.height), 32);
const grid_pos_x = @divFloor(640, 2) - (grid_cell_size * @divFloor(Grid.ncolumns, 2)); const grid_pos_x = @divFloor(wsize.width, 2) - (grid_cell_size * @divFloor(Grid.ncolumns, 2));
const grid_pos_y = @divFloor(480, 2) - (grid_cell_size * @divFloor(Grid.nrows + Grid.buffer, 2)); const grid_pos_y = @divFloor(wsize.height, 2) - (grid_cell_size * @divFloor(Grid.nrows + Grid.buffer, 2));
return Self{ return Self{
.renderer = renderer, .renderer = renderer,
.grid_pos_x = grid_pos_x, .grid_pos_x = grid_pos_x,
@ -30,15 +30,8 @@ pub fn init(renderer: *Renderer) Self {
} }
pub fn renderBag(self: *Self, game: Game) void { pub fn renderBag(self: *Self, game: Game) void {
_ = self; const pos_x = self.grid_pos_x + ((Grid.ncolumns + 1) * self.grid_cell_size);
const pos_y = self.grid_pos_y + (Grid.buffer * self.grid_cell_size);
//const pos_x = self.grid_pos_x + ((Grid.ncolumns + 1) * self.grid_cell_size);
//const pos_y = self.grid_pos_y + (Grid.buffer * self.grid_cell_size);
// var r: u8 = 0;
// var g: u8 = 0;
// var b: u8 = 0;
// var a: u8 = 255;
for (game.bag.contents[0..5], 0..) |_, i| { for (game.bag.contents[0..5], 0..) |_, i| {
if (game.bag.contents[i]) |piece_type| { if (game.bag.contents[i]) |piece_type| {
@ -46,25 +39,25 @@ pub fn renderBag(self: *Self, game: Game) void {
for (piece.structure, 0..) |_, y| { for (piece.structure, 0..) |_, y| {
for (piece.structure[y], 0..) |_, x| { for (piece.structure[y], 0..) |_, x| {
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;
var alpha: u8 = 50;
if (piece.structure[y][x]) { if (piece.structure[y][x]) {
//r = piece.color.r; alpha = 255;
//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;
} }
//const new_x = pos_x + @as(i32, @intCast(x)) * self.grid_cell_size; self.renderer.setColor([_]u8{
//const new_y = pos_y + (@as(i32, @intCast(y)) + @as(i32, @intCast(i * piece.structure.len))) * self.grid_cell_size; piece.color[0] -| 30, // r
piece.color[1] -| 30, // g
piece.color[2] -| 30, // b
alpha,
});
self.renderer.fillRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size);
//self.renderer.setColor(r, g, b, a); self.renderer.setColor(.{ piece.color[0], piece.color[1], piece.color[2], alpha });
//self.renderer.fillRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size); self.renderer.fillRectangle(new_x+1, new_y+1, self.grid_cell_size-2, self.grid_cell_size-2);
//self.renderer.setColor(r -| 30, g -| 30, b -| 30, a);
//self.renderer.drawRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size);
} }
} }
} }
@ -72,40 +65,33 @@ pub fn renderBag(self: *Self, game: Game) void {
} }
pub fn renderHeld(self: *Self, game: Game) void { pub fn renderHeld(self: *Self, game: Game) void {
_ = self; const pos_x = self.grid_pos_x - (5 * self.grid_cell_size);
const pos_y = self.grid_pos_y + (Grid.buffer * self.grid_cell_size);
//const pos_x = self.grid_pos_x - (5 * self.grid_cell_size);
//const pos_y = self.grid_pos_y + (Grid.buffer * self.grid_cell_size);
// var r: u8 = 0;
// var g: u8 = 0;
// var b: u8 = 0;
// var a: u8 = 255;
if (game.held) |held_type| { if (game.held) |held_type| {
var held = Piece.init(held_type); var held = Piece.init(held_type);
for (held.structure, 0..) |_, y| { for (held.structure, 0..) |_, y| {
for (held.structure[y], 0..) |_, x| { for (held.structure[y], 0..) |_, x| {
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;
var alpha: u8 = 50;
if (held.structure[y][x]) { if (held.structure[y][x]) {
//r = held.color.r; alpha = 255;
//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;
} }
//const new_x = pos_x + @as(i32, @intCast(x)) * self.grid_cell_size; self.renderer.setColor([_]u8{
//const new_y = pos_y + @as(i32, @intCast(y)) * self.grid_cell_size; held.color[0] -| 30, // r
held.color[1] -| 30, // g
held.color[2] -| 30, // b
alpha,
});
self.renderer.fillRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size);
// self.renderer.setColor(r, g, b, a); self.renderer.setColor(.{ held.color[0], held.color[1], held.color[2], alpha });
//self.renderer.fillRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size); self.renderer.fillRectangle(new_x+1, new_y+1, self.grid_cell_size-2, self.grid_cell_size-2);
// self.renderer.setColor(r -| 30, g -| 30, b -| 30, a);
//self.renderer.drawRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size);
} }
} }
} }
@ -120,10 +106,8 @@ pub fn render(self: *Self, game: Game) void {
} }
pub fn renderPiece(self: *Self, piece: Piece) void { pub fn renderPiece(self: *Self, piece: Piece) void {
_ = self; const pos_x = self.grid_pos_x;
const pos_y = self.grid_pos_y;
//const pos_x = self.grid_pos_x;
//const pos_y = self.grid_pos_y;
for (piece.structure, 0..) |_, y| { for (piece.structure, 0..) |_, y| {
for (piece.structure[y], 0..) |_, x| { for (piece.structure[y], 0..) |_, x| {
@ -132,46 +116,36 @@ pub fn renderPiece(self: *Self, piece: Piece) void {
continue; continue;
} }
//var r = piece.color.r; const new_x = pos_x + (@as(i32, @intCast(x)) + piece.col) * self.grid_cell_size;
//var g = piece.color.g; const new_y = pos_y + (@as(i32, @intCast(y)) + piece.row) * self.grid_cell_size;
//var b = piece.color.b;
//var a = piece.color.a;
//const new_x = pos_x + (@as(i32, @intCast(x)) + piece.col) * self.grid_cell_size; self.renderer.setColor(.{
//const new_y = pos_y + (@as(i32, @intCast(y)) + piece.row) * self.grid_cell_size; piece.color[0] -| 30,
piece.color[1] -| 30,
//self.renderer.setColor(r, g, b, a); piece.color[2] -| 30,
//self.renderer.fillRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size); piece.color[3],
//self.renderer.setColor(r -| 30, g -| 30, b -| 30, a); });
//self.renderer.drawRectangle(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);
self.renderer.setColor(piece.color);
self.renderer.fillRectangle(new_x+1, new_y+1, self.grid_cell_size-2, self.grid_cell_size-2);
} }
} }
} }
pub fn renderGrid(self: *Self, grid: Grid) void { pub fn renderGrid(self: *Self, grid: Grid) void {
_ = self; const pos_x = self.grid_pos_x;
const pos_y = self.grid_pos_y;
//const pos_x = self.grid_pos_x;
//const pos_y = self.grid_pos_y;
//const lg = color.light_grey;
var visible = grid.cells[Grid.buffer..]; var visible = grid.cells[Grid.buffer..];
for (visible, 0..) |_, y| { for (visible, 0..) |_, y| {
for (visible[y], 0..) |_, x| { for (visible[y], 0..) |_, x| {
_ = x; 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;
//var r = visible[y][x].color.r; self.renderer.setColor(color.light_grey);
//var g = visible[y][x].color.g; self.renderer.fillRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size);
//var b = visible[y][x].color.b; self.renderer.setColor(visible[y][x].color);
//var a = visible[y][x].color.a; self.renderer.fillRectangle(new_x+1, new_y+1, self.grid_cell_size-2, self.grid_cell_size-2);
//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;
//self.renderer.setColor(r, g, b, a);
//self.renderer.fillRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size);
//self.renderer.setColor(lg.r, lg.g, lg.b, lg.a);
//self.renderer.drawRectangle(new_x, new_y, self.grid_cell_size, self.grid_cell_size);
} }
} }
} }

View File

@ -1,22 +1,11 @@
const std = @import("std"); const std = @import("std");
//const sdl = @import("sdl2");
//const zlm = @import("zlm");
//const gl = @import("zgl");
//const m = zlm.SpecializeOn(f32);
const rl = @import("raylib.zig"); const rl = @import("raylib.zig");
const Self = @This(); const Self = @This();
const Color = [4]u8; const Color = @import("color.zig").Color;
color: Color = .{ 0, 0, 0, 0 }, color: Color = .{ 0, 0, 0, 0 },
// There's a vbo for each shader program
vbo: [max_objects]f32 = .{0.0} ** max_objects,
vbo_index: usize = 0,
// The index of the program is the enum converted to int
//textures: [max_objects]Texture = .{undefined} ** max_objects,
const max_objects: usize = 16384; const max_objects: usize = 16384;
const quadSize: usize = 9 * 6; const quadSize: usize = 9 * 6;
@ -29,13 +18,12 @@ pub fn init() !Self {
} }
pub fn render(self: *Self) void { pub fn render(self: *Self) void {
_ = self;
rl.endDrawing(); rl.endDrawing();
rl.beginDrawing(); rl.beginDrawing();
rl.clearBackground(.{ 232, 216, 166, 255 }); rl.clearBackground(.{ 232, 216, 166, 255 });
//self.setColor(.{0,0,0,255});
self.fillRectangle(10, 10, 100, 100);
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {

View File

@ -1,68 +1,13 @@
const SDL = @import("sdl2"); pub const Color = [4]u8;
pub const yellow = SDL.Color{ pub const yellow = Color{ 232, 216, 165, 255 };
.r = 232, pub const brown = Color{ 180, 130, 90, 255 };
.g = 216, pub const cyan = Color{ 138, 167, 172, 255 };
.b = 165, pub const orange = Color{ 222, 154, 40, 255 };
.a = 255, pub const blue = Color{ 112, 123, 136, 255 };
}; pub const green = Color{ 142, 146, 87, 255 };
pub const brown = SDL.Color{ pub const red = Color{ 229, 93, 77, 255 };
.r = 180, pub const purple = Color{ 180, 171, 189, 255 };
.g = 130, pub const pink = Color{ 230, 115, 170, 255 };
.b = 90, pub const dark_grey = Color{ 40, 40, 40, 255 };
.a = 255, pub const light_grey = Color{ 80, 80, 80, 255 };
};
pub const cyan = SDL.Color{
.r = 138,
.g = 167,
.b = 172,
.a = 255,
};
pub const orange = SDL.Color{
.r = 222,
.g = 154,
.b = 40,
.a = 255,
};
pub const blue = SDL.Color{
.r = 112,
.g = 123,
.b = 136,
.a = 255,
};
pub const green = SDL.Color{
.r = 142,
.g = 146,
.b = 87,
.a = 255,
};
pub const red = SDL.Color{
.r = 229,
.g = 93,
.b = 77,
.a = 255,
};
pub const purple = SDL.Color{
.r = 180,
.g = 171,
.b = 189,
.a = 255,
};
pub const pink = SDL.Color{
.r = 230,
.g = 115,
.b = 170,
.a = 255,
};
pub const dark_grey = SDL.Color{
.r = 40,
.g = 40,
.b = 40,
.a = 255,
};
pub const light_grey = SDL.Color{
.r = 80,
.g = 80,
.b = 80,
.a = 255,
};