mirror of https://git.sr.ht/~kikoodx/kble
Level uses memory allocation + LICENSE file
This commit is contained in:
parent
9795160001
commit
98eb7ad4e7
|
@ -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.
|
|
@ -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);
|
||||
}
|
||||
|
|
27
src/main.zig
27
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue