|
| 1 | +! collision.f90 |
| 2 | +! |
| 3 | +! Example program to test collision detection. Based on the raylib example |
| 4 | +! `shapes_collision_area.c`. |
| 5 | +! |
| 6 | +! Author: Philipp Engel |
| 7 | +! Licence: ISC |
| 8 | +program main |
| 9 | + use, intrinsic :: iso_c_binding, only: c_null_char |
| 10 | + use :: raylib |
| 11 | + implicit none (type, external) |
| 12 | + |
| 13 | + integer, parameter :: SCREEN_WIDTH = 800 |
| 14 | + integer, parameter :: SCREEN_HEIGHT = 450 |
| 15 | + |
| 16 | + character(len=32) :: str |
| 17 | + integer :: limit, speed |
| 18 | + logical :: collision, pause |
| 19 | + type(rectangle_type) :: box_a, box_b, box_c |
| 20 | + |
| 21 | + call init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'Fortran + raylib' // c_null_char) |
| 22 | + call set_target_fps(60) |
| 23 | + |
| 24 | + box_a = rectangle_type(10.0, & |
| 25 | + get_screen_height() / 2.0 - 50.0, & |
| 26 | + 200.0, & |
| 27 | + 100.0) |
| 28 | + box_b = rectangle_type(get_screen_width() / 2.0 - 30.0, & |
| 29 | + get_screen_height() / 2.0 - 30.0, & |
| 30 | + 60.0, & |
| 31 | + 60.0) |
| 32 | + speed = 4 |
| 33 | + limit = 40 |
| 34 | + |
| 35 | + pause = .false. |
| 36 | + collision = .false. |
| 37 | + |
| 38 | + do while (.not. window_should_close()) |
| 39 | + if (.not. pause) box_a%x = box_a%x + speed |
| 40 | + |
| 41 | + if ((box_a%x + box_a%width >= get_screen_width()) .or. (box_a%x <= 0)) & |
| 42 | + speed = -1 * speed |
| 43 | + |
| 44 | + box_b%x = get_mouse_x() - box_b%width / 2.0 |
| 45 | + box_b%y = get_mouse_y() - box_b%height / 2.0 |
| 46 | + |
| 47 | + if (box_b%x + box_b%width >= get_screen_width()) then |
| 48 | + box_b%x = get_screen_width() - box_b%width |
| 49 | + else if (box_b%x <= 0) then |
| 50 | + box_b%x = 0.0 |
| 51 | + end if |
| 52 | + |
| 53 | + if (box_b%y + box_b%height >= get_screen_height()) then |
| 54 | + box_b%y = get_screen_height() - box_b%height |
| 55 | + else if (box_b%y <= limit) then |
| 56 | + box_b%y = real(limit) |
| 57 | + end if |
| 58 | + |
| 59 | + collision = check_collision_recs(box_a, box_b) |
| 60 | + if (collision) box_c = get_collision_rec(box_a, box_b) |
| 61 | + |
| 62 | + if (is_key_pressed(KEY_SPACE)) pause = .not. pause |
| 63 | + |
| 64 | + call begin_drawing() |
| 65 | + call clear_background(RAYWHITE) |
| 66 | + |
| 67 | + if (collision) then |
| 68 | + call draw_rectangle(0, 0, screen_width, limit, RED) |
| 69 | + else |
| 70 | + call draw_rectangle(0, 0, screen_width, limit, BLACK) |
| 71 | + end if |
| 72 | + |
| 73 | + call draw_rectangle_rec(box_a, GOLD) |
| 74 | + call draw_rectangle_rec(box_b, BLUE) |
| 75 | + |
| 76 | + if (collision) then |
| 77 | + call draw_rectangle_rec(box_c, LIME) |
| 78 | + call draw_text('COLLISION!' // c_null_char, & |
| 79 | + get_screen_width() / 2 - measure_text('COLLISION!' // c_null_char, 20) / 2, & |
| 80 | + limit / 2 - 10, 20, BLACK) |
| 81 | + |
| 82 | + write (str, '("Collision Area: ", i0)') int(box_c%width) * int(box_c%height) |
| 83 | + call draw_text(trim(str) // c_null_char, get_screen_width() / 2 - 100, & |
| 84 | + limit + 10, 20, BLACK) |
| 85 | + end if |
| 86 | + |
| 87 | + call draw_fps(10, 10) |
| 88 | + call end_drawing() |
| 89 | + end do |
| 90 | + |
| 91 | + call close_window() |
| 92 | +end program main |
0 commit comments