~sircmpwn/tetrominoes

tetrominoes/draw.ha -rw-r--r-- 1.3 KiB
7209df15Drew DeVault Update for integer changes 3 months ago
                                                                                
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
use sdl2;

fn draw(state: *state) (void | sdl2::error) = {
	sdl2::set_render_draw_color(state.render, 0, 0, 0, 255)?;
	sdl2::render_clear(state.render)?;
	sdl2::render_copy(state.render, state.scene.tex, null, &sdl2::rect{
		x = 0,
		y = 0,
		w = 640,
		h = 480,
	})?;

	draw_board(state, &state.board)?;

	switch (state.state) {
	case gamestate::CLEAR =>
		draw_clear(state)?;
	case => void; // No special drawing required
	};

	sdl2::render_present(state.render);
};

fn draw_clear(state: *state) (void | sdl2::error) = {
	if (state.clearframe < 0) {
		return;
	};
	for (let i = 0z; i < len(state.clearlines); i += 1) {
		draw_clearline(state, state.clearlines[i])?;
	};
};

fn draw_clearline(state: *state, y: int) (void | sdl2::error) = {
	const inrect = &sdl2::rect {
		x = state.clearframe * 4,
		y = 0,
		w = 4,
		h = 4,
	};
	const tex = state.clear;
	const board = &state.board;
	const offs = (board.offs_x * SCENE_SCALE, board.offs_y * SCENE_SCALE);
	for (let x = 0; x < BOARD_WIDTH; x += 1) {
		const outrect = &sdl2::rect {
			x = offs.0 + x * BOARD_SCALE * TETROMINO_WIDTH,
			y = offs.1 + y * BOARD_SCALE * TETROMINO_HEIGHT,
			w = TETROMINO_WIDTH * BOARD_SCALE,
			h = TETROMINO_HEIGHT * BOARD_SCALE,
		};
		sdl2::render_copy(state.render, tex.tex, inrect, outrect)?;
	};
};