1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-25 17:53:47 +00:00
Aquaria/BBGE/QuadGrid.cpp

128 lines
2.7 KiB
C++
Raw Normal View History

#include "QuadGrid.h"
#include "Texture.h"
#include "RenderBase.h"
#include "Core.h"
QuadGrid::QuadGrid(size_t w, size_t h)
2022-05-14 03:14:08 +00:00
: pauseLevel(0), _w(w+1), _h(h+1)
{
addType(SCO_QUAD_GRID);
_points.resize((w+1) * (h+1));
2022-05-07 00:47:59 +00:00
resetUV();
2022-05-13 15:13:27 +00:00
resetPos(1, 1);
this->width = 2;
this->height = 2;
2022-05-14 03:14:08 +00:00
this->cull = false;
this->repeatTexture = true;
}
QuadGrid* QuadGrid::New(size_t w, size_t h)
{
return w && h ? new QuadGrid(w, h) : NULL;
}
QuadGrid::~QuadGrid()
{
}
void QuadGrid::resetUV(float xmul, float ymul)
{
2022-05-14 03:14:08 +00:00
const float incX = xmul / float(quadsX());
const float incY = ymul / float(quadsY());
const size_t NX = pointsX();
2022-05-14 03:14:08 +00:00
float v = 0;
2022-05-14 03:14:08 +00:00
// go over points
for(size_t y = 0; y < _h; ++y)
{
2022-05-14 03:14:08 +00:00
Point *row = &_points[y * NX];
float u = 0;
for(size_t x = 0; x < NX; ++x)
{
2022-05-14 03:14:08 +00:00
row[x].u = u;
row[x].v = v;
u += incX;
}
2022-05-14 03:14:08 +00:00
v += incY;
}
}
2022-05-13 15:13:27 +00:00
void QuadGrid::resetPos(float w, float h, float xoffs, float yoffs)
{
2022-05-14 03:14:08 +00:00
const float dx = w / float(quadsX());
const float dy = h / float(quadsY());
const size_t NX = pointsX();
2022-05-13 15:13:27 +00:00
float yy = yoffs;
2022-05-14 03:14:08 +00:00
// go over points
for(size_t y = 0; y < _h; ++y, yy += dy)
2022-05-13 15:13:27 +00:00
{
Point * const row = &_points[y * NX];
float xx = xoffs;
for(size_t x = 0; x < NX; ++x, xx += dx)
{
row[x].x = xx;
2022-05-14 03:14:08 +00:00
row[x].y = yy;
2022-05-13 15:13:27 +00:00
}
}
}
static inline void drawOnePoint(const QuadGrid::Point& p, float ox, float oy)
{
glTexCoord2f(p.u + ox, p.v + oy);
glVertex2f(p.x, p.y);
}
void QuadGrid::onRender(const RenderState& rs) const
{
glColor4f(color.x, color.y, color.z, alpha.x * alphaMod);
const float ox = texOffset.x;
const float oy = texOffset.y;
2022-05-14 03:14:08 +00:00
const size_t NX = pointsX();
2022-05-14 03:14:08 +00:00
// go over grids
const size_t W = quadsX();
const size_t H = quadsY();
for(size_t y = 0; y < H; ++y)
{
const Point * const row0 = &_points[y * NX];
const Point * const row1 = row0 + NX;
2022-05-14 03:14:08 +00:00
for(size_t x = 0; x < W; ++x)
{
glBegin(GL_QUADS);
drawOnePoint(row0[x], ox, oy);
drawOnePoint(row0[x+1], ox, oy);
drawOnePoint(row1[x+1], ox, oy);
drawOnePoint(row1[x], ox, oy);
glEnd();
}
}
}
void QuadGrid::onUpdate(float dt)
{
if(pauseLevel < core->particlesPaused)
return;
texOffset.update(dt);
RenderObject::onUpdate(dt);
}
2022-05-13 15:13:27 +00:00
void QuadGrid::onSetTexture() // same as Quad::setTexture()
{
if (texture)
{
width = this->texture->width;
height = this->texture->height;
}
else
{
width = 64;
height = 64;
}
}