DEADSOFTWARE

gl: draw gui controls
[d2df-sdl.git] / src / game / renders / opengl / r_render.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../../../shared/a_modes.inc}
16 unit r_render;
18 interface
20 uses
21 {$IFDEF ENABLE_MENU}
22 g_gui,
23 {$ENDIF}
24 g_base // TRectWH
25 ;
27 (* render startup *)
28 procedure r_Render_Initialize;
29 procedure r_Render_Finalize;
31 (* load globally used textures *)
32 procedure r_Render_Load;
33 procedure r_Render_Free;
35 (* load map specific textures *)
36 procedure r_Render_LoadTextures;
37 procedure r_Render_FreeTextures;
39 procedure r_Render_Update;
40 procedure r_Render_Draw;
42 procedure r_Render_Resize (w, h: Integer);
43 procedure r_Render_Apply;
45 function r_Render_WriteScreenShot (filename: String): Boolean;
47 {$IFDEF ENABLE_GIBS}
48 function r_Render_GetGibRect (m, id: Integer): TRectWH;
49 {$ENDIF}
51 {$IFDEF ENABLE_GFX}
52 procedure r_Render_QueueEffect (AnimType, X, Y: Integer);
53 {$ENDIF}
55 {$IFDEF ENABLE_TOUCH}
56 // touch screen button location and size
57 procedure r_Render_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean);
58 {$ENDIF}
60 {$IFDEF ENABLE_MENU}
61 procedure r_Render_GetControlSize (ctrl: TGUIControl; out w, h: Integer);
62 procedure r_Render_GetLogoSize (out w, h: Integer);
63 procedure r_Render_GetMaxFontSize (BigFont: Boolean; out w, h: Integer);
64 procedure r_Render_GetStringSize (BigFont: Boolean; str: String; out w, h: Integer);
65 {$ENDIF}
67 procedure r_Render_DrawLoading (force: Boolean); // !!! remove it
69 implementation
71 uses
72 {$IFDEF USE_GLES1}
73 GLES11,
74 {$ELSE}
75 GL, GLEXT,
76 {$ENDIF}
77 {$IFDEF ENABLE_MENU}
78 r_gui,
79 {$ENDIF}
80 {$IFDEF ENABLE_SYSTEM}
81 g_system,
82 {$ENDIF}
83 SysUtils, Classes, Math,
84 e_log, utils,
85 g_game, g_options, g_console, g_player, g_weapons, g_language,
86 g_net,
87 r_draw, r_textures, r_fonts, r_common, r_map
88 ;
90 type
91 TBasePoint = (
92 BP_LEFTUP, BP_UP, BP_RIGHTUP,
93 BP_LEFT, BP_CENTER, BP_RIGHT,
94 BP_LEFTDOWN, BP_DOWN, BP_RIGHTDOWN
95 );
97 var
98 menuBG: TGLTexture;
100 hud, hudbg: TGLTexture;
101 hudhp: array [Boolean] of TGLTexture;
102 hudap: TGLTexture;
103 hudwp: array [0..WP_LAST] of TGLTexture;
104 hudkey: array [0..2] of TGLTexture;
105 hudair: TGLTexture;
106 hudjet: TGLTexture;
108 procedure r_Render_LoadTextures;
109 begin
110 r_Map_LoadTextures;
111 end;
113 procedure r_Render_FreeTextures;
114 begin
115 r_Map_FreeTextures;
116 end;
118 procedure r_Render_Load;
119 const
120 WeapName: array [0..WP_LAST] of AnsiString = ('KASTET', 'SAW', 'PISTOL', 'SHOTGUN1', 'SHOTGUN2', 'MGUN', 'RLAUNCHER', 'PGUN', 'BFG', 'SPULEMET', 'FLAMETHROWER');
121 var
122 i: Integer;
123 begin
124 r_Common_Load;
125 menuBG := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/TITLE');
126 hud := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/HUD');
127 hudbg := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/HUDBG');
128 hudhp[false] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/MED2');
129 hudhp[true] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/BMED');
130 hudap := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/ARMORHUD');
131 for i := 0 to WP_LAST do
132 hudwp[i] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/' + WeapName[i]);
133 hudkey[0] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/KEYR');
134 hudkey[1] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/KEYG');
135 hudkey[2] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/KEYB');
136 hudair := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/AIRBAR');
137 hudjet := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/JETBAR');
138 r_Map_Load;
139 {$IFDEF ENABLE_MENU}
140 r_GUI_Load;
141 {$ENDIF}
142 end;
144 procedure r_Render_Free;
145 var i: Integer;
146 begin
147 {$IFDEF ENABLE_MENU}
148 r_GUI_Free;
149 {$ENDIF}
150 r_Map_Free;
151 hudjet.Free;
152 hudair.Free;
153 hudkey[0].Free;
154 hudkey[1].Free;
155 hudkey[2].Free;
156 for i := 0 to WP_LAST do
157 begin
158 if hudwp[i] <> nil then
159 hudwp[i].Free;
160 hudwp[i] := nil;
161 end;
162 hudap.Free;
163 hudhp[true].Free;
164 hudhp[false].Free;
165 hudbg.Free;
166 hud.Free;
167 menuBG.Free;
168 r_Common_Free;
169 end;
171 {$IFDEF ENABLE_SYSTEM}
172 function GetInfo (): TGLDisplayInfo;
173 var info: TGLDisplayInfo;
174 begin
175 info := Default(TGLDisplayInfo);
176 info.w := Max(1, gRC_Width);
177 info.h := Max(1, gRC_Height);
178 info.bpp := Max(1, gBPP);
179 info.fullscreen := gRC_FullScreen;
180 info.maximized := gRC_Maximized;
181 info.major := 1;
182 info.minor := 1;
183 info.profile := TGLProfile.Compat;
184 result := info;
185 end;
186 {$ENDIF}
188 procedure r_Render_Initialize;
189 begin
190 {$IFDEF ENABLE_SYSTEM}
191 if sys_SetDisplayModeGL(GetInfo()) = False then
192 raise Exception.Create('Failed to set videomode on startup.');
193 sys_EnableVSync(gVSync);
194 {$ENDIF}
195 r_Textures_Initialize;
196 r_Map_Initialize;
197 end;
199 procedure r_Render_Finalize;
200 begin
201 r_Map_Finalize;
202 r_Textures_Finalize;
203 end;
205 procedure r_Render_Update;
206 begin
207 r_Map_Update;
208 end;
210 procedure SetupMatrix;
211 begin
212 glViewport(0, 0, gScreenWidth, gScreenHeight);
213 glScissor(0, 0, gScreenWidth, gScreenHeight);
214 glMatrixMode(GL_PROJECTION);
215 glLoadIdentity;
216 glOrtho(0, gScreenWidth, gScreenHeight, 0, 0, 1);
217 glMatrixMode(GL_MODELVIEW);
218 glLoadIdentity;
219 end;
221 procedure r_Render_GetBasePoint (x, y, w, h: Integer; p: TBasePoint; out xx, yy: Integer);
222 begin
223 case p of
224 TBasePoint.BP_LEFTUP, TBasePoint.BP_LEFT, TBasePoint.BP_LEFTDOWN: xx := x;
225 TBasePoint.BP_UP, TBasePoint.BP_CENTER, TBasePoint.BP_DOWN: xx := x - w div 2;
226 TBasePoint.BP_RIGHTUP, TBasePoint.BP_RIGHT, TBasePoint.BP_RIGHTDOWN: xx := x - w;
227 end;
228 case p of
229 TBasePoint.BP_LEFTUP, TBasePoint.BP_UP, TBasePoint.BP_RIGHTUP: yy := y;
230 TBasePoint.BP_LEFT, TBasePoint.BP_CENTER, TBasePoint.BP_RIGHT: yy := y - h div 2;
231 TBasePoint.BP_LEFTDOWN, TBasePoint.BP_DOWN, TBasePoint.BP_RIGHTDOWN: yy := y - h;
232 end;
233 end;
235 procedure r_Render_DrawText (const text: AnsiString; x, y: Integer; r, g, b, a: Byte; f: TGLFont; p: TBasePoint);
236 var w, h: Integer;
237 begin
238 if p <> TBasePoint.BP_LEFTUP then
239 begin
240 r_Draw_GetTextSize(text, f, w, h);
241 r_Render_GetBasePoint(x, y, w, h, p, x, y);
242 end;
243 r_Draw_Text(text, x, y, r, g, b, a, f);
244 end;
246 procedure r_Render_DrawTexture (img: TGLTexture; x, y, w, h: Integer; p: TBasePoint);
247 begin
248 r_Render_GetBasePoint(x, y, w, h, p, x, y);
249 r_Draw_TextureRepeat(img, x, y, w, h, false, 255, 255, 255, 255, false);
250 end;
252 procedure r_Render_DrawHUD (x, y: Integer; p: TPlayer);
253 var t: TGLTexture; s: AnsiString;
254 begin
255 ASSERT(p <> nil);
257 // hud area is 196 x 240 pixels
258 r_Render_DrawTexture(hud, x, y, hud.width, hud.height, TBasePoint.BP_LEFTUP);
259 r_Render_DrawText(p.name, x + 98, y + 16, 255, 0, 0, 255, smallfont, TBasePoint.BP_CENTER);
261 t := hudhp[R_BERSERK in p.FRulez];
262 r_Render_DrawTexture(t, x + 51, y + 61, t.width, t.height, TBasePoint.BP_CENTER);
263 r_Render_DrawTexture(hudap, x + 50, y + 85, hudap.width, hudap.height, TBasePoint.BP_CENTER);
265 r_Render_DrawText(IntToStr(MAX(0, p.health)), x + 174, y + 56, 255, 0, 0, 255, menufont, TBasePoint.BP_RIGHT);
266 r_Render_DrawText(IntToStr(MAX(0, p.armor)), x + 174, y + 84, 255, 0, 0, 255, menufont, TBasePoint.BP_RIGHT);
268 case p.CurrWeap of
269 WEAPON_KASTET, WEAPON_SAW: s := '--';
270 else s := IntToStr(p.GetAmmoByWeapon(p.CurrWeap));
271 end;
272 r_Render_DrawText(s, x + 174, y + 174, 255, 0, 0, 255, menufont, TBasePoint.BP_RIGHT);
274 if p.CurrWeap <= WP_LAST then
275 begin
276 t := hudwp[p.CurrWeap];
277 r_Render_DrawTexture(t, x + 18, y + 160, t.width, t.height, TBasePoint.BP_LEFTUP);
278 end;
280 if R_KEY_RED in p.FRulez then
281 r_Render_DrawTexture(hudkey[0], x + 76, y + 214, 16, 16, TBasePoint.BP_LEFTUP);
282 if R_KEY_GREEN in p.FRulez then
283 r_Render_DrawTexture(hudkey[1], x + 93, y + 214, 16, 16, TBasePoint.BP_LEFTUP);
284 if R_KEY_BLUE in p.FRulez then
285 r_Render_DrawTexture(hudkey[2], x + 110, y + 214, 16, 16, TBasePoint.BP_LEFTUP);
287 if p.JetFuel > 0 then
288 begin
289 r_Render_DrawTexture(hudair, x, y + 116, hudair.width, hudair.height, TBasePoint.BP_LEFTUP);
290 if p.air > 0 then
291 r_Draw_FillRect(x + 14, y + 116 + 4, x + 14 + 168 * p.air div AIR_MAX, y + 116 + 4 + 4, 0, 0, 196, 255);
292 r_Render_DrawTexture(hudjet, x, y + 126, hudjet.width, hudjet.height, TBasePoint.BP_LEFTUP);
293 r_Draw_FillRect(x + 14, y + 126 + 4, x + 14 + 168 * p.JetFuel div JET_MAX, y + 126 + 4 + 4, 208, 0, 0, 255);
294 end
295 else
296 begin
297 r_Render_DrawTexture(hudair, x, y + 124, hudair.width, hudair.height, TBasePoint.BP_LEFTUP);
298 if p.air > 0 then
299 r_Draw_FillRect(x + 14, y + 124 + 4, x + 14 + 168 * p.air div AIR_MAX, y + 124 + 4 + 4, 0, 0, 196, 255);
300 end;
301 end;
303 procedure r_Render_DrawHUDArea (x, y, w, h: Integer; p: TPlayer);
304 var s: AnsiString;
305 begin
306 r_Render_DrawTexture(hudbg, x, y, w, h, TBasePoint.BP_LEFTUP);
308 if p <> nil then
309 r_Render_DrawHUD(x + w - 196 + 2, y, p);
311 if gShowPing and g_Game_IsClient then
312 begin
313 s := _lc[I_GAME_PING_HUD] + IntToStr(NetPeer.lastRoundTripTime) + _lc[I_NET_SLIST_PING_MS];
314 r_Render_DrawText(s, x + 4, y + 242, 255, 255, 255, 255, stdfont, TBasePoint.BP_LEFTUP);
315 end;
317 if p.Spectator then
318 begin
319 r_Render_DrawText(_lc[I_PLAYER_SPECT], x + 4, y + 242, 255, 255, 255, 255, stdfont, TBasePoint.BP_LEFTUP);
320 r_Render_DrawText(_lc[I_PLAYER_SPECT2], x + 4, y + 258, 255, 255, 255, 255, stdfont, TBasePoint.BP_LEFTUP);
321 r_Render_DrawText(_lc[I_PLAYER_SPECT1], x + 4, y + 274, 255, 255, 255, 255, stdfont, TBasePoint.BP_LEFTUP);
322 if p.NoRespawn then
323 r_Render_DrawText(_lc[I_PLAYER_SPECT1S], x + 4, y + 290, 255, 255, 255, 255, stdfont, TBasePoint.BP_LEFTUP);
324 end;
325 end;
327 procedure r_Render_DrawView (x, y, w, h: Integer; p: TPlayer);
328 begin
329 if p <> nil then
330 r_Map_Draw(x, y, w, h, p.obj.x + PLAYER_RECT_CX, p.obj.y + PLAYER_RECT_CY, p)
331 else
332 r_Map_Draw(x, y, w, h, 0, 0, nil);
334 // TODO draw stats
336 if p <> nil then
337 begin
338 if p.Spectator and p.NoRespawn then
339 r_Render_DrawText(_lc[I_PLAYER_SPECT4], x div 2 + w div 2, y div 2 + h div 2, 255, 255, 255, 255, stdfont, TBasePoint.BP_CENTER);
340 end;
341 end;
343 procedure r_Render_DrawPlayerView (x, y, w, h: Integer; p: TPlayer);
344 begin
345 r_Render_DrawView(x, y, w - 196, h, p);
346 r_Render_DrawHUDArea(x + w - 196, y, 196, h, p);
347 end;
349 procedure r_Render_Draw;
350 begin
351 if gExit = EXIT_QUIT then
352 exit;
354 SetupMatrix;
356 glClearColor(0.0, 0.0, 0.0, 0.0);
357 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
359 glColor4ub(255, 255, 255, 255);
361 //e_LogWritefln('r_render_draw: %sx%s', [gScreenWidth, gScreenHeight]);
363 if gGameOn or (gState = STATE_FOLD) then
364 begin
365 // TODO setup player view
366 // TODO setup sectator mode
367 // TODO setup player hear point
368 // TODO setup player view siz
370 // TODO draw player view + setup screen coords
371 r_Render_DrawPlayerView(0, 0, gScreenWidth, gScreenHeight, gPlayer1);
373 // TODO draw holmes inspector
375 // TODO draw messages
376 // TODO draw stats (?)
377 // TODO draw spectator hud
378 end;
380 if gPauseMain and gGameOn {$IFDEF ENABLE_MENU}and (g_ActiveWindow = nil){$ENDIF} then
381 begin
382 // TODO draw pause screen
383 end;
385 if not gGameOn then
386 begin
387 case gState of
388 STATE_MENU: ; // TODO draw menu bg
389 STATE_FOLD: ;
390 STATE_INTERCUSTOM: ;
391 STATE_INTERSINGLE: ;
392 STATE_ENDPIC: ;
393 STATE_SLIST: ;
394 end;
395 end;
397 {$IFDEF ENABLE_MENU}
398 if g_ActiveWindow <> nil then
399 begin
400 if gGameOn then
401 r_Draw_FillRect(0, 0, gScreenWidth - 1, gScreenHeight - 1, 0, 0, 0, 105);
402 r_GUI_Draw_Window(g_ActiveWindow);
403 end;
404 {$ENDIF}
406 // TODO draw console
408 // TODO draw holmes interface
410 glFinish();
411 glFlush();
412 sys_Repaint;
413 end;
415 procedure r_Render_Resize (w, h: Integer);
416 begin
417 gWinSizeX := w;
418 gWinSizeY := h;
419 gRC_Width := w;
420 gRC_Height := h;
421 gScreenWidth := w;
422 gScreenHeight := h;
423 end;
425 procedure r_Render_Apply;
426 begin
427 {$IFDEF ENABLE_SYSTEM}
428 if sys_SetDisplayModeGL(GetInfo()) then
429 e_LogWriteln('resolution changed')
430 else
431 e_LogWriteln('resolution not changed');
432 sys_EnableVSync(gVSync)
433 {$ENDIF}
434 end;
436 function r_Render_WriteScreenShot (filename: String): Boolean;
437 begin
438 Result := False;
439 end;
441 {$IFDEF ENABLE_GIBS}
442 function r_Render_GetGibRect (m, id: Integer): TRectWH;
443 begin
444 result := r_Map_GetGibSize(m, id);
445 end;
446 {$ENDIF}
448 {$IFDEF ENABLE_GFX}
449 procedure r_Render_QueueEffect (AnimType, X, Y: Integer);
450 begin
451 r_Map_NewGFX(AnimType, X, Y);
452 end;
453 {$ENDIF}
455 {$IFDEF ENABLE_TOUCH}
456 procedure r_Render_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean);
457 begin
458 founded := False;
459 end;
460 {$ENDIF}
462 {$IFDEF ENABLE_MENU}
463 procedure r_Render_GetControlSize (ctrl: TGUIControl; out w, h: Integer);
464 begin
465 r_GUI_GetSize(ctrl, w, h);
466 end;
468 procedure r_Render_GetLogoSize (out w, h: Integer);
469 begin
470 r_GUI_GetLogoSize(w, h);
471 end;
473 procedure r_Render_GetMaxFontSize (BigFont: Boolean; out w, h: Integer);
474 begin
475 r_GUI_GetMaxFontSize(BigFont, w, h);
476 end;
478 procedure r_Render_GetStringSize (BigFont: Boolean; str: String; out w, h: Integer);
479 begin
480 r_GUI_GetStringSize(BigFont, str, w, h);
481 end;
482 {$ENDIF}
484 procedure r_Render_DrawLoading (force: Boolean);
485 begin
486 end;
488 end.