| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
// Standard C++ library includes |
| 23 |
#include <iostream> |
| 24 |
#include <iomanip> |
| 25 |
|
| 26 |
// Platform specific includes |
| 27 |
#include "fife_unittest.h" |
| 28 |
|
| 29 |
// 3rd party library includes |
| 30 |
#include <boost/scoped_ptr.hpp> |
| 31 |
#include <boost/shared_ptr.hpp> |
| 32 |
#include <SDL.h> |
| 33 |
|
| 34 |
// FIFE includes |
| 35 |
// These includes are split up in two parts, separated by one empty line |
| 36 |
// First block: files included from the FIFE root src directory |
| 37 |
// Second block: files included from the same folder |
| 38 |
#include "vfs/vfs.h" |
| 39 |
#include "util/structures/rect.h" |
| 40 |
#include "util/time/timemanager.h" |
| 41 |
#include "vfs/vfsdirectory.h" |
| 42 |
#include "vfs/raw/rawdata.h" |
| 43 |
#include "video/image_location.h" |
| 44 |
#include "video/image.h" |
| 45 |
#include "video/imagepool.h" |
| 46 |
#include "video/sdl/renderbackendsdl.h" |
| 47 |
#include "video/opengl/renderbackendopengl.h" |
| 48 |
#include "loaders/native/video_loaders/image_loader.h" |
| 49 |
#include "loaders/native/video_loaders/subimage_loader.h" |
| 50 |
#include "util/base/exception.h" |
| 51 |
|
| 52 |
using namespace FIFE; |
| 53 |
|
| 54 |
static const std::string IMAGE_FILE = "tests/data/beach_e1.png"; |
| 55 |
static const std::string ALPHA_IMAGE_FILE = "tests/data/alpha_fidgit.png"; |
| 56 |
static const std::string SUBIMAGE_FILE = "tests/data/rpg_tiles_01.png"; |
| 57 |
|
| 58 |
// Environment |
| 59 |
struct environment { |
| 60 |
boost::shared_ptr<TimeManager> timemanager; |
| 61 |
boost::shared_ptr<VFS> vfs; |
| 62 |
|
| 63 |
environment() |
| 64 |
: timemanager(new TimeManager()), |
| 65 |
vfs(new VFS()) { |
| 66 |
vfs->addSource(new VFSDirectory(vfs.get())); |
| 67 |
if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER) < 0) { |
| 68 |
throw SDLException(SDL_GetError()); |
| 69 |
} |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
void test_image(VFS* vfs, RenderBackend& renderbackend) { |
| 74 |
renderbackend.init(); |
| 75 |
renderbackend.createMainScreen(800, 600, 0, false, "FIFE", ""); |
| 76 |
|
| 77 |
ImageLoader provider(vfs); |
| 78 |
boost::scoped_ptr<Image> img(dynamic_cast<Image*>(provider.loadResource(ImageLocation(IMAGE_FILE)))); |
| 79 |
|
| 80 |
int h = img->getHeight(); |
| 81 |
int w = img->getWidth(); |
| 82 |
for (int i = 0; i < 100; i++) { |
| 83 |
renderbackend.startFrame(); |
| 84 |
img->render(Rect(i, i, w, h)); |
| 85 |
renderbackend.endFrame(); |
| 86 |
} |
| 87 |
for (int j = 0; j < 5; j++) { |
| 88 |
for (int i = -10; i < 10; i++) { |
| 89 |
renderbackend.startFrame(); |
| 90 |
img->setXShift(i); |
| 91 |
img->setYShift(i); |
| 92 |
img->render(Rect(200, 200, w, h)); |
| 93 |
renderbackend.endFrame(); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void test_subimage(VFS* vfs, RenderBackend& renderbackend) { |
| 99 |
renderbackend.init(); |
| 100 |
renderbackend.createMainScreen(800, 600, 0, false, "FIFE", ""); |
| 101 |
|
| 102 |
ImageLoader imgprovider(vfs); |
| 103 |
boost::scoped_ptr<Image> img(dynamic_cast<Image*>(imgprovider.loadResource(ImageLocation(SUBIMAGE_FILE)))); |
| 104 |
|
| 105 |
ImageLocation location(SUBIMAGE_FILE); |
| 106 |
location.setParentSource(&*img); |
| 107 |
int W = img->getWidth(); |
| 108 |
int w = W / 12; |
| 109 |
int H = img->getHeight(); |
| 110 |
int h = H / 12; |
| 111 |
location.setWidth(w); |
| 112 |
location.setHeight(h); |
| 113 |
std::vector<Image*> subimages; |
| 114 |
|
| 115 |
SubImageLoader subprovider; |
| 116 |
for (int x = 0; x < (W - w); x+=w) { |
| 117 |
for (int y = 0; y < (H - h); y+=h) { |
| 118 |
location.setXShift(x); |
| 119 |
location.setYShift(y); |
| 120 |
Image* sub = dynamic_cast<Image*>(subprovider.loadResource(location)); |
| 121 |
subimages.push_back(sub); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
for (unsigned int i = 0; i < 200; i++) { |
| 126 |
renderbackend.startFrame(); |
| 127 |
subimages[i / 40]->render(Rect(200, 200, w, h)); |
| 128 |
renderbackend.endFrame(); |
| 129 |
} |
| 130 |
std::vector<Image*>::iterator i = subimages.begin(); |
| 131 |
while (i != subimages.end()) { |
| 132 |
delete *i; |
| 133 |
i++; |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
TEST(test_sdl_alphaoptimize) |
| 139 |
{ |
| 140 |
environment env; |
| 141 |
RenderBackendSDL renderbackend; |
| 142 |
renderbackend.init(); |
| 143 |
renderbackend.createMainScreen(800, 600, 0, false, "FIFE", ""); |
| 144 |
renderbackend.setAlphaOptimizerEnabled(true); |
| 145 |
|
| 146 |
ImageLoader provider(env.vfs.get()); |
| 147 |
env.vfs.get()->exists(IMAGE_FILE); |
| 148 |
boost::scoped_ptr<Image> img(dynamic_cast<Image*>(provider.loadResource(ImageLocation(IMAGE_FILE)))); |
| 149 |
env.vfs.get()->exists(ALPHA_IMAGE_FILE); |
| 150 |
boost::scoped_ptr<Image> alpha_img(dynamic_cast<Image*>(provider.loadResource(ImageLocation(ALPHA_IMAGE_FILE)))); |
| 151 |
|
| 152 |
int h0 = img->getHeight(); |
| 153 |
int w0 = img->getWidth(); |
| 154 |
|
| 155 |
int h1 = alpha_img->getHeight(); |
| 156 |
int w1 = alpha_img->getWidth(); |
| 157 |
for(int i=0; i != 200; ++i) { |
| 158 |
renderbackend.startFrame(); |
| 159 |
img->render(Rect(i, i, w0, h0)); |
| 160 |
alpha_img->render(Rect(i, i, w1, h1)); |
| 161 |
alpha_img->render(Rect(i, h0+i, w1, h1)); |
| 162 |
img->render(Rect(i, h0+i, w0, h0)); |
| 163 |
renderbackend.endFrame(); |
| 164 |
} |
| 165 |
|
| 166 |
CHECK(img->getSurface()->format->Amask == 0); |
| 167 |
CHECK(alpha_img->getSurface()->format->Amask != 0); |
| 168 |
} |
| 169 |
|
| 170 |
TEST(test_sdl_image) |
| 171 |
{ |
| 172 |
environment env; |
| 173 |
RenderBackendSDL renderbackend; |
| 174 |
test_image(env.vfs.get(), renderbackend); |
| 175 |
} |
| 176 |
|
| 177 |
TEST(test_ogl_image) |
| 178 |
{ |
| 179 |
environment env; |
| 180 |
RenderBackendOpenGL renderbackend; |
| 181 |
test_image(env.vfs.get(), renderbackend); |
| 182 |
} |
| 183 |
|
| 184 |
TEST(test_sdl_subimage) |
| 185 |
{ |
| 186 |
environment env; |
| 187 |
RenderBackendSDL renderbackend; |
| 188 |
CHECK(env.vfs.get()->exists(IMAGE_FILE)); |
| 189 |
test_subimage(env.vfs.get(), renderbackend); |
| 190 |
} |
| 191 |
|
| 192 |
TEST(test_ogl_subimage) |
| 193 |
{ |
| 194 |
environment env; |
| 195 |
RenderBackendOpenGL renderbackend; |
| 196 |
test_subimage(env.vfs.get(), renderbackend); |
| 197 |
} |
| 198 |
|
| 199 |
// need this here because SDL redefines |
| 200 |
// main to SDL_main in SDL_main.h |
| 201 |
#ifdef main |
| 202 |
#undef main |
| 203 |
#endif |
| 204 |
|
| 205 |
int main() |
| 206 |
{ |
| 207 |
return UnitTest::RunAllTests(); |
| 208 |
} |