conworlds

annotate src/texture.cc @ 17:c814f77d177e

moved to SDL2
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 25 Aug 2014 22:02:08 +0300
parents bd8202d6d28d
children
rev   line source
nuclear@2 1 #include "texture.h"
nuclear@2 2 #include "opengl.h"
nuclear@2 3
nuclear@2 4 Texture::Texture()
nuclear@2 5 {
nuclear@2 6 tex = 0;
nuclear@2 7 type = GL_TEXTURE_2D;
nuclear@2 8 }
nuclear@2 9
nuclear@2 10 Texture::~Texture()
nuclear@2 11 {
nuclear@2 12 destroy();
nuclear@2 13 }
nuclear@2 14
nuclear@7 15 int Texture::get_width() const
nuclear@7 16 {
nuclear@7 17 return img.get_width();
nuclear@7 18 }
nuclear@7 19
nuclear@7 20 int Texture::get_height() const
nuclear@7 21 {
nuclear@7 22 return img.get_height();
nuclear@7 23 }
nuclear@7 24
nuclear@2 25 void Texture::create2d(int xsz, int ysz)
nuclear@2 26 {
nuclear@2 27 destroy();
nuclear@2 28
nuclear@2 29 type = GL_TEXTURE_2D;
nuclear@2 30 img.create(xsz, ysz);
nuclear@2 31
nuclear@2 32 if(!tex) {
nuclear@2 33 glGenTextures(1, &tex);
nuclear@2 34 }
nuclear@2 35 glBindTexture(type, tex);
nuclear@2 36 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@2 37 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@2 38 glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
nuclear@2 39 }
nuclear@2 40
nuclear@2 41 void Texture::destroy()
nuclear@2 42 {
nuclear@2 43 if(tex) {
nuclear@2 44 glDeleteTextures(1, &tex);
nuclear@2 45 }
nuclear@2 46 }
nuclear@2 47
nuclear@2 48 void Texture::set_image(const Image &img)
nuclear@2 49 {
nuclear@2 50 this->img = img;
nuclear@2 51 create2d(img.get_width(), img.get_height());
nuclear@2 52
nuclear@2 53 glTexSubImage2D(type, 0, 0, 0, img.get_width(), img.get_height(),
nuclear@2 54 GL_RGBA, GL_UNSIGNED_BYTE, img.get_pixels());
nuclear@2 55 }
nuclear@2 56
nuclear@2 57 Image &Texture::get_image()
nuclear@2 58 {
nuclear@2 59 return img;
nuclear@2 60 }
nuclear@2 61
nuclear@2 62 const Image &Texture::get_image() const
nuclear@2 63 {
nuclear@2 64 return img;
nuclear@2 65 }
nuclear@2 66
nuclear@13 67 unsigned int Texture::get_type() const
nuclear@13 68 {
nuclear@13 69 return type;
nuclear@13 70 }
nuclear@13 71
nuclear@2 72 unsigned int Texture::get_texture_id() const
nuclear@2 73 {
nuclear@2 74 return tex;
nuclear@2 75 }
nuclear@2 76
nuclear@2 77 bool Texture::load(const char *fname)
nuclear@2 78 {
nuclear@2 79 Image image;
nuclear@2 80 if(!image.load(fname)) {
nuclear@2 81 return false;
nuclear@2 82 }
nuclear@2 83 set_image(image);
nuclear@2 84 return true;
nuclear@2 85 }
nuclear@6 86
nuclear@13 87 void bind_texture(const Texture *tex, int tunit)
nuclear@13 88 {
nuclear@13 89 static unsigned int cur_tex_type[8] = {
nuclear@13 90 GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D,
nuclear@13 91 GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D
nuclear@13 92 };
nuclear@13 93
nuclear@13 94 glActiveTextureARB(GL_TEXTURE0_ARB + tunit);
nuclear@13 95 if(tex) {
nuclear@13 96 glBindTexture(tex->get_type(), tex->get_texture_id());
nuclear@13 97 glEnable(tex->get_type());
nuclear@13 98 } else {
nuclear@13 99 glDisable(cur_tex_type[tunit]);
nuclear@13 100 }
nuclear@13 101 glActiveTextureARB(GL_TEXTURE0_ARB);
nuclear@13 102
nuclear@13 103 cur_tex_type[tunit] = tex->get_type();
nuclear@13 104 }
nuclear@13 105
nuclear@13 106
nuclear@6 107 unsigned int next_pow2(unsigned int x)
nuclear@6 108 {
nuclear@6 109 x -= 1;
nuclear@6 110 x |= x >> 1;
nuclear@6 111 x |= x >> 2;
nuclear@6 112 x |= x >> 4;
nuclear@6 113 x |= x >> 8;
nuclear@6 114 x |= x >> 16;
nuclear@6 115 return x + 1;
nuclear@6 116 }