From 7a3b8488cbfdef6a065dfe6efa3873fd579b8baf Mon Sep 17 00:00:00 2001 From: dekrain Date: Fri, 6 Jan 2023 04:37:21 +0100 Subject: [PATCH 1/3] Revert #24 macOS workaround --- src/graphics/shader.cpp | 55 ++++++++--------------------------------- src/graphics/shader.hpp | 13 ++++------ 2 files changed, 15 insertions(+), 53 deletions(-) diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 6a513d0..fae4e18 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -9,41 +9,11 @@ namespace gl { -#ifndef __APPLE__ static std::string_view bytes_as_string_view(const std::vector& vec) { return std::string_view(reinterpret_cast(vec.data()), vec.size()); } -#else -// Load a whole file as a string. -const GLchar* load_file_as_string(const char* path) { - // TODO - do this with C++ classes. - FILE* fp = fopen(path, "r"); - if (!fp) return nullptr; - fseek(fp, 0, SEEK_END); - int len = ftell(fp); - // Yes, I know the memes. Haha. Funny. - char* code = (char*) malloc(len + 1); - rewind(fp); - for (int pos = 0; pos < len; ++pos) { - code[pos] = getc(fp); - } - fclose(fp); - code[len] = 0; - return code; -} -#endif // __APPLE__ - -Shader::Shader(ZStringView vertex_fname, ZStringView fragment_fname) { -#ifndef __APPLE__ - std::vector vvertex_source = load_file_as_bytes(vertex_fname); - std::vector vfragment_source = load_file_as_bytes(fragment_fname); - std::string_view vertex_source{reinterpret_cast(vvertex_source.data()), vvertex_source.size()}; - std::string_view fragment_source{reinterpret_cast(vfragment_source.data()), vfragment_source.size()}; -#else - auto vertex_source = load_file_as_string(vertex_fname); - auto fragment_source = load_file_as_string(fragment_fname); -#endif // __APPLE__ +Shader::Shader(std::string_view vertex_source, std::string_view fragment_source) { this->program_id = glCreateProgram(); // TODO - check for error @@ -52,28 +22,23 @@ Shader::Shader(ZStringView vertex_fname, ZStringView fragment_fname) { int frag = create_subshader(fragment_source, GL_FRAGMENT_SHADER); this->link(vert, frag); +} -#ifdef __APPLE__ - free((void *) vertex_source); - free((void *) fragment_source); -#endif // __APPLE__ - +Shader Shader::load_from_file(ZStringView vertex_fname, ZStringView fragment_fname) { + std::vector vertcode = load_file_as_bytes(vertex_fname); + std::vector fragcode = load_file_as_bytes(fragment_fname); + return Shader(bytes_as_string_view(vertcode), bytes_as_string_view(fragcode)); } -int Shader::create_subshader(FileContents source, GLenum type) { +int Shader::create_subshader(std::string_view source, GLenum type) { int id = glCreateShader(type); if (!id) { // TODO -- log some error } -#ifndef __APPLE__ - GLchar const * src = source.data(); + const char* src = source.data(); int len = source.size(); -#else - GLchar const * src = source; - int len = strlen(source); -#endif // __APPLE__ glShaderSource(id, 1, &src, &len); glCompileShader(id); @@ -136,9 +101,9 @@ void Shader::set_uniform_value(std::string name, float value) { } void load_all_shaders() { - GAME_SHADER = std::make_unique( + GAME_SHADER = std::make_unique(Shader::load_from_file( "src/shader/basic_vert.glsl", "src/shader/basic_frag.glsl" - ); + )); } void unload_all_shaders() { diff --git a/src/graphics/shader.hpp b/src/graphics/shader.hpp index c257baa..b30ccac 100644 --- a/src/graphics/shader.hpp +++ b/src/graphics/shader.hpp @@ -11,12 +11,6 @@ #include -#ifdef __APPLE__ -typedef const GLchar * FileContents; -#else -typedef std::string_view FileContents; -#endif // __APPLE__ - namespace gl { class Shader { @@ -25,7 +19,7 @@ class Shader { // The OpenGL ID of this shader. unsigned program_id; // Create a single vertex or fragment shader. - int create_subshader(FileContents source, GLenum type); + int create_subshader(std::string_view source, GLenum type); // Finalize the shader. void link(int vert, int frag); @@ -34,9 +28,12 @@ class Shader { public: // Take in the file paths. - Shader(ZStringView vertex_source, ZStringView fragment_source); + Shader(std::string_view vertex_source, std::string_view fragment_source); ~Shader(); + static Shader load_from_file(ZStringView vertex_fname, + ZStringView fragment_fname); + void bind(); // set as active shader void unbind(); void destroy(); From 2f06aaad15fd4b6313efa556017070b382a48b58 Mon Sep 17 00:00:00 2001 From: dekrain Date: Fri, 6 Jan 2023 04:42:29 +0100 Subject: [PATCH 2/3] Fix stat return value typo --- src/system/file.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/system/file.cpp b/src/system/file.cpp index a0d21c4..06a9026 100644 --- a/src/system/file.cpp +++ b/src/system/file.cpp @@ -54,7 +54,7 @@ static LoadResult load_file(ZStringView filename) { do { struct stat stat; int fd = fp.fd(); - if (fstat(fd, &stat) < -1) { + if (fstat(fd, &stat) < 0) { break; } if (stat.st_size == 0) { From c5a4e489c6ba2c6631afee359759a4a9d5c22ac1 Mon Sep 17 00:00:00 2001 From: dekrain Date: Fri, 6 Jan 2023 04:43:11 +0100 Subject: [PATCH 3/3] Detect macOS when using mmap --- src/system/file.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/system/file.cpp b/src/system/file.cpp index 06a9026..5554b6a 100644 --- a/src/system/file.cpp +++ b/src/system/file.cpp @@ -6,7 +6,11 @@ #include #include -#if defined(__unix__) +#if defined(__unix__) || defined(__APPLE__) +#define FILE_HAS_POSIX +#endif + +#if defined(FILE_HAS_POSIX) #include #include #endif @@ -36,7 +40,7 @@ struct FileHandle { } } - #if defined(__unix__) + #if defined(FILE_HAS_POSIX) int fd() const { return fileno(fp); } @@ -50,7 +54,7 @@ static LoadResult load_file(ZStringView filename) { return LoadResult::last_errno(); // Strategy 1. Try mmap - #if defined (__unix__) + #if defined(FILE_HAS_POSIX) do { struct stat stat; int fd = fp.fd();