Просмотр исходного кода

Support loading fonts again

This was... not fun, lots of `*const [*:0]u8` vs `[31:0]u8` and such
sillyness.  What worked in the end was a code example from [1] and a fix
from [2].

[1]: https://github.com/ziglang/zig/blob/c82c3585c8fdf02820747c118c78957e2eb5d072/lib/std/special/test_runner.zig#L17
[2]: https://github.com/ziglang/zig/issues/1481#issuecomment-806459027
Luna Stadler лет назад: 4
Родитель
Сommit
9af081f85e
1 измененных файлов с 8 добавлено и 3 удалено
  1. 8 3
      c/sdl/hello_sdl.zig

+ 8 - 3
c/sdl/hello_sdl.zig

@ -9,10 +9,14 @@ const c = @cImport({
9 9
    @cInclude("SDL2/SDL.h");
10 10
    @cInclude("SDL2/SDL_ttf.h");
11 11
});
12
const assert = @import("std").debug.assert;
13
const process = @import("std").process;
12
const std = @import("std");
14 13
15 14
pub fn main() !void {
15
    var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
16
    const gpa = &general_purpose_allocator.allocator;
17
    const args = try std.process.argsAlloc(gpa);
18
    defer std.process.argsFree(gpa, args);
19
16 20
    if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
17 21
        c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError());
18 22
        return error.SDLInitializationFailed;
@ -25,12 +29,13 @@ pub fn main() !void {
25 29
    }
26 30
    defer c.TTF_Quit();
27 31
28
    var font_file = "./FantasqueSansMono-Regular.ttf";
32
    var font_file = if (args.len > 1) args[1] else "./FantasqueSansMono-Regular.ttf";
29 33
    const font = c.TTF_OpenFont(font_file, 20) orelse {
30 34
        c.SDL_Log("Unable to load font: %s", c.TTF_GetError());
31 35
        return error.TTFInitializationFailed;
32 36
    };
33 37
    defer c.TTF_CloseFont(font);
38
    c.SDL_Log("Using font %s", font_file.ptr);
34 39
35 40
    const msg = "howdy there, enby! 🐘                                          ";
36 41
    c.SDL_Log(msg);