#include "PreCompile.h"
#include "AtlasImage.h"
#include "ImageGL.h"
#include "ResManager.h"
#include "XParser.h"
AtlasImage::AtlasImage(const AtlasImage& atlasImage)
{
m_pName = atlasImage.m_pName;
m_x = atlasImage.m_x;
m_y = atlasImage.m_y;
m_width = atlasImage.m_width;
m_height = atlasImage.m_height;
m_transWidth = atlasImage.m_transWidth;
m_transHeight = atlasImage.m_transHeight;
m_xOffset = atlasImage.m_xOffset;
m_yOffset = atlasImage.m_yOffset;
m_transparent = atlasImage.m_transparent;
for (int n = 0; n < verticesPerQuad*compVertTex; n++)
{
m_texCoords[n] = atlasImage.m_texCoords[n];
}
m_pImageGL = atlasImage.m_pImageGL;
}
void AtlasImage::Load(const XParser* pXParser)
{
m_x = pXParser->GetValueInt("x");
m_y = pXParser->GetValueInt("y");
m_width = pXParser->GetValueInt("width");
m_height = pXParser->GetValueInt("height");
m_xOffset = pXParser->GetOptionalInt("xOffset", 0);
m_yOffset = pXParser->GetOptionalInt("yOffset", 0);
m_transWidth = pXParser->GetOptionalInt("transWidth", m_width);
m_transHeight = pXParser->GetOptionalInt("transHeight", m_height);
m_transparent = true;
string value;
if (pXParser->GetOptionalValue("trans", value))
{
if (value == "false")
m_transparent = false;
}
::CreateQuad(m_texCoords, m_x, m_y, m_width, m_height,
m_pImageGL->GetWidth(), m_pImageGL->GetHeight());
}
static const GLubyte m_indices[indicesPerQuad] = {0, 1, 2, 1, 3, 2};
// plain vanilla
void AtlasImage::Render(int x, int y) const
{
float x1, x2, y1, y2;
x1 = (float)x + m_xOffset;
y1 = (float)y + m_yOffset;
x2 = x1 + m_width;
y2 = y1 + m_height;
if (x2 < 0 || y2 < 0)
return;
if (x1 > g_resManager.GetScreenWidth() || y1 > g_resManager.GetScreenHeight())
return;
float vertices[verticesPerQuad*compVertPos];
vertices[0] = x1;
vertices[1] = y1;
vertices[2] = x2;
vertices[3] = y1;
vertices[4] = x1;
vertices[5] = y2;
vertices[6] = x2;
vertices[7] = y2;
glEnable(GL_TEXTURE_2D);
glClientActiveTexture(GL_TEXTURE0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
if (m_transparent)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
else
{
glDisable(GL_BLEND);
}
glBindTexture(GL_TEXTURE_2D, m_pImageGL->GetTextureID());
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glVertexPointer(compVertPos, GL_FLOAT, 0, vertices);
glTexCoordPointer(compVertTex, GL_FLOAT, 0, m_texCoords);
glDrawElements(GL_TRIANGLES, (GLsizei)indicesPerQuad, GL_UNSIGNED_BYTE, m_indices);
} // Render
void CreateQuad(float* pTexCoords,
int x, int y, int width, int height,
int texWidth, int texHeight)
{
// note: the 0.5 is to compensate for problem of sampling pixels
// outside of the texture co-ordinates (if you don't include it the
// quad will have pixels from the texture below when rotated)
//float x1 = ((float)x)/(float)texWidth;
//float y1 = ((float)y)/(float)texHeight;
//float x2 = ((float)(x+width))/(float)texWidth;
//float y2 = ((float)(y+height))/(float)texHeight;
float x1 = ((float)x+0.5f)/(float)texWidth;
float y1 = ((float)y+0.5f)/(float)texHeight;
float x2 = ((float)(x+width)-0.5f)/(float)texWidth;
float y2 = ((float)(y+height)-0.5f)/(float)texHeight;
pTexCoords[0] = x1;
pTexCoords[1] = y1;
pTexCoords[2] = x2;
pTexCoords[3] = y1;
pTexCoords[4] = x1;
pTexCoords[5] = y2;
pTexCoords[6] = x2;
pTexCoords[7] = y2;
}