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