aboutsummaryrefslogtreecommitdiff
path: root/client/main.ha
blob: 30a83410549dd5cdae8d2f2e0f06959e62428fbe (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use fmt;
use sdl2;
use math;
use io;
use net;
use net::dial;
use unix::poll;

use drawing;
use drawing::{pos,CHUNKSIZE,picture};
use client::paintui;
use packet_reader;
use packet_reader::{VERSION};

def NSURFS = 16;

def WIN_H: i32 = 480;
def WIN_W: i32 = 640;

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

	const win = sdl2::SDL_CreateWindow("garden", 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)!;

	let pmgr = picture_mgr { ... };

	// connect to server
	const conn = match(dial::dial("tcp","localhost","41460")) {
		case let c: net::socket => yield c;
		case let err: net::error =>
			fmt::fatal("couldn't connect to server:",net::strerror(err));
	};

	// version check
	let byte = [0u8];
	match (io::read(conn, byte)) {
	case let e: io::error =>
		fmt::fatal("error with server handshake",io::strerror(e));
	case =>	if (byte[0] != VERSION)
		fmt::fatalf("invalid version: got {}, expecting {}",
			byte[0], VERSION);
	};
				
	const pollfd: [1]poll::pollfd = [ poll::pollfd {
		fd = conn,
		events=poll::event::POLLIN | poll::event::POLLOUT,
		...
	}];
	const packet_reader = packet_reader::new(conn);

	// paintui state
	let pstate = paintui::state { size_idx = 4, ... };

	let camera_pos: pos = (25, 50);
	let quit = false;
	let n = 0;
	let lasttime = sdl2::SDL_GetTicks();

	// in SCREEN coords
	let mouse_pos: pos = (0,0);
	let mouse_down = false;


	process_chunk_loadedness(&pmgr, &packet_reader, camera_pos);

	const win_pic = picture_from_surface(wsurf, (9,9));
	for (!quit) {
		const did_move = do_movement(&camera_pos);

		// let nvis = 0;
		// for (const pic &.. pictures) {
		// 	if (is_picture_visible(camera_pos, pic)) {
		// 		fmt::printfln("  {} {}",pic.world_pos.0, pic.world_pos.1)!;
		// 		nvis += 1;
		// 	};
		// };
		// fmt::printfln("{} visible",nvis)!;

		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
				else if (sdl2::SDL_Keycode::ZERO <= keysym
						&& keysym <= sdl2::SDL_Keycode::NINE)
					paintui::key(&pstate, keysym - sdl2::SDL_Keycode::ZERO);
			case sdl2::SDL_EventType::MOUSEBUTTONDOWN,
					sdl2::SDL_EventType::MOUSEBUTTONUP =>
				const edata = ev.button;
				mouse_pos = (edata.x, edata.y);
				if (edata.button == 1)
					mouse_down = (edata.state == 1);
			case sdl2::SDL_EventType::MOUSEMOTION =>
				const edata = ev.motion;
				mouse_pos = (edata.x, edata.y);
			case sdl2::SDL_EventType::MOUSEWHEEL =>
				const edata = ev.wheel;
				paintui::mousewheel(&pstate, edata.y);
			case => void;
		};

		const mouse_pos_world = (mouse_pos.0 + camera_pos.0, mouse_pos.1 + camera_pos.1);
		match (paintui::tick(&pstate, mouse_pos_world, mouse_down)) {
		case void => yield;
		case let op: drawing::op =>
			perform_drawop(&pmgr, op);
			packet_reader::write(&packet_reader, op: packet_reader::packet_drawop)!;
		};

		if (did_move) {
			packet_reader::write(&packet_reader, camera_pos: packet_reader::packet_position)!;
			process_chunk_loadedness(&pmgr, &packet_reader, camera_pos);
		};

		const n = poll::poll(pollfd, poll::NONBLOCK)!;
		if (n > 0) {
			const should_read = 0 != pollfd[0].revents & poll::event::POLLIN;
			const should_write = 0 != pollfd[0].revents & poll::event::POLLOUT;
			packet_reader::service(&packet_reader, should_read, should_write)!;
			for (true) match (packet_reader::read_next(&packet_reader)) {
			case done => break;
			case let e: packet_reader::error =>
				fmt::fatalf("death: packet_reader: {}", e);
			case let op: packet_reader::packet_drawop =>
				perform_drawop(&pmgr, op);
			case let packet: packet_reader::packet_sendchunk =>
				enact_chunkdata(&pmgr, packet, camera_pos);
				free(packet.chunk_data);
			};
		};

		drawing::clear_picture(&win_pic, 0xdddddd);

		for (const pic &.. pmgr.pictures)
			render_picture(pic, wsurf, camera_pos);

		drawing::circle_hollow(&win_pic, mouse_pos, paintui::sizes[pstate.size_idx]: i32, pstate.color);

		sdl2::SDL_UpdateWindowSurface(win)!;
		n += 1;
		sdl2::SDL_Delay(1000/60);
	};
};

fn picture_from_surface(surf: *sdl2::SDL_Surface, world_pos: pos) drawing::picture = drawing::picture {
	w = surf.w: size,
	h = surf.h: size,
	d = (surf.pixels as *opaque: *[*]u32),
	world_pos = world_pos,
};

fn render_picture(pic: *picture_c, winsurf: *sdl2::SDL_Surface, camera_pos: pos) void = {
	sdl2::SDL_BlitSurface(pic.surface, null, winsurf, &sdl2::SDL_Rect{
		x = pic.world_pos.0 - camera_pos.0, y = pic.world_pos.1 - camera_pos.1, ...
	})!;
};


def SPEED = 17;
def DIAG_SPEED = 12;  // thereabouts

// returned whether movement happened
fn do_movement(pos: *pos) bool = {
	const kb = sdl2::SDL_GetKeyboardState();
	let dx = 0;
	let dy = 0;
	if (kb[sdl2::SDL_Scancode::W]) dy -= 1;
	if (kb[sdl2::SDL_Scancode::S]) dy += 1;
	if (kb[sdl2::SDL_Scancode::A]) dx -= 1;
	if (kb[sdl2::SDL_Scancode::D]) dx += 1;

	const did_move = dx!=0 || dy!=0;

	let speed = SPEED;
	if (dx != 0 && dy != 0) speed = DIAG_SPEED;

	pos.0 += dx * speed;
	pos.1 += dy * speed;

	return did_move;
};