raylib-bindings 0.1.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,143 +1,171 @@
1
- <!-- -*- mode:markdown; coding:utf-8; -*- -->
2
-
3
- # Yet another raylib wrapper for Ruby #
4
-
5
- * Created : 2021-10-17
6
- * Last modified : 2022-12-25
7
-
8
- Provides Ruby bindings for raylib-related libraries including:
9
-
10
- * [raylib](https://github.com/raysan5/raylib)
11
- * raylib
12
- * raymath
13
- * rlgl
14
- * [raygui](https://github.com/raysan5/raygui)
15
- * [Physac](https://github.com/raysan5/physac)
16
-
17
- ## Features ##
18
-
19
- * Generated semi-automatically
20
- * Based on Ruby/FFI
21
- * No need to build C extension library
22
-
23
- ## Quick Start ##
24
-
25
- For Windows and macOS users:
26
-
27
- ```
28
- D:\> gem install raylib-bindings
29
- Fetching raylib-bindings-...
30
- ...
31
- 1 gem installed
32
-
33
- D:\> ruby -r raylib -e 'Raylib.template'
34
- [Info] Raylib.template : C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/raylib-bindings-0.0.11/examples/template.rb => d://template.rb
35
- [Info] Raylib.template : Done
36
-
37
- D:\> ruby template.rb
38
- ```
39
-
40
-
41
- <img src="https://raw.githubusercontent.com/vaiorabbit/raylib-bindings/main/doc/template_screenshot.png" width="400">
42
-
43
- ## Prerequisites ##
44
-
45
- * Ruby interpreter
46
- * Tested on:
47
- * [macOS]
48
- * ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]
49
- * [Windows] https://rubyinstaller.org/downloads/ Ruby+Devkit
50
- * ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x64-mingw-ucrt]
51
-
52
- * If you need to build DLLs/shared libralies for your own runtime envrioenment (Linux, etc.):
53
- * CMake https://cmake.org/download/
54
- * C Compiler
55
- * Tested compilers:
56
- * [macOS] clang
57
-
58
- $ clang --version
59
- Apple clang version 13.1.6 (clang-1316.0.21.2.5)
60
- Target: arm64-apple-darwin21.5.0
61
- Thread model: posix
62
- InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
63
-
64
- * [Windows] gcc
65
-
66
- gcc (Rev10, Built by MSYS2 project) 11.2.0
67
-
68
-
69
- <details>
70
- <summary>Older versions</summary>
71
-
72
- * Ruby interpreter
73
- * Tested on:
74
- * [macOS]
75
- * ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [arm64-darwin20]
76
- * ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [arm64-darwin20]
77
- * [Windows] https://rubyinstaller.org/downloads/ Ruby+Devkit
78
- * ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x64-mingw-ucrt]
79
- * ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x64-mingw32]
80
-
81
- * Compiler
82
- * Tested on:
83
- * [macOS] clang
84
-
85
- $ clang --version
86
- Apple clang version 13.1.6 (clang-1316.0.21.2.3)
87
- Target: arm64-apple-darwin21.5.0
88
- Thread model: posix
89
- InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
90
-
91
- Apple clang version 13.0.0 (clang-1300.0.29.3)
92
- Target: arm64-apple-darwin20.6.0
93
- Thread model: posix
94
- InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
95
-
96
- * [Windows] gcc
97
-
98
- gcc (Rev9, Built by MSYS2 project) 11.2.0
99
-
100
- </details>
101
-
102
- ## Limitation ##
103
-
104
- * `SetTraceLogCallback` and `TraceLogCallback` are unusable since Ruby-FFI does not support `:varargs` parameter in callbacks (`Proc`, etc.)
105
- * Ref.: https://ffi.github.io/ruby-ffi-archive/messages/20130214-%5Bruby-ffi%5D%20Re_%20Callback%20Function%20with%20varargs-345.html
106
-
107
- ## License ##
108
-
109
- Shared libraries in `lib` directory are built on top of these products and are available under the terms of these licenses:
110
-
111
- * `libraylib.dylib`, `libraylib.dll`
112
- * raylib ( https://github.com/raysan5/raylib )
113
- * https://github.com/raysan5/raylib/blob/master/LICENSE
114
- * `raygui.dylib`, `raygui.dll`
115
- * raygui ( https://github.com/raysan5/raygui )
116
- * https://github.com/raysan5/raygui/blob/master/LICENSE
117
- * `physac.dylib`, `physac.dll`
118
- * Physac ( https://github.com/raysan5/physac )
119
- * https://github.com/raysan5/physac/blob/4a8e17f263fb8e1150b3fbafc96f880c7d7a4833/src/physac.h#L51-L68
120
-
121
- All ruby codes here are available under the terms of the zlib/libpng License ( http://opensource.org/licenses/Zlib ).
122
-
123
- Ruby-raylib : Yet another raylib wrapper for Ruby
124
- Copyright (c) 2021-2022 vaiorabbit <http://twitter.com/vaiorabbit>
125
-
126
- This software is provided 'as-is', without any express or implied
127
- warranty. In no event will the authors be held liable for any damages
128
- arising from the use of this software.
129
-
130
- Permission is granted to anyone to use this software for any purpose,
131
- including commercial applications, and to alter it and redistribute it
132
- freely, subject to the following restrictions:
133
-
134
- 1. The origin of this software must not be misrepresented; you must not
135
- claim that you wrote the original software. If you use this software
136
- in a product, an acknowledgment in the product documentation would be
137
- appreciated but is not required.
138
-
139
- 2. Altered source versions must be plainly marked as such, and must not be
140
- misrepresented as being the original software.
141
-
142
- 3. This notice may not be removed or altered from any source
143
- distribution.
1
+ <!-- -*- mode:markdown; coding:utf-8; -*- -->
2
+
3
+ # Yet another raylib wrapper for Ruby #
4
+
5
+ * Created : 2021-10-17
6
+ * Last modified : 2023-01-28
7
+
8
+ Provides Ruby bindings for raylib-related libraries including:
9
+
10
+ * [raylib](https://github.com/raysan5/raylib)
11
+ * raylib
12
+ * raymath
13
+ * rlgl
14
+ * [raygui](https://github.com/raysan5/raygui)
15
+ * [Physac](https://github.com/raysan5/physac)
16
+
17
+ <img src="https://raw.githubusercontent.com/vaiorabbit/raylib-bindings/main/doc/bitmap_font_rb.png" width="125"> <img src="https://raw.githubusercontent.com/vaiorabbit/raylib-bindings/main/doc/game_of_life_simple_rb.png" width="125"> <img src="https://raw.githubusercontent.com/vaiorabbit/raylib-bindings/main/doc/lissajous_curve_rb.png" width="125"> <img src="https://raw.githubusercontent.com/vaiorabbit/raylib-bindings/main/doc/procedural_texture_rb.png" width="125"> <img src="https://raw.githubusercontent.com/vaiorabbit/raylib-bindings/main/doc/reversi_board_rb.png" width="125"> <img src="https://raw.githubusercontent.com/vaiorabbit/raylib-bindings/main/doc/utf8_font_rb.png" width="125">
18
+
19
+ ## Features ##
20
+
21
+ * Generated semi-automatically
22
+ * Based on Ruby/FFI ( https://github.com/ffi/ffi )
23
+ * Pre-built binaries are inside:
24
+ * Windows (x86_64)
25
+ * macOS (x86_64, ARM64)
26
+ * Linux (x86_64 WSL, ARM64 Chromebook)
27
+
28
+ ## Quick Start ##
29
+
30
+ ```
31
+ D:\> gem install raylib-bindings
32
+ Fetching raylib-bindings-...
33
+ ...
34
+ 1 gem installed
35
+
36
+ D:\> ruby -r raylib -e 'Raylib.template'
37
+ [Info] Raylib.template : C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/raylib-bindings-0.0.11/examples/template.rb => d://template.rb
38
+ [Info] Raylib.template : Done
39
+
40
+ D:\> ruby template.rb
41
+ ```
42
+
43
+
44
+ <img src="https://raw.githubusercontent.com/vaiorabbit/raylib-bindings/main/doc/template_screenshot.png" width="400">
45
+
46
+ ## Prerequisites ##
47
+
48
+ * Ruby interpreter
49
+ * Tested on:
50
+ * [macOS] https://rvm.io
51
+ * ruby 3.2.0 (2022-12-25 revision a528908271) [arm64-darwin21]
52
+ * [Windows] https://rubyinstaller.org/downloads/ Ruby+Devkit
53
+ * ruby 3.2.0 (2022-12-25 revision a528908271) [x64-mingw-ucrt]
54
+ * [Linux/x86_64 WSL] https://github.com/rvm/ubuntu_rvm
55
+ * ruby 3.2.0preview1 (2022-04-03 master f801386f0c) [x86_64-linux]
56
+ * [Linux/ARM64 Chromebook] https://github.com/rvm/ubuntu_rvm
57
+ * ruby 3.2.0 (2022-12-25 revision a528908271) [aarch64-linux]
58
+
59
+ * If you need to build DLLs/shared libralies for your own runtime envrioenment (Linux, etc.):
60
+ * CMake https://cmake.org/download/
61
+ * C Compiler
62
+ * Tested compilers:
63
+ * [macOS] clang
64
+
65
+ $ clang --version
66
+ Apple clang version 14.0.0 (clang-1400.0.29.202)
67
+ Target: arm64-apple-darwin22.2.0
68
+ Thread model: posix
69
+ InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
70
+
71
+ * [Windows] gcc
72
+
73
+ gcc (Rev7, Built by MSYS2 project) 12.2.0
74
+
75
+ * [Linux] gcc, clang
76
+
77
+ (x86_64) Ubuntu clang version 14.0.0-1ubuntu1
78
+ (arm64) aarch64-linux-gnu-g++ ( https://packages.ubuntu.com/focal/g++-aarch64-linux-gnu )
79
+
80
+ <details>
81
+ <summary>Older versions</summary>
82
+
83
+ * Ruby interpreter
84
+ * Tested on:
85
+ * [macOS]
86
+ * ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]
87
+ * ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [arm64-darwin20]
88
+ * ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [arm64-darwin20]
89
+ * [Windows] https://rubyinstaller.org/downloads/ Ruby+Devkit
90
+ * ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x64-mingw-ucrt]
91
+ * ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x64-mingw-ucrt]
92
+ * ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x64-mingw32]
93
+
94
+ * Compiler
95
+ * Tested on:
96
+ * [macOS] clang
97
+
98
+ $ clang --version
99
+ Apple clang version 13.1.6 (clang-1316.0.21.2.5)
100
+ Target: arm64-apple-darwin21.5.0
101
+ Thread model: posix
102
+ InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
103
+
104
+ $ clang --version
105
+ Apple clang version 13.1.6 (clang-1316.0.21.2.3)
106
+ Target: arm64-apple-darwin21.5.0
107
+ Thread model: posix
108
+ InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
109
+
110
+ Apple clang version 13.0.0 (clang-1300.0.29.3)
111
+ Target: arm64-apple-darwin20.6.0
112
+ Thread model: posix
113
+ InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
114
+
115
+ * [Windows] gcc
116
+
117
+ gcc (Rev9, Built by MSYS2 project) 11.2.0
118
+ gcc (Rev10, Built by MSYS2 project) 11.2.0
119
+
120
+ </details>
121
+
122
+ ## Project ##
123
+
124
+ See the project below to learn how to use this library:
125
+
126
+ * Whac-a-Mole! : Ruby raylib bindings demo
127
+ * <img src="https://raw.githubusercontent.com/vaiorabbit/raylib-bindings-whacamole/main/doc/screenshot_00.png" width="300"> <img src="https://raw.githubusercontent.com/vaiorabbit/raylib-bindings-whacamole/main/doc/screenshot_01.png" width="300">
128
+ * <https://github.com/vaiorabbit/raylib-bindings-whacamole>
129
+
130
+ ## Limitation ##
131
+
132
+ * `SetTraceLogCallback` and `TraceLogCallback` are unusable since Ruby-FFI does not support `:varargs` parameter in callbacks (`Proc`, etc.)
133
+ * Ref.: https://ffi.github.io/ruby-ffi-archive/messages/20130214-%5Bruby-ffi%5D%20Re_%20Callback%20Function%20with%20varargs-345.html
134
+
135
+ ## License ##
136
+
137
+ Shared libraries in `lib` directory are built on top of these products and are available under the terms of these licenses:
138
+
139
+ * `libraylib.dylib`, `libraylib.dll`, `libraylib.aarch64.so`, `libraylib.x86_64.so`
140
+ * raylib ( https://github.com/raysan5/raylib )
141
+ * zlib License https://github.com/raysan5/raylib/blob/master/LICENSE
142
+ * `raygui.dylib`, `raygui.dll`, `raygui.aarch64.so`, `raygui.x86_64.so`
143
+ * raygui ( https://github.com/raysan5/raygui )
144
+ * zlib License https://github.com/raysan5/raygui/blob/master/LICENSE
145
+ * `physac.dylib`, `physac.dll`, `physac.aarch64.so` `physac.x86_64.so`
146
+ * Physac ( https://github.com/raysan5/physac )
147
+ * LICENSE: zlib/libpng https://github.com/raysan5/physac/blob/4a8e17f263fb8e1150b3fbafc96f880c7d7a4833/src/physac.h#L51-L68
148
+
149
+ All ruby codes here are available under the terms of the zlib/libpng License ( http://opensource.org/licenses/Zlib ).
150
+
151
+ Ruby-raylib : Yet another raylib wrapper for Ruby
152
+ Copyright (c) 2021-2023 vaiorabbit <http://twitter.com/vaiorabbit>
153
+
154
+ This software is provided 'as-is', without any express or implied
155
+ warranty. In no event will the authors be held liable for any damages
156
+ arising from the use of this software.
157
+
158
+ Permission is granted to anyone to use this software for any purpose,
159
+ including commercial applications, and to alter it and redistribute it
160
+ freely, subject to the following restrictions:
161
+
162
+ 1. The origin of this software must not be misrepresented; you must not
163
+ claim that you wrote the original software. If you use this software
164
+ in a product, an acknowledgment in the product documentation would be
165
+ appreciated but is not required.
166
+
167
+ 2. Altered source versions must be plainly marked as such, and must not be
168
+ misrepresented as being the original software.
169
+
170
+ 3. This notice may not be removed or altered from any source
171
+ distribution.
data/examples/template.rb CHANGED
@@ -1,146 +1,148 @@
1
- # Yet another raylib wrapper for Ruby
2
- #
3
- # * https://github.com/vaiorabbit/raylib-bindings
4
- #
5
- # Demonstrates several key features including:
6
- # * Core : window management, camera, etc.
7
- # * Gameplay : fetching player input, collision handling, etc.
8
- # * Rendering : drawing shapes, texts, etc.
9
- #
10
- # To get more information, see:
11
- # * https://www.raylib.com/cheatsheet/cheatsheet.html for API reference
12
- # * https://github.com/vaiorabbit/raylib-bindings/tree/main/examples for more actual codes written in Ruby
13
-
14
- require 'raylib'
15
-
16
- shared_lib_path = Gem::Specification.find_by_name('raylib-bindings').full_gem_path + '/lib/'
17
-
18
- case RUBY_PLATFORM
19
- when /mswin|msys|mingw|cygwin/
20
- Raylib.load_lib(shared_lib_path + 'libraylib.dll', raygui_libpath: shared_lib_path + 'raygui.dll', physac_libpath: shared_lib_path + 'physac.dll')
21
- when /darwin/
22
- Raylib.load_lib(shared_lib_path + 'libraylib.dylib', raygui_libpath: shared_lib_path + 'raygui.dylib', physac_libpath: shared_lib_path + 'physac.dylib')
23
- else
24
- raise RuntimeError, "Unknown OS: #{RUBY_PLATFORM}"
25
- end
26
-
27
- include Raylib
28
-
29
- if __FILE__ == $PROGRAM_NAME
30
- screen_width = 1280
31
- screen_height = 720
32
- InitWindow(screen_width, screen_height, "Yet Another Ruby-raylib bindings")
33
- SetTargetFPS(60)
34
-
35
- ruby_red = Color.from_u8(155, 17, 30, 255)
36
-
37
- # Camera
38
- camera = Camera.new
39
- reset_camera = lambda {
40
- camera[:position] = Vector3.create(0.0, 10.0, 10.0)
41
- camera[:target] = Vector3.create(0.0, 0.0, 0.0)
42
- camera[:up] = Vector3.create(0.0, 1.0, 0.0)
43
- camera[:fovy] = 45.0
44
- camera[:projection] = CAMERA_PERSPECTIVE
45
- }
46
- reset_camera.call
47
- SetCameraMode(camera, CAMERA_FREE)
48
- auto_rotate = false
49
-
50
- # Player (red cube) settings
51
- player_pos = Vector3.create(0.0, 0.0, 0.0)
52
- player_size = Vector3.create(2.0, 2.0, 2.0)
53
- speed = 0.25
54
-
55
- # Obstacle settings
56
- obstacle_cube_pos = Vector3.create(-4.0, 1.0, 0.0)
57
- obstacle_cube_size = Vector3.create(2.0, 2.0, 2.0)
58
- obstacle_sphere_pos = Vector3.create(4.0, 0.0, 0.0)
59
- obstacle_sphere_size = 1.5
60
-
61
- until WindowShouldClose()
62
- ### Update phase
63
-
64
- # Reset camera settings
65
- if IsKeyPressed(KEY_F1)
66
- auto_rotate = !auto_rotate
67
- reset_camera.call
68
- SetCameraMode(camera, auto_rotate ? CAMERA_ORBITAL : CAMERA_FREE)
69
- end
70
- UpdateCamera(camera)
71
-
72
- # Calculate move direction
73
- move = Vector3.create(0, 0, 0)
74
- move[:x] += speed if IsKeyDown(KEY_RIGHT)
75
- move[:x] -= speed if IsKeyDown(KEY_LEFT)
76
- move[:z] += speed if IsKeyDown(KEY_DOWN)
77
- move[:z] -= speed if IsKeyDown(KEY_UP)
78
-
79
- to_camera = Vector3Normalize(Vector3.create(camera[:position][:x], 0, camera[:position][:z]))
80
- rotate_y = QuaternionFromVector3ToVector3(Vector3.create(0, 0, 1), to_camera)
81
- move = Vector3RotateByQuaternion(move, rotate_y)
82
-
83
- player_pos = Vector3Add(player_pos, move)
84
- player_screen_pos = GetWorldToScreen(Vector3.create(player_pos[:x], player_pos[:y] + 2.5, player_pos[:z]), camera)
85
-
86
- # Check collision status
87
- collision = false
88
-
89
- player_bbox = BoundingBox.create(player_pos[:x] - player_size[:x]/2,
90
- player_pos[:y] - player_size[:y]/2,
91
- player_pos[:z] - player_size[:z]/2,
92
- player_pos[:x] + player_size[:x]/2,
93
- player_pos[:y] + player_size[:y]/2,
94
- player_pos[:z] + player_size[:z]/2)
95
-
96
- obstacle_cube_bbox = BoundingBox.create(obstacle_cube_pos[:x] - obstacle_cube_size[:x]/2,
97
- obstacle_cube_pos[:y] - obstacle_cube_size[:y]/2,
98
- obstacle_cube_pos[:z] - obstacle_cube_size[:z]/2,
99
- obstacle_cube_pos[:x] + obstacle_cube_size[:x]/2,
100
- obstacle_cube_pos[:y] + obstacle_cube_size[:y]/2,
101
- obstacle_cube_pos[:z] + obstacle_cube_size[:z]/2)
102
-
103
- # Check collisions player vs obstacle_cube
104
- collision = true if CheckCollisionBoxes(player_bbox, obstacle_cube_bbox)
105
-
106
- # Check collisions player vs obstacle_sphere
107
- collision = true if CheckCollisionBoxSphere(player_bbox, obstacle_sphere_pos, obstacle_sphere_size)
108
-
109
- ### Rendering phase
110
-
111
- BeginDrawing()
112
-
113
- ClearBackground(RAYWHITE)
114
-
115
- ## 3D scene
116
- BeginMode3D(camera)
117
- # Red cube
118
- DrawCube(player_pos, 2.0, 2.0, 2.0, collision ? Fade(ruby_red, 0.25) : ruby_red)
119
- DrawCubeWires(player_pos, 2.0, 2.0, 2.0, MAROON)
120
- # Obstacle cube
121
- DrawCube(obstacle_cube_pos, obstacle_cube_size[:x], obstacle_cube_size[:y], obstacle_cube_size[:z], GRAY)
122
- DrawCubeWires(obstacle_cube_pos, obstacle_cube_size[:x], obstacle_cube_size[:y], obstacle_cube_size[:z], DARKGRAY)
123
- # Obstacle sphere
124
- DrawSphere(obstacle_sphere_pos, obstacle_sphere_size, GRAY)
125
- DrawSphereWires(obstacle_sphere_pos, obstacle_sphere_size, 16, 16, DARKGRAY)
126
- # Floor
127
- DrawGrid(10, 1)
128
- EndMode3D()
129
-
130
- ## HUD
131
- # Text over the red cube
132
- DrawText("Player HP: 100 / 100", player_screen_pos[:x] - MeasureText("Player HP: 100/100", 20)/2, player_screen_pos[:y], 20, BLACK)
133
- # Help message
134
- DrawRectangle(10, screen_height - 100, 300, 80, Fade(MAROON, 0.25))
135
- DrawRectangleLines(10, screen_height - 100, 300, 80, ruby_red)
136
- DrawText("Arrow keys : move red cube", 20, screen_height - 90, 20, BLACK)
137
- DrawText("F1 : camera rotation", 20, screen_height - 70, 20, BLACK)
138
- DrawText("ESC : exit", 20, screen_height - 50, 20, BLACK)
139
- # FPS
140
- DrawFPS(screen_width - 100, 16)
141
-
142
- EndDrawing()
143
- end
144
-
145
- CloseWindow()
146
- end
1
+ # Yet another raylib wrapper for Ruby
2
+ #
3
+ # * https://github.com/vaiorabbit/raylib-bindings
4
+ #
5
+ # Demonstrates several key features including:
6
+ # * Core : window management, camera, etc.
7
+ # * Gameplay : fetching player input, collision handling, etc.
8
+ # * Rendering : drawing shapes, texts, etc.
9
+ #
10
+ # To get more information, see:
11
+ # * https://www.raylib.com/cheatsheet/cheatsheet.html for API reference
12
+ # * https://github.com/vaiorabbit/raylib-bindings/tree/main/examples for more actual codes written in Ruby
13
+
14
+ require 'raylib'
15
+
16
+ shared_lib_path = Gem::Specification.find_by_name('raylib-bindings').full_gem_path + '/lib/'
17
+
18
+ case RUBY_PLATFORM
19
+ when /mswin|msys|mingw|cygwin/
20
+ Raylib.load_lib(shared_lib_path + 'libraylib.dll', raygui_libpath: shared_lib_path + 'raygui.dll', physac_libpath: shared_lib_path + 'physac.dll')
21
+ when /darwin/
22
+ Raylib.load_lib(shared_lib_path + 'libraylib.dylib', raygui_libpath: shared_lib_path + 'raygui.dylib', physac_libpath: shared_lib_path + 'physac.dylib')
23
+ when /linux/
24
+ Raylib.load_lib(shared_lib_path + 'libraylib.so', raygui_libpath: shared_lib_path + 'raygui.so', physac_libpath: shared_lib_path + 'physac.so')
25
+ else
26
+ raise RuntimeError, "Unknown OS: #{RUBY_PLATFORM}"
27
+ end
28
+
29
+ include Raylib
30
+
31
+ if __FILE__ == $PROGRAM_NAME
32
+ screen_width = 1280
33
+ screen_height = 720
34
+ InitWindow(screen_width, screen_height, "Yet Another Ruby-raylib bindings")
35
+ SetTargetFPS(60)
36
+
37
+ ruby_red = Color.from_u8(155, 17, 30, 255)
38
+
39
+ # Camera
40
+ camera = Camera.new
41
+ reset_camera = lambda {
42
+ camera.position.set(0.0, 10.0, 10.0)
43
+ camera.target.set(0.0, 0.0, 0.0)
44
+ camera.up.set(0.0, 1.0, 0.0)
45
+ camera.fovy = 45.0
46
+ camera.projection = CAMERA_PERSPECTIVE
47
+ }
48
+ reset_camera.call
49
+ SetCameraMode(camera, CAMERA_FREE)
50
+ auto_rotate = false
51
+
52
+ # Player (red cube) settings
53
+ player_pos = Vector3.create(0.0, 0.0, 0.0)
54
+ player_size = Vector3.create(2.0, 2.0, 2.0)
55
+ speed = 0.25
56
+
57
+ # Obstacle settings
58
+ obstacle_cube_pos = Vector3.create(-4.0, 1.0, 0.0)
59
+ obstacle_cube_size = Vector3.create(2.0, 2.0, 2.0)
60
+ obstacle_sphere_pos = Vector3.create(4.0, 0.0, 0.0)
61
+ obstacle_sphere_size = 1.5
62
+
63
+ until WindowShouldClose()
64
+ ### Update phase
65
+
66
+ # Reset camera settings
67
+ if IsKeyPressed(KEY_F1)
68
+ auto_rotate = !auto_rotate
69
+ reset_camera.call
70
+ SetCameraMode(camera, auto_rotate ? CAMERA_ORBITAL : CAMERA_FREE)
71
+ end
72
+ UpdateCamera(camera)
73
+
74
+ # Calculate move direction
75
+ move = Vector3.create(0, 0, 0)
76
+ move[:x] += speed if IsKeyDown(KEY_RIGHT)
77
+ move[:x] -= speed if IsKeyDown(KEY_LEFT)
78
+ move[:z] += speed if IsKeyDown(KEY_DOWN)
79
+ move[:z] -= speed if IsKeyDown(KEY_UP)
80
+
81
+ to_camera = Vector3Normalize(Vector3.create(camera.position.x, 0, camera.position.z))
82
+ rotate_y = QuaternionFromVector3ToVector3(Vector3.create(0, 0, 1), to_camera)
83
+ move = Vector3RotateByQuaternion(move, rotate_y)
84
+
85
+ player_pos = Vector3Add(player_pos, move)
86
+ player_screen_pos = GetWorldToScreen(Vector3.create(player_pos.x, player_pos.y + 2.5, player_pos.z), camera)
87
+
88
+ # Check collision status
89
+ collision = false
90
+
91
+ player_bbox = BoundingBox.create(player_pos.x - player_size.x/2,
92
+ player_pos.y - player_size.y/2,
93
+ player_pos.z - player_size.z/2,
94
+ player_pos.x + player_size.x/2,
95
+ player_pos.y + player_size.y/2,
96
+ player_pos.z + player_size.z/2)
97
+
98
+ obstacle_cube_bbox = BoundingBox.create(obstacle_cube_pos.x - obstacle_cube_size.x/2,
99
+ obstacle_cube_pos.y - obstacle_cube_size.y/2,
100
+ obstacle_cube_pos.z - obstacle_cube_size.z/2,
101
+ obstacle_cube_pos.x + obstacle_cube_size.x/2,
102
+ obstacle_cube_pos.y + obstacle_cube_size.y/2,
103
+ obstacle_cube_pos.z + obstacle_cube_size.z/2)
104
+
105
+ # Check collisions player vs obstacle_cube
106
+ collision = true if CheckCollisionBoxes(player_bbox, obstacle_cube_bbox)
107
+
108
+ # Check collisions player vs obstacle_sphere
109
+ collision = true if CheckCollisionBoxSphere(player_bbox, obstacle_sphere_pos, obstacle_sphere_size)
110
+
111
+ ### Rendering phase
112
+
113
+ BeginDrawing()
114
+
115
+ ClearBackground(RAYWHITE)
116
+
117
+ ## 3D scene
118
+ BeginMode3D(camera)
119
+ # Red cube
120
+ DrawCube(player_pos, 2.0, 2.0, 2.0, collision ? Fade(ruby_red, 0.25) : ruby_red)
121
+ DrawCubeWires(player_pos, 2.0, 2.0, 2.0, MAROON)
122
+ # Obstacle cube
123
+ DrawCube(obstacle_cube_pos, obstacle_cube_size.x, obstacle_cube_size.y, obstacle_cube_size.z, GRAY)
124
+ DrawCubeWires(obstacle_cube_pos, obstacle_cube_size.x, obstacle_cube_size.y, obstacle_cube_size.z, DARKGRAY)
125
+ # Obstacle sphere
126
+ DrawSphere(obstacle_sphere_pos, obstacle_sphere_size, GRAY)
127
+ DrawSphereWires(obstacle_sphere_pos, obstacle_sphere_size, 16, 16, DARKGRAY)
128
+ # Floor
129
+ DrawGrid(10, 1)
130
+ EndMode3D()
131
+
132
+ ## HUD
133
+ # Text over the red cube
134
+ DrawText("Player HP: 100 / 100", player_screen_pos.x - MeasureText("Player HP: 100/100", 20)/2, player_screen_pos.y, 20, BLACK)
135
+ # Help message
136
+ DrawRectangle(10, screen_height - 100, 300, 80, Fade(MAROON, 0.25))
137
+ DrawRectangleLines(10, screen_height - 100, 300, 80, ruby_red)
138
+ DrawText("Arrow keys : move red cube", 20, screen_height - 90, 20, BLACK)
139
+ DrawText("F1 : camera rotation", 20, screen_height - 70, 20, BLACK)
140
+ DrawText("ESC : exit", 20, screen_height - 50, 20, BLACK)
141
+ # FPS
142
+ DrawFPS(screen_width - 100, 16)
143
+
144
+ EndDrawing()
145
+ end
146
+
147
+ CloseWindow()
148
+ end