Pārlūkot izejas kodu

Do the same in Zig!

Only text rendering so far, text input should come next.
Luna Stadler 4 gadi atpakaļ
vecāks
revīzija
c43697936e
2 mainītis faili ar 93 papildinājumiem un 0 dzēšanām
  1. 17 0
      c/sdl/build.zig
  2. 76 0
      c/sdl/hello_sdl.zig

+ 17 - 0
c/sdl/build.zig

@ -0,0 +1,17 @@
1
const Builder = @import("std").build.Builder;
2
3
pub fn build(b: *Builder) void {
4
    const mode = b.standardReleaseOptions();
5
    const exe = b.addExecutable("sdl-zig-demo", "./hello_sdl.zig");
6
    exe.setBuildMode(mode);
7
    exe.linkSystemLibrary("SDL2");
8
    exe.linkSystemLibrary("SDL2_ttf");
9
    exe.linkSystemLibrary("c");
10
11
    b.default_step.dependOn(&exe.step);
12
    b.installArtifact(exe);
13
14
    const run = b.step("run", "Run the demo");
15
    const run_cmd = exe.run();
16
    run.dependOn(&run_cmd.step);
17
}

+ 76 - 0
c/sdl/hello_sdl.zig

@ -0,0 +1,76 @@
1
// Playing around with SDL + TTF.
2
//
3
// Based on `hello_sdl.c`, zig + SDL code from https://github.com/andrewrk/sdl-zig-demo.
4
//
5
// Resources:
6
// - http://wiki.libsdl.org/CategoryAPI
7
// - https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html
8
const c = @cImport({
9
    @cInclude("SDL2/SDL.h");
10
    @cInclude("SDL2/SDL_ttf.h");
11
});
12
const assert = @import("std").debug.assert;
13
const process = @import("std").process;
14
15
pub fn main() !void {
16
    if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
17
        c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError());
18
        return error.SDLInitializationFailed;
19
    }
20
    defer c.SDL_Quit();
21
22
    if (c.TTF_Init() != 0) {
23
        c.SDL_Log("Unable to initialize SDL_ttf: %s", c.TTF_GetError());
24
        return error.TTFInitializationFailed;
25
    }
26
    defer c.TTF_Quit();
27
28
    var font_file = "./FantasqueSansMono-Regular.ttf";
29
    const font = c.TTF_OpenFont(font_file, 20) orelse {
30
        c.SDL_Log("Unable to load font: %s", c.TTF_GetError());
31
        return error.TTFInitializationFailed;
32
    };
33
    defer c.TTF_CloseFont(font);
34
35
    const msg = "howdy there, enby! 🐘                                          ";
36
    c.SDL_Log(msg);
37
38
    const screen = c.SDL_CreateWindow("hello fonts", c.SDL_WINDOWPOS_CENTERED, c.SDL_WINDOWPOS_CENTERED, 600, 100, c.SDL_WINDOW_BORDERLESS) orelse {
39
        c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
40
        return error.SDLInitializationFailed;
41
    };
42
    defer c.SDL_DestroyWindow(screen);
43
44
    const surface = c.SDL_GetWindowSurface(screen);
45
46
    var quit = false;
47
    while (!quit) {
48
        var event: c.SDL_Event = undefined;
49
        while (c.SDL_PollEvent(&event) != 0) {
50
            switch (event.@"type") {
51
                c.SDL_QUIT => {
52
                    quit = true;
53
                },
54
                c.SDL_KEYDOWN => {
55
                    switch (event.key.keysym.sym) {
56
                        c.SDLK_ESCAPE => {
57
                            quit = true;
58
                        },
59
                        else => {},
60
                    }
61
                },
62
                else => {},
63
            }
64
        }
65
66
        // thanks to https://stackoverflow.com/questions/22886500/how-to-render-text-in-sdl2 for some actually useful code here
67
        const white: c.SDL_Color = c.SDL_Color{ .r = 255, .g = 255, .b = 255, .a = 255 };
68
        const black: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
69
        const text = c.TTF_RenderUTF8_Shaded(font, msg, white, black);
70
        _ = c.SDL_BlitSurface(text, null, surface, null);
71
72
        _ = c.SDL_UpdateWindowSurface(screen);
73
74
        c.SDL_Delay(16);
75
    }
76
}