# HG changeset patch # User John Tsiombikas # Date 1393774870 -7200 # Node ID 3eb6c8f89fe123cae89c95b5ed4323def4d7c4f9 # Parent 7f0aed0fe2896bb1f6481b1eec1b6d3b537211df# Parent 253542d715f4b30d319a1bc8cc18986f6b8e6ae6 merge diff -r 7f0aed0fe289 -r 3eb6c8f89fe1 src/texture.cc --- a/src/texture.cc Sun Mar 02 17:40:51 2014 +0200 +++ b/src/texture.cc Sun Mar 02 17:41:10 2014 +0200 @@ -43,6 +43,9 @@ } } + +Image *Texture::default_img; + Texture::Texture() { target = 0; @@ -178,6 +181,38 @@ texfmt = ifmt; } +#define DEF_IMAGE_SIZE 64 +void Texture::create_default(TextureType type) +{ + if(!default_img) { + default_img = new Image; + default_img->create(DEF_IMAGE_SIZE, DEF_IMAGE_SIZE, Image::FMT_RGBA); + + unsigned char *pixels = (unsigned char*)default_img->get_pixels(); + for(int i=0; i> 3) & 1) == ((j >> 3) & 1); + pixels[0] = chess ? 255 : 32; + pixels[1] = 64; + pixels[2] = chess ? 32 : 255; + pixels[3] = 255; + } + } + } + + switch(type) { + case TEX_2D: + set_image(*default_img); + break; + + case TEX_CUBE: + for(int i=0; i<6; i++) { + set_image(*default_img, i); + } + break; + } +} + void Texture::set_image(const Image &img, int idx) { if(idx >= 0 && idx < 6) { @@ -422,6 +457,32 @@ { } +Texture *TextureSet::get_texture(const char *name, TextureType type) const +{ + typename std::map::const_iterator iter = data.find(name); + if(iter != data.end()) { + return iter->second; + } + + const char *fname, *slash; + if((slash = strrchr(name, '/'))) { + fname = slash + 1; + } else { + fname = name; + } + + std::string path = datafile_path(fname); + if(path.empty()) { + fprintf(stderr, "can't find data file: %s\n", name); + return 0; + } + + Texture *res = create(); + res->create_default(type); + resman_lookup(rman, path.c_str(), res); + return res; +} + // static callbacks Texture *TextureSet::create_tex() diff -r 7f0aed0fe289 -r 3eb6c8f89fe1 src/texture.h --- a/src/texture.h Sun Mar 02 17:40:51 2014 +0200 +++ b/src/texture.h Sun Mar 02 17:41:10 2014 +0200 @@ -17,6 +17,7 @@ unsigned int texfmt; int sz[3]; Image *img; + static Image *default_img; Texture(const Texture &tex) {} Texture &operator =(const Texture &tex) { return *this; } @@ -44,6 +45,7 @@ int get_size(int dim) const; void create(int xsz, int ysz, TextureType type = TEX_2D, unsigned int ifmt = GL_RGBA); + void create_default(TextureType type = TEX_2D); void set_image(const Image &img, int idx = -1); bool load(const char *fname); @@ -67,6 +69,8 @@ public: TextureSet(); + + Texture *get_texture(const char *name, TextureType type = TEX_2D) const; }; } // namespace goatgfx