commit ecf0aa26737931e8bf87f0a69f1748089a3855d3
parent 5261991f8ed18cdca49f34d7c7c62bb8fecfe0c9
Author: rexim <reximkut@gmail.com>
Date: Sun, 18 Jul 2021 19:56:24 +0700
Remove legacy Tile Glyph Renderer
Diffstat:
5 files changed, 4 insertions(+), 317 deletions(-)
diff --git a/build.sh b/build.sh
@@ -6,7 +6,7 @@ CC="${CXX:-cc}"
PKGS="sdl2 glew freetype2"
CFLAGS="-Wall -Wextra -std=c11 -pedantic -ggdb"
LIBS=-lm
-SRC="src/main.c src/la.c src/editor.c src/sdl_extra.c src/file.c src/gl_extra.c src/tile_glyph.c src/free_glyph.c src/cursor_renderer.c"
+SRC="src/main.c src/la.c src/editor.c src/sdl_extra.c src/file.c src/gl_extra.c src/free_glyph.c src/cursor_renderer.c"
if [ `uname` = "Darwin" ]; then
CFLAGS+=" -framework OpenGL"
diff --git a/charmap-oldschool_white.png b/charmap-oldschool_white.png
Binary files differ.
diff --git a/src/main.c b/src/main.c
@@ -24,7 +24,6 @@
#include "./la.h"
#include "./sdl_extra.h"
#include "./gl_extra.h"
-#include "./tile_glyph.h"
#include "./free_glyph.h"
#include "./cursor_renderer.h"
@@ -71,70 +70,11 @@ void MessageCallback(GLenum source,
type, severity, message);
}
-void gl_render_cursor(Tile_Glyph_Buffer *tgb)
-{
- const char *c = editor_char_under_cursor(&editor);
- Vec2i tile = vec2i((int) editor.cursor_col, -(int) editor.cursor_row);
- tile_glyph_render_line_sized(tgb, c ? c : " ", 1, tile, vec4fs(0.0f), vec4fs(1.0f));
-}
-
-// #define TILE_GLYPH_RENDER
-
-#ifdef TILE_GLYPH_RENDER
-static Tile_Glyph_Buffer tgb = {0};
-#else
static Free_Glyph_Buffer fgb = {0};
static Cursor_Renderer cr = {0};
-#endif
-
-void render_editor_into_tgb(SDL_Window *window, Tile_Glyph_Buffer *tgb, Editor *editor)
-{
- {
- // TODO: Zoom
- const Vec2f cursor_pos =
- vec2f((float) editor->cursor_col * FONT_CHAR_WIDTH * FONT_SCALE,
- (float) (-(int)editor->cursor_row) * FONT_CHAR_HEIGHT * FONT_SCALE);
-
- camera_vel = vec2f_mul(
- vec2f_sub(cursor_pos, camera_pos),
- vec2fs(2.0f));
-
- camera_pos = vec2f_add(camera_pos, vec2f_mul(camera_vel, vec2fs(DELTA_TIME)));
- }
-
- {
- int w, h;
- SDL_GetWindowSize(window, &w, &h);
- glUniform2f(tgb->resolution_uniform, (float) w, (float) h);
- }
-
- tile_glyph_buffer_clear(tgb);
- {
- for (size_t row = 0; row < editor->size; ++row) {
- const Line *line = editor->lines + row;
-
- tile_glyph_render_line_sized(tgb, line->chars, line->size, vec2i(0, -(int)row), vec4fs(1.0f), vec4fs(0.0f));
- }
- }
- tile_glyph_buffer_sync(tgb);
-
- glUniform1f(tgb->time_uniform, (float) SDL_GetTicks() / 1000.0f);
- glUniform2f(tgb->camera_uniform, camera_pos.x, camera_pos.y);
-
- tile_glyph_buffer_draw(tgb);
-
- tile_glyph_buffer_clear(tgb);
- {
- gl_render_cursor(tgb);
- }
- tile_glyph_buffer_sync(tgb);
-
- tile_glyph_buffer_draw(tgb);
-}
#define FREE_GLYPH_FONT_SIZE 64
-
void render_editor_into_fgb(SDL_Window *window, Free_Glyph_Buffer *fgb, Cursor_Renderer *cr, Editor *editor)
{
int w, h;
@@ -202,7 +142,8 @@ int main(int argc, char **argv)
exit(1);
}
- const char *const font_file_path = "./VictorMono-Regular.ttf";
+ // const char *const font_file_path = "./VictorMono-Regular.ttf";
+ const char *const font_file_path = "./ComicNeue-Regular.otf";
FT_Face face;
error = FT_New_Face(library, font_file_path, 0, &face);
@@ -284,12 +225,7 @@ int main(int argc, char **argv)
fprintf(stderr, "WARNING! GLEW_ARB_debug_output is not available");
}
-#ifdef TILE_GLYPH_RENDER
- tile_glyph_buffer_init(&tgb,
- "./charmap-oldschool_white.png",
- "./shaders/tile_glyph.vert",
- "./shaders/tile_glyph.frag");
-#else
+
free_glyph_buffer_init(&fgb,
face,
"./shaders/free_glyph.vert",
@@ -297,7 +233,6 @@ int main(int argc, char **argv)
cursor_renderer_init(&cr,
"./shaders/cursor.vert",
"./shaders/cursor.frag");
-#endif
bool quit = false;
while (!quit) {
@@ -411,11 +346,7 @@ int main(int argc, char **argv)
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
-#ifdef TILE_GLYPH_RENDER
- render_editor_into_tgb(window, &tgb, &editor);
-#else
render_editor_into_fgb(window, &fgb, &cr, &editor);
-#endif // TILE_GLYPH_RENDER
SDL_GL_SwapWindow(window);
diff --git a/src/tile_glyph.c b/src/tile_glyph.c
@@ -1,178 +0,0 @@
-#include <assert.h>
-#include <string.h>
-#include <stdbool.h>
-#include "./stb_image.h"
-#include "./tile_glyph.h"
-#include "./gl_extra.h"
-
-typedef struct {
- size_t offset;
- GLint comps;
- GLenum type;
-} Attr_Def;
-
-static const Attr_Def glyph_attr_defs[COUNT_TILE_GLYPH_ATTRS] = {
- [TILE_GLYPH_ATTR_TILE] = {
- .offset = offsetof(Tile_Glyph, tile),
- .comps = 2,
- .type = GL_INT
- },
- [TILE_GLYPH_ATTR_CH] = {
- .offset = offsetof(Tile_Glyph, ch),
- .comps = 1,
- .type = GL_INT
- },
- [TILE_GLYPH_ATTR_FG_COLOR] = {
- .offset = offsetof(Tile_Glyph, fg_color),
- .comps = 4,
- .type = GL_FLOAT
- },
- [TILE_GLYPH_ATTR_BG_COLOR] = {
- .offset = offsetof(Tile_Glyph, bg_color),
- .comps = 4,
- .type = GL_FLOAT
- },
-};
-static_assert(COUNT_TILE_GLYPH_ATTRS == 4, "The amount of glyph vertex attributes have changed");
-
-void tile_glyph_buffer_clear(Tile_Glyph_Buffer *tgb)
-{
- tgb->glyphs_count = 0;
-}
-
-void tile_glyph_buffer_push(Tile_Glyph_Buffer *tgb, Tile_Glyph glyph)
-{
- assert(tgb->glyphs_count < TILE_GLYPH_BUFFER_CAP);
- tgb->glyphs[tgb->glyphs_count++] = glyph;
-}
-
-void tile_glyph_buffer_sync(Tile_Glyph_Buffer *tgb)
-{
- glBufferSubData(GL_ARRAY_BUFFER,
- 0,
- tgb->glyphs_count * sizeof(Tile_Glyph),
- tgb->glyphs);
-}
-
-void tile_glyph_render_line_sized(Tile_Glyph_Buffer *tgb, const char *text, size_t text_size, Vec2i tile, Vec4f fg_color, Vec4f bg_color)
-{
- for (size_t i = 0; i < text_size; ++i) {
- tile_glyph_buffer_push(tgb, (Tile_Glyph) {
- .tile = vec2i_add(tile, vec2i((int) i, 0)),
- .ch = text[i],
- .fg_color = fg_color,
- .bg_color = bg_color,
- });
- }
-}
-
-void tile_glyph_render_line(Tile_Glyph_Buffer *tgb, const char *text, Vec2i tile, Vec4f fg_color, Vec4f bg_color)
-{
- tile_glyph_render_line_sized(tgb, text, strlen(text), tile, fg_color, bg_color);
-}
-
-void tile_glyph_buffer_init(Tile_Glyph_Buffer *tgb, const char *texture_file_path, const char *vert_file_path, const char *frag_file_path)
-{
- // Init Vertex Attributes
- {
- glGenVertexArrays(1, &tgb->vao);
- glBindVertexArray(tgb->vao);
-
- glGenBuffers(1, &tgb->vbo);
- glBindBuffer(GL_ARRAY_BUFFER, tgb->vbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(tgb->glyphs), tgb->glyphs, GL_DYNAMIC_DRAW);
-
- for (Tile_Glyph_Attr attr = 0; attr < COUNT_TILE_GLYPH_ATTRS; ++attr) {
- glEnableVertexAttribArray(attr);
- switch (glyph_attr_defs[attr].type) {
- case GL_FLOAT:
- glVertexAttribPointer(
- attr,
- glyph_attr_defs[attr].comps,
- glyph_attr_defs[attr].type,
- GL_FALSE,
- sizeof(Tile_Glyph),
- (void*) glyph_attr_defs[attr].offset);
- break;
-
- case GL_INT:
- glVertexAttribIPointer(
- attr,
- glyph_attr_defs[attr].comps,
- glyph_attr_defs[attr].type,
- sizeof(Tile_Glyph),
- (void*) glyph_attr_defs[attr].offset);
- break;
-
- default:
- assert(false && "unreachable");
- exit(1);
- }
- glVertexAttribDivisor(attr, 1);
- }
- }
-
- // Init Shaders
- {
- GLuint shaders[2] = {0};
-
- if (!compile_shader_file(vert_file_path, GL_VERTEX_SHADER, &shaders[0])) {
- exit(1);
- }
- if (!compile_shader_file(frag_file_path, GL_FRAGMENT_SHADER, &shaders[1])) {
- exit(1);
- }
-
- GLuint program = glCreateProgram();
- attach_shaders_to_program(shaders, sizeof(shaders) / sizeof(shaders[0]), program);
- if (!link_program(program, __FILE__, __LINE__)) {
- exit(1);
- }
-
- glUseProgram(program);
-
- tgb->time_uniform = glGetUniformLocation(program, "time");
- tgb->resolution_uniform = glGetUniformLocation(program, "resolution");
- tgb->scale_uniform = glGetUniformLocation(program, "scale");
- tgb->camera_uniform = glGetUniformLocation(program, "camera");
-
- glUniform2f(tgb->scale_uniform, FONT_SCALE, FONT_SCALE);
- }
-
- // Init Texture
- {
- int width, height, n;
- unsigned char *pixels = stbi_load(texture_file_path, &width, &height, &n, STBI_rgb_alpha);
- if (pixels == NULL) {
- fprintf(stderr, "ERROR: could not load file %s: %s\n",
- texture_file_path, stbi_failure_reason());
- exit(1);
- }
-
- glActiveTexture(GL_TEXTURE0);
- glGenTextures(1, &tgb->font_texture);
- glBindTexture(GL_TEXTURE_2D, tgb->font_texture);
-
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-
- glTexImage2D(GL_TEXTURE_2D,
- 0,
- GL_RGBA,
- width,
- height,
- 0,
- GL_RGBA,
- GL_UNSIGNED_BYTE,
- pixels);
-
- stbi_image_free(pixels);
- }
-}
-
-void tile_glyph_buffer_draw(Tile_Glyph_Buffer *tgb)
-{
- glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, (GLsizei) tgb->glyphs_count);
-}
diff --git a/src/tile_glyph.h b/src/tile_glyph.h
@@ -1,66 +0,0 @@
-#ifndef TILE_GLYPH_H_
-#define TILE_GLYPH_H_
-
-#include <stdlib.h>
-#include "./la.h"
-
-#define GLEW_STATIC
-#include <GL/glew.h>
-
-#define GL_GLEXT_PROTOTYPES
-#include <SDL2/SDL_opengl.h>
-
-#define FONT_SCALE 5
-#define FONT_WIDTH 128
-#define FONT_HEIGHT 64
-#define FONT_COLS 18
-#define FONT_ROWS 7
-#define FONT_CHAR_WIDTH (FONT_WIDTH / FONT_COLS)
-#define FONT_CHAR_HEIGHT (FONT_HEIGHT / FONT_ROWS)
-
-typedef struct {
- Vec2i tile;
- int ch;
- Vec4f fg_color;
- Vec4f bg_color;
-} Tile_Glyph;
-
-typedef enum {
- TILE_GLYPH_ATTR_TILE = 0,
- TILE_GLYPH_ATTR_CH,
- TILE_GLYPH_ATTR_FG_COLOR,
- TILE_GLYPH_ATTR_BG_COLOR,
- COUNT_TILE_GLYPH_ATTRS,
-} Tile_Glyph_Attr;
-
-#define TILE_GLYPH_BUFFER_CAP (640 * 1024)
-
-typedef struct {
- GLuint vao;
- GLuint vbo;
-
- GLuint font_texture;
-
- GLint time_uniform;
- GLint resolution_uniform;
- GLint scale_uniform;
- GLint camera_uniform;
-
- size_t glyphs_count;
- Tile_Glyph glyphs[TILE_GLYPH_BUFFER_CAP];
-} Tile_Glyph_Buffer;
-
-void tile_glyph_buffer_init(Tile_Glyph_Buffer *buffer,
- const char *texture_file_path,
- const char *vert_file_path,
- const char *frag_file_path);
-void tile_glyph_buffer_clear(Tile_Glyph_Buffer *buffer);
-void tile_glyph_buffer_push(Tile_Glyph_Buffer *buffer, Tile_Glyph glyph);
-void tile_glyph_buffer_sync(Tile_Glyph_Buffer *buffer);
-void tile_glyph_buffer_draw(Tile_Glyph_Buffer *buffer);
-
-void tile_glyph_render_line_sized(Tile_Glyph_Buffer *buffer, const char *text, size_t text_size, Vec2i tile, Vec4f fg_color, Vec4f bg_color);
-void tile_glyph_render_line(Tile_Glyph_Buffer *buffer, const char *text, Vec2i tile, Vec4f fg_color, Vec4f bg_color);
-
-
-#endif // TILE_GLYPH_H_