Skip to content

Commit 339d4bb

Browse files
committed
Added examples.
1 parent 423fbb2 commit 339d4bb

File tree

5 files changed

+170
-1
lines changed

5 files changed

+170
-1
lines changed

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $(TARGET): src/raylib.f90 src/raylib_util.f90
2121
$(FC) $(FFLAGS) -c src/raylib_util.f90
2222
$(AR) $(ARFLAGS) $(TARGET) raylib.o raylib_camera.o raylib_math.o raylib_util.o
2323

24-
examples: camera camera3d castle cubes flags font geometric julia keys map \
24+
examples: camera camera3d castle collision cubes flags font geometric julia keys log map \
2525
plane shapes truck window
2626

2727
camera: $(TARGET) examples/camera.f90
@@ -33,6 +33,9 @@ camera3d: $(TARGET) examples/camera3d.f90
3333
castle: $(TARGET) examples/castle.f90
3434
$(FC) $(FFLAGS) $(LDFLAGS) -o castle examples/castle.f90 $(TARGET) $(LDLIBS)
3535

36+
collision: $(TARGET) examples/collision.f90
37+
$(FC) $(FFLAGS) $(LDFLAGS) -o collision examples/collision.f90 $(TARGET) $(LDLIBS)
38+
3639
cubes: $(TARGET) examples/cubes.f90
3740
$(FC) $(FFLAGS) $(LDFLAGS) -o cubes examples/cubes.f90 $(TARGET) $(LDLIBS)
3841

@@ -51,6 +54,9 @@ julia: $(TARGET) examples/julia.f90
5154
keys: $(TARGET) examples/keys.f90
5255
$(FC) $(FFLAGS) $(LDFLAGS) -o keys examples/keys.f90 $(TARGET) $(LDLIBS)
5356

57+
log: $(TARGET) examples/log.f90
58+
$(FC) $(FFLAGS) $(LDFLAGS) -o log examples/log.f90 $(TARGET) $(LDLIBS)
59+
5460
map: $(TARGET) examples/map.f90
5561
$(FC) $(FFLAGS) $(LDFLAGS) -o map examples/map.f90 $(TARGET) $(LDLIBS)
5662

@@ -73,12 +79,14 @@ clean:
7379
if [ -e camera ]; then rm camera; fi
7480
if [ -e camera3d ]; then rm camera3d; fi
7581
if [ -e castle ]; then rm castle; fi
82+
if [ -e collision ]; then rm collision; fi
7683
if [ -e cubes ]; then rm cubes; fi
7784
if [ -e flags ]; then rm flags; fi
7885
if [ -e font ]; then rm font; fi
7986
if [ -e geometric ]; then rm geometric; fi
8087
if [ -e julia ]; then rm julia; fi
8188
if [ -e keys ]; then rm keys; fi
89+
if [ -e log ]; then rm log; fi
8290
if [ -e map ]; then rm map; fi
8391
if [ -e plane ]; then rm plane; fi
8492
if [ -e shapes ]; then rm shapes; fi

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ More examples can be found in `examples/`:
9696
* **camera** renders a cube in 3-D.
9797
* **camera3d** renders a 3-D scene, with keyboard and mouse controls.
9898
* **castle** renders a 3-D model loaded from file.
99+
* **collision** shows 2-D collision detection.
99100
* **cubes** renders waving cubes.
100101
* **flags** shows window flags.
101102
* **geometric** renders basic geometric shapes.
102103
* **font** displays text using bitmap fonts.
103104
* **julia** renders animated Julia set (GLSL).
104105
* **keys** demonstrates keyboard input.
106+
* **log** adds a custom logging routine.
105107
* **map** renders a height map.
106108
* **plane** demonstrates pitch/yaw/roll of a 3-D model.
107109
* **shapes** renders basic shapes.

examples/collision.f90

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

examples/log.f90

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
! log.f90
2+
!
3+
! Example program that adds a custom log routine. Based on the raylib example
4+
! `core_custom_logging.c`.
5+
!
6+
! Author: Philipp Engel
7+
! Licence: ISC
8+
program main
9+
use, intrinsic :: iso_c_binding
10+
use :: raylib
11+
use :: raylib_util
12+
implicit none (type, external)
13+
14+
integer, parameter :: SCREEN_WIDTH = 800
15+
integer, parameter :: SCREEN_HEIGHT = 450
16+
17+
call set_trace_log_callback(c_funloc(custom_log))
18+
call init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'Fortran + raylib' // c_null_char)
19+
call set_target_fps(60)
20+
21+
do while (.not. window_should_close())
22+
call begin_drawing()
23+
call clear_background(RAYWHITE)
24+
call draw_text('Check out the console output to see the custom logger in action!' // c_null_char, &
25+
60, 200, 20, LIGHTGRAY)
26+
call end_drawing()
27+
end do
28+
29+
call close_window()
30+
contains
31+
subroutine custom_log(type, text, args) bind(c)
32+
integer(kind=c_int), intent(in), value :: type
33+
type(c_ptr), intent(in), value :: text
34+
type(c_ptr), intent(in), value :: args
35+
36+
character(len=:), allocatable :: str
37+
integer :: dt(8)
38+
39+
call date_and_time(values=dt)
40+
41+
write (*, '("[", i4, "-", i2.2, "-", i2.2, " ", 2(i2.2, ":"), i2.2, "] ")', advance='no') &
42+
dt(1:3), dt(5:7)
43+
44+
call c_f_str_ptr(text, str)
45+
46+
select case (type)
47+
case (LOG_INFO)
48+
print '("[INFO] ", a)', str
49+
case (LOG_ERROR)
50+
print '("[ERROR] ", a)', str
51+
case (LOG_WARNING)
52+
print '("[WARN] ", a)', str
53+
case (LOG_DEBUG)
54+
print '("[DEBUG] ", a)', str
55+
end select
56+
end subroutine custom_log
57+
end program main

fpm.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ source-dir = "examples"
3131
name = "castle"
3232
main = "castle.f90"
3333

34+
[[executable]]
35+
source-dir = "examples"
36+
name = "collision"
37+
main = "collision.f90"
38+
3439
[[executable]]
3540
source-dir = "examples"
3641
name = "cubes"
@@ -61,6 +66,11 @@ source-dir = "examples"
6166
name = "keys"
6267
main = "keys.f90"
6368

69+
[[executable]]
70+
source-dir = "examples"
71+
name = "log"
72+
main = "log.f90"
73+
6474
[[executable]]
6575
source-dir = "examples"
6676
name = "map"

0 commit comments

Comments
 (0)