glerminal/source/glerminal-private.h
2024-05-27 13:07:50 -04:00

105 lines
2.8 KiB
C++

#ifndef GLERMINAL_PRIVATE_H
#define GLERMINAL_PRIVATE_H
#include "glerminal.h"
#include <stb_image.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <stdexcept>
namespace glerminal
{
constexpr unsigned int CELL_SIZE = 8;
constexpr unsigned int GRID_WIDTH = ::GRID_WIDTH;
constexpr unsigned int GRID_HEIGHT = ::GRID_HEIGHT;
constexpr unsigned int LAYER_COUNT = ::LAYER_COUNT;
constexpr unsigned int CELL_SCALE = ::CELL_SCALE;
constexpr unsigned int GRID_AREA = GRID_WIDTH * GRID_HEIGHT;
constexpr unsigned int SCREEN_WIDTH = GRID_WIDTH * CELL_SIZE * CELL_SCALE;
constexpr unsigned int SCREEN_HEIGHT = GRID_HEIGHT * CELL_SIZE * CELL_SCALE;
class glerminal
{
public:
glerminal(glerminal_init_cb init, glerminal_main_cb main);
~glerminal();
glerminal(const glerminal&) = delete;
glerminal(glerminal&&) = delete;
glerminal& operator=(const glerminal&) = delete;
glerminal& operator=(glerminal&&) = delete;
void run();
void quit();
void flush();
void set(unsigned char x, unsigned char y, unsigned char layer, unsigned char sprite);
unsigned char get(unsigned char x, unsigned char y, unsigned char layer) const;
void offset(unsigned char x, unsigned char y, unsigned char layer, float x_offset, float y_offset);
void layer_color(unsigned char layer, unsigned int color);
void layer_scale(unsigned char layer, float scale);
void load_atlas(unsigned char w, unsigned char h, const unsigned int* data);
private:
// glfw data
GLFWwindow* m_window;
// opengl handles
unsigned int m_vbo;
unsigned int m_sprites_instance_vbo;
unsigned int m_offsets_instance_vbo;
unsigned int m_vao;
unsigned int m_screen_vao;
unsigned int m_program;
unsigned int m_screen_program;
unsigned int m_sprites_texture;
unsigned int m_framebuffer;
unsigned int m_framebuffer_backing_texture;
unsigned int m_screen_framebuffer;
unsigned int m_screen_framebuffer_backing_texture;
unsigned int m_layer_colors_buffer;
unsigned int m_layer_scales_buffer;
unsigned int m_screen_size_uniform_location;
unsigned int m_palette_uniform_location;
// per-cell data
unsigned char m_cells[GRID_AREA * LAYER_COUNT];
float m_offsets[GRID_AREA * LAYER_COUNT * 2];
// per-layer data
float m_layer_colors[LAYER_COUNT * 4];
float m_layer_scales[LAYER_COUNT];
// library state
unsigned int m_sprites[CELL_SIZE * CELL_SIZE * (1 << (8 * sizeof(*m_cells)))];
glerminal_main_cb m_main;
#ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT
mutable std::ofstream m_log;
#endif
void log(GLenum type, GLuint id, GLenum severity, const char* message) const;
void init_glfw();
void init_gl();
void deinit_glfw();
void deinit_gl();
void update_sprites();
void update_layer_colors();
void update_layer_scales();
};
}
#endif//GLERMINAL_PRIVATE_H