Selaa lähdekoodia

Implement horizontal scrolling and tabs!

Luna Stadler 4 vuotta sitten
vanhempi
commit
a8962ac68e
1 muutettua tiedostoa jossa 22 lisäystä ja 5 poistoa
  1. 22 5
      zig/sdl/hello_sdl.zig

+ 22 - 5
zig/sdl/hello_sdl.zig

@ -425,6 +425,7 @@ pub fn main() !void {
425 425
426 426
    var quit = false;
427 427
    var skip: i32 = 0;
428
    var skip_horizontal: usize = 0;
428 429
    var num_lines: i32 = 0;
429 430
430 431
    var changed = false;
@ -538,6 +539,14 @@ pub fn main() !void {
538 539
                                    skip = num_lines - 10;
539 540
                                }
540 541
                            },
542
                            c.SDLK_LEFT => {
543
                                if (skip_horizontal > 0) {
544
                                    skip_horizontal -= 1;
545
                                }
546
                            },
547
                            c.SDLK_RIGHT => {
548
                                skip_horizontal += 1;
549
                            },
541 550
                            else => {},
542 551
                        }
543 552
                    }
@ -565,6 +574,9 @@ pub fn main() !void {
565 574
566 575
            changed = false;
567 576
            lastChange = c.SDL_GetTicks();
577
578
            skip = 0;
579
            skip_horizontal = 0;
568 580
        }
569 581
570 582
        _ = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 100);
@ -595,6 +607,7 @@ pub fn main() !void {
595 607
        }
596 608
597 609
        var i: c_int = 1;
610
        var line_buf = [_]u8{0} ** 10000;
598 611
        for (commands) |*command| {
599 612
            if (!command.isActive(cmd)) {
600 613
                continue;
@ -620,12 +633,16 @@ pub fn main() !void {
620 633
                }
621 634
            }
622 635
            while (line != null and i * glyph_height < window_height) {
623
                const line_c = try gpa.dupeZ(u8, line.?);
624
                // TODO: render tabs at correct width (or some width at least)
625
                const result_text = c.TTF_RenderUTF8_Shaded(font, line_c, white, black);
626
                gpa.free(line_c);
636
                const skipped_line = line.?[std.math.min(skip_horizontal, line.?.len)..];
637
638
                // fix tabs
639
                const repl_size = std.mem.replacementSize(u8, skipped_line, "\t", " " ** 8);
640
                _ = std.mem.replace(u8, skipped_line, "\t", " " ** 8, &line_buf);
641
                line_buf[repl_size] = 0;
642
643
                const result_text = c.TTF_RenderUTF8_Shaded(font, &line_buf, white, black);
627 644
                const result_texture = c.SDL_CreateTextureFromSurface(renderer, result_text);
628
                _ = c.SDL_RenderCopy(renderer, result_texture, null, &c.SDL_Rect{ .x = 0, .y = i * glyph_height, .w = @intCast(c_int, line.?.len) * glyph_width, .h = glyph_height });
645
                _ = c.SDL_RenderCopy(renderer, result_texture, null, &c.SDL_Rect{ .x = 0, .y = i * glyph_height, .w = @intCast(c_int, repl_size) * glyph_width, .h = glyph_height });
629 646
                c.SDL_FreeSurface(result_text);
630 647
                c.SDL_DestroyTexture(result_texture);
631 648