First pass at a document map viewer

This commit is contained in:
Nathan McRae 2023-06-04 14:46:48 -07:00
parent 0e9e56a8f1
commit b695ee0bf7

View File

@ -1,16 +1,21 @@
const std = @import("std");
const c = @cImport({
@cInclude("SDL2/SDL.h");
});
const assert = @import("std").debug.assert;
pub fn main() !void {
const window_width = 300;
const window_height = 500;
if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError());
return error.SDLInitializationFailed;
}
defer c.SDL_Quit();
const screen = c.SDL_CreateWindow("My Game Window", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, 300, 73, c.SDL_WINDOW_OPENGL) orelse
const screen = c.SDL_CreateWindow("Document Map Viewer", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, window_width, window_height, c.SDL_WINDOW_OPENGL) orelse
{
c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
return error.SDLInitializationFailed;
@ -23,24 +28,15 @@ pub fn main() !void {
};
defer c.SDL_DestroyRenderer(renderer);
const zig_bmp = @embedFile("zig.bmp");
const rw = c.SDL_RWFromConstMem(zig_bmp, zig_bmp.len) orelse {
c.SDL_Log("Unable to get RWFromConstMem: %s", c.SDL_GetError());
return error.SDLInitializationFailed;
};
defer assert(c.SDL_RWclose(rw) == 0);
const source_code = @embedFile("GapProbeScan.cs");
const zig_surface = c.SDL_LoadBMP_RW(rw, 0) orelse {
c.SDL_Log("Unable to load bmp: %s", c.SDL_GetError());
const color_result = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
if (color_result != 0) {
c.SDL_Log("Unable to set draw color: %d", color_result);
return error.SDLInitializationFailed;
};
defer c.SDL_FreeSurface(zig_surface);
}
const zig_texture = c.SDL_CreateTextureFromSurface(renderer, zig_surface) orelse {
c.SDL_Log("Unable to create texture from surface: %s", c.SDL_GetError());
return error.SDLInitializationFailed;
};
defer c.SDL_DestroyTexture(zig_texture);
// const surface = c.SDL_GetWindowSurface(screen);
var quit = false;
while (!quit) {
@ -54,8 +50,61 @@ pub fn main() !void {
}
}
_ = c.SDL_RenderClear(renderer);
_ = c.SDL_RenderCopy(renderer, zig_texture, null, null);
const initial_color_result = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
if (initial_color_result != 0) {
c.SDL_Log("Unable to set draw color: %d", initial_color_result);
return error.SDLInitializationFailed;
}
var x:c_int = 0;
while (x < window_width) : (x += 1) {
var y:c_int = 0;
while (y < window_height) : (y += 1) {
// std.debug.print("x: {any}, y: {any}\n", .{x, y});
const draw_result = c.SDL_RenderDrawPoint(renderer, x, y);
if (draw_result != 0) {
c.SDL_Log("Unable to draw point: %d", draw_result);
return error.SDLDrawingFailed;
}
}
}
var i:usize = 0;
x = 0;
var y:c_int = 0;
var color:bool = false;
while (i < source_code.len) : (i += 1) {
if (source_code[i] == 10) {
x = 0;
y += 1;
continue;
}
if (source_code[i] == 32 and color) {
color = false;
const white_color_result = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
if (white_color_result != 0) {
c.SDL_Log("Unable to set draw color: %d", white_color_result);
return error.SDLInitializationFailed;
}
}
else if (source_code[i] != 32 and !color) {
color = true;
const black_color_result = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
if (black_color_result != 0) {
c.SDL_Log("Unable to set draw color: %d", black_color_result);
return error.SDLInitializationFailed;
}
}
const draw_result = c.SDL_RenderDrawPoint(renderer, x, y);
if (draw_result != 0) {
c.SDL_Log("Unable to draw point: %d", draw_result);
return error.SDLDrawingFailed;
}
x += 1;
}
c.SDL_RenderPresent(renderer);
c.SDL_Delay(17);