d1b7026e9ca8a370efe522e7d8f4966e012b4012
[supertux.git] / src / video / gl / gl_renderer.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HEADER_SUPERTUX_VIDEO_GL_RENDERER_HPP
18 #define HEADER_SUPERTUX_VIDEO_GL_RENDERER_HPP
19
20 #include "math/size.hpp"
21 #include "video/drawing_request.hpp"
22 #include "video/renderer.hpp"
23
24 #include "SDL.h"
25 #include <math.h>
26
27 namespace {
28
29 inline void intern_draw(float left, float top, float right, float bottom,
30                         float uv_left, float uv_top,
31                         float uv_right, float uv_bottom,
32                         float angle, float alpha,
33                         const Color& color,
34                         const Blend& blend,
35                         DrawingEffect effect)
36 {
37   if(effect & HORIZONTAL_FLIP)
38     std::swap(uv_left, uv_right);
39
40   if(effect & VERTICAL_FLIP)
41     std::swap(uv_top, uv_bottom);
42
43   glBlendFunc(blend.sfactor, blend.dfactor);
44   glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
45
46   // unrotated blit
47   if (angle == 0.0f) {
48     float vertices[] = {
49       left, top,
50       right, top,
51       right, bottom,
52       left, bottom,
53     };
54     glVertexPointer(2, GL_FLOAT, 0, vertices);
55
56     float uvs[] = {
57       uv_left, uv_top,
58       uv_right, uv_top,
59       uv_right, uv_bottom,
60       uv_left, uv_bottom,
61     };
62     glTexCoordPointer(2, GL_FLOAT, 0, uvs);
63
64     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
65   } else {
66     // rotated blit
67     float center_x = (left + right) / 2;
68     float center_y = (top + bottom) / 2;
69
70     float sa = sinf(angle/180.0f*M_PI);
71     float ca = cosf(angle/180.0f*M_PI);
72
73     left  -= center_x;
74     right -= center_x;
75
76     top    -= center_y;
77     bottom -= center_y;
78
79     float vertices[] = {
80       left*ca - top*sa + center_x, left*sa + top*ca + center_y,
81       right*ca - top*sa + center_x, right*sa + top*ca + center_y,
82       right*ca - bottom*sa + center_x, right*sa + bottom*ca + center_y,
83       left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y
84     };
85     glVertexPointer(2, GL_FLOAT, 0, vertices);
86
87     float uvs[] = {
88       uv_left, uv_top,
89       uv_right, uv_top,
90       uv_right, uv_bottom,
91       uv_left, uv_bottom,
92     };
93     glTexCoordPointer(2, GL_FLOAT, 0, uvs);
94
95     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
96   }
97
98   // FIXME: find a better way to restore the blend mode
99   glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
100   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
101 }
102
103 } // namespace
104
105
106
107 class GLRenderer : public Renderer
108 {
109 private:
110   SDL_Window* window;
111   SDL_GLContext glcontext;
112   SDL_Rect viewport;
113   Size desktop_size;
114   Size screen_size;
115   bool fullscreen_active;
116
117   GLuint last_texture;
118
119 public:
120   GLRenderer();
121   ~GLRenderer();
122
123   void draw_surface(const DrawingRequest& request);
124   void draw_surface_part(const DrawingRequest& request);
125   void draw_gradient(const DrawingRequest& request);
126   void draw_filled_rect(const DrawingRequest& request);
127   void draw_inverse_ellipse(const DrawingRequest& request);
128   void do_take_screenshot();
129   void flip();
130   void resize(int w, int h);
131   void apply_config();
132   void apply_video_mode(const Size& size, bool fullscreen);
133   Vector to_logical(int physical_x, int physical_y);
134   void set_gamma(float gamma);
135   SDL_Window* get_window() const { return window; }
136 };
137
138 #endif
139
140 /* EOF */