// // texture.cpp // graphics // // Created by Sam Jaffe on 7/5/16. // #include "game/graphics/texture.hpp" #include #include "scope_guard/scope_guard.hpp" #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wcomma" #define STB_IMAGE_IMPLEMENTATION #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" unsigned char * stbi_load(char const *, int *, int *, int *, int); void stbi_image_free(void *); using namespace graphics; using namespace textures; static format as_format(int comps) { switch (comps) { case 3: return format::RGB; case 4: return format::RGBA; default: throw unknown_texture_format(comps); } } 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 file_read_error("texture", rel_path); } color = as_format(components); } external_data::~external_data() { if (buffer) { stbi_image_free(buffer); } } texture::texture(unsigned int id, math::vec2i const & size) : identity(id), size(size) {}