refactoring complete! i hope
parent
2f146a9a14
commit
07fb08b35e
|
|
@ -1,10 +1,21 @@
|
|||
const std = @import("std");
|
||||
const Color = @This();
|
||||
|
||||
r: f32,
|
||||
g: f32,
|
||||
b: f32,
|
||||
a: f32,
|
||||
const FRGBA = struct {
|
||||
r: f32,
|
||||
g: f32,
|
||||
b: f32,
|
||||
a: f32,
|
||||
};
|
||||
const URGBA = struct {
|
||||
r: u8,
|
||||
g: u8,
|
||||
b: u8,
|
||||
a: u8,
|
||||
};
|
||||
|
||||
f: FRGBA,
|
||||
u: URGBA,
|
||||
|
||||
pub fn blend(a: *const Color, b: *const Color, t: f32) Color {
|
||||
const real_t = 1 - (if (t < 0.0) 0.0 else if (t > 1.0) 1.0 else t);
|
||||
|
|
@ -16,22 +27,35 @@ pub fn blend(a: *const Color, b: *const Color, t: f32) Color {
|
|||
);
|
||||
}
|
||||
|
||||
fn to_u8(n: f32) u8 {
|
||||
const temp: u16 = @intFromFloat(std.math.round(n * 255));
|
||||
return if (temp >= 255) 255 else if (temp <= 0) 0 else @intCast(temp);
|
||||
}
|
||||
|
||||
pub fn rgba(r: f32, g: f32, b: f32, a: f32) Color {
|
||||
return .{
|
||||
.r = r,
|
||||
.g = g,
|
||||
.b = b,
|
||||
.a = a,
|
||||
return Color {
|
||||
.f = .{
|
||||
.r = r,
|
||||
.g = g,
|
||||
.b = b,
|
||||
.a = a,
|
||||
},
|
||||
.u = .{
|
||||
.r = to_u8(r),
|
||||
.g = to_u8(g),
|
||||
.b = to_u8(b),
|
||||
.a = to_u8(a),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn with_opacity(self: *const Color, a: f32) Color {
|
||||
return .{
|
||||
.r = self.r,
|
||||
.g = self.g,
|
||||
.b = self.b,
|
||||
.a = a * self.a,
|
||||
};
|
||||
return Color.rgba(
|
||||
self.f.r,
|
||||
self.f.g,
|
||||
self.f.b,
|
||||
a * self.f.a
|
||||
);
|
||||
}
|
||||
|
||||
// r: 0 - 360
|
||||
|
|
|
|||
|
|
@ -22,17 +22,22 @@ const SDL_RenderClear = c.SDL_RenderClear;
|
|||
const SDL_RenderPresent = c.SDL_RenderPresent;
|
||||
const SDL_Quit = c.SDL_Quit;
|
||||
|
||||
pub const Error = error {
|
||||
WindowNotInitialized,
|
||||
WindowAlreadyInitialized,
|
||||
RendererNotInitialized,
|
||||
RendererAlreadyInitialized,
|
||||
NoScene,
|
||||
AlreadyRunning
|
||||
const SDL_Data = struct {
|
||||
window: *c.SDL_Window,
|
||||
renderer: *c.SDL_Renderer,
|
||||
ready: bool = false,
|
||||
};
|
||||
|
||||
var window: ?*c.SDL_Window = null;
|
||||
var renderer: ?*c.SDL_Renderer = null;
|
||||
pub var sdl_data: SDL_Data = .{
|
||||
.ready = false,
|
||||
.renderer = undefined,
|
||||
.window = undefined,
|
||||
};
|
||||
|
||||
var hovered: bool = true;
|
||||
var window_width: i32 = 0;
|
||||
var window_height: i32 = 0;
|
||||
pub var mouse_pos: ?Vec2i = null;
|
||||
|
||||
var current_scene: ?Scene = null;
|
||||
var next_scene: ?Scene = null;
|
||||
|
|
@ -42,17 +47,24 @@ const LoaderFn = *const fn (*c.SDL_Window, *c.SDL_Renderer) void;
|
|||
var loader: ?LoaderFn = null;
|
||||
|
||||
pub fn setup() !void {
|
||||
if (window != null) return Error.WindowAlreadyInitialized;
|
||||
if (renderer != null) return Error.RendererAlreadyInitialized;
|
||||
if (sdl_data.ready) return error.WindowAndRendererAlreadyInitialized;
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
std.debug.panic("SDL failed to initialize with error: {s}", .{ SDL_GetError() });
|
||||
}
|
||||
if (!c.SDL_CreateWindowAndRenderer("SDL3", 800, 600, 0, &window, &renderer)) {
|
||||
var temp_window: ?*c.SDL_Window = null;
|
||||
var temp_renderer: ?*c.SDL_Renderer = null;
|
||||
if (!c.SDL_CreateWindowAndRenderer("SDL3", 800, 600, 0, &temp_window, &temp_renderer)) {
|
||||
std.debug.panic("SDL failed to initialize window or renderer with error: {s}", .{ SDL_GetError() });
|
||||
}
|
||||
sdl_data.window = temp_window.?;
|
||||
sdl_data.renderer = temp_renderer.?;
|
||||
sdl_data.ready = true;
|
||||
_ = c.SDL_SetRenderDrawBlendMode(sdl_data.renderer, c.SDL_BLENDMODE_BLEND);
|
||||
_ = c.SDL_SetWindowResizable(sdl_data.window, true);
|
||||
|
||||
if (loader) |loader_fn| {
|
||||
loader_fn(window.?, renderer.?);
|
||||
loader_fn(sdl_data.window, sdl_data.renderer);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -69,22 +81,96 @@ pub fn set_scene(scene: Scene) void {
|
|||
}
|
||||
|
||||
pub fn run() !void {
|
||||
if (window == null) return Error.WindowNotInitialized;
|
||||
if (renderer == null) return Error.RendererNotInitialized;
|
||||
if (current_scene != null) return Error.AlreadyRunning;
|
||||
if (next_scene == null) return Error.NoScene;
|
||||
if (!sdl_data.ready) return error.WindowOrRendererNotInitialized;
|
||||
if (next_scene == null) return error.NoScene;
|
||||
running = true;
|
||||
|
||||
var event: SDL_Event = undefined;
|
||||
while (running) {
|
||||
_ = c.SDL_SetRenderDrawColor(renderer, 127, 127, 127, 255);
|
||||
_ = c.SDL_RenderClear(renderer);
|
||||
_ = c.SDL_RenderPresent(renderer);
|
||||
_ = c.SDL_SetRenderDrawColor(sdl_data.renderer, 10, 10, 10, 255);
|
||||
_ = c.SDL_RenderClear(sdl_data.renderer);
|
||||
|
||||
if (next_scene != null) {
|
||||
if (current_scene != null) current_scene.?.destroy();
|
||||
current_scene = next_scene;
|
||||
next_scene = null;
|
||||
// start our scene
|
||||
try current_scene.?.start();
|
||||
// and send it the appropriate resize.
|
||||
current_scene.?.resize(window_width, window_height);
|
||||
if (hovered) current_scene.?.mouse_enter();
|
||||
if (mouse_pos) |pos| current_scene.?.mouse_move(pos.x, pos.y);
|
||||
}
|
||||
|
||||
current_scene.?.draw();
|
||||
|
||||
_ = c.SDL_RenderPresent(sdl_data.renderer);
|
||||
|
||||
while(c.SDL_PollEvent(&event)) {
|
||||
if (event.type == c.SDL_EVENT_QUIT) {
|
||||
running = false;
|
||||
switch (event.type) {
|
||||
c.SDL_EVENT_QUIT => running = false, // ignore close requested, unless doing autosaves.
|
||||
c.SDL_EVENT_WINDOW_CLOSE_REQUESTED => {},
|
||||
|
||||
c.SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED => set_window_size(event.window.data1, event.window.data2),
|
||||
c.SDL_EVENT_WINDOW_RESIZED => {}, // ignore resized bc it doesnt happen on startup.
|
||||
c.SDL_EVENT_WINDOW_SAFE_AREA_CHANGED => {}, // same with safe area, might have
|
||||
|
||||
c.SDL_EVENT_WINDOW_MOVED => {},
|
||||
c.SDL_EVENT_WINDOW_MOUSE_ENTER => mouse_enter(),
|
||||
c.SDL_EVENT_WINDOW_MOUSE_LEAVE => mouse_leave(),
|
||||
|
||||
c.SDL_EVENT_MOUSE_MOTION => mouse_move(event.motion.x, event.motion.y),
|
||||
c.SDL_EVENT_MOUSE_BUTTON_DOWN => mouse_button_down(event.button.button),
|
||||
c.SDL_EVENT_MOUSE_BUTTON_UP => mouse_button_up(event.button.button),
|
||||
|
||||
c.SDL_EVENT_KEY_DOWN => {},
|
||||
c.SDL_EVENT_KEY_UP => {},
|
||||
|
||||
// log clutter
|
||||
c.SDL_EVENT_WINDOW_SHOWN => {},
|
||||
c.SDL_EVENT_WINDOW_EXPOSED => {},
|
||||
c.SDL_EVENT_WINDOW_FOCUS_GAINED => {},
|
||||
c.SDL_EVENT_WINDOW_FOCUS_LOST => {},
|
||||
else => std.debug.print("[Engine:events] Unknown Event: {}\n", .{ event.type }),
|
||||
}
|
||||
}
|
||||
|
||||
try current_scene.?.update(0.0042);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_window_size(width: i32, height: i32) void {
|
||||
window_width = width;
|
||||
window_height = height;
|
||||
std.debug.print("[Engine:set_window_size] {d} x {d}\n", .{ width, height });
|
||||
if (current_scene != null) current_scene.?.resize(width, height);
|
||||
}
|
||||
|
||||
fn mouse_move(fx: f32, fy: f32) void {
|
||||
const x: i32 = @intFromFloat(std.math.round(fx));
|
||||
const y: i32 = @intFromFloat(std.math.round(fy));
|
||||
mouse_pos = Vec2i { .x = x, .y = y };
|
||||
|
||||
if (current_scene != null and hovered) {
|
||||
current_scene.?.mouse_move(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
fn mouse_enter() void {
|
||||
hovered = true;
|
||||
}
|
||||
|
||||
fn mouse_leave() void {
|
||||
hovered = false;
|
||||
mouse_pos = null;
|
||||
}
|
||||
|
||||
fn mouse_button_down(button: i32) void {
|
||||
if (current_scene == null) return;
|
||||
current_scene.?.mouse_down(button);
|
||||
}
|
||||
|
||||
fn mouse_button_up(button: i32) void {
|
||||
if (current_scene == null) return;
|
||||
current_scene.?.mouse_up(button);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,24 +7,25 @@ const Scene = engine.Scene;
|
|||
const Recti = engine.geometry.Recti;
|
||||
const Layer = engine.Layer;
|
||||
|
||||
|
||||
|
||||
|
||||
const EntityOptions = struct {
|
||||
tag: Tag = Tag.NONE,
|
||||
layer: Layer,
|
||||
};
|
||||
|
||||
ptr: *anyopaque,
|
||||
tag: Tag,
|
||||
layer: Layer,
|
||||
|
||||
_name: *const fn(*const anyopaque) []const u8,
|
||||
_destroy: *const fn(*const anyopaque) void,
|
||||
_update: *const fn(*anyopaque, f32) void,
|
||||
_draw: *const fn(*const anyopaque) void,
|
||||
_draw_opacity: *const fn(*const anyopaque) void,
|
||||
|
||||
_start: *const fn(*anyopaque, *Scene) void,
|
||||
_resize: *const fn(*anyopaque, i32, i32) void,
|
||||
|
||||
|
||||
// mouse functions
|
||||
_mouse_ready: *const fn(*const anyopaque) bool,
|
||||
_mouse_enabled: *const fn(*const anyopaque) bool,
|
||||
|
|
@ -216,6 +217,7 @@ pub fn init(ptr: anytype, options: EntityOptions) Entity {
|
|||
return Entity {
|
||||
.ptr = ptr,
|
||||
.tag = options.tag,
|
||||
.layer = options.layer,
|
||||
._name = gen._name,
|
||||
._destroy = gen._destroy,
|
||||
._start = gen._start,
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
const engine = @import("engine");
|
||||
const Vec2i = engine.geometry.Vec2i;
|
||||
|
||||
|
||||
|
||||
pub var mouse_pos: ?Vec2i = Vec2i.create(0, 0);
|
||||
|
|
@ -8,6 +8,7 @@ const Entity = engine.Entity;
|
|||
const Vec2i = @import("geometry/Vec2i.zig");
|
||||
const Tag = @import("Tag.zig");
|
||||
const std = @import("std");
|
||||
const c = @import("c");
|
||||
const ArrayList = std.ArrayList;
|
||||
const AutoHashMap = std.AutoHashMap;
|
||||
|
||||
|
|
@ -23,7 +24,6 @@ mouse_pos: ?Vec2i = null,
|
|||
|
||||
started: bool = false,
|
||||
|
||||
|
||||
pub fn create() Scene {
|
||||
const self = Scene {
|
||||
.entities = ArrayList(Entity).init(std.heap.page_allocator),
|
||||
|
|
@ -43,22 +43,8 @@ pub fn destroy(self: *Scene) void {
|
|||
|
||||
pub fn start(self: *Scene) !void {
|
||||
std.debug.print("[Scene:start] Scene starting...\n", .{});
|
||||
std.debug.print("[Scene:start] entities: {}\n", .{ self.entities.items.len });
|
||||
|
||||
try self.ingest_queued_entities();
|
||||
self.started = true;
|
||||
for (self.entities.items) |*entity| {
|
||||
if (entity.tag.id == Tag.NONE.id) {
|
||||
std.debug.print("[Scene:start] - {s}\n", .{ entity.name() });
|
||||
} else {
|
||||
std.debug.print("[Scene:start] - {s} [{s}]\n", .{ entity.name(), entity.tag.type });
|
||||
}
|
||||
}
|
||||
|
||||
for (self.entities.items) |entity| {
|
||||
entity.start(self);
|
||||
}
|
||||
|
||||
try self.add_queued();
|
||||
}
|
||||
|
||||
pub fn get(self: *const Scene, tag: Tag, comptime T: type) *T {
|
||||
|
|
@ -92,42 +78,70 @@ pub fn add(self: *Scene, instance_ptr: anytype) !void {
|
|||
|
||||
// again, entities are fat pointers. they are okay to be copied
|
||||
const entity: Entity = instance_ptr.entity();
|
||||
|
||||
if (self.started) {
|
||||
try self.entities_to_add.append(entity);
|
||||
} else {
|
||||
try self.add_unsafe(entity);
|
||||
}
|
||||
}
|
||||
|
||||
inline fn add_queued(self: *Scene) !void {
|
||||
for (self.entities_to_add.items) |entity| {
|
||||
try self.entities.append(entity);
|
||||
entity.start(self);
|
||||
}
|
||||
self.entities_to_add.clearAndFree();
|
||||
}
|
||||
|
||||
inline fn add_unsafe(self: *Scene, entity: Entity) !void {
|
||||
std.debug.print("[Scene:add] Adding {s}\n", .{
|
||||
std.debug.print("[Scene:add] Queueing {s}\n", .{
|
||||
entity.name()
|
||||
});
|
||||
try self.entities.append(entity);
|
||||
if (entity.tag.id != Tag.NONE.id) {
|
||||
try self.tagged_entities.put(entity.tag.id, entity);
|
||||
try self.entities_to_add.append(entity);
|
||||
}
|
||||
|
||||
fn ingest_queued_entities(self: *Scene) !void {
|
||||
// TODO make this a while loop so entities that add entities add them on the same frame.
|
||||
var temp_entities: ArrayList(Entity) = try ArrayList(Entity).initCapacity(std.heap.page_allocator, self.entities_to_add.items.len);
|
||||
|
||||
for(self.entities_to_add.items) |entity| {
|
||||
std.debug.print("[Scene:ingest_queued_entities] Adding {s}\n", .{
|
||||
entity.name()
|
||||
});
|
||||
try temp_entities.append(entity);
|
||||
try self.entities.append(entity);
|
||||
|
||||
if (entity.tag.id != Tag.NONE.id) {
|
||||
try self.tagged_entities.put(entity.tag.id, entity);
|
||||
}
|
||||
|
||||
if (entity.mouse_ready()) {
|
||||
try self.mouse_ready_entities.append(entity);
|
||||
}
|
||||
}
|
||||
self.entities_to_add.clearAndFree();
|
||||
|
||||
std.mem.sort(Entity, self.mouse_ready_entities.items, {}, entity_layer_less_than);
|
||||
std.mem.sort(Entity, self.entities.items, {}, entity_draw_layer_less_than);
|
||||
|
||||
|
||||
// std.debug.print("[Scene.ingest_queued_entities] Sorted:\n", .{});
|
||||
// for(self.entities.items) |*entity| {
|
||||
// std.debug.print("[Scene:ingest_queued_entities] {d}: {s}\n", .{ entity.layer.z_index, entity.name() });
|
||||
// }
|
||||
|
||||
std.debug.print("[Scene:ingest_queued_entities] Starting {} entities\n", .{ temp_entities.items.len });
|
||||
|
||||
for(temp_entities.items) |entity| {
|
||||
entity.start(self);
|
||||
|
||||
if (entity.tag.id == Tag.NONE.id) {
|
||||
std.debug.print("[Scene:ingest_queued_entities] - {s}\n", .{ entity.name() });
|
||||
} else {
|
||||
std.debug.print("[Scene:ingest_queued_entities] - {s} [{s}]\n", .{ entity.name(), entity.tag.type });
|
||||
}
|
||||
}
|
||||
|
||||
if (entity.mouse_ready()) {
|
||||
try self.mouse_ready_entities.append(entity);
|
||||
std.mem.sort(Entity, self.mouse_ready_entities.items, {}, entity_layer_less_than);
|
||||
}
|
||||
}
|
||||
|
||||
fn entity_draw_layer_less_than(_: void, lhs: Entity, rhs: Entity) bool {
|
||||
return lhs.layer.z_index < rhs.layer.z_index;
|
||||
}
|
||||
|
||||
fn entity_layer_less_than(_: void, lhs: Entity, rhs: Entity) bool {
|
||||
return lhs.mouse_layer().z_index < rhs.mouse_layer().z_index;
|
||||
}
|
||||
|
||||
pub fn update(self: *Scene, dt: f32) void {
|
||||
pub fn update(self: *Scene, dt: f32) !void {
|
||||
|
||||
if (self.entities_to_add.items.len > 0) {
|
||||
try self.ingest_queued_entities();
|
||||
}
|
||||
|
||||
for (self.entities.items) |entity| {
|
||||
entity.update(dt);
|
||||
}
|
||||
|
|
@ -168,6 +182,7 @@ pub fn update(self: *Scene, dt: f32) void {
|
|||
|
||||
}
|
||||
|
||||
|
||||
pub fn draw(self: *Scene) void {
|
||||
for (self.entities.items) |entity| {
|
||||
entity.draw();
|
||||
|
|
@ -177,9 +192,9 @@ pub fn draw(self: *Scene) void {
|
|||
}
|
||||
for (self.mouse_ready_entities.items) |item| {
|
||||
if (self.current_hover_item != null and item.ptr == self.current_hover_item.?.ptr) {
|
||||
item.mouse_area().draw(item.mouse_layer(), Color.LIME.with_opacity(0.3));
|
||||
// item.mouse_area().draw(item.mouse_layer(), Color.LIME.with_opacity(0.3));
|
||||
} else {
|
||||
item.mouse_area().draw(item.mouse_layer(), Color.WHITE.with_opacity(0.3));
|
||||
// item.mouse_area().draw(item.mouse_layer(), Color.WHITE.with_opacity(0.3));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +1,43 @@
|
|||
const Sprite = @This();
|
||||
|
||||
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 Sprite = @This();
|
||||
const engine = @import("engine");
|
||||
const std = @import("std");
|
||||
const c = @import("c");
|
||||
|
||||
texture_area: Rectf = undefined,
|
||||
texture_area: Recti = undefined,
|
||||
texture: *const Texture = undefined,
|
||||
|
||||
pub fn create(tex: *const Texture, rect: Recti) Sprite {
|
||||
return .{
|
||||
.texture_area = rect.scalef(Vec2f.create(
|
||||
@floatFromInt(tex.width),
|
||||
@floatFromInt(tex.height)
|
||||
)),
|
||||
.texture_area = rect,
|
||||
.texture = tex,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn draw(self: *const Sprite, screen_pos: Recti, layer: Layer, color: Color) void {
|
||||
_ = self;
|
||||
_ = screen_pos;
|
||||
_ = layer;
|
||||
_ = color;
|
||||
@panic("Not Implemented");
|
||||
pub fn get_src_uv(self: *const Sprite) Rectf {
|
||||
return self.texture_area.scalef(Vec2f.create(
|
||||
@floatFromInt(self.texture.width),
|
||||
@floatFromInt(self.texture.height),
|
||||
));
|
||||
}
|
||||
|
||||
pub fn draw(self: *const Sprite, screen_pos: Recti, layer: Layer, color: Color) void {
|
||||
// _ = screen_pos;
|
||||
_ = layer;
|
||||
// _ = color;
|
||||
_ = c.SDL_SetRenderDrawColor(engine.Control.sdl_data.renderer, color.u.r, color.u.g, color.u.b, color.u.a);
|
||||
_ = c.SDL_SetTextureColorModFloat(self.texture.handle, color.f.r, color.f.g, color.f.b);
|
||||
_ = c.SDL_RenderTexture(
|
||||
engine.Control.sdl_data.renderer,
|
||||
self.texture.handle,
|
||||
&self.texture_area.to_sdl(),
|
||||
&screen_pos.to_sdl()
|
||||
);
|
||||
// std.debug.print("drew: {}\n", .{ drew });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,10 +4,8 @@ const std = @import("std");
|
|||
const c = @import("c");
|
||||
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}) {};
|
||||
// var gpa = Allocator {};
|
||||
var alloc = gpa.allocator();
|
||||
const alloc = gpa.allocator();
|
||||
|
||||
// var alloc: std.mem.Allocator = (std.heap.GeneralPurposeAllocator(.{}) {}).allocator();
|
||||
var exe_dir: []const u8 = undefined;
|
||||
var exe_dir_valid = false;
|
||||
|
||||
|
|
@ -31,18 +29,21 @@ pub fn create(renderer: *c.SDL_Renderer, path: []const u8) Texture {
|
|||
|
||||
// std.debug.print("{s} length {}\n", .{ new_path, new_path.len });
|
||||
|
||||
const io: *c.SDL_IOStream = c.SDL_IOFromFile(new_path, "r") orelse {
|
||||
std.debug.print("SDL Error: {s}\n", .{ c.SDL_GetError() });
|
||||
@panic("Failed to create IO for Texture");
|
||||
};
|
||||
const surface: *c.SDL_Surface = c.IMG_LoadPNG_IO(io) orelse {
|
||||
// const io: *c.SDL_IOStream = c.SDL_IOFromFile(new_path, "r") orelse {
|
||||
// std.debug.print("SDL Error: {s}\n", .{ c.SDL_GetError() });
|
||||
// @panic("Failed to create IO for Texture");
|
||||
// };
|
||||
// c.IMG_Load()
|
||||
const surface: *c.SDL_Surface = c.IMG_Load(new_path) orelse {
|
||||
std.debug.print("SDL Error: {s}\n", .{ c.SDL_GetError() });
|
||||
@panic("Failed to create Surface for Texture");
|
||||
};
|
||||
defer c.SDL_DestroySurface(surface);
|
||||
const texture = c.SDL_CreateTextureFromSurface(renderer, surface) orelse {
|
||||
std.debug.print("SDL Error: {s}\n", .{ c.SDL_GetError() });
|
||||
@panic("Failed to create Texture");
|
||||
};
|
||||
_ = c.SDL_SetTextureScaleMode(texture, c.SDL_SCALEMODE_NEAREST);
|
||||
|
||||
return .{
|
||||
.handle = texture,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
const Vec2f = @import("./Vec2f.zig");
|
||||
const Recti = @import("./Recti.zig");
|
||||
const c = @import("c");
|
||||
|
||||
const Rectf = @This();
|
||||
|
||||
|
|
@ -47,3 +48,15 @@ pub fn to_recti(self: *const Rectf) Recti {
|
|||
@intCast(self.h),
|
||||
);
|
||||
}
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
pub fn to_sdl(self: *const Rectf) c.SDL_FRect {
|
||||
std.debug.print("<{}, {}, {}, {}>\n", .{ self.x, self.y, self.w, self.h });
|
||||
return .{
|
||||
.x = self.x,
|
||||
.y = self.y,
|
||||
.w = self.w,
|
||||
.h = self.h,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,9 +5,11 @@ const Layer = engine.Layer;
|
|||
const Vec2i = engine.geometry.Vec2i;
|
||||
const Vec2f = engine.geometry.Vec2f;
|
||||
const Rectf = engine.geometry.Rectf;
|
||||
const c = @import("c");
|
||||
|
||||
const Recti = @This();
|
||||
|
||||
|
||||
x: i32,
|
||||
y: i32,
|
||||
w: i32,
|
||||
|
|
@ -72,13 +74,26 @@ pub fn draw(self: *const Recti, layer: Layer, color: Color) void {
|
|||
}
|
||||
|
||||
pub fn draw_filled(self: *const Recti, layer: Layer, color: Color) void {
|
||||
_ = self;
|
||||
// _ = self;
|
||||
_ = layer;
|
||||
_ = color;
|
||||
// _ = color;
|
||||
_ = c.SDL_SetRenderDrawColorFloat(engine.Control.sdl_data.renderer, color.f.r, color.f.g, color.f.b, color.f.a);
|
||||
_ = c.SDL_RenderFillRect(engine.Control.sdl_data.renderer, &self.to_sdl());
|
||||
}
|
||||
|
||||
pub fn to_sdl(self: *const Recti) c.SDL_FRect {
|
||||
return .{
|
||||
.x = @floatFromInt(self.x),
|
||||
.y = @floatFromInt(self.y),
|
||||
.w = @floatFromInt(self.w),
|
||||
.h = @floatFromInt(self.h),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn draw_outline(self: *const Recti, layer: Layer, color: Color) void {
|
||||
_ = self;
|
||||
_ = layer;
|
||||
_ = color;
|
||||
// _ = c.SDL_SetRenderDrawColorFloat(engine.Control.sdl_data.renderer, color.f.r, color.f.g, color.f.b, color.f.a);
|
||||
// _ = c.SDL_RenderRect(engine.Control.sdl_data.renderer, &self.to_sdl());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ pub const Interface = @import("Interface.zig");
|
|||
pub const Entity = @import("Entity.zig");
|
||||
pub const Color = @import("Color.zig");
|
||||
pub const Noise = @import("Noise.zig");
|
||||
pub const Input = @import("Input.zig");
|
||||
pub const Sprite = @import("Sprite.zig");
|
||||
pub const Texture = @import("Texture.zig");
|
||||
|
||||
|
|
@ -14,5 +13,5 @@ pub const geometry = @import("geometry/root.zig");
|
|||
|
||||
|
||||
pub const Debug = struct {
|
||||
pub const camera = false;
|
||||
pub const camera = true;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,8 +1 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
const Button = struct {
|
||||
|
||||
}
|
||||
const Button = struct {};
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@ const Camera = @This();
|
|||
|
||||
const std = @import("std");
|
||||
const engine = @import("engine");
|
||||
const c = @import("c");
|
||||
const Entity = engine.Entity;
|
||||
const Scene = engine.Scene;
|
||||
const Input = engine.Input;
|
||||
const Sprite = engine.Sprite;
|
||||
const Color = engine.Color;
|
||||
const Layer = engine.Layer;
|
||||
|
|
@ -25,10 +25,15 @@ drag_pos: ?Vec2i = null,
|
|||
drag_focus: ?Vec2f = null,
|
||||
|
||||
fn draw_line_vec2i(a: Vec2i, b: Vec2i, layer: Layer, color: Color) void {
|
||||
_ = a;
|
||||
_ = b;
|
||||
_ = layer;
|
||||
_ = color;
|
||||
_ = c.SDL_SetRenderDrawColor(engine.Control.sdl_data.renderer, color.u.r, color.u.g, color.u.b, color.u.a);
|
||||
_ = c.SDL_RenderLine(
|
||||
engine.Control.sdl_data.renderer,
|
||||
@floatFromInt(a.x),
|
||||
@floatFromInt(a.y),
|
||||
@floatFromInt(b.x),
|
||||
@floatFromInt(b.y)
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_line_vec2f(a: Vec2f, b: Vec2f, layer: Layer, color: Color) void {
|
||||
|
|
@ -36,6 +41,7 @@ fn draw_line_vec2f(a: Vec2f, b: Vec2f, layer: Layer, color: Color) void {
|
|||
_ = b;
|
||||
_ = layer;
|
||||
_ = color;
|
||||
@panic("Not Implemented");
|
||||
}
|
||||
|
||||
pub fn create() !*Camera {
|
||||
|
|
@ -54,15 +60,16 @@ pub fn destroy(self: *const Camera) void {
|
|||
|
||||
pub fn entity(self: *Camera) Entity {
|
||||
return Entity.init(self, .{
|
||||
.tag = TAG
|
||||
.tag = TAG,
|
||||
.layer = Layer.CAMERA
|
||||
});
|
||||
}
|
||||
|
||||
pub fn update(self: *Camera, _: f32) void {
|
||||
if (self.drag_pos != null and self.drag_focus != null and Input.mouse_pos != null) {
|
||||
if (self.drag_pos != null and self.drag_focus != null and engine.Control.mouse_pos != null) {
|
||||
// new focus = OG focus + difference between drag start and drag now
|
||||
const drag_start_world = self.screen_to_world_vec2i(self.drag_pos.?);
|
||||
const drag_current_world = self.screen_to_world_vec2i(Input.mouse_pos.?);
|
||||
const drag_current_world = self.screen_to_world_vec2i(engine.Control.mouse_pos.?);
|
||||
|
||||
self.focus = Vec2f {
|
||||
.x = self.drag_focus.?.x + (drag_start_world.x - drag_current_world.x),
|
||||
|
|
@ -73,9 +80,9 @@ pub fn update(self: *Camera, _: f32) void {
|
|||
|
||||
pub fn draw(self: *const Camera) void {
|
||||
if (engine.Debug.camera and self.drag_pos != null and self.drag_focus != null) {
|
||||
if(Input.mouse_pos != null) {
|
||||
draw_line_vec2i(self.drag_pos.?, Input.mouse_pos.?, Layer.CAMERA, Color.WHITE);
|
||||
Input.mouse_pos.?.draw(4, Layer.CAMERA, Color.CYAN);
|
||||
if(engine.Control.mouse_pos != null) {
|
||||
draw_line_vec2i(self.drag_pos.?, engine.Control.mouse_pos.?, Layer.CAMERA, Color.WHITE);
|
||||
engine.Control.mouse_pos.?.draw(4, Layer.CAMERA, Color.CYAN);
|
||||
}
|
||||
self.drag_pos.?.draw(4, Layer.CAMERA, Color.RED);
|
||||
|
||||
|
|
@ -149,10 +156,9 @@ pub fn mouse_area(_: *const Camera) Recti {
|
|||
}
|
||||
|
||||
pub fn mouse_down(self: *Camera, button: i32) bool {
|
||||
// middle mouse
|
||||
if (button == 2) {
|
||||
if (Input.mouse_pos == null) return false;
|
||||
self.drag_pos = Input.mouse_pos.?;
|
||||
if (button == c.SDL_BUTTON_MIDDLE) {
|
||||
if (engine.Control.mouse_pos == null) return false;
|
||||
self.drag_pos = engine.Control.mouse_pos.?;
|
||||
self.drag_focus = self.focus;
|
||||
return true;
|
||||
} else return false;
|
||||
|
|
@ -160,7 +166,7 @@ pub fn mouse_down(self: *Camera, button: i32) bool {
|
|||
|
||||
pub fn mouse_up(self: *Camera, button: i32) bool {
|
||||
if (self.drag_pos == null) return false;
|
||||
if (button != 2) return false;
|
||||
if (button != c.SDL_BUTTON_MIDDLE) return false;
|
||||
self.drag_pos = null;
|
||||
self.drag_focus = null;
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ pub fn create() !*Selection {
|
|||
pub fn entity(self: *Selection) Entity {
|
||||
return Entity.init(self, .{
|
||||
.tag = TAG,
|
||||
.layer = Layer.SELECTION
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +38,7 @@ pub fn destroy(self: *const Selection) void {
|
|||
|
||||
pub fn mouse_area(_: *const Selection) Recti {
|
||||
// return Recti.from_xywh(0, 0, std.math.maxInt(i16), std.math.maxInt(i16));
|
||||
return Recti.from_xywh(0, 0, 300, 1000);
|
||||
return Recti.from_xywh(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
pub fn mouse_layer(_: *const Selection) Layer {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ const Color = engine.Color;
|
|||
const Recti = engine.geometry.Recti;
|
||||
const Noise = engine.Noise;
|
||||
const Layer = engine.Layer;
|
||||
const c = @import("c");
|
||||
|
||||
const std = @import("std");
|
||||
const allocator = std.heap.page_allocator;
|
||||
|
|
@ -26,8 +27,6 @@ const tile_size: usize = 16;
|
|||
const GROWS = true;
|
||||
const GROWTH_RATE: f32 = 0.5;
|
||||
|
||||
|
||||
|
||||
chunks: std.ArrayList(*Chunk),
|
||||
generator: WorldGenerator,
|
||||
|
||||
|
|
@ -37,6 +36,7 @@ scene: ?*Scene = null,
|
|||
pub fn entity(self: *Terrain) Entity {
|
||||
return Entity.init(self, .{
|
||||
.tag = TAG,
|
||||
.layer = Layer.HIDDEN
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ fn contrast(n: f32, blend_strength: f32) f32 {
|
|||
}
|
||||
|
||||
fn gen_chunk(self: *Terrain, chunk_pos: Vec2i) !void {
|
||||
std.debug.print("[Terrain:gen_chunk] {}\n", .{ chunk_pos });
|
||||
std.debug.print("[Terrain:gen_chunk] ({}, {})\n", .{ chunk_pos.x, chunk_pos.y });
|
||||
const chunk = try Chunk.generate(chunk_pos, &self.generator);
|
||||
try self.chunks.append(chunk);
|
||||
if (self.scene != null) try self.scene.?.add(chunk);
|
||||
|
|
@ -189,7 +189,9 @@ const Chunk = struct {
|
|||
}
|
||||
|
||||
pub fn entity(self: *Chunk) Entity {
|
||||
return Entity.init(self, .{});
|
||||
return Entity.init(self, .{
|
||||
.layer = Layer.FLOOR
|
||||
});
|
||||
}
|
||||
|
||||
pub fn start(self: *Chunk, scene: *Scene) void {
|
||||
|
|
@ -206,6 +208,8 @@ const Chunk = struct {
|
|||
}
|
||||
}
|
||||
|
||||
var rect_test: c.SDL_FRect = .{};
|
||||
|
||||
pub fn draw(self: *const Chunk) void {
|
||||
for(0..CHUNK_LENGTH) |idx| {
|
||||
const tile = self.tiles[idx];
|
||||
|
|
@ -213,6 +217,7 @@ const Chunk = struct {
|
|||
const rect = Recti.from_xywh(x, y, 1, 1);
|
||||
const sprite = assets.terrain[0][tile.texture_idx];
|
||||
self.camera.draw_sprite_i(sprite, rect, Layer.FLOOR, tile.grass_color);
|
||||
|
||||
// switch(tile.texture_idx) {
|
||||
// 0 => self.camera.draw_sprite_i(sprite, rect, Layer.FLOOR, Color.WHITE),
|
||||
// 1 => self.camera.draw_sprite_i(sprite, rect, Layer.FLOOR, Color.RED),
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ inline fn make8 (x: i32, y: i32, w: i32, h: i32) Sprite {
|
|||
|
||||
pub fn load(window: *c.SDL_Window, renderer: *c.SDL_Renderer) void {
|
||||
_ = window;
|
||||
|
||||
texture = Texture.create(renderer, "textures.png");
|
||||
|
||||
assets = .{
|
||||
|
|
|
|||
Loading…
Reference in New Issue