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
24 // Platform specific includes
25 #include "fife_unittest.h"
26
27 // 3rd party library includes
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/shared_ptr.hpp>
30 #include <SDL.h>
31 #include <fifechan.hpp>
32
33 #include "vfs/vfs.h"
34 #include "util/structures/rect.h"
35 #include "util/time/timemanager.h"
36 #include "vfs/vfs.h"
37 #include "vfs/vfsdirectory.h"
38 #include "vfs/raw/rawdata.h"
39 #include "video/image_location.h"
40 #include "video/image.h"
41 #include "video/imagepool.h"
42 #include "video/sdl/renderbackendsdl.h"
43 #include "video/opengl/renderbackendopengl.h"
44 #include "loaders/native/video_loaders/image_loader.h"
45 #include "loaders/native/video_loaders/subimage_loader.h"
46 #include "util/base/exception.h"
47 #include "gui/base/opengl/opengl_gui_graphics.h"
48 #include "gui/base/sdl/sdl_gui_graphics.h"
49 #include "gui/base/gui_image.h"
50 #include "gui/base/gui_imageloader.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 struct environment {
57 boost::shared_ptr<TimeManager> timemanager;
58
environmentenvironment59 environment()
60 : timemanager(new TimeManager()) {
61 if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER) < 0) {
62 throw SDLException(SDL_GetError());
63 }
64 }
65 };
66
test_gui_image(RenderBackend & renderbackend,fcn::Graphics & graphics,ImagePool & pool)67 void test_gui_image(RenderBackend& renderbackend, fcn::Graphics& graphics, ImagePool& pool) {
68 boost::scoped_ptr<VFS> vfs(new VFS());
69 vfs->addSource(new VFSDirectory(vfs.get()));
70
71 pool.addResourceLoader(new SubImageLoader());
72 pool.addResourceLoader(new ImageLoader(vfs.get()));
73
74 GuiImageLoader imageloader(pool);
75 fcn::Image::setImageLoader(&imageloader);
76
77
78 fcn::Container* top = new fcn::Container();
79 top->setDimension(fcn::Rectangle(0, 0, 200, 300));
80 fcn::Gui* gui = new fcn::Gui();
81 gui->setGraphics(&graphics);
82 gui->setTop(top);
83 fcn::Label* label = new fcn::Label("Label");
84
85 fcn::Image* guiimage = fcn::Image::load(IMAGE_FILE);
86 fcn::Icon* icon = new fcn::Icon(guiimage);
87
88 top->add(label, 10, 10);
89 top->add(icon, 10, 30);
90
91 ImageLoader provider(vfs.get());
92 boost::scoped_ptr<Image> img(dynamic_cast<Image*>(provider.loadResource(ImageLocation(IMAGE_FILE))));
93
94 int h = img->getHeight();
95 int w = img->getWidth();
96 for (int i = 0; i < 100; i+=2) {
97 renderbackend.startFrame();
98 img->render(Rect(i, i, w, h));
99 gui->logic();
100 gui->draw();
101 renderbackend.endFrame();
102 }
103 delete label;
104 delete icon;
105 delete guiimage;
106 }
107
108
TEST(test_sdl_gui_image)109 TEST(test_sdl_gui_image)
110 {
111 environment env;
112 RenderBackendSDL renderbackend;
113 renderbackend.init();
114 ImagePool pool;
115 Image* screen = renderbackend.createMainScreen(800, 600, 0, false, "FIFE", "");
116 SdlGuiGraphics graphics(pool);
117 graphics.setTarget(screen->getSurface());
118 test_gui_image(renderbackend, graphics, pool);
119 }
120
TEST(test_ogl_gui_image)121 TEST(test_ogl_gui_image)
122 {
123 environment env;
124 RenderBackendOpenGL renderbackend;
125 renderbackend.init();
126 ImagePool pool;
127 renderbackend.createMainScreen(800, 600, 0, false, "FIFE", "");
128 OpenGLGuiGraphics graphics(pool);
129 test_gui_image(renderbackend, graphics, pool);
130 }
131
132 // need this here because SDL redefines
133 // main to SDL_main in SDL_main.h
134 #ifdef main
135 #undef main
136 #endif
137
main()138 int main()
139 {
140 return UnitTest::RunAllTests();
141 }
142