Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file handling #42

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 10 additions & 45 deletions src/graphics/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,11 @@

namespace gl {

#ifndef __APPLE__
static std::string_view bytes_as_string_view(const std::vector<unsigned char>& vec) {
return std::string_view(reinterpret_cast<const char*>(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<unsigned char> vvertex_source = load_file_as_bytes(vertex_fname);
std::vector<unsigned char> vfragment_source = load_file_as_bytes(fragment_fname);
std::string_view vertex_source{reinterpret_cast<char *>(vvertex_source.data()), vvertex_source.size()};
std::string_view fragment_source{reinterpret_cast<char *>(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
Expand All @@ -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<unsigned char> vertcode = load_file_as_bytes(vertex_fname);
std::vector<unsigned char> 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);

Expand Down Expand Up @@ -136,9 +101,9 @@ void Shader::set_uniform_value(std::string name, float value) {
}

void load_all_shaders() {
GAME_SHADER = std::make_unique<Shader>(
GAME_SHADER = std::make_unique<Shader>(Shader::load_from_file(
"src/shader/basic_vert.glsl", "src/shader/basic_frag.glsl"
);
));
}

void unload_all_shaders() {
Expand Down
13 changes: 5 additions & 8 deletions src/graphics/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@

#include <glad/glad.h>

#ifdef __APPLE__
typedef const GLchar * FileContents;
#else
typedef std::string_view FileContents;
#endif // __APPLE__

namespace gl {

class Shader {
Expand All @@ -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);

Expand All @@ -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();
Expand Down
12 changes: 8 additions & 4 deletions src/system/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
#include <cerrno>
#include <cstring>

#if defined(__unix__)
#if defined(__unix__) || defined(__APPLE__)
#define FILE_HAS_POSIX
#endif

#if defined(FILE_HAS_POSIX)
#include <sys/mman.h>
#include <sys/stat.h>
#endif
Expand Down Expand Up @@ -36,7 +40,7 @@ struct FileHandle {
}
}

#if defined(__unix__)
#if defined(FILE_HAS_POSIX)
int fd() const {
return fileno(fp);
}
Expand All @@ -50,11 +54,11 @@ static LoadResult<T> load_file(ZStringView filename) {
return LoadResult<T>::last_errno();

// Strategy 1. Try mmap
#if defined (__unix__)
#if defined(FILE_HAS_POSIX)
do {
struct stat stat;
int fd = fp.fd();
if (fstat(fd, &stat) < -1) {
if (fstat(fd, &stat) < 0) {
break;
}
if (stat.st_size == 0) {
Expand Down