| 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 SUBIMAGE_FILE = "tests/data/rpg_tiles_01.png"; |
| 56 |
static const std::string ANIM_FILE = "tests/data/crate_full_001.xml"; |
| 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()), vfs(new VFS()) { |
| 65 |
if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER) < 0) { |
| 66 |
throw SDLException(SDL_GetError()); |
| 67 |
} |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
TEST_FIXTURE(environment, test_image_pool) |
| 72 |
{ |
| 73 |
RenderBackendSDL renderbackend; |
| 74 |
|
| 75 |
vfs->addSource(new VFSDirectory(vfs.get())); |
| 76 |
|
| 77 |
renderbackend.init(); |
| 78 |
renderbackend.createMainScreen(800, 600, 0, false, "FIFE", ""); |
| 79 |
ImagePool pool; |
| 80 |
pool.addResourceLoader(new SubImageLoader()); |
| 81 |
pool.addResourceLoader(new ImageLoader(vfs.get())); |
| 82 |
|
| 83 |
CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED)); |
| 84 |
CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED)); |
| 85 |
|
| 86 |
ImageLocation location(IMAGE_FILE); |
| 87 |
pool.addResourceFromLocation(&location); |
| 88 |
CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED)); |
| 89 |
CHECK_EQUAL(1, pool.getResourceCount(RES_NON_LOADED)); |
| 90 |
|
| 91 |
location = ImageLocation(SUBIMAGE_FILE); |
| 92 |
ImageLoader imgprovider(vfs.get()); |
| 93 |
int fullImgInd = pool.addResourceFromLocation(&location); |
| 94 |
CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED)); |
| 95 |
CHECK_EQUAL(2, pool.getResourceCount(RES_NON_LOADED)); |
| 96 |
Image& img = pool.getImage(fullImgInd); |
| 97 |
CHECK_EQUAL(1, pool.getResourceCount(RES_LOADED)); |
| 98 |
CHECK_EQUAL(1, pool.getResourceCount(RES_NON_LOADED)); |
| 99 |
|
| 100 |
location.setParentSource(&img); |
| 101 |
int W = img.getWidth(); |
| 102 |
int w = W / 12; |
| 103 |
int H = img.getHeight(); |
| 104 |
int h = H / 12; |
| 105 |
location.setWidth(w); |
| 106 |
location.setHeight(h); |
| 107 |
CHECK(w != 0 && h !=0); |
| 108 |
|
| 109 |
int subImgInd = pool.addResourceFromLocation(&location); |
| 110 |
CHECK(fullImgInd != subImgInd); |
| 111 |
|
| 112 |
CHECK_EQUAL(1, pool.getResourceCount(RES_LOADED)); |
| 113 |
CHECK_EQUAL(2, pool.getResourceCount(RES_NON_LOADED)); |
| 114 |
|
| 115 |
for (int k = 0; k < 3; k++) { |
| 116 |
for (int j = 0, s = pool.getResourceCount(RES_LOADED | RES_NON_LOADED); j < s; j++) { |
| 117 |
std::cout << j << std::endl; |
| 118 |
Image& r = dynamic_cast<Image&>(pool.get(j)); |
| 119 |
int h = r.getHeight(); |
| 120 |
int w = r.getWidth(); |
| 121 |
for (int i = 20; i > 0; i-=2) { |
| 122 |
renderbackend.startFrame(); |
| 123 |
r.render(Rect(i, i, w, h)); |
| 124 |
renderbackend.endFrame(); |
| 125 |
TimeManager::instance()->update(); |
| 126 |
} |
| 127 |
} |
| 128 |
CHECK_EQUAL(3, pool.getResourceCount(RES_LOADED)); |
| 129 |
CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED)); |
| 130 |
} |
| 131 |
CHECK_EQUAL(3, pool.getResourceCount(RES_LOADED)); |
| 132 |
CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED)); |
| 133 |
|
| 134 |
CHECK_EQUAL(3, pool.purgeLoadedResources() ); |
| 135 |
|
| 136 |
CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED)); |
| 137 |
CHECK_EQUAL(3, pool.getResourceCount(RES_NON_LOADED)); |
| 138 |
pool.reset(); |
| 139 |
CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED)); |
| 140 |
CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED)); |
| 141 |
} |
| 142 |
|
| 143 |
// need this here because SDL redefines |
| 144 |
// main to SDL_main in SDL_main.h |
| 145 |
#ifdef main |
| 146 |
#undef main |
| 147 |
#endif |
| 148 |
|
| 149 |
int main() |
| 150 |
{ |
| 151 |
return UnitTest::RunAllTests(); |
| 152 |
} |
| 153 |
|
| 154 |
|