NonoGL is a lightweight and simple 2D game library for C, designed for easy and quick setup of 2D rendering using OpenGL and FreeGLUT. It provides basic functionalities for window creation, image loading, text rendering, basic collision handling, and mouse and keyboard input handling, and a small set of ready to use themable GUI elements.
- Window Management: Easy window creation and management.
- Image Loading: Load and render images. (JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC)
- Truetype Font Loading: Load and render Truetype fonts.
- Collision Handling: Basic functions to check point-rectangle, rectangle-rectangle, point-circle, rectangle-circle and circle-circle collisions.
- Mouse and Keyboard Input Handling: Simple functions to check for mouse and keyboard button presses.
- Basic GUI Elements: Comes with a small set of common and themable gui elements.
-
Clone the repository:
git clone git@github.com:RednibCoding/nonogl.git
-
Copy the 'nonogl' folder into your project.
-
Compile your project with FreeGLUT and OpenGL:
Note: NonoGL comes bundled with freeGlut, so there is no need to install freeGlut manually
Use the following GCC command to compile your project:
Example for Linux:
gcc main.c -o main -lnonogl/internal/lib/linux/x64 -llibfreeglut_static -lGL -lGLU -lm -lpthread
Example for Windows:
gcc gui.c -o main.exe -Lnonogl/internal/lib/win32/x64 -lfreeglut_static -lopengl32 -lwinmm -lgdi32 -lglu32
-
Include the NonoGL header:
#define NONOGL_IMPLEMENTATION #include "nonogl/ngl.h"
Do this before you include this file in one C file to create the implementation. In all other files just include
nonoglwithout creating the implementations.#include "nonogl/ngl.h"
Here is a minimal example of using NonoGL to create a window and a render function:
#include <stdio.h>
#include <stdbool.h>
#define NONOGL_IMPLEMENTATION // Also create the implementations
#include "nonogl/ngl.h"
void render()
{
// Draw stuff here
}
int main()
{
// Create a window
if (!nnCreateWindow("NonoGL Window", 800, 600, false, false))
{
printf("Failed to create window.\n");
return -1;
}
nnSetRenderFunc(render); // Set the render callback
nnRun(); // Start the rendering loop
nnDestroyWindow(); // Cleanup resources
return 0;
}-
nnPosRepresents a 2D position.typedef struct { int x; // X-coordinate int y; // Y-coordinate } nnPos;
-
nnRecfRepresents a 2D rectangle.typedef struct { float x; // X-coordinate of the rectangle's top-left corner float y; // Y-coordinate of the rectangle's top-left corner float width; // Width of the rectangle float height; // Height of the rectangle } nnRecf;
-
nnColorfRepresents an RGBA color.typedef struct { float r; // Red component (0.0 to 1.0) float g; // Green component (0.0 to 1.0) float b; // Blue component (0.0 to 1.0) float a; // Alpha (transparency) component (0.0 to 1.0) } nnColorf;
-
nnImageRepresents an image loaded into OpenGL.typedef struct { unsigned int textureID; // OpenGL texture ID int width; // Width of the image int height; // Height of the buffer float scaleX; // Scale in x axies float scaleY; // Scale in x axies bool isFlippedX; // Is x-axies flipped bool isFlippedY; // Is y-axies flipped float angle; // Rotation angle in degrees } nnImage;
-
nnPixmapRepresents a 2D buffer of colors (nnColorf), for fast pixel manipulations.typedef struct { unsigned int textureID; // OpenGL texture ID int width; // Width of the pixmap int height; // Height of the pixmap float scaleX; // Scale in x axies float scaleY; // Scale in x axies bool isFlippedX; // Is x-axies flipped bool isFlippedY; // Is y-axies flipped float angle; // Rotation angle in degrees nnColorf *pixels; // CPU-side pixel pixmap } nnPixmap;
-
nnFontRepresents a font loaded with stb_truetype.typedef struct { stbtt_fontinfo fontInfo; float glyphHeight; // Height of the loaded font unsigned char *fontBuffer; // Holds the font file data unsigned int textureID; // OpenGL texture for the font atlas int atlasWidth; // Width of the font atlas int atlasHeight; // Height of the font atlas float scale; // Font scaling factor stbtt_bakedchar charData[96]; // Holds character data for ASCII 32-127 } nnFont;
nnThemeTheme used by gui elements
typedef struct { nnColorf primaryColor; // Main color for elements nnColorf primaryColorAccent; // Accent color for primary elements nnColorf secondaryColor; // Secondary elements (e.g., backgrounds) nnColorf secondaryColorAccent; // Accent color for secondary elements nnColorf borderColor; // Border color nnColorf borderColorAccent; // Accent border color nnColorf textPrimaryColor; // Primary text color nnColorf textSecondaryColor; // Secondary text color } nnTheme;
-
nnCreateWindow
bool nnCreateWindow(char *title, int width, int height, bool virtual, bool filtered);
Creates a window with the specified title, width and height.
virtualdetermines whether to use a fixed, virtual coordinate system for rendering, regardless of the actual size of the window.filtereddetermines whether a smoothing filter should be applied to images and text. Otherwise, they stay pixelated (good for pixel art). -
nnSetRenderFunc
void nnSetRenderFunc(void (*callback)(void));
Set the render callback function that NonoGL will call every frame.
-
nnDestroyWindow
void nnDestroyWindow();
Destroys the created window and cleans up resources.
-
nnSetTargetFPS
void nnSetTargetFPS(int fps);
Sets the target FPS.
-
nnSetWindowTitle
void nnSetWindowTitle(const char *format, ...);
Sets the window title with formatted text.
-
nnSetClearColor
void nnSetClearColor(nnColorf color);
Sets the background color for clearing the window.
-
nnSetColor
void nnSetColor(nnColorf color);
Sets the current drawing color.
-
nnGetColor
nnColorf nnGetColor();
Gets the current drawing color.
-
nnResetColor
void nnResetColor();
Resets the color to default (1.0, 1.0, 1.0, 1.0).
-
nnRun
void nnRun();
Starts the main rendering loop.
-
nnLoadImage
nnImage nnLoadImage(const char *filepath);
Loads an image from the specified file path.
-
nnLoadImageMem
nnImage nnLoadImageMem(const unsigned char *data, int size);
Loads an image from memory.
-
nnDrawImage
void nnDrawImage(nnImage image, int x, int y);
Draws the loaded image at the specified position.
-
nnDrawImagePortion
void nnDrawImagePortion(nnImage image, int x, int y, nnRecf srcRec);
Draws a portion of the loaded image at the specified position.
-
nnFlipImage
void nnFlipImage(nnImage image, bool flipX, bool flipY);
Flips the given image on the given axes.
-
nnRotateImage
void nnRotateImage(nnImage *image, float degrees);
Sets the rotation of the given image to the specified degrees.
-
nnScaleImage
void nnScaleImage(nnImage *image, float scaleX, float scaleY);
Sets the scale of the given image to the specified scale values.
-
nnFreeImage
void nnFreeImage(nnImage image);
Frees the given image.
-
nnCreatePixmap
nnPixmap *nnCreatePixmap(int width, int height);
Creates a pixmap with the given width and height.
-
nnCreatePixmapFromImage
nnPixmap *nnCreatePixmapFromImage(nnImage image);
Creates a pixmap from an image.
-
nnReadPixel
nnColorf nnReadPixel(nnPixmap *pixmap, int x, int y);
Reads a pixel from the given pixmap.
-
nnUpdatePixmap
void nnUpdatePixmap(nnPixmap *pixmap);
Updates pixels that have changed in the given pixmap. Only updates pixels that have actually changed.
-
nnDrawPixmap
void nnDrawPixmap(nnPixmap *pixmap, int x, int y);
Draws the pixmap to the screen at the specified coordinates.
-
nnFlipPixmap
void nnFlipPixmap(nnPixmap *pixmap, bool flipX, bool flipY);
Flips the given pixmap on the specified axes.
-
nnRotatePixmap
void nnRotatePixmap(nnPixmap *pixmap, float angle);
Sets the rotation of the given pixmap to the specified degrees.
-
nnScalePixmap
void nnScalePixmap(nnPixmap *pixmap, float scaleX, float scaleY);
Sets the scale of the given pixmap to the specified scale values.
-
nnCopyPixmap
nnPixmap *nnCopyPixmap(nnPixmap *pixmap);
Returns a copy of the given pixmap.
-
nnFreePixmap
void nnFreePixmap(nnPixmap *pixmap);
Frees the given pixmap.
-
nnPutPixel
void nnPutPixel(float x, float y);
Draws an individual pixel to the screen.
Note: When drawing large chunks of pixels, it is recommended to use a pixmap instead for performance reasons.
-
nnDrawPixel
void nnDrawPixel(nnPixmap *pixmap, int x, int y, nnColorf color);
Writes a pixel at the specified location with the given color to the given pixmap.
-
nnDrawLine
void nnDrawLine(nnPixmap *pixmap, int x0, int y0, int x1, int y1, nnColorf color);
Draws a line between the specified points.
-
nnDrawOval
void nnDrawOval(nnPixmap *pixmap, int x, int y, int width, int height, nnColorf color, bool filled);
Draws an oval with its center at the specified coordinates.
-
nnDrawTriangle
void nnDrawTriangle(nnPixmap *pixmap, int x1, int y1, int x2, int y2, int x3, int y3, nnColorf color, bool filled);
Draws a triangle.
-
nnDrawRect
void nnDrawRect(nnPixmap *pixmap, int x, int y, int width, int height, nnColorf color, bool filled);
Draws a rectangle where the specified coordinates are the top-left corner.
-
nnLoadFont
nnFont *nnLoadFont(const char *filepath, float fontSize);
Loads a TTF font from a file.
-
nnLoadFontMem
nnFont *nnLoadFontMem(const unsigned char *data, size_t dataSize, float fontSize);
Loads a TTF font from memory.
-
nnSetFont
void nnSetFont(nnFont *font);
Sets the font for text rendering.
-
nnGetFont
nnFont *nnGetFont();
Gets the currently set font.
-
nnDrawText
void nnDrawText(const char *format, int x, int y, ...);
Renders the given formatted text using the font set with
nnSetFont. If no font is set, the internal default font will be used. -
nnDrawTextZ
void nnDrawTextZ(const char *format, int x, int y, float zIndex, ...);
Renders the given formatted text with a custom z-index (-1.0 to 1.0).
-
nnDrawDebugText
void nnDrawDebugText(const char *format, int x, int y, ...);
Draws text when debug mode is enabled.
-
nnTextWidth
float nnTextWidth(const char *format, ...);
Returns the width in pixels of the given formatted text.
-
nnTextHeight
float nnTextHeight();
Returns the height in pixels of the current font.
-
nnFreeFont
void nnFreeFont(nnFont *font);
Frees the given font.
-
nnPosRecOverlaps
bool nnPosRecOverlaps(int x, int y, nnRecf rect);
Checks if a point overlaps with a rectangle.
-
nnRecsOverlap
bool nnRecsOverlap(nnRecf rec1, nnRecf rec2);
Checks if two rectangles overlap.
-
nnPosCircleOverlaps
bool nnPosCircleOverlaps(int x, int y, int cx, int cy, float circleRadius);
Checks if a point overlaps with a circle.
-
nnRecCircleOverlaps
bool nnRecCircleOverlaps(nnRecf rec, int cx, int cy, float circleRadius);
Checks if a rectangle overlaps with a circle.
-
nnCirclesOverlap
bool nnCirclesOverlap(int cx1, int cy1, float circle1Radius, int cx2, int cy2, float circle2Radius);
Checks if two circles overlap.
-
nnKeyHit
bool nnKeyHit(int key);
Returns
trueif the specified key was pressed since the last frame. -
nnKeyDown
bool nnKeyDown(int key);
Returns
trueif the specified key is currently being held down. -
nnKeyReleased
bool nnKeyReleased(int key);
Returns
trueif the specified key was released since the last frame. -
nnFlushKeys
void nnFlushKeys();
Resets all key states.
-
nnMouseHit
bool nnMouseHit(int button);
Returns
trueif the specified mouse button was clicked since the last frame. -
nnMouseDown
bool nnMouseDown(int button);
Returns
trueif the specified mouse button is currently being held down. -
nnMouseReleased
bool nnMouseReleased(int button);
Returns
trueif the specified mouse button was released since the last frame. -
nnMouseWheelDelta
int nnMouseWheelDelta();
Returns the direction of the mouse wheel movement.
-
nnGetMousePosition
nnPos nnGetMousePosition();
Returns the current position of the mouse cursor.
-
nnMouseMotionDelta
nnPos nnMouseMotionDelta();
Returns the mouse motion delta (change in position) since the last frame.
-
nnFlushMouse
void nnFlushMouse();
Resets all mouse button states.
-
nnLoadFileBytes
unsigned char *nnLoadFileBytes(const char *filepath, int *size);
Loads a file into a buffer and returns its pointer.
-
nnFreeFileBytes
void nnFreeFileBytes(unsigned char *buffer);
Frees the memory allocated by
nnLoadFileBytes. -
nnLerp
float nnLerp(float min, float max, float speed, float ease);
Lerps between
minandmaxby the given speed. Useeasefor smooth transitions. -
nnSetDebugMode
void nnSetDebugMode(bool flag);
Enables or disables debug mode.
-
nnIsDebugMode
bool nnIsDebugMode();
Returns whether debug mode is enabled.
-
nnFPS
int nnFPS;
Holds the current frames per second.
-
nnDT
float nnDT;
Holds the delta time (time elapsed since the last frame).
-
nnMS
float nnMS;
Time in milliseconds since the application started.
-
nnPanel
bool nnPanel(int x, int y, int width, int height);
A panel element with background color and border. Returns
trueif the mouse is hovering over it, otherwisefalse. -
nnLabel
void nnLabel(const char *format, int x, int y, int width, int height, bool border, ...);
Displays text with an optional border. Wraps text within the defined width and clips it if it exceeds the provided height.
-
nnTextInput
int nnTextInput(char *buffer, int maxLength, int x, int y, int width, int height, const char *placeholder);
A basic single line text input box.
-
nnButton
bool nnButton(const char *format, int x, int y, int width, int height, ...);
Creates a button that returns
truewhen clicked. -
nnCheckbox
bool nnCheckbox(const char *format, bool isChecked, int x, int y, ...);
Creates a checkbox that returns
truewhen checked. -
nnHSlider
float nnHSlider(float min, float max, float initial, float step, int x, int y, int height);
Creates a horizontal slider that returns the current value.
-
nnVSlider
float nnVSlider(float min, float max, float initial, float step, int x, int y, int height);
Creates a vertical slider that returns the current value.
-
nnHProgressbar
int nnHProgressbar(float min, float max, float deltaFillState, int x, int y, int width);
Creates a horizontal progress bar that returns the current fill state in percent.
-
nnVProgressbar
int nnVProgressbar(float min, float max, float deltaFillState, int x, int y, int width);
Creates a vertical progress bar that returns the current fill state in percent.
-
nnDropdown
int nnDropdown(const char *buttonText, const char **options, int numOptions, int x, int y, int width, int height);
Creates a dropdown that returns the selected index.
-
nnScrollableList
int nnScrollableList(const char **items, int numItems, int x, int y, int width, int height);
Creates a scrollable list that returns the selected index.
NonoGL is licensed under the MIT License. See the LICENSE file for more details.