Texture support WIP

This commit is contained in:
Dusk 2023-09-24 19:11:25 +02:00
parent b666e83a0c
commit 3e552932ad
3 changed files with 76 additions and 52 deletions

View File

@ -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();
}
};

View File

@ -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();
}