| 1 |
! Measure units: |
| 2 |
! - *_px - pixels |
| 3 |
! - *_cl - cells |
| 4 |
! - *_rl - relative (fraction of width, height, cell size, etc) |
| 5 |
|
| 6 |
program main |
| 7 |
use iso_c_binding, only: c_int, c_int32_t, C_NULL_CHAR, C_NULL_PTR, c_loc |
| 8 |
use raylib |
| 9 |
use raymath |
| 10 |
use game |
| 11 |
use ai |
| 12 |
use ui |
| 13 |
implicit none |
| 14 |
|
| 15 |
type :: Particle |
| 16 |
real, dimension(2) :: position, velocity |
| 17 |
integer(c_int32_t) :: color |
| 18 |
real :: size, lt_sec, lt_t |
| 19 |
end type Particle |
| 20 |
|
| 21 |
integer, parameter :: font_size = 128 |
| 22 |
real, parameter :: particle_min_mag = 50.0 |
| 23 |
real, parameter :: particle_max_mag = 400.0 |
| 24 |
real, parameter :: particle_min_size = 2.0 |
| 25 |
real, parameter :: particle_max_size = 5.0 |
| 26 |
real, parameter :: particle_min_lt = 0.5 |
| 27 |
real, parameter :: particle_max_lt = 0.8 |
| 28 |
integer, parameter :: particles_burst_count = 100 |
| 29 |
|
| 30 |
real :: dt |
| 31 |
real :: board_x_px, board_y_px, board_boundary_width, board_boundary_height, board_size_px, cell_size_px |
| 32 |
|
| 33 |
integer,dimension(board_size_cl, board_size_cl) :: board |
| 34 |
|
| 35 |
integer :: current_player |
| 36 |
type(TLine) :: final_line |
| 37 |
integer :: state |
| 38 |
type(Font) :: game_font |
| 39 |
type(Particle) :: particles(particles_burst_count*board_size_cl*board_size_cl + 10) |
| 40 |
type(Sound) :: click_sound |
| 41 |
logical :: click_played_on_previous_frame, click_played_on_this_frame |
| 42 |
|
| 43 |
logical, dimension(2) :: ai_checkboxes |
| 44 |
integer :: i |
| 45 |
|
| 46 |
enum, bind(C) |
| 47 |
enumerator :: STATE_GAME = 0 |
| 48 |
enumerator :: STATE_WON |
| 49 |
enumerator :: STATE_TIE |
| 50 |
end enum |
| 51 |
|
| 52 |
ai_checkboxes(CELL_CROSS) = .false. |
| 53 |
ai_checkboxes(CELL_KNOTT) = .true. |
| 54 |
do i=1,size(particles) |
| 55 |
particles(i)%lt_t = 0.0 |
| 56 |
end do |
| 57 |
|
| 58 |
call restart_game() |
| 59 |
|
| 60 |
call set_config_flags(FLAG_WINDOW_RESIZABLE) |
| 61 |
! TODO: draw_rectangle_rounded, draw_circle_v, etc (basically anything that renders circles) has rendering artifacts that make some of the pixels of the background visible when FLAG_MSAA_4X_HINT is enabled |
| 62 |
! This could be a bug of Raylib. The implementations may not be taking MSAA into account. |
| 63 |
call set_config_flags(FLAG_MSAA_4X_HINT) |
| 64 |
call init_window(16*80, 9*80, "Fortran GOTY"//C_NULL_CHAR) |
| 65 |
call init_audio_device() |
| 66 |
call set_target_fps(60) |
| 67 |
|
| 68 |
! TODO: set the working directory to where the executable is located |
| 69 |
! This is needed to be able to locate the assets properly |
| 70 |
game_font = load_font_ex("./fonts/Alegreya-Regular.ttf"//C_NULL_CHAR, font_size, C_NULL_PTR, 0) |
| 71 |
call set_texture_filter(game_font%texture, TEXTURE_FILTER_BILINEAR) |
| 72 |
click_sound = load_sound("./sounds/misc_26_fixed.ogg"//C_NULL_CHAR) |
| 73 |
|
| 74 |
do while (.not. window_should_close()) |
| 75 |
click_played_on_previous_frame = click_played_on_this_frame |
| 76 |
click_played_on_this_frame = .false. |
| 77 |
call begin_drawing() |
| 78 |
call begin_screen_fitting() |
| 79 |
call clear_background(background_color) |
| 80 |
|
| 81 |
dt = get_frame_time() |
| 82 |
board_boundary_width = screen_width_px*2/3 |
| 83 |
board_boundary_height = screen_height_px |
| 84 |
|
| 85 |
if (board_boundary_width > board_boundary_height) then |
| 86 |
board_size_px = board_boundary_height |
| 87 |
board_x_px = real(board_boundary_width)/2 - board_size_px/2 |
| 88 |
board_y_px = 0 |
| 89 |
else |
| 90 |
board_size_px = board_boundary_width |
| 91 |
board_x_px = 0 |
| 92 |
board_y_px = real(board_boundary_height)/2 - board_size_px/2 |
| 93 |
end if |
| 94 |
|
| 95 |
board_x_px = board_x_px + board_size_px*board_margin_rl |
| 96 |
board_y_px = board_y_px + board_size_px*board_margin_rl |
| 97 |
board_size_px = board_size_px - board_size_px*board_margin_rl*2 |
| 98 |
|
| 99 |
cell_size_px = board_size_px/board_size_cl |
| 100 |
|
| 101 |
select case (state) |
| 102 |
case (STATE_GAME) |
| 103 |
call render_game_state() |
| 104 |
case (STATE_WON) |
| 105 |
call render_won_state() |
| 106 |
case (STATE_TIE) |
| 107 |
call render_tie_state() |
| 108 |
end select |
| 109 |
|
| 110 |
call render_ai_checkboxes(rectangle( & |
| 111 |
board_boundary_width, & |
| 112 |
0, & |
| 113 |
screen_width_px - board_boundary_width, & |
| 114 |
board_boundary_height)) |
| 115 |
call end_screen_fitting() |
| 116 |
call end_drawing() |
| 117 |
end do |
| 118 |
|
| 119 |
contains |
| 120 |
pure function lerp(a, b, t) result(c) |
| 121 |
real, intent(in) :: a, b, t |
| 122 |
real :: c |
| 123 |
c = a + (b - a)*t |
| 124 |
end function lerp |
| 125 |
|
| 126 |
subroutine spawn_random_particles_along_line(start, end, count, color) |
| 127 |
real, dimension(2), intent(in) :: start, end |
| 128 |
integer, intent(in) :: count |
| 129 |
integer(c_int32_t), intent(in) :: color |
| 130 |
|
| 131 |
real, dimension(2) :: position |
| 132 |
real :: t |
| 133 |
integer :: i |
| 134 |
|
| 135 |
do i=1,count |
| 136 |
call random_number(t) |
| 137 |
position = start + (end - start)*t |
| 138 |
call spawn_random_particle_at(position, color) |
| 139 |
end do |
| 140 |
end subroutine spawn_random_particles_along_line |
| 141 |
|
| 142 |
subroutine spawn_random_particles_in_region(region, count, color) |
| 143 |
type(Rectangle),intent(in) :: region |
| 144 |
integer,intent(in) :: count |
| 145 |
integer(c_int32_t),intent(in) :: color |
| 146 |
|
| 147 |
real, dimension(2) :: position, t |
| 148 |
integer :: i |
| 149 |
|
| 150 |
do i=1,count |
| 151 |
call random_number(t) |
| 152 |
position = [region%x, region%y] + [region%width, region%height]*t |
| 153 |
call spawn_random_particle_at(position, color) |
| 154 |
end do |
| 155 |
end subroutine spawn_random_particles_in_region |
| 156 |
|
| 157 |
subroutine spawn_random_particle_at(position,color) |
| 158 |
real, dimension(2), intent(in) :: position |
| 159 |
integer(c_int32_t), intent(in) :: color |
| 160 |
|
| 161 |
type(Particle) :: p |
| 162 |
real :: angle, mag, t |
| 163 |
real, parameter :: pi = 4.D0*DATAN(1.D0) |
| 164 |
|
| 165 |
p%position = position |
| 166 |
|
| 167 |
call random_number(t) |
| 168 |
angle = 2.0*pi*t |
| 169 |
|
| 170 |
call random_number(t) |
| 171 |
mag = lerp(particle_min_mag, particle_max_mag, t) |
| 172 |
p%velocity = [cos(angle), sin(angle)]*mag |
| 173 |
|
| 174 |
p%color = color |
| 175 |
|
| 176 |
call random_number(t) |
| 177 |
p%size = lerp(particle_min_size, particle_max_size, t) |
| 178 |
|
| 179 |
call random_number(t) |
| 180 |
p%lt_sec = lerp(particle_min_lt, particle_max_lt, t) |
| 181 |
p%lt_t = 1.0 |
| 182 |
|
| 183 |
call spawn_particle(p) |
| 184 |
end subroutine spawn_random_particle_at |
| 185 |
|
| 186 |
subroutine spawn_particle(p) |
| 187 |
type(Particle),intent(in) :: p |
| 188 |
type(Particle) :: pi |
| 189 |
integer :: i |
| 190 |
|
| 191 |
do i=1,size(particles) |
| 192 |
pi = particles(i) |
| 193 |
if (pi%lt_t <= 0.0) then |
| 194 |
particles(i) = p |
| 195 |
return |
| 196 |
end if |
| 197 |
end do |
| 198 |
end subroutine spawn_particle |
| 199 |
|
| 200 |
subroutine render_particles(dt) |
| 201 |
real,intent(in) :: dt |
| 202 |
type(Particle) :: p |
| 203 |
integer :: i |
| 204 |
|
| 205 |
do i=1,size(particles) |
| 206 |
p = particles(i) |
| 207 |
if (p%lt_t > 0.0) then |
| 208 |
particles(i)%velocity = p%velocity*0.98 |
| 209 |
particles(i)%position = p%position + particles(i)%velocity*dt |
| 210 |
particles(i)%lt_t = (p%lt_t*p%lt_sec - dt)/p%lt_sec |
| 211 |
call draw_circle_v(Vector2(p%position), p%size, color_alpha(p%color, p%lt_t)) |
| 212 |
end if |
| 213 |
end do |
| 214 |
end subroutine render_particles |
| 215 |
|
| 216 |
subroutine render_ai_checkboxes(boundary) |
| 217 |
real,parameter :: checkbox_width_rl = 0.45 |
| 218 |
real,parameter :: checkbox_height_rl = 0.10 |
| 219 |
real,parameter :: checkbox_padding_rl = 0.05 |
| 220 |
type(Rectangle),intent(in) :: boundary |
| 221 |
|
| 222 |
type(Rectangle) :: cross_boundary, knott_boundary |
| 223 |
real :: checkbox_height_px, checkbox_padding_px |
| 224 |
type(Vector2) :: text_pos, text_size |
| 225 |
|
| 226 |
checkbox_height_px = boundary%height*checkbox_height_rl |
| 227 |
checkbox_padding_px = boundary%height*checkbox_padding_rl |
| 228 |
|
| 229 |
cross_boundary%width = boundary%width*checkbox_width_rl |
| 230 |
cross_boundary%height = checkbox_height_px |
| 231 |
cross_boundary%x = boundary%x + boundary%width/2 - cross_boundary%width/2 |
| 232 |
cross_boundary%y = boundary%y + boundary%height/2 - checkbox_height_px - checkbox_padding_px*0.5 |
| 233 |
|
| 234 |
knott_boundary%width = boundary%width*checkbox_width_rl |
| 235 |
knott_boundary%height = checkbox_height_px |
| 236 |
knott_boundary%x = boundary%x + boundary%width/2 - knott_boundary%width/2 |
| 237 |
knott_boundary%y = boundary%y + boundary%height/2 + checkbox_padding_px*0.5 |
| 238 |
|
| 239 |
text_size = measure_text_ex(game_font, "AI"//C_NULL_CHAR, checkbox_height_px, 0.0) |
| 240 |
text_pos = Vector2( & |
| 241 |
[cross_boundary%x, & |
| 242 |
! cross_boundary%x + cross_boundary%width/2 - text_size%x/2, & |
| 243 |
cross_boundary%y - checkbox_height_px - checkbox_padding_px]) |
| 244 |
call draw_text_ex(game_font, "AI"//C_NULL_CHAR, text_pos,checkbox_height_px, 0.0, restart_button_color) |
| 245 |
|
| 246 |
call checkbox(cross_checkbox_id,CELL_CROSS,cross_boundary,ai_checkboxes(CELL_CROSS)) |
| 247 |
call checkbox(knott_checkbox_id,CELL_KNOTT,knott_boundary,ai_checkboxes(CELL_KNOTT)) |
| 248 |
end subroutine render_ai_checkboxes |
| 249 |
|
| 250 |
subroutine render_tie_state() |
| 251 |
call render_board(board_x_px, board_y_px, board_size_px, board) |
| 252 |
call render_particles(dt) |
| 253 |
|
| 254 |
if (restart_button(game_font, board_x_px, board_y_px, board_size_px)) then |
| 255 |
call restart_game() |
| 256 |
end if |
| 257 |
end subroutine render_tie_state |
| 258 |
|
| 259 |
subroutine render_won_state() |
| 260 |
call render_board(board_x_px, board_y_px, board_size_px, board) |
| 261 |
call render_particles(dt) |
| 262 |
|
| 263 |
call strikethrough(final_line, Square([board_x_px, board_y_px], board_size_px)) |
| 264 |
if (restart_button(game_font, board_x_px, board_y_px, board_size_px)) then |
| 265 |
call restart_game() |
| 266 |
end if |
| 267 |
end subroutine render_won_state |
| 268 |
|
| 269 |
subroutine render_game_state() |
| 270 |
integer :: x_cl, y_cl |
| 271 |
real :: board_cell_size |
| 272 |
real,dimension(2) :: start, end |
| 273 |
|
| 274 |
board_cell_size = board_size_px/board_size_cl |
| 275 |
|
| 276 |
if (ai_checkboxes(current_player)) then |
| 277 |
call render_board(board_x_px, board_y_px, board_size_px, board) |
| 278 |
|
| 279 |
if (.not. ai_next_move(board, current_player, x_cl, y_cl)) then |
| 280 |
board(x_cl, y_cl) = current_player |
| 281 |
if (.not. click_played_on_previous_frame) then |
| 282 |
call play_sound(click_sound) |
| 283 |
click_played_on_this_frame = .true. |
| 284 |
end if |
| 285 |
if (player_won(board, CELL_CROSS, [x_cl, y_cl], final_line)) then |
| 286 |
state = STATE_WON |
| 287 |
call map_tline_on_screen(final_line, Square([board_x_px, board_y_px], board_size_px), start, end) |
| 288 |
call spawn_random_particles_along_line(start, end, particles_burst_count*3, strikethrough_color) |
| 289 |
return |
| 290 |
end if |
| 291 |
if (player_won(board, CELL_KNOTT, [x_cl, y_cl], final_line)) then |
| 292 |
state = STATE_WON |
| 293 |
call map_tline_on_screen(final_line, Square([board_x_px, board_y_px], board_size_px), start, end) |
| 294 |
call spawn_random_particles_along_line(start, end, particles_burst_count*3, strikethrough_color) |
| 295 |
return |
| 296 |
end if |
| 297 |
call spawn_random_particles_in_region( & |
| 298 |
Rectangle(board_x_px + (x_cl - 1)*board_cell_size, & |
| 299 |
board_y_px + (y_cl - 1)*board_cell_size, & |
| 300 |
board_cell_size, & |
| 301 |
board_cell_size), & |
| 302 |
particles_burst_count, & |
| 303 |
shape_colors(current_player)) |
| 304 |
current_player = 3 - current_player |
| 305 |
if (board_full(board)) then |
| 306 |
state = STATE_TIE |
| 307 |
return |
| 308 |
end if |
| 309 |
else |
| 310 |
state = STATE_TIE |
| 311 |
return |
| 312 |
end if |
| 313 |
else |
| 314 |
if (render_board_clickable(board_x_px, board_y_px, board_size_px, board, x_cl, y_cl)) then |
| 315 |
board(x_cl, y_cl) = current_player |
| 316 |
if (.not. click_played_on_previous_frame) then |
| 317 |
call play_sound(click_sound) |
| 318 |
click_played_on_this_frame = .true. |
| 319 |
end if |
| 320 |
if (player_won(board, CELL_CROSS, [x_cl, y_cl], final_line)) then |
| 321 |
state = STATE_WON |
| 322 |
call map_tline_on_screen(final_line, Square([board_x_px, board_y_px], board_size_px), start, end) |
| 323 |
call spawn_random_particles_along_line(start, end, particles_burst_count*3, strikethrough_color) |
| 324 |
return |
| 325 |
end if |
| 326 |
if (player_won(board, CELL_KNOTT, [x_cl, y_cl], final_line)) then |
| 327 |
state = STATE_WON |
| 328 |
call map_tline_on_screen(final_line, Square([board_x_px, board_y_px], board_size_px), start, end) |
| 329 |
call spawn_random_particles_along_line(start, end, particles_burst_count*3, strikethrough_color) |
| 330 |
return |
| 331 |
end if |
| 332 |
call spawn_random_particles_in_region( & |
| 333 |
Rectangle(board_x_px + (x_cl - 1)*board_cell_size, & |
| 334 |
board_y_px + (y_cl - 1)*board_cell_size, & |
| 335 |
board_cell_size, & |
| 336 |
board_cell_size), & |
| 337 |
particles_burst_count, & |
| 338 |
shape_colors(current_player)) |
| 339 |
current_player = 3 - current_player |
| 340 |
if (board_full(board)) then |
| 341 |
state = STATE_TIE |
| 342 |
return |
| 343 |
end if |
| 344 |
end if |
| 345 |
end if |
| 346 |
call render_particles(dt) |
| 347 |
end subroutine render_game_state |
| 348 |
|
| 349 |
subroutine restart_game() |
| 350 |
board(:,:) = 0 |
| 351 |
state = STATE_GAME |
| 352 |
current_player = CELL_CROSS |
| 353 |
end subroutine restart_game |
| 354 |
end program |
| 355 |
|
| 356 |
! # Roadmap |
| 357 |
! - TODO: customizable board size |
| 358 |
! - TODO: sound volume controls |
| 359 |
! - TODO: accessibility: control via keyboard |