diff --git a/src/Color.zig b/src/Color.zig new file mode 100644 index 0000000..225ec68 --- /dev/null +++ b/src/Color.zig @@ -0,0 +1,30 @@ + +const Color = @This(); + +r: f32, +g: f32, +b: f32, +a: f32, + +pub fn rgba(r: f32, g: f32, b: f32, a: f32) Color { + return .{ + .r = r, + .g = g, + .b = b, + .a = a, + }; +} + +pub const WHITE = rgba(1, 1, 1, 1); +pub const RED = rgba(1, 0, 0, 1); +pub const ORANGE = rgba(1, 0.5, 0, 1); +pub const YELLOW = rgba(1, 1, 0, 1); +pub const LIME = rgba(0.5, 1, 0, 1); +pub const GREEN = rgba(0, 1, 0, 1); +pub const TEAL = rgba(0, 1, 0.5, 1); +pub const CYAN = rgba(0, 1, 1, 1); +pub const LIGHT_BLUE = rgba(0, 0.5, 1, 1); +pub const BLUE = rgba(0, 0, 1, 1); +pub const INDIGO = rgba(0.5, 0, 1, 1); +pub const MAGENTA = rgba(1, 0, 1, 1); +pub const HOT_PINK = rgba(1, 0, 0.5, 1); diff --git a/src/Sprite.zig b/src/Sprite.zig index b9bf0fa..2432467 100644 --- a/src/Sprite.zig +++ b/src/Sprite.zig @@ -1,6 +1,7 @@ const Rectf = @import("geometry/Rectf.zig"); const Recti = @import("geometry/Recti.zig"); const Vec2f = @import("geometry/Vec2f.zig"); +const Color = @import("Color.zig"); const Texture = @import("Texture.zig"); const Layer = @import("Layer.zig"); const shaders = @import("shaders.zig"); @@ -23,23 +24,22 @@ pub fn create(tex: *const Texture, rect: Recti) Sprite { }; } -pub fn draw(self: *const Sprite, screen_pos: *const Recti, layer: Layer) void { +pub fn draw(self: *const Sprite, screen_pos: *const Recti, layer: Layer, color: Color) void { self.texture.bind(c.GL_TEXTURE0); c.glBegin(c.GL_QUADS); { c.glVertexAttrib2f(shaders.TEXCOORD_ATTRIBUTE_ID, self.texture_area.a.x, self.texture_area.a.y); - c.glVertexAttrib4f(shaders.COLOR_ATTRIBUTE_ID, 1, 1, 1, 1); + c.glVertexAttrib4f(shaders.COLOR_ATTRIBUTE_ID, color.r, color.g, color.b, color.a); c.glVertex3i(screen_pos.a.x, screen_pos.a.y, layer.z_index); c.glVertexAttrib2f(shaders.TEXCOORD_ATTRIBUTE_ID, self.texture_area.b.x, self.texture_area.a.y); - c.glVertexAttrib4f(shaders.COLOR_ATTRIBUTE_ID, 1, 1, 1, 1); + c.glVertexAttrib4f(shaders.COLOR_ATTRIBUTE_ID, color.r, color.g, color.b, color.a); c.glVertex3i(screen_pos.b.x, screen_pos.a.y, layer.z_index); c.glVertexAttrib2f(shaders.TEXCOORD_ATTRIBUTE_ID, self.texture_area.b.x, self.texture_area.b.y); - c.glVertexAttrib4f(shaders.COLOR_ATTRIBUTE_ID, 1, 1, 1, 1); + c.glVertexAttrib4f(shaders.COLOR_ATTRIBUTE_ID, color.r, color.g, color.b, color.a); c.glVertex3i(screen_pos.b.x, screen_pos.b.y, layer.z_index); c.glVertexAttrib2f(shaders.TEXCOORD_ATTRIBUTE_ID, self.texture_area.a.x, self.texture_area.b.y); - c.glVertexAttrib4f(shaders.COLOR_ATTRIBUTE_ID, 1, 1, 1, 1); + c.glVertexAttrib4f(shaders.COLOR_ATTRIBUTE_ID, color.r, color.g, color.b, color.a); c.glVertex3i(screen_pos.a.x, screen_pos.b.y, layer.z_index); } c.glEnd(); -} - +} diff --git a/src/engine.zig b/src/engine.zig index f84cdb3..74c444f 100644 --- a/src/engine.zig +++ b/src/engine.zig @@ -5,6 +5,7 @@ const shaders = @import("shaders.zig"); const Texture = @import("Texture.zig"); const Sprite = @import("Sprite.zig"); const Layer = @import("Layer.zig"); +const Color = @import("Color.zig"); const c = @cImport({ @cInclude("glad/glad.h"); @@ -73,11 +74,11 @@ pub fn run() !void { c.glClearColor(clearBrightness, clearBrightness, clearBrightness, 1.0); // c.glEnable(c.GL_MULTISAMPLE); c.glDisable(c.GL_MULTISAMPLE); - // c.glEnable(c.GL_BLEND); - // c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA); - // c.glEnable(c.GL_DEPTH_TEST); - // c.glDepthFunc(c.GL_LEQUAL); - // c.glDepthMask(c.GL_TRUE); + c.glEnable(c.GL_BLEND); + c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA); + c.glEnable(c.GL_DEPTH_TEST); + c.glDepthFunc(c.GL_LEQUAL); + c.glDepthMask(c.GL_TRUE); var width: c_int = undefined; var height: c_int = undefined; @@ -103,7 +104,9 @@ pub fn run() !void { // // c.glVertexAttrib4f(1, 0, 0, 1, 1); // // c.glEnd(); - sprite.draw(&Recti.from_xywh(100, 100, 32, 64), Layer.FLOOR); + sprite.draw(&Recti.from_xywh(120, 100, 32, 64), Layer.ENTITIES, Color.LIGHT_BLUE); + sprite.draw(&Recti.from_xywh(100, 100, 32, 64), Layer.FLOOR, Color.WHITE); + sprite.draw(&Recti.from_xywh(80, 100, 32, 64), Layer.ENTITIES, Color.INDIGO); // draw.rect(&geometry.Rect.from_xywh(100, 100, 256, 256), draw.Layer.FLOOR); // game.render(); diff --git a/src/geometry.zig b/src/geometry.zig deleted file mode 100644 index 6312c9f..0000000 --- a/src/geometry.zig +++ /dev/null @@ -1,49 +0,0 @@ -const Vec2i = @import("geometry/Vec2i.zig"); - -pub const Recti = struct { - x: i32, - y: i32, - w: i32, - h: i32, - a: Vec2i, - b: Vec2i, - - pub fn from_xywh(x: i32, y: i32, w: i32, h: i32) Recti { - return .{ - .x = x, - .y = y, - .w = w, - .h = h, - .a = Vec2i.new(x, y), - .b = Vec2i.new(x + w, y + h), - }; - } -}; - -pub const Matrix4f = struct { - values: [16]f32 = identity, - - const SIZE: i8 = 4 * 4; - const identity = from_values([16]f32 { - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1, - }); - - pub fn from_values(values: [16]f32) Matrix4f { - return .{ .values = values }; - } - - pub fn orthographic(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) Matrix4f { - var ortho = Matrix4f.identity; - ortho.values[0 + 0 * 4] = 2.0 / (right - left); - ortho.values[1 + 1 * 4] = 2.0 / (top - bottom); - ortho.values[2 + 2 * 4] = 2.0 / (near - far); - - ortho.values[0 + 3 * 4] = (left + right) / (left - right); - ortho.values[1 + 3 * 4] = (bottom + top) / (bottom - top); - ortho.values[2 + 3 * 4] = (far + near) / (far - near); - return ortho; - } -};