argh, clean out copy
[supertux.git] / src / unison / src / video / Window.cpp
1 //          Copyright Timothy Goya 2007.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE_1_0.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <unison/video/Window.hpp>
7 #include <unison/video/Surface.hpp>
8 #include <unison/video/Texture.hpp>
9 #include <unison/video/Renderers.hpp>
10 #include <unison/video/backend/Renderer.hpp>
11 #include <unison/video/backend/Window.hpp>
12
13 #include <assert.h>
14
15 namespace Unison
16 {
17    namespace Video
18    {
19       Window::Window() :
20          logical_size(),
21          title(),
22          icon(Surface(Area(1, 1))),
23          window(0),
24          list(),
25          layers()
26       {
27       }
28
29       Window::~Window()
30       {
31       }
32
33       Window &Window::get()
34       {
35          // FIXME: fix init order more naturally
36          Renderers::get();
37          static Window window;
38          return window;
39       }
40
41       void Window::set_logical_size(const Area &logical_size)
42       {
43          this->logical_size = logical_size;
44          if(window)
45          {
46             std::vector<Surface> surfaces = Texture::save_textures();
47             open(get_size(), is_fullscreen());
48             Texture::load_textures(surfaces);
49          }
50       }
51
52       Area Window::get_logical_size() const
53       {
54          return logical_size;
55       }
56
57       void Window::open(const Area &size, bool fullscreen)
58       {
59          std::vector<Surface> surfaces = Texture::save_textures();
60          if(logical_size.x == 0 || logical_size.y == 0)
61          {
62             logical_size = size;
63          }
64          delete window;
65          window = Renderers::get().get_renderer().create_window(size, logical_size, fullscreen);
66          assert(window);
67          Texture::load_textures(surfaces);
68          window->set_title(title);
69          window->set_icon(icon);
70          redraw();
71       }
72
73       void Window::take_screenshot(const std::string &filename) const
74       {
75          assert(window);
76          window->take_screenshot(filename);
77       }
78
79       void Window::flip()
80       {
81          list = layers;
82          layers.clear();
83          redraw();
84       }
85
86       void Window::redraw()
87       {
88          assert(window);
89          fill(Color::BLACK);
90          window->draw(list);
91          window->flip();
92       }
93
94       void Window::set_title(const std::string &title)
95       {
96          this->title = title;
97          if(window)
98          {
99             window->set_title(title);
100          }
101       }
102
103       void Window::set_icon(const Surface &icon)
104       {
105          this->icon = icon.get_size() == Area() ? Surface(Area(1, 1)) : icon;
106          if(window)
107          {
108             window->set_icon(icon);
109          }
110       }
111
112       Area Window::get_size() const
113       {
114          assert(window);
115          return window->get_size();
116       }
117
118       bool Window::is_fullscreen() const
119       {
120          assert(window);
121          return window->is_fullscreen();
122       }
123
124       bool Window::is_open() const
125       {
126          return window;
127       }
128    }
129 }