2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** plane.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "plane.h"
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "bitmap.h"
|
|
|
|
#include "etc.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include "gl-util.h"
|
|
|
|
#include "quad.h"
|
2014-07-17 08:09:58 +00:00
|
|
|
#include "quadarray.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "transform.h"
|
|
|
|
#include "etc-internal.h"
|
|
|
|
#include "shader.h"
|
|
|
|
#include "glstate.h"
|
|
|
|
|
2013-10-17 13:28:55 +00:00
|
|
|
#include <sigc++/connection.h>
|
|
|
|
|
2014-07-17 08:09:58 +00:00
|
|
|
static float fwrap(float value, float range)
|
|
|
|
{
|
|
|
|
float res = fmod(value, range);
|
|
|
|
return res < 0 ? res + range : res;
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
struct PlanePrivate
|
|
|
|
{
|
|
|
|
Bitmap *bitmap;
|
2014-08-09 16:35:01 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
NormValue opacity;
|
|
|
|
BlendType blendType;
|
|
|
|
Color *color;
|
|
|
|
Tone *tone;
|
|
|
|
|
|
|
|
int ox, oy;
|
|
|
|
float zoomX, zoomY;
|
|
|
|
|
|
|
|
Scene::Geometry sceneGeo;
|
|
|
|
|
|
|
|
bool quadSourceDirty;
|
|
|
|
|
2014-07-17 08:09:58 +00:00
|
|
|
SimpleQuadArray qArray;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
EtcTemps tmp;
|
|
|
|
|
2013-10-17 13:28:55 +00:00
|
|
|
sigc::connection prepareCon;
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
PlanePrivate()
|
|
|
|
: bitmap(0),
|
|
|
|
opacity(255),
|
|
|
|
blendType(BlendNormal),
|
|
|
|
color(&tmp.color),
|
|
|
|
tone(&tmp.tone),
|
|
|
|
ox(0), oy(0),
|
|
|
|
zoomX(1), zoomY(1),
|
|
|
|
quadSourceDirty(false)
|
2013-10-17 13:28:55 +00:00
|
|
|
{
|
|
|
|
prepareCon = shState->prepareDraw.connect
|
|
|
|
(sigc::mem_fun(this, &PlanePrivate::prepare));
|
2014-07-17 08:09:58 +00:00
|
|
|
|
|
|
|
qArray.resize(1);
|
2013-10-17 13:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~PlanePrivate()
|
|
|
|
{
|
|
|
|
prepareCon.disconnect();
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
void updateQuadSource()
|
|
|
|
{
|
2014-07-17 08:09:58 +00:00
|
|
|
if (gl.npot_repeat)
|
|
|
|
{
|
|
|
|
FloatRect srcRect;
|
2015-02-10 15:42:32 +00:00
|
|
|
srcRect.x = (sceneGeo.orig.x + ox) / zoomX;
|
|
|
|
srcRect.y = (sceneGeo.orig.y + oy) / zoomY;
|
2014-07-17 08:09:58 +00:00
|
|
|
srcRect.w = sceneGeo.rect.w / zoomX;
|
|
|
|
srcRect.h = sceneGeo.rect.h / zoomY;
|
|
|
|
|
|
|
|
Quad::setTexRect(&qArray.vertices[0], srcRect);
|
|
|
|
qArray.commit();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:12:58 +00:00
|
|
|
if (nullOrDisposed(bitmap))
|
2014-07-17 08:09:58 +00:00
|
|
|
return;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-17 08:09:58 +00:00
|
|
|
/* Scaled (zoomed) bitmap dimensions */
|
2015-07-14 16:20:31 +00:00
|
|
|
float sw = bitmap->width() * zoomX;
|
|
|
|
float sh = bitmap->height() * zoomY;
|
2014-07-17 08:09:58 +00:00
|
|
|
|
|
|
|
/* Plane offset wrapped by scaled bitmap dims */
|
|
|
|
float wox = fwrap(ox, sw);
|
|
|
|
float woy = fwrap(oy, sh);
|
|
|
|
|
|
|
|
/* Viewport dimensions */
|
|
|
|
int vpw = sceneGeo.rect.w;
|
|
|
|
int vph = sceneGeo.rect.h;
|
|
|
|
|
|
|
|
/* Amount the scaled bitmap is tiled (repeated) */
|
|
|
|
size_t tilesX = ceil((vpw - sw + wox) / sw) + 1;
|
|
|
|
size_t tilesY = ceil((vph - sh + woy) / sh) + 1;
|
|
|
|
|
|
|
|
FloatRect tex = bitmap->rect();
|
|
|
|
|
|
|
|
qArray.resize(tilesX * tilesY);
|
|
|
|
|
|
|
|
for (size_t y = 0; y < tilesY; ++y)
|
|
|
|
for (size_t x = 0; x < tilesX; ++x)
|
|
|
|
{
|
|
|
|
SVertex *vert = &qArray.vertices[(y*tilesX + x) * 4];
|
|
|
|
FloatRect pos(x*sw - wox, y*sh - woy, sw, sh);
|
|
|
|
|
|
|
|
Quad::setTexPosRect(vert, tex, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
qArray.commit();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
2013-10-17 13:28:55 +00:00
|
|
|
|
|
|
|
void prepare()
|
|
|
|
{
|
|
|
|
if (quadSourceDirty)
|
|
|
|
{
|
|
|
|
updateQuadSource();
|
|
|
|
quadSourceDirty = false;
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Plane::Plane(Viewport *viewport)
|
|
|
|
: ViewportElement(viewport)
|
|
|
|
{
|
|
|
|
p = new PlanePrivate();
|
|
|
|
|
|
|
|
onGeometryChange(scene->getGeometry());
|
|
|
|
}
|
|
|
|
|
Implement a new tileset atlas layout to allow for bigger tilesets
The atlas packing algorithm has been reworked to pack autotiles
and tileset very efficiently into a texture, splitting the tileset
in multiple ways and eliminating the previous duplication of image
data in the atlas across "frames". Animation, which these frames
were designed for, is now done via duplicated buffer frames,
ie. each animation frame has its own VBO and IBO data. This was
not done to save on VRAM (hardly less memory is used), but to
make place for the new atlas layout.
Thanks to this new layout, even with a max texture size of 2048,
one can use tilesets with up to 15000 height. Of course, such
a tileset couldn't be stored in a regular Bitmap to begin with,
which is why I also introduced a hack called "mega surfaces":
software surfaces stored in RAM and wrapped inside a Bitmap,
whose sole purpose is to be passed to a Tilemap as tilesets.
Various other minor changes and fixes are included.
2013-09-23 20:21:58 +00:00
|
|
|
DEF_ATTR_RD_SIMPLE(Plane, Bitmap, Bitmap*, p->bitmap)
|
|
|
|
DEF_ATTR_RD_SIMPLE(Plane, OX, int, p->ox)
|
|
|
|
DEF_ATTR_RD_SIMPLE(Plane, OY, int, p->oy)
|
|
|
|
DEF_ATTR_RD_SIMPLE(Plane, ZoomX, float, p->zoomX)
|
|
|
|
DEF_ATTR_RD_SIMPLE(Plane, ZoomY, float, p->zoomY)
|
|
|
|
DEF_ATTR_RD_SIMPLE(Plane, BlendType, int, p->blendType)
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-10-25 21:33:41 +00:00
|
|
|
DEF_ATTR_SIMPLE(Plane, Opacity, int, p->opacity)
|
|
|
|
DEF_ATTR_SIMPLE(Plane, Color, Color&, *p->color)
|
|
|
|
DEF_ATTR_SIMPLE(Plane, Tone, Tone&, *p->tone)
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
Plane::~Plane()
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
dispose();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
Implement a new tileset atlas layout to allow for bigger tilesets
The atlas packing algorithm has been reworked to pack autotiles
and tileset very efficiently into a texture, splitting the tileset
in multiple ways and eliminating the previous duplication of image
data in the atlas across "frames". Animation, which these frames
were designed for, is now done via duplicated buffer frames,
ie. each animation frame has its own VBO and IBO data. This was
not done to save on VRAM (hardly less memory is used), but to
make place for the new atlas layout.
Thanks to this new layout, even with a max texture size of 2048,
one can use tilesets with up to 15000 height. Of course, such
a tileset couldn't be stored in a regular Bitmap to begin with,
which is why I also introduced a hack called "mega surfaces":
software surfaces stored in RAM and wrapped inside a Bitmap,
whose sole purpose is to be passed to a Tilemap as tilesets.
Various other minor changes and fixes are included.
2013-09-23 20:21:58 +00:00
|
|
|
void Plane::setBitmap(Bitmap *value)
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
guardDisposed();
|
|
|
|
|
Implement a new tileset atlas layout to allow for bigger tilesets
The atlas packing algorithm has been reworked to pack autotiles
and tileset very efficiently into a texture, splitting the tileset
in multiple ways and eliminating the previous duplication of image
data in the atlas across "frames". Animation, which these frames
were designed for, is now done via duplicated buffer frames,
ie. each animation frame has its own VBO and IBO data. This was
not done to save on VRAM (hardly less memory is used), but to
make place for the new atlas layout.
Thanks to this new layout, even with a max texture size of 2048,
one can use tilesets with up to 15000 height. Of course, such
a tileset couldn't be stored in a regular Bitmap to begin with,
which is why I also introduced a hack called "mega surfaces":
software surfaces stored in RAM and wrapped inside a Bitmap,
whose sole purpose is to be passed to a Tilemap as tilesets.
Various other minor changes and fixes are included.
2013-09-23 20:21:58 +00:00
|
|
|
p->bitmap = value;
|
2013-09-28 19:48:02 +00:00
|
|
|
|
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
value->ensureNonMega();
|
Implement a new tileset atlas layout to allow for bigger tilesets
The atlas packing algorithm has been reworked to pack autotiles
and tileset very efficiently into a texture, splitting the tileset
in multiple ways and eliminating the previous duplication of image
data in the atlas across "frames". Animation, which these frames
were designed for, is now done via duplicated buffer frames,
ie. each animation frame has its own VBO and IBO data. This was
not done to save on VRAM (hardly less memory is used), but to
make place for the new atlas layout.
Thanks to this new layout, even with a max texture size of 2048,
one can use tilesets with up to 15000 height. Of course, such
a tileset couldn't be stored in a regular Bitmap to begin with,
which is why I also introduced a hack called "mega surfaces":
software surfaces stored in RAM and wrapped inside a Bitmap,
whose sole purpose is to be passed to a Tilemap as tilesets.
Various other minor changes and fixes are included.
2013-09-23 20:21:58 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
void Plane::setOX(int value)
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
guardDisposed();
|
|
|
|
|
2014-07-16 11:43:13 +00:00
|
|
|
if (p->ox == value)
|
|
|
|
return;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->ox = value;
|
|
|
|
p->quadSourceDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Plane::setOY(int value)
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
guardDisposed();
|
|
|
|
|
2014-07-16 11:43:13 +00:00
|
|
|
if (p->oy == value)
|
|
|
|
return;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->oy = value;
|
|
|
|
p->quadSourceDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Plane::setZoomX(float value)
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
guardDisposed();
|
|
|
|
|
2014-07-16 11:43:13 +00:00
|
|
|
if (p->zoomX == value)
|
|
|
|
return;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->zoomX = value;
|
|
|
|
p->quadSourceDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Plane::setZoomY(float value)
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
guardDisposed();
|
|
|
|
|
2014-07-16 11:43:13 +00:00
|
|
|
if (p->zoomY == value)
|
|
|
|
return;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->zoomY = value;
|
|
|
|
p->quadSourceDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Plane::setBlendType(int value)
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
guardDisposed();
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
switch (value)
|
|
|
|
{
|
|
|
|
default :
|
|
|
|
case BlendNormal :
|
|
|
|
p->blendType = BlendNormal;
|
|
|
|
return;
|
|
|
|
case BlendAddition :
|
|
|
|
p->blendType = BlendAddition;
|
|
|
|
return;
|
|
|
|
case BlendSubstraction :
|
|
|
|
p->blendType = BlendSubstraction;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 23:26:03 +00:00
|
|
|
void Plane::initDynAttribs()
|
|
|
|
{
|
|
|
|
p->color = new Color;
|
|
|
|
p->tone = new Tone;
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
void Plane::draw()
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
if (nullOrDisposed(p->bitmap))
|
2013-09-01 14:27:21 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!p->opacity)
|
|
|
|
return;
|
|
|
|
|
2013-09-23 05:15:01 +00:00
|
|
|
ShaderBase *base;
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
if (p->color->hasEffect() || p->tone->hasEffect() || p->opacity != 255)
|
|
|
|
{
|
2013-12-11 04:22:13 +00:00
|
|
|
PlaneShader &shader = shState->shaders().plane;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
shader.bind();
|
2013-09-23 05:15:01 +00:00
|
|
|
shader.applyViewportProj();
|
2013-09-01 14:27:21 +00:00
|
|
|
shader.setTone(p->tone->norm);
|
|
|
|
shader.setColor(p->color->norm);
|
|
|
|
shader.setFlash(Vec4());
|
|
|
|
shader.setOpacity(p->opacity.norm);
|
2013-09-23 05:15:01 +00:00
|
|
|
|
|
|
|
base = &shader;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-11 04:22:13 +00:00
|
|
|
SimpleShader &shader = shState->shaders().simple;
|
2013-09-23 05:15:01 +00:00
|
|
|
|
|
|
|
shader.bind();
|
|
|
|
shader.applyViewportProj();
|
|
|
|
shader.setTranslation(Vec2i());
|
|
|
|
|
|
|
|
base = &shader;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glState.blendMode.pushSet(p->blendType);
|
|
|
|
|
2013-09-23 05:15:01 +00:00
|
|
|
p->bitmap->bindTex(*base);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-17 08:09:58 +00:00
|
|
|
if (gl.npot_repeat)
|
|
|
|
TEX::setRepeat(true);
|
|
|
|
|
|
|
|
p->qArray.draw();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-17 08:09:58 +00:00
|
|
|
if (gl.npot_repeat)
|
|
|
|
TEX::setRepeat(false);
|
Implement a new tileset atlas layout to allow for bigger tilesets
The atlas packing algorithm has been reworked to pack autotiles
and tileset very efficiently into a texture, splitting the tileset
in multiple ways and eliminating the previous duplication of image
data in the atlas across "frames". Animation, which these frames
were designed for, is now done via duplicated buffer frames,
ie. each animation frame has its own VBO and IBO data. This was
not done to save on VRAM (hardly less memory is used), but to
make place for the new atlas layout.
Thanks to this new layout, even with a max texture size of 2048,
one can use tilesets with up to 15000 height. Of course, such
a tileset couldn't be stored in a regular Bitmap to begin with,
which is why I also introduced a hack called "mega surfaces":
software surfaces stored in RAM and wrapped inside a Bitmap,
whose sole purpose is to be passed to a Tilemap as tilesets.
Various other minor changes and fixes are included.
2013-09-23 20:21:58 +00:00
|
|
|
|
|
|
|
glState.blendMode.pop();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Plane::onGeometryChange(const Scene::Geometry &geo)
|
|
|
|
{
|
2014-07-17 08:09:58 +00:00
|
|
|
if (gl.npot_repeat)
|
|
|
|
Quad::setPosRect(&p->qArray.vertices[0], FloatRect(geo.rect));
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->sceneGeo = geo;
|
|
|
|
p->quadSourceDirty = true;
|
|
|
|
}
|
2014-09-23 19:12:58 +00:00
|
|
|
|
|
|
|
void Plane::releaseResources()
|
|
|
|
{
|
|
|
|
unlink();
|
|
|
|
|
|
|
|
delete p;
|
|
|
|
}
|