Skip to content

Commit 7a31124

Browse files
committed
Added more examples.
1 parent 339d4bb commit 7a31124

File tree

13 files changed

+370
-26
lines changed

13 files changed

+370
-26
lines changed

Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ $(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 collision cubes flags font geometric julia keys log map \
25-
plane shapes truck window
24+
examples: bunny camera camera3d castle collision cubes explosion flags font geometric julia keys \
25+
log map maze plane shapes truck window
26+
27+
bunny: $(TARGET) examples/bunny.f90
28+
$(FC) $(FFLAGS) $(LDFLAGS) -o bunny examples/bunny.f90 $(TARGET) $(LDLIBS)
2629

2730
camera: $(TARGET) examples/camera.f90
2831
$(FC) $(FFLAGS) $(LDFLAGS) -o camera examples/camera.f90 $(TARGET) $(LDLIBS)
@@ -39,6 +42,9 @@ collision: $(TARGET) examples/collision.f90
3942
cubes: $(TARGET) examples/cubes.f90
4043
$(FC) $(FFLAGS) $(LDFLAGS) -o cubes examples/cubes.f90 $(TARGET) $(LDLIBS)
4144

45+
explosion: $(TARGET) examples/explosion.f90
46+
$(FC) $(FFLAGS) $(LDFLAGS) -o explosion examples/explosion.f90 $(TARGET) $(LDLIBS)
47+
4248
flags: $(TARGET) examples/flags.f90
4349
$(FC) $(FFLAGS) $(LDFLAGS) -o flags examples/flags.f90 $(TARGET) $(LDLIBS)
4450

@@ -60,6 +66,9 @@ log: $(TARGET) examples/log.f90
6066
map: $(TARGET) examples/map.f90
6167
$(FC) $(FFLAGS) $(LDFLAGS) -o map examples/map.f90 $(TARGET) $(LDLIBS)
6268

69+
maze: $(TARGET) examples/maze.f90
70+
$(FC) $(FFLAGS) $(LDFLAGS) -o maze examples/maze.f90 $(TARGET) $(LDLIBS)
71+
6372
plane: $(TARGET) examples/plane.f90
6473
$(FC) $(FFLAGS) $(LDFLAGS) -o plane examples/plane.f90 $(TARGET) $(LDLIBS)
6574

@@ -76,18 +85,21 @@ clean:
7685
if [ `ls -1 *.mod 2>/dev/null | wc -l` -gt 0 ]; then rm *.mod; fi
7786
if [ `ls -1 *.o 2>/dev/null | wc -l` -gt 0 ]; then rm *.o; fi
7887
if [ -e $(TARGET) ]; then rm $(TARGET); fi
88+
if [ -e bunny ]; then rm bunny; fi
7989
if [ -e camera ]; then rm camera; fi
8090
if [ -e camera3d ]; then rm camera3d; fi
8191
if [ -e castle ]; then rm castle; fi
8292
if [ -e collision ]; then rm collision; fi
8393
if [ -e cubes ]; then rm cubes; fi
94+
if [ -e explosion ]; then rm explosion; fi
8495
if [ -e flags ]; then rm flags; fi
8596
if [ -e font ]; then rm font; fi
8697
if [ -e geometric ]; then rm geometric; fi
8798
if [ -e julia ]; then rm julia; fi
8899
if [ -e keys ]; then rm keys; fi
89100
if [ -e log ]; then rm log; fi
90101
if [ -e map ]; then rm map; fi
102+
if [ -e maze ]; then rm maze; fi
91103
if [ -e plane ]; then rm plane; fi
92104
if [ -e shapes ]; then rm shapes; fi
93105
if [ -e truck ]; then rm truck; fi

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,21 @@ $ ./example
9393

9494
More examples can be found in `examples/`:
9595

96+
* **bunny** is a bunny benchmark.
9697
* **camera** renders a cube in 3-D.
9798
* **camera3d** renders a 3-D scene, with keyboard and mouse controls.
9899
* **castle** renders a 3-D model loaded from file.
99100
* **collision** shows 2-D collision detection.
100101
* **cubes** renders waving cubes.
102+
* **explosion** renders sprites and plays sound.
101103
* **flags** shows window flags.
102104
* **geometric** renders basic geometric shapes.
103105
* **font** displays text using bitmap fonts.
104-
* **julia** renders animated Julia set (GLSL).
106+
* **julia** renders animated Julia set (via GLSL shader).
105107
* **keys** demonstrates keyboard input.
106108
* **log** adds a custom logging routine.
107109
* **map** renders a height map.
110+
* **maze** renders a 3-D maze in first-person view.
108111
* **plane** demonstrates pitch/yaw/roll of a 3-D model.
109112
* **shapes** renders basic shapes.
110113
* **truck** rotates a 3-D model loaded from file.

examples/bunny.f90

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
! bunny.f90
2+
!
3+
! Example program that renders thousands of textures (bunny benchmark). Based
4+
! on the raylib example `textures_bunnymark.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+
integer, parameter :: MAX_BUNNIES = 50000
16+
17+
type :: bunny_type
18+
type(vector2_type) :: position
19+
type(vector2_type) :: speed
20+
type(color_type) :: color
21+
end type bunny_type
22+
23+
character(len=32) :: str
24+
integer :: i, x, y
25+
integer :: nbunnies
26+
type(texture2d_type) :: texture
27+
28+
type(bunny_type), allocatable :: bunnies(:)
29+
30+
call init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'Fortran + raylib' // c_null_char)
31+
call set_target_fps(60)
32+
33+
texture = load_texture('share/wabbit.png' // c_null_char)
34+
35+
allocate (bunnies(MAX_BUNNIES))
36+
nbunnies = 0
37+
38+
do while (.not. window_should_close())
39+
if (is_mouse_button_down(MOUSE_BUTTON_LEFT)) then
40+
! Create more bunnies.
41+
do i = 1, 100
42+
if (nbunnies > MAX_BUNNIES) exit
43+
nbunnies = nbunnies + 1
44+
45+
bunnies(nbunnies)%position = get_mouse_position()
46+
bunnies(nbunnies)%speed%x = get_random_value(-250, 250) / 60.0
47+
bunnies(nbunnies)%speed%y = get_random_value(-250, 250) / 60.0
48+
bunnies(nbunnies)%color = color_type(r = int(get_random_value( 50, 240), c_unsigned_char), &
49+
g = int(get_random_value( 80, 240), c_unsigned_char), &
50+
b = int(get_random_value(100, 240), c_unsigned_char), &
51+
a = int(255, c_unsigned_char))
52+
end do
53+
end if
54+
55+
! Update bunnies.
56+
do concurrent (i = 1:nbunnies)
57+
bunnies(i)%position%x = bunnies(i)%position%x + bunnies(i)%speed%x
58+
bunnies(i)%position%y = bunnies(i)%position%y + bunnies(i)%speed%y
59+
60+
x = bunnies(i)%position%x + texture%width / 2
61+
y = bunnies(i)%position%y + texture%height / 2
62+
63+
if (x > SCREEN_WIDTH .or. x < 0) bunnies(i)%speed%x = -1 * bunnies(i)%speed%x
64+
if (y > SCREEN_HEIGHT .or. y - 40 < 0) bunnies(i)%speed%y = -1 * bunnies(i)%speed%x
65+
end do
66+
67+
call begin_drawing()
68+
call clear_background(RAYWHITE)
69+
70+
! Render bunnies.
71+
do i = 1, nbunnies
72+
x = int(bunnies(i)%position%x)
73+
y = int(bunnies(i)%position%y)
74+
call draw_texture(texture, x, y, bunnies(i)%color)
75+
end do
76+
77+
write (str, '("bunnies: ", i0)') nbunnies
78+
call draw_rectangle(0, 0, SCREEN_WIDTH, 40, BLACK)
79+
call draw_text(trim(str) // c_null_char, 120, 10, 20, GREEN)
80+
call draw_fps(10, 10)
81+
call end_drawing()
82+
end do
83+
84+
deallocate (bunnies)
85+
call unload_texture(texture)
86+
call close_window()
87+
end program main

examples/collision.f90

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ program main
2222
call set_target_fps(60)
2323

2424
box_a = rectangle_type(10.0, &
25-
get_screen_height() / 2.0 - 50.0, &
25+
SCREEN_HEIGHT / 2.0 - 50.0, &
2626
200.0, &
2727
100.0)
28-
box_b = rectangle_type(get_screen_width() / 2.0 - 30.0, &
29-
get_screen_height() / 2.0 - 30.0, &
28+
box_b = rectangle_type(SCREEN_WIDTH / 2.0 - 30.0, &
29+
SCREEN_HEIGHT / 2.0 - 30.0, &
3030
60.0, &
3131
60.0)
3232
speed = 4
@@ -38,20 +38,20 @@ program main
3838
do while (.not. window_should_close())
3939
if (.not. pause) box_a%x = box_a%x + speed
4040

41-
if ((box_a%x + box_a%width >= get_screen_width()) .or. (box_a%x <= 0)) &
41+
if ((box_a%x + box_a%width >= SCREEN_WIDTH) .or. (box_a%x <= 0)) &
4242
speed = -1 * speed
4343

4444
box_b%x = get_mouse_x() - box_b%width / 2.0
4545
box_b%y = get_mouse_y() - box_b%height / 2.0
4646

47-
if (box_b%x + box_b%width >= get_screen_width()) then
48-
box_b%x = get_screen_width() - box_b%width
47+
if (box_b%x + box_b%width >= SCREEN_WIDTH) then
48+
box_b%x = SCREEN_WIDTH - box_b%width
4949
else if (box_b%x <= 0) then
5050
box_b%x = 0.0
5151
end if
5252

53-
if (box_b%y + box_b%height >= get_screen_height()) then
54-
box_b%y = get_screen_height() - box_b%height
53+
if (box_b%y + box_b%height >= SCREEN_HEIGHT) then
54+
box_b%y = SCREEN_HEIGHT - box_b%height
5555
else if (box_b%y <= limit) then
5656
box_b%y = real(limit)
5757
end if
@@ -65,9 +65,9 @@ program main
6565
call clear_background(RAYWHITE)
6666

6767
if (collision) then
68-
call draw_rectangle(0, 0, screen_width, limit, RED)
68+
call draw_rectangle(0, 0, SCREEN_WIDTH, limit, RED)
6969
else
70-
call draw_rectangle(0, 0, screen_width, limit, BLACK)
70+
call draw_rectangle(0, 0, SCREEN_WIDTH, limit, BLACK)
7171
end if
7272

7373
call draw_rectangle_rec(box_a, GOLD)
@@ -76,11 +76,11 @@ program main
7676
if (collision) then
7777
call draw_rectangle_rec(box_c, LIME)
7878
call draw_text('COLLISION!' // c_null_char, &
79-
get_screen_width() / 2 - measure_text('COLLISION!' // c_null_char, 20) / 2, &
79+
SCREEN_WIDTH / 2 - measure_text('COLLISION!' // c_null_char, 20) / 2, &
8080
limit / 2 - 10, 20, BLACK)
8181

8282
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, &
83+
call draw_text(trim(str) // c_null_char, SCREEN_WIDTH / 2 - 100, &
8484
limit + 10, 20, BLACK)
8585
end if
8686

examples/explosion.f90

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
! explosion.f90
2+
!
3+
! Example program that renders a sprite explosion. Based on the raylib example
4+
! `textures_sprite_explosion.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+
integer, parameter :: NCOLS = 5
17+
integer, parameter :: NROWS = 5
18+
19+
integer :: frame, col, row
20+
logical :: active
21+
real :: width, height
22+
type(sound_type) :: boom
23+
type(rectangle_type) :: rectangle
24+
type(texture2d_type) :: explosion
25+
type(vector2_type) :: position
26+
27+
call init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'Fortran + raylib' // c_null_char)
28+
call init_audio_device()
29+
30+
call set_target_fps(120)
31+
32+
boom = load_sound('share/boom.wav' // c_null_char)
33+
explosion = load_texture('share/explosion.png' // c_null_char)
34+
35+
width = real(explosion%width / NCOLS)
36+
height = real(explosion%height / NROWS)
37+
38+
col = 0
39+
row = 0
40+
41+
rectangle = rectangle_type(0.0, 0.0, width, height)
42+
43+
active = .false.
44+
frame = 0
45+
46+
do while (.not. window_should_close())
47+
if (is_mouse_button_pressed(MOUSE_BUTTON_LEFT) .and. .not. active) then
48+
active = .true.
49+
call play_sound(boom)
50+
51+
position = get_mouse_position()
52+
position%x = position%x - (width / 2.0)
53+
position%y = position%y - (height / 2.0)
54+
end if
55+
56+
if (active) then
57+
frame = frame + 1
58+
59+
if (frame > 2) then
60+
col = col + 1
61+
62+
if (col >= NCOLS) then
63+
col = 0
64+
row = row + 1
65+
66+
if (row >= NROWS) then
67+
row = 0
68+
active = .false.
69+
end if
70+
end if
71+
72+
frame = 0
73+
end if
74+
end if
75+
76+
rectangle%x = width * col
77+
rectangle%y = height * row
78+
79+
call begin_drawing()
80+
call clear_background(RAYWHITE)
81+
82+
if (active) then
83+
call draw_texture_rec(explosion, rectangle, position, WHITE)
84+
end if
85+
call end_drawing()
86+
end do
87+
88+
call unload_texture(explosion)
89+
call unload_sound(boom)
90+
91+
call close_audio_device()
92+
call close_window()
93+
end program main

examples/map.f90

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ program main
2020
type(texture2d_type) :: texture
2121
type(vector3_type) :: position
2222

23-
type(material_type), pointer :: material_ptrs(:)
24-
type(material_map_type), pointer :: material_map_ptrs(:)
25-
2623
call init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'Fortran + raylib' // c_null_char)
2724
call set_target_fps(60)
2825

@@ -38,16 +35,11 @@ program main
3835
mesh = gen_mesh_heightmap(image, vector3_type(16.0, 8.0, 16.0))
3936
model = load_model_from_mesh(mesh)
4037

41-
! We have to add 1 to the array indices as a work-around, as we can't set
42-
! the lower bounds of the pointer arrays with `c_f_pointer()`.
43-
call c_f_pointer(model%materials, material_ptrs, [ model%material_count ])
44-
call c_f_pointer(material_ptrs(1)%maps, material_map_ptrs, [ MATERIAL_MAP_BRDF + 1 ])
45-
material_map_ptrs(MATERIAL_MAP_DIFFUSE + 1)%texture = texture
38+
call set_model_diffuse(model, texture)
39+
call unload_image(image)
4640

4741
position = vector3_type(-8.0, 0.0, -8.0)
4842

49-
call unload_image(image)
50-
5143
do while (.not. window_should_close())
5244
call update_camera(camera, CAMERA_ORBITAL)
5345

@@ -71,4 +63,18 @@ program main
7163
call unload_model(model)
7264

7365
call close_window()
66+
contains
67+
subroutine set_model_diffuse(model, texture)
68+
type(model_type), intent(inout) :: model
69+
type(texture2d_type), intent(inout) :: texture
70+
71+
type(material_type), pointer :: material_ptrs(:)
72+
type(material_map_type), pointer :: material_map_ptrs(:)
73+
74+
! We have to add 1 to the array indices as a work-around, as we can't set
75+
! the lower bounds of the pointer arrays with `c_f_pointer()`.
76+
call c_f_pointer(model%materials, material_ptrs, [ model%material_count ])
77+
call c_f_pointer(material_ptrs(1)%maps, material_map_ptrs, [ MATERIAL_MAP_BRDF + 1 ])
78+
material_map_ptrs(MATERIAL_MAP_DIFFUSE + 1)%texture = texture
79+
end subroutine set_model_diffuse
7480
end program main

0 commit comments

Comments
 (0)