rawdraw Graphics Library

repository·master·Indexed 19 days ago

https://github.com/cntools/rawdraw

An OS-agnostic, lightweight graphics library for drawing lines, boxes, text, and bitmaps. It supports a range of environments from high-level desktop applications using OpenGL, Vulkan, or WebAssembly to bare-metal embedded systems using software rasterization. The library can be integrated as a single-file header (STB-style) or a Git submodule.

Tokens
1.6K
Snippets
6
Records
7
Agent score
17%

What's inside rawdraw

  1. Integrate rawdraw into your project

    master

    You can integrate rawdraw using two methods:

    1. Single-file header (STB-style)

    Download rawdraw_sf.h and include it directly in your project. To use this method, you must define CNFG_IMPLEMENTATION in exactly one C file before including the header.

    #define CNFG_IMPLEMENTATION
    #include "rawdraw_sf.h"

    2. Git Submodule

    Add the repository as a submodule to your project:

    git submodule add https://github.com/cntools/rawdraw

    Note: If you encounter a git error, ensure you have run git init in your project root first.

    //Method 1: Single-file header
    #define CNFG_IMPLEMENTATION
    #include "rawdraw_sf.h"
  2. Build rawdraw on Linux

    master

    Prerequisites

    Install the necessary X11 and Mesa development libraries.

    Debian/Ubuntu:

    sudo apt-get update
    sudo apt-get install xorg-dev libx11-dev libxinerama-dev libxext-dev mesa-common-dev libglu1-mesa-dev

    Arch Linux:

    sudo pacman -Sy xorg-server-devel libx11 libxinerama libxext mesa glu

    Compilation

    Use gcc to compile your source file (e.g., simple.c) linking against math, X11, and GL libraries:

    gcc -o simple simple.c -lm -lX11 -lGL
  3. Install prerequisites for rawdrawwasm

    master

    To build or run rawdrawwasm, you must install the following system dependencies via apt-get:

    • binaryen
    • clang
    • llvm
    • lld
    • terser

    Depending on your environment, you may need to manually add the LLVM binaries to your PATH to ensure the build tools are discoverable.

    sudo apt-get install binaryen clang llvm lld terser
    
    # Example: adding LLVM to PATH if necessary
    export PATH=$PATH:/usr/lib/llvm-10/bin
  4. Configure rawdraw backend and features

    master

    Rawdraw uses #define flags to configure its backend and capabilities.

    Core Backend Configurations

    • CNFGOGL: Use OpenGL/OpenGL ES if available.
    • CNFGVK: Use Vulkan if available.
    • CNFGRASTERIZER: Use software rasterization (useful for embedded platforms or when no GPU is available).
    • CNFG3D: Includes CNFG3D rasterized graphics functions (requires CNFGRASTERIZER).

    Platform/Build Configurations

    • __android__: Build for Android (forces CNFGOGL).
    • WINDOWS, WIN32, WIN64: Build for Windows.
    • HAS_XSHAPE: Include X11 on-screen display functionality.
    • HAS_XINERAMA: Use X11 Xinerama for cleaner full-screen handling.

    Implementation Note

    • CNFG_IMPLEMENTATION: Required in exactly one file to include the implementation code.
  5. Load a rawdrawwasm WebAssembly instance

    master

    When integrating rawdrawwasm into a web application, you can load the WebAssembly module either synchronously (for small blobs to prevent flashing) or asynchronously using WebAssembly.instantiate. Once loaded, you typically access the exports and call the main() function to start the application.

    function wasmloaded(wa)
    {
    	instance = wa;
    	wasmExports = instance.exports;
    	instance.exports.main();
    }
    
    // Attempt in-thread loading for small blobs to avoid flashing
    if( blob.length < 4096 )
    {
    	let mod = new WebAssembly.Module(array);
    	var wa = new WebAssembly.Instance( mod, imports );
    	wasmloaded( wa );
    }
    else
    {
    	WebAssembly.instantiate(array, imports).then( function(wa) { wasmloaded(wa.instance); } );
    }
  6. Create a basic graphics application with rawdraw

    master

    A standard rawdraw application follows this lifecycle:

    1. Define CNFG_IMPLEMENTATION.
    2. Implement input callbacks: HandleKey, HandleButton, HandleMotion, and HandleDestroy.
    3. In main, call CNFGSetup to initialize the window.
    4. Run a loop checking CNFGHandleInput().
    5. Inside the loop: Clear the frame, set colors, draw primitives, and call CNFGSwapBuffers().

    Common drawing functions include:

    • CNFGColor(uint32_t color): Sets the current drawing color.
    • CNFGTackPixel(int x, int y): Draws a single pixel.
    • CNFGTackSegment(int x1, int y1, int x2, int y2): Draws a line.
    • CNFGTackRectangle(int x, int y, int w, int h): Draws a box.
    • CNFGBlitImage(uint32_t* data, int x, int y, int w, int h): Draws a bitmap.
    #define CNFG_IMPLEMENTATION
    #include "rawdraw_sf.h"
    
    void HandleKey( int keycode, int bDown ) { }
    void HandleButton( int x, int y, int button, int bDown ) { }
    void HandleMotion( int x, int y, int mask ) { }
    int HandleDestroy() { return 0; }
    
    int main()
    {
    	CNFGSetup( "Example App", 1024, 768 );
    	while(CNFGHandleInput())
    	{
    		CNFGBGColor = 0x000080ff; //Dark Blue Background
    
    		short w, h;
    		CNFGClearFrame();
    		CNFGGetDimensions( &w, &h );
    
    		CNFGColor( 0xffffffff ); 
    
    		CNFGPenX = 1; CNFGPenY = 1;
    		CNFGDrawText( "Hello, World", 2 );
    		CNFGTackPixel( 30, 30 );         
    		CNFGTackSegment( 50, 50, 100, 50 );
    
    		CNFGColor( 0x800000FF ); 
    		CNFGTackRectangle( 100, 50, 150, 100 ); 
    
    		CNFGColor( 0x800000FF ); 
    		RDPoint points[3] = { { 30, 36}, {20, 50}, { 40, 50 } };
    		CNFGTackPoly( points, 3 );
    
    		CNFGSwapBuffers();
    	}
    }