瀏覽代碼

Prepare for possible commands

Luna Stadler 4 年之前
父節點
當前提交
4b7a5bd273
共有 1 個文件被更改,包括 36 次插入2 次删除
  1. 36 2
      zig/sdl/hello_sdl.zig

+ 36 - 2
zig/sdl/hello_sdl.zig

@ -52,16 +52,21 @@ pub fn main() !void {
52 52
53 53
    // assume monospace font
54 54
    var glyph_width: c_int = 0;
55
    if (c.TTF_GlyphMetrics(font, 'x', null, null, null, null, &glyph_width) != 0) {
55
    if (c.TTF_GlyphMetrics(font, 'g', null, null, null, null, &glyph_width) != 0) {
56 56
        c.SDL_Log("Unable to measure glyph: %s", c.TTF_GetError());
57 57
        return error.TTFInitializationFailed;
58 58
    }
59
    std.debug.assert(glyph_width < 1000);
59
    var glyph_height = c.TTF_FontLineSkip(font);
60 60
61 61
    var msg = "howdy there, enby! 🐘                                          ".*;
62 62
    var pos: usize = 0;
63 63
    var max_chars = std.math.min(@divTrunc(@intCast(usize, surface.*.w), @intCast(usize, glyph_width)), msg.len);
64 64
65
    var result: ?[*:0]u8 = null;
66
    result = "";
67
68
    const keyboardState = c.SDL_GetKeyboardState(null);
69
65 70
    var quit = false;
66 71
    while (!quit) {
67 72
        var event: c.SDL_Event = undefined;
@ -87,8 +92,29 @@ pub fn main() !void {
87 92
                            pos = if (pos == 0) max_chars - 1 else (pos - 1) % (max_chars - 1);
88 93
                            msg[pos] = '_';
89 94
                        },
95
                        c.SDLK_RETURN => {
96
                            result = runCommand(&msg);
97
                        },
90 98
                        else => {},
91 99
                    }
100
101
                    // ctrl + key combinations
102
                    if (keyboardState[c.SDL_SCANCODE_LCTRL] != 0) {
103
                        switch (event.key.keysym.sym) {
104
                            c.SDLK_a => {
105
                                pos = 0;
106
                                msg[pos] = '_';
107
                            },
108
                            c.SDLK_k => {
109
                                var i: usize = 0;
110
                                while (i < max_chars) : (i += 1) {
111
                                    msg[i] = ' ';
112
                                }
113
                                msg[max_chars] = 0;
114
                            },
115
                            else => {},
116
                        }
117
                    }
92 118
                },
93 119
                c.SDL_TEXTINPUT => {
94 120
                    if (event.text.text.len > 0) {
@ -109,8 +135,16 @@ pub fn main() !void {
109 135
        const text = c.TTF_RenderUTF8_Shaded(font, &msg, white, black);
110 136
        _ = c.SDL_BlitSurface(text, null, surface, null);
111 137
138
        const result_text = c.TTF_RenderUTF8_Shaded(font, result, white, black);
139
        _ = c.SDL_BlitSurface(result_text, null, surface, &c.SDL_Rect{ .x = 0, .y = glyph_height, .w = surface.*.w, .h = surface.*.h - glyph_height });
140
112 141
        _ = c.SDL_UpdateWindowSurface(screen);
113 142
114 143
        c.SDL_Delay(16);
115 144
    }
116 145
}
146
147
fn runCommand(cmd: ?[*:0]u8) ?[*:0]u8 {
148
    c.SDL_Log("cmd is '%s'", cmd);
149
    return "unknown command";
150
}