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)
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>
26 Window::Window(const Area &size, const Area &logical_size, bool fullscreen) :
27 logical_size(logical_size),
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);
40 window = SDL_SetVideoMode(size.x, size.y, 0, SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0));
43 glDisable(GL_DEPTH_TEST);
44 glDisable(GL_CULL_FACE);
45 glEnable(GL_TEXTURE_2D);
48 glMatrixMode(GL_PROJECTION);
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);
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());
66 void Window::take_screenshot(const std::string &filename) const
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);
81 void Window::set_title(const std::string &title)
83 SDL_WM_SetCaption(title.c_str(), title.c_str());
86 void Window::set_icon(const Surface &icon)
88 SDL_Surface *icon_surface = SDL::Blitters::create_sdl_surface_from(icon);
90 SDL_WM_SetIcon(icon_surface, 0);
91 SDL_FreeSurface(icon_surface);
94 Area Window::get_size() const
96 return Area(window->w, window->h);
99 bool Window::is_fullscreen() const
101 return window->flags & SDL_FULLSCREEN;
104 void Window::blit(const Surface &src, const Point &dst_pos, const Rect &src_rect, const RenderOptions &options)
106 Video::Texture texture(src);
107 blit(src, dst_pos, src_rect, options);
110 void Window::blit(const Video::Texture &src, const Point &dst_pos, const Rect &src_rect, const RenderOptions &options)
112 Texture *texture = dynamic_cast<Texture *>(Backend::Texture::get_texture(src.get_id()));
116 texture->blit_draw_buffer(src_rect, dst_pos, options);
119 void Window::fill(const Color &color, const Rect &rect)
124 glDisable(GL_TEXTURE_2D);
125 glColor4ub(color.red, color.green, color.blue, color.alpha);
130 glVertex2i(get_size().x, 0);
131 glVertex2i(get_size().x, get_size().y);
132 glVertex2i(0, get_size().y);
139 glTranslatef(rect.pos.x, rect.pos.y, 0);
143 glVertex2i(rect.size.x, 0);
144 glVertex2i(rect.size.x, rect.size.y);
145 glVertex2i(0, rect.size.y);
150 glEnable(GL_TEXTURE_2D);
151 //glColor4ub(0xff, 0xff, 0xff, 0xff);
154 void Window::fill_blend(const Color &color, const Rect &rect)
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);
166 glVertex2i(get_size().x, 0);
167 glVertex2i(get_size().x, get_size().y);
168 glVertex2i(0, get_size().y);
175 glTranslatef(rect.pos.x, rect.pos.y, 0);
179 glVertex2i(rect.size.x, 0);
180 glVertex2i(rect.size.x, rect.size.y);
181 glVertex2i(0, rect.size.y);
186 glEnable(GL_TEXTURE_2D);
187 //glColor4ub(0xff, 0xff, 0xff, 0xff);