X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fimage.cc;h=77b15c13903bead2a4bd05bc33e3a0081fbd2f91;hp=09f797ca8f3fc3d9235fb7ccf4f1ad21f5f84eae;hb=44b3014bce798789e795242d1556cb7449e6386a;hpb=4f6e4488a55f7e3ba3f7485d78177f793c0eab9a diff --git a/src/moof/image.cc b/src/moof/image.cc index 09f797c..77b15c1 100644 --- a/src/moof/image.cc +++ b/src/moof/image.cc @@ -7,14 +7,16 @@ * *****************************************************************************/ -#include // strncmp #include #include - #include + #include +#include + #include "backend.hh" +#include "debug.hh" #include "image.hh" #include "log.hh" #include "opengl.hh" @@ -25,38 +27,83 @@ namespace moof { +MOOF_REGISTER_RESOURCE(image, bmp, textures); MOOF_REGISTER_RESOURCE(image, png, textures); -//static int power_of_two(int input) -//{ - //int value = 1; - - //while (value < input) - //{ - //value <<= 1; - //} - //return value; -//} - -unsigned image::global_object_ = 0; +static int higher_power_of_two(int input) +{ + int value = 2; + while (value <= input) value <<= 1; + return value; +} -static void read_from_stream(png_structp context, png_bytep data, png_size_t length) +static void read_from_stream(png_structp context, + png_bytep data, png_size_t length) { std::istream& stream(*(std::istream*)png_get_io_ptr(context)); stream.read((char*)data, length); } -image::image(const std::string& path) : - pixels_(0), - object_(0), - min_filter_(GL_NEAREST), - mag_filter_(GL_NEAREST), - tile_s_(1), - tile_t_(1), - wrap_s_(GL_CLAMP), - wrap_t_(GL_CLAMP) +struct texture_attributes +{ + texture_attributes() : + min_filter(GL_NEAREST), + mag_filter(GL_NEAREST), + tile_s(1), + tile_t(1), + wrap_s(GL_CLAMP_TO_EDGE), + wrap_t(GL_CLAMP_TO_EDGE) {} + + void init(const std::string& info) + { + script script; + log::import(script); + + script::slot g = script.globals(); +#define EXPORT_CONSTANT(K) g.set_field(#K, GL_##K) + EXPORT_CONSTANT(CLAMP); + EXPORT_CONSTANT(CLAMP_TO_EDGE); + EXPORT_CONSTANT(REPEAT); + EXPORT_CONSTANT(LINEAR); + EXPORT_CONSTANT(NEAREST); + EXPORT_CONSTANT(LINEAR_MIPMAP_LINEAR); + EXPORT_CONSTANT(LINEAR_MIPMAP_NEAREST); + EXPORT_CONSTANT(NEAREST_MIPMAP_LINEAR); + EXPORT_CONSTANT(NEAREST_MIPMAP_NEAREST); +#undef export_constant + + if (script.do_string(info) != script::success) + { + std::string str; + script[-1].get(str); + log_warning(str); + } + else + { + log_info("loading texture information..."); + + script::slot globals = script.globals(); + globals.get(min_filter, "min_filter"); + globals.get(mag_filter, "mag_filter"); + globals.get(tile_s, "tile_s"); + globals.get(tile_t, "tile_t"); + globals.get(wrap_s, "wrap_s"); + globals.get(wrap_t, "wrap_t"); + } + } + + GLuint min_filter; + GLuint mag_filter; + int tile_s; + int tile_t; + GLuint wrap_s; + GLuint wrap_t; +}; + + +static SDL_Surface* load_png(const std::string& path, texture_attributes& attribs) { std::ifstream file(path.c_str(), std::ifstream::binary); if (!file.good())