From 3e552932adeb2ee06ab6a40f0a0248ee7902fc30 Mon Sep 17 00:00:00 2001 From: dusk Date: Sun, 24 Sep 2023 19:11:25 +0200 Subject: [PATCH] Texture support WIP --- src/Game/Renderer.zig | 8 ++--- src/Renderer.zig | 75 ++++++++++++++++--------------------------- src/raylib.zig | 45 ++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 52 deletions(-) diff --git a/src/Game/Renderer.zig b/src/Game/Renderer.zig index b264fd9..5887f4d 100644 --- a/src/Game/Renderer.zig +++ b/src/Game/Renderer.zig @@ -57,7 +57,7 @@ pub fn renderBag(self: *Self, game: Game) void { self.renderer.fillRectangle(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); + self.renderer.fillRectangle(new_x + 1, new_y + 1, self.grid_cell_size - 2, self.grid_cell_size - 2); } } } @@ -91,7 +91,7 @@ pub fn renderHeld(self: *Self, game: Game) void { self.renderer.fillRectangle(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); + self.renderer.fillRectangle(new_x + 1, new_y + 1, self.grid_cell_size - 2, self.grid_cell_size - 2); } } } @@ -127,7 +127,7 @@ pub fn renderPiece(self: *Self, piece: Piece) void { }); 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); + self.renderer.fillRectangle(new_x + 1, new_y + 1, self.grid_cell_size - 2, self.grid_cell_size - 2); } } } @@ -145,7 +145,7 @@ pub fn renderGrid(self: *Self, grid: Grid) void { 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); + self.renderer.fillRectangle(new_x + 1, new_y + 1, self.grid_cell_size - 2, self.grid_cell_size - 2); } } } diff --git a/src/Renderer.zig b/src/Renderer.zig index 902e225..1b219f3 100644 --- a/src/Renderer.zig +++ b/src/Renderer.zig @@ -2,9 +2,10 @@ const std = @import("std"); const rl = @import("raylib.zig"); const Self = @This(); -const Color = @import("color.zig").Color; +const colors = @import("color.zig"); -color: Color = .{ 0, 0, 0, 0 }, +color: colors.Color = .{ 0, 0, 0, 0 }, +textureTest: Texture, const max_objects: usize = 16384; const quadSize: usize = 9 * 6; @@ -12,14 +13,17 @@ const quadSize: usize = 9 * 6; pub fn init() !Self { rl.initWindow(1280, 720, "USG", 120); - var renderer = Self{}; + var renderer = Self{ + .textureTest = Texture.init("res/cell.png"), + }; return renderer; } pub fn render(self: *Self) void { - _ = self; + //_ = self; + self.textureTest.drawTo(20, 100, 160, 160, colors.pink); rl.drawFPS(10, 10); rl.endDrawing(); @@ -59,7 +63,7 @@ pub fn drawText(self: Self, text: [:0]const u8, x: i32, y: i32, size: i32) void rl.drawText(text, x, y, size, self.color); } -pub fn setColor(self: *Self, color: Color) void { +pub fn setColor(self: *Self, color: colors.Color) void { self.color = color; } @@ -72,46 +76,21 @@ pub fn getOutputSize(self: Self) OutputSize { }; } -//pub const Texture = struct { -// texture: gl.Texture, -// width: usize, -// height: usize, -// -// pub fn init(data: [*]const u8, width: usize, height: usize) Texture { -// var tex = gl.genTexture(); -// //defer gl.Texture.delete(tex); -// gl.bindTexture(tex, .@"2d"); -// -// gl.textureImage2D(.@"2d", 0, .rgba, width, height, .rgba, .unsigned_byte, data); -// -// gl.texParameter(.@"2d", .wrap_s, .clamp_to_edge); -// gl.texParameter(.@"2d", .wrap_r, .clamp_to_edge); -// gl.texParameter(.@"2d", .min_filter, .linear); -// gl.texParameter(.@"2d", .mag_filter, .linear); -// -// return Texture{ -// .texture = tex, -// .width = width, -// .height = height, -// }; -// } -// -// pub fn fromText(text: [:0]const u8, size: c_int) Texture { -// var font = sdl.ttf.openFont("res/fonts/MarginaliaRegular-8XlZ.ttf", size) catch unreachable; -// defer font.close(); -// -// var surface = font.renderTextBlended(text, sdl.Color.white) catch unreachable; -// defer surface.destroy(); -// -// const width = @intCast(usize, surface.ptr.w); -// const height = @intCast(usize, surface.ptr.h); -// var newsurf = sdl.c.SDL_ConvertSurfaceFormat(surface.ptr, @enumToInt(sdl.PixelFormatEnum.argb8888), 0) orelse unreachable; -// const data = @ptrCast([*]const u8, newsurf.pixels); -// -// return Texture.init(data, width, height); -// } -// -// pub fn deinit(self: *Texture) void { -// gl.Texture.delete(self.texture); -// } -//}; +pub const Texture = struct { + texture: rl.Texture, + + pub fn init(path: [*]const u8) Texture { + return .{ + .texture = rl.Texture.load(path), + }; + } + + pub fn drawTo(self: Texture, x: i32, y: i32, w: i32, h: i32, tint: [4]u8) void { + std.debug.print("\n\n I love penis \n\n", .{}); + self.texture.drawTo(x, y, w, h, tint); + } + + pub fn deinit(self: *Texture) void { + self.texture.unload(); + } +}; diff --git a/src/raylib.zig b/src/raylib.zig index 732f5ff..59b77bf 100644 --- a/src/raylib.zig +++ b/src/raylib.zig @@ -66,6 +66,51 @@ pub fn drawTriangle(v1: [2]f32, v2: [2]f32, v3: [2]f32, color: [4]u8) void { ); } +pub const Texture = struct { + texture: c.Texture2D, + + pub fn load(file_name: [*c]const u8) Texture { + const aux = .{ + .texture = c.LoadTexture(file_name), + }; + c.SetTextureFilter(aux.texture, c.TEXTURE_FILTER_BILINEAR); + return aux; + } + + pub fn drawTo(self: Texture, x: i32, y: i32, w: i32, h: i32, tint: [4]u8) void { + std.debug.print("\n\nachooooo\n\n", .{}); + const srcRec: c.Rectangle = .{ + .x = 0, + .y = 0, + .width = @floatFromInt(self.texture.width), + .height = @floatFromInt(self.texture.height), + }; + std.debug.print("\n\nnaguru\n\n", .{}); + + const dstRec: c.Rectangle = .{ + .x = @floatFromInt(x), + .y = @floatFromInt(y), + .width = @floatFromInt(w), + .height = @floatFromInt(h), + }; + + std.debug.print("\n\nHAstta aqui hemos llegao\n\n", .{}); + c.DrawTexturePro( + self.texture, + srcRec, + dstRec, + .{ .x = 0, .y = 0 }, + 0, + .{ .r = tint[0], .g = tint[1], .b = tint[2], .a = tint[3] }, + ); + std.debug.print("\n\nlmaolol\n\n", .{}); + } + + pub fn unload(self: Texture) void { + c.UnloadTexture(self.texture); + } +}; + pub fn getScreenWidth() i32 { return c.GetScreenWidth(); }