-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.odin
More file actions
295 lines (251 loc) · 9.45 KB
/
Copy pathmain.odin
File metadata and controls
295 lines (251 loc) · 9.45 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import "core:fmt"
import "core:mem"
import "core:strings"
import "core:slice"
import rl "vendor:raylib"
@(private = "file") hovered_square_vec: [2]int
@(private = "file") highlighted_squares: [dynamic]Square
@(private = "file") selected_piece: ^Piece
@(private = "file") move_sound: rl.Sound
// Buttons
@(private = "file") is_reset_pressed: bool
@(private = "file") is_next_move_pressed: bool
@(private = "file") is_prev_move_pressed: bool
@(private) current_move: int
// CONFIG
@(private = "file") SHOW_VALID_SQUARES := true
main :: proc() {
when ODIN_DEBUG {
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
context.allocator = mem.tracking_allocator(&track)
defer {
if len(track.allocation_map) > 0 {
for _, entry in track.allocation_map {
fmt.eprintf("%v leaked %v bytes\n", entry.location, entry.size)
}
}
if len(track.bad_free_array) > 0 {
for entry in track.bad_free_array {
fmt.eprintf("%v bad free at %v\n", entry.location, entry.memory)
}
}
mem.tracking_allocator_destroy(&track)
}
}
rl.InitWindow(1000, 800, "RayChess")
rl.SetWindowMonitor(1)
rl.InitAudioDevice()
move_sound = rl.LoadSound("./assets/move-self.mp3")
rl.SetTargetFPS(60)
game := Game{}
init_board(&game)
/*board_pieces_clone := slice.clone_to_dynamic(game.board.pieces[:])
defer delete(board_pieces_clone)
append(&game.board_history, board_pieces_clone)*/
for !rl.WindowShouldClose() {
update_init(&game)
rl.BeginDrawing()
rl.ClearBackground(rl.RAYWHITE)
draw_init(game)
rl.EndDrawing()
}
for player_key, player_pieces in game.board.pieces_map {
for piece_key, piece in player_pieces {
rl.UnloadTexture(piece.texture)
}
}
rl.UnloadSound(move_sound)
rl.CloseWindow()
}
@(private = "file")
update_init :: proc(game: ^Game) {
mouse_pos := rl.GetMousePosition()
pieces_on_board := &game.board.pieces_map
if (mouse_pos.x >= 810 ||
mouse_pos.y < 0 ||
mouse_pos.y >= 810 ||
mouse_pos.x < 0) && selected_piece != nil
{
starting_pos := selected_piece.position_on_board
starting_square := game.board.squares[starting_pos.x][starting_pos.y]
selected_piece.rect = starting_square.rect
selected_piece = nil
if SHOW_VALID_SQUARES {
clear(&highlighted_squares)
}
}
// highlight valid squares for the selected piece
// and set/deselect the selected_piece
for player_key, player_pieces in pieces_on_board {
for piece_key, &piece in player_pieces {
if rl.CheckCollisionPointRec(mouse_pos, piece.rect) {
if rl.IsMouseButtonPressed(rl.MouseButton.LEFT) {
if selected_piece != nil && piece.type == selected_piece.type && piece.player == selected_piece.player {
selected_piece = nil
if SHOW_VALID_SQUARES {
clear(&highlighted_squares)
}
} else {
selected_piece = &piece
if SHOW_VALID_SQUARES {
highlighted_squares = valid_moves(game, &piece)
}
}
}
}
}
}
// Check which square the mouse is hovering
for row, row_idx in game.board.squares {
for square, square_idx in row {
is_collision := rl.CheckCollisionPointRec(mouse_pos, square.rect)
if is_collision {
hovered_square_vec = {row_idx, square_idx}
}
if selected_piece != nil {
for player_key, player_pieces in pieces_on_board {
for piece_key, &piece in player_pieces {
if rl.CheckCollisionPointRec(mouse_pos, square.rect) && selected_piece == &piece {
if rl.IsMouseButtonDown(rl.MouseButton.LEFT) {
piece.rect.x = mouse_pos.x - 50
piece.rect.y = mouse_pos.y - 50
}
if rl.IsMouseButtonReleased(rl.MouseButton.LEFT) {
square_to_move_to := game.board.squares[hovered_square_vec.x][hovered_square_vec.y]
moved := move_piece(game, &piece, square_to_move_to)
if moved {
rl.PlaySound(move_sound)
current_move += 1
}
// hovered over a wrong square and could not make a move
// so snap back to where the move started
if !moved {
starting_pos := piece.position_on_board
starting_square := game.board.squares[starting_pos.x][starting_pos.y]
piece.rect = starting_square.rect
}
selected_piece = nil
if SHOW_VALID_SQUARES {
clear(&highlighted_squares)
}
}
}
}
}
}
}
}
if is_reset_pressed {
selected_piece = nil
current_move = 0
clear(&game.board_history)
reset_game(game)
}
// @todo: what to do when observing past moves and then making
// a move while not in the latest state of the board
//
// #1 reset to latest state and do not allow the move
if is_next_move_pressed {
fmt.println("--------start-------")
for state in game.board_history {
for piece in state {
fmt.println(piece.position_on_board, piece.type, piece.player)
}
fmt.println("---------------")
}
fmt.println("-------end--------")
/*if current_move < len(move_history) - 1 {
current_move += 1
}
fmt.println(current_move)
fmt.println(len(move_history))
game.board.pieces = move_history[current_move]*/
}
/*if is_prev_move_pressed {
if current_move > 0 {
current_move -= 1
}
fmt.println(current_move)
fmt.println(move_history)
game.board.pieces = move_history[current_move]
}*/
}
@(private = "file")
draw_init :: proc(game: Game) {
draw_board(game.board)
draw_current_active_square_coordinates(game.board)
is_reset_pressed = rl.GuiButton(rl.Rectangle{900, 10, 50, 20}, "Reset")
is_prev_move_pressed = rl.GuiButton(rl.Rectangle{850, 50, 50, 20}, "Prev")
is_next_move_pressed = rl.GuiButton(rl.Rectangle{910, 50, 50, 20}, "Next")
/*
The order we render pieces is important
because if white pieces are rendered first
then they always appear behing the black pieces when
making a 'taking' move.
So when current selected piece is black then we
draw white pieces first, If white then black first.
This also means that pieces can not be added to the list
in a random order: first add all white then all black
pieces. !IMPORTANT: do not mix and match them
*/
if selected_piece != nil && selected_piece.player == Player.BLACK {
for _, piece in game.board.pieces_map[.WHITE] {
draw_piece(piece)
}
for _, piece in game.board.pieces_map[.BLACK] {
draw_piece(piece)
}
} else {
for _, piece in game.board.pieces_map[.BLACK] {
draw_piece(piece)
}
for _, piece in game.board.pieces_map[.WHITE] {
draw_piece(piece)
}
}
}
@(private = "file")
draw_piece :: proc(piece: Piece) {
rl.DrawRectangleRec(piece.rect, rl.Fade(rl.WHITE, 0))
rl.DrawTexture(
piece.texture,
i32(piece.rect.x),
i32(piece.rect.y),
rl.WHITE
)
}
@(private = "file")
draw_board :: proc(board: Board) {
for row, row_idx in board.squares {
for square, square_idx in row {
// @todo: draw coordinates on the board
rl.DrawRectangleRec(square.rect, square.color)
if hovered_square_vec.x == row_idx && hovered_square_vec.y == square_idx {
rl.DrawRectangleRec(square.rect, rl.Fade(rl.PURPLE, .5))
}
for highlighted_square in highlighted_squares {
if highlighted_square.row == square.row && highlighted_square.col == square.col {
rl.DrawRectangleRec(square.rect, rl.Fade(rl.ORANGE, .5))
}
}
}
}
}
@(private = "file")
draw_current_active_square_coordinates :: proc(board: Board) {
rows := ROWS
columns := COLUMNS
coord := board.squares[hovered_square_vec.x][hovered_square_vec.y]
coordinate_string := fmt.aprintf("{0}{1} (idx: {2}{3})",
rows[coord.row],
columns[coord.col],
coord.row,
coord.col
)
defer delete(coordinate_string)
coord_cstring := strings.clone_to_cstring(coordinate_string)
defer delete(coord_cstring)
rl.DrawText(coord_cstring, 0 ,0 , 20, rl.BLACK)
}