From 98eb7ad4e70d7b01851abb75567e1065e31a100a Mon Sep 17 00:00:00 2001 From: KikooDX Date: Tue, 26 Jan 2021 13:10:16 +0100 Subject: [PATCH] Level uses memory allocation + LICENSE file --- LICENSE | 21 +++++++++++++++++++++ src/level.zig | 35 ++++++++++++++++++++++++++++------- src/main.zig | 27 ++++++++++++++++++++------- 3 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a90c6d1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2021 KikooDX + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/level.zig b/src/level.zig index e4ec82d..710e73b 100644 --- a/src/level.zig +++ b/src/level.zig @@ -1,23 +1,44 @@ const std = @import("std"); const expect = std.testing.expect; -const Level = struct { +pub const Level = struct { width: u16, height: u16, - buffer: [65536]u16, + buffer: []u16, - pub fn init(width: u16, height: u16) Level { - return Level { + pub fn init(allocator: *std.mem.Allocator, width: u16, height: u16) !Level { + var level = Level{ .width = width, .height = height, .buffer = undefined, }; + level.buffer = try allocator.alloc(u16, width * height); + return level; + } + + pub fn deinit(self: *Level, allocator: *std.mem.Allocator) void { + allocator.free(self.buffer); } }; test "create level buffer" { - // Create level. - const level: Level = Level.init(64, 32); + // Create allocator. + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer { + const leaked = gpa.deinit(); + if (leaked) expect(false); //fail test + } + const allocator = &gpa.allocator; + + // Initialize level struct (twice 'cause why not?). + var level: Level = try Level.init(allocator, 64, 32); + level.deinit(allocator); + level = try Level.init(allocator, 64, 64); + defer level.deinit(allocator); + + level.buffer[128] = 32; + + expect(level.buffer[128] == 32); expect(level.width == 64); - expect(level.height == 32); + expect(level.height == 64); } diff --git a/src/main.zig b/src/main.zig index f2a3022..0f7ca1e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,23 +1,36 @@ +const std = @import("std"); +const assert = std.debug.assert; +const Level = @import("level.zig").Level; const ray = @cImport({ @cInclude("raylib.h"); }); -pub fn main() void { - const screenWidth = 640; - const screenHeight = 480; +pub fn main() !void { + // Create allocator + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer { + const leaked = gpa.deinit(); + if (leaked) assert(false); //raise error + } + const allocator = &gpa.allocator; + // Create window ray.SetConfigFlags(ray.FLAG_WINDOW_RESIZABLE); - ray.InitWindow(screenWidth, screenHeight, "KBLE"); + ray.InitWindow(640, 480, "KBLE"); defer ray.CloseWindow(); + // Limit FPS for performance ray.SetTargetFPS(60); - while(!ray.WindowShouldClose()) { + // Create level + var level: Level = try Level.init(allocator, 128, 128); + defer level.deinit(allocator); + + while (!ray.WindowShouldClose()) { ray.BeginDrawing(); defer ray.EndDrawing(); ray.ClearBackground(ray.RAYWHITE); - ray.DrawText("Hello, World!", 190, 200, 20, ray.LIGHTGRAY); + ray.DrawText("Hellowo!", 190, 200, 20, ray.LIGHTGRAY); } } -