~sircmpwn/hdmg

0a3d4317269102a37bb0802c371972b9fc276b7e — Drew DeVault 2 years ago 0d3a856
hdmg: initial SDL riggings
1 files changed, 51 insertions(+), 0 deletions(-)

M cmd/hdmg/main.ha
M cmd/hdmg/main.ha => cmd/hdmg/main.ha +51 -0
@@ 3,6 3,18 @@ use dmg::cart;
use fmt;
use io;
use os;
use sdl2;
use sdl2::{
	SDL_EventType,
	SDL_RendererFlags,
	SDL_WindowFlags
};

export type state = struct {
	quit: bool,
	window: *sdl2::SDL_Window,
	render: *sdl2::SDL_Renderer,
};

export fn main() void = {
	const file = os::open(os::args[1])!;


@@ 12,4 24,43 @@ export fn main() void = {

	const dmg = dmg::init(cart);
	defer dmg::finish(&dmg);

	sdl2::SDL_Init(sdl2::SDL_INIT_VIDEO)!;
	defer sdl2::SDL_Quit();

	const win = sdl2::SDL_CreateWindow("hdmg",
		sdl2::SDL_WINDOWPOS_UNDEFINED, sdl2::SDL_WINDOWPOS_UNDEFINED,
		640, 576, SDL_WindowFlags::NONE)!;
	defer sdl2::SDL_DestroyWindow(win);

	const render = sdl2::SDL_CreateRenderer(win, -1, SDL_RendererFlags::NONE)!;
	defer sdl2::SDL_DestroyRenderer(render);

	let state = state {
		quit = false,
		window = win,
		render = render,
	};

	for (!state.quit) {
		update(&state)!;
		draw(&state)!;
		sdl2::SDL_Delay(1000 / 60);
	};
};

fn update(state: *state) (void | sdl2::error) = {
	let ev = sdl2::event { ... };
	for (sdl2::SDL_PollEvent(&ev)? == 1) switch (ev.event_type) {
	case SDL_EventType::QUIT =>
		state.quit = true;
		return;
	case =>
		yield;
	};
};

fn draw(state: *state) (void | sdl2::error) = {
	sdl2::SDL_RenderClear(state.render)?;
	sdl2::SDL_RenderPresent(state.render);
};