2022-09-22 23:03:25 +02:00
|
|
|
const ext = @cImport({
|
2022-08-10 15:52:16 +02:00
|
|
|
@cInclude("SDL2/SDL.h");
|
|
|
|
});
|
|
|
|
|
2022-09-09 23:55:34 +02:00
|
|
|
const io = @import("./io.zig");
|
|
|
|
const stack = @import("./stack.zig");
|
2022-08-10 15:52:16 +02:00
|
|
|
const std = @import("std");
|
2022-09-25 00:09:02 +02:00
|
|
|
const sys = @import("./sys.zig");
|
2022-09-09 23:55:34 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Entry point.
|
|
|
|
///
|
2022-08-10 15:52:16 +02:00
|
|
|
pub fn main() anyerror!void {
|
2022-09-22 23:03:25 +02:00
|
|
|
if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) {
|
|
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL2 runtime");
|
2022-08-10 15:52:16 +02:00
|
|
|
|
2022-09-09 23:55:34 +02:00
|
|
|
return error.InitFailure;
|
2022-08-10 15:52:16 +02:00
|
|
|
}
|
|
|
|
|
2022-09-22 23:03:25 +02:00
|
|
|
defer ext.SDL_Quit();
|
2022-08-10 15:52:16 +02:00
|
|
|
|
2022-09-09 23:55:34 +02:00
|
|
|
const pref_path = create_pref_path: {
|
2022-09-22 23:03:25 +02:00
|
|
|
const path = ext.SDL_GetPrefPath("ona", "ona") orelse {
|
|
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load user path");
|
2022-08-10 17:38:50 +02:00
|
|
|
|
2022-09-09 23:55:34 +02:00
|
|
|
return error.InitFailure;
|
|
|
|
};
|
2022-08-10 17:38:50 +02:00
|
|
|
|
2022-09-09 23:55:34 +02:00
|
|
|
break: create_pref_path path[0 .. std.mem.len(path)];
|
|
|
|
};
|
2022-08-10 17:38:50 +02:00
|
|
|
|
2022-09-22 23:03:25 +02:00
|
|
|
defer ext.SDL_free(pref_path.ptr);
|
2022-08-10 17:38:50 +02:00
|
|
|
|
2022-09-09 23:55:34 +02:00
|
|
|
const window = create_window: {
|
2022-09-22 23:03:25 +02:00
|
|
|
const pos = ext.SDL_WINDOWPOS_UNDEFINED;
|
2022-08-10 15:52:16 +02:00
|
|
|
var flags = @as(u32, 0);
|
|
|
|
|
2022-09-22 23:03:25 +02:00
|
|
|
break: create_window ext.SDL_CreateWindow("Ona", pos, pos, 640, 480, flags) orelse {
|
|
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 window");
|
2022-08-10 15:52:16 +02:00
|
|
|
|
2022-09-09 23:55:34 +02:00
|
|
|
return error.InitFailure;
|
|
|
|
};
|
|
|
|
};
|
2022-08-10 15:52:16 +02:00
|
|
|
|
2022-09-22 23:03:25 +02:00
|
|
|
defer ext.SDL_DestroyWindow(window);
|
2022-08-10 15:52:16 +02:00
|
|
|
|
2022-09-09 23:55:34 +02:00
|
|
|
const renderer = create_renderer: {
|
2022-08-10 17:38:50 +02:00
|
|
|
var flags = @as(u32, 0);
|
|
|
|
|
2022-09-22 23:03:25 +02:00
|
|
|
break: create_renderer ext.SDL_CreateRenderer(window, -1, flags) orelse {
|
|
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 renderer");
|
2022-09-09 23:55:34 +02:00
|
|
|
|
|
|
|
return error.InitFailure;
|
|
|
|
};
|
2022-08-10 17:38:50 +02:00
|
|
|
};
|
|
|
|
|
2022-09-22 23:03:25 +02:00
|
|
|
defer ext.SDL_DestroyRenderer(renderer);
|
2022-08-10 17:38:50 +02:00
|
|
|
|
2022-09-25 00:09:02 +02:00
|
|
|
var event_loop = sys.EventLoop{};
|
2022-08-10 15:52:16 +02:00
|
|
|
var is_running = true;
|
|
|
|
|
|
|
|
while (is_running) {
|
2022-09-22 23:03:25 +02:00
|
|
|
var event = std.mem.zeroes(ext.SDL_Event);
|
2022-08-10 15:52:16 +02:00
|
|
|
|
2022-09-22 23:03:25 +02:00
|
|
|
while (ext.SDL_PollEvent(&event) != 0) {
|
2022-09-09 23:55:34 +02:00
|
|
|
switch (event.type) {
|
2022-09-22 23:03:25 +02:00
|
|
|
ext.SDL_QUIT => is_running = false,
|
2022-08-10 15:52:16 +02:00
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 17:38:50 +02:00
|
|
|
|
2022-09-22 23:03:25 +02:00
|
|
|
if (ext.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) != 0) {
|
|
|
|
ext.SDL_LogError(ext.SDL_LOG_CATEGORY_VIDEO, ext.SDL_GetError());
|
|
|
|
ext.SDL_ClearError();
|
2022-09-09 23:55:34 +02:00
|
|
|
}
|
|
|
|
|
2022-09-22 23:03:25 +02:00
|
|
|
if (ext.SDL_RenderClear(renderer) != 0) {
|
|
|
|
ext.SDL_LogError(ext.SDL_LOG_CATEGORY_VIDEO, ext.SDL_GetError());
|
|
|
|
ext.SDL_ClearError();
|
2022-09-09 23:55:34 +02:00
|
|
|
}
|
|
|
|
|
2022-09-22 23:03:25 +02:00
|
|
|
ext.SDL_RenderPresent(renderer);
|
2022-09-25 00:09:02 +02:00
|
|
|
event_loop.tick();
|
2022-09-22 23:03:25 +02:00
|
|
|
ext.SDL_Delay(1);
|
2022-08-10 15:52:16 +02:00
|
|
|
}
|
|
|
|
}
|