2023-07-10 00:29:21 +00:00
|
|
|
#include "TileRender.h"
|
|
|
|
#include "RenderBase.h"
|
|
|
|
#include "Core.h"
|
|
|
|
#include "Tileset.h"
|
2023-07-11 20:30:28 +00:00
|
|
|
#include "RenderGrid.h"
|
|
|
|
#include "RenderObject.h"
|
|
|
|
|
2023-07-10 00:29:21 +00:00
|
|
|
|
|
|
|
TileRender::TileRender(const TileStorage& tiles)
|
2023-07-11 20:30:28 +00:00
|
|
|
: storage(tiles), renderBorders(false)
|
2023-07-10 00:29:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TileRender::~TileRender()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-07-11 20:30:28 +00:00
|
|
|
// shamelessly ripped from paint.net default palette
|
|
|
|
static const Vector s_tagColors[] =
|
|
|
|
{
|
|
|
|
/* 0 */ Vector(0.5f, 0.5f, 0.5f),
|
|
|
|
/* 1 */ Vector(1,0,0),
|
|
|
|
/* 2 */ Vector(1, 0.415686f, 0),
|
|
|
|
/* 3 */ Vector(1,0.847059f, 0),
|
|
|
|
/* 4 */ Vector(0.298039f,1,0),
|
|
|
|
/* 5 */ Vector(0,1,1),
|
|
|
|
/* 6 */ Vector(0,0.580392,1),
|
|
|
|
/* 7 */ Vector(0,0.149020f,1),
|
|
|
|
/* 8 */ Vector(0.282353f,0,1),
|
|
|
|
/* 9 */ Vector(0.698039f,0,1),
|
|
|
|
|
|
|
|
/* 10 */ Vector(1,0,1), // anything outside of the pretty range
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline const Vector& getTagColor(int tag)
|
|
|
|
{
|
|
|
|
const unsigned idx = std::min<unsigned>(unsigned(tag), Countof(s_tagColors)-1);
|
|
|
|
return s_tagColors[idx];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-10 00:29:21 +00:00
|
|
|
void TileRender::onRender(const RenderState& rs) const
|
|
|
|
{
|
|
|
|
// prepare. get parallax scroll factors
|
|
|
|
const RenderObjectLayer& rl = core->renderObjectLayers[this->layer];
|
2023-07-11 20:30:28 +00:00
|
|
|
const Vector M = rl.followCameraMult; // affected by parallaxLock
|
2023-07-10 00:29:21 +00:00
|
|
|
const float F = rl.followCamera;
|
|
|
|
|
|
|
|
// Formula from RenderObject::getFollowCameraPosition() and optimized for speed
|
|
|
|
const Vector C = core->screenCenter;
|
|
|
|
const Vector M1 = Vector(1,1) - M;
|
|
|
|
const Vector T = C * (1 - F);
|
|
|
|
|
|
|
|
unsigned lastTexRepeat = false;
|
|
|
|
unsigned lastTexId = 0;
|
2023-07-11 20:30:28 +00:00
|
|
|
|
|
|
|
const bool renderBorders = this->renderBorders;
|
|
|
|
//bool mustSetColor = false;
|
2023-07-10 00:29:21 +00:00
|
|
|
|
|
|
|
for(size_t i = 0; i < storage.tiles.size(); ++i)
|
|
|
|
{
|
|
|
|
const TileData& tile = storage.tiles[i];
|
2023-07-11 20:30:28 +00:00
|
|
|
if(tile.flags & TILEFLAG_HIDDEN)
|
|
|
|
continue;
|
|
|
|
|
2023-07-10 00:29:21 +00:00
|
|
|
const Vector tilepos(tile.x, tile.y);
|
|
|
|
const Vector tmp = T + (F * tilepos);
|
|
|
|
const Vector pos = tilepos * M1 + (tmp * M); // lerp, used to select whether to use original v or parallax-corrected v
|
|
|
|
|
|
|
|
ElementTemplate * const et = tile.et;
|
|
|
|
if(Texture * const tex = et->tex.content())
|
|
|
|
{
|
|
|
|
unsigned texid = tex->gltexid;
|
|
|
|
unsigned rep = tile.flags & TILEFLAG_REPEAT;
|
|
|
|
if(texid != lastTexId || rep != lastTexRepeat)
|
|
|
|
{
|
|
|
|
lastTexId = texid;
|
|
|
|
lastTexRepeat = rep;
|
|
|
|
tex->apply(!!rep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-07-11 20:30:28 +00:00
|
|
|
{
|
|
|
|
lastTexId = 0;
|
2023-07-10 00:29:21 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0); // unlikely
|
2023-07-11 20:30:28 +00:00
|
|
|
}
|
2023-07-10 00:29:21 +00:00
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(pos.x, pos.y, pos.z);
|
|
|
|
|
|
|
|
glRotatef(tile.rotation, 0, 0, 1);
|
2023-07-11 20:30:28 +00:00
|
|
|
if(tile.flags & TILEFLAG_FH) // TODO: This is not necessary! Since we have no children, flipped texcoords are fine
|
2023-07-10 00:29:21 +00:00
|
|
|
glRotatef(180, 0, 1, 0);
|
|
|
|
|
|
|
|
// this is only relevant in editor mode and is always 0 otherwise
|
2023-07-11 20:30:28 +00:00
|
|
|
glTranslatef(tile.beforeScaleOffsetX, tile.beforeScaleOffsetY, 0);
|
2023-07-10 00:29:21 +00:00
|
|
|
|
2023-07-11 20:30:28 +00:00
|
|
|
glScalef(tile.scalex * et->w, tile.scaley * et->h, 1);
|
|
|
|
//glScalef(tile.scalex * et->w, tile.scaley * et->h, 1); // TODO use this + fixed verts
|
2023-07-10 00:29:21 +00:00
|
|
|
|
2023-07-11 20:30:28 +00:00
|
|
|
BlendType blend = BLEND_DEFAULT;
|
|
|
|
float alpha = rs.alpha;
|
|
|
|
RenderGrid *grid = NULL;
|
|
|
|
const TileEffectData * const eff = tile.eff;
|
|
|
|
if(eff)
|
2023-07-10 00:29:21 +00:00
|
|
|
{
|
2023-07-11 20:30:28 +00:00
|
|
|
grid = eff->grid;
|
|
|
|
alpha *= eff->alpha.x;
|
|
|
|
blend = eff->blend;
|
2023-07-10 00:29:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 20:30:28 +00:00
|
|
|
rs.gpu.setBlend(blend);
|
|
|
|
|
|
|
|
// TODO: only need to do this when prev. tile had different alpha
|
|
|
|
glColor4f(rs.color.x, rs.color.y, rs.color.z, alpha);
|
2023-07-10 00:29:21 +00:00
|
|
|
|
2023-07-11 20:30:28 +00:00
|
|
|
const Vector upperLeftTextureCoordinates(et->tu1, et->tv1);
|
|
|
|
const Vector lowerRightTextureCoordinates(et->tu2, et->tv2);
|
|
|
|
|
|
|
|
if(!grid)
|
2023-07-10 00:29:21 +00:00
|
|
|
{
|
2023-07-11 20:30:28 +00:00
|
|
|
const float _w2 = et->w * 0.5f;
|
|
|
|
const float _h2 = et->h * 0.5f;
|
2023-07-10 00:29:21 +00:00
|
|
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
{
|
|
|
|
glTexCoord2f(upperLeftTextureCoordinates.x, 1.0f-upperLeftTextureCoordinates.y);
|
|
|
|
glVertex2f(-_w2, +_h2);
|
|
|
|
|
|
|
|
glTexCoord2f(lowerRightTextureCoordinates.x, 1.0f-upperLeftTextureCoordinates.y);
|
|
|
|
glVertex2f(+_w2, +_h2);
|
|
|
|
|
|
|
|
glTexCoord2f(lowerRightTextureCoordinates.x, 1.0f-lowerRightTextureCoordinates.y);
|
|
|
|
glVertex2f(+_w2, -_h2);
|
|
|
|
|
|
|
|
glTexCoord2f(upperLeftTextureCoordinates.x, 1.0f-lowerRightTextureCoordinates.y);
|
|
|
|
glVertex2f(-_w2, -_h2);
|
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
}
|
2023-07-11 20:30:28 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
RenderState rx(rs);
|
|
|
|
rx.alpha = alpha;
|
|
|
|
grid->render(rx, upperLeftTextureCoordinates, lowerRightTextureCoordinates);
|
|
|
|
if (RenderObject::renderCollisionShape)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
grid->renderDebugPoints(rs);
|
|
|
|
lastTexId = 0;
|
|
|
|
}
|
|
|
|
}
|
2023-07-10 00:29:21 +00:00
|
|
|
|
|
|
|
if(renderBorders)
|
|
|
|
{
|
|
|
|
lastTexId = 0;
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
2023-07-11 20:30:28 +00:00
|
|
|
float c = (tile.flags & TILEFLAG_SELECTED) ? 1.0f : 0.5f;
|
|
|
|
Vector color(c,c,c);
|
|
|
|
color *= getTagColor(tile.tag);
|
|
|
|
const float _w2 = et->w * 0.5f;
|
|
|
|
const float _h2 = et->h * 0.5f;
|
2023-07-10 00:29:21 +00:00
|
|
|
|
|
|
|
glColor4f(color.x, color.y, color.z, 1.0f);
|
|
|
|
glPointSize(16);
|
|
|
|
glBegin(GL_POINTS);
|
|
|
|
glVertex2f(0,0);
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glLineWidth(2);
|
|
|
|
glBegin(GL_LINE_STRIP);
|
|
|
|
glVertex2f(_w2, _h2);
|
|
|
|
glVertex2f(_w2, -_h2);
|
|
|
|
glVertex2f(-_w2, -_h2);
|
|
|
|
glVertex2f(-_w2, _h2);
|
|
|
|
glVertex2f(_w2, _h2);
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RenderObject::lastTextureApplied = lastTexId;
|
|
|
|
RenderObject::lastTextureRepeat = !!lastTexRepeat;
|
|
|
|
}
|