Skip to content
Vlod edited this page Dec 18, 2023 · 8 revisions

GL2D C++ Library Documentation

Version: 1.5.0
Author: Luta Vlad
GitHub Repository: gl2d

Overview

GL2D is a C++ library providing functionality for 2D graphics using OpenGL. It includes features such as drawing shapes with rotation, texture transparency, text rendering with fonts and shadows, camera manipulation, shaders, Vsync control, texture atlases, frame buffer rendering, and a particle system with custom shaders.

Why GL2D?

GL2D is a very lightweight OpenGL 2D library and it is very very easy to integrate into your project. So if you have a 3D OpenGL project, but don't want to make the 2D stuff yourself, integrating gl2d is something very easy and it provides all the things that you need.

Installing

GL2D is a header + source-only library! You only need to copy the header and the cpp file to use it. You can also optionally copy the particle system files if you want to also use that. You also need the dependencies described below but don't worry you also have them in this repo so you can copy them from here! You can also use the CMake provided!

Dependencies

Usage

Initialization

#include "gl2d.h"

int main() 
{
    gl2d::init();
    // Your code here
    gl2d::clearnup(); // Clean up before exiting if needed (the os will also do that for you)
    return 0;
}

Error Handling

GL2D provides a default error function that writes to the console. You can set a custom error function using:

void yourCustomErrorFunc(const char* msg, void *userDefinedData)
{
	std::cerr << "gl2d error: " << msg << "\n";
}
//...
gl2d::setErrorFuncCallback(yourCustomErrorFunc);
gl2d::setUserDefinedData(youData); //optional

Transforms

Whenever a method requests a transform as a vec4, it is encoded as X, Y, Width, and Height. Example:

renderer.renderRectangle(glm::vec4{200, 350, 25,25}, Colors_Orange);
//renders an orange rectangle, 200 pixels from the left of the window, 350 pixels from the top of the window, with 25 by 25 pixels size.

Complete example:

#include <glad/glad.h>
#include <glfw/glfw3.h>
#include "gl2d/gl2d.h"

int main()
{
	// Initialize GLFW
	glfwInit();
	GLFWwindow *window = glfwCreateWindow(840, 640, "Window", nullptr, nullptr);
	glfwMakeContextCurrent(window);
	gladLoadGLLoader((GLADloadproc)(glfwGetProcAddress));

	// Initialize gl2d
	gl2d::init();
	gl2d::Renderer2D renderer;
	renderer.create();

	// Load resources example
	//gl2d::Font font(RESOURCES_PATH "roboto_black.ttf");
	//gl2d::Texture texture(RESOURCES_PATH "test.jpg");

	// Main loop
	while (!glfwWindowShouldClose(window))
	{
		//very important, don't forget to call renderer.updateWindowMetrics, 
		//this is probably the thing that I forget most often
		int w = 0; int h = 0;
		glfwGetWindowSize(window, &w, &h);
		renderer.updateWindowMetrics(w, h);

		// Handle input and update

		// Clear screen
		renderer.clearScreen({0.1, 0.2, 0.6, 1});

		// Render objects
		renderer.renderRectangle({100, 250, 100, 100}, Colors_White, {}, 0);
		//renderer.renderRectangle({100, 100, 100, 100}, texture, Colors_White, {}, 0);
		// Add more rendering here...

		// Flush renderer (dump your rendering into the screen)
		renderer.flush();

		// Swap buffers and poll events
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	//cleanup if you want, no need for it here tho.
	return 0;
}

Result:

Features

  • Drawing Shapes:

    • Color and Texture Transparency
  • Text Rendering:

    • Fonts
    • Shadows
    • Customizable Size and Spacing
  • Camera:

    • Position
    • Rotation
    • Zoom
  • Shaders:

    • Custom Shader Programs
  • Texture Handling:

    • Texture Loading
    • Texture Atlases
  • Frame Buffer:

    • Rendering to Screen or Texture
  • Particle System:

    • Custom Shaders
    • Particles can have various flags and emit other particles.
    • Pixelate Effect