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, c_void};
86
87use crate::{
88    color::Color,
89    consts::{ConfigFlag, MouseButton},
90    rectangle::Rectangle,
91    texture::{Image, 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    /// Toggle window state: fullscreen/windowed, resizes monitor to match window resolution
170    #[link_name = "ToggleFullscreen"]
171    pub fn toggle_fullscreen();
172    /// Toggle window state: borderless windowed, resizes window to match monitor resolution
173    #[link_name = "ToggleBorderlessWindowed"]
174    pub fn toggle_borderless_windowed();
175    /// Set window state: maximized, if resizable
176    #[link_name = "MaximizeWindow"]
177    pub fn maximize_window();
178    /// Set window state: minimized, if resizable
179    #[link_name = "MinimizeWindow"]
180    pub fn minimize_window();
181    /// Set window state: not minimized/maximized
182    #[link_name = "RestoreWindow"]
183    pub fn restore_window();
184    /// Set icon for window (single image, RGBA 32bit)
185    #[link_name = "SetWindowIcon"]
186    pub fn set_window_icon(image: Image);
187    /// Set icon for window (multiple images, RGBA 32bit)
188    #[link_name = "SetWindowIcons"]
189    pub fn set_window_icons(images: *const Image);
190    /// Set title for window
191    #[link_name = "SetWindowTitle"]
192    pub fn set_window_title(images: *const c_char);
193    /// Set window position on screen
194    #[link_name = "SetWindowPosition"]
195    pub fn set_window_position(x: c_int, y: c_int);
196    /// Set monitor for the current window
197    #[link_name = "SetWindowMonitor"]
198    pub fn set_window_monitor(monitor: c_int);
199    /// Set window minimum dimensions (for [ConfigFlag::WindowResizable])
200    #[link_name = "SetWindowMinSize"]
201    pub fn set_window_min_size(width: c_int, height: c_int);
202    /// Set window maximum dimensions (for [ConfigFlag::WindowResizable])
203    #[link_name = "SetWindowMaxSize"]
204    pub fn set_window_max_size(width: c_int, height: c_int);
205    /// Set window dimension
206    #[link_name = "SetWindowSize"]
207    pub fn set_window_size(width: c_int, height: c_int);
208    /// Set window opacity [0.0..1.0]
209    #[link_name = "SetWindowOpacity"]
210    pub fn set_window_opacity(opacity: c_float);
211    /// Set window focused
212    #[link_name = "SetWindowFocused"]
213    pub fn set_window_focused();
214    /// Get current screen width
215    #[link_name = "GetScreenWidth"]
216    pub fn get_screen_width() -> c_int;
217    /// Get current screen height
218    #[link_name = "GetScreenHeight"]
219    pub fn get_screen_height() -> c_int;
220    /// Get current render width (it considers HiDPI)
221    #[link_name = "GetRenderWidth"]
222    pub fn get_render_width() -> c_int;
223    /// Get current render height (it considers HiDPI)
224    #[link_name = "GetRenderHeight"]
225    pub fn get_render_height() -> c_int;
226    /// Get number of connected monitors
227    #[link_name = "GetMonitorCount"]
228    pub fn get_monitor_count() -> c_int;
229    /// Get current monitor where window is placed
230    #[link_name = "GetCurrentMonitor"]
231    pub fn get_current_monitor() -> c_int;
232    /// Get specified monitor position
233    #[link_name = "GetMonitorPosition"]
234    pub fn get_monitor_position(monitor: c_int) -> Vector2;
235    /// Get specified monitor width (current video mode used by monitor)
236    #[link_name = "GetMonitorWidth"]
237    pub fn get_monitor_width(monitor: c_int) -> c_int;
238    /// Get specified monitor height (current video mode used by monitor)
239    #[link_name = "GetMonitorHeight"]
240    pub fn get_monitor_height(monitor: c_int) -> c_int;
241    /// Get specified monitor physical width in millimetres
242    #[link_name = "GetMonitorPhysicalWidth"]
243    pub fn get_monitor_physical_width(monitor: c_int) -> c_int;
244    /// Get specified monitor physical height in millimetres
245    #[link_name = "GetMonitorPhysicalHeight"]
246    pub fn get_monitor_physical_height(monitor: c_int) -> c_int;
247    /// Get specified monitor refresh rate
248    #[link_name = "GetMonitorRefreshRate"]
249    pub fn get_monitor_refresh_rate(monitor: c_int) -> c_int;
250    /// Get position XY on monitor
251    #[link_name = "GetWindowPosition"]
252    pub fn get_window_position() -> Vector2;
253    /// Get window scale DPI factor
254    #[link_name = "GetWindowScaleDPI"]
255    pub fn get_window_scale_dpi() -> Vector2;
256    /// Get the human-readable, UTF-8 encoded name of the specified monitor
257    #[link_name = "GetMonitorName"]
258    pub fn get_monitor_name(monitor: c_int) -> *const c_char;
259    /// Set clipboard text content
260    #[link_name = "SetClipboardText"]
261    pub fn set_clipboard_text(text: *const c_char);
262    /// Get clipboard text content
263    #[link_name = "GetClipboardText"]
264    pub fn get_clipboard_text() -> *const c_char;
265    /// Get clipboard image content
266    #[link_name = "GetClipboardImage"]
267    pub fn get_clipboard_image() -> Image;
268    /// Enable waiting for events on [end_drawing], no automatic event polling
269    #[link_name = "EnableEventWaiting"]
270    pub fn enable_event_waiting();
271    /// Disable waiting for events on [end_drawing], automatic event polling
272    #[link_name = "DisableEventWaiting"]
273    pub fn disable_event_waiting();
274}
275
276// Cursor-related functions
277unsafe extern "C" {
278    /// Shows cursor
279    #[link_name = "ShowCursor"]
280    pub fn show_cursor();
281    /// Hides cursor
282    #[link_name = "HideCursor"]
283    pub fn hide_cursor();
284    /// Check if cursor is not visible
285    #[link_name = "IsCursorHidden"]
286    pub fn is_cursor_hidden();
287    /// Enables cursor (unlock cursor)
288    #[link_name = "EnableCursor"]
289    pub fn enable_cursor();
290    /// Disabled cursor (lock cursor)
291    #[link_name = "DisableCursor"]
292    pub fn disable_cursor();
293    /// Check if cursor is on the screen
294    #[link_name = "IsCursorOnScreen"]
295    pub fn is_cursor_on_screen();
296}
297
298// Drawing related functions
299unsafe extern "C" {
300    /// Set background color (framebuffer clear color)
301    #[link_name = "ClearBackground"]
302    pub fn clear_background(color: Color);
303    /// Setup canvas (framebuffer) to start drawing
304    #[link_name = "BeginDrawing"]
305    pub fn begin_drawing();
306    /// End canvas drawing and swap buffers (double buffering)
307    #[link_name = "EndDrawing"]
308    pub fn end_drawing();
309    /// Begin drawing to render texture
310    #[link_name = "BeginTextureMode"]
311    pub fn begin_texture_mode(render_texture: RenderTexture2D);
312    /// Ends drawing to render texture
313    #[link_name = "EndTextureMode"]
314    pub fn end_texture_mode();
315}
316
317// Texture loading functions
318// Note: These function require GPU access
319unsafe extern "C" {
320    /// Load texture from file into GPU memory (VRAM)
321    #[link_name = "LoadTexture"]
322    pub fn load_texture(path: *const c_char) -> Texture;
323    /// Load texture from image data
324    #[link_name = "LoadTextureFromImage"]
325    pub fn load_texture_from_image(image: Image) -> Texture;
326    /// Load texture for rendering (framebuffer)
327    #[link_name = "LoadRenderTexture"]
328    pub fn load_render_texture(width: c_int, height: c_int) -> RenderTexture;
329    /// Check if a texture is valid (loaded in GPU)
330    #[link_name = "IsTextureValid"]
331    pub fn is_texture_valid(texture: Texture) -> bool;
332    /// Unload texture from GPU memory (VRAM)
333    #[link_name = "UnloadTexture"]
334    pub fn unload_texture(texture: Texture);
335    /// Check if a render texture is valid (loaded in GPU)
336    #[link_name = "IsRenderTextureValid"]
337    pub fn is_render_texture_valid(target: RenderTexture) -> bool;
338    /// Unload render texture from GPU memory (VRAM)
339    #[link_name = "UnloadRenderTexture"]
340    pub fn unload_render_texture(render_texture: RenderTexture);
341    /// Update GPU texture with new data
342    #[link_name = "UpdateTexture"]
343    pub fn update_texture(texture: Texture, pixels: *const c_void);
344    /// Update GPU texture rectangle with new data
345    #[link_name = "UpdateTextureRec"]
346    pub fn update_texture_rec(texture: Texture, rec: Rectangle, pixels: *const c_void);
347}
348
349// Texture configuration function
350unsafe extern "C" {
351    /// Generate GPU mipmaps for a texture
352    #[link_name = "GenTextureMipmaps"]
353    pub fn gen_texture_mipmaps(texture: *mut Texture);
354    /// Set texture scaling filter mode
355    #[link_name = "SetTextureFilter"]
356    pub fn set_texture_filter(texture: Texture, filter: c_int);
357    /// Set texture wrapping mode
358    #[link_name = "SetTextureWrap"]
359    pub fn set_texture_wrap(texture: Texture, wrap: c_int);
360}
361
362// Texture drawing functions
363unsafe extern "C" {
364    /// Draw a [Texture]
365    #[link_name = "DrawTexture"]
366    pub fn draw_texture(texture: Texture, pos_x: c_int, pos_y: c_int, tint: Color);
367    /// Draw a [Texture] with position defined as [Vector2]
368    #[link_name = "DrawTextureV"]
369    pub fn draw_texture_v(texture: Texture, pos: Vector2, tint: Color);
370    /// Draw a [Texture] with extended parameters
371    #[link_name = "DrawTextureEx"]
372    pub fn draw_texture_ex(
373        texture: Texture,
374        pos: Vector2,
375        rotation: c_float,
376        scale: c_float,
377        tint: Color,
378    );
379    /// Draw a part of a [Texture] defined by a [Rectangle]
380    #[link_name = "DrawTextureRec"]
381    pub fn draw_texture_rec(texture: Texture, source: Rectangle, position: Vector2, tint: Color);
382    /// Draw a part of a [Texture] defined by a [Rectangle] with 'pro' parameters
383    #[link_name = "DrawTexturePro"]
384    pub fn draw_texture_pro(
385        texture: Texture,
386        source: Rectangle,
387        dest: Rectangle,
388        origin: Vector2,
389        rotation: c_float,
390        tint: Color,
391    );
392    // TODO: Add draw_texture_npatch when NPatchInfo is implemented
393}
394
395// Text drawing functions
396unsafe extern "C" {
397    /// Draw current FPS
398    #[link_name = "DrawFPS"]
399    pub fn draw_fps(pos_x: c_int, pos_y: c_int);
400    /// Draw text (using default font)
401    #[link_name = "DrawText"]
402    pub fn draw_text(
403        text: *const c_char,
404        pos_x: c_int,
405        pos_y: c_int,
406        font_size: c_int,
407        color: Color,
408    );
409}
410
411// Text font info functions
412unsafe extern "C" {
413    /// Measure string width for default font
414    #[link_name = "MeasureText"]
415    pub fn measure_text(text: *const c_char, font_size: c_int);
416}
417
418// Basic shapes drawing functions
419unsafe extern "C" {
420    /// Draw a color-filled rectangle
421    #[link_name = "DrawRectangleRec"]
422    pub fn draw_rectangle_rec(rec: Rectangle, color: Color);
423}
424
425// Input-related functions: mouse
426unsafe extern "C" {
427    /// Check if a mouse button is beening pressed
428    #[link_name = "IsMouseButtonDown"]
429    pub fn is_mouse_button_down(button: MouseButton) -> bool;
430    /// Get mouse position X
431    #[link_name = "GetMouseX"]
432    pub fn get_mouse_x() -> c_int;
433    /// Get mouse position Y
434    #[link_name = "GetMouseY"]
435    pub fn get_mouse_y() -> c_int;
436    /// Get mouse position XY
437    #[link_name = "GetMousePosition"]
438    pub fn get_mouse_position() -> Vector2;
439}
440
441// Timing-related functions
442unsafe extern "C" {
443    /// Set target FPS (maximum)
444    #[link_name = "SetTargetFPS"]
445    pub fn set_target_fps(fps: c_int);
446    /// Get time in seconds for last frame drawn (delta time)
447    #[link_name = "GetFrameTime"]
448    pub fn get_frame_time() -> c_float;
449    /// Get elapsed time in seconds since InitWindow()
450    #[link_name = "GetTime"]
451    pub fn get_time() -> c_double;
452    /// Get current FPS
453    #[link_name = "GetFPS"]
454    pub fn get_fps() -> c_int;
455}
456
457// Misc functions
458unsafe extern "C" {
459    /// Setup init configuration flags
460    ///
461    /// Flags should be of values defined in [ConfigFlag].
462    ///
463    /// Use this value as a bitmask.
464    ///
465    /// # Examples
466    /// ```no_run
467    /// use rustyray_sys::{ffi::set_config_flags, consts::ConfigFlag};
468    ///
469    /// unsafe { set_config_flags(ConfigFlag::VsyncHint | ConfigFlag::FullscreenMode) }
470    /// ```
471    #[link_name = "SetConfigFlags"]
472    pub fn set_config_flags(flags: ConfigFlag);
473}
474
475// Audio device management functions
476unsafe extern "C" {
477    /// Initialize audio device and context
478    #[link_name = "InitAudioDevice"]
479    pub fn init_audio_device();
480    /// Close the audio device and context
481    #[link_name = "CloseAudioDevice"]
482    pub fn close_audio_device();
483    /// Check if audio device has been initialized successfully
484    #[link_name = "IsAudioDeviceReady"]
485    pub fn is_audio_device_ready() -> bool;
486    /// Set master volume (listener)
487    #[link_name = "SetMasterVolume"]
488    pub fn set_master_volume(volume: c_float);
489    /// Get master volume (listener)
490    #[link_name = "GetMasterVolume"]
491    pub fn get_master_volume() -> c_float;
492}