commit 55c101219394ae01cab80a3b8d67312fa248e580
parent 7e97a9aad4935722f11d08d74714cedb39c1d7f4
Author: rexim <reximkut@gmail.com>
Date: Mon, 9 Jan 2023 13:44:54 +0700
Merge file and gl_extra translation units into simple_renderer
Diffstat:
8 files changed, 103 insertions(+), 134 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/file.c src/gl_extra.c src/free_glyph.c src/simple_renderer.c"
+SRC="src/main.c src/la.c src/editor.c src/free_glyph.c src/simple_renderer.c"
if [ `uname` = "Darwin" ]; then
CFLAGS+=" -framework OpenGL"
diff --git a/src/file.c b/src/file.c
@@ -1,36 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include "./file.h"
-
-char *slurp_file(const char *file_path)
-{
-#define SLURP_FILE_PANIC \
- do { \
- fprintf(stderr, "Could not read file `%s`: %s\n", file_path, strerror(errno)); \
- exit(1); \
- } while (0)
-
- FILE *f = fopen(file_path, "r");
- if (f == NULL) SLURP_FILE_PANIC;
- if (fseek(f, 0, SEEK_END) < 0) SLURP_FILE_PANIC;
-
- long size = ftell(f);
- if (size < 0) SLURP_FILE_PANIC;
-
- char *buffer = malloc(size + 1);
- if (buffer == NULL) SLURP_FILE_PANIC;
-
- if (fseek(f, 0, SEEK_SET) < 0) SLURP_FILE_PANIC;
-
- fread(buffer, 1, size, f);
- if (ferror(f) < 0) SLURP_FILE_PANIC;
-
- buffer[size] = '\0';
-
- if (fclose(f) < 0) SLURP_FILE_PANIC;
-
- return buffer;
-#undef SLURP_FILE_PANIC
-}
diff --git a/src/file.h b/src/file.h
@@ -1,6 +0,0 @@
-#ifndef FILE_H_
-#define FILE_H_
-
-char *slurp_file(const char *file_path);
-
-#endif // FILE_H_
diff --git a/src/free_glyph.c b/src/free_glyph.c
@@ -1,7 +1,6 @@
#include <assert.h>
#include <stdbool.h>
#include "./free_glyph.h"
-#include "./gl_extra.h"
void free_glyph_atlas_init(Free_Glyph_Atlas *atlas, FT_Face face)
{
diff --git a/src/gl_extra.c b/src/gl_extra.c
@@ -1,72 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include "file.h"
-#include "gl_extra.h"
-
-const char *shader_type_as_cstr(GLuint shader)
-{
- switch (shader) {
- case GL_VERTEX_SHADER:
- return "GL_VERTEX_SHADER";
- case GL_FRAGMENT_SHADER:
- return "GL_FRAGMENT_SHADER";
- default:
- return "(Unknown)";
- }
-}
-
-bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader)
-{
- *shader = glCreateShader(shader_type);
- glShaderSource(*shader, 1, &source, NULL);
- glCompileShader(*shader);
-
- GLint compiled = 0;
- glGetShaderiv(*shader, GL_COMPILE_STATUS, &compiled);
-
- if (!compiled) {
- GLchar message[1024];
- GLsizei message_size = 0;
- glGetShaderInfoLog(*shader, sizeof(message), &message_size, message);
- fprintf(stderr, "ERROR: could not compile %s\n", shader_type_as_cstr(shader_type));
- fprintf(stderr, "%.*s\n", message_size, message);
- return false;
- }
-
- return true;
-}
-
-bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader)
-{
- char *source = slurp_file(file_path);
- bool ok = compile_shader_source(source, shader_type, shader);
- if (!ok) {
- fprintf(stderr, "ERROR: failed to compile `%s` shader file\n", file_path);
- }
- free(source);
- return ok;
-}
-
-void attach_shaders_to_program(GLuint *shaders, size_t shaders_count, GLuint program)
-{
- for (size_t i = 0; i < shaders_count; ++i) {
- glAttachShader(program, shaders[i]);
- }
-}
-
-bool link_program(GLuint program, const char *file_path, size_t line)
-{
- glLinkProgram(program);
-
- GLint linked = 0;
- glGetProgramiv(program, GL_LINK_STATUS, &linked);
- if (!linked) {
- GLsizei message_size = 0;
- GLchar message[1024];
-
- glGetProgramInfoLog(program, sizeof(message), &message_size, message);
- fprintf(stderr, "%s:%zu: Program Linking: %.*s\n", file_path, line, message_size, message);
- }
-
- return linked;
-}
diff --git a/src/gl_extra.h b/src/gl_extra.h
@@ -1,16 +0,0 @@
-#ifndef GL_EXTRA_H_
-#define GL_EXTRA_H_
-
-#define GLEW_STATIC
-#include <GL/glew.h>
-#define GL_GLEXT_PROTOTYPES
-#include <SDL2/SDL_opengl.h>
-
-#include <stdbool.h>
-
-bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader);
-bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader);
-void attach_shaders_to_program(GLuint *shaders, size_t shaders_count, GLuint program);
-bool link_program(GLuint program, const char *file_path, size_t line);
-
-#endif // GL_EXTRA_H_
diff --git a/src/main.c b/src/main.c
@@ -16,7 +16,6 @@
#include "./editor.h"
#include "./la.h"
-#include "./gl_extra.h"
#include "./free_glyph.h"
#include "./simple_renderer.h"
diff --git a/src/simple_renderer.c b/src/simple_renderer.c
@@ -1,8 +1,109 @@
#include <assert.h>
#include <stdio.h>
+#include <stdbool.h>
#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
#include "./simple_renderer.h"
-#include "./gl_extra.h"
+
+static char *slurp_file(const char *file_path)
+{
+#define SLURP_FILE_PANIC \
+ do { \
+ fprintf(stderr, "Could not read file `%s`: %s\n", file_path, strerror(errno)); \
+ exit(1); \
+ } while (0)
+
+ FILE *f = fopen(file_path, "r");
+ if (f == NULL) SLURP_FILE_PANIC;
+ if (fseek(f, 0, SEEK_END) < 0) SLURP_FILE_PANIC;
+
+ long size = ftell(f);
+ if (size < 0) SLURP_FILE_PANIC;
+
+ char *buffer = malloc(size + 1);
+ if (buffer == NULL) SLURP_FILE_PANIC;
+
+ if (fseek(f, 0, SEEK_SET) < 0) SLURP_FILE_PANIC;
+
+ fread(buffer, 1, size, f);
+ if (ferror(f) < 0) SLURP_FILE_PANIC;
+
+ buffer[size] = '\0';
+
+ if (fclose(f) < 0) SLURP_FILE_PANIC;
+
+ return buffer;
+#undef SLURP_FILE_PANIC
+}
+
+static const char *shader_type_as_cstr(GLuint shader)
+{
+ switch (shader) {
+ case GL_VERTEX_SHADER:
+ return "GL_VERTEX_SHADER";
+ case GL_FRAGMENT_SHADER:
+ return "GL_FRAGMENT_SHADER";
+ default:
+ return "(Unknown)";
+ }
+}
+
+static bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader)
+{
+ *shader = glCreateShader(shader_type);
+ glShaderSource(*shader, 1, &source, NULL);
+ glCompileShader(*shader);
+
+ GLint compiled = 0;
+ glGetShaderiv(*shader, GL_COMPILE_STATUS, &compiled);
+
+ if (!compiled) {
+ GLchar message[1024];
+ GLsizei message_size = 0;
+ glGetShaderInfoLog(*shader, sizeof(message), &message_size, message);
+ fprintf(stderr, "ERROR: could not compile %s\n", shader_type_as_cstr(shader_type));
+ fprintf(stderr, "%.*s\n", message_size, message);
+ return false;
+ }
+
+ return true;
+}
+
+static bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader)
+{
+ char *source = slurp_file(file_path);
+ bool ok = compile_shader_source(source, shader_type, shader);
+ if (!ok) {
+ fprintf(stderr, "ERROR: failed to compile `%s` shader file\n", file_path);
+ }
+ free(source);
+ return ok;
+}
+
+static void attach_shaders_to_program(GLuint *shaders, size_t shaders_count, GLuint program)
+{
+ for (size_t i = 0; i < shaders_count; ++i) {
+ glAttachShader(program, shaders[i]);
+ }
+}
+
+static bool link_program(GLuint program, const char *file_path, size_t line)
+{
+ glLinkProgram(program);
+
+ GLint linked = 0;
+ glGetProgramiv(program, GL_LINK_STATUS, &linked);
+ if (!linked) {
+ GLsizei message_size = 0;
+ GLchar message[1024];
+
+ glGetProgramInfoLog(program, sizeof(message), &message_size, message);
+ fprintf(stderr, "%s:%zu: Program Linking: %.*s\n", file_path, line, message_size, message);
+ }
+
+ return linked;
+}
typedef struct {
Uniform_Slot slot;