rustyray_sys/
ffi.rs

1/**********************************************************************************************
2*
3*   raylib v5.5 - A simple and easy-to-use library to enjoy videogames programming (www.raylib.com)
4*
5*   FEATURES:
6*       - NO external dependencies, all required libraries included with raylib
7*       - Multiplatform: Windows, Linux, FreeBSD, OpenBSD, NetBSD, DragonFly,
8*                        MacOS, Haiku, Android, Raspberry Pi, DRM native, HTML5.
9*       - Written in plain C code (C99) in PascalCase/camelCase notation
10*       - Hardware accelerated with OpenGL (1.1, 2.1, 3.3, 4.3, ES2, ES3 - choose at compile)
11*       - Unique OpenGL abstraction layer (usable as standalone module): [rlgl]
12*       - Multiple Fonts formats supported (TTF, OTF, FNT, BDF, Sprite fonts)
13*       - Outstanding texture formats support, including compressed formats (DXT, ETC, ASTC)
14*       - Full 3d support for 3d Shapes, Models, Billboards, Heightmaps and more!
15*       - Flexible Materials system, supporting classic maps and PBR maps
16*       - Animated 3D models supported (skeletal bones animation) (IQM, M3D, GLTF)
17*       - Shaders support, including Model shaders and Postprocessing shaders
18*       - Powerful math module for Vector, Matrix and Quaternion operations: [raymath]
19*       - Audio loading and playing with streaming support (WAV, OGG, MP3, FLAC, QOA, XM, MOD)
20*       - VR stereo rendering with configurable HMD device parameters
21*       - Bindings to multiple programming languages available!
22*
23*   NOTES:
24*       - One default Font is loaded on InitWindow()->LoadFontDefault() [core, text]
25*       - One default Texture2D is loaded on rlglInit(), 1x1 white pixel R8G8B8A8 [rlgl] (OpenGL 3.3 or ES2)
26*       - One default Shader is loaded on rlglInit()->rlLoadShaderDefault() [rlgl] (OpenGL 3.3 or ES2)
27*       - One default RenderBatch is loaded on rlglInit()->rlLoadRenderBatch() [rlgl] (OpenGL 3.3 or ES2)
28*
29*   DEPENDENCIES (included):
30*       [rcore][GLFW] rglfw (Camilla Löwy - github.com/glfw/glfw) for window/context management and input
31*       [rcore][RGFW] rgfw (ColleagueRiley - github.com/ColleagueRiley/RGFW) for window/context management and input
32*       [rlgl] glad/glad_gles2 (David Herberth - github.com/Dav1dde/glad) for OpenGL 3.3 extensions loading
33*       [raudio] miniaudio (David Reid - github.com/mackron/miniaudio) for audio device/context management
34*
35*   OPTIONAL DEPENDENCIES (included):
36*       [rcore] msf_gif (Miles Fogle) for GIF recording
37*       [rcore] sinfl (Micha Mettke) for DEFLATE decompression algorithm
38*       [rcore] sdefl (Micha Mettke) for DEFLATE compression algorithm
39*       [rcore] rprand (Ramon Snatamaria) for pseudo-random numbers generation
40*       [rtextures] qoi (Dominic Szablewski - https://phoboslab.org) for QOI image manage
41*       [rtextures] stb_image (Sean Barret) for images loading (BMP, TGA, PNG, JPEG, HDR...)
42*       [rtextures] stb_image_write (Sean Barret) for image writing (BMP, TGA, PNG, JPG)
43*       [rtextures] stb_image_resize2 (Sean Barret) for image resizing algorithms
44*       [rtextures] stb_perlin (Sean Barret) for Perlin Noise image generation
45*       [rtext] stb_truetype (Sean Barret) for ttf fonts loading
46*       [rtext] stb_rect_pack (Sean Barret) for rectangles packing
47*       [rmodels] par_shapes (Philip Rideout) for parametric 3d shapes generation
48*       [rmodels] tinyobj_loader_c (Syoyo Fujita) for models loading (OBJ, MTL)
49*       [rmodels] cgltf (Johannes Kuhlmann) for models loading (glTF)
50*       [rmodels] m3d (bzt) for models loading (M3D, https://bztsrc.gitlab.io/model3d)
51*       [rmodels] vox_loader (Johann Nadalutti) for models loading (VOX)
52*       [raudio] dr_wav (David Reid) for WAV audio file loading
53*       [raudio] dr_flac (David Reid) for FLAC audio file loading
54*       [raudio] dr_mp3 (David Reid) for MP3 audio file loading
55*       [raudio] stb_vorbis (Sean Barret) for OGG audio loading
56*       [raudio] jar_xm (Joshua Reisenauer) for XM audio module loading
57*       [raudio] jar_mod (Joshua Reisenauer) for MOD audio module loading
58*       [raudio] qoa (Dominic Szablewski - https://phoboslab.org) for QOA audio manage
59*
60*
61*   LICENSE: zlib/libpng
62*
63*   raylib is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
64*   BSD-like license that allows static linking with closed source software:
65*
66*   Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
67*
68*   This software is provided "as-is", without any express or implied warranty. In no event
69*   will the authors be held liable for any damages arising from the use of this software.
70*
71*   Permission is granted to anyone to use this software for any purpose, including commercial
72*   applications, and to alter it and redistribute it freely, subject to the following restrictions:
73*
74*     1. The origin of this software must not be misrepresented; you must not claim that you
75*     wrote the original software. If you use this software in a product, an acknowledgment
76*     in the product documentation would be appreciated but is not required.
77*
78*     2. Altered source versions must be plainly marked as such, and must not be misrepresented
79*     as being the original software.
80*
81*     3. This notice may not be removed or altered from any source distribution.
82*
83**********************************************************************************************/
84
85use libc::{c_char, c_double, c_float, c_int};
86
87use crate::{
88    color::Color,
89    consts::{ConfigFlag, MouseButton},
90    rectangle::Rectangle,
91    texture::{RenderTexture, RenderTexture2D, Texture},
92    vector::Vector2,
93};
94
95// Window-related functions
96unsafe extern "C" {
97    /// Initialize window and OpenGL context
98    #[link_name = "InitWindow"]
99    pub fn init_window(width: c_int, height: c_int, title: *const c_char);
100    /// Close window and unload OpenGL context
101    #[link_name = "CloseWindow"]
102    pub fn close_window();
103    /// Check if application should close (KEY_ESCAPE pressed or windows close icon clicked)
104    #[link_name = "WindowShouldClose"]
105    pub fn window_should_close() -> bool;
106    /// Check if window has been initialized successfully
107    #[link_name = "IsWindowReady"]
108    pub fn is_window_ready() -> bool;
109    /// Check if window is currently fullscreen
110    #[link_name = "IsWindowFullscreen"]
111    pub fn is_window_fullscreen() -> bool;
112    /// Check if window is currently hidden
113    #[link_name = "IsWindowHidden"]
114    pub fn is_window_hidden() -> bool;
115    /// Check if window is currently minimized
116    #[link_name = "IsWindowMinimized"]
117    pub fn is_window_minimized() -> bool;
118    /// Check if window is currently maximized
119    #[link_name = "IsWindowMaximized"]
120    pub fn is_window_maximized() -> bool;
121    /// Check if window is currently focused
122    #[link_name = "IsWindowFocused"]
123    pub fn is_window_focused() -> bool;
124    /// Check if window has been resized last frame
125    #[link_name = "IsWindowResized"]
126    pub fn is_window_resized() -> bool;
127    /// Check if one specific window flag is enabled
128    ///
129    /// Flags should be of values defined in [ConfigFlag].
130    ///
131    /// Use this value as a bitmask.
132    ///
133    /// # Examples
134    /// ```no_run
135    /// use rustyray_sys::{ffi::is_window_state, consts::ConfigFlag};
136    ///
137    /// unsafe { is_window_state(ConfigFlag::VsyncHint | ConfigFlag::FullscreenMode) };
138    /// ```
139    #[link_name = "IsWindowState"]
140    pub fn is_window_state(flags: ConfigFlag) -> bool;
141    /// Set window configuration state using flags
142    ///
143    /// Flags should be of values defined in [ConfigFlag].
144    ///
145    /// Use this value as a bitmask.
146    ///
147    /// # Examples
148    /// ```no_run
149    /// use rustyray_sys::{ffi::set_window_state, consts::ConfigFlag};
150    ///
151    /// unsafe { set_window_state(ConfigFlag::VsyncHint | ConfigFlag::FullscreenMode); }
152    /// ```
153    #[link_name = "SetWindowState"]
154    pub fn set_window_state(flags: ConfigFlag);
155    /// Clear window configuration state flags
156    ///
157    /// Flags should be of values defined in [ConfigFlag].
158    ///
159    /// Use this value as a bitmask.
160    ///
161    /// # Examples
162    /// ```no_run
163    /// use rustyray_sys::{ffi::clear_window_state, consts::ConfigFlag};
164    ///
165    /// unsafe { clear_window_state(ConfigFlag::VsyncHint | ConfigFlag::FullscreenMode); }
166    /// ```
167    #[link_name = "ClearWindowState"]
168    pub fn clear_window_state(flags: ConfigFlag);
169    /// Set window dimension
170    #[link_name = "SetWindowSize"]
171    pub fn set_window_size(width: c_int, height: c_int);
172    /// Set window opacity [0.0..1.0]
173    #[link_name = "SetWindowOpacity"]
174    pub fn set_window_opacity(opacity: c_float);
175    /// Set window focused
176    #[link_name = "SetWindowFocused"]
177    pub fn set_window_focused();
178    /// Get current screen width
179    #[link_name = "GetScreenWidth"]
180    pub fn get_screen_width() -> c_int;
181    /// Get current screen height
182    #[link_name = "GetScreenHeight"]
183    pub fn get_screen_height() -> c_int;
184}
185
186// Drawing related functions
187unsafe extern "C" {
188    /// Set background color (framebuffer clear color)
189    #[link_name = "ClearBackground"]
190    pub fn clear_background(color: Color);
191    #[link_name = "BeginDrawing"]
192    pub fn begin_drawing();
193    #[link_name = "EndDrawing"]
194    pub fn end_drawing();
195    #[link_name = "BeginTextureMode"]
196    pub fn begin_texture_mode(render_texture: RenderTexture2D);
197    #[link_name = "EndTextureMode"]
198    pub fn end_texture_mode();
199    #[link_name = "LoadTexture"]
200    pub fn load_texture(path: *const c_char) -> Texture;
201    #[link_name = "LoadRenderTexture"]
202    pub fn load_render_texture(width: c_int, height: c_int) -> RenderTexture;
203    #[link_name = "UnloadTexture"]
204    pub fn unload_texture(texture: Texture);
205    #[link_name = "UnloadRenderTexture"]
206    pub fn unload_render_texture(render_texture: RenderTexture);
207}
208
209// Text drawing functions
210unsafe extern "C" {
211    /// Draw current FPS
212    #[link_name = "DrawFPS"]
213    pub fn draw_fps(pos_x: c_int, pos_y: c_int);
214    /// Draw text (using default font)
215    #[link_name = "DrawText"]
216    pub fn draw_text(
217        text: *const c_char,
218        pos_x: c_int,
219        pos_y: c_int,
220        font_size: c_int,
221        color: Color,
222    );
223}
224
225// Texture drawing functions
226unsafe extern "C" {
227    /// Draw a [Texture]
228    #[link_name = "DrawTexture"]
229    pub fn draw_texture(texture: Texture, pos_x: c_int, pos_y: c_int, tint: Color);
230    /// Draw a part of a [Texture] defined by a [Rectangle] with 'pro' parameters
231    #[link_name = "DrawTexturePro"]
232    pub fn draw_texture_pro(
233        texture: Texture,
234        source: Rectangle,
235        dest: Rectangle,
236        origin: Vector2,
237        rotation: c_float,
238        tint: Color,
239    );
240}
241
242// Basic shapes drawing functions
243unsafe extern "C" {
244    /// Draw a color-filled rectangle
245    #[link_name = "DrawRectangleRec"]
246    pub fn draw_rectangle_rec(rec: Rectangle, color: Color);
247}
248
249// Input-related functions: mouse
250unsafe extern "C" {
251    /// Check if a mouse button is beening pressed
252    #[link_name = "IsMouseButtonDown"]
253    pub fn is_mouse_button_down(button: MouseButton) -> bool;
254    /// Get mouse position X
255    #[link_name = "GetMouseX"]
256    pub fn get_mouse_x() -> c_int;
257    /// Get mouse position Y
258    #[link_name = "GetMouseY"]
259    pub fn get_mouse_y() -> c_int;
260    /// Get mouse position XY
261    #[link_name = "GetMousePosition"]
262    pub fn get_mouse_position() -> Vector2;
263}
264
265// Timing-related functions
266unsafe extern "C" {
267    /// Set target FPS (maximum)
268    #[link_name = "SetTargetFPS"]
269    pub fn set_target_fps(fps: c_int);
270    /// Get time in seconds for last frame drawn (delta time)
271    #[link_name = "GetFrameTime"]
272    pub fn get_frame_time() -> c_float;
273    /// Get elapsed time in seconds since InitWindow()
274    #[link_name = "GetTime"]
275    pub fn get_time() -> c_double;
276    /// Get current FPS
277    #[link_name = "GetFPS"]
278    pub fn get_fps() -> c_int;
279}
280
281// Misc functions
282unsafe extern "C" {
283    /// Setup init configuration flags
284    ///
285    /// Flags should be of values defined in [ConfigFlag].
286    ///
287    /// Use this value as a bitmask.
288    ///
289    /// # Examples
290    /// ```no_run
291    /// use rustyray_sys::{ffi::set_config_flags, consts::ConfigFlag};
292    ///
293    /// unsafe { set_config_flags(ConfigFlag::VsyncHint | ConfigFlag::FullscreenMode) }
294    /// ```
295    #[link_name = "SetConfigFlags"]
296    pub fn set_config_flags(flags: ConfigFlag);
297}