Luna Stadler лет назад: 4
Родитель
Сommit
4b7a5bd273
1 измененных файлов с 36 добавлено и 2 удалено
  1. 36 2
      zig/sdl/hello_sdl.zig

+ 36 - 2
zig/sdl/hello_sdl.zig

52
52
53
    // assume monospace font
53
    // assume monospace font
54
    var glyph_width: c_int = 0;
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
        c.SDL_Log("Unable to measure glyph: %s", c.TTF_GetError());
56
        c.SDL_Log("Unable to measure glyph: %s", c.TTF_GetError());
57
        return error.TTFInitializationFailed;
57
        return error.TTFInitializationFailed;
58
    }
58
    }
59
    std.debug.assert(glyph_width < 1000);
59
    var glyph_height = c.TTF_FontLineSkip(font);
60
60
61
    var msg = "howdy there, enby! 🐘                                          ".*;
61
    var msg = "howdy there, enby! 🐘                                          ".*;
62
    var pos: usize = 0;
62
    var pos: usize = 0;
63
    var max_chars = std.math.min(@divTrunc(@intCast(usize, surface.*.w), @intCast(usize, glyph_width)), msg.len);
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
    var quit = false;
70
    var quit = false;
66
    while (!quit) {
71
    while (!quit) {
67
        var event: c.SDL_Event = undefined;
72
        var event: c.SDL_Event = undefined;
87
                            pos = if (pos == 0) max_chars - 1 else (pos - 1) % (max_chars - 1);
92
                            pos = if (pos == 0) max_chars - 1 else (pos - 1) % (max_chars - 1);
88
                            msg[pos] = '_';
93
                            msg[pos] = '_';
89
                        },
94
                        },
95
                        c.SDLK_RETURN => {
96
                            result = runCommand(&msg);
97
                        },
90
                        else => {},
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
                c.SDL_TEXTINPUT => {
119
                c.SDL_TEXTINPUT => {
94
                    if (event.text.text.len > 0) {
120
                    if (event.text.text.len > 0) {
109
        const text = c.TTF_RenderUTF8_Shaded(font, &msg, white, black);
135
        const text = c.TTF_RenderUTF8_Shaded(font, &msg, white, black);
110
        _ = c.SDL_BlitSurface(text, null, surface, null);
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
        _ = c.SDL_UpdateWindowSurface(screen);
141
        _ = c.SDL_UpdateWindowSurface(screen);
113
142
114
        c.SDL_Delay(16);
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
}