DEADSOFTWARE

135141e69ca752e333f82730d8b9a251877df077
[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_console, 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_Console_Load;
139 r_Map_Load;
140 {$IFDEF ENABLE_MENU}
141 r_GUI_Load;
142 {$ENDIF}
143 end;
145 procedure r_Render_Free;
146 var i: Integer;
147 begin
148 {$IFDEF ENABLE_MENU}
149 r_GUI_Free;
150 {$ENDIF}
151 r_Map_Free;
152 r_Console_Free;
153 hudjet.Free;
154 hudair.Free;
155 hudkey[0].Free;
156 hudkey[1].Free;
157 hudkey[2].Free;
158 for i := 0 to WP_LAST do
159 begin
160 if hudwp[i] <> nil then
161 hudwp[i].Free;
162 hudwp[i] := nil;
163 end;
164 hudap.Free;
165 hudhp[true].Free;
166 hudhp[false].Free;
167 hudbg.Free;
168 hud.Free;
169 menuBG.Free;
170 r_Common_Free;
171 end;
173 {$IFDEF ENABLE_SYSTEM}
174 function GetInfo (): TGLDisplayInfo;
175 var info: TGLDisplayInfo;
176 begin
177 info := Default(TGLDisplayInfo);
178 info.w := Max(1, gRC_Width);
179 info.h := Max(1, gRC_Height);
180 info.bpp := Max(1, gBPP);
181 info.fullscreen := gRC_FullScreen;
182 info.maximized := gRC_Maximized;
183 info.major := 1;
184 info.minor := 1;
185 info.profile := TGLProfile.Compat;
186 result := info;
187 end;
188 {$ENDIF}
190 procedure r_Render_Initialize;
191 begin
192 {$IFDEF ENABLE_SYSTEM}
193 if sys_SetDisplayModeGL(GetInfo()) = False then
194 raise Exception.Create('Failed to set videomode on startup.');
195 sys_EnableVSync(gVSync);
196 {$ENDIF}
197 r_Textures_Initialize;
198 r_Console_Initialize;
199 r_Map_Initialize;
200 end;
202 procedure r_Render_Finalize;
203 begin
204 r_Map_Finalize;
205 r_Console_Finalize;
206 r_Textures_Finalize;
207 end;
209 procedure r_Render_Update;
210 begin
211 r_Console_Update;
212 r_Map_Update;
213 end;
215 procedure SetupMatrix;
216 begin
217 glViewport(0, 0, gScreenWidth, gScreenHeight);
218 glScissor(0, 0, gScreenWidth, gScreenHeight);
219 glMatrixMode(GL_PROJECTION);
220 glLoadIdentity;
221 glOrtho(0, gScreenWidth, gScreenHeight, 0, 0, 1);
222 glMatrixMode(GL_MODELVIEW);
223 glLoadIdentity;
224 end;
226 procedure r_Render_GetBasePoint (x, y, w, h: Integer; p: TBasePoint; out xx, yy: Integer);
227 begin
228 case p of
229 TBasePoint.BP_LEFTUP, TBasePoint.BP_LEFT, TBasePoint.BP_LEFTDOWN: xx := x;
230 TBasePoint.BP_UP, TBasePoint.BP_CENTER, TBasePoint.BP_DOWN: xx := x - w div 2;
231 TBasePoint.BP_RIGHTUP, TBasePoint.BP_RIGHT, TBasePoint.BP_RIGHTDOWN: xx := x - w;
232 end;
233 case p of
234 TBasePoint.BP_LEFTUP, TBasePoint.BP_UP, TBasePoint.BP_RIGHTUP: yy := y;
235 TBasePoint.BP_LEFT, TBasePoint.BP_CENTER, TBasePoint.BP_RIGHT: yy := y - h div 2;
236 TBasePoint.BP_LEFTDOWN, TBasePoint.BP_DOWN, TBasePoint.BP_RIGHTDOWN: yy := y - h;
237 end;
238 end;
240 procedure r_Render_DrawText (const text: AnsiString; x, y: Integer; r, g, b, a: Byte; f: TGLFont; p: TBasePoint);
241 var w, h: Integer;
242 begin
243 if p <> TBasePoint.BP_LEFTUP then
244 begin
245 r_Draw_GetTextSize(text, f, w, h);
246 r_Render_GetBasePoint(x, y, w, h, p, x, y);
247 end;
248 r_Draw_Text(text, x, y, r, g, b, a, f);
249 end;
251 procedure r_Render_DrawTexture (img: TGLTexture; x, y, w, h: Integer; p: TBasePoint);
252 begin
253 r_Render_GetBasePoint(x, y, w, h, p, x, y);
254 r_Draw_TextureRepeat(img, x, y, w, h, false, 255, 255, 255, 255, false);
255 end;
257 procedure r_Render_DrawHUD (x, y: Integer; p: TPlayer);
258 var t: TGLTexture; s: AnsiString;
259 begin
260 ASSERT(p <> nil);
262 // hud area is 196 x 240 pixels
263 r_Render_DrawTexture(hud, x, y, hud.width, hud.height, TBasePoint.BP_LEFTUP);
264 r_Render_DrawText(p.name, x + 98, y + 16, 255, 0, 0, 255, smallfont, TBasePoint.BP_CENTER);
266 t := hudhp[R_BERSERK in p.FRulez];
267 r_Render_DrawTexture(t, x + 51, y + 61, t.width, t.height, TBasePoint.BP_CENTER);
268 r_Render_DrawTexture(hudap, x + 50, y + 85, hudap.width, hudap.height, TBasePoint.BP_CENTER);
270 r_Render_DrawText(IntToStr(MAX(0, p.health)), x + 174, y + 56, 255, 0, 0, 255, menufont, TBasePoint.BP_RIGHT);
271 r_Render_DrawText(IntToStr(MAX(0, p.armor)), x + 174, y + 84, 255, 0, 0, 255, menufont, TBasePoint.BP_RIGHT);
273 case p.CurrWeap of
274 WEAPON_KASTET, WEAPON_SAW: s := '--';
275 else s := IntToStr(p.GetAmmoByWeapon(p.CurrWeap));
276 end;
277 r_Render_DrawText(s, x + 174, y + 174, 255, 0, 0, 255, menufont, TBasePoint.BP_RIGHT);
279 if p.CurrWeap <= WP_LAST then
280 begin
281 t := hudwp[p.CurrWeap];
282 r_Render_DrawTexture(t, x + 18, y + 160, t.width, t.height, TBasePoint.BP_LEFTUP);
283 end;
285 if R_KEY_RED in p.FRulez then
286 r_Render_DrawTexture(hudkey[0], x + 76, y + 214, 16, 16, TBasePoint.BP_LEFTUP);
287 if R_KEY_GREEN in p.FRulez then
288 r_Render_DrawTexture(hudkey[1], x + 93, y + 214, 16, 16, TBasePoint.BP_LEFTUP);
289 if R_KEY_BLUE in p.FRulez then
290 r_Render_DrawTexture(hudkey[2], x + 110, y + 214, 16, 16, TBasePoint.BP_LEFTUP);
292 if p.JetFuel > 0 then
293 begin
294 r_Render_DrawTexture(hudair, x, y + 116, hudair.width, hudair.height, TBasePoint.BP_LEFTUP);
295 if p.air > 0 then
296 r_Draw_FillRect(x + 14, y + 116 + 4, x + 14 + 168 * p.air div AIR_MAX, y + 116 + 4 + 4, 0, 0, 196, 255);
297 r_Render_DrawTexture(hudjet, x, y + 126, hudjet.width, hudjet.height, TBasePoint.BP_LEFTUP);
298 r_Draw_FillRect(x + 14, y + 126 + 4, x + 14 + 168 * p.JetFuel div JET_MAX, y + 126 + 4 + 4, 208, 0, 0, 255);
299 end
300 else
301 begin
302 r_Render_DrawTexture(hudair, x, y + 124, hudair.width, hudair.height, TBasePoint.BP_LEFTUP);
303 if p.air > 0 then
304 r_Draw_FillRect(x + 14, y + 124 + 4, x + 14 + 168 * p.air div AIR_MAX, y + 124 + 4 + 4, 0, 0, 196, 255);
305 end;
306 end;
308 procedure r_Render_DrawHUDArea (x, y, w, h: Integer; p: TPlayer);
309 var s: AnsiString;
310 begin
311 r_Render_DrawTexture(hudbg, x, y, w, h, TBasePoint.BP_LEFTUP);
313 if p <> nil then
314 r_Render_DrawHUD(x + w - 196 + 2, y, p);
316 if gShowPing and g_Game_IsClient then
317 begin
318 s := _lc[I_GAME_PING_HUD] + IntToStr(NetPeer.lastRoundTripTime) + _lc[I_NET_SLIST_PING_MS];
319 r_Render_DrawText(s, x + 4, y + 242, 255, 255, 255, 255, stdfont, TBasePoint.BP_LEFTUP);
320 end;
322 if p.Spectator then
323 begin
324 r_Render_DrawText(_lc[I_PLAYER_SPECT], x + 4, y + 242, 255, 255, 255, 255, stdfont, TBasePoint.BP_LEFTUP);
325 r_Render_DrawText(_lc[I_PLAYER_SPECT2], x + 4, y + 258, 255, 255, 255, 255, stdfont, TBasePoint.BP_LEFTUP);
326 r_Render_DrawText(_lc[I_PLAYER_SPECT1], x + 4, y + 274, 255, 255, 255, 255, stdfont, TBasePoint.BP_LEFTUP);
327 if p.NoRespawn then
328 r_Render_DrawText(_lc[I_PLAYER_SPECT1S], x + 4, y + 290, 255, 255, 255, 255, stdfont, TBasePoint.BP_LEFTUP);
329 end;
330 end;
332 procedure r_Render_DrawView (x, y, w, h: Integer; p: TPlayer);
333 begin
334 if p <> nil then
335 r_Map_Draw(x, y, w, h, p.obj.x + PLAYER_RECT_CX, p.obj.y + PLAYER_RECT_CY, p)
336 else
337 r_Map_Draw(x, y, w, h, 0, 0, nil);
339 // TODO draw stats
341 if p <> nil then
342 begin
343 if p.Spectator and p.NoRespawn then
344 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);
345 end;
346 end;
348 procedure r_Render_DrawPlayerView (x, y, w, h: Integer; p: TPlayer);
349 begin
350 r_Render_DrawView(x, y, w - 196, h, p);
351 r_Render_DrawHUDArea(x + w - 196, y, 196, h, p);
352 end;
354 procedure r_Render_Draw;
355 begin
356 if gExit = EXIT_QUIT then
357 exit;
359 SetupMatrix;
361 glClearColor(0.0, 0.0, 0.0, 0.0);
362 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
364 glColor4ub(255, 255, 255, 255);
366 //e_LogWritefln('r_render_draw: %sx%s', [gScreenWidth, gScreenHeight]);
368 if gGameOn or (gState = STATE_FOLD) then
369 begin
370 // TODO setup player view
371 // TODO setup sectator mode
372 // TODO setup player hear point
373 // TODO setup player view siz
375 // TODO draw player view + setup screen coords
376 r_Render_DrawPlayerView(0, 0, gScreenWidth, gScreenHeight, gPlayer1);
378 // TODO draw holmes inspector
380 // TODO draw messages
381 // TODO draw stats (?)
382 // TODO draw spectator hud
383 end;
385 if gPauseMain and gGameOn {$IFDEF ENABLE_MENU}and (g_ActiveWindow = nil){$ENDIF} then
386 begin
387 // TODO draw pause screen
388 end;
390 if not gGameOn then
391 begin
392 case gState of
393 STATE_MENU: ; // TODO draw menu bg
394 STATE_FOLD: ;
395 STATE_INTERCUSTOM: ;
396 STATE_INTERSINGLE: ;
397 STATE_ENDPIC: ;
398 STATE_SLIST: ;
399 end;
400 end;
402 {$IFDEF ENABLE_MENU}
403 if g_ActiveWindow <> nil then
404 begin
405 if gGameOn then
406 r_Draw_FillRect(0, 0, gScreenWidth - 1, gScreenHeight - 1, 0, 0, 0, 105);
407 r_GUI_Draw_Window(g_ActiveWindow);
408 end;
409 {$ENDIF}
411 r_Console_Draw(false);
413 // TODO draw holmes interface
415 glFinish();
416 glFlush();
417 sys_Repaint;
418 end;
420 procedure r_Render_Resize (w, h: Integer);
421 begin
422 gWinSizeX := w;
423 gWinSizeY := h;
424 gRC_Width := w;
425 gRC_Height := h;
426 gScreenWidth := w;
427 gScreenHeight := h;
428 end;
430 procedure r_Render_Apply;
431 begin
432 {$IFDEF ENABLE_SYSTEM}
433 if sys_SetDisplayModeGL(GetInfo()) then
434 e_LogWriteln('resolution changed')
435 else
436 e_LogWriteln('resolution not changed');
437 sys_EnableVSync(gVSync)
438 {$ENDIF}
439 end;
441 function r_Render_WriteScreenShot (filename: String): Boolean;
442 begin
443 Result := False;
444 end;
446 {$IFDEF ENABLE_GIBS}
447 function r_Render_GetGibRect (m, id: Integer): TRectWH;
448 begin
449 result := r_Map_GetGibSize(m, id);
450 end;
451 {$ENDIF}
453 {$IFDEF ENABLE_GFX}
454 procedure r_Render_QueueEffect (AnimType, X, Y: Integer);
455 begin
456 r_Map_NewGFX(AnimType, X, Y);
457 end;
458 {$ENDIF}
460 {$IFDEF ENABLE_TOUCH}
461 procedure r_Render_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean);
462 begin
463 founded := False;
464 end;
465 {$ENDIF}
467 {$IFDEF ENABLE_MENU}
468 procedure r_Render_GetControlSize (ctrl: TGUIControl; out w, h: Integer);
469 begin
470 r_GUI_GetSize(ctrl, w, h);
471 end;
473 procedure r_Render_GetLogoSize (out w, h: Integer);
474 begin
475 r_GUI_GetLogoSize(w, h);
476 end;
478 procedure r_Render_GetMaxFontSize (BigFont: Boolean; out w, h: Integer);
479 begin
480 r_GUI_GetMaxFontSize(BigFont, w, h);
481 end;
483 procedure r_Render_GetStringSize (BigFont: Boolean; str: String; out w, h: Integer);
484 begin
485 r_GUI_GetStringSize(BigFont, str, w, h);
486 end;
487 {$ENDIF}
489 procedure r_Render_DrawLoading (force: Boolean);
490 begin
491 // TODO draw loading screen
492 end;
494 end.