rework on dat and spr loader

This commit is contained in:
Eduardo Bart
2011-08-15 16:15:49 -03:00
parent b21ccd16f9
commit 2e1a96c2df
43 changed files with 191 additions and 581 deletions

View File

@@ -12,8 +12,8 @@ AnimatedTexture::AnimatedTexture(int width, int height, int channels, int numFra
{
m_size.setSize(width, height);
m_framesTextureId = new uint[numFrames];
m_framesDelay = new int[numFrames];
m_framesTextureId.resize(numFrames);
m_framesDelay.resize(numFrames);
for(int i=0;i<numFrames;++i) {
uchar *framePixels = framesPixels + (i*height*width* channels);
@@ -29,9 +29,8 @@ AnimatedTexture::AnimatedTexture(int width, int height, int channels, int numFra
AnimatedTexture::~AnimatedTexture()
{
glDeleteTextures(m_numFrames, m_framesTextureId);
delete[] m_framesTextureId;
delete[] m_framesDelay;
assert(!g_graphics.isDrawing());
glDeleteTextures(m_numFrames, &m_framesTextureId[0]);
m_textureId = 0;
}

View File

@@ -7,14 +7,14 @@ class AnimatedTexture : public Texture
{
public:
AnimatedTexture(int width, int height, int channels, int numFrames, uchar *framesPixels, int *framesDelay);
~AnimatedTexture();
virtual ~AnimatedTexture();
void enableBilinearFilter();
void processAnimation();
private:
uint *m_framesTextureId;
int *m_framesDelay;
std::vector<uint> m_framesTextureId;
std::vector<int> m_framesDelay;
int m_numFrames;
int m_currentFrame;
int m_lastAnimCheckTicks;

View File

@@ -224,7 +224,7 @@ Size Font::calculateTextRectSize(const std::string& text)
void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
{
int numHorizontalGlyphs = m_texture->getSize().width() / glyphSize.width();
uchar *texturePixels = m_texture->getPixels();
auto texturePixels = m_texture->getPixels();
// small AI to auto calculate pixels widths
for(int glyph = m_firstGlyph; glyph< 256; ++glyph) {
@@ -256,6 +256,4 @@ void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
// store glyph size
m_glyphsSize[glyph].setSize(width, m_glyphHeight);
}
delete[] texturePixels;
}

View File

@@ -26,13 +26,16 @@ void Graphics::init()
logInfo("OpenGL ", glGetString(GL_VERSION));
m_drawing = false;
bindColor(Color::white);
m_opacity = 255;
m_emptyTexture = TexturePtr(new Texture);
bindColor(Color::white);
}
void Graphics::terminate()
{
g_fonts.releaseFonts();
m_emptyTexture.reset();
}
bool Graphics::isExtensionSupported(const char *extension)
@@ -110,7 +113,7 @@ void Graphics::drawTexturedRect(const Rect& screenCoords,
const TexturePtr& texture,
const Rect& textureCoords)
{
if(screenCoords.isEmpty())
if(screenCoords.isEmpty() || texture->getId() == 0)
return;
// rect correction for opengl
@@ -156,7 +159,7 @@ void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
const TexturePtr& texture,
const Rect& textureCoords)
{
if(screenCoords.isEmpty() || textureCoords.isEmpty())
if(screenCoords.isEmpty() || texture->getId() == 0 || textureCoords.isEmpty())
return;
if(!m_drawing) {

View File

@@ -50,14 +50,17 @@ public:
void startDrawing();
void stopDrawing();
bool isDrawing() const { return m_drawing; }
int getOpacity() const { return m_opacity; }
void setOpacity(int opacity) { m_opacity = opacity; }
TexturePtr getEmptyTexture() { return m_emptyTexture; }
private:
bool m_drawing;
int m_opacity;
Size m_screenSize;
TexturePtr m_emptyTexture;
};
extern Graphics g_graphics;

View File

@@ -3,14 +3,29 @@
#include <GL/gl.h>
Texture::Texture()
{
m_textureId = 0;
}
Texture::Texture(int width, int height, int channels, uchar *pixels)
{
// generate opengl texture
m_textureId = internalLoadGLTexture(pixels, channels, width, height);
}
Texture::~Texture()
{
assert(!g_graphics.isDrawing());
// free texture from gl memory
if(m_textureId > 0)
glDeleteTextures(1, &m_textureId);
}
uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int height)
{
assert(!g_graphics.isDrawing());
m_size.setSize(width, height);
// gets max texture size supported by the driver
@@ -30,7 +45,8 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
GLuint id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
bool mustFree = false;
std::vector<uchar> tmp;
// old opengl drivers only accept power of two dimensions
if(!g_graphics.isExtensionSupported("GL_ARB_texture_non_power_of_two") && pixels) {
@@ -43,15 +59,13 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
glHeight = glHeight << 1;
if(m_size != m_glSize) {
uchar *tmp = new uchar[glHeight*glWidth*channels];
memset(tmp, 0, glHeight*glWidth*channels);
tmp.resize(glHeight*glWidth*channels, 0);
if(pixels)
for(int y=0; y<height; ++y)
for(int x=0; x<width; ++x)
for(int i=0; i<channels; ++i)
tmp[y*glWidth*channels+x*channels+i] = pixels[y*width*channels+x*channels+i];
pixels = tmp;
mustFree = true;
pixels = &tmp[0];
}
m_glSize.setSize(glWidth, glHeight);
@@ -86,19 +100,9 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
if(mustFree)
delete[] pixels;
return id;
}
Texture::~Texture()
{
// free texture from gl memory
if(m_textureId)
glDeleteTextures(1, &m_textureId);
}
void Texture::enableBilinearFilter()
{
// enable smooth texture
@@ -107,12 +111,12 @@ void Texture::enableBilinearFilter()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
uchar* Texture::getPixels()
std::vector<uint8> Texture::getPixels()
{
// copy pixels from opengl memory
uchar* pixels = new uchar[m_glSize.area()*4];
std::vector<uint8> pixels(m_glSize.area()*4, 0);
glBindTexture(GL_TEXTURE_2D, m_textureId);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[0]);
// convert pixels to the real texture size
if(m_size != m_glSize)

View File

@@ -7,6 +7,7 @@ class Texture : public std::enable_shared_from_this<Texture>
{
public:
/// Create a texture, width and height must be a multiple of 2
Texture();
Texture(int width, int height, int channels, uchar* pixels = NULL);
virtual ~Texture();
@@ -17,15 +18,16 @@ public:
virtual uint getId() const { return m_textureId; }
/// Copy pixels from OpenGL texture
uchar* getPixels();
std::vector<uint8> getPixels();
int getWidth() const { return m_size.width(); }
int getHeight() const { return m_size.height(); }
const Size getSize() const { return m_size; }
const Size& getGlSize() const { return m_glSize; }
bool isEmpty() const { return m_textureId == 0; }
protected:
Texture() { }
uint internalLoadGLTexture(uchar* pixels, int channels, int w, int h);
uint m_textureId;