mirror of
https://git.shylie.info/shylie/glerminal.git
synced 2025-11-29 12:10:08 +00:00
Rework sprite API
This commit is contained in:
@@ -3,24 +3,23 @@
|
||||
|
||||
#include "glerminal.h"
|
||||
|
||||
#include <stb_image.h>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#ifdef _DEBUG
|
||||
#include <fstream>
|
||||
#endif
|
||||
#include <stdexcept>
|
||||
|
||||
namespace glerminal
|
||||
{
|
||||
constexpr unsigned int SCREEN_WIDTH = 1280;
|
||||
constexpr unsigned int SCREEN_HEIGHT = 800;
|
||||
constexpr unsigned int CELL_SIZE = GLERMINAL_CELL_SIZE;
|
||||
constexpr unsigned int CELL_SIZE = 8;
|
||||
constexpr unsigned int CELL_SCALE = 4;
|
||||
constexpr unsigned int LAYER_COUNT = 256;
|
||||
constexpr unsigned int GRID_WIDTH = SCREEN_WIDTH / (CELL_SIZE * CELL_SCALE);
|
||||
constexpr unsigned int GRID_HEIGHT = SCREEN_HEIGHT / (CELL_SIZE * CELL_SCALE);
|
||||
constexpr unsigned int GRID_AREA = GRID_WIDTH * GRID_HEIGHT;
|
||||
constexpr unsigned int LAYER_COUNT = 256;
|
||||
|
||||
class glerminal
|
||||
{
|
||||
@@ -43,8 +42,7 @@ namespace glerminal
|
||||
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 update_sprite(unsigned char id, glerminal_sprite sprite);
|
||||
void load_atlas(unsigned char w, unsigned char h, const unsigned int* data);
|
||||
|
||||
private:
|
||||
// glfw data
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STBI_ONLY_PNG
|
||||
#define STBI_MAX_DIMENSIONS 128
|
||||
#include "glerminal-private.h"
|
||||
|
||||
#define GRID_SIZE_UNIFORM_NAME "grid_size"
|
||||
@@ -255,10 +258,26 @@ namespace glerminal
|
||||
m_layer_scales[layer] = scale;
|
||||
}
|
||||
|
||||
void glerminal::update_sprite(unsigned char id, glerminal_sprite sprite)
|
||||
void glerminal::load_atlas(unsigned char w, unsigned char h, const unsigned int* data)
|
||||
{
|
||||
// does this work?
|
||||
reinterpret_cast<glerminal_sprite*>(m_sprites)[id] = sprite;
|
||||
// each row of the atlas
|
||||
for (int j = 0; j < h; j++)
|
||||
{
|
||||
// each column of the atlas
|
||||
for (int i = 0; i < w; i++)
|
||||
{
|
||||
// each row of the individual sprite
|
||||
for (int k = 0; k < CELL_SIZE; k++)
|
||||
{
|
||||
// offset from base address in atlas layout
|
||||
const unsigned int src_offset = i + k * w + j * w * CELL_SIZE;
|
||||
// offset from base address in array layout
|
||||
const unsigned int dst_offset = k + i * CELL_SIZE + j * w * CELL_SIZE;
|
||||
|
||||
memcpy(m_sprites + CELL_SIZE * dst_offset, data + CELL_SIZE * src_offset, CELL_SIZE * sizeof(unsigned int));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void glerminal::init_glfw()
|
||||
@@ -682,9 +701,29 @@ void glerminal_layer_scale(unsigned char layer, float scale)
|
||||
GLERMINAL_G->layer_scale(layer, scale);
|
||||
}
|
||||
|
||||
void glerminal_update_sprite(unsigned char id, glerminal_sprite sprite)
|
||||
void glerminal_load_sprites_file(const char* filename)
|
||||
{
|
||||
if (!GLERMINAL_G) { return; }
|
||||
|
||||
GLERMINAL_G->update_sprite(id, sprite);
|
||||
int w, h;
|
||||
stbi_uc* const buffer = stbi_load(filename, &w, &h, nullptr, 4);
|
||||
|
||||
// verify atlas size is a multiple of CELL_SIZE in each dimension
|
||||
if (w % glerminal::CELL_SIZE == 0 && h % glerminal::CELL_SIZE == 0)
|
||||
{
|
||||
GLERMINAL_G->load_atlas(w / glerminal::CELL_SIZE, h / glerminal::CELL_SIZE, reinterpret_cast<unsigned int*>(buffer));
|
||||
}
|
||||
|
||||
stbi_image_free(buffer);
|
||||
}
|
||||
|
||||
void glerminal_load_sprites_buffer(unsigned char width, unsigned char height, const unsigned int* buffer)
|
||||
{
|
||||
if (!GLERMINAL_G) { return; }
|
||||
|
||||
// verify atlas size is a multiple of CELL_SIZE in each dimension
|
||||
if (width % glerminal::CELL_SIZE == 0 && height % glerminal::CELL_SIZE == 0)
|
||||
{
|
||||
GLERMINAL_G->load_atlas(width / glerminal::CELL_SIZE, height / glerminal::CELL_SIZE, buffer);
|
||||
}
|
||||
}
|
||||
7985
source/stb_image.h
Normal file
7985
source/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user