-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.odin
260 lines (209 loc) · 8.27 KB
/
draw.odin
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package snake
import "core:fmt"
import "core:strings"
import rl "vendor:raylib"
import imgui "odin-imgui"
import imgui_rl "imgui_impl_raylib"
debug_draw_input_keydowns :: proc( x_offset, y_offset: i32) -> (x_end, y_end: i32) {
x_start_text_draw : i32 = x_offset
y_start_text_draw : i32 = y_offset
padding : i32 : 20
x : i32 = x_start_text_draw
y : i32 = y_start_text_draw
keyboard_up_str : string = "rl.KeyboardKey.UP"
keyboard_down_str : string = "rl.KeyboardKey.DOWN"
keyboard_left_str : string = "rl.KeyboardKey.LEFT"
keyboard_right_str : string = "rl.KeyboardKey.RIGHT"
key_down_str : string = "down"
key_up_str : string = "up"
// draw current state of each key
if rl.IsKeyDown(rl.KeyboardKey.UP) {
str := strings.concatenate([]string{keyboard_up_str, ": ", key_down_str})
cstr := strings.clone_to_cstring(str)
rl.DrawText(cstr, x, y, 20, rl.Color{255, 255, 255, 255})
} else {
str := strings.concatenate([]string{keyboard_up_str, ": ", key_up_str})
cstr := strings.clone_to_cstring(str)
rl.DrawText(cstr, x, y, 20, rl.Color{255, 255, 255, 255})
}
y += padding
if rl.IsKeyDown(rl.KeyboardKey.DOWN) {
str := strings.concatenate([]string{keyboard_down_str, ": ", key_down_str})
cstr := strings.clone_to_cstring(str)
rl.DrawText(cstr, x, y, 20, rl.Color{255, 255, 255, 255})
} else {
str := strings.concatenate([]string{keyboard_down_str, ": ", key_up_str})
cstr := strings.clone_to_cstring(str)
rl.DrawText(cstr, x, y, 20, rl.Color{255, 255, 255, 255})
}
y += padding
if rl.IsKeyDown(rl.KeyboardKey.LEFT) {
str := strings.concatenate([]string{keyboard_left_str, ": ", key_down_str})
cstr := strings.clone_to_cstring(str)
rl.DrawText(cstr, x, y, 20, rl.Color{255, 255, 255, 255})
} else {
str := strings.concatenate([]string{keyboard_left_str, ": ", key_up_str})
cstr := strings.clone_to_cstring(str)
rl.DrawText(cstr, x, y, 20, rl.Color{255, 255, 255, 255})
}
y += padding
if rl.IsKeyDown(rl.KeyboardKey.RIGHT) {
str := strings.concatenate([]string{keyboard_right_str, ": ", key_down_str})
cstr := strings.clone_to_cstring(str)
rl.DrawText(cstr, x, y, 20, rl.Color{255, 255, 255, 255})
} else {
str := strings.concatenate([]string{keyboard_right_str, ": ", key_up_str})
cstr := strings.clone_to_cstring(str)
rl.DrawText(cstr, x, y, 20, rl.Color{255, 255, 255, 255})
}
x_end = x
y_end = y + padding
return
}
debug_draw_input_keys_pressed_array :: proc(x_offset, y_offset: i32) -> (x_end, y_end: i32) {
x_start_text_draw : i32 = x_offset
y_start_text_draw : i32 = y_offset
padding : i32 : 20
x : i32 = x_start_text_draw
y : i32 = y_start_text_draw
keyboard_up_str : string = "rl.KeyboardKey.UP"
keyboard_down_str : string = "rl.KeyboardKey.DOWN"
keyboard_left_str : string = "rl.KeyboardKey.LEFT"
keyboard_right_str : string = "rl.KeyboardKey.RIGHT"
// draw keys pressed down in array
for i in 0..<input_num_keys_pressed_down {
key := input_keys_pressed_down[i]
key_str : string
if key == rl.KeyboardKey.UP {
key_str = keyboard_up_str
} else if key == rl.KeyboardKey.DOWN {
key_str = keyboard_down_str
} else if key == rl.KeyboardKey.LEFT {
key_str = keyboard_left_str
} else if key == rl.KeyboardKey.RIGHT {
key_str = keyboard_right_str
}
str := strings.concatenate([]string{"[", key_str, "]"})
cstr := strings.clone_to_cstring(str)
rl.DrawText(cstr, x, y, 20, rl.Color{255, 255, 255, 255})
y += padding
}
// increment y for padding for elements not present
for _ in input_num_keys_pressed_down..<len(input_keys_pressed_down) {
y += padding
}
x_end = x
y_end = y + padding
return
}
debug_draw_snake_movement_direction :: proc(x_offset, y_offset: i32) -> (x_end, y_end: i32) {
x_start_text_draw : i32 = x_offset
y_start_text_draw : i32 = y_offset
padding : i32 : 20
x : i32 = x_start_text_draw
y : i32 = y_start_text_draw
snake_movement_direction_label_str := "snake_movement_direction: "
snake_movement_direction_up_str := "Direction.UP"
snake_movement_direction_down_str := "Direction.DOWN"
snake_movement_direction_left_str := "Direction.LEFT"
snake_movement_direction_right_str := "Direction.RIGHT"
text: string
if snake_movement_direction == Direction.UP {
text = strings.concatenate([]string{snake_movement_direction_label_str, snake_movement_direction_up_str})
} else if snake_movement_direction == Direction.DOWN {
text = strings.concatenate([]string{snake_movement_direction_label_str, snake_movement_direction_down_str})
} else if snake_movement_direction == Direction.LEFT {
text = strings.concatenate([]string{snake_movement_direction_label_str, snake_movement_direction_left_str})
} else if snake_movement_direction == Direction.RIGHT {
text = strings.concatenate([]string{snake_movement_direction_label_str, snake_movement_direction_right_str})
} else {
text = strings.concatenate([]string{snake_movement_direction_label_str, "unknown"})
}
cstr := strings.clone_to_cstring(text)
rl.DrawText(cstr, x, y, 20, rl.Color{255, 255, 255, 255})
x_end = x
y_end = y + padding
return
}
draw_editor_ui :: proc() {
imgui.ShowDemoWindow(nil)
imgui.Render()
imgui_rl.render_draw_data(imgui.GetDrawData())
}
// draw procedures
draw :: proc() {
rl.BeginDrawing()
// draw background color
// rl.ClearBackground(COLOR_BG)
// draw checkered background
color1 : rl.Color = rl.Color{209/2, 113/2, 52/2, 255}
color2 : rl.Color = rl.Color{97/2, 40/2, 21/2, 255}
for x in 0..=GRID_SIZE {
for y in 0..=GRID_SIZE {
color : rl.Color
if (x + y) % 2 == 0 {
color = color1
} else {
color = color2
}
rl.DrawRectangle(x * CELL_RENDER_SIZE, y * CELL_RENDER_SIZE, CELL_RENDER_SIZE, CELL_RENDER_SIZE, color)
}
}
// draw food
rl.DrawRectangle(
food_cell.x * CELL_RENDER_SIZE,
food_cell.y * CELL_RENDER_SIZE,
CELL_RENDER_SIZE,
CELL_RENDER_SIZE,
color_food
)
// draw snake
// head
rl.DrawRectangle(
snake_cells[snake_head_index].x * CELL_RENDER_SIZE,
snake_cells[snake_head_index].y * CELL_RENDER_SIZE,
CELL_RENDER_SIZE,
CELL_RENDER_SIZE,
COLOR_SNAKE_HEAD
)
//tail
tail_cells : []Cell
if snake_head_index > 0 do tail_cells = snake_cells[0 : snake_head_index ]
for cell in tail_cells {
rl.DrawRectangle(
cell.x * CELL_RENDER_SIZE,
cell.y * CELL_RENDER_SIZE,
CELL_RENDER_SIZE,
CELL_RENDER_SIZE,
COLOR_SNAKE_TAIL
)
}
// draw border
// top border
rl.DrawRectangle(0, 0, WINDOW_SIZE, CELL_RENDER_SIZE * BORDER_CELL_PADDING, COLOR_BORDER)
// bottom border
rl.DrawRectangle(0, WINDOW_SIZE - CELL_RENDER_SIZE * BORDER_CELL_PADDING, WINDOW_SIZE, CELL_RENDER_SIZE * BORDER_CELL_PADDING, COLOR_BORDER)
// left border
rl.DrawRectangle(0, 0, CELL_RENDER_SIZE * BORDER_CELL_PADDING, WINDOW_SIZE, COLOR_BORDER)
// right border
rl.DrawRectangle(WINDOW_SIZE - CELL_RENDER_SIZE * BORDER_CELL_PADDING, 0, CELL_RENDER_SIZE * BORDER_CELL_PADDING, WINDOW_SIZE, COLOR_BORDER)
// draw grid
// for x in 0..=GRID_SIZE {
// rl.DrawLine(x * CELL_RENDER_SIZE, 0, x * CELL_RENDER_SIZE, WINDOW_SIZE, COLOR_GRID)
// }
// for y in 0..=GRID_SIZE {
// rl.DrawLine(0, y * CELL_RENDER_SIZE, WINDOW_SIZE, y * CELL_RENDER_SIZE, COLOR_GRID)
// }
// draw debug ui
// x :i32 = 10
// y :i32 = 10
// y_padding_between_elements : i32 = 20
// x, y = debug_draw_snake_movement_direction(x, y)
// y += y_padding_between_elements
// x, y = debug_draw_input_keydowns(x, y)
// y += y_padding_between_elements
// debug_draw_input_keys_pressed_array(x, y)
draw_editor_ui()
// end
rl.EndDrawing()
}