tavli

diff src/image.cc @ 22:c2a2069a49ec

slot highlighting, TODO blur
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 07 Jul 2015 21:56:37 +0300
parents b41ceead1708
children 3e6430028d54
line diff
     1.1 --- a/src/image.cc	Thu Jul 02 00:01:39 2015 +0300
     1.2 +++ b/src/image.cc	Tue Jul 07 21:56:37 2015 +0300
     1.3 @@ -38,7 +38,7 @@
     1.4  	destroy();
     1.5  
     1.6  	try {
     1.7 -		unsigned char *tmp = new unsigned char[width * height * 3];
     1.8 +		unsigned char *tmp = new unsigned char[width * height * 4];
     1.9  		this->pixels = tmp;
    1.10  		this->width = width;
    1.11  		this->height = height;
    1.12 @@ -48,7 +48,7 @@
    1.13  	}
    1.14  
    1.15  	if(pixels) {
    1.16 -		memcpy(this->pixels, pixels, width * height * 3);
    1.17 +		memcpy(this->pixels, pixels, width * height * 4);
    1.18  	}
    1.19  	return true;
    1.20  }
    1.21 @@ -69,7 +69,7 @@
    1.22  bool Image::load(const char *fname)
    1.23  {
    1.24  	int xsz, ysz;
    1.25 -	unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGB24);
    1.26 +	unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32);
    1.27  	if(!pix) {
    1.28  		return false;
    1.29  	}
    1.30 @@ -96,13 +96,13 @@
    1.31  		if(GLEW_SGIS_generate_mipmap) {
    1.32  			glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1);
    1.33  
    1.34 -			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE,
    1.35 +			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
    1.36  					width == tex_width && height == tex_height ? pixels : 0);
    1.37  			if(width != tex_width || height != tex_height) {
    1.38 -				glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
    1.39 +				glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    1.40  			}
    1.41  		} else {
    1.42 -			gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, tex_width, tex_height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
    1.43 +			gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, tex_width, tex_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    1.44  		}
    1.45  
    1.46  		if(GLEW_EXT_texture_filter_anisotropic) {
    1.47 @@ -138,28 +138,45 @@
    1.48  
    1.49  void clear_image(Image *img)
    1.50  {
    1.51 -	clear_image(img, 0, 0, 0);
    1.52 +	clear_image(img, 0, 0, 0, 255);
    1.53  }
    1.54  
    1.55 -void clear_image(Image *img, float r, float g, float b)
    1.56 +void clear_image(Image *img, float r, float g, float b, float a)
    1.57  {
    1.58  	if(!img->pixels) {
    1.59  		return;
    1.60  	}
    1.61  
    1.62 -	unsigned char col[3];
    1.63 +	unsigned char col[4];
    1.64  	unsigned char *ptr = img->pixels;
    1.65  	int npix = img->width * img->height;
    1.66  
    1.67  	col[0] = (int)(r * 255.0);
    1.68  	col[1] = (int)(g * 255.0);
    1.69  	col[2] = (int)(b * 255.0);
    1.70 +	col[3] = (int)(a * 255.0);
    1.71  
    1.72  	for(int i=0; i<npix; i++) {
    1.73 -		for(int j=0; j<3; j++) {
    1.74 +		for(int j=0; j<4; j++) {
    1.75  			ptr[j] = col[j];
    1.76  		}
    1.77 -		ptr += 3;
    1.78 +		ptr += 4;
    1.79 +	}
    1.80 +}
    1.81 +
    1.82 +void clear_image_alpha(Image *img, float a)
    1.83 +{
    1.84 +	if(!img->pixels) {
    1.85 +		return;
    1.86 +	}
    1.87 +
    1.88 +	unsigned char alpha = (int)(a * 255.0);
    1.89 +	unsigned char *ptr = img->pixels;
    1.90 +	int npix = img->width * img->height;
    1.91 +
    1.92 +	for(int i=0; i<npix; i++) {
    1.93 +		ptr[3] = alpha;
    1.94 +		ptr += 4;
    1.95  	}
    1.96  }
    1.97  
    1.98 @@ -168,7 +185,7 @@
    1.99  	int xsz = dest->width;
   1.100  	int ysz = dest->height;
   1.101  	int npixels = xsz * ysz;
   1.102 -	int nbytes = npixels * 3;
   1.103 +	int nbytes = npixels * 4;
   1.104  	int tint = (int)(t * 255);
   1.105  
   1.106  	if(aimg->width != xsz || bimg->width != xsz || aimg->height != ysz || bimg->height != ysz) {