goat3dgfx

diff src/texture.cc @ 33:253542d715f4

default texture
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Mar 2014 06:32:37 +0200
parents dc5918c62a64
children
line diff
     1.1 --- a/src/texture.cc	Sun Mar 02 04:20:00 2014 +0200
     1.2 +++ b/src/texture.cc	Sun Mar 02 06:32:37 2014 +0200
     1.3 @@ -43,6 +43,9 @@
     1.4  	}
     1.5  }
     1.6  
     1.7 +
     1.8 +Image *Texture::default_img;
     1.9 +
    1.10  Texture::Texture()
    1.11  {
    1.12  	target = 0;
    1.13 @@ -178,6 +181,38 @@
    1.14  	texfmt = ifmt;
    1.15  }
    1.16  
    1.17 +#define DEF_IMAGE_SIZE	64
    1.18 +void Texture::create_default(TextureType type)
    1.19 +{
    1.20 +	if(!default_img) {
    1.21 +		default_img = new Image;
    1.22 +		default_img->create(DEF_IMAGE_SIZE, DEF_IMAGE_SIZE, Image::FMT_RGBA);
    1.23 +
    1.24 +		unsigned char *pixels = (unsigned char*)default_img->get_pixels();
    1.25 +		for(int i=0; i<DEF_IMAGE_SIZE; i++) {
    1.26 +			for(int j=0; j<DEF_IMAGE_SIZE; j++) {
    1.27 +				bool chess = ((i >> 3) & 1) == ((j >> 3) & 1);
    1.28 +				pixels[0] = chess ? 255 : 32;
    1.29 +				pixels[1] = 64;
    1.30 +				pixels[2] = chess ? 32 : 255;
    1.31 +				pixels[3] = 255;
    1.32 +			}
    1.33 +		}
    1.34 +	}
    1.35 +
    1.36 +	switch(type) {
    1.37 +	case TEX_2D:
    1.38 +		set_image(*default_img);
    1.39 +		break;
    1.40 +
    1.41 +	case TEX_CUBE:
    1.42 +		for(int i=0; i<6; i++) {
    1.43 +			set_image(*default_img, i);
    1.44 +		}
    1.45 +		break;
    1.46 +	}
    1.47 +}
    1.48 +
    1.49  void Texture::set_image(const Image &img, int idx)
    1.50  {
    1.51  	if(idx >= 0 && idx < 6) {
    1.52 @@ -422,6 +457,32 @@
    1.53  {
    1.54  }
    1.55  
    1.56 +Texture *TextureSet::get_texture(const char *name, TextureType type) const
    1.57 +{
    1.58 +	typename std::map<std::string, Texture*>::const_iterator iter = data.find(name);
    1.59 +	if(iter != data.end()) {
    1.60 +		return iter->second;
    1.61 +	}
    1.62 +
    1.63 +	const char *fname, *slash;
    1.64 +	if((slash = strrchr(name, '/'))) {
    1.65 +		fname = slash + 1;
    1.66 +	} else {
    1.67 +		fname = name;
    1.68 +	}
    1.69 +
    1.70 +	std::string path = datafile_path(fname);
    1.71 +	if(path.empty()) {
    1.72 +		fprintf(stderr, "can't find data file: %s\n", name);
    1.73 +		return 0;
    1.74 +	}
    1.75 +
    1.76 +	Texture *res = create();
    1.77 +	res->create_default(type);
    1.78 +	resman_lookup(rman, path.c_str(), res);
    1.79 +	return res;
    1.80 +}
    1.81 +
    1.82  // static callbacks
    1.83  
    1.84  Texture *TextureSet::create_tex()