# HG changeset patch # User John Tsiombikas # Date 1453494814 -7200 # Node ID 57050ca14de609bb667bfbb13c1776b2fe515bc9 # Parent e831d38e6faa2e47b5ab87eb4207aa664d69428a forgot to add texture.h/cc diff -r e831d38e6faa -r 57050ca14de6 src/texture.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/texture.cc Fri Jan 22 22:33:34 2016 +0200 @@ -0,0 +1,77 @@ +#include +#include +#include "texture.h" +#include "logger.h" + +Texture::Texture() +{ + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + width = height = 0; +} + +Texture::~Texture() +{ + if(tex) { + glDeleteTextures(1, &tex); + } +} + +void Texture::set_image(int x, int y, unsigned char *pix) +{ + glBindTexture(GL_TEXTURE_2D, tex); + + if(x == width && y == height) { + if(pix) { + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, x, y, GL_BGRA, GL_UNSIGNED_BYTE, pix); + } + + } else { + width = x; + height = y; + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_BGRA, GL_UNSIGNED_BYTE, + pix ? pix : 0); + } +} + +void Texture::set_image(Display *dpy, Pixmap pixmap) +{ + Window root_ret; + int x, y; + unsigned int w, h, border, depth; + XGetGeometry(dpy, pixmap, &root_ret, &x, &y, &w, &h, &border, &depth); + + XImage *ximg = XGetImage(dpy, pixmap, 0, 0, w, h, 0xffffffff, ZPixmap); + if(!ximg) { + log_error("XGetImage failed to create an image from the window pixmap\n"); + return; + } + + glBindTexture(GL_TEXTURE_2D, tex); + + if((int)w == width && (int)h == height) { + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_BYTE, ximg->data); + } else { + width = w; + height = h; + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, ximg->data); + } +} + +int Texture::get_width() const +{ + return width; +} + +int Texture::get_height() const +{ + return height; +} + +unsigned int Texture::get_id() const +{ + return tex; +} diff -r e831d38e6faa -r 57050ca14de6 src/texture.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/texture.h Fri Jan 22 22:33:34 2016 +0200 @@ -0,0 +1,24 @@ +#ifndef TEXTURE_H_ +#define TEXTURE_H_ + +#include + +class Texture { +private: + unsigned int tex; + int width, height; + +public: + Texture(); + ~Texture(); + + void set_image(int x, int y, unsigned char *pix = 0); + void set_image(Display *dpy, Pixmap pixmap); + + int get_width() const; + int get_height() const; + + unsigned int get_id() const; +}; + +#endif // TEXTURE_H_