raylib-bindings 0.2.0 → 0.4.0

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