argh, clean out copy
[supertux.git] / src / unison / src / video / opengl / 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 "Window.hpp"
7 #include "Texture.hpp"
8 #include <unison/video/Window.hpp>
9 #include <unison/video/Surface.hpp>
10 #include <unison/video/Texture.hpp>
11 #include <unison/video/Renderers.hpp>
12 #include <unison/video/sdl/Blitters.hpp>
13 #include <unison/video/backend/Renderer.hpp>
14 #include <unison/video/backend/Texture.hpp>
15
16 #include <assert.h>
17
18 #include "SDL.h"
19
20 namespace Unison
21 {
22    namespace Video
23    {
24       namespace OpenGL
25       {
26          Window::Window(const Area &size, const Area &logical_size, bool fullscreen) :
27             logical_size(logical_size),
28             window(0)
29          {
30             assert(size.x);
31             assert(size.y);
32
33             SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
34             //SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
35             SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
36             SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
37             SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
38             SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
39
40             window = SDL_SetVideoMode(size.x, size.y, 0, SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0));
41             assert(window);
42
43             glDisable(GL_DEPTH_TEST);
44             glDisable(GL_CULL_FACE);
45             glEnable(GL_TEXTURE_2D);
46             //glEnable(GL_BLEND);
47
48             glMatrixMode(GL_PROJECTION);
49             glLoadIdentity();
50             glOrtho(0, logical_size.x, logical_size.y, 0, -1.0, 1.0);
51             glMatrixMode(GL_MODELVIEW);
52             glViewport(0, 0, size.x, size.y);
53             glLoadIdentity();
54             //glTranslatef(0, 0, 0);
55             //glScalef(GLfloat(size.x) / logical_size.x, GLfloat(size.y) / logical_size.y, 1);
56             //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
57             glClearColor(0, 0, 0, 1);
58             glClear(GL_COLOR_BUFFER_BIT);
59             assert(!glGetError());
60          }
61
62          Window::~Window()
63          {
64          }
65
66          void Window::take_screenshot(const std::string &filename) const
67          {
68             //Surface surface(logical_size);
69             Surface surface(get_size());
70             glReadBuffer(GL_FRONT);
71             glReadPixels(0, 0, get_size().x, get_size().y, GL_RGBA, GL_UNSIGNED_BYTE, surface.get_pixels());
72             //glReadPixels(0, 0, logical_size.x, logical_size.y, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
73             surface.v_flip().save(filename);
74          }
75
76          void Window::flip()
77          {
78             SDL_GL_SwapBuffers();
79          }
80
81          void Window::set_title(const std::string &title)
82          {
83             SDL_WM_SetCaption(title.c_str(), title.c_str());
84          }
85
86          void Window::set_icon(const Surface &icon)
87          {
88             SDL_Surface *icon_surface = SDL::Blitters::create_sdl_surface_from(icon);
89             assert(icon_surface);
90             SDL_WM_SetIcon(icon_surface, 0);
91             SDL_FreeSurface(icon_surface);
92          }
93
94          Area Window::get_size() const
95          {
96             return Area(window->w, window->h);
97          }
98
99          bool Window::is_fullscreen() const
100          {
101             return window->flags & SDL_FULLSCREEN;
102          }
103
104          void Window::blit(const Surface &src, const Point &dst_pos, const Rect &src_rect, const RenderOptions &options)
105          {
106             Video::Texture texture(src);
107             blit(src, dst_pos, src_rect, options);
108          }
109
110          void Window::blit(const Video::Texture &src, const Point &dst_pos, const Rect &src_rect, const RenderOptions &options)
111          {
112             Texture *texture = dynamic_cast<Texture *>(Backend::Texture::get_texture(src.get_id()));
113             assert(texture);
114             assert(window);
115
116             texture->blit_draw_buffer(src_rect, dst_pos, options);
117          }
118
119          void Window::fill(const Color &color, const Rect &rect)
120          {
121             assert(window);
122
123             glDisable(GL_BLEND);
124             glDisable(GL_TEXTURE_2D);
125             glColor4ub(color.red, color.green, color.blue, color.alpha);
126             if(rect == Rect())
127             {
128                glBegin(GL_QUADS);
129                   glVertex2i(0, 0);
130                   glVertex2i(get_size().x, 0);
131                   glVertex2i(get_size().x, get_size().y);
132                   glVertex2i(0, get_size().y);
133                glEnd();
134             }
135             else
136             {
137                glPushMatrix();
138
139                glTranslatef(rect.pos.x, rect.pos.y, 0);
140
141                glBegin(GL_QUADS);
142                   glVertex2i(0, 0);
143                   glVertex2i(rect.size.x, 0);
144                   glVertex2i(rect.size.x, rect.size.y);
145                   glVertex2i(0, rect.size.y);
146                glEnd();
147
148                glPopMatrix();
149             }
150             glEnable(GL_TEXTURE_2D);
151             //glColor4ub(0xff, 0xff, 0xff, 0xff);
152          }
153
154          void Window::fill_blend(const Color &color, const Rect &rect)
155          {
156             assert(window);
157
158             glEnable(GL_BLEND);
159             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
160             glDisable(GL_TEXTURE_2D);
161             glColor4ub(color.red, color.green, color.blue, color.alpha);
162             if(rect == Rect())
163             {
164                glBegin(GL_QUADS);
165                   glVertex2i(0, 0);
166                   glVertex2i(get_size().x, 0);
167                   glVertex2i(get_size().x, get_size().y);
168                   glVertex2i(0, get_size().y);
169                glEnd();
170             }
171             else
172             {
173                glPushMatrix();
174
175                glTranslatef(rect.pos.x, rect.pos.y, 0);
176
177                glBegin(GL_QUADS);
178                   glVertex2i(0, 0);
179                   glVertex2i(rect.size.x, 0);
180                   glVertex2i(rect.size.x, rect.size.y);
181                   glVertex2i(0, rect.size.y);
182                glEnd();
183
184                glPopMatrix();
185             }
186             glEnable(GL_TEXTURE_2D);
187             //glColor4ub(0xff, 0xff, 0xff, 0xff);
188          }
189       }
190    }
191 }