Compare commits

..

No commits in common. "db46885ee96e0b5e327225c3f1032d4b64e62611" and "45ec13db451445821d667a516ff3d2134ba3c976" have entirely different histories.

15 changed files with 112 additions and 92851 deletions

View File

@ -20,7 +20,7 @@ jobs:
fetch-depth: 0 fetch-depth: 0
submodules: recursive submodules: recursive
lfs: true lfs: true
- run: cd prev && git checkout 7c0c02b9af - run: cd prev && git checkout 10a90a5f6a
- name: Build current - name: Build current
run: cmake -S curr -B curr/build -DGLERMINAL_TEST=ON && cmake --build curr/build run: cmake -S curr -B curr/build -DGLERMINAL_TEST=ON && cmake --build curr/build
- name: Build previous - name: Build previous

4
.gitmodules vendored
View File

@ -1,6 +1,6 @@
[submodule "glfw"] [submodule "glfw"]
path = glfw path = glfw
url = https://github.com/glfw/glfw.git url = https://git.shylie.info/shylie/glfw.git
[submodule "Lua"] [submodule "Lua"]
path = Lua path = Lua
url = https://github.com/walterschell/Lua.git url = https://git.shylie.info/shylie/Lua.git

View File

@ -28,9 +28,6 @@ add_library(glerminallib STATIC
source/glad/glad.h source/glad/glad.h
source/KHR/khrplatform.h source/KHR/khrplatform.h
source/glad.c source/glad.c
source/miniaudio.h
source/miniaudio.c
) )
set_target_properties(glerminallib set_target_properties(glerminallib

View File

@ -30,8 +30,8 @@ namespace
{ {
for (int k = 0; k < LAYER_COUNT; k++) for (int k = 0; k < LAYER_COUNT; k++)
{ {
glerminal_set(i + 1, j + 1, k, rand() % 4); glerminal_set(i, j, k, rand() % 4);
glerminal_offset(i + 1, j + 1, k, (rand() * rand()) % 64 - 32, (rand() * rand()) % 64 - 32); glerminal_offset(i, j, k, (rand() * rand()) % 64 - 32, (rand() * rand()) % 64 - 32);
} }
} }
} }

View File

@ -9,9 +9,9 @@ namespace
{ {
glerminal_load_sprites_file("resources/basic.png"); glerminal_load_sprites_file("resources/basic.png");
for (int i = 0; i < GRID_WIDTH + 2; i++) for (int i = 0; i < GRID_WIDTH; i++)
{ {
for (int j = 0; j < GRID_HEIGHT + 2; j++) for (int j = 0; j < GRID_HEIGHT; j++)
{ {
glerminal_set(i, j, 0, 1); glerminal_set(i, j, 0, 1);
} }
@ -25,9 +25,9 @@ namespace
time += dt; time += dt;
time = fmodf(time, 3.1415926f * 2); time = fmodf(time, 3.1415926f * 2);
for (int i = 0; i < GRID_WIDTH + 2; i++) for (int i = 0; i < GRID_WIDTH; i++)
{ {
for (int j = 0; j < GRID_HEIGHT + 2; j++) for (int j = 0; j < GRID_HEIGHT; j++)
{ {
glerminal_offset(i, j, 0, cosf(time - i / 3.1415f), sinf(time - j / 3.1415f)); glerminal_offset(i, j, 0, cosf(time - i / 3.1415f), sinf(time - j / 3.1415f));
} }

View File

@ -18,16 +18,16 @@ namespace
{ {
glerminal_load_sprites_file("resources/rogue.png"); glerminal_load_sprites_file("resources/rogue.png");
for (int i = 0; i < GRID_WIDTH + 2; i++) for (int i = 0; i < GRID_WIDTH; i++)
{ {
for (int j = 0; j < GRID_HEIGHT + 2; j++) for (int j = 0; j < GRID_HEIGHT; j++)
{ {
for (int k = 0; k < WALL_LAYERS; k++) for (int k = 0; k < WALL_LAYERS; k++)
{ {
const unsigned char v = (k + 1) * (256 / WALL_LAYERS) - 1; const unsigned char v = (k + 1) * (256 / WALL_LAYERS) - 1;
const unsigned int c = (0xFF << 24) | (v << 16) | (v << 8) | v; const unsigned int c = (0xFF << 24) | (v << 16) | (v << 8) | v;
if (i == 1 || j == 1 || i == GRID_WIDTH || j == GRID_HEIGHT) if (i == 0 || j == 0 || i == GRID_WIDTH - 1 || j == GRID_HEIGHT - 1)
{ {
glerminal_set(i, j, k, floor); glerminal_set(i, j, k, floor);
} }
@ -48,12 +48,12 @@ namespace
static double time = 0; static double time = 0;
time += dt; time += dt;
const float cx = GRID_WIDTH / 2.0f * cosf(time / 2) + GRID_WIDTH / 2.0f + 0.5f; const float cx = GRID_WIDTH / 2.0f * cosf(time / 2) + GRID_WIDTH / 2.0f;
const float cy = GRID_HEIGHT / 2.0f * sinf(time / 2) + GRID_HEIGHT / 2.0f + 0.5f; const float cy = GRID_HEIGHT / 2.0f * sinf(time / 2) + GRID_HEIGHT / 2.0f;
for (int i = 0; i < GRID_WIDTH + 2; i++) for (int i = 0; i < GRID_WIDTH; i++)
{ {
for (int j = 0; j < GRID_HEIGHT + 2; j++) for (int j = 0; j < GRID_HEIGHT; j++)
{ {
for (int k = 0; k < WALL_LAYERS; k++) for (int k = 0; k < WALL_LAYERS; k++)
{ {

View File

@ -12,9 +12,9 @@ namespace
glerminal_load_sprites_file("resources/towers.png"); glerminal_load_sprites_file("resources/towers.png");
for (int i = 0; i < GRID_WIDTH + 2; i++) for (int i = 0; i < GRID_WIDTH; i++)
{ {
for (int j = 0; j < GRID_HEIGHT + 2; j++) for (int j = 0; j < GRID_HEIGHT; j++)
{ {
const int c = rand() % (LAYER_COUNT * 3 / 4) + LAYER_COUNT / 4; const int c = rand() % (LAYER_COUNT * 3 / 4) + LAYER_COUNT / 4;
for (int k = 0; k < c; k++) for (int k = 0; k < c; k++)
@ -40,12 +40,12 @@ namespace
time += dt; time += dt;
const float cx = (GRID_WIDTH / 2.0f) * cosf(time / 3.1415f) + (GRID_WIDTH / 2.0f) + 0.5f; const float cx = (GRID_WIDTH / 2.0f) * cosf(time / 3.1415f) + (GRID_WIDTH / 2.0f);
const float cy = (GRID_HEIGHT / 2.0f) * sinf(time / 3.1415f) + (GRID_HEIGHT / 2.0f) + 0.5f; const float cy = (GRID_HEIGHT / 2.0f) * sinf(time / 3.1415f) + (GRID_HEIGHT / 2.0f);
for (int i = 0; i < GRID_WIDTH + 2; i++) for (int i = 0; i < GRID_WIDTH; i++)
{ {
for (int j = 0; j < GRID_WIDTH + 2; j++) for (int j = 0; j < GRID_WIDTH; j++)
{ {
for (int k = 0; k < LAYER_COUNT; k++) for (int k = 0; k < LAYER_COUNT; k++)
{ {

View File

@ -42,7 +42,7 @@ void glerminal_flush();
* @param layer layer of the cell in the range [0, LAYER_COUNT) * @param layer layer of the cell in the range [0, LAYER_COUNT)
* @param sprite sprite's index in the range [0, 4096) * @param sprite sprite's index in the range [0, 4096)
*/ */
void glerminal_set(int x, int y, int layer, unsigned short sprite); void glerminal_set(unsigned char x, unsigned char y, unsigned char layer, unsigned short sprite);
/** /**
* @brief Get a cell's sprite * @brief Get a cell's sprite
* @param x position of the cell in the range [0, GRID_WIDTH) * @param x position of the cell in the range [0, GRID_WIDTH)
@ -50,7 +50,7 @@ void glerminal_set(int x, int y, int layer, unsigned short sprite);
* @param layer layer of the cell in the range [0, LAYER_COUNT) * @param layer layer of the cell in the range [0, LAYER_COUNT)
* @return sprite index currently assigned to the cell * @return sprite index currently assigned to the cell
*/ */
unsigned short glerminal_get(int x, int y, int layer); unsigned char glerminal_get(unsigned char x, unsigned char y, unsigned short layer);
/** /**
* @brief Set a cell's offset * @brief Set a cell's offset
* @param x position of the cell in the range [0, GRID_WIDTH) * @param x position of the cell in the range [0, GRID_WIDTH)
@ -59,7 +59,7 @@ unsigned short glerminal_get(int x, int y, int layer);
* @param x_offset offset of the cell on the x axis in cells * @param x_offset offset of the cell on the x axis in cells
* @param y_offset offset of the cell on the y axis in cells * @param y_offset offset of the cell on the y axis in cells
*/ */
void glerminal_offset(int x, int y, int layer, float x_offset, float y_offset); void glerminal_offset(unsigned char x, unsigned char y, unsigned char layer, float x_offset, float y_offset);
/** /**
* @brief Set a cell's color * @brief Set a cell's color
@ -68,7 +68,7 @@ void glerminal_offset(int x, int y, int layer, float x_offset, float y_offset);
* @param layer layer of the cell in the range [0, LAYER_COUNT) * @param layer layer of the cell in the range [0, LAYER_COUNT)
* @param color The new color * @param color The new color
*/ */
void glerminal_color(int x, int y, int layer, unsigned int color); void glerminal_color(unsigned char x, unsigned char y, unsigned char layer, unsigned int color);
/** /**
* @brief Set a cell's scale * @brief Set a cell's scale
* @param x position of the cell in the range [0, GRID_WIDTH) * @param x position of the cell in the range [0, GRID_WIDTH)
@ -76,7 +76,7 @@ void glerminal_color(int x, int y, int layer, unsigned int color);
* @param layer layer of the cell in the range [0, LAYER_COUNT) * @param layer layer of the cell in the range [0, LAYER_COUNT)
* @param scale The new scale * @param scale The new scale
*/ */
void glerminal_scale(int x, int y, int layer, float scale); void glerminal_scale(unsigned char x, unsigned char y, unsigned char layer, float scale);
/** /**
* @brief Load sprites from a png file * @brief Load sprites from a png file
@ -92,8 +92,6 @@ int glerminal_load_sprites_file(const char* filename);
*/ */
int glerminal_load_sprites_buffer(unsigned char width, unsigned char height, const unsigned int* buffer); int glerminal_load_sprites_buffer(unsigned char width, unsigned char height, const unsigned int* buffer);
void glerminal_sound(const char* name);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -38,9 +38,9 @@ namespace
int lglerminal_set(lua_State* L) int lglerminal_set(lua_State* L)
{ {
const int x = luaL_checkinteger(L, 1); const unsigned char x = luaL_checkinteger(L, 1) - 1;
const int y = luaL_checkinteger(L, 2); const unsigned char y = luaL_checkinteger(L, 2) - 1;
const int layer = luaL_checkinteger(L, 3) - 1; const unsigned char layer = luaL_checkinteger(L, 3) - 1;
const unsigned short sprite = luaL_checkinteger(L, 4) - 1; const unsigned short sprite = luaL_checkinteger(L, 4) - 1;
glerminal_set(x, y, layer, sprite); glerminal_set(x, y, layer, sprite);
@ -49,9 +49,9 @@ namespace
int lglerminal_get(lua_State* L) int lglerminal_get(lua_State* L)
{ {
const int x = luaL_checkinteger(L, 1); const unsigned char x = luaL_checkinteger(L, 1) - 1;
const int y = luaL_checkinteger(L, 2); const unsigned char y = luaL_checkinteger(L, 2) - 1;
const int layer = luaL_checkinteger(L, 3) - 1; const unsigned char layer = luaL_checkinteger(L, 3) - 1;
lua_pushnumber(L, glerminal_get(x, y, layer) + 1); lua_pushnumber(L, glerminal_get(x, y, layer) + 1);
return 1; return 1;
@ -59,9 +59,9 @@ namespace
int lglerminal_offset(lua_State* L) int lglerminal_offset(lua_State* L)
{ {
const int x = luaL_checkinteger(L, 1); const unsigned char x = luaL_checkinteger(L, 1) - 1;
const int y = luaL_checkinteger(L, 2); const unsigned char y = luaL_checkinteger(L, 2) - 1;
const int layer = luaL_checkinteger(L, 3) - 1; const unsigned char layer = luaL_checkinteger(L, 3) - 1;
const float ox = luaL_checknumber(L, 4); const float ox = luaL_checknumber(L, 4);
const float oy = luaL_checknumber(L, 5); const float oy = luaL_checknumber(L, 5);
glerminal_offset(x, y, layer, ox, oy); glerminal_offset(x, y, layer, ox, oy);
@ -71,33 +71,10 @@ namespace
int lglerminal_layer_color(lua_State* L) int lglerminal_layer_color(lua_State* L)
{ {
const int x = luaL_checkinteger(L, 1); const unsigned char x = luaL_checkinteger(L, 1) - 1;
const int y = luaL_checkinteger(L, 2); const unsigned char y = luaL_checkinteger(L, 2) - 1;
const int layer = luaL_checkinteger(L, 3) - 1; const unsigned char layer = luaL_checkinteger(L, 3) - 1;
const float r = luaL_checknumber(L, 4); const unsigned int color = luaL_checkinteger(L, 4);
const float g = luaL_checknumber(L, 5);
const float b = luaL_checknumber(L, 6);
const float a = luaL_optnumber(L, 7, 1.0f);
int ri = 255 * r;
int gi = 255 * g;
int bi = 255 * b;
int ai = 255 * a;
if (ri > 255) { ri = 255; }
if (ri < 0) { ri = 0; }
if (gi > 255) { gi = 255; }
if (gi < 0) { gi = 0; }
if (bi > 255) { bi = 255; }
if (bi < 0) { bi = 0; }
if (ai > 255) { ai = 255; }
if (ai < 0) { ai = 0; }
const unsigned int color = ri | (gi << 8) | (bi << 16) | (ai << 24);
glerminal_color(x, y, layer, color); glerminal_color(x, y, layer, color);
return 0; return 0;
@ -105,9 +82,9 @@ namespace
int lglerminal_layer_scale(lua_State* L) int lglerminal_layer_scale(lua_State* L)
{ {
const int x = luaL_checkinteger(L, 1); const unsigned char x = luaL_checkinteger(L, 1) - 1;
const int y = luaL_checkinteger(L, 2); const unsigned char y = luaL_checkinteger(L, 2) - 1;
const int layer = luaL_checkinteger(L, 3) - 1; const unsigned char layer = luaL_checkinteger(L, 3) - 1;
const float scale = luaL_checknumber(L, 4); const float scale = luaL_checknumber(L, 4);
glerminal_scale(x, y, layer, scale); glerminal_scale(x, y, layer, scale);
@ -122,14 +99,6 @@ namespace
return 1; return 1;
} }
int lglerminal_play_sound(lua_State* L)
{
const char* name = luaL_checkstring(L, 1);
glerminal_sound(name);
return 0;
}
const luaL_Reg lglerminal_methods[] = const luaL_Reg lglerminal_methods[] =
{ {
{ "quit", lglerminal_quit }, { "quit", lglerminal_quit },
@ -140,7 +109,6 @@ namespace
{ "tint", lglerminal_layer_color }, { "tint", lglerminal_layer_color },
{ "scale", lglerminal_layer_scale }, { "scale", lglerminal_layer_scale },
{ "sprites", lglerminal_load_sprites_file }, { "sprites", lglerminal_load_sprites_file },
{ "sound", lglerminal_play_sound },
{ nullptr, nullptr } { nullptr, nullptr }
}; };

View File

@ -2,7 +2,6 @@
#define GLERMINAL_PRIVATE_H #define GLERMINAL_PRIVATE_H
#include "glerminal.h" #include "glerminal.h"
#include "miniaudio.h"
#include <stb_image.h> #include <stb_image.h>
#include <glad/glad.h> #include <glad/glad.h>
@ -11,8 +10,6 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <stdexcept> #include <stdexcept>
#include <map>
#include <string>
namespace glerminal namespace glerminal
{ {
@ -26,9 +23,6 @@ namespace glerminal
constexpr unsigned int GRID_AREA = GRID_WIDTH * GRID_HEIGHT; constexpr unsigned int GRID_AREA = GRID_WIDTH * GRID_HEIGHT;
constexpr unsigned int SCREEN_WIDTH = GRID_WIDTH * CELL_SIZE * CELL_SCALE; constexpr unsigned int SCREEN_WIDTH = GRID_WIDTH * CELL_SIZE * CELL_SCALE;
constexpr unsigned int SCREEN_HEIGHT = GRID_HEIGHT * CELL_SIZE * CELL_SCALE; constexpr unsigned int SCREEN_HEIGHT = GRID_HEIGHT * CELL_SIZE * CELL_SCALE;
constexpr unsigned int SOUND_CHANNELS = 8;
constexpr unsigned int GRID_AREA_2 = (GRID_WIDTH + 2) * (GRID_HEIGHT + 2);
class glerminal class glerminal
{ {
@ -47,14 +41,12 @@ namespace glerminal
void flush(); void flush();
void set(int x, int y, int layer, unsigned short sprite); void set(unsigned char x, unsigned char y, unsigned char layer, unsigned short sprite);
unsigned short get(int x, int y, int layer) const; unsigned short get(unsigned char x, unsigned char y, unsigned char layer) const;
void offset(int x, int y, int layer, float x_offset, float y_offset); void offset(unsigned char x, unsigned char y, unsigned char layer, float x_offset, float y_offset);
void color(int x, int y, int layer, unsigned int color); void color(unsigned char x, unsigned char y, unsigned char layer, unsigned int color);
void scale(int x,int y, int layer, float scale); void scale(unsigned char x, unsigned char y, unsigned char layer, float scale);
void load_atlas(unsigned char w, unsigned char h, const unsigned int* data); void load_atlas(unsigned char w, unsigned char h, const unsigned int* data);
bool load_sound(const char* name);
void play_sound(const char* name);
private: private:
// glfw data // glfw data
@ -77,13 +69,15 @@ namespace glerminal
unsigned int m_screen_framebuffer_backing_texture; unsigned int m_screen_framebuffer_backing_texture;
unsigned int m_colors_instance_vbo; unsigned int m_colors_instance_vbo;
unsigned int m_scales_instance_vbo; unsigned int m_scales_instance_vbo;
unsigned int m_screen_size_uniform_location;
unsigned int m_palette_uniform_location;
// per-cell data // per-cell data
unsigned short m_cells[GRID_AREA_2 * LAYER_COUNT]; unsigned short m_cells[GRID_AREA * LAYER_COUNT];
float m_offsets[GRID_AREA_2 * LAYER_COUNT * 2]; float m_offsets[GRID_AREA * LAYER_COUNT * 2];
unsigned char m_colors[GRID_AREA_2 * LAYER_COUNT * 4]; unsigned char m_colors[GRID_AREA * LAYER_COUNT * 4];
float m_scales[GRID_AREA_2 * LAYER_COUNT]; float m_scales[GRID_AREA * LAYER_COUNT];
// library state // library state
@ -93,9 +87,6 @@ namespace glerminal
glerminal_mousemoved_cb m_mousemoved; glerminal_mousemoved_cb m_mousemoved;
glerminal_mousepress_cb m_mousepressed, m_mousereleased; glerminal_mousepress_cb m_mousepressed, m_mousereleased;
ma_engine m_audio_engine;
std::map<std::string, ma_sound> m_sounds;
#ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT #ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT
mutable std::ofstream m_log; mutable std::ofstream m_log;
#endif #endif
@ -104,13 +95,9 @@ namespace glerminal
void init_glfw(); void init_glfw();
void init_gl(); void init_gl();
void init_audio();
void deinit_glfw(); void deinit_glfw();
void deinit_gl(); void deinit_gl();
void deinit_audio();
void update_sprites(); void update_sprites();
void update_colors(); void update_colors();
void update_scales(); void update_scales();

View File

@ -1,6 +1,6 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG #define STBI_ONLY_PNG
#define STBI_MAX_DIMENSIONS 2048 #define STBI_MAX_DIMENSIONS 512
#include "glerminal-private.h" #include "glerminal-private.h"
#define GRID_SIZE_UNIFORM_NAME "grid_size" #define GRID_SIZE_UNIFORM_NAME "grid_size"
@ -49,8 +49,8 @@ namespace
" vs_out.layer = layer;\n" " vs_out.layer = layer;\n"
" vs_out.layer_color = color;\n" " vs_out.layer_color = color;\n"
" vs_out.texcoord = vec2(sprite % " ATLAS_WIDTH_UNIFORM_NAME " + position.x + 1, (sprite / " ATLAS_WIDTH_UNIFORM_NAME ") - position.y) / vec2(" ATLAS_WIDTH_UNIFORM_NAME ") + vec2(-(2 * position.x + 1) * " GRID_SIZE_UNIFORM_NAME ".z / 16, (2 * position.y + 1) * " GRID_SIZE_UNIFORM_NAME ".z / 16);\n" " vs_out.texcoord = vec2(sprite % " ATLAS_WIDTH_UNIFORM_NAME " + position.x + 1, (sprite / " ATLAS_WIDTH_UNIFORM_NAME ") - position.y) / vec2(" ATLAS_WIDTH_UNIFORM_NAME ") + vec2(-(2 * position.x + 1) * " GRID_SIZE_UNIFORM_NAME ".z / 16, (2 * position.y + 1) * " GRID_SIZE_UNIFORM_NAME ".z / 16);\n"
" vec2 cell_position = vec2(scale + (gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) - " GRID_SIZE_UNIFORM_NAME ".x * floor((gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) / " GRID_SIZE_UNIFORM_NAME ".x), -floor((gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) / " GRID_SIZE_UNIFORM_NAME ".x));\n" " vec2 cell_position = vec2(scale + (gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) - " GRID_SIZE_UNIFORM_NAME ".x * floor((gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) * " GRID_SIZE_UNIFORM_NAME ".z), -floor((gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) * " GRID_SIZE_UNIFORM_NAME ".z));\n"
" vec2 temp = ((position + vec2(-0.5, 0.5)) * scale + cell_position + vec2(-0.5, 0.5)) * " GRID_SIZE_UNIFORM_NAME ".zw * 2 + vec2(-1, 1);\n" " vec2 temp = ((position + vec2(-0.5, 0.5)) * scale + cell_position + vec2(0.5, -0.5)) * " GRID_SIZE_UNIFORM_NAME ".zw * 2 + vec2(-1, 1);\n"
" gl_Position = vec4(scaled_offset.x + temp.x, scaled_offset.y - temp.y, 0, 1);\n" " gl_Position = vec4(scaled_offset.x + temp.x, scaled_offset.y - temp.y, 0, 1);\n"
"}"; "}";
@ -79,8 +79,8 @@ namespace
" vs_out.sprite = sprite;\n" " vs_out.sprite = sprite;\n"
" vs_out.texcoord = vec2(sprite % " ATLAS_WIDTH_UNIFORM_NAME " + position.x + 1, (sprite / " ATLAS_WIDTH_UNIFORM_NAME ") - position.y) / vec2(" ATLAS_WIDTH_UNIFORM_NAME ") + vec2(-(2 * position.x + 1) * " GRID_SIZE_UNIFORM_NAME ".z / 16, (2 * position.y + 1) * " GRID_SIZE_UNIFORM_NAME ".z / 16);\n" " vs_out.texcoord = vec2(sprite % " ATLAS_WIDTH_UNIFORM_NAME " + position.x + 1, (sprite / " ATLAS_WIDTH_UNIFORM_NAME ") - position.y) / vec2(" ATLAS_WIDTH_UNIFORM_NAME ") + vec2(-(2 * position.x + 1) * " GRID_SIZE_UNIFORM_NAME ".z / 16, (2 * position.y + 1) * " GRID_SIZE_UNIFORM_NAME ".z / 16);\n"
" vs_out.layer_color = color;\n" " vs_out.layer_color = color;\n"
" vec2 cell_position = vec2(scale + (gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) - " GRID_SIZE_UNIFORM_NAME ".x * floor((gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) / " GRID_SIZE_UNIFORM_NAME ".x), -floor((gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) / " GRID_SIZE_UNIFORM_NAME ".x));\n" " vec2 cell_position = vec2(scale + (gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) - " GRID_SIZE_UNIFORM_NAME ".x * floor((gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) * " GRID_SIZE_UNIFORM_NAME ".z), -floor((gl_InstanceID % int(" GRID_SIZE_UNIFORM_NAME ".y)) * " GRID_SIZE_UNIFORM_NAME ".z));\n"
" vec2 temp = ((position + vec2(-0.5, 0.5)) * scale + cell_position + vec2(-0.5, 0.5)) * " GRID_SIZE_UNIFORM_NAME ".zw * 2 + vec2(-1, 1);\n" " vec2 temp = ((position + vec2(-0.5, 0.5)) * scale + cell_position + vec2(0.5, -0.5)) * " GRID_SIZE_UNIFORM_NAME ".zw * 2 + vec2(-1, 1);\n"
" gl_Position = vec4(scaled_offset.x + temp.x, scaled_offset.y - temp.y, 0, 1);\n" " gl_Position = vec4(scaled_offset.x + temp.x, scaled_offset.y - temp.y, 0, 1);\n"
"}"; "}";
@ -221,7 +221,7 @@ namespace glerminal
throw std::runtime_error("No main callback provided."); throw std::runtime_error("No main callback provided.");
} }
for (int i = 0; i < GRID_AREA_2 * LAYER_COUNT; i++) for (int i = 0; i < GRID_AREA * LAYER_COUNT; i++)
{ {
m_colors[i * 4 + 0] = m_colors[i * 4 + 1] = m_colors[i * 4 + 2] = m_colors[i * 4 + 3] = 255; m_colors[i * 4 + 0] = m_colors[i * 4 + 1] = m_colors[i * 4 + 2] = m_colors[i * 4 + 3] = 255;
m_scales[i] = 1; m_scales[i] = 1;
@ -229,7 +229,6 @@ namespace glerminal
init_glfw(); init_glfw();
init_gl(); init_gl();
init_audio();
GLERMINAL_G = this; GLERMINAL_G = this;
@ -238,7 +237,6 @@ namespace glerminal
glerminal::~glerminal() glerminal::~glerminal()
{ {
deinit_audio();
deinit_gl(); deinit_gl();
deinit_glfw(); deinit_glfw();
@ -277,7 +275,7 @@ namespace glerminal
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
glBindVertexArray(m_vao); glBindVertexArray(m_vao);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glDrawArraysInstanced(GL_TRIANGLES, 0, 6, GRID_AREA_2 * LAYER_COUNT); glDrawArraysInstanced(GL_TRIANGLES, 0, 6, GRID_AREA * LAYER_COUNT);
glUseProgram(m_screen_program); glUseProgram(m_screen_program);
glBindFramebuffer(GL_FRAMEBUFFER, m_screen_framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, m_screen_framebuffer);
@ -285,23 +283,28 @@ namespace glerminal
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
glBlitNamedFramebuffer(m_screen_framebuffer, 0, 0, 0, GRID_WIDTH * CELL_SIZE, GRID_HEIGHT * CELL_SIZE, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_COLOR_BUFFER_BIT, GL_NEAREST); glBlitNamedFramebuffer(m_screen_framebuffer, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glfwSwapInterval(0);
glfwSwapBuffers(m_window);
glBlitNamedFramebuffer(m_screen_framebuffer, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glfwSwapInterval(1);
glfwSwapBuffers(m_window); glfwSwapBuffers(m_window);
} }
void glerminal::set(int x, int y, int layer, unsigned short sprite) void glerminal::set(unsigned char x, unsigned char y, unsigned char layer, unsigned short sprite)
{ {
if (x >= 0 && x < GRID_WIDTH + 2 && y >= 0 && y < GRID_HEIGHT + 2 && layer >= 0 && layer < LAYER_COUNT) if (x < GRID_WIDTH && y < GRID_HEIGHT && layer < LAYER_COUNT)
{ {
m_cells[x + y * (GRID_WIDTH + 2) + layer * GRID_AREA_2] = sprite; m_cells[x + y * GRID_WIDTH + layer * GRID_AREA] = sprite;
} }
} }
unsigned short glerminal::get(int x, int y, int layer) const unsigned short glerminal::get(unsigned char x, unsigned char y, unsigned char layer) const
{ {
if (x >= 0 && x < GRID_WIDTH + 2 && y >= 0 && y < GRID_HEIGHT + 2 && layer >= 0 && layer < LAYER_COUNT) if (x < GRID_WIDTH && y < GRID_HEIGHT && layer < LAYER_COUNT)
{ {
return m_cells[x + y * (GRID_WIDTH + 2) + layer * GRID_AREA_2]; return m_cells[x + y * GRID_WIDTH + layer * GRID_AREA];
} }
else else
{ {
@ -309,32 +312,26 @@ namespace glerminal
} }
} }
void glerminal::offset(int x, int y, int layer, float x_offset, float y_offset) void glerminal::offset(unsigned char x, unsigned char y, unsigned char layer, float x_offset, float y_offset)
{ {
if (x >= 0 && x < GRID_WIDTH + 2 && y >= 0 && y < GRID_HEIGHT + 2 && layer >= 0 && layer < LAYER_COUNT) if (x < GRID_WIDTH && y < GRID_HEIGHT && layer < LAYER_COUNT)
{ {
m_offsets[2 * (x + y * (GRID_WIDTH + 2) + layer * GRID_AREA_2) + 0] = x_offset; m_offsets[2 * (x + y * GRID_WIDTH + layer * GRID_AREA) + 0] = x_offset;
m_offsets[2 * (x + y * (GRID_WIDTH + 2) + layer * GRID_AREA_2) + 1] = y_offset; m_offsets[2 * (x + y * GRID_WIDTH + layer * GRID_AREA) + 1] = y_offset;
} }
} }
void glerminal::color(int x, int y, int layer, unsigned int color) void glerminal::color(unsigned char x, unsigned char y, unsigned char layer, unsigned int color)
{ {
if (x >= 0 && x < GRID_WIDTH + 2 && y >= 0 && y < GRID_HEIGHT + 2 && layer >= 0 && layer < LAYER_COUNT) m_colors[4 * (x + y * GRID_WIDTH + layer * GRID_AREA) + 0] = ((color >> 0) & 0xFF);
{ m_colors[4 * (x + y * GRID_WIDTH + layer * GRID_AREA) + 1] = ((color >> 8) & 0xFF);
m_colors[4 * (x + y * (GRID_WIDTH + 2) + layer * GRID_AREA_2) + 0] = (color >> 0) & 0xFF; m_colors[4 * (x + y * GRID_WIDTH + layer * GRID_AREA) + 2] = ((color >> 16) & 0xFF);
m_colors[4 * (x + y * (GRID_WIDTH + 2) + layer * GRID_AREA_2) + 1] = (color >> 8) & 0xFF; m_colors[4 * (x + y * GRID_WIDTH + layer * GRID_AREA) + 3] = ((color >> 24) & 0xFF);
m_colors[4 * (x + y * (GRID_WIDTH + 2) + layer * GRID_AREA_2) + 2] = (color >> 16) & 0xFF;
m_colors[4 * (x + y * (GRID_WIDTH + 2) + layer * GRID_AREA_2) + 3] = (color >> 24) & 0xFF;
}
} }
void glerminal::scale(int x, int y, int layer, float scale) void glerminal::scale(unsigned char x, unsigned char y, unsigned char layer, float scale)
{ {
if (x >= 0 && x < GRID_WIDTH + 2 && y >= 0 && y < GRID_HEIGHT + 2 && layer >= 0 && layer < LAYER_COUNT) m_scales[x + y * GRID_WIDTH + layer * GRID_AREA] = scale;
{
m_scales[x + y * (GRID_WIDTH + 2) + layer * GRID_AREA_2] = scale;
}
} }
void glerminal::load_atlas(unsigned char w, unsigned char h, const unsigned int* data) void glerminal::load_atlas(unsigned char w, unsigned char h, const unsigned int* data)
@ -363,39 +360,6 @@ namespace glerminal
} }
} }
bool glerminal::load_sound(const char* name)
{
if (m_sounds.find(name) == m_sounds.end())
{
ma_sound& ref = m_sounds[name];
const ma_result result = ma_sound_init_from_file(
&m_audio_engine,
name,
MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_NO_SPATIALIZATION,
nullptr,
nullptr,
&ref
);
if (result != MA_SUCCESS)
{
return false;
}
}
return true;
}
void glerminal::play_sound(const char* name)
{
load_sound(name);
const ma_result result = ma_engine_play_sound(&m_audio_engine, name, nullptr);
if (result != MA_SUCCESS)
{
}
}
void glerminal::init_glfw() void glerminal::init_glfw()
{ {
glfwInit(); glfwInit();
@ -503,7 +467,7 @@ namespace glerminal
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0, 0, GRID_WIDTH * CELL_SIZE, GRID_HEIGHT * CELL_SIZE); glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// -- setup vertex data -- // -- setup vertex data --
// create vertex buffer object // create vertex buffer object
@ -651,8 +615,10 @@ namespace glerminal
} }
// setup uniforms // setup uniforms
m_screen_size_uniform_location = glGetUniformLocation(m_program, GRID_SIZE_UNIFORM_NAME);
glUseProgram(m_program); glUseProgram(m_program);
glUniform4f(glGetUniformLocation(m_program, GRID_SIZE_UNIFORM_NAME), GRID_WIDTH + 2, GRID_AREA_2, 1.0f / GRID_WIDTH, 1.0f / GRID_HEIGHT); glUniform4f(m_screen_size_uniform_location, GRID_WIDTH, GRID_AREA, 1.0f / GRID_WIDTH, 1.0f / GRID_HEIGHT);
glUniform1i(glGetUniformLocation(m_program, ATLAS_WIDTH_UNIFORM_NAME), MAX_SPRITES_ROW); glUniform1i(glGetUniformLocation(m_program, ATLAS_WIDTH_UNIFORM_NAME), MAX_SPRITES_ROW);
// compile // compile
@ -733,6 +699,7 @@ namespace glerminal
update_sprites(); update_sprites();
glBindTextureUnit(2, m_sprites_texture); glBindTextureUnit(2, m_sprites_texture);
const auto err = glGetError();
// -- setup framebuffer -- // -- setup framebuffer --
glGenFramebuffers(1, &m_framebuffer); glGenFramebuffers(1, &m_framebuffer);
@ -750,7 +717,7 @@ namespace glerminal
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 0);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, GRID_WIDTH * CELL_SIZE, GRID_HEIGHT * CELL_SIZE, LAYER_COUNT); glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, SCREEN_WIDTH, SCREEN_HEIGHT, LAYER_COUNT);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_framebuffer_backing_texture, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_framebuffer_backing_texture, 0);
glBindTextureUnit(1, m_framebuffer_backing_texture); glBindTextureUnit(1, m_framebuffer_backing_texture);
@ -771,22 +738,10 @@ namespace glerminal
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, GRID_WIDTH * CELL_SIZE, GRID_HEIGHT * CELL_SIZE); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, SCREEN_WIDTH, SCREEN_HEIGHT);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_screen_framebuffer_backing_texture, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_screen_framebuffer_backing_texture, 0);
} }
void glerminal::init_audio()
{
const ma_result result = ma_engine_init(nullptr, &m_audio_engine);
if (result != MA_SUCCESS)
{
throw std::runtime_error("Failed to initialize audio engine");
}
ma_engine_set_volume(&m_audio_engine, 1);
}
void glerminal::deinit_glfw() void glerminal::deinit_glfw()
{ {
glfwDestroyWindow(m_window); glfwDestroyWindow(m_window);
@ -811,15 +766,6 @@ namespace glerminal
glDeleteProgram(m_program); glDeleteProgram(m_program);
} }
void glerminal::deinit_audio()
{
for (auto& elem : m_sounds)
{
ma_sound_uninit(&elem.second);
}
ma_engine_uninit(&m_audio_engine);
}
void glerminal::update_sprites() void glerminal::update_sprites()
{ {
glTextureSubImage2D(m_sprites_texture, 0, 0, 0, (CELL_SIZE + 2) * MAX_SPRITES_ROW, (CELL_SIZE + 2) * MAX_SPRITES_ROW, GL_RGBA, GL_UNSIGNED_BYTE, m_sprites); glTextureSubImage2D(m_sprites_texture, 0, 0, 0, (CELL_SIZE + 2) * MAX_SPRITES_ROW, (CELL_SIZE + 2) * MAX_SPRITES_ROW, GL_RGBA, GL_UNSIGNED_BYTE, m_sprites);
@ -837,7 +783,7 @@ namespace glerminal
void glerminal::glfw_key_handler(GLFWwindow* window, int key, int scancode, int action, int mods) void glerminal::glfw_key_handler(GLFWwindow* window, int key, int scancode, int action, int mods)
{ {
const glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window)); glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
if (self->m_keypressed && action == GLFW_PRESS) if (self->m_keypressed && action == GLFW_PRESS)
{ {
@ -852,26 +798,26 @@ namespace glerminal
void glerminal::glfw_mousemoved_handler(GLFWwindow *window, double x, double y) void glerminal::glfw_mousemoved_handler(GLFWwindow *window, double x, double y)
{ {
const glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window)); glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
if (self->m_mousemoved) { self->m_mousemoved(x / (CELL_SIZE * CELL_SCALE), y / (CELL_SIZE * CELL_SCALE)); } if (self->m_mousemoved) { self->m_mousemoved(x / CELL_SIZE, y / CELL_SIZE); }
} }
void glerminal::glfw_mousepress_handler(GLFWwindow *window, int button, int action, int mods) void glerminal::glfw_mousepress_handler(GLFWwindow *window, int button, int action, int mods)
{ {
const glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window)); glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
double x, y; double x, y;
glfwGetCursorPos(window, &x, &y); glfwGetCursorPos(window, &x, &y);
if (self->m_mousepressed && action == GLFW_PRESS) if (self->m_mousepressed && action == GLFW_PRESS)
{ {
self->m_mousepressed(button, x / (CELL_SIZE * CELL_SCALE), y / (CELL_SIZE * CELL_SCALE)); self->m_mousepressed(button, x / CELL_SIZE, y / CELL_SIZE);
} }
if (self->m_mousereleased && action == GLFW_RELEASE) if (self->m_mousereleased && action == GLFW_RELEASE)
{ {
self->m_mousereleased(button, x / (CELL_SIZE * CELL_SCALE), y / (CELL_SIZE * CELL_SCALE)); self->m_mousereleased(button, x / CELL_SIZE, y / CELL_SIZE);
} }
} }
@ -905,39 +851,39 @@ void glerminal_flush()
GLERMINAL_G->flush(); GLERMINAL_G->flush();
} }
void glerminal_set(int x, int y, int layer, unsigned short sprite) void glerminal_set(unsigned char x, unsigned char y, unsigned char layer, unsigned short sprite)
{ {
if (!GLERMINAL_G) { return; } if (!GLERMINAL_G) { return; }
GLERMINAL_G->set(x + 1, y + 1, layer, sprite); GLERMINAL_G->set(x, y, layer, sprite);
} }
unsigned short glerminal_get(int x, int y, int layer) unsigned char glerminal_get(unsigned char x, unsigned char y, unsigned short layer)
{ {
if (!GLERMINAL_G) { return 0; } if (!GLERMINAL_G) { return 0; }
return GLERMINAL_G->get(x + 1, y + 1, layer); return GLERMINAL_G->get(x, y, layer);
} }
void glerminal_offset(int x, int y, int layer, float x_offset, float y_offset) void glerminal_offset(unsigned char x, unsigned char y, unsigned char layer, float x_offset, float y_offset)
{ {
if (!GLERMINAL_G) { return; } if (!GLERMINAL_G) { return; }
GLERMINAL_G->offset(x + 1, y + 1, layer, x_offset, y_offset); GLERMINAL_G->offset(x, y, layer, x_offset, y_offset);
} }
void glerminal_color(int x, int y, int layer, unsigned int color) void glerminal_color(unsigned char x, unsigned char y, unsigned char layer, unsigned int color)
{ {
if (!GLERMINAL_G) { return; } if (!GLERMINAL_G) { return; }
GLERMINAL_G->color(x + 1, y + 1, layer, color); GLERMINAL_G->color(x, y, layer, color);
} }
void glerminal_scale(int x, int y, int layer, float scale) void glerminal_scale(unsigned char x, unsigned char y, unsigned char layer, float scale)
{ {
if (!GLERMINAL_G) { return; } if (!GLERMINAL_G) { return; }
GLERMINAL_G->scale(x + 1, y + 1, layer, scale); GLERMINAL_G->scale(x, y, layer, scale);
} }
int glerminal_load_sprites_file(const char* filename) int glerminal_load_sprites_file(const char* filename)
@ -950,7 +896,7 @@ int glerminal_load_sprites_file(const char* filename)
stbi_uc* const buffer = stbi_load(filename, &w, &h, nullptr, 4); stbi_uc* const buffer = stbi_load(filename, &w, &h, nullptr, 4);
// verify atlas size is a multiple of CELL_SIZE in each dimension // verify atlas size is a multiple of CELL_SIZE in each dimension
if (buffer && w % glerminal::CELL_SIZE == 0 && h % glerminal::CELL_SIZE == 0) 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)); GLERMINAL_G->load_atlas(w / glerminal::CELL_SIZE, h / glerminal::CELL_SIZE, reinterpret_cast<unsigned int*>(buffer));
@ -977,11 +923,4 @@ int glerminal_load_sprites_buffer(unsigned char width, unsigned char height, con
{ {
return false; return false;
} }
}
void glerminal_sound(const char* name)
{
if (!GLERMINAL_G) { return; }
GLERMINAL_G->play_sound(name);
} }

View File

@ -1,7 +0,0 @@
#ifndef GLERMINAL_MINIAUDIO_H
#define GLERMINAL_MINIAUDIO_H
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#endif//GLERMINAL_MINIAUDIO_H

File diff suppressed because it is too large Load Diff

View File

@ -25,12 +25,12 @@ namespace
glerminal_quit(); glerminal_quit();
} }
void mainloop(double) {} void mainloop(float) {}
} }
int main() int main()
{ {
glerminal_run({init, mainloop}); glerminal_run(init, mainloop, nullptr, nullptr);
return 0; return 0;
} }

View File

@ -8,7 +8,7 @@
namespace namespace
{ {
unsigned char pixels[(GRID_WIDTH * 8) * (GRID_HEIGHT * 8) * 3]; unsigned char pixels[(GRID_WIDTH * CELL_SCALE * 8) * (GRID_HEIGHT * CELL_SCALE * 8) * 3];
} }
void glerminal_test_save_image() void glerminal_test_save_image()
@ -21,5 +21,5 @@ void glerminal_test_save_image()
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels); glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
stbi_flip_vertically_on_write(true); stbi_flip_vertically_on_write(true);
stbi_write_png("image.png", GRID_WIDTH * 8, GRID_HEIGHT * 8, 3, pixels, GRID_WIDTH * 8 * 3); stbi_write_png("image.png", GRID_WIDTH * CELL_SCALE * 8, GRID_HEIGHT * CELL_SCALE * 8, 3, pixels, GRID_WIDTH * CELL_SCALE * 8 * 3);
} }