~mil/mepo

0427fc1431663c2917bb3f96a412f1ad38ae3fe5 — Miles Alan 2 years ago 7641751 move-sdl-user-eventtypes-to-utilsdl
Move custom SDL userevent for mepolang execution to utilsdl

E.g. to pave way for also doing signals like this without 'global state'..
downside is there is an extra if statement to get event type
3 files changed, 18 insertions(+), 11 deletions(-)

M src/Mepo.zig
M src/api/shellpipe.zig
M src/util/utilsdl.zig
M src/Mepo.zig => src/Mepo.zig +2 -9
@@ 811,13 811,6 @@ fn event_unhandled(_: *@This(), e: sdl.SDL_Event) types.Pending {
    return .None;
}

pub fn sdl_pushevent_mepolang_execution(mepo: *@This(), mepolang_text: *[]const u8) void {
    var sdlevent: sdl.SDL_Event = undefined;
    sdlevent.type = mepo.sdl_event_type_mepolang_execution;
    sdlevent.user.data1 = mepolang_text;
    _ = sdl.SDL_PushEvent(&sdlevent);
}

pub fn mepolang_execute(mepo: *@This(), mepolang_text: []const u8) !void {
    var arena = std.heap.ArenaAllocator.init(mepo.allocator);
    defer arena.deinit();


@@ 927,7 920,7 @@ pub fn init_video_and_sdl_stdin_loop(mepo: *@This()) !void {
        const stdin = &std.io.getStdIn().reader();
        var read_slice: []const u8 = stdin.readUntilDelimiterAlloc(mepo.allocator, '\n', 102400) catch continue;
        std.debug.print("Read {d} bytes, running input as mepolang\n", .{read_slice.len});
        mepo.sdl_pushevent_mepolang_execution(&read_slice);
        utilsdl.sdl_push_user_mepolang_execution(&read_slice);
    }
}



@@ 968,7 961,7 @@ pub fn sdl_event_loop(mepo: *@This()) !void {
                sdl.SDL_QUIT => event_quit,
                sdl.SDL_WINDOWEVENT => event_windowevent,
                else => b: {
                    if (e.type == mepo.sdl_event_type_mepolang_execution) break :b event_mepolangexecution;
                    if (e.type == utilsdl.sdl_eventtype_mepolang()) break :b event_mepolangexecution;
                    break :b event_unhandled;
                },
            }(mepo, e);

M src/api/shellpipe.zig => src/api/shellpipe.zig +1 -1
@@ 128,7 128,7 @@ fn async_mepolang_execute(mepo: *Mepo, allocator: std.mem.Allocator, cmd: []cons
                const statement = stdout.items[0 .. mepolang_statement_end_index + 1];
                utildbg.log("Running mepolang statement from async shellipe: {s}\n", .{statement});
                var heap_statement = try mepo.allocator.dupe(u8, statement);
                mepo.sdl_pushevent_mepolang_execution(&heap_statement);
                utilsdl.sdl_push_user_mepolang_execution(&heap_statement);

                // TODO: is there a slice 'shift' like functionality in arraylist?
                var i: usize = mepolang_statement_end_index + 2;

M src/util/utilsdl.zig => src/util/utilsdl.zig +15 -1
@@ 2,9 2,10 @@ const sdl = @import("../sdlshim.zig");
const types = @import("../types.zig");
const errors = @import("../errors.zig");
const utildbg = @import("./utildbg.zig");

const RectOpt = enum { Draw, Fill };
var sdl_user_event_0 : ?u32 = null;

// E.g. basically used as a refresh event
pub fn sdl_push_resize_event() void {
    const pending_drag = sdl.SDL_HasEvent(sdl.SDL_MOUSEMOTION) == sdl.SDL_TRUE;
    const pending_redraw = sdl.SDL_HasEvent(sdl.SDL_WINDOWEVENT) == sdl.SDL_TRUE;


@@ 16,6 17,19 @@ pub fn sdl_push_resize_event() void {
    _ = sdl.SDL_PushEvent(&sdlevent);
}

pub fn sdl_eventtype_mepolang() u32 {
    if (sdl_user_event_0) |u| return u;
    sdl_user_event_0 = sdl.SDL_RegisterEvents(1);
    return sdl_user_event_0.?;
}

pub fn sdl_push_user_mepolang_execution(mepolang_text: *[]const u8) void {
    var sdlevent: sdl.SDL_Event = undefined;
    sdlevent.type = sdl_eventtype_mepolang();
    sdlevent.user.data1 = mepolang_text;
    _ = sdl.SDL_PushEvent(&sdlevent);
}

pub fn sdl_renderer_set_draw_color(renderer: *sdl.SDL_Renderer, color: types.Color) errors.SDLError!void {
    var sdl_color = color.to_sdl();
    const blend_mode = if (sdl_color.a != sdl.SDL_ALPHA_OPAQUE) sdl.SDL_BLENDMODE_ADD else sdl.SDL_BLENDMODE_NONE;