فهرست منبع

Move file_read_error to exception header location.

Sam Jaffe 6 سال پیش
والد
کامیت
241eecfaa3
3فایلهای تغییر یافته به همراه25 افزوده شده و 8 حذف شده
  1. 21 0
      graphics/include/game/graphics/exception.h
  2. 2 5
      graphics/src/openGL/opengl_manager.cxx
  3. 2 3
      graphics/src/texture.cpp

+ 21 - 0
graphics/include/game/graphics/exception.h

@@ -0,0 +1,21 @@
+//
+//  exception.h
+//  graphics
+//
+//  Created by Sam Jaffe on 7/6/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <stdexcept>
+#include <string>
+
+namespace graphics {
+  struct file_read_error : std::runtime_error {
+    file_read_error(std::string const & file)
+        : std::runtime_error("Unable to read file " + file) {}
+    file_read_error(std::string const & type, std::string const & file)
+        : std::runtime_error("Unable to read " + type + " file " + file) {}
+  };
+}

+ 2 - 5
graphics/src/openGL/opengl_manager.cxx

@@ -19,6 +19,7 @@
 #include "scope_guard/scope_guard.hpp"
 #include "vector/vector.hpp"
 
+#include "game/graphics/exception.h"
 #include "game/graphics/material.hpp"
 #include "game/graphics/shader.hpp"
 #include "game/graphics/shader_program.hpp"
@@ -32,10 +33,6 @@ namespace graphics {
   extern void print_shader_program_error(GLuint, std::string const &,
                                          std::string const &);
 
-  struct file_read_error : std::runtime_error {
-    using std::runtime_error::runtime_error;
-  };
-
   struct compilation_error : std::runtime_error {
     using std::runtime_error::runtime_error;
   };
@@ -143,7 +140,7 @@ shader opengl_manager::compile(shaders::type tp,
   // 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))) {
-    throw file_read_error("Could not load shader file " + abs_path);
+    throw file_read_error("shader", abs_path);
   }
 
   // 2. Create a new shader ID

+ 2 - 3
graphics/src/texture.cpp

@@ -17,6 +17,7 @@
 #include "stb/stb_image.h"
 #pragma clang diagnostic pop
 
+#include "game/graphics/exception.h"
 #include "game/util/env.hpp"
 #include "game/util/hash.hpp"
 #include "helper.hpp"
@@ -42,9 +43,7 @@ external_data::external_data(std::string const & rel_path) {
   std::string abs_path = env::resource_file(rel_path);
   int components = 0;
   buffer = stbi_load(abs_path.c_str(), &size.x(), &size.y(), &components, 0);
-  if (!buffer) {
-    throw std::runtime_error("Unable to read file: '" + rel_path + "'");
-  }
+  if (!buffer) { throw file_read_error("texture", rel_path); }
   color = as_format(components);
 }