00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "platform/display.hpp"
00035 #include "platform/lowlevel.hpp"
00036 #include "platform/extensions.hpp"
00037
00038 #include <cmath>
00039
00040 namespace gsgl
00041 {
00042
00043 using namespace data;
00044 using namespace math;
00045
00046 namespace platform
00047 {
00048
00049 config_variable<int> display::DISPLAY_WIDTH(L"display/width", 1028);
00050 config_variable<int> display::DISPLAY_HEIGHT(L"display/height", 768);
00051
00052
00053
00054 display::display(const int & width, const int & height, bool is_console)
00055 : surface(0), is_console(is_console)
00056 {
00057 unsigned int rmask, gmask, bmask, amask;
00058
00059 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
00060 rmask = 0xff000000;
00061 gmask = 0x00ff0000;
00062 bmask = 0x0000ff00;
00063 amask = 0x000000ff;
00064 #else
00065
00066 rmask = 0x000000ff;
00067 gmask = 0x0000ff00;
00068 bmask = 0x00ff0000;
00069 amask = 0xff000000;
00070 #endif
00071
00072 if (is_console)
00073 {
00074
00075 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
00076 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
00077
00078 surface = SDL_SetVideoMode(width, height, 32, SDL_OPENGL);
00079
00080 if (surface)
00081 gsgl::platform::init_extensions();
00082
00083
00084 center_console_window(width, height);
00085 }
00086 else
00087 {
00088 surface = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA, width, height, 32, rmask, gmask, bmask, amask);
00089 }
00090 }
00091
00092 display::~display()
00093 {
00094 if (!is_console)
00095 SDL_FreeSurface(surface);
00096 }
00097
00098 int display::get_width() const
00099 {
00100 return surface->w;
00101 }
00102
00103 int display::get_height() const
00104 {
00105 return surface->h;
00106 }
00107
00108
00109 gsgl::real_t display::get_aspect_ratio() const
00110 {
00111 return static_cast<gsgl::real_t>(surface->w) / static_cast<gsgl::real_t>(surface->h);
00112 }
00113
00114
00115 void display::draw_rectangle(float x1, float y1, float x2, float y2,
00116 float s1, float t1, float s2, float t2)
00117 {
00118 glBegin(GL_TRIANGLE_STRIP);
00119
00120
00121 glNormal3f(0, 0, 1);
00122 glTexCoord2f(s1, t2);
00123 glVertex2f(x1, y2);
00124
00125
00126 glNormal3f(0, 0, 1);
00127 glTexCoord2f(s1, t1);
00128 glVertex2f(x1, y1);
00129
00130
00131 glNormal3f(0, 0, 1);
00132 glTexCoord2f(s2, t2);
00133 glVertex2f(x2, y2);
00134
00135
00136 glNormal3f(0, 0, 1);
00137 glTexCoord2f(s2, t1);
00138 glVertex2f(x2, y1);
00139
00140 glEnd(); CHECK_GL_ERRORS();
00141 }
00142
00143
00144 void display::record_3d_text_info()
00145 {
00146 glGetIntegerv(GL_VIEWPORT, text_draw_viewport); CHECK_GL_ERRORS();
00147 glGetFloatv(GL_MODELVIEW_MATRIX, text_draw_modelview.ptr()); CHECK_GL_ERRORS();
00148 glGetFloatv(GL_PROJECTION_MATRIX, text_draw_projection.ptr()); CHECK_GL_ERRORS();
00149 text_draw_pm = text_draw_projection * text_draw_modelview; CHECK_GL_ERRORS();
00150 }
00151
00152
00153 void display::draw_text_start()
00154 {
00155 glPushAttrib(GL_ALL_ATTRIB_BITS); CHECK_GL_ERRORS();
00156 glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS); CHECK_GL_ERRORS();
00157
00158 glMatrixMode(GL_PROJECTION); CHECK_GL_ERRORS();
00159 glLoadIdentity(); CHECK_GL_ERRORS();
00160 glOrtho(0.0, surface->w, 0.0, surface->h, -1, 1);
00161
00162 glMatrixMode(GL_MODELVIEW); CHECK_GL_ERRORS();
00163 glPushMatrix();
00164 glLoadIdentity(); CHECK_GL_ERRORS();
00165 }
00166
00167
00168 void display::draw_2d_text(const gsgl::real_t x, const gsgl::real_t y, font *f, const string & str)
00169 {
00170 glMatrixMode(GL_MODELVIEW); CHECK_GL_ERRORS();
00171 glLoadIdentity(); CHECK_GL_ERRORS();
00172 glTranslatef(x, y, 0); CHECK_GL_ERRORS();
00173 f->draw(str);
00174 }
00175
00176
00177 void display::draw_3d_text(const vector & p, font *f, const string & str, const gsgl::real_t x_offset, const gsgl::real_t y_offset)
00178 {
00179 vector p_in_clip_space = text_draw_pm * p;
00180
00181 if (p_in_clip_space.get_z() >= 0)
00182 {
00183 gsgl::real_t wx, wy;
00184 wx = text_draw_viewport[0] + text_draw_viewport[2]*(p_in_clip_space.get_x() + 1)/2;
00185 wy = text_draw_viewport[1] + text_draw_viewport[3]*(p_in_clip_space.get_y() + 1)/2;
00186
00187 if (wx >= -100 && wx < surface->w && wy >= -10 && wy < surface->h+10)
00188 draw_2d_text(wx + x_offset, wy + y_offset, f, str);
00189 }
00190 }
00191
00192
00193 void display::draw_text_stop()
00194 {
00195 glMatrixMode(GL_MODELVIEW);
00196 glPopMatrix();
00197
00198 glPopClientAttrib(); CHECK_GL_ERRORS();
00199 glPopAttrib(); CHECK_GL_ERRORS();
00200 }
00201
00202
00203 #ifdef WIN32
00204
00205 void display::center_console_window(const int & width, const int & height)
00206 {
00207 SDL_SysWMinfo info;
00208 SDL_VERSION(&info.version);
00209
00210 if (SDL_GetWMInfo(&info))
00211 {
00212 HWND desktop = GetDesktopWindow();
00213 RECT rect;
00214 if (desktop && GetWindowRect(desktop, &rect))
00215 {
00216 int left = ((rect.right - rect.left) < 2*(rect.bottom - rect.top)) ? (rect.left + rect.right - width)/2 : (3*rect.left + rect.right - 2*width)/4;
00217 int top = (rect.top + rect.bottom - height)/2;
00218
00219 SetWindowPos(info.window, HWND_TOP, left, top, width, height, SWP_SHOWWINDOW);
00220 }
00221 }
00222 }
00223
00224 #else
00225 #error You must define display::center_console_window() for your platform!
00226 #endif
00227
00228 }
00229
00230 }