translate main to zig

This commit is contained in:
Luna 2021-03-17 23:04:58 -03:00
parent 7e3c4d43c7
commit 46ae92d40e

View file

@ -20,6 +20,35 @@ export fn text_height(font: c.mu_Font) c_int {
return c.r_get_text_height(); return c.r_get_text_height();
} }
fn toMuMouse(sdl_mouse: u8) u8 {
return switch (sdl_mouse) {
c.SDL_BUTTON_LEFT => c.MU_MOUSE_LEFT,
c.SDL_BUTTON_RIGHT => c.MU_MOUSE_RIGHT,
c.SDL_BUTTON_MIDDLE => c.MU_MOUSE_MIDDLE,
else => 0,
};
}
fn toMuKey(sdl_key: i32) u8 {
return switch (sdl_key) {
c.SDLK_LSHIFT => c.MU_KEY_SHIFT,
c.SDLK_RSHIFT => c.MU_KEY_SHIFT,
c.SDLK_LCTRL => c.MU_KEY_CTRL,
c.SDLK_RCTRL => c.MU_KEY_CTRL,
c.SDLK_LALT => c.MU_KEY_ALT,
c.SDLK_RALT => c.MU_KEY_ALT,
c.SDLK_RETURN => c.MU_KEY_RETURN,
c.SDLK_BACKSPACE => c.MU_KEY_BACKSPACE,
else => 0,
};
}
fn processFrame(ctx: *c.mu_Context) void {
c.mu_begin(ctx);
c.mu_end(ctx);
}
pub fn main() anyerror!void { pub fn main() anyerror!void {
_ = c.SDL_Init(c.SDL_INIT_EVERYTHING); _ = c.SDL_Init(c.SDL_INIT_EVERYTHING);
c.r_init(); c.r_init();
@ -34,12 +63,51 @@ pub fn main() anyerror!void {
while (true) { while (true) {
var e: c.SDL_Event = undefined; var e: c.SDL_Event = undefined;
while (c.SDL_PollEvent(&e) != 0) { while (c.SDL_PollEvent(&e) != 0) {
switch (e.@"type") { switch (e.@"type") {
c.SDL_QUIT => std.os.exit(0), c.SDL_QUIT => std.os.exit(0),
else => @panic("fuck"), c.SDL_MOUSEMOTION => c.mu_input_mousemove(ctx, e.motion.x, e.motion.y),
c.SDL_MOUSEWHEEL => c.mu_input_scroll(ctx, 0, e.motion.y * -30),
c.SDL_TEXTINPUT => c.mu_input_text(ctx, &e.text.text),
c.SDL_MOUSEBUTTONDOWN, c.SDL_MOUSEBUTTONUP => {
const b = toMuMouse(e.button.button);
if (b != 0 and e.type == c.SDL_MOUSEBUTTONDOWN) {
c.mu_input_mousedown(ctx, e.button.x, e.button.y, b);
}
if (b != 0 and e.type == c.SDL_MOUSEBUTTONUP) {
c.mu_input_mouseup(ctx, e.button.x, e.button.y, b);
}
},
c.SDL_KEYDOWN, c.SDL_KEYUP => {
var mu_char = toMuKey(e.key.keysym.sym);
if (mu_char != 0 and e.@"type" == c.SDL_KEYDOWN) {
c.mu_input_keydown(ctx, mu_char);
}
if (mu_char != 0 and e.@"type" == c.SDL_KEYUP) {
c.mu_input_keyup(ctx, mu_char);
}
},
else => {},
} }
} }
processFrame(ctx);
c.r_clear(c.mu_color(90, 95, 100, 255));
var cmd: [*c]c.mu_Command = null;
while (c.mu_next_command(ctx, &cmd) != 0) {
switch (cmd.*.@"type") {
c.MU_COMMAND_TEXT => c.r_draw_text(&cmd.*.text.str, cmd.*.text.pos, cmd.*.text.color),
c.MU_COMMAND_RECT => c.r_draw_rect(cmd.*.rect.rect, cmd.*.rect.color),
c.MU_COMMAND_ICON => c.r_draw_icon(cmd.*.icon.id, cmd.*.icon.rect, cmd.*.icon.color),
c.MU_COMMAND_CLIP => c.r_set_clip_rect(cmd.*.clip.rect),
else => {},
}
}
c.r_present();
} }
} }