Forráskód Böngészése

Fix several bugs:
- Not properly null-terminating file buffers
- Error printing for openGL
- Not clearing batch renders

Sam Jaffe 6 éve
szülő
commit
faeb4f8f0c

+ 5 - 9
graphics/src/opengl_helper.cxx

@@ -61,9 +61,6 @@ namespace {
     errorLineNumber =
         errorLog.substr(openParen + 1, closeParen - openParen - 1);
 
-    errorString << fileName << " , "
-                << "line " << errorLineNumber << ":\n";
-
     std::string errorStart = errorLog.find("error") != std::string::npos
                                  ? errorLog.substr(errorLog.find("error"))
                                  : errorLog;
@@ -90,18 +87,17 @@ namespace {
     MessageBoxA(nullptr, errorString.str().c_str(), eWindowTitle.c_str(),
                 MB_OK);
 #else
-    std::cerr << consoleOutput.str() << std::endl;
     std::cerr << errorString.str() << std::endl;
 #endif
   }
 
   void error_formatter::shader_error(GLuint id, std::string const & path) {
-    std::string fileName = path.substr(path.find_last_of("/"));
+    std::string fileName = path.substr(path.find_last_of("/") + 1);
     error_formatter{id,
                     fileName,
                     path,
                     "GLSL shader compile error!",
-                    "File location" + path,
+                    "File location: " + path,
                     "GLSL Compile Error in " + fileName,
                     glGetShaderiv,
                     glGetShaderInfoLog}();
@@ -110,7 +106,7 @@ namespace {
   void error_formatter::shader_program_error(GLuint id,
                                              std::string const & frag_path,
                                              std::string const & vert_path) {
-    std::string fileName = frag_path.substr(frag_path.find_last_of("/"));
+    std::string fileName = frag_path.substr(frag_path.find_last_of("/") + 1);
     error_formatter{id,
                     fileName,
                     frag_path,
@@ -227,7 +223,7 @@ namespace graphics { namespace shaders {
 
     // 1. Load the vertex shader code (text file) to a new memory buffer
     std::string const abs_path = env::resource_file(path);
-    if ((buffer = files::load(abs_path))) {
+    if (!(buffer = files::load(abs_path))) {
       throw file_read_error("Could not load shader file " + abs_path);
     }
 
@@ -237,7 +233,7 @@ namespace graphics { namespace shaders {
 
     // 3. Associate the shader code with the new shader ID
     char const * buffer_ptr = buffer.get();
-    glShaderSource(id, 1, &buffer_ptr, nullptr);
+    glShaderSource(id, 1, &buffer_ptr, NULL);
 
     // 4. Compile the shader (the shader compiler is built in to your graphics
     //    card driver)

+ 1 - 1
graphics/src/renderer.cxx

@@ -44,7 +44,7 @@ void direct_renderer::flush() { pimpl->flush(); }
 batch_renderer::batch_renderer(renderer * impl, std::size_t batch_size)
     : impl_(impl), batches_(), batch_size_(batch_size), elements_(0) {}
 
-batch_renderer::~batch_renderer() {}
+batch_renderer::~batch_renderer() { flush(); }
 
 void batch_renderer::draw(object const & obj) {
   std::vector<vertex> & batch_verts = batches_[obj.material];

+ 3 - 2
graphics/src/texture.cpp

@@ -17,6 +17,7 @@
 #include "stb/stb_image.h"
 #pragma clang diagnostic pop
 
+#include "game/util/env.hpp"
 #include "game/util/hash.hpp"
 #include "helper.hpp"
 
@@ -46,8 +47,8 @@ flyweight<texture> texture::create(std::string const & imagefile) {
 
   int components = 0;
   math::vec2i size;
-  unsigned char * data =
-      stbi_load(imagefile.c_str(), &size.x(), &size.y(), &components, 0);
+  std::string file = env::resource_file(imagefile);
+  auto data = stbi_load(file.c_str(), &size.x(), &size.y(), &components, 0);
   scope(exit) { stbi_image_free(data); };
   auto id = textures::init(format(components), size, data);
   flyweight<texture> fly{id, {id, size}};

+ 1 - 1
util/src/files.cxx

@@ -29,7 +29,7 @@ namespace files {
       fputs("Error reading file", stderr);
       return nullptr;
     } else {
-      buffer[read + 1] = '\0';
+      buffer[read] = '\0';
     }
     return buffer;
   }