| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- //
- // texture.cpp
- // graphics
- //
- // Created by Sam Jaffe on 7/5/16.
- //
- #include "game/graphics/texture.hpp"
- #include <unordered_map>
- #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/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;
- }
- }
- 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 + "'");
- }
- color = as_format(components);
- }
- external_data::~external_data() {
- if (buffer) { stbi_image_free(buffer); }
- }
- texture::texture(unsigned int id, math::vec2i const & size)
- : identity<texture>(id), size(size) {}
|