Add docmap struct

We want to have an array of docmaps, one for each file and be able to
iterate through them. That way the layout can be done in a single
function.
This commit is contained in:
Nathan McRae 2023-06-19 22:42:26 -07:00
parent d3e47ee184
commit aade06e600

View File

@ -1,204 +1,270 @@
const std = @import("std"); const std = @import("std");
const c = @cImport({ const c = @cImport({
@cInclude("SDL.h"); @cInclude("SDL.h");
@cInclude("SDL_ttf.h"); @cInclude("SDL_ttf.h");
}); });
const assert = @import("std").debug.assert; const assert = @import("std").debug.assert;
pub fn main() !void { const DrawnText = struct {
const window_width = 300; surface: [*c]c.SDL_Surface,
const window_height = 500; texture: ?*c.SDL_Texture,
const column_width = 100; rect: c.SDL_Rect,
};
if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError()); const FileDocMap = struct {
return error.SDLInitializationFailed; title_text: DrawnText,
} contents: []u8,
defer c.SDL_Quit(); };
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 pub fn main() !void {
{ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
c.SDL_Log("Unable to create window: %s", c.SDL_GetError()); defer {
return error.SDLInitializationFailed; if(gpa.deinit()) {
}; std.debug.print("Allocator leak\n", .{});
defer c.SDL_DestroyWindow(screen); }
}
const renderer = c.SDL_CreateRenderer(screen, -1, 0) orelse {
c.SDL_Log("Unable to create renderer: %s", c.SDL_GetError()); const allocator = gpa.allocator();
return error.SDLInitializationFailed;
}; const window_width = 300;
defer c.SDL_DestroyRenderer(renderer); const window_height = 500;
const column_width = 100;
const source_code = @embedFile("GapProbeScan.cs");
const file_name = "GapProbeScan.cs"; if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError());
const color_result: c_int = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); return error.SDLInitializationFailed;
if (color_result != 0) { }
c.SDL_Log("Unable to set draw color: %d", color_result); defer c.SDL_Quit();
return error.SDLInitializationFailed;
} 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
{
// https://github.com/cirosantilli/cpp-cheat/blob/e52ed4b838e2697d8f44ab5bef3e7a170705d48e/sdl/ttf.c c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
const ttf_init_result: c_int = c.TTF_Init(); return error.SDLInitializationFailed;
if (ttf_init_result != 0) { };
c.SDL_Log("Unable to init TTF: %d", ttf_init_result); defer c.SDL_DestroyWindow(screen);
return error.TTFInitializationFailed;
} const renderer = c.SDL_CreateRenderer(screen, -1, 0) orelse {
defer c.TTF_Quit(); c.SDL_Log("Unable to create renderer: %s", c.SDL_GetError());
return error.SDLInitializationFailed;
const text_color: c.SDL_Color = c.SDL_Color { };
.r = 0, defer c.SDL_DestroyRenderer(renderer);
.g = 0,
.b = 0, // Turn on anti-aliasing
.a = 255 // :( doesn't seem to work
}; const aa_result = c.SDL_SetHint(c.SDL_HINT_RENDER_SCALE_QUALITY, "1");
if (aa_result != c.SDL_TRUE) {
// TODO: can we embed this file? c.SDL_Log("Unable to set anti-aliasing: %d", c.SDL_GetError());
const font: ?*c.TTF_Font = c.TTF_OpenFont("FreeSans.ttf", 24); return error.SDLInitializationFailed;
if (font == null) { }
const err = c.TTF_GetError();
c.SDL_Log("Unable to load font: %s", err); // https://github.com/cirosantilli/cpp-cheat/blob/e52ed4b838e2697d8f44ab5bef3e7a170705d48e/sdl/ttf.c
return error.TTFFontLoadFailed; const ttf_init_result: c_int = c.TTF_Init();
} if (ttf_init_result != 0) {
c.SDL_Log("Unable to init TTF: %d", ttf_init_result);
const surface: [*c]c.SDL_Surface = c.TTF_RenderText_Solid(font, file_name, text_color); return error.TTFInitializationFailed;
if (surface == null) { }
c.SDL_Log("Unable to render text"); defer c.TTF_Quit();
return error.RenderTextFailed;
} const text_color: c.SDL_Color = c.SDL_Color {
defer c.SDL_FreeSurface(surface); .r = 0,
.g = 0,
const texture: ?*c.SDL_Texture = c.SDL_CreateTextureFromSurface(renderer, surface); .b = 0,
if (texture == null) { .a = 255
c.SDL_Log("Unable to create texture from surface"); };
return error.CreateTextureFailed;
} // TODO: can we embed this file?
defer c.SDL_DestroyTexture(texture); const font: ?*c.TTF_Font = c.TTF_OpenFont("FreeSans.ttf", 12);
if (font == null) {
const rect = c.SDL_Rect { const err = c.TTF_GetError();
.x = 0, c.SDL_Log("Unable to load font: %s", err);
.y = 0, return error.TTFFontLoadFailed;
.w = surface.*.w, }
.h = surface.*.h,
}; var file_paths = std.ArrayList([]const u8).init(allocator);
defer file_paths.deinit();
// const surface = c.SDL_GetWindowSurface(screen); try file_paths.append("GapProbeScan.cs");
try file_paths.append("src/main.zig");
var quit = false; // const file_name = "GapProbeScan.cs";
while (!quit) { // const source_code = @embedFile(file_name);
var event: c.SDL_Event = undefined;
while (c.SDL_PollEvent(&event) != 0) { var file_docmaps = std.ArrayList(FileDocMap).init(allocator);
switch (event.@"type") { defer file_docmaps.deinit();
c.SDL_QUIT => {
quit = true; for (file_paths.items) |path| {
}, const surface: [*c]c.SDL_Surface = c.TTF_RenderText_Solid(font, @ptrCast([*c]const u8, path), text_color);
else => {}, if (surface == null) {
} c.SDL_Log("Unable to render text");
} return error.RenderTextFailed;
}
const initial_color_result = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
if (initial_color_result != 0) { const texture: ?*c.SDL_Texture = c.SDL_CreateTextureFromSurface(renderer, surface);
c.SDL_Log("Unable to set draw color: %d", initial_color_result); if (texture == null) {
return error.SDLInitializationFailed; c.SDL_Log("Unable to create texture from surface");
} return error.CreateTextureFailed;
}
var x: c_int = 0;
while (x < window_width) : (x += 1) { var rect = c.SDL_Rect {
var y: c_int = 0; .x = 0,
while (y < window_height) : (y += 1) { .y = 0,
const draw_result = c.SDL_RenderDrawPoint(renderer, x, y); .w = surface.*.w,
if (draw_result != 0) { .h = surface.*.h,
c.SDL_Log("Unable to draw point: %d", draw_result); };
return error.SDLDrawingFailed;
} const file = try std.fs.cwd().openFile(path, .{ });
} defer file.close();
}
const stat = try file.stat();
var i: usize = 0; var buffer = try allocator.alloc(u8, stat.size);
var column: c_int = 0;
x = 0; const bytes_read = try file.readAll(buffer);
var y: c_int = surface.*.h; if (bytes_read != stat.size) {
// white: false, black: true std.debug.print("bytes_read: {any}, stat.size: {any}\n", .{
var color: bool = false; bytes_read,
while (i < source_code.len) : (i += 1) { stat.size,
if (source_code[i] == 10) { });
y += 1; return error.FileReadError;
if (y > window_height) { }
y = 0;
column += 1; var docmap = FileDocMap {
// std.debug.print("Column: {any}, x: {any}\n", .{column, column*column_width}); .title_text = DrawnText {
} .surface = surface,
x = column * column_width; .texture = texture,
.rect = rect,
continue; },
} .contents = buffer,
};
if (source_code[i] == 32 and color) {
color = false; try file_docmaps.append(docmap);
const white_color_result = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); }
if (white_color_result != 0) { defer {
c.SDL_Log("Unable to set draw color: %d", white_color_result); for (file_docmaps.items) |docmap| {
return error.SDLInitializationFailed; c.SDL_FreeSurface(docmap.title_text.surface);
} c.SDL_DestroyTexture(docmap.title_text.texture);
} else if (source_code[i] != 32 and !color) { allocator.free(docmap.contents);
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); const color_result: c_int = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
return error.SDLInitializationFailed; if (color_result != 0) {
} c.SDL_Log("Unable to set draw color: %d", color_result);
} return error.SDLInitializationFailed;
}
if (x < (column + 1) * column_width) {
const draw_result = c.SDL_RenderDrawPoint(renderer, x, y); // const surface = c.SDL_GetWindowSurface(screen);
if (draw_result != 0) {
c.SDL_Log("Unable to draw point: %d", draw_result); var quit = false;
return error.SDLDrawingFailed; while (!quit) {
} var event: c.SDL_Event = undefined;
} else { while (c.SDL_PollEvent(&event) != 0) {
const red_color_result = c.SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); switch (event.@"type") {
if (red_color_result != 0) { c.SDL_QUIT => {
c.SDL_Log("Unable to set draw color: %d", red_color_result); quit = true;
return error.SDLInitializationFailed; },
} else => {},
}
const draw_result = c.SDL_RenderDrawPoint(renderer, (column + 1) * column_width - 1, y); }
if (draw_result != 0) {
c.SDL_Log("Unable to draw point: %d", draw_result); const initial_color_result = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
return error.SDLDrawingFailed; if (initial_color_result != 0) {
} c.SDL_Log("Unable to set draw color: %d", initial_color_result);
return error.SDLInitializationFailed;
if (!color) { }
const white_color_result = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
if (white_color_result != 0) { var x: c_int = 0;
c.SDL_Log("Unable to set draw color: %d", white_color_result); while (x < window_width) : (x += 1) {
return error.SDLInitializationFailed; var y: c_int = 0;
} while (y < window_height) : (y += 1) {
} else { const draw_result = c.SDL_RenderDrawPoint(renderer, x, y);
const black_color_result = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); if (draw_result != 0) {
if (black_color_result != 0) { c.SDL_Log("Unable to draw point: %d", draw_result);
c.SDL_Log("Unable to set draw color: %d", black_color_result); return error.SDLDrawingFailed;
return error.SDLInitializationFailed; }
} }
} }
}
var i: usize = 0;
x += 1; var column: c_int = 0;
} x = 0;
var y: c_int = file_docmaps.items[0].title_text.rect.h;
// Display text // white: false, black: true
if (c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) != 0) { var color: bool = false;
c.SDL_Log("Unable to set draw color"); while (i < file_docmaps.items[0].contents.len) : (i += 1) {
} if (file_docmaps.items[0].contents[i] == 10) {
y += 1;
if (c.SDL_RenderCopy(renderer, texture, null, &rect) != 0) { if (y > window_height) {
c.SDL_Log("Unable to render copy"); y = 0;
} column += 1;
// std.debug.print("Column: {any}, x: {any}\n", .{column, column*column_width});
c.SDL_RenderPresent(renderer); }
x = column * column_width;
c.SDL_Delay(17);
} continue;
} }
if (file_docmaps.items[0].contents[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 (file_docmaps.items[0].contents[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;
}
}
if (x < (column + 1) * column_width) {
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;
}
} else {
const red_color_result = c.SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
if (red_color_result != 0) {
c.SDL_Log("Unable to set draw color: %d", red_color_result);
return error.SDLInitializationFailed;
}
const draw_result = c.SDL_RenderDrawPoint(renderer, (column + 1) * column_width - 1, y);
if (draw_result != 0) {
c.SDL_Log("Unable to draw point: %d", draw_result);
return error.SDLDrawingFailed;
}
if (!color) {
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 {
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;
}
}
}
x += 1;
}
const text = file_docmaps.items[0].title_text;
// Display text
if (c.SDL_RenderCopy(renderer, text.texture, null, &(text.rect)) != 0) {
c.SDL_Log("Unable to render copy");
}
c.SDL_RenderPresent(renderer);
c.SDL_Delay(17);
}
}