ソースを参照

Draw input hints to a separate string

This makes it possible to actually run things after deleting
characters...
Luna Stadler 4 年 前
コミット
42f9763ae9
共有1 個のファイルを変更した33 個の追加10 個の削除を含む
  1. 33 10
      zig/sdl/hello_sdl.zig

+ 33 - 10
zig/sdl/hello_sdl.zig

@ -402,6 +402,7 @@ pub fn main() !void {
402 402
    const renderer = c.SDL_CreateRenderer(screen, -1, c.SDL_RENDERER_ACCELERATED);
403 403
404 404
    var msg = "                                                                                                    ".*;
405
    var msg_overlay = "                                                                                                    ".*;
405 406
    var pos: usize = 0;
406 407
    var max_chars = std.math.min(@divTrunc(@intCast(usize, window_width), @intCast(usize, glyph_width)), msg.len);
407 408
@ -451,8 +452,11 @@ pub fn main() !void {
451 452
                    if (ctrlPressed) {
452 453
                        switch (event.key.keysym.sym) {
453 454
                            c.SDLK_a => {
455
                                if (msg_overlay[pos] == '_') {
456
                                    msg_overlay[pos] = ' ';
457
                                }
454 458
                                pos = 0;
455
                                msg[pos] = '_';
459
                                msg_overlay[pos] = '_';
456 460
                            },
457 461
                            c.SDLK_k => {
458 462
                                var i: usize = 0;
@ -493,8 +497,14 @@ pub fn main() !void {
493 497
                                quit = true;
494 498
                            },
495 499
                            c.SDLK_BACKSPACE => {
500
                                if (msg_overlay[pos] == '_') {
501
                                    msg_overlay[pos] = ' ';
502
                                }
503
                                if (pos > 0) {
504
                                    msg[pos - 1] = ' ';
505
                                }
496 506
                                pos = if (pos == 0) max_chars - 1 else (pos - 1) % (max_chars - 1);
497
                                msg[pos] = '_';
507
                                msg_overlay[pos] = '_';
498 508
                                changed = true;
499 509
                            },
500 510
                            c.SDLK_RETURN => {
@ -536,6 +546,7 @@ pub fn main() !void {
536 546
                    if (!ctrlPressed and event.text.text.len > 0) {
537 547
                        c.SDL_Log("input: '%s' at %d", event.text.text, pos);
538 548
                        msg[pos] = event.text.text[0];
549
                        msg_overlay[pos] = ' ';
539 550
                        pos = (pos + 1) % (max_chars - 1);
540 551
541 552
                        changed = true;
@ -561,15 +572,27 @@ pub fn main() !void {
561 572
        _ = c.SDL_RenderClear(renderer);
562 573
563 574
        // thanks to https://stackoverflow.com/questions/22886500/how-to-render-text-in-sdl2 for some actually useful code here
564
        const white: c.SDL_Color = c.SDL_Color{ .r = 255, .g = 255, .b = 255, .a = 0 };
575
        const white: c.SDL_Color = c.SDL_Color{ .r = 255, .g = 255, .b = 255, .a = 255 };
565 576
        const gray: c.SDL_Color = c.SDL_Color{ .r = 150, .g = 150, .b = 150, .a = 255 };
566
        const black: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 100 };
567
        // Shaded vs Solid gives a nicer output, with solid the output
568
        // was squiggly and not aligned with a baseline.
569
        const text = c.TTF_RenderUTF8_Shaded(font, &msg, white, black);
570
        const texture = c.SDL_CreateTextureFromSurface(renderer, text);
571
        c.SDL_FreeSurface(text);
572
        _ = c.SDL_RenderCopy(renderer, texture, null, &c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(c_int, msg.len) * glyph_width, .h = glyph_height });
577
        const black: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
578
579
        {
580
            // Shaded vs Solid gives a nicer output, with solid the output
581
            // was squiggly and not aligned with a baseline.
582
            const text = c.TTF_RenderUTF8_Shaded(font, &msg, white, black);
583
            const texture = c.SDL_CreateTextureFromSurface(renderer, text);
584
            c.SDL_FreeSurface(text);
585
            _ = c.SDL_RenderCopy(renderer, texture, null, &c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(c_int, msg.len) * glyph_width, .h = glyph_height });
586
        }
587
        {
588
            const text = c.TTF_RenderUTF8_Shaded(font, &msg_overlay, white, c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 0 });
589
            const texture = c.SDL_CreateTextureFromSurface(renderer, text);
590
            if (c.SDL_SetTextureBlendMode(texture, c.SDL_BLENDMODE_ADD) != 0) {
591
                c.SDL_Log("Unable set texture blend mode: %s", c.SDL_GetError());
592
            }
593
            c.SDL_FreeSurface(text);
594
            _ = c.SDL_RenderCopy(renderer, texture, null, &c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(c_int, msg.len) * glyph_width, .h = glyph_height });
595
        }
573 596
574 597
        var i: c_int = 1;
575 598
        for (commands) |*command| {