Ver Código Fonte

Support writing again

Luna Stadler 4 anos atrás
pai
commit
e3f214f587
1 arquivos alterados com 20 adições e 4 exclusões
  1. 20 4
      zig/sdl/hello_sdl.zig

+ 20 - 4
zig/sdl/hello_sdl.zig

42
    defer c.TTF_CloseFont(font);
42
    defer c.TTF_CloseFont(font);
43
    c.SDL_Log("Using font %s", font_file.ptr);
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
    const screen = c.SDL_CreateWindow("hello fonts", c.SDL_WINDOWPOS_CENTERED, c.SDL_WINDOWPOS_CENTERED, 600, 100, c.SDL_WINDOW_BORDERLESS) orelse {
45
    const screen = c.SDL_CreateWindow("hello fonts", c.SDL_WINDOWPOS_CENTERED, c.SDL_WINDOWPOS_CENTERED, 600, 100, c.SDL_WINDOW_BORDERLESS) orelse {
49
        c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
46
        c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
50
        return error.SDLInitializationFailed;
47
        return error.SDLInitializationFailed;
53
50
54
    const surface = c.SDL_GetWindowSurface(screen);
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
    var quit = false;
65
    var quit = false;
57
    while (!quit) {
66
    while (!quit) {
58
        var event: c.SDL_Event = undefined;
67
        var event: c.SDL_Event = undefined;
69
                        else => {},
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
                else => {},
88
                else => {},
73
            }
89
            }
74
        }
90
        }
78
        const black: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
94
        const black: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
79
        // Shaded vs Solid gives a nicer output, with solid the output
95
        // Shaded vs Solid gives a nicer output, with solid the output
80
        // was squiggly and not aligned with a baseline.
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
        _ = c.SDL_BlitSurface(text, null, surface, null);
98
        _ = c.SDL_BlitSurface(text, null, surface, null);
83
99
84
        _ = c.SDL_UpdateWindowSurface(screen);
100
        _ = c.SDL_UpdateWindowSurface(screen);