Code Monkey home page Code Monkey logo

rawdraw's Introduction

CN's Fundamental Graphics Library

An OS-Agnostic (including No OS AT ALL!) drawing system. It includes lines, boxes, linetext, points, bitmaps.

The drawing method can either be by the OS (i.e. SetPixel in Windows), rasterized in software (we draw our own lines) or by OpenGL where available. You can reconfigure it with various #defines. It also makes it really easy to get an OpenGL window on just about any supported platform.

Somewhere unusual and want to open a window and draw stuff? Use rawdraw.

Other packages too big, bulky or include junk you don't want? Use rawdraw.

Want to use OpenGL really quickly? Use rawdraw.

For embedded platforms, CNFGRASTERIZER is usually the back end which allows you to have high level commands on a framebuffer. It's what was used in ESP Channel 3. You can even play with the rasterizer on every mode except the Android port by defining it. You can even be twisted on Windows, Linux and on WASM (Web) and define it.

This is not like SDL - some drivers have different quirks, some have more features implemented than others, and a major goal is not to abstract very much, but almost to provide an exmaple how to use graphics on a specific platform.

Platform statuses

  • Windows, 100% Support for CNFGOGL and/or CNFGRASTERIZER, Native (GDI32) does not support alpha-blitting, or variable line width.
  • Linux, 100% Support for CNFGOGL and/or CNFGRASTERIZER, Native (X11) doe not support alpha-blitting, or variable line thickness .
  • Android, 100% Support for CNFGOGL (It is forced on) CNFGRASTERIZER not allowed.
  • WASM, 100% Support for CNFGOGL
  • Other EGL (Like Raspberry Pi Raw Metal) Platforms, same as Android, but fixed window size.
  • EGL, CNFGOGL represent OpenGL or OpenGL ES depending on platform support.
  • Vulkan support in progress.
  • CNFG3D Support has been ported with fixed-precision numericals to ESP8266.
  • Mac/OSX Support currently unknown. Legacy files moved here: https://github.com/cntools/rawdrawtools/tree/main/attic

Usage in project

You probably want to include CNFG as a submodule.

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

To use CNFG, be sure to do this, or include "CNFG.c" in your project.

#define CNFG_IMPLEMENTATION

Example program

A more comprehensive example can be found in rawdraw.c, but a basic example is as follows:

//Make it so we don't need to include any other C files in our build.
#define CNFG_IMPLEMENTATION

#include "CNFG.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 ) { }
void HandleDestroy() { }
int main()
{
	CNFGSetup( "Example App", 1024, 768 );
	while(1)
	{
		CNFGBGColor = 0x000080ff; //Dark Blue Background

		short w, h;
		CNFGClearFrame();
		CNFGHandleInput();
		CNFGGetDimensions( &w, &h );

		//Change color to white.
		CNFGColor( 0xffffffff ); 

		CNFGPenX = 1; CNFGPenY = 1;
		CNFGDrawText( "Hello, World", 2 );
		//Draw a white pixel at 3,0 30 
		CNFGTackPixel( 30, 30 );         

		//Draw a line from 50,50 to 100,50
		CNFGTackSegment( 50, 50, 100, 50 );

		//Dark Red Color Select
		CNFGColor( 0x800000FF ); 

		//Draw 50x50 box starting at 100,50
		CNFGTackRectangle( 100, 50, 150, 100 ); 

		//Bright Purple Select
		CNFGColor( 0x800000FF ); 

		//Draw a triangle
		RDPoint points[3] = { { 30, 36}, {20, 50}, { 40, 50 } };
		CNFGTackPoly( points, 3 );

		//Draw a bunch of random pixels as a blitted image.
		{
			static uint32_t data[64*64];
			int x, y;

			for( y = 0; y < 64; y++ ) for( x = 0; x < 64; x++ )
				data[x+y*64] = 0xff | (rand()<<8);

			CNFGBlitImage( data, 150, 30, 64, 64 );
		}

		//Display the image and wait for time to display next frame.
		CNFGSwapBuffers();		
	}
}

Building

Windows compile:

C:\tcc\tcc simple.c -Irawdraw -lopengl32 -lgdi32 -luser32 C:\windows\system32\msvcrt.dll

Linux compile:

gcc -o simple simple.c -lm -lX11

Note, with the STB-style header, you don't need to #define CNFG_IMPLEMENTATION anywhere in your code, and instead can also compile in cnfg.c

Configurations

Rawdraw is a very disjoint set of configurations

CNFG Configuration options include:

  • CNFG_IMPLEMENTATION = Include code for implementation.
  • __android__ = build Android port
  • WINDOWS, WIN32, WIN64 = Windows build
  • CNFGOGL = Make underlying functions use OpenGL if possible.
  • CNFGRASTERIZER = Make the underlying graphics engine rasterized functions (software rendering)
  • CNFG3D = Include CNFG3D with this rawdraw build. This provides rasterized graphics functions. Only use this option with the rasterizer.

Platform-Specific

  • HAS_XSHAPE = Include extra functions for handling on-screen display functionality in X11.
  • HAS_XINERAMA = Use X11's Xinerama to handle full-screen more cleanly.

Flags you will probably never want to use:

  • EGL_LEAN_AND_MEAN = Bare bones EGL Driver

Extra goodies

chew.h

You can copy-and-paste chew.c and chew.h into your program for a single-file compile-only version of GLEW. This is WAY BETTER than having to deal with the complicated system they have set up, but it doesn't include all the functions. You can easily add OpenGL functions to it.

It provides a very easy-to-use interface for OpenGL Extensions.

os_generic.h

os_generic is a platform independent way of creating threads, managing TLS, mutices, semaphores as well as getting the current time. It has the following configuration options:

OSG_NO_IMPLEMENTATION - Do not include implementation as static functions. OSG_PREFIX - Override function prefix OSG_NOSTATIC - Do not declare the functions as static.

TextTool/Text.c etc...

There is a sister repository, https://github.com/cntools/rawdrawtools which houses a text tool.

Warnings

OSX Support has been moved to attic.

rawdraw's People

Contributors

cnlohr avatar overlisting avatar efrenmanuel avatar viknet avatar dreua avatar aefeinstein avatar ultramn avatar caib avatar bondarenkoartur avatar char avatar rpherman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.