Mercurial > fife-parpg
comparison engine/core/video/opengl/renderbackendopengl.cpp @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 90005975cdbb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 /*************************************************************************** | |
2 * Copyright (C) 2005-2008 by the FIFE team * | |
3 * http://www.fifengine.de * | |
4 * This file is part of FIFE. * | |
5 * * | |
6 * FIFE is free software; you can redistribute it and/or modify * | |
7 * it under the terms of the GNU General Public License as published by * | |
8 * the Free Software Foundation; either version 2 of the License, or * | |
9 * (at your option) any later version. * | |
10 * * | |
11 * This program 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 * | |
14 * GNU General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU General Public License * | |
17 * along with this program; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * | |
20 ***************************************************************************/ | |
21 | |
22 // Standard C++ library includes | |
23 | |
24 // Platform specific includes | |
25 | |
26 // 3rd party library includes | |
27 #include <SDL.h> | |
28 | |
29 // FIFE includes | |
30 #include "util/base/exception.h" | |
31 | |
32 #include "fife_opengl.h" | |
33 #include "glimage.h" | |
34 #include "renderbackendopengl.h" | |
35 | |
36 namespace FIFE { | |
37 | |
38 RenderBackendOpenGL::RenderBackendOpenGL() : RenderBackend() { | |
39 // Get the pixelformat we want. | |
40 SDL_Surface* testsurface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, 1, 1, 32, | |
41 RMASK, GMASK, BMASK ,AMASK); | |
42 | |
43 m_rgba_format = *(testsurface->format); | |
44 SDL_FreeSurface(testsurface); | |
45 } | |
46 | |
47 const std::string& RenderBackendOpenGL::getName() const { | |
48 static std::string backend_name = "OpenGL"; | |
49 return backend_name; | |
50 } | |
51 | |
52 RenderBackendOpenGL::~RenderBackendOpenGL() { | |
53 } | |
54 | |
55 | |
56 void RenderBackendOpenGL::init() { | |
57 Uint32 flags = SDL_INIT_VIDEO; | |
58 if (SDL_InitSubSystem(flags) < 0) | |
59 throw SDLException(SDL_GetError()); | |
60 | |
61 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); // temporary hack | |
62 } | |
63 | |
64 Image* RenderBackendOpenGL::createMainScreen(unsigned int width, unsigned int height, unsigned char bitsPerPixel, bool fs) { | |
65 delete m_screen; | |
66 m_screen = 0; | |
67 | |
68 Uint32 flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_HWACCEL; | |
69 if ( fs ) { | |
70 flags |= SDL_FULLSCREEN; | |
71 } | |
72 | |
73 SDL_Surface* screen = NULL; | |
74 | |
75 if( 0 == bitsPerPixel ) { | |
76 /// autodetect best mode | |
77 unsigned char possibleBitsPerPixel[] = {32, 24, 16, 0}; | |
78 int i = 0; | |
79 while( true ) { | |
80 bitsPerPixel = possibleBitsPerPixel[i]; | |
81 if( !bitsPerPixel ) { | |
82 throw SDLException("Videomode not available"); | |
83 } | |
84 | |
85 if ( SDL_VideoModeOK(width, height, bitsPerPixel, flags) ) { | |
86 screen = SDL_SetVideoMode(width, height, bitsPerPixel, flags); | |
87 if( screen ) { | |
88 break; | |
89 } | |
90 } | |
91 ++i; | |
92 } | |
93 } else { | |
94 if ( !SDL_VideoModeOK(width, height, bitsPerPixel, flags) ) { | |
95 throw SDLException("Videomode not available"); | |
96 } | |
97 screen = SDL_SetVideoMode(width, height, bitsPerPixel, flags); | |
98 } | |
99 | |
100 SDL_WM_SetCaption("FIFE", 0); | |
101 | |
102 if (!screen) { | |
103 throw SDLException(SDL_GetError()); | |
104 } | |
105 | |
106 glViewport(0, 0, width, height); | |
107 glMatrixMode(GL_PROJECTION); | |
108 gluOrtho2D(0, width, height, 0); | |
109 glMatrixMode(GL_MODELVIEW); | |
110 | |
111 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
112 glEnable(GL_TEXTURE_2D); | |
113 glEnable(GL_BLEND); | |
114 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
115 | |
116 glEnable(GL_SCISSOR_TEST); | |
117 | |
118 glPointSize(1.0); | |
119 glLineWidth(1.0); | |
120 | |
121 m_screen = new GLImage(screen); | |
122 return m_screen; | |
123 } | |
124 | |
125 void RenderBackendOpenGL::startFrame() { | |
126 glClear(GL_COLOR_BUFFER_BIT); | |
127 } | |
128 | |
129 void RenderBackendOpenGL::endFrame() { | |
130 SDL_GL_SwapBuffers(); | |
131 } | |
132 | |
133 Image* RenderBackendOpenGL::createImage(SDL_Surface* surface) { | |
134 // Given an abritary surface, we must convert it to the format GLImage will understand. | |
135 // It's easiest to let SDL do this for us. | |
136 | |
137 // Uh. Gotta love this :-) | |
138 // Check for colorkey too? | |
139 // Leave out the loss/shift checks? | |
140 if( m_rgba_format.BitsPerPixel == surface->format->BitsPerPixel | |
141 && m_rgba_format.Rmask == surface->format->Rmask | |
142 && m_rgba_format.Gmask == surface->format->Gmask | |
143 && m_rgba_format.Bmask == surface->format->Bmask | |
144 && m_rgba_format.Amask == surface->format->Amask | |
145 && m_rgba_format.Rshift == surface->format->Rshift | |
146 && m_rgba_format.Gshift == surface->format->Gshift | |
147 && m_rgba_format.Bshift == surface->format->Bshift | |
148 && m_rgba_format.Ashift == surface->format->Ashift | |
149 && m_rgba_format.Rloss == surface->format->Rloss | |
150 && m_rgba_format.Gloss == surface->format->Gloss | |
151 && m_rgba_format.Bloss == surface->format->Bloss | |
152 && m_rgba_format.Aloss == surface->format->Aloss | |
153 && surface->flags & SDL_SRCALPHA ) { | |
154 | |
155 return new GLImage(surface); | |
156 } | |
157 | |
158 SDL_Surface* conv = SDL_ConvertSurface(surface, &m_rgba_format, SDL_SWSURFACE|SDL_SRCALPHA); | |
159 GLImage* image = new GLImage(conv); | |
160 SDL_FreeSurface( surface ); | |
161 return image; | |
162 } | |
163 | |
164 Image* RenderBackendOpenGL::createImage(const uint8_t* data, unsigned int width, unsigned int height) { | |
165 return new GLImage(data, width, height); | |
166 } | |
167 | |
168 bool RenderBackendOpenGL::putPixel(int x, int y, int r, int g, int b) { | |
169 if ((x < 0) || (x >= (int)getWidth()) || (y < 0) || (y >= (int)getHeight())) { | |
170 return false; | |
171 } | |
172 glColor4ub(r, g, b, 255); | |
173 glBegin(GL_POINTS); | |
174 glVertex2i(x, y); | |
175 glEnd(); | |
176 std::cout << x << ", "<< y << "\n"; | |
177 return true; | |
178 } | |
179 | |
180 void RenderBackendOpenGL::drawLine(const Point& p1, const Point& p2, int r, int g, int b) { | |
181 glColor4ub(r, g, b, 255); | |
182 glBegin(GL_LINES); | |
183 glVertex3f(p1.x+0.5f, p1.y+0.5f, 0); | |
184 glVertex3f(p2.x+0.5f, p2.y+0.5f, 0); | |
185 glEnd(); | |
186 | |
187 glBegin(GL_POINTS); | |
188 glVertex3f(p2.x+0.5f, p2.y+0.5f, 0); | |
189 glEnd(); | |
190 } | |
191 | |
192 void RenderBackendOpenGL::drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4, int r, int g, int b) { | |
193 glColor4ub(r, g, b, 165); | |
194 glBegin(GL_QUADS); | |
195 glVertex3f(p1.x, p1.y, 0); | |
196 glVertex3f(p2.x, p2.y, 0); | |
197 glVertex3f(p3.x, p3.y, 0); | |
198 glVertex3f(p4.x, p4.y, 0); | |
199 glEnd(); | |
200 } | |
201 | |
202 } |