Skip to content

Sample raylib but using low level access for cimgui to attach to opengl and window.

Lightnet/raylib_abstraction_cimgui

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

raylib_abstraction_cimgui

License: MIT

Information:

A minimal CMake-based template for integrating raylib (using its low-level rlgl abstraction) with cimgui (C bindings for Dear ImGui) in pure C. This project demonstrates a standalone setup for rendering a rotating 3D cube with an ImGui overlay for interactive controls, without relying on the full raylib high-level API. It's ideal for prototyping tools, games, or apps where you want direct control over OpenGL via rlgl, GLFW for windowing, and raymath for vector/matrix math, all while adding an immediate-mode GUI.This template is inspired by official raylib examples and cimgui backends, adapted for a lightweight, header-only workflow. It supports Windows, Linux, and macOS out-of-the-box via CMake, with static linking options for portability.Features

  • Pure C Implementation: No C++ required—uses cimgui for ImGui bindings.
  • Low-Level Rendering: Leverages rlgl.h for OpenGL 3.3 core profile rendering (e.g., perspective projection, depth testing, matrix transformations).
  • 3D Example: Renders a colorful rotating cube using raymath for matrices and camera setup.
  • ImGui Integration: Overlay with text, sliders (e.g., control cube rotation), and demo windows. Uses GLFW and OpenGL3 backends.
  • Dynamic Resizing: Handles window resizing with viewport updates.
  • Cross-Platform: Builds with CMake; tested on Windows (MinGW/MSVC), Linux (GCC/Clang), macOS.
  • Minimal Dependencies: Fetches raylib and cimgui via FetchContent; links GLFW and OpenGL automatically.
  • Static Linking: Optional full static build on Windows for standalone executables.

Prerequisites:

  • CMake 3.15 or later.

  • A C compiler (GCC, Clang, MSVC).

  • Git (for cloning submodules if building manually).

  • OpenGL development headers (usually provided by your system or via package manager, e.g., libgl1-mesa-dev on Ubuntu).

    No need to pre-install raylib or cimgui—CMake handles fetching them.Building with CMake.

If you encounter issues (e.g., missing GLFW), install system packages:

  • Ubuntu/Debian: sudo apt install libglfw3-dev libgl1-mesa-dev
  • macOS (with Homebrew): brew install glfw glew
  • Windows: Use vcpkg or ensure MinGW includes them.

Usage:

  • Run the executable: A window opens showing a yellow background with a rotating multicolored 3D cube (viewed from a perspective camera at (5,5,5)).
  • ImGui Window: Drag the "Cube Y Rotation" slider to interactively control the cube's rotation (overrides auto-rotation). Text displays the current value.
  • Controls:
    • Mouse: Drag ImGui elements.
    • Keyboard: ESC to close.
    • Resize: Window adapts automatically.
  • Auto-Rotation: The cube spins at 30°/second around the Y-axis unless the slider is active.

The main loop follows the standard ImGui + custom OpenGL flow:

  1. Poll events.
  2. Clear buffers (color + depth).
  3. Start ImGui frame.
  4. Build UI.
  5. Set projection/view/model matrices.
  6. Draw cube via rlBegin(RL_TRIANGLES).
  7. Flush batch with rlDrawRenderBatchActive().
  8. Render ImGui.
  9. Swap buffers.

Project Structure:

raylib_cimgui/
├── CMakeLists.txt      # CMake configuration (fetches raylib/cimgui, builds static cimgui lib)
├── src/
│   └── main.c          # Entry point: GLFW setup, rlgl init, ImGui integration, cube rendering
└── README.md           # This file

Why This Approach?

  • Standalone rlgl: Avoids full raylib overhead; rlgl.h provides OpenGL emulation (fixed-function style in core profile).
  • cimgui for C: Dear ImGui is C++-native, but cimgui generates pure C bindings—perfect for raylib's C ecosystem.
  • Learning Curve: It took time to reconcile rlgl matrices with ImGui's state (e.g., reset glUseProgram(0) before ImGui render). Few pure-C examples exist, so this template fills the gap.
  • Performance Note: cimgui adds minor overhead vs. native C++ ImGui, but it's negligible for tools/prototypes. For high-perf, consider rlImGui (C++ wrapper).

Credits and Inspirations:

Notes:

  • It took a while to learn how raylib and cimgui. Had to search around which there are few projects work on cimgui.

About

Sample raylib but using low level access for cimgui to attach to opengl and window.

Topics

Resources

Stars

Watchers

Forks