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

View File

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

View File

@ -50,7 +50,7 @@ dropped: bool = false,
swapped: bool = false,
piece_type: Type,
//color: SDL.Color,
color: color.Color,
pub fn init(piece_type: Type) Self {
return Self{
@ -105,15 +105,15 @@ pub fn init(piece_type: Type) Self {
},
},
//.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,
//},
.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,
},
};
}

View File

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

View File

@ -1,22 +1,11 @@
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 Self = @This();
const Color = [4]u8;
const Color = @import("color.zig").Color;
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 quadSize: usize = 9 * 6;
@ -29,13 +18,12 @@ pub fn init() !Self {
}
pub fn render(self: *Self) void {
_ = self;
rl.endDrawing();
rl.beginDrawing();
rl.clearBackground(.{ 232, 216, 166, 255 });
//self.setColor(.{0,0,0,255});
self.fillRectangle(10, 10, 100, 100);
}
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{
.r = 232,
.g = 216,
.b = 165,
.a = 255,
};
pub const brown = SDL.Color{
.r = 180,
.g = 130,
.b = 90,
.a = 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,
};
pub const yellow = Color{ 232, 216, 165, 255 };
pub const brown = Color{ 180, 130, 90, 255 };
pub const cyan = Color{ 138, 167, 172, 255 };
pub const orange = Color{ 222, 154, 40, 255 };
pub const blue = Color{ 112, 123, 136, 255 };
pub const green = Color{ 142, 146, 87, 255 };
pub const red = Color{ 229, 93, 77, 255 };
pub const purple = Color{ 180, 171, 189, 255 };
pub const pink = Color{ 230, 115, 170, 255 };
pub const dark_grey = Color{ 40, 40, 40, 255 };
pub const light_grey = Color{ 80, 80, 80, 255 };