Browse Source

Merge file and gl_extra translation units into simple_renderer

rexim 2 years ago
parent
commit
55c1012193
8 changed files with 103 additions and 134 deletions
  1. 1 1
      build.sh
  2. 0 36
      src/file.c
  3. 0 6
      src/file.h
  4. 0 1
      src/free_glyph.c
  5. 0 72
      src/gl_extra.c
  6. 0 16
      src/gl_extra.h
  7. 0 1
      src/main.c
  8. 102 1
      src/simple_renderer.c

+ 1 - 1
build.sh

@@ -6,7 +6,7 @@ CC="${CXX:-cc}"
 PKGS="sdl2 glew freetype2"
 PKGS="sdl2 glew freetype2"
 CFLAGS="-Wall -Wextra -std=c11 -pedantic -ggdb"
 CFLAGS="-Wall -Wextra -std=c11 -pedantic -ggdb"
 LIBS=-lm
 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
 if [ `uname` = "Darwin" ]; then
     CFLAGS+=" -framework OpenGL"
     CFLAGS+=" -framework OpenGL"

+ 0 - 36
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
-}

+ 0 - 6
src/file.h

@@ -1,6 +0,0 @@
-#ifndef FILE_H_
-#define FILE_H_
-
-char *slurp_file(const char *file_path);
-
-#endif // FILE_H_

+ 0 - 1
src/free_glyph.c

@@ -1,7 +1,6 @@
 #include <assert.h>
 #include <assert.h>
 #include <stdbool.h>
 #include <stdbool.h>
 #include "./free_glyph.h"
 #include "./free_glyph.h"
-#include "./gl_extra.h"
 
 
 void free_glyph_atlas_init(Free_Glyph_Atlas *atlas, FT_Face face)
 void free_glyph_atlas_init(Free_Glyph_Atlas *atlas, FT_Face face)
 {
 {

+ 0 - 72
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;
-}

+ 0 - 16
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_

+ 0 - 1
src/main.c

@@ -16,7 +16,6 @@
 
 
 #include "./editor.h"
 #include "./editor.h"
 #include "./la.h"
 #include "./la.h"
-#include "./gl_extra.h"
 #include "./free_glyph.h"
 #include "./free_glyph.h"
 #include "./simple_renderer.h"
 #include "./simple_renderer.h"
 
 

+ 102 - 1
src/simple_renderer.c

@@ -1,8 +1,109 @@
 #include <assert.h>
 #include <assert.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include "./simple_renderer.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 {
 typedef struct {
     Uniform_Slot slot;
     Uniform_Slot slot;