comparison tests/core_tests/test_imagepool++.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 4c8334b0ab30
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 #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()) {
65 if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER) < 0) {
66 throw SDLException(SDL_GetError());
67 }
68 }
69 };
70
71 TEST(test_image_pool)
72 {
73 environment env;
74 RenderBackendSDL renderbackend;
75
76 boost::scoped_ptr<VFS> vfs(new VFS());
77 vfs->addSource(new VFSDirectory(vfs.get()));
78
79 renderbackend.init();
80 renderbackend.createMainScreen(800, 600, 0, false);
81 ImagePool pool;
82 pool.addResourceLoader(new SubImageLoader());
83 pool.addResourceLoader(new ImageLoader(vfs.get()));
84
85 CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED));
86 CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED));
87
88 pool.addResourceFromLocation(ImageLocation(IMAGE_FILE));
89 CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED));
90 CHECK_EQUAL(1, pool.getResourceCount(RES_NON_LOADED));
91 ImageLocation location(SUBIMAGE_FILE);
92 ImageLoader imgprovider(vfs.get());
93 int fullImgInd = pool.addResourceFromLocation(ImageLocation(SUBIMAGE_FILE));
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 pool.addResourceFromLocation(location);
108
109 CHECK_EQUAL(1, pool.getResourceCount(RES_LOADED));
110 CHECK_EQUAL(2, pool.getResourceCount(RES_NON_LOADED));
111
112 for (int k = 0; k < 3; k++) {
113 for (int j = 0, s = pool.getResourceCount(RES_LOADED | RES_NON_LOADED); j < s; j++) {
114 std::cout << j << std::endl;
115 Image& r = dynamic_cast<Image&>(pool.get(j));
116 int h = r.getHeight();
117 int w = r.getWidth();
118 for (int i = 20; i > 0; i-=2) {
119 renderbackend.startFrame();
120 r.render(Rect(i, i, w, h));
121 renderbackend.endFrame();
122 TimeManager::instance()->update();
123 }
124 }
125 CHECK_EQUAL(3, pool.getResourceCount(RES_LOADED));
126 CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED));
127 }
128 CHECK_EQUAL(3, pool.getResourceCount(RES_LOADED));
129 CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED));
130 pool.clear();
131 CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED));
132 CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED));
133 }