xglcomp

changeset 4:57050ca14de6

forgot to add texture.h/cc
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Jan 2016 22:33:34 +0200
parents e831d38e6faa
children 86e57e56a454
files src/texture.cc src/texture.h
diffstat 2 files changed, 101 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/texture.cc	Fri Jan 22 22:33:34 2016 +0200
     1.3 @@ -0,0 +1,77 @@
     1.4 +#include <GL/gl.h>
     1.5 +#include <X11/Xlib.h>
     1.6 +#include "texture.h"
     1.7 +#include "logger.h"
     1.8 +
     1.9 +Texture::Texture()
    1.10 +{
    1.11 +	glGenTextures(1, &tex);
    1.12 +	glBindTexture(GL_TEXTURE_2D, tex);
    1.13 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    1.14 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    1.15 +
    1.16 +	width = height = 0;
    1.17 +}
    1.18 +
    1.19 +Texture::~Texture()
    1.20 +{
    1.21 +	if(tex) {
    1.22 +		glDeleteTextures(1, &tex);
    1.23 +	}
    1.24 +}
    1.25 +
    1.26 +void Texture::set_image(int x, int y, unsigned char *pix)
    1.27 +{
    1.28 +	glBindTexture(GL_TEXTURE_2D, tex);
    1.29 +
    1.30 +	if(x == width && y == height) {
    1.31 +		if(pix) {
    1.32 +			glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, x, y, GL_BGRA, GL_UNSIGNED_BYTE, pix);
    1.33 +		}
    1.34 +
    1.35 +	} else {
    1.36 +		width = x;
    1.37 +		height = y;
    1.38 +		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_BGRA, GL_UNSIGNED_BYTE,
    1.39 +				pix ? pix : 0);
    1.40 +	}
    1.41 +}
    1.42 +
    1.43 +void Texture::set_image(Display *dpy, Pixmap pixmap)
    1.44 +{
    1.45 +	Window root_ret;
    1.46 +	int x, y;
    1.47 +	unsigned int w, h, border, depth;
    1.48 +	XGetGeometry(dpy, pixmap, &root_ret, &x, &y, &w, &h, &border, &depth);
    1.49 +
    1.50 +	XImage *ximg = XGetImage(dpy, pixmap, 0, 0, w, h, 0xffffffff, ZPixmap);
    1.51 +	if(!ximg) {
    1.52 +		log_error("XGetImage failed to create an image from the window pixmap\n");
    1.53 +		return;
    1.54 +	}
    1.55 +
    1.56 +	glBindTexture(GL_TEXTURE_2D, tex);
    1.57 +
    1.58 +	if((int)w == width && (int)h == height) {
    1.59 +		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_BYTE, ximg->data);
    1.60 +	} else {
    1.61 +		width = w;
    1.62 +		height = h;
    1.63 +		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, ximg->data);
    1.64 +	}
    1.65 +}
    1.66 +
    1.67 +int Texture::get_width() const
    1.68 +{
    1.69 +	return width;
    1.70 +}
    1.71 +
    1.72 +int Texture::get_height() const
    1.73 +{
    1.74 +	return height;
    1.75 +}
    1.76 +
    1.77 +unsigned int Texture::get_id() const
    1.78 +{
    1.79 +	return tex;
    1.80 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/texture.h	Fri Jan 22 22:33:34 2016 +0200
     2.3 @@ -0,0 +1,24 @@
     2.4 +#ifndef TEXTURE_H_
     2.5 +#define TEXTURE_H_
     2.6 +
     2.7 +#include <X11/Xlib.h>
     2.8 +
     2.9 +class Texture {
    2.10 +private:
    2.11 +	unsigned int tex;
    2.12 +	int width, height;
    2.13 +
    2.14 +public:
    2.15 +	Texture();
    2.16 +	~Texture();
    2.17 +
    2.18 +	void set_image(int x, int y, unsigned char *pix = 0);
    2.19 +	void set_image(Display *dpy, Pixmap pixmap);
    2.20 +
    2.21 +	int get_width() const;
    2.22 +	int get_height() const;
    2.23 +
    2.24 +	unsigned int get_id() const;
    2.25 +};
    2.26 +
    2.27 +#endif	// TEXTURE_H_