forked from ManIsCat2/sm64coopdx
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhud.lua
More file actions
235 lines (179 loc) · 6.09 KB
/
hud.lua
File metadata and controls
235 lines (179 loc) · 6.09 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
-- This file is an example of how to render to the screen
local function test_text()
-- set text and scale
local text = "This is example text in the bottom right."
local scale = 1
-- render to native screen space, with the MENU font
djui_hud_set_resolution(RESOLUTION_DJUI)
djui_hud_set_font(FONT_MENU)
-- get width of screen and text
local screenWidth = djui_hud_get_screen_width()
local width = djui_hud_measure_text(text) * scale
-- get height of screen and text
local screenHeight = djui_hud_get_screen_height()
local height = 64 * scale
-- set location
local x = screenWidth - width
local y = screenHeight - height
-- set color and render
djui_hud_set_color(255, 0, 255, 255)
djui_hud_print_text(text, x, y, scale)
end
local function test_texture()
-- render to the N64's screen space
djui_hud_set_resolution(RESOLUTION_N64)
local tex = gTextures.star
-- color and render
djui_hud_set_color(255, 0, 255, 255)
djui_hud_render_texture(tex, 256, 64, 1, 1)
-- render texture, but cut in half
djui_hud_render_texture_tile(tex, 256, 84, 2, 1, 0, 0, 8, 16)
end
local function test_rect()
-- render to native screen space
djui_hud_set_resolution(RESOLUTION_DJUI)
-- set location
local x = 512
local y = 512
-- set width/height
local w = 256
local h = 256
-- set color and render first rectangle
djui_hud_set_color(255, 0, 0, 128)
djui_hud_render_rect(x, y, w, h)
-- adjust location and size
x = x + 16
y = y + 16
w = w - 32
h = h - 32
-- set color and render second rectangle
djui_hud_set_color(0, 255, 0, 128)
djui_hud_render_rect(x, y, w, h)
-- adjust location and size
x = x + 16
y = y + 16
w = w - 32
h = h - 32
-- set color and render third rectangle
djui_hud_set_color(0, 0, 255, 128)
djui_hud_render_rect(x, y, w, h)
end
local function test_rainbow_text()
-- this function is incredibly silly
-- don't do anything like this
local text = "HELLO WORLD"
local scale = 3
local gt = get_global_timer()
djui_hud_set_resolution(RESOLUTION_DJUI)
djui_hud_set_font(FONT_NORMAL)
for i=0,255 do
j = gt / 50
local r = math.sin(0.00 + i / 15 + j) * 127 + 127
local g = math.sin(0.33 + i / 33 + j) * 127 + 127
local b = math.sin(0.66 + i / 77 + j) * 127 + 127
local x = 64 + i
local y = 64 + i + math.sin(i / 40 + j) * 64
if i == 255 then
r = 0
g = 0
b = 0
end
djui_hud_set_color(r, g, b, i)
djui_hud_print_text(text, x, y, scale)
end
end
local function test_rotation()
-- render to native screen space
djui_hud_set_resolution(RESOLUTION_DJUI)
-- get SM64 global timer
local gt = get_global_timer()
-- rotate object around pivot (center)
djui_hud_set_rotation(gt * 512, 0.5, 0.5)
-- color and render
djui_hud_set_color(255, 255, 0, 128)
djui_hud_render_rect(1280, 512, 64, 64)
-- rotate object around pivot (top-left); color and render
djui_hud_set_rotation(gt * 512, 0, 0)
djui_hud_set_color(0, 255, 255, 128)
djui_hud_render_rect(1280, 512, 64, 64)
-- rotate object around pivot (bottom-right); color and render
djui_hud_set_rotation(gt * 512, 1, 1)
djui_hud_set_color(255, 0, 255, 128)
djui_hud_render_rect(1280, 512, 64, 64)
end
local function test_filtering()
local scale = 1
-- reset rotation from earlier
djui_hud_set_rotation(0, 0, 0)
-- render to the N64's screen space with the HUD font
djui_hud_set_resolution(RESOLUTION_N64)
djui_hud_set_font(FONT_HUD)
djui_hud_reset_color()
-- get height of screen and text
local screenHeight = djui_hud_get_screen_height()
local height = 16 * scale
local y = screenHeight - height
-- set filtering and render
djui_hud_set_filter(FILTER_NEAREST)
djui_hud_print_text("NEAREST", 0, y, scale)
-- adjust position
y = y - height
-- set filtering and render
djui_hud_set_filter(FILTER_LINEAR)
djui_hud_print_text("LINEAR", 0, y, scale)
end
local function test_world_to_screen()
-- render to the N64's screen space (recommended for this)
djui_hud_set_resolution(RESOLUTION_N64)
-- reset filtering from earlier
djui_hud_set_filter(FILTER_NEAREST)
-- project to mario's position
local out = {x = 0, y = 0, z = 0}
djui_hud_world_pos_to_screen_pos(gMarioStates[0].pos,out)
-- get player's character texture
local tex = gMarioStates[0].character.hudHeadTexture
djui_hud_reset_color()
djui_hud_render_texture(tex, out.x, out.y, 1, 1)
end
local function test_mouse()
-- render to native screen space (recommended for this)
djui_hud_set_resolution(RESOLUTION_DJUI)
djui_hud_reset_color()
-- get mouse position
local mouseX = djui_hud_get_mouse_x()
local mouseY = djui_hud_get_mouse_y()
local scale = 4
djui_hud_render_texture(gTextures.arrow_up, mouseX, mouseY, scale, scale)
end
local function test_pow_meter()
djui_hud_set_resolution(RESOLUTION_N64)
local hp = gMarioStates[0].health
hud_render_power_meter(hp,16,128,24,24) -- health value ranges from 255 (0xFF in hex) to 2176 (0x880 in hex)
end
-- HOOK_ON_HUD_RENDER renders above SM64's hud
local function on_hud_render()
test_text()
test_rect()
test_texture()
test_rainbow_text()
test_rotation()
test_filtering()
test_world_to_screen()
test_pow_meter()
test_mouse()
end
-- HOOK_ON_HUD_RENDER_BEHIND renders behind SM64's hud
local function on_hud_render_behind()
local scale = 0.5
local text = "This text renders behind SM64's HUD!"
djui_hud_set_resolution(RESOLUTION_N64)
djui_hud_set_font(FONT_RECOLOR_HUD)
local screenWidth = djui_hud_get_screen_width()
local width = djui_hud_measure_text(text) * scale
local x = screenWidth - width
djui_hud_set_color(0, 255, 255, 255)
djui_hud_print_text(text, x, 22, scale)
end
-- hooks --
hook_event(HOOK_ON_HUD_RENDER, on_hud_render)
hook_event(HOOK_ON_HUD_RENDER_BEHIND, on_hud_render_behind)