Explorar el Código

Support writing again

Luna Stadler %!s(int64=4) %!d(string=hace) años
padre
commit
e3f214f587
Se han modificado 1 ficheros con 20 adiciones y 4 borrados
  1. 20 4
      zig/sdl/hello_sdl.zig

+ 20 - 4
zig/sdl/hello_sdl.zig

@ -42,9 +42,6 @@ pub fn main() !void {
42 42
    defer c.TTF_CloseFont(font);
43 43
    c.SDL_Log("Using font %s", font_file.ptr);
44 44
45
    const msg = "howdy there, enby! 🐘                                          ";
46
    c.SDL_Log(msg);
47
48 45
    const screen = c.SDL_CreateWindow("hello fonts", c.SDL_WINDOWPOS_CENTERED, c.SDL_WINDOWPOS_CENTERED, 600, 100, c.SDL_WINDOW_BORDERLESS) orelse {
49 46
        c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
50 47
        return error.SDLInitializationFailed;
@ -53,6 +50,18 @@ pub fn main() !void {
53 50
54 51
    const surface = c.SDL_GetWindowSurface(screen);
55 52
53
    // assume monospace font
54
    var glyph_width: c_int = 0;
55
    if (c.TTF_GlyphMetrics(font, 'x', null, null, null, null, &glyph_width) != 0) {
56
        c.SDL_Log("Unable to measure glyph: %s", c.TTF_GetError());
57
        return error.TTFInitializationFailed;
58
    }
59
    std.debug.assert(glyph_width < 1000);
60
61
    var msg = "howdy there, enby! 🐘                                          ".*;
62
    var pos: usize = 0;
63
    var max_chars = std.math.min(@divTrunc(@intCast(usize, surface.*.w), @intCast(usize, glyph_width)), msg.len);
64
56 65
    var quit = false;
57 66
    while (!quit) {
58 67
        var event: c.SDL_Event = undefined;
@ -69,6 +78,13 @@ pub fn main() !void {
69 78
                        else => {},
70 79
                    }
71 80
                },
81
                c.SDL_TEXTINPUT => {
82
                    if (event.text.text.len > 0) {
83
                        c.SDL_Log("input: '%s' at %d", event.text.text, pos);
84
                        msg[pos] = event.text.text[0];
85
                        pos = (pos + 1) % (max_chars - 1);
86
                    }
87
                },
72 88
                else => {},
73 89
            }
74 90
        }
@ -78,7 +94,7 @@ pub fn main() !void {
78 94
        const black: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
79 95
        // Shaded vs Solid gives a nicer output, with solid the output
80 96
        // was squiggly and not aligned with a baseline.
81
        const text = c.TTF_RenderUTF8_Shaded(font, msg, white, black);
97
        const text = c.TTF_RenderUTF8_Shaded(font, &msg, white, black);
82 98
        _ = c.SDL_BlitSurface(text, null, surface, null);
83 99
84 100
        _ = c.SDL_UpdateWindowSurface(screen);