2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** bitmap.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 "bitmap.h"
|
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
#include <SDL_image.h>
|
|
|
|
#include <SDL_ttf.h>
|
|
|
|
#include <SDL_rect.h>
|
2014-07-11 01:25:28 +00:00
|
|
|
#include <SDL_surface.h>
|
2013-09-03 13:23:20 +00:00
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <pixman.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
#include "gl-util.h"
|
2014-07-11 06:31:12 +00:00
|
|
|
#include "gl-meta.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "quad.h"
|
|
|
|
#include "quadarray.h"
|
2013-09-04 19:03:59 +00:00
|
|
|
#include "transform.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "exception.h"
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "glstate.h"
|
|
|
|
#include "texpool.h"
|
|
|
|
#include "shader.h"
|
|
|
|
#include "filesystem.h"
|
|
|
|
#include "font.h"
|
|
|
|
#include "eventthread.h"
|
|
|
|
|
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
|
|
|
#define GUARD_MEGA \
|
|
|
|
{ \
|
|
|
|
if (p->megaSurface) \
|
|
|
|
throw Exception(Exception::MKXPError, \
|
|
|
|
"Operation not supported for mega surfaces"); \
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
struct BitmapPrivate
|
|
|
|
{
|
2014-07-11 01:25:28 +00:00
|
|
|
Bitmap *self;
|
|
|
|
|
2013-10-06 06:54:16 +00:00
|
|
|
TEXFBO gl;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
Font *font;
|
|
|
|
|
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
|
|
|
/* "Mega surfaces" are a hack to allow Tilesets to be used
|
|
|
|
* whose Bitmaps don't fit into a regular texture. They're
|
|
|
|
* kept in RAM and will throw an error if they're used in
|
|
|
|
* any context other than as Tilesets */
|
|
|
|
SDL_Surface *megaSurface;
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
/* A cached version of the bitmap in client memory, for
|
|
|
|
* getPixel calls. Is invalidated any time the bitmap
|
|
|
|
* is modified */
|
|
|
|
SDL_Surface *surface;
|
|
|
|
SDL_PixelFormat *format;
|
|
|
|
|
2013-09-03 13:23:20 +00:00
|
|
|
/* The 'tainted' area describes which parts of the
|
|
|
|
* bitmap are not cleared, ie. don't have 0 opacity.
|
|
|
|
* If we're blitting / drawing text to a cleared part
|
|
|
|
* with full opacity, we can disregard any old contents
|
|
|
|
* in the texture and blit to it directly, saving
|
|
|
|
* ourselves the expensive blending calculation */
|
|
|
|
pixman_region16_t tainted;
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
BitmapPrivate(Bitmap *self)
|
|
|
|
: self(self),
|
|
|
|
megaSurface(0),
|
|
|
|
surface(0)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-07-11 01:25:28 +00:00
|
|
|
format = SDL_AllocFormat(SDL_PIXELFORMAT_ABGR8888);
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
font = &shState->defaultFont();
|
2013-09-03 13:23:20 +00:00
|
|
|
pixman_region_init(&tainted);
|
|
|
|
}
|
|
|
|
|
|
|
|
~BitmapPrivate()
|
|
|
|
{
|
|
|
|
pixman_region_fini(&tainted);
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
void allocSurface()
|
|
|
|
{
|
|
|
|
surface = SDL_CreateRGBSurface(0, gl.width, gl.height, format->BitsPerPixel,
|
|
|
|
format->Rmask, format->Gmask,
|
|
|
|
format->Bmask, format->Amask);
|
|
|
|
}
|
|
|
|
|
2013-09-03 13:23:20 +00:00
|
|
|
void clearTaintedArea()
|
|
|
|
{
|
|
|
|
pixman_region_clear(&tainted);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addTaintedArea(const IntRect &rect)
|
|
|
|
{
|
2014-08-09 06:04:56 +00:00
|
|
|
/* Normalize (= ensure width and
|
|
|
|
* height are positive) */
|
|
|
|
IntRect norm = rect;
|
|
|
|
|
|
|
|
if (norm.w < 0)
|
|
|
|
{
|
|
|
|
norm.x += norm.w;
|
|
|
|
norm.w = -norm.w;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (norm.h < 0)
|
|
|
|
{
|
|
|
|
norm.y += norm.h;
|
|
|
|
norm.h = -norm.h;
|
|
|
|
}
|
|
|
|
|
2013-09-03 13:23:20 +00:00
|
|
|
pixman_region_union_rect
|
2014-08-09 06:04:56 +00:00
|
|
|
(&tainted, &tainted, norm.x, norm.y, norm.w, norm.h);
|
2013-09-03 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void substractTaintedArea(const IntRect &rect)
|
|
|
|
{
|
|
|
|
if (!touchesTaintedArea(rect))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pixman_region16_t m_reg;
|
|
|
|
pixman_region_init_rect(&m_reg, rect.x, rect.y, rect.w, rect.h);
|
|
|
|
|
|
|
|
pixman_region_subtract(&tainted, &m_reg, &tainted);
|
|
|
|
|
|
|
|
pixman_region_fini(&m_reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool touchesTaintedArea(const IntRect &rect)
|
|
|
|
{
|
|
|
|
pixman_box16_t box;
|
|
|
|
box.x1 = rect.x;
|
|
|
|
box.y1 = rect.y;
|
|
|
|
box.x2 = rect.x + rect.w;
|
|
|
|
box.y2 = rect.y + rect.h;
|
|
|
|
|
|
|
|
pixman_region_overlap_t result =
|
|
|
|
pixman_region_contains_rectangle(&tainted, &box);
|
|
|
|
|
|
|
|
return result != PIXMAN_REGION_OUT;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-23 05:15:01 +00:00
|
|
|
void bindTexture(ShaderBase &shader)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-10-06 06:54:16 +00:00
|
|
|
TEX::bind(gl.tex);
|
|
|
|
shader.setTexSize(Vec2i(gl.width, gl.height));
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bindFBO()
|
|
|
|
{
|
2014-07-16 03:22:43 +00:00
|
|
|
FBO::bind(gl.fbo);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-23 05:15:01 +00:00
|
|
|
void pushSetViewport(ShaderBase &shader) const
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-10-06 06:54:16 +00:00
|
|
|
glState.viewport.pushSet(IntRect(0, 0, gl.width, gl.height));
|
2013-09-23 05:15:01 +00:00
|
|
|
shader.applyViewportProj();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void popViewport() const
|
|
|
|
{
|
2013-09-23 05:15:01 +00:00
|
|
|
glState.viewport.pop();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void blitQuad(Quad &quad)
|
|
|
|
{
|
2014-07-20 11:17:12 +00:00
|
|
|
glState.blend.pushSet(false);
|
2013-09-01 14:27:21 +00:00
|
|
|
quad.draw();
|
2014-07-20 11:17:12 +00:00
|
|
|
glState.blend.pop();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void fillRect(const IntRect &rect,
|
|
|
|
const Vec4 &color)
|
|
|
|
{
|
|
|
|
bindFBO();
|
|
|
|
|
|
|
|
glState.scissorTest.pushSet(true);
|
|
|
|
glState.scissorBox.pushSet(rect);
|
|
|
|
glState.clearColor.pushSet(color);
|
|
|
|
|
2013-10-01 11:10:14 +00:00
|
|
|
FBO::clear();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
glState.clearColor.pop();
|
|
|
|
glState.scissorBox.pop();
|
|
|
|
glState.scissorTest.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ensureFormat(SDL_Surface *&surf, Uint32 format)
|
|
|
|
{
|
|
|
|
if (surf->format->format == format)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SDL_Surface *surfConv = SDL_ConvertSurfaceFormat(surf, format, 0);
|
|
|
|
SDL_FreeSurface(surf);
|
|
|
|
surf = surfConv;
|
|
|
|
}
|
2014-07-11 01:25:28 +00:00
|
|
|
|
|
|
|
void onModified()
|
|
|
|
{
|
|
|
|
if (surface)
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(surface);
|
|
|
|
surface = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->modified();
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Bitmap::Bitmap(const char *filename)
|
|
|
|
{
|
|
|
|
SDL_RWops ops;
|
2013-11-30 13:28:50 +00:00
|
|
|
const char *extension;
|
|
|
|
shState->fileSystem().openRead(ops, filename, FileSystem::Image, false, &extension);
|
|
|
|
SDL_Surface *imgSurf = IMG_LoadTyped_RW(&ops, 1, extension);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
if (!imgSurf)
|
2014-01-05 09:28:35 +00:00
|
|
|
throw Exception(Exception::SDLError, "Error loading image '%s': %s",
|
|
|
|
filename, SDL_GetError());
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->ensureFormat(imgSurf, SDL_PIXELFORMAT_ABGR8888);
|
|
|
|
|
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
|
|
|
if (imgSurf->w > glState.caps.maxTexSize || imgSurf->h > glState.caps.maxTexSize)
|
2013-09-06 13:52:45 +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
|
|
|
/* Mega surface */
|
2014-07-11 01:25:28 +00:00
|
|
|
p = new BitmapPrivate(this);
|
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->megaSurface = imgSurf;
|
2013-09-06 13:52:45 +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
|
|
|
else
|
2013-09-06 13:52:45 +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
|
|
|
/* Regular surface */
|
|
|
|
TEXFBO tex;
|
2013-10-18 23:47:12 +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
|
|
|
try
|
|
|
|
{
|
2013-10-09 10:30:33 +00:00
|
|
|
tex = shState->texPool().request(imgSurf->w, imgSurf->h);
|
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
|
|
|
}
|
|
|
|
catch (const Exception &e)
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(imgSurf);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p = new BitmapPrivate(this);
|
2013-10-06 06:54:16 +00:00
|
|
|
p->gl = tex;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-06 06:54:16 +00:00
|
|
|
TEX::bind(p->gl.tex);
|
|
|
|
TEX::uploadImage(p->gl.width, p->gl.height, imgSurf->pixels, GL_RGBA);
|
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
|
|
|
SDL_FreeSurface(imgSurf);
|
|
|
|
}
|
2013-09-03 13:23:20 +00:00
|
|
|
|
|
|
|
p->addTaintedArea(rect());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap::Bitmap(int width, int height)
|
|
|
|
{
|
2013-09-04 16:11:16 +00:00
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
throw Exception(Exception::RGSSError, "failed to create bitmap");
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
TEXFBO tex = shState->texPool().request(width, height);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p = new BitmapPrivate(this);
|
2013-10-06 06:54:16 +00:00
|
|
|
p->gl = tex;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap::Bitmap(const Bitmap &other)
|
|
|
|
{
|
2013-10-31 09:26:39 +00:00
|
|
|
other.ensureNonMega();
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p = new BitmapPrivate(this);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
p->gl = shState->texPool().request(other.width(), other.height());
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
blt(0, 0, &other, rect());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap::~Bitmap()
|
|
|
|
{
|
2014-08-09 16:35:01 +00:00
|
|
|
if (p->megaSurface)
|
|
|
|
SDL_FreeSurface(p->megaSurface);
|
|
|
|
else
|
|
|
|
shState->texPool().release(p->gl);
|
|
|
|
|
|
|
|
delete p;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Bitmap::width() const
|
|
|
|
{
|
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
|
|
|
if (p->megaSurface)
|
|
|
|
return p->megaSurface->w;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-06 06:54:16 +00:00
|
|
|
return p->gl.width;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Bitmap::height() const
|
|
|
|
{
|
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
|
|
|
if (p->megaSurface)
|
|
|
|
return p->megaSurface->h;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-06 06:54:16 +00:00
|
|
|
return p->gl.height;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IntRect Bitmap::rect() const
|
|
|
|
{
|
|
|
|
return IntRect(0, 0, width(), height());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::blt(int x, int y,
|
2014-08-09 16:35:01 +00:00
|
|
|
const Bitmap *source, IntRect rect,
|
2013-09-01 14:27:21 +00:00
|
|
|
int opacity)
|
|
|
|
{
|
2014-08-09 16:35:01 +00:00
|
|
|
if (!source)
|
|
|
|
return;
|
|
|
|
|
2014-07-09 00:56:35 +00:00
|
|
|
// FIXME: RGSS allows the source rect to both lie outside
|
|
|
|
// the bitmap rect and be inverted in both directions;
|
|
|
|
// clamping only covers a subset of these cases (and
|
|
|
|
// doesn't fix anything for a direct stretch_blt call).
|
|
|
|
|
|
|
|
/* Clamp rect to source bitmap size */
|
2014-08-09 16:35:01 +00:00
|
|
|
if (rect.x + rect.w > source->width())
|
|
|
|
rect.w = source->width() - rect.x;
|
2014-07-09 00:56:35 +00:00
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
if (rect.y + rect.h > source->height())
|
|
|
|
rect.h = source->height() - rect.y;
|
2014-07-09 00:56:35 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
stretchBlt(IntRect(x, y, rect.w, rect.h),
|
|
|
|
source, rect, opacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::stretchBlt(const IntRect &destRect,
|
2014-08-09 16:35:01 +00:00
|
|
|
const Bitmap *source, const IntRect &sourceRect,
|
2013-09-03 13:23:20 +00:00
|
|
|
int opacity)
|
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
|
|
|
GUARD_MEGA;
|
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
if (!source)
|
|
|
|
return;
|
|
|
|
|
2013-09-03 13:31:29 +00:00
|
|
|
opacity = clamp(opacity, 0, 255);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
if (opacity == 0)
|
2013-09-25 15:07:43 +00:00
|
|
|
return;
|
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
if (source->megaSurface())
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-25 15:07:43 +00:00
|
|
|
/* Don't do transparent blits for now */
|
|
|
|
if (opacity < 255)
|
2014-08-09 16:35:01 +00:00
|
|
|
source->ensureNonMega();
|
2013-09-25 15:07:43 +00:00
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
SDL_Surface *srcSurf = source->megaSurface();
|
2013-09-25 15:07:43 +00:00
|
|
|
|
2014-08-09 11:43:15 +00:00
|
|
|
SDL_Rect srcRect = sourceRect;
|
|
|
|
SDL_Rect dstRect = destRect;
|
|
|
|
SDL_Rect btmRect = { 0, 0, width(), height() };
|
|
|
|
SDL_Rect bltRect;
|
|
|
|
|
|
|
|
if (SDL_IntersectRect(&btmRect, &dstRect, &bltRect) != SDL_TRUE)
|
|
|
|
return;
|
|
|
|
|
2013-09-25 15:07:43 +00:00
|
|
|
int bpp;
|
|
|
|
Uint32 rMask, gMask, bMask, aMask;
|
|
|
|
SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_ABGR8888,
|
|
|
|
&bpp, &rMask, &gMask, &bMask, &aMask);
|
|
|
|
SDL_Surface *blitTemp =
|
2014-08-09 11:43:15 +00:00
|
|
|
SDL_CreateRGBSurface(0, destRect.w, destRect.h, bpp, rMask, gMask, bMask, aMask);
|
2013-09-25 15:07:43 +00:00
|
|
|
|
|
|
|
// FXIME: This is supposed to be a scaled blit, put SDL2 for some reason
|
|
|
|
// makes the source surface unusable after BlitScaled() is called. Investigate!
|
2014-08-09 11:43:15 +00:00
|
|
|
SDL_BlitSurface(srcSurf, &srcRect, blitTemp, 0);
|
2013-09-25 15:07:43 +00:00
|
|
|
|
2013-10-06 06:54:16 +00:00
|
|
|
TEX::bind(p->gl.tex);
|
2014-08-09 11:43:15 +00:00
|
|
|
|
|
|
|
if (bltRect.w == dstRect.w && bltRect.h == dstRect.h)
|
|
|
|
{
|
|
|
|
TEX::uploadSubImage(destRect.x, destRect.y,
|
|
|
|
destRect.w, destRect.h,
|
|
|
|
blitTemp->pixels, GL_RGBA);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GLMeta::subRectImageUpload(blitTemp->w, bltRect.x - dstRect.x, bltRect.y - dstRect.y,
|
|
|
|
bltRect.x, bltRect.y, bltRect.w, bltRect.h, blitTemp, GL_RGBA);
|
|
|
|
GLMeta::subRectImageEnd();
|
|
|
|
}
|
|
|
|
|
2013-09-25 15:07:43 +00:00
|
|
|
|
|
|
|
SDL_FreeSurface(blitTemp);
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-09-01 14:27:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-09-25 15:07:43 +00:00
|
|
|
|
|
|
|
if (opacity == 255 && !p->touchesTaintedArea(destRect))
|
2013-09-03 13:23:20 +00:00
|
|
|
{
|
|
|
|
/* Fast blit */
|
2014-07-16 02:48:40 +00:00
|
|
|
GLMeta::blitBegin(p->gl);
|
2014-08-09 16:35:01 +00:00
|
|
|
GLMeta::blitSource(source->p->gl);
|
2014-07-16 02:48:40 +00:00
|
|
|
GLMeta::blitRectangle(sourceRect, destRect);
|
2014-07-16 02:53:08 +00:00
|
|
|
GLMeta::blitEnd();
|
2013-09-03 13:23:20 +00:00
|
|
|
}
|
|
|
|
else
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-03 13:23:20 +00:00
|
|
|
/* Fragment pipeline */
|
2013-09-01 14:27:21 +00:00
|
|
|
float normOpacity = (float) opacity / 255.0f;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
TEXFBO &gpTex = shState->gpTexFBO(destRect.w, destRect.h);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-16 02:48:40 +00:00
|
|
|
GLMeta::blitBegin(gpTex);
|
|
|
|
GLMeta::blitSource(p->gl);
|
|
|
|
GLMeta::blitRectangle(destRect, Vec2i());
|
2014-07-16 02:53:08 +00:00
|
|
|
GLMeta::blitEnd();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
FloatRect bltSubRect((float) sourceRect.x / source->width(),
|
|
|
|
(float) sourceRect.y / source->height(),
|
|
|
|
((float) source->width() / sourceRect.w) * ((float) destRect.w / gpTex.width),
|
|
|
|
((float) source->height() / sourceRect.h) * ((float) destRect.h / gpTex.height));
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 04:22:13 +00:00
|
|
|
BltShader &shader = shState->shaders().blt;
|
2013-09-01 14:27:21 +00:00
|
|
|
shader.bind();
|
|
|
|
shader.setDestination(gpTex.tex);
|
|
|
|
shader.setSubRect(bltSubRect);
|
|
|
|
shader.setOpacity(normOpacity);
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
Quad &quad = shState->gpQuad();
|
2013-09-01 14:27:21 +00:00
|
|
|
quad.setTexPosRect(sourceRect, destRect);
|
|
|
|
quad.setColor(Vec4(1, 1, 1, normOpacity));
|
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
source->p->bindTexture(shader);
|
2013-09-01 14:27:21 +00:00
|
|
|
p->bindFBO();
|
2013-09-23 05:15:01 +00:00
|
|
|
p->pushSetViewport(shader);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->blitQuad(quad);
|
|
|
|
|
|
|
|
p->popViewport();
|
|
|
|
}
|
|
|
|
|
2013-09-03 13:23:20 +00:00
|
|
|
p->addTaintedArea(destRect);
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::fillRect(int x, int y,
|
|
|
|
int width, int height,
|
|
|
|
const Vec4 &color)
|
|
|
|
{
|
|
|
|
fillRect(IntRect(x, y, width, height), color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::fillRect(const IntRect &rect, const Vec4 &color)
|
|
|
|
{
|
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
|
|
|
GUARD_MEGA;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->fillRect(rect, color);
|
|
|
|
|
2013-09-03 13:23:20 +00:00
|
|
|
if (color.w == 0)
|
|
|
|
/* Clear op */
|
|
|
|
p->substractTaintedArea(rect);
|
|
|
|
else
|
|
|
|
/* Fill op */
|
|
|
|
p->addTaintedArea(rect);
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 20:40:09 +00:00
|
|
|
#ifdef RGSS2
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
void Bitmap::gradientFillRect(int x, int y,
|
|
|
|
int width, int height,
|
|
|
|
const Vec4 &color1, const Vec4 &color2,
|
|
|
|
bool vertical)
|
|
|
|
{
|
|
|
|
gradientFillRect(IntRect(x, y, width, height), color1, color2, vertical);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::gradientFillRect(const IntRect &rect,
|
|
|
|
const Vec4 &color1, const Vec4 &color2,
|
|
|
|
bool vertical)
|
|
|
|
{
|
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
|
|
|
GUARD_MEGA;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 04:22:13 +00:00
|
|
|
SimpleColorShader &shader = shState->shaders().simpleColor;
|
2013-09-23 05:15:01 +00:00
|
|
|
shader.bind();
|
|
|
|
shader.setTranslation(Vec2i());
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
Quad &quad = shState->gpQuad();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
if (vertical)
|
|
|
|
{
|
|
|
|
quad.vert[0].color = color2;
|
|
|
|
quad.vert[1].color = color2;
|
|
|
|
quad.vert[2].color = color1;
|
|
|
|
quad.vert[3].color = color1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quad.vert[0].color = color1;
|
|
|
|
quad.vert[3].color = color1;
|
|
|
|
quad.vert[1].color = color2;
|
|
|
|
quad.vert[2].color = color2;
|
|
|
|
}
|
|
|
|
|
|
|
|
quad.setPosRect(rect);
|
|
|
|
|
|
|
|
p->bindFBO();
|
2013-09-23 05:15:01 +00:00
|
|
|
p->pushSetViewport(shader);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->blitQuad(quad);
|
|
|
|
|
|
|
|
p->popViewport();
|
|
|
|
|
2013-09-03 13:23:20 +00:00
|
|
|
p->addTaintedArea(rect);
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::clearRect(int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
clearRect(IntRect(x, y, width, height));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::clearRect(const IntRect &rect)
|
|
|
|
{
|
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
|
|
|
GUARD_MEGA;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->fillRect(rect, Vec4());
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-10-01 16:10:43 +00:00
|
|
|
void Bitmap::blur()
|
|
|
|
{
|
|
|
|
GUARD_MEGA;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
Quad &quad = shState->gpQuad();
|
2013-10-01 16:10:43 +00:00
|
|
|
FloatRect rect(0, 0, width(), height());
|
|
|
|
quad.setTexPosRect(rect, rect);
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
TEXFBO auxTex = shState->texPool().request(width(), height());
|
2013-10-01 16:10:43 +00:00
|
|
|
|
2013-12-11 04:22:13 +00:00
|
|
|
BlurShader &shader = shState->shaders().blur;
|
2013-10-01 16:10:43 +00:00
|
|
|
BlurShader::HPass &pass1 = shader.pass1;
|
|
|
|
BlurShader::VPass &pass2 = shader.pass2;
|
|
|
|
|
2014-07-20 11:17:12 +00:00
|
|
|
glState.blend.pushSet(false);
|
2013-10-01 16:10:43 +00:00
|
|
|
glState.viewport.pushSet(IntRect(0, 0, width(), height()));
|
|
|
|
|
2013-12-04 14:53:42 +00:00
|
|
|
TEX::bind(p->gl.tex);
|
2014-07-16 03:22:43 +00:00
|
|
|
FBO::bind(auxTex.fbo);
|
2013-10-01 16:10:43 +00:00
|
|
|
|
|
|
|
pass1.bind();
|
|
|
|
pass1.setTexSize(Vec2i(width(), height()));
|
|
|
|
pass1.applyViewportProj();
|
|
|
|
|
|
|
|
quad.draw();
|
|
|
|
|
|
|
|
TEX::bind(auxTex.tex);
|
2014-07-16 02:48:40 +00:00
|
|
|
p->bindFBO();
|
2013-10-01 16:10:43 +00:00
|
|
|
|
|
|
|
pass2.bind();
|
|
|
|
pass2.setTexSize(Vec2i(width(), height()));
|
|
|
|
pass2.applyViewportProj();
|
|
|
|
|
|
|
|
quad.draw();
|
|
|
|
|
|
|
|
glState.viewport.pop();
|
2014-07-20 11:17:12 +00:00
|
|
|
glState.blend.pop();
|
2013-10-01 16:10:43 +00:00
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->texPool().release(auxTex);
|
2013-10-01 16:10:43 +00:00
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-10-01 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 19:03:59 +00:00
|
|
|
void Bitmap::radialBlur(int angle, int divisions)
|
|
|
|
{
|
|
|
|
GUARD_MEGA;
|
|
|
|
|
|
|
|
angle = clamp<int>(angle, 0, 359);
|
|
|
|
divisions = clamp<int>(divisions, 2, 100);
|
|
|
|
|
|
|
|
const int _width = width();
|
|
|
|
const int _height = height();
|
|
|
|
|
|
|
|
float angleStep = (float) angle / (divisions-1);
|
|
|
|
float opacity = 1.0f / divisions;
|
|
|
|
float baseAngle = -((float) angle / 2);
|
|
|
|
|
|
|
|
ColorQuadArray qArray;
|
|
|
|
qArray.resize(5);
|
|
|
|
|
2013-12-26 19:18:33 +00:00
|
|
|
std::vector<Vertex> &vert = qArray.vertices;
|
2013-09-04 19:03:59 +00:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
/* Center */
|
|
|
|
FloatRect texRect(0, 0, _width, _height);
|
|
|
|
FloatRect posRect(0, 0, _width, _height);
|
|
|
|
|
|
|
|
i += Quad::setTexPosRect(&vert[i*4], texRect, posRect);
|
|
|
|
|
|
|
|
/* Upper */
|
|
|
|
posRect = FloatRect(0, 0, _width, -_height);
|
|
|
|
|
|
|
|
i += Quad::setTexPosRect(&vert[i*4], texRect, posRect);
|
|
|
|
|
|
|
|
/* Lower */
|
|
|
|
posRect = FloatRect(0, _height*2, _width, -_height);
|
|
|
|
|
|
|
|
i += Quad::setTexPosRect(&vert[i*4], texRect, posRect);
|
|
|
|
|
|
|
|
/* Left */
|
|
|
|
posRect = FloatRect(0, 0, -_width, _height);
|
|
|
|
|
|
|
|
i += Quad::setTexPosRect(&vert[i*4], texRect, posRect);
|
|
|
|
|
|
|
|
/* Right */
|
|
|
|
posRect = FloatRect(_width*2, 0, -_width, _height);
|
|
|
|
|
|
|
|
i += Quad::setTexPosRect(&vert[i*4], texRect, posRect);
|
|
|
|
|
|
|
|
for (int i = 0; i < 4*5; ++i)
|
|
|
|
vert[i].color = Vec4(1, 1, 1, opacity);
|
|
|
|
|
|
|
|
qArray.commit();
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
TEXFBO newTex = shState->texPool().request(_width, _height);
|
2013-09-04 19:03:59 +00:00
|
|
|
|
2014-07-16 03:22:43 +00:00
|
|
|
FBO::bind(newTex.fbo);
|
2013-09-04 19:03:59 +00:00
|
|
|
|
|
|
|
glState.clearColor.pushSet(Vec4());
|
2013-10-01 11:10:14 +00:00
|
|
|
FBO::clear();
|
2013-09-04 19:03:59 +00:00
|
|
|
|
|
|
|
Transform trans;
|
|
|
|
trans.setOrigin(Vec2(_width / 2.0f, _height / 2.0f));
|
|
|
|
trans.setPosition(Vec2(_width / 2.0f, _height / 2.0f));
|
|
|
|
|
|
|
|
glState.blendMode.pushSet(BlendAddition);
|
|
|
|
|
2013-12-11 04:22:13 +00:00
|
|
|
SimpleMatrixShader &shader = shState->shaders().simpleMatrix;
|
2013-09-04 19:03:59 +00:00
|
|
|
shader.bind();
|
|
|
|
|
|
|
|
p->bindTexture(shader);
|
|
|
|
TEX::setSmooth(true);
|
|
|
|
|
|
|
|
p->pushSetViewport(shader);
|
|
|
|
|
2013-10-01 10:15:28 +00:00
|
|
|
for (int i = 0; i < divisions; ++i)
|
2013-09-04 19:03:59 +00:00
|
|
|
{
|
|
|
|
trans.setRotation(baseAngle + i*angleStep);
|
|
|
|
shader.setMatrix(trans.getMatrix());
|
|
|
|
qArray.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
p->popViewport();
|
|
|
|
|
|
|
|
TEX::setSmooth(false);
|
|
|
|
|
|
|
|
glState.blendMode.pop();
|
|
|
|
glState.clearColor.pop();
|
|
|
|
|
2013-12-04 14:53:42 +00:00
|
|
|
shState->texPool().release(p->gl);
|
|
|
|
p->gl = newTex;
|
2013-09-04 19:03:59 +00:00
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-09-04 19:03:59 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 20:40:09 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
void Bitmap::clear()
|
|
|
|
{
|
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
|
|
|
GUARD_MEGA;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->bindFBO();
|
|
|
|
|
|
|
|
glState.clearColor.pushSet(Vec4());
|
|
|
|
|
2013-10-01 11:10:14 +00:00
|
|
|
FBO::clear();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
glState.clearColor.pop();
|
|
|
|
|
2013-09-03 13:23:20 +00:00
|
|
|
p->clearTaintedArea();
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 09:19:16 +00:00
|
|
|
Color Bitmap::getPixel(int x, int y) const
|
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
|
|
|
GUARD_MEGA;
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
if (x < 0 || y < 0 || x >= width() || y >= height())
|
|
|
|
return Vec4();
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
if (!p->surface)
|
|
|
|
{
|
|
|
|
p->allocSurface();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-16 03:22:43 +00:00
|
|
|
FBO::bind(p->gl.fbo);
|
2014-01-31 09:19:16 +00:00
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
glState.viewport.pushSet(IntRect(0, 0, width(), height()));
|
2014-01-31 09:19:16 +00:00
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
gl.ReadPixels(0, 0, width(), height(), GL_RGBA, GL_UNSIGNED_BYTE, p->surface->pixels);
|
|
|
|
|
|
|
|
glState.viewport.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t offset = x*p->format->BytesPerPixel + y*p->surface->pitch;
|
|
|
|
uint8_t *bytes = (uint8_t*) p->surface->pixels + offset;
|
|
|
|
uint32_t pixel = *((uint32_t*) bytes);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
return Color((pixel >> p->format->Rshift) & 0xFF,
|
|
|
|
(pixel >> p->format->Gshift) & 0xFF,
|
|
|
|
(pixel >> p->format->Bshift) & 0xFF,
|
|
|
|
(pixel >> p->format->Ashift) & 0xFF);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 09:19:16 +00:00
|
|
|
void Bitmap::setPixel(int x, int y, const Color &color)
|
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
|
|
|
GUARD_MEGA;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-01-31 09:19:16 +00:00
|
|
|
uint8_t pixel[] =
|
|
|
|
{
|
|
|
|
(uint8_t) clamp<double>(color.red, 0, 255),
|
|
|
|
(uint8_t) clamp<double>(color.green, 0, 255),
|
|
|
|
(uint8_t) clamp<double>(color.blue, 0, 255),
|
|
|
|
(uint8_t) clamp<double>(color.alpha, 0, 255)
|
|
|
|
};
|
|
|
|
|
|
|
|
TEX::bind(p->gl.tex);
|
|
|
|
TEX::uploadSubImage(x, y, 1, 1, &pixel, GL_RGBA);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-09-03 13:23:20 +00:00
|
|
|
p->addTaintedArea(IntRect(x, y, 1, 1));
|
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::hueChange(int hue)
|
|
|
|
{
|
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
|
|
|
GUARD_MEGA;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
if ((hue % 360) == 0)
|
|
|
|
return;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
TEXFBO newTex = shState->texPool().request(width(), height());
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
FloatRect texRect(rect());
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
Quad &quad = shState->gpQuad();
|
2013-09-01 14:27:21 +00:00
|
|
|
quad.setTexPosRect(texRect, texRect);
|
2013-09-04 17:07:28 +00:00
|
|
|
quad.setColor(Vec4(1, 1, 1, 1));
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
/* Calculate hue parameter */
|
|
|
|
hue = wrapRange(hue, 0, 359);
|
|
|
|
float hueAdj = -((M_PI * 2) / 360) * hue;
|
|
|
|
|
2013-12-11 04:22:13 +00:00
|
|
|
HueShader &shader = shState->shaders().hue;
|
2013-09-01 14:27:21 +00:00
|
|
|
shader.bind();
|
|
|
|
shader.setHueAdjust(hueAdj);
|
|
|
|
|
2014-07-16 03:22:43 +00:00
|
|
|
FBO::bind(newTex.fbo);
|
2013-09-23 05:15:01 +00:00
|
|
|
p->pushSetViewport(shader);
|
|
|
|
p->bindTexture(shader);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->blitQuad(quad);
|
|
|
|
|
|
|
|
p->popViewport();
|
|
|
|
|
2013-09-23 05:15:01 +00:00
|
|
|
TEX::unbind();
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->texPool().release(p->gl);
|
2013-10-06 06:54:16 +00:00
|
|
|
p->gl = newTex;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::drawText(int x, int y,
|
|
|
|
int width, int height,
|
|
|
|
const char *str, int align)
|
|
|
|
{
|
|
|
|
drawText(IntRect(x, y, width, height), str, align);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::drawText(const IntRect &rect, const char *str, int align)
|
|
|
|
{
|
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
|
|
|
GUARD_MEGA;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
if (*str == '\0')
|
|
|
|
return;
|
|
|
|
|
2013-10-02 22:48:12 +00:00
|
|
|
if (str[0] == ' ' && str[1] == '\0')
|
|
|
|
return;
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
TTF_Font *font = p->font->getSdlFont();
|
2013-09-03 13:23:20 +00:00
|
|
|
Color *fontColor = p->font->getColor();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
SDL_Color c;
|
2013-09-03 13:23:20 +00:00
|
|
|
fontColor->toSDLColor(c);
|
|
|
|
|
|
|
|
float txtAlpha = fontColor->norm.w;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
SDL_Surface *txtSurf;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
if (shState->rtData().config.solidFonts)
|
2013-09-01 14:27:21 +00:00
|
|
|
txtSurf = TTF_RenderUTF8_Solid(font, str, c);
|
|
|
|
else
|
|
|
|
txtSurf = TTF_RenderUTF8_Blended(font, str, c);
|
|
|
|
|
2014-07-21 01:38:22 +00:00
|
|
|
p->ensureFormat(txtSurf, SDL_PIXELFORMAT_ABGR8888);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
int alignX = rect.x;
|
|
|
|
|
|
|
|
switch (align)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case Left :
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Center :
|
|
|
|
alignX += (rect.w - txtSurf->w) / 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Right :
|
|
|
|
alignX += rect.w - txtSurf->w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alignX < rect.x)
|
|
|
|
alignX = rect.x;
|
|
|
|
|
|
|
|
int alignY = rect.y + (rect.h - txtSurf->h) / 2;
|
|
|
|
|
|
|
|
float squeeze = (float) rect.w / txtSurf->w;
|
|
|
|
|
|
|
|
if (squeeze > 1)
|
|
|
|
squeeze = 1;
|
|
|
|
|
|
|
|
FloatRect posRect(alignX, alignY, txtSurf->w * squeeze, txtSurf->h);
|
|
|
|
|
2013-09-23 05:15:01 +00:00
|
|
|
Vec2i gpTexSize;
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->ensureTexSize(txtSurf->w, txtSurf->h, gpTexSize);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-02 19:39:44 +00:00
|
|
|
bool fastBlit = !p->touchesTaintedArea(posRect) && txtAlpha == 1.0;
|
2013-09-03 13:23:20 +00:00
|
|
|
|
|
|
|
if (fastBlit)
|
|
|
|
{
|
|
|
|
if (squeeze == 1.0)
|
|
|
|
{
|
2013-10-02 19:39:44 +00:00
|
|
|
/* Even faster: upload directly to bitmap texture.
|
|
|
|
* We have to make sure the posRect lies within the texture
|
|
|
|
* boundaries or texSubImage will generate errors.
|
|
|
|
* If it partly lies outside bounds we have to upload
|
|
|
|
* the clipped visible part of it. */
|
|
|
|
SDL_Rect btmRect;
|
|
|
|
btmRect.x = btmRect.y = 0;
|
|
|
|
btmRect.w = width();
|
|
|
|
btmRect.h = height();
|
|
|
|
|
|
|
|
SDL_Rect txtRect;
|
|
|
|
txtRect.x = posRect.x;
|
|
|
|
txtRect.y = posRect.y;
|
|
|
|
txtRect.w = posRect.w;
|
|
|
|
txtRect.h = posRect.h;
|
|
|
|
|
|
|
|
SDL_Rect inters;
|
|
|
|
|
|
|
|
/* If we have no intersection at all,
|
|
|
|
* there's nothing to upload to begin with */
|
|
|
|
if (SDL_IntersectRect(&btmRect, &txtRect, &inters))
|
|
|
|
{
|
2014-07-11 06:31:12 +00:00
|
|
|
bool subImage = false;
|
|
|
|
int subSrcX = 0, subSrcY = 0;
|
|
|
|
|
2013-10-02 19:39:44 +00:00
|
|
|
if (inters.w != txtRect.w || inters.h != txtRect.h)
|
|
|
|
{
|
|
|
|
/* Clip the text surface */
|
2014-07-11 06:31:12 +00:00
|
|
|
subSrcX = inters.x - txtRect.x;
|
|
|
|
subSrcY = inters.y - txtRect.y;
|
|
|
|
subImage = true;
|
2013-10-02 19:39:44 +00:00
|
|
|
|
|
|
|
posRect.x = inters.x;
|
|
|
|
posRect.y = inters.y;
|
|
|
|
posRect.w = inters.w;
|
|
|
|
posRect.h = inters.h;
|
|
|
|
}
|
|
|
|
|
2013-10-06 06:54:16 +00:00
|
|
|
TEX::bind(p->gl.tex);
|
2013-10-16 20:54:05 +00:00
|
|
|
|
2014-07-11 06:31:12 +00:00
|
|
|
if (!subImage)
|
|
|
|
{
|
|
|
|
TEX::uploadSubImage(posRect.x, posRect.y,
|
|
|
|
posRect.w, posRect.h,
|
2014-07-21 01:38:22 +00:00
|
|
|
txtSurf->pixels, GL_RGBA);
|
2014-07-11 06:31:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GLMeta::subRectImageUpload(txtSurf->w, subSrcX, subSrcY,
|
|
|
|
posRect.x, posRect.y,
|
|
|
|
posRect.w, posRect.h,
|
2014-07-21 01:38:22 +00:00
|
|
|
txtSurf, GL_RGBA);
|
2014-07-16 02:53:08 +00:00
|
|
|
GLMeta::subRectImageEnd();
|
2014-07-11 06:31:12 +00:00
|
|
|
}
|
2013-10-02 19:39:44 +00:00
|
|
|
}
|
2013-09-03 13:23:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Squeezing involved: need to use intermediary TexFBO */
|
2013-10-09 10:30:33 +00:00
|
|
|
TEXFBO &gpTF = shState->gpTexFBO(txtSurf->w, txtSurf->h);
|
2013-09-03 13:23:20 +00:00
|
|
|
|
|
|
|
TEX::bind(gpTF.tex);
|
2014-07-21 01:38:22 +00:00
|
|
|
TEX::uploadSubImage(0, 0, txtSurf->w, txtSurf->h, txtSurf->pixels, GL_RGBA);
|
2013-09-03 13:23:20 +00:00
|
|
|
|
2014-07-16 02:48:40 +00:00
|
|
|
GLMeta::blitBegin(p->gl);
|
|
|
|
GLMeta::blitSource(gpTF);
|
|
|
|
GLMeta::blitRectangle(IntRect(0, 0, txtSurf->w, txtSurf->h),
|
2014-07-16 03:22:43 +00:00
|
|
|
posRect, true);
|
2014-07-16 02:53:08 +00:00
|
|
|
GLMeta::blitEnd();
|
2013-09-03 13:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
/* Aquire a partial copy of the destination
|
|
|
|
* buffer we're about to render to */
|
2013-10-09 10:30:33 +00:00
|
|
|
TEXFBO &gpTex2 = shState->gpTexFBO(posRect.w, posRect.h);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-16 02:48:40 +00:00
|
|
|
GLMeta::blitBegin(gpTex2);
|
|
|
|
GLMeta::blitSource(p->gl);
|
|
|
|
GLMeta::blitRectangle(posRect, Vec2i());
|
2014-07-16 02:53:08 +00:00
|
|
|
GLMeta::blitEnd();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
FloatRect bltRect(0, 0,
|
2013-09-23 05:15:01 +00:00
|
|
|
(float) gpTexSize.x / gpTex2.width,
|
|
|
|
(float) gpTexSize.y / gpTex2.height);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-11 04:22:13 +00:00
|
|
|
BltShader &shader = shState->shaders().blt;
|
2013-09-01 14:27:21 +00:00
|
|
|
shader.bind();
|
2013-09-23 05:15:01 +00:00
|
|
|
shader.setTexSize(gpTexSize);
|
2013-09-01 14:27:21 +00:00
|
|
|
shader.setSource();
|
|
|
|
shader.setDestination(gpTex2.tex);
|
|
|
|
shader.setSubRect(bltRect);
|
2013-09-03 13:23:20 +00:00
|
|
|
shader.setOpacity(txtAlpha);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->bindTex();
|
2014-07-21 01:38:22 +00:00
|
|
|
TEX::uploadSubImage(0, 0, txtSurf->w, txtSurf->h, txtSurf->pixels, GL_RGBA);
|
2013-09-03 13:23:20 +00:00
|
|
|
TEX::setSmooth(true);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
Quad &quad = shState->gpQuad();
|
2013-09-03 13:23:20 +00:00
|
|
|
quad.setTexRect(FloatRect(0, 0, txtSurf->w, txtSurf->h));
|
|
|
|
quad.setPosRect(posRect);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-09-03 13:23:20 +00:00
|
|
|
p->bindFBO();
|
|
|
|
p->pushSetViewport(shader);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-20 11:17:12 +00:00
|
|
|
p->blitQuad(quad);
|
2013-09-03 13:23:20 +00:00
|
|
|
|
|
|
|
p->popViewport();
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_FreeSurface(txtSurf);
|
2013-10-02 19:39:44 +00:00
|
|
|
p->addTaintedArea(posRect);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-11 01:25:28 +00:00
|
|
|
p->onModified();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
/* http://www.lemoda.net/c/utf8-to-ucs2/index.html */
|
|
|
|
static uint16_t utf8_to_ucs2(const char *_input,
|
|
|
|
const char **end_ptr)
|
|
|
|
{
|
|
|
|
const unsigned char *input =
|
|
|
|
reinterpret_cast<const unsigned char*>(_input);
|
|
|
|
*end_ptr = _input;
|
|
|
|
|
|
|
|
if (input[0] == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (input[0] < 0x80)
|
|
|
|
{
|
|
|
|
*end_ptr = _input + 1;
|
|
|
|
|
|
|
|
return input[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((input[0] & 0xE0) == 0xE0)
|
|
|
|
{
|
|
|
|
if (input[1] == 0 || input[2] == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*end_ptr = _input + 3;
|
|
|
|
|
|
|
|
return (input[0] & 0x0F)<<12 |
|
|
|
|
(input[1] & 0x3F)<<6 |
|
|
|
|
(input[2] & 0x3F);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((input[0] & 0xC0) == 0xC0)
|
|
|
|
{
|
|
|
|
if (input[1] == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*end_ptr = _input + 2;
|
|
|
|
|
|
|
|
return (input[0] & 0x1F)<<6 |
|
|
|
|
(input[1] & 0x3F);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
IntRect Bitmap::textSize(const char *str)
|
|
|
|
{
|
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
|
|
|
GUARD_MEGA;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
TTF_Font *font = p->font->getSdlFont();
|
|
|
|
|
|
|
|
int w, h;
|
|
|
|
TTF_SizeUTF8(font, str, &w, &h);
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
/* If str is one character long, *endPtr == 0 */
|
|
|
|
const char *endPtr;
|
|
|
|
uint16_t ucs2 = utf8_to_ucs2(str, &endPtr);
|
2013-10-20 20:14:38 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
/* For cursive characters, returning the advance
|
|
|
|
* as width yields better results */
|
|
|
|
if (p->font->getItalic() && *endPtr == '\0')
|
|
|
|
TTF_GlyphMetrics(font, ucs2, 0, 0, 0, 0, &w);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
return IntRect(0, 0, w, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_ATTR_SIMPLE(Bitmap, Font, Font*, p->font)
|
|
|
|
|
2013-09-06 10:26:41 +00:00
|
|
|
TEXFBO &Bitmap::getGLTypes()
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-10-06 06:54:16 +00:00
|
|
|
return p->gl;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-25 15:07:43 +00:00
|
|
|
SDL_Surface *Bitmap::megaSurface() const
|
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
|
|
|
{
|
|
|
|
return p->megaSurface;
|
|
|
|
}
|
|
|
|
|
2013-09-25 15:07:43 +00:00
|
|
|
void Bitmap::ensureNonMega() const
|
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
|
|
|
{
|
|
|
|
GUARD_MEGA;
|
|
|
|
}
|
|
|
|
|
2013-09-23 05:15:01 +00:00
|
|
|
void Bitmap::bindTex(ShaderBase &shader)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-23 05:15:01 +00:00
|
|
|
p->bindTexture(shader);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 17:05:36 +00:00
|
|
|
void Bitmap::taintArea(const IntRect &rect)
|
|
|
|
{
|
|
|
|
p->addTaintedArea(rect);
|
|
|
|
}
|