1 //**************************************************************************
2 //**
3 //** ## ## ## ## ## #### #### ### ###
4 //** ## ## ## ## ## ## ## ## ## ## #### ####
5 //** ## ## ## ## ## ## ## ## ## ## ## ## ## ##
6 //** ## ## ######## ## ## ## ## ## ## ## ### ##
7 //** ### ## ## ### ## ## ## ## ## ##
8 //** # ## ## # #### #### ## ##
9 //**
10 //** $Id: gl_agl.cpp 4346 2010-12-17 14:27:56Z dj_jl $
11 //**
12 //** Copyright (C) 1999-2006 Jānis Legzdiņš
13 //**
14 //** This program is free software; you can redistribute it and/or
15 //** modify it under the terms of the GNU General Public License
16 //** as published by the Free Software Foundation; either version 2
17 //** of the License, or (at your option) any later version.
18 //**
19 //** This program is distributed in the hope that it will be useful,
20 //** but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 //** GNU General Public License for more details.
23 //**
24 //**************************************************************************
25 //**
26 //** OpenGL driver for Allegro and AllegroGL
27 //**
28 //**************************************************************************
29
30 // HEADER FILES ------------------------------------------------------------
31
32 #include <allegro.h>
33 #include <alleggl.h>
34 #include "gl_local.h"
35
36 // MACROS ------------------------------------------------------------------
37
38 // TYPES -------------------------------------------------------------------
39
40 class VAllegroOpenGLDrawer : public VOpenGLDrawer
41 {
42 public:
43 void Init();
44 bool SetResolution(int, int, int, bool);
45 void* GetExtFuncPtr(const char*);
46 void Update();
47 void Shutdown();
48 };
49
50 // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
51
52 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
53
54 // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
55
56 // EXTERNAL DATA DECLARATIONS ----------------------------------------------
57
58 // PUBLIC DATA DEFINITIONS -------------------------------------------------
59
60 IMPLEMENT_DRAWER(VAllegroOpenGLDrawer, DRAWER_OpenGL, "OpenGL",
61 "Allegro OpenGL rasteriser device", "-opengl");
62
63 // PRIVATE DATA DEFINITIONS ------------------------------------------------
64
65 // CODE --------------------------------------------------------------------
66
67 //==========================================================================
68 //
69 // VAllegroOpenGLDrawer::Init
70 //
71 // Determine the hardware configuration
72 //
73 //==========================================================================
74
Init()75 void VAllegroOpenGLDrawer::Init()
76 {
77 }
78
79 //==========================================================================
80 //
81 // VAllegroOpenGLDrawer::SetResolution
82 //
83 // Set up the video mode
84 //
85 //==========================================================================
86
SetResolution(int AWidth,int AHeight,int ABPP,bool Windowed)87 bool VAllegroOpenGLDrawer::SetResolution(int AWidth, int AHeight, int ABPP,
88 bool Windowed)
89 {
90 guard(VAllegroOpenGLDrawer::SetResolution);
91 int Width = AWidth;
92 int Height = AHeight;
93 int BPP = ABPP;
94 bool default_mode = false;
95 if (!Width || !Height)
96 {
97 // Set defaults
98 Width = 640;
99 Height = 480;
100 BPP = 16;
101 default_mode = true;
102 }
103
104 if (BPP == 15)
105 {
106 BPP = 16;
107 }
108
109 if (BPP < 16)
110 {
111 // True-colour only
112 GCon->Log(NAME_Init, "Attempt to set a paletized video mode for OpenGL");
113 return false;
114 }
115
116 // Shut down current mode
117 Shutdown();
118
119 install_allegro_gl();
120
121 allegro_gl_clear_settings();
122 allegro_gl_set(AGL_COLOR_DEPTH, BPP);
123 allegro_gl_set(AGL_Z_DEPTH, 24);
124 allegro_gl_set(AGL_STENCIL_DEPTH, 8);
125 allegro_gl_set(AGL_WINDOWED, Windowed);
126 allegro_gl_set(AGL_DOUBLEBUFFER, 1);
127 allegro_gl_set(AGL_RENDERMETHOD, 1);
128 allegro_gl_set(AGL_SUGGEST, AGL_COLOR_DEPTH | AGL_DOUBLEBUFFER
129 | AGL_RENDERMETHOD | AGL_Z_DEPTH | AGL_WINDOWED);
130
131 HaveStencil = true;
132 set_color_depth(BPP);
133 if (set_gfx_mode(GFX_OPENGL, Width, Height, 0, 0))
134 {
135 // Try without stencil.
136 HaveStencil = false;
137 allegro_gl_set(AGL_Z_DEPTH, 8);
138 allegro_gl_set(AGL_STENCIL_DEPTH, 0);
139 if (set_gfx_mode(GFX_OPENGL, Width, Height, 0, 0))
140 {
141 allegro_message("Error setting OpenGL graphics mode:\n%s\n"
142 "Allegro GL error : %s\n",
143 allegro_error, allegro_gl_error);
144 return false;
145 }
146 }
147 if (HaveStencil)
148 {
149 GCon->Logf(NAME_Init, "Stencil buffer available");
150 }
151
152 ScreenWidth = Width;
153 ScreenHeight = Height;
154 ScreenBPP = BPP;
155
156 return true;
157 unguard;
158 }
159
160 //==========================================================================
161 //
162 // VAllegroOpenGLDrawer::GetExtFuncPtr
163 //
164 //==========================================================================
165
GetExtFuncPtr(const char * name)166 void* VAllegroOpenGLDrawer::GetExtFuncPtr(const char* name)
167 {
168 guard(VAllegroOpenGLDrawer::GetExtFuncPtr);
169 return allegro_gl_get_proc_address(name);
170 unguard;
171 }
172
173 //==========================================================================
174 //
175 // VAllegroOpenGLDrawer::Update
176 //
177 // Blit to the screen / Flip surfaces
178 //
179 //==========================================================================
180
Update()181 void VAllegroOpenGLDrawer::Update()
182 {
183 guard(VAllegroOpenGLDrawer::Update);
184 allegro_gl_flip();
185 unguard;
186 }
187
188 //==========================================================================
189 //
190 // VAllegroOpenGLDrawer::Shutdown
191 //
192 // Close the graphics
193 //
194 //==========================================================================
195
Shutdown()196 void VAllegroOpenGLDrawer::Shutdown()
197 {
198 guard(VAllegroOpenGLDrawer::Shutdown);
199 DeleteTextures();
200 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
201 remove_allegro_gl();
202 unguard;
203 }
204