1 /* 2 Copyright (c) 2013 yvt 3 4 This file is part of OpenSpades. 5 6 OpenSpades is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 OpenSpades is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with OpenSpades. If not, see <http://www.gnu.org/licenses/>. 18 19 */ 20 21 #include "GLImage.h" 22 #include <Core/Bitmap.h> 23 #include <Core/Debug.h> 24 25 namespace spades { 26 namespace draw { GLImage(IGLDevice::UInteger texObj,IGLDevice * dev,float w,float h,bool autoDelete)27 GLImage::GLImage(IGLDevice::UInteger texObj, IGLDevice *dev, float w, float h, 28 bool autoDelete) 29 : tex(texObj), 30 device(dev), 31 width(w), 32 height(h), 33 autoDelete(autoDelete), 34 invWidth(1.f / w), 35 invHeight(1.f / h) { 36 SPADES_MARK_FUNCTION(); 37 valid = true; 38 } ~GLImage()39 GLImage::~GLImage() { 40 SPADES_MARK_FUNCTION(); 41 if (valid) { 42 Invalidate(); 43 } 44 } MakeSureValid()45 void GLImage::MakeSureValid() { 46 if (!valid) { 47 SPRaise("Attempted to use an invalid image."); 48 } 49 } Bind(IGLDevice::Enum target)50 void GLImage::Bind(IGLDevice::Enum target) { 51 SPADES_MARK_FUNCTION(); 52 MakeSureValid(); 53 device->BindTexture(target, tex); 54 } 55 FromBitmap(spades::Bitmap * bmp,spades::draw::IGLDevice * dev)56 GLImage *GLImage::FromBitmap(spades::Bitmap *bmp, spades::draw::IGLDevice *dev) { 57 SPADES_MARK_FUNCTION(); 58 59 IGLDevice::UInteger tex; 60 tex = dev->GenTexture(); 61 dev->BindTexture(IGLDevice::Texture2D, tex); 62 dev->TexImage2D(IGLDevice::Texture2D, 0, IGLDevice::RGBA, bmp->GetWidth(), 63 bmp->GetHeight(), 0, IGLDevice::RGBA, IGLDevice::UnsignedByte, 64 bmp->GetPixels()); 65 dev->TexParamater(IGLDevice::Texture2D, IGLDevice::TextureMagFilter, IGLDevice::Linear); 66 dev->TexParamater(IGLDevice::Texture2D, IGLDevice::TextureMinFilter, 67 IGLDevice::LinearMipmapNearest); 68 dev->TexParamater(IGLDevice::Texture2D, IGLDevice::TextureWrapS, IGLDevice::Repeat); 69 dev->TexParamater(IGLDevice::Texture2D, IGLDevice::TextureWrapT, IGLDevice::Repeat); 70 dev->GenerateMipmap(IGLDevice::Texture2D); 71 return new GLImage(tex, dev, bmp->GetWidth(), bmp->GetHeight()); 72 } 73 SubImage(spades::Bitmap * bmp,int x,int y)74 void GLImage::SubImage(spades::Bitmap *bmp, int x, int y) { 75 MakeSureValid(); 76 Bind(IGLDevice::Texture2D); 77 device->TexSubImage2D(IGLDevice::Texture2D, 0, x, y, bmp->GetWidth(), bmp->GetHeight(), 78 IGLDevice::RGBA, IGLDevice::UnsignedByte, bmp->GetPixels()); 79 } 80 Invalidate()81 void GLImage::Invalidate() { 82 SPADES_MARK_FUNCTION(); 83 MakeSureValid(); 84 valid = false; 85 86 if (autoDelete) 87 device->DeleteTexture(tex); 88 } 89 } 90 } 91