aboutsummaryrefslogtreecommitdiff
path: root/hud.c
blob: a8213059447f9581676420cafda9dd2db55590b5 (plain) (blame)
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
#include "asset.h"
#include "config.h"
#include "render.h"
#include "world.h"

const int specials_pos[][2] = {
#define x(name, x, y) { x, y },
	specials_xmacro
#undef x
};

const Rect heart_empty_rect  = { 0,  0,  7,  6  };
const Rect heart_half_rect   = { 7,  0,  7,  6  };
const Rect heart_full_rect   = { 14, 0,  7,  6  };
const Rect charge_empty_rect = { 0,  6,  7,  7  };
const Rect charge_full_rect  = { 7,  6,  7,  7  };
const Rect background_rect   = { 0,  13, 37, 29 };
const int special_size[2]    = { 16, 8 };

const int hud_offset_x = 4;
const int hud_offset_y = 4;

void hud_clip(Renderer* r) {
	Rect clip;
	clip.x = hud_offset_x - 1;
	clip.y = hud_offset_y - 1;
	clip.w = background_rect.w + 2;
	clip.h = background_rect.h + 2;
	ren_clip(r, &clip);
}

void hud_background(Renderer* r, const Bitmap* bm) {
	ren_map(
		r,
		hud_offset_x,
		hud_offset_y,
		&background_rect,
		bm
	);
}

void hud_hp(Renderer* r, const Bitmap* bm, int h) {
	const int rmd = h & 1;
	const int hh = h >> 1;
	const int hmh = player_health >> 1;
	int i;
	int x = hud_offset_x + 3;
	const int y = hud_offset_y + 3;
	const int s = heart_empty_rect.w + 1;
	for (i = 0; i < hh; i++, x += s) {
		ren_map(
			r,
			x,
			y,
			&heart_full_rect,
			bm
		);
	}
	if (rmd) {
		ren_map(
			r,
			x,
			y,
			&heart_half_rect,
			bm
		);
		i++;
		x += s;
	}
	for (; i < hmh; i++, x += s) 
		ren_map(
			r,
			x,
			y,
			&heart_empty_rect,
			bm
		);
}

void hud_charge(Renderer* r, const Bitmap* bm, int c, int f) {
	int x = hud_offset_x + 3;
	const int y = hud_offset_y + heart_empty_rect.h + 4;
	if (c >= player_special_hits) {
		int tx, ty;
		Rect rect;
		rect.x = x;
		rect.y = y;
		rect.w =
			charge_empty_rect.w *
			player_special_hits +
			player_special_hits - 1;
		rect.h = charge_empty_rect.h;
		tx = rect.x + (rect.w >> 1) - 13 + ((f & 4) == 0);
		ty = rect.y + (rect.h >> 1) - 2 + ((f & 6) == 0);
		ren_text(r, tx, ty, "SPECIAL");
		ren_rect(r, &rect);
	} else {
		const int s = charge_empty_rect.w + 1;
		int i;
		for (i = 0; i < c; i++, x += s)
			ren_map(
				r,
				x,
				y,
				&charge_full_rect,
				bm
			);
		for (; i < player_special_hits; i++, x += s) {
			ren_map(
				r,
				x,
				y,
				&charge_empty_rect,
				bm
			);
		}
	}
}

void hud_special(Renderer* r, const Bitmap* bm, Special s) {
	const int y =
		hud_offset_y +
		heart_empty_rect.h +
		charge_empty_rect.h +
		5;
	int x;
	Rect rect;
	rect.x = specials_pos[s][0];
	rect.y = specials_pos[s][1];
	rect.w = special_size[0];
	rect.h = special_size[1];
	x =
		hud_offset_x +
		(background_rect.w >> 1) -
		(special_size[0] >> 1);
	ren_map(
		r,
		x,
		y,
		&rect,
		bm
	);
}

void ren_hud(const World* w, Renderer* r) {
	const Bitmap* bm = get_bitmap(asset_id_hud_img);
	const Player* p = &w->player;
	const int hp = p->hp;
	const int c = p->charge;
	const Special s = p->spec;
	hud_clip(r);
	ren_clear(r);
	hud_background(r, bm);
	hud_hp(r, bm, hp);
	hud_charge(r, bm, c, w->frame);
	hud_special(r, bm, s);
	ren_rclip(r);
}