### Query Monitor Information and Manage Windows with RGFW (C) Source: https://context7.com/colleagueriley/rgfw/llms.txt This C code snippet demonstrates how to use the RGFW library to retrieve detailed information about all connected monitors, including their names, positions, resolutions, physical dimensions, and scaling factors. It also shows how to identify the primary monitor, get the monitor a specific window is on, list available display modes, and move a window to a different monitor. The example includes basic window creation and event polling. ```c #define RGFW_IMPLEMENTATION #include "RGFW.h" #include int main(void) { RGFW_window* win = RGFW_createWindow("Monitor Info", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowScaleToMonitor); // Get all available monitors size_t monitorCount; RGFW_monitor** monitors = RGFW_getMonitors(&monitorCount); printf("Found %zu monitors:\n", monitorCount); for (size_t i = 0; i < monitorCount; i++) { RGFW_monitor* mon = monitors[i]; printf("\nMonitor %zu: %s\n", i, mon->name); printf(" Position: (%d, %d)\n", mon->x, mon->y); printf(" Resolution: %dx%d\n", mon->mode.w, mon->mode.h); printf(" Refresh rate: %.2f Hz\n", mon->mode.refreshRate); printf(" Physical size: %.1f\" x %.1f\"\n", mon->physW, mon->physH); printf(" Scale: %.2fx%.2f\n", mon->scaleX, mon->scaleY); printf(" Pixel ratio: %.2f\n", mon->pixelRatio); // Get workarea (excluding taskbar) i32 workX, workY, workW, workH; if (RGFW_monitor_getWorkarea(mon, &workX, &workY, &workW, &workH)) { printf(" Work area: (%d, %d) %dx%d\n", workX, workY, workW, workH); } } // Get primary monitor RGFW_monitor* primary = RGFW_getPrimaryMonitor(); printf("\nPrimary monitor: %s\n", primary->name); // Get the monitor the window is currently on RGFW_monitor* windowMonitor = RGFW_window_getMonitor(win); printf("Window is on: %s\n", windowMonitor->name); // Get available modes for the primary monitor size_t modeCount; RGFW_monitorMode* modes = RGFW_monitor_getModes(primary, &modeCount); printf("\nAvailable modes for %s:\n", primary->name); for (size_t i = 0; i < modeCount && i < 5; i++) { printf(" %dx%d @ %.2f Hz\n", modes[i].w, modes[i].h, modes[i].refreshRate); } RGFW_freeModes(modes); // Example: Move window to second monitor if available if (monitorCount > 1) { printf("\nMoving window to monitor 1...\n"); RGFW_window_moveToMonitor(win, monitors[1]); } while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_pollEvents(); } RGFW_window_close(win); return 0; } ``` -------------------------------- ### Vulkan Surface Creation with RGFW Source: https://context7.com/colleagueriley/rgfw/llms.txt Demonstrates how to create a Vulkan-compatible window and surface using RGFW when the RGFW_VULKAN flag is defined. It includes simplified Vulkan instance creation and highlights the use of RGFW_VK_SURFACE for platform-specific extensions. This example assumes Vulkan headers are available. ```c #define RGFW_VULKAN #define RGFW_IMPLEMENTATION #include "RGFW.h" #include #include int main(void) { // Create window without OpenGL (Vulkan will handle rendering) RGFW_window* win = RGFW_createWindow("Vulkan Demo", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowAllowDND); RGFW_window_setExitKey(win, RGFW_escape); // Vulkan instance creation (simplified) VkApplicationInfo appInfo = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pApplicationName = "RGFW Vulkan", .applicationVersion = VK_MAKE_VERSION(1, 0, 0), .pEngineName = "No Engine", .engineVersion = VK_MAKE_VERSION(1, 0, 0), .apiVersion = VK_API_VERSION_1_0 }; // Required extensions for surface creation // RGFW_VK_SURFACE provides the platform-specific surface extension name const char* extensions[] = { VK_KHR_SURFACE_EXTENSION_NAME, RGFW_VK_SURFACE // Platform-specific: VK_KHR_xlib_surface, VK_KHR_win32_surface, etc. }; VkInstanceCreateInfo createInfo = { .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &appInfo, .enabledExtensionCount = 2, .ppEnabledExtensionNames = extensions }; VkInstance instance; if (vkCreateInstance(&createInfo, NULL, &instance) != VK_SUCCESS) { printf("Failed to create Vulkan instance\n"); return -1; } // Create Vulkan surface using RGFW helper // VkSurfaceKHR surface = RGFW_window_createSurface_Vulkan(win, instance); // Main loop while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_event event; while (RGFW_window_checkEvent(win, &event)) { if (event.type == RGFW_quit) break; if (event.type == RGFW_windowResized) { // Handle swapchain recreation on resize printf("Window resized - recreate swapchain\n"); } } // Vulkan rendering here... } // Cleanup // vkDestroySurfaceKHR(instance, surface, NULL); vkDestroyInstance(instance, NULL); RGFW_window_close(win); return 0; } ``` -------------------------------- ### Manage Example Execution with JavaScript Source: https://github.com/colleagueriley/rgfw/blob/main/wasm/events.html This JavaScript code dynamically manages the loading and unloading of example scripts based on the presence of a 'Component' element. It ensures that specific example scripts are removed if a custom component is not found, preventing potential conflicts or unnecessary script execution. ```javascript document.addEventListener("DOMContentLoaded", function (event) { if (document.querySelector('Component') == null) { var script = document.querySelector('script[src="../examples/event_queue/event_queue.js"]') document.head.removeChild(script) } }); document.addEventListener("DOMContentLoaded", function (event) { if (document.querySelector('Component') == null) { var script = document.querySelector('script[src="../event_queue.js"]') document.head.removeChild(script) } }); ``` -------------------------------- ### Initialize WebGPU in JavaScript Source: https://github.com/colleagueriley/rgfw/blob/main/wasm/webgpu.html This JavaScript code snippet initializes the WebGPU API for the browser. It checks for WebGPU support, requests an adapter and device, and sets the device in the RGFW Module. It includes error handling for unsupported browsers and lost devices. ```javascript var Module = { print: (function() { return (text) => {console.log(text); }; })(), canvas: (function() { return document.getElementById('canvas'); })(), }; initWebGPU = async () => { // Check to ensure the user agent supports WebGPU if (!('gpu' in navigator)) { const msg = '⚠️ WebGPU is not available on this browser.'; const pre = document.createElement('pre'); pre.style.color = '#f00'; pre.style.textAlign = 'center'; pre.textContent = msg; document.body.appendChild(pre); console.error(msg); return false; } // Request an adapter const adapter = await navigator.gpu.requestAdapter(); if (!adapter) { console.error('No WebGPU adapters found.'); return false; } // Request a device const device = await adapter.requestDevice(); device.lost.then((info) => { console.error(`WebGPU device was lost: ${info.message}`); device = null; if (info.reason != 'destroyed') { initWebGPU(); } }); // Set WebGPU device in Module Module.preinitializedWebGPUDevice = device; return true; } initWebGPU(); document.addEventListener("DOMContentLoaded", function (event) { if (document.querySelector('Component') == null) { var script = document.querySelector('script[src="../examples/webgpu/webgpu.js"]') document.head.removeChild(script) } }); document.addEventListener("DOMContentLoaded", function (event) { if (document.querySelector('Component') == null) { var script = document.querySelector('script[src="../webgpu.js"]') document.head.removeChild(script) } }); ``` -------------------------------- ### Simple RGFW Window with OpenGL Example (C) Source: https://github.com/colleagueriley/rgfw/blob/main/README.md This C code snippet demonstrates how to create a basic window using the RGFW library, handle keyboard and mouse events, and perform simple OpenGL rendering. It requires the RGFW library and OpenGL headers. The function `keyfunc` handles keyboard input, and the main loop processes events and renders a triangle. ```c #define RGFW_IMPLEMENTATION #define RGFW_OPENGL /* if this line is not added, OpenGL functions will not be included */ #include "RGFW.h" #include #ifdef RGFW_MACOS #include /* why does macOS do this */ #else #include #endif void keyfunc(RGFW_window* win, RGFW_key key, RGFW_keymod keyMod, RGFW_bool repeat, RGFW_bool pressed) { RGFW_UNUSED(repeat); if (key == RGFW_escape && pressed) { RGFW_window_setShouldClose(win, 1); } } int main() { /* the RGFW_windowOpenGL flag tells it to create an OpenGL context, but you can also create your own with RGFW_window_createContext_OpenGL */ RGFW_window* win = RGFW_createWindow("a window", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowNoResize | RGFW_windowOpenGL); RGFW_setKeyCallback(keyfunc); // you can use callbacks like this if you want while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_event event; while (RGFW_window_checkEvent(win, &event)) { // or RGFW_pollEvents(); if you only want callbacks // you can either check the current event yourself if (event.type == RGFW_quit) break; i32 mouseX, mouseY; RGFW_window_getMouse(win, &mouseX, &mouseY); if (event.type == RGFW_mouseButtonPressed && event.button.value == RGFW_mouseLeft) { printf("You clicked at x: %d, y: %d\n", mouseX, mouseY); } // or use the existing functions if (RGFW_isMousePressed(RGFW_mouseRight)) { printf("The right mouse button was clicked at x: %d, y: %d\n", mouseX, mouseY); } } // OpenGL 1.1 is used here for a simple example, but you can use any version you want (if you request it first (see gl33/gl33.c)) glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.6f, -0.75f); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.6f, -0.75f); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.0f, 0.75f); glEnd(); RGFW_window_swapBuffers_OpenGL(win); glFlush(); } RGFW_window_close(win); return 0; } ``` -------------------------------- ### Create Window with OpenGL Context using RGFW Source: https://context7.com/colleagueriley/rgfw/llms.txt Demonstrates how to create a window with specified dimensions, position, and feature flags, including an OpenGL context. It also shows how to set an exit key and implement a basic rendering loop. This example requires the RGFW library and platform-specific OpenGL libraries for compilation. ```c #define RGFW_IMPLEMENTATION #define RGFW_OPENGL #include "RGFW.h" #ifdef RGFW_MACOS #include #else #include #endif int main(void) { // Create a centered 800x600 window with OpenGL context RGFW_window* win = RGFW_createWindow( "My Application", // Window title 0, 0, // x, y position (ignored with RGFW_windowCenter) 800, 600, // width, height RGFW_windowCenter | RGFW_windowOpenGL | RGFW_windowNoResize ); if (win == NULL) { printf("Failed to create window\n"); return -1; } // Set escape key to close window RGFW_window_setExitKey(win, RGFW_escape); // Main loop while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_pollEvents(); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); RGFW_window_swapBuffers_OpenGL(win); } RGFW_window_close(win); return 0; } // Compile: gcc main.c -lX11 -lGL -lXrandr (Linux) // Compile: gcc main.c -lopengl32 -lgdi32 (Windows) // Compile: gcc main.c -framework Cocoa -framework OpenGL (macOS) ``` -------------------------------- ### RGFW Compilation Commands (Shell) Source: https://github.com/colleagueriley/rgfw/blob/main/README.md These shell commands provide the necessary instructions to compile the RGFW C example on Linux, Windows, and macOS. They specify the compiler (gcc) and the required libraries for each operating system, including OpenGL and windowing system dependencies. ```sh linux : gcc main.c -lX11 -lGL -lXrandr windows : gcc main.c -lopengl32 -lgdi32 macos : gcc main.c -framework Cocoa -framework OpenGL ``` -------------------------------- ### RGFW Window Management Example (C) Source: https://context7.com/colleagueriley/rgfw/llms.txt This C code snippet demonstrates how to use RGFW functions to manage a window. It covers toggling fullscreen, maximizing, minimizing, restoring, centering, showing/hiding, toggling borders, setting always-on-top, moving, resizing, setting size constraints, aspect ratio, and opacity. It requires the RGFW library. ```c #define RGFW_IMPLEMENTATION #include "RGFW.h" int main(void) { RGFW_window* win = RGFW_createWindow("Window Management", 0, 0, 800, 600, RGFW_windowCenter); RGFW_window_setExitKey(win, RGFW_escape); while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_event event; while (RGFW_window_checkEvent(win, &event)) { if (event.type == RGFW_keyPressed) { switch (event.key.value) { case RGFW_f: // Toggle fullscreen if (RGFW_window_isFullscreen(win)) { RGFW_window_setFullscreen(win, RGFW_FALSE); } else { RGFW_window_setFullscreen(win, RGFW_TRUE); } break; case RGFW_m: // Maximize window RGFW_window_maximize(win); break; case RGFW_n: // Minimize window RGFW_window_minimize(win); break; case RGFW_r: // Restore window from minimized/maximized RGFW_window_restore(win); break; case RGFW_c: // Center window on screen RGFW_window_center(win); break; case RGFW_h: // Toggle window visibility if (RGFW_window_isHidden(win)) { RGFW_window_show(win); } else { RGFW_window_hide(win); } break; case RGFW_b: // Toggle border RGFW_window_setBorder(win, !RGFW_window_borderless(win)); break; case RGFW_t: // Toggle always-on-top (floating) RGFW_window_setFloating(win, !RGFW_window_isFloating(win)); break; case RGFW_1: // Move window RGFW_window_move(win, 100, 100); break; case RGFW_2: // Resize window RGFW_window_resize(win, 1024, 768); break; case RGFW_3: // Set min/max size constraints RGFW_window_setMinSize(win, 400, 300); RGFW_window_setMaxSize(win, 1920, 1080); break; case RGFW_4: // Set aspect ratio (16:9) RGFW_window_setAspectRatio(win, 16, 9); break; case RGFW_5: // Set window opacity (50%) RGFW_window_setOpacity(win, 128); break; } } } } RGFW_window_close(win); return 0; } ``` -------------------------------- ### Setup OpenGL 3.3 Context with RGFW Source: https://context7.com/colleagueriley/rgfw/llms.txt Configures and creates an OpenGL 3.3 context using RGFW. It allows setting major/minor versions, profile, double buffering, depth/stencil buffers, MSAA samples, and sRGB color space. Requires an OpenGL loader like GLAD or GLEW. ```c #define RGFW_IMPLEMENTATION #define RGFW_OPENGL #include "RGFW.h" // OpenGL loader (use your preferred loader like GLAD, GLEW, or rglLoad) #include // #include "glad.h" int main(void) { // Configure OpenGL hints before window creation RGFW_glHints* hints = RGFW_getGlobalHints_OpenGL(); hints->major = 3; hints->minor = 3; hints->profile = RGFW_glCore; // Core profile hints->doubleBuffer = 1; // Enable double buffering hints->depth = 24; // 24-bit depth buffer hints->stencil = 8; // 8-bit stencil buffer hints->samples = 4; // 4x MSAA hints->sRGB = RGFW_TRUE; // sRGB color space RGFW_setGlobalHints_OpenGL(hints); // Create window with OpenGL context RGFW_window* win = RGFW_createWindow("OpenGL 3.3", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowOpenGL); if (win == NULL) { printf("Failed to create window\n"); return -1; } RGFW_window_setExitKey(win, RGFW_escape); RGFW_window_makeCurrentContext_OpenGL(win); // Load OpenGL functions (platform-specific) // gladLoadGL(); // or use RGFW_getProcAddress_OpenGL for manual loading // Enable vsync RGFW_window_swapInterval_OpenGL(win, 1); // Check for extensions if (RGFW_extensionSupported_OpenGL("GL_ARB_debug_output", 19)) { printf("Debug output extension supported\n"); } // Render loop while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_pollEvents(); glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Your OpenGL rendering here... RGFW_window_swapBuffers_OpenGL(win); } RGFW_window_close(win); return 0; } ``` -------------------------------- ### Raw Mouse Input and Capture with RGFW Source: https://context7.com/colleagueriley/rgfw/llms.txt Illustrates how to enable and manage raw mouse input and mouse capture features in RGFW. This allows for precise, unaccelerated mouse movement and locking the cursor to the window, essential for many games. The example shows toggling these modes and processing raw movement data. ```c #define RGFW_IMPLEMENTATION #include "RGFW.h" #include int main(void) { // Create window with raw mouse mode enabled RGFW_window* win = RGFW_createWindow("Raw Mouse Input", 0, 0, 800, 600, RGFW_windowCenter); RGFW_window_setExitKey(win, RGFW_escape); RGFW_bool rawMode = RGFW_FALSE; RGFW_bool captured = RGFW_FALSE; printf("Press R to toggle raw mouse mode\n"); printf("Press C to toggle mouse capture\n"); printf("Press B to toggle both (capture + raw)\n"); while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_event event; while (RGFW_window_checkEvent(win, &event)) { if (event.type == RGFW_keyPressed) { if (event.key.value == RGFW_r) { rawMode = !rawMode; RGFW_window_setRawMouseMode(win, rawMode); printf("Raw mouse mode: %s\n", rawMode ? "ON" : "OFF"); } else if (event.key.value == RGFW_c) { captured = !captured; RGFW_window_captureMouse(win, captured); printf("Mouse capture: %s\n", captured ? "ON" : "OFF"); } else if (event.key.value == RGFW_b) { // Toggle both capture and raw mode together RGFW_bool state = !RGFW_window_isCaptured(win); RGFW_window_captureRawMouse(win, state); printf("Capture + Raw mode: %s\n", state ? "ON" : "OFF"); } } // Raw mouse movement is provided in the mouse event if (event.type == RGFW_mousePosChanged) { if (RGFW_window_isRawMouseMode(win)) { // vecX/vecY contain raw delta movement printf("Raw delta: (%.2f, %.2f)\n", event.mouse.vecX, event.mouse.vecY); } else { // Regular mouse position printf("Mouse pos: (%d, %d)\n", event.mouse.x, event.mouse.y); } } } // Alternative: get mouse movement vector directly float vecX, vecY; RGFW_getMouseVector(&vecX, &vecY); if (vecX != 0 || vecY != 0) { // Movement this frame } } RGFW_window_close(win); return 0; } ``` -------------------------------- ### Remove Script on Component Null (JavaScript) Source: https://github.com/colleagueriley/rgfw/blob/main/wasm/gl11.html This JavaScript code snippet removes a specific script tag from the DOM if a 'Component' element is not found. It's designed to conditionally load or unload scripts based on the presence of a component. ```javascript document.addEventListener("DOMContentLoaded", function (event) { if (document.querySelector('Component') == null) { var script = document.querySelector('script\[src="../examples/gl11/gl11.js"]') document.head.removeChild(script) } }); document.addEventListener("DOMContentLoaded", function (event) { if (document.querySelector('Component') == null) { var script = document.querySelector('script\[src="../gl11.js"]') document.head.removeChild(script) } }); ``` -------------------------------- ### DOM Content Loaded Event Listener - JavaScript Source: https://github.com/colleagueriley/rgfw/blob/main/wasm/gamepad.html This JavaScript code snippet listens for the DOMContentLoaded event. It checks for the presence of a 'Component' element and conditionally removes a script tag from the document's head if 'Component' is not found. This is likely used for managing script loading based on page structure. ```javascript document.addEventListener("DOMContentLoaded", function (event) { if (document.querySelector('Component') == null) { var script = document.querySelector('script[src="../examples/gamepad/gamepad.js"]') document.head.removeChild(script) } }); document.addEventListener("DOMContentLoaded", function (event) { if (document.querySelector('Component') == null) { var script = document.querySelector('script[src="../gamepad.js"]') document.head.removeChild(script) } }); ``` -------------------------------- ### Custom Console Log Implementation - JavaScript Source: https://github.com/colleagueriley/rgfw/blob/main/wasm/gamepad.html This JavaScript code overrides the native console.log function to also display messages in a specific HTML element with the ID 'log'. It handles object stringification for better readability and automatically scrolls the log container to the bottom. This is useful for debugging and logging within a web application. ```javascript (function () { var logContainer = document.getElementById('log'); var originalLog = console.log; console.log = function (message) { if (typeof message === 'object') { message = JSON.stringify(message, null, 2); } logContainer.innerHTML += message + '\n'; logContainer.scrollTop = logContainer.scrollHeight; // Auto-scroll to the bottom originalLog.apply(console, arguments); }; })(); ``` -------------------------------- ### Remove Script if Component Not Found (JavaScript) Source: https://github.com/colleagueriley/rgfw/blob/main/wasm/silk.html This JavaScript code snippet listens for the DOMContentLoaded event. If a 'Component' element is not found on the page, it removes a specific script tag from the document's head. This is likely used to conditionally load or unload JavaScript resources based on the presence of a UI component. ```javascript document.addEventListener("DOMContentLoaded", function (event) { if (document.querySelector('Component') == null) { var script = document.querySelector('script[src="../examples/silk/silk.js"]') document.head.removeChild(script) } }); document.addEventListener("DOMContentLoaded", function (event) { if (document.querySelector('Component') == null) { var script = document.querySelector('script[src="../silk.js"]') document.head.removeChild(script) } }); ``` -------------------------------- ### RGFW Input State Checking in C Source: https://context7.com/colleagueriley/rgfw/llms.txt Demonstrates how to directly query the state of keyboard keys and mouse buttons in RGFW without relying on event callbacks. This method allows checking if a key was pressed, released, or is currently held down, as well as mouse button states and scroll wheel activity. It also shows how to get the mouse position and check window-specific input states. ```c #define RGFW_IMPLEMENTATION #include "RGFW.h" #include int main(void) { RGFW_window* win = RGFW_createWindow("Input State Demo", 0, 0, 800, 600, RGFW_windowCenter); RGFW_window_setExitKey(win, RGFW_escape); while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_pollEvents(); // Global input state (any window) if (RGFW_isKeyPressed(RGFW_space)) { printf("Space key was just pressed this frame\n"); } if (RGFW_isKeyDown(RGFW_w)) { printf("W key is being held down\n"); } if (RGFW_isKeyReleased(RGFW_enter)) { printf("Enter key was just released this frame\n"); } // Mouse state if (RGFW_isMousePressed(RGFW_mouseLeft)) { printf("Left mouse button just pressed\n"); } if (RGFW_isMouseDown(RGFW_mouseRight)) { printf("Right mouse button is held\n"); } // Get mouse scroll float scrollX, scrollY; RGFW_getMouseScroll(&scrollX, &scrollY); if (scrollY != 0) { printf("Scroll: %.2f\n", scrollY); } // Window-specific input state (only when window has focus) if (RGFW_window_isKeyPressed(win, RGFW_a)) { printf("A pressed while window focused\n"); } if (RGFW_window_isMouseDown(win, RGFW_mouseMiddle)) { printf("Middle mouse held while window focused\n"); } // Get mouse position i32 mouseX, mouseY; RGFW_window_getMouse(win, &mouseX, &mouseY); } RGFW_window_close(win); return 0; } ``` -------------------------------- ### C: Implement RGFW Debug Callback for Error Handling Source: https://context7.com/colleagueriley/rgfw/llms.txt This C code snippet demonstrates how to set up and use a custom debug callback function in RGFW. The callback handles different debug message types (Error, Warning, Info) and specific error codes, printing formatted messages to the console. It also shows the basic setup for creating a window and integrating the debug callback. ```c #define RGFW_DEBUG #define RGFW_IMPLEMENTATION #include "RGFW.h" #include void debugCallback(RGFW_debugType type, RGFW_errorCode err, const char* msg) { const char* typeStr; switch (type) { case RGFW_typeError: typeStr = "ERROR"; break; case RGFW_typeWarning: typeStr = "WARNING"; break; case RGFW_typeInfo: typeStr = "INFO"; break; default: typeStr = "UNKNOWN"; break; } printf("[RGFW %s] Code %d: %s\n", typeStr, err, msg); // Handle specific errors switch (err) { case RGFW_errOpenGLContext: printf(" -> OpenGL context creation failed\n"); break; case RGFW_errOutOfMemory: printf(" -> Out of memory - consider freeing resources\n"); break; case RGFW_errClipboard: printf(" -> Clipboard operation failed\n"); break; default: break; } } int main(void) { // Set debug callback before creating window RGFW_setDebugCallback(debugCallback); RGFW_window* win = RGFW_createWindow("Debug Demo", 0, 0, 800, 600, RGFW_windowCenter); if (win == NULL) { printf("Window creation failed!\n"); return -1; } RGFW_window_setExitKey(win, RGFW_escape); // Manually send debug info RGFW_sendDebugInfo(RGFW_typeInfo, RGFW_noError, "Application started successfully"); while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_pollEvents(); } RGFW_sendDebugInfo(RGFW_typeInfo, RGFW_noError, "Application shutting down"); RGFW_window_close(win); return 0; } ``` -------------------------------- ### Handle RGFW Events in C Source: https://context7.com/colleagueriley/rgfw/llms.txt This C code snippet demonstrates how to set up an RGFW window and process various events within an event loop. It includes handling window creation, closing, keyboard presses/releases, mouse button actions, mouse movement, scrolling, window resizing/moving, focus changes, and file drag-and-drop operations. The code relies on the RGFW library. ```c #define RGFW_IMPLEMENTATION #include "RGFW.h" #include int main(void) { RGFW_window* win = RGFW_createWindow("Event Demo", 0, 0, 640, 480, RGFW_windowCenter | RGFW_windowAllowDND); RGFW_window_setExitKey(win, RGFW_escape); RGFW_event event; while (RGFW_window_shouldClose(win) == RGFW_FALSE) { // Poll all pending events while (RGFW_window_checkEvent(win, &event)) { switch (event.type) { case RGFW_quit: printf("Window close requested\n"); break; case RGFW_keyPressed: printf("Key pressed: %d (repeat: %s)", event.key.value, event.key.repeat ? "yes" : "no"); break; case RGFW_keyReleased: printf("Key released: %d\n", event.key.value); break; case RGFW_mouseButtonPressed: printf("Mouse button pressed: %d\n", event.button.value); break; case RGFW_mouseButtonReleased: printf("Mouse button released: %d\n", event.button.value); break; case RGFW_mousePosChanged: printf("Mouse moved to: %d, %d\n", event.mouse.x, event.mouse.y); break; case RGFW_mouseScroll: printf("Mouse scroll: %.2f, %.2f\n", event.scroll.x, event.scroll.y); break; case RGFW_windowResized: printf("Window resized\n"); break; case RGFW_windowMoved: printf("Window moved\n"); break; case RGFW_focusIn: printf("Window gained focus\n"); break; case RGFW_focusOut: printf("Window lost focus\n"); break; case RGFW_dataDrop: printf("Files dropped: %zu files\n", event.drop.count); for (size_t i = 0; i < event.drop.count; i++) { printf(" - %s\n", event.drop.files[i]); } break; default: break; } } } RGFW_window_close(win); return 0; } ``` -------------------------------- ### RGFW Callback-Based Event Handling in C Source: https://context7.com/colleagueriley/rgfw/llms.txt Demonstrates how to set up and use callback functions in RGFW for handling keyboard, mouse, window resize, focus, and file drop events. Callbacks are registered globally and receive window and event-specific data. This approach separates event logic from the main application loop. ```c #define RGFW_IMPLEMENTATION #include "RGFW.h" #include RGFW_window* mainWindow; void onKeyPressed(RGFW_window* win, u8 key, RGFW_keymod mod, RGFW_bool repeat, RGFW_bool pressed) { if (win != mainWindow) return; if (pressed) { printf("Key pressed: %d, modifiers: %d, repeat: %s\n", key, mod, repeat ? "yes" : "no"); // Convert physical key to mapped key (keyboard layout aware) RGFW_key mapped = RGFW_physicalToMappedKey(key); printf("Mapped key: %d (%c)\n", mapped, mapped); } else { printf("Key released: %d\n", key); } } void onMouseButton(RGFW_window* win, RGFW_mouseButton button, RGFW_bool pressed) { if (win != mainWindow) return; const char* buttons[] = {"Left", "Middle", "Right", "Misc1", "Misc2"}; printf("Mouse %s: %s\n", buttons[button], pressed ? "pressed" : "released"); } void onMouseMove(RGFW_window* win, i32 x, i32 y, float vecX, float vecY) { if (win != mainWindow) return; printf("Mouse at (%d, %d), delta: (%.2f, %.2f)\n", x, y, vecX, vecY); } void onWindowResize(RGFW_window* win, i32 w, i32 h) { if (win != mainWindow) return; printf("Window resized to %dx%d\n", w, h); } void onFocus(RGFW_window* win, RGFW_bool inFocus) { if (win != mainWindow) return; printf("Window %s focus\n", inFocus ? "gained" : "lost"); } void onFileDrop(RGFW_window* win, char** files, size_t count) { if (win != mainWindow) return; printf("Dropped %zu files:\n", count); for (size_t i = 0; i < count; i++) { printf(" %s\n", files[i]); } } int main(void) { mainWindow = RGFW_createWindow("Callbacks Demo", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowAllowDND); RGFW_window_setExitKey(mainWindow, RGFW_escape); // Register callbacks RGFW_setKeyCallback(onKeyPressed); RGFW_setMouseButtonCallback(onMouseButton); RGFW_setMousePosCallback(onMouseMove); RGFW_setWindowResizedCallback(onWindowResize); RGFW_setFocusCallback(onFocus); RGFW_setDataDropCallback(onFileDrop); while (RGFW_window_shouldClose(mainWindow) == RGFW_FALSE) { RGFW_pollEvents(); // Triggers callbacks } RGFW_window_close(mainWindow); return 0; } ``` -------------------------------- ### Software Rendering with RGFW Surfaces Source: https://context7.com/colleagueriley/rgfw/llms.txt Demonstrates software rendering using RGFW's surface API for direct pixel manipulation. Includes functions for clearing the buffer and drawing rectangles, useful for 2D graphics or environments without GPU acceleration. Requires memory allocation for the buffer. ```c #define RGFW_IMPLEMENTATION #include "RGFW.h" #include // Clear buffer to a solid color void clearBuffer(u8* buffer, i32 width, i32 height, u8 r, u8 g, u8 b, u8 a) { for (i32 y = 0; y < height; y++) { for (i32 x = 0; x < width; x++) { u32 index = (y * width + x) * 4; buffer[index + 0] = r; buffer[index + 1] = g; buffer[index + 2] = b; buffer[index + 3] = a; } } } // Draw a filled rectangle void drawRect(u8* buffer, i32 bufferWidth, i32 x, i32 y, i32 w, i32 h, u8 r, u8 g, u8 b, u8 a) { for (i32 py = y; py < y + h; py++) { for (i32 px = x; px < x + w; px++) { u32 index = (py * bufferWidth + px) * 4; buffer[index + 0] = r; buffer[index + 1] = g; buffer[index + 2] = b; buffer[index + 3] = a; } } } int main(void) { RGFW_window* win = RGFW_createWindow("Software Rendering", 0, 0, 640, 480, RGFW_windowCenter | RGFW_windowTransparent); RGFW_window_setExitKey(win, RGFW_escape); // Allocate pixel buffer (RGBA format) i32 width = 640; i32 height = 480; u8* buffer = (u8*)RGFW_ALLOC(width * height * 4); // Create surface from buffer RGFW_surface* surface = RGFW_createSurface(buffer, width, height, RGFW_formatRGBA8); i32 rectX = 100, rectY = 100; while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_event event; while (RGFW_window_checkEvent(win, &event)) { if (event.type == RGFW_quit) break; } // Handle input for movement if (RGFW_isKeyDown(RGFW_left)) rectX -= 5; if (RGFW_isKeyDown(RGFW_right)) rectX += 5; if (RGFW_isKeyDown(RGFW_up)) rectY -= 5; if (RGFW_isKeyDown(RGFW_down)) rectY += 5; // Clear to blue background clearBuffer(buffer, width, height, 50, 50, 200, 255); // Draw moving red rectangle drawRect(buffer, width, rectX, rectY, 100, 100, 255, 0, 0, 255); // Draw green rectangle drawRect(buffer, width, 300, 200, 80, 80, 0, 255, 0, 255); // Blit surface to window RGFW_window_blitSurface(win, surface); } RGFW_surface_free(surface); RGFW_FREE(buffer); RGFW_window_close(win); return 0; } ``` -------------------------------- ### RGFW Clipboard Operations: Copy and Paste Text Source: https://context7.com/colleagueriley/rgfw/llms.txt Demonstrates how to copy text to the system clipboard and read text from it using RGFW functions. Requires RGFW.h and standard C libraries for string manipulation and input/output. It takes no explicit input but responds to 'C' and 'V' key presses with Ctrl modifier. ```c #define RGFW_IMPLEMENTATION #include "RGFW.h" #include #include int main(void) { RGFW_window* win = RGFW_createWindow("Clipboard Demo", 0, 0, 800, 600, RGFW_windowCenter); RGFW_window_setExitKey(win, RGFW_escape); printf("Press C to copy, V to paste\n"); while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_event event; while (RGFW_window_checkEvent(win, &event)) { if (event.type == RGFW_keyPressed) { // Check for Ctrl modifier if (event.key.mod & RGFW_modControl) { if (event.key.value == RGFW_c) { // Copy text to clipboard const char* textToCopy = "Hello from RGFW!"; RGFW_writeClipboard(textToCopy, strlen(textToCopy)); printf("Copied to clipboard: %s\n", textToCopy); } else if (event.key.value == RGFW_v) { // Read text from clipboard size_t size; const char* clipboardText = RGFW_readClipboard(&size); if (clipboardText != NULL && size > 0) { printf("Pasted from clipboard (%zu bytes): %s\n", size, clipboardText); } else { printf("Clipboard is empty\n"); } } } } } } RGFW_window_close(win); return 0; } ``` -------------------------------- ### RGFW Window Icon Setting: Custom Icons Source: https://context7.com/colleagueriley/rgfw/llms.txt Illustrates how to set custom icons for both the window and the taskbar using RGFW. This function requires the RGFW.h header and takes bitmap data, dimensions, and pixel format as input. It allows for detailed control over the window's visual representation. ```c #define RGFW_IMPLEMENTATION #include "RGFW.h" int main(void) { RGFW_window* win = RGFW_createWindow("Window Icon", 0, 0, 800, 600, RGFW_windowCenter); RGFW_window_setExitKey(win, RGFW_escape); // Create a simple 32x32 icon (red square with blue border) u8 iconData[32 * 32 * 4]; for (int y = 0; y < 32; y++) { for (int x = 0; x < 32; x++) { int idx = (y * 32 + x) * 4; // Blue border if (x < 2 || x >= 30 || y < 2 || y >= 30) { iconData[idx + 0] = 0; // R iconData[idx + 1] = 0; // G iconData[idx + 2] = 255; // B iconData[idx + 3] = 255; // A } else { // Red center iconData[idx + 0] = 255; // R iconData[idx + 1] = 0; // G iconData[idx + 2] = 0; // B iconData[idx + 3] = 255; // A } } } // Set icon for both window and taskbar RGFW_window_setIcon(win, iconData, 32, 32, RGFW_formatRGBA8); // Or set icons separately: // RGFW_window_setIconEx(win, iconData, 32, 32, RGFW_formatRGBA8, RGFW_iconWindow); // RGFW_window_setIconEx(win, iconData, 32, 32, RGFW_formatRGBA8, RGFW_iconTaskbar); // Change window title RGFW_window_setName(win, "Window with Custom Icon"); while (RGFW_window_shouldClose(win) == RGFW_FALSE) { RGFW_pollEvents(); } RGFW_window_close(win); return 0; } ```