aboutsummaryrefslogtreecommitdiff
path: root/main.ha
blob: 42d42cfa6c681af26afef2d85be77c64e1e4933c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use fmt;
use sdl2;

export fn main() void = {
	sdl2::SDL_Init(sdl2::SDL_INIT_VIDEO)!;
	defer sdl2::SDL_Quit();

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

	const wsurf = sdl2::SDL_GetWindowSurface(win)!;
	const format = wsurf.format;
	assert(format.bytesperpixel == 4, "can only work with u32 pixels");
	fmt::printfln("got format {}. bytes per pixel {}. r{:x} g{:x} b{:x} a{:x}",
		format.format, format.bytesperpixel, format.rmask, format.gmask, format.bmask, format.amask)!;
	fmt::printfln("red: {:x} green {:x} blue {:x}",
		sdl2::SDL_MapRGB(format, 255, 0, 0),
		sdl2::SDL_MapRGB(format, 0, 255, 0),
		sdl2::SDL_MapRGB(format, 0, 0, 255))!;
	fmt::println(wsurf.w * wsurf.h)!;

	const pixels = (wsurf.pixels as *opaque: *[*]u32)[..wsurf.w * wsurf.h];


	let quit = false;
	let n = 0z;
	for (!quit) {
		let ev = sdl2::event { ... };
		for (sdl2::SDL_PollEvent(&ev)! == 1) switch (ev.event_type) {
			case sdl2::SDL_EventType::QUIT => quit = true;
			case sdl2::SDL_EventType::KEYDOWN => {
				const keysym = ev.key.keysym.sym;
				if (keysym == sdl2::SDL_Keycode::ESCAPE) quit = true;
				fmt::println("got key", keysym: int)!;
			};
			case => void;
		};

		for (let i = 0z; i < 100; i+=1) {
			pixels[n] = sdl2::SDL_MapRGB(format, 0, 255, 0);
			n += 1;
		};


		sdl2::SDL_UpdateWindowSurface(win)!;
		sdl2::SDL_Delay(1000 / 60);

		// sdl2::SDL_SetRenderDrawColor(render, 50, 50, 50, 255)!;
		// sdl2::SDL_RenderClear(render)!;

		// rect.x = (sdl2::SDL_GetTicks(): int)/100;
		// sdl2::SDL_SetRenderDrawColor(render, 255, 0, 0, 0)!;
		// sdl2::SDL_RenderFillRect(render, &rect)!;

		// sdl2::SDL_RenderPresent(render);
		// sdl2::SDL_Delay(1000 / 60);
	};




};