DEADSOFTWARE

gl: draw hud
[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_SYSTEM}
78 g_system,
79 {$ENDIF}
80 SysUtils, Classes, Math,
81 e_log, utils,
82 g_game, g_options, g_console, g_player, g_weapons, g_language,
83 g_net,
84 r_draw, r_textures, r_fonts, r_map
85 ;
87 var
88 menuBG: TGLTexture;
90 stdfont: TGLFont;
91 smallfont: TGLFont;
92 menufont: TGLFont;
94 hud, hudbg: TGLTexture;
95 hudhp: array [Boolean] of TGLTexture;
96 hudap: TGLTexture;
97 hudwp: array [0..WP_LAST] of TGLTexture;
98 hudkey: array [0..2] of TGLTexture;
99 hudair: TGLTexture;
100 hudjet: TGLTexture;
102 procedure r_Render_LoadTextures;
103 begin
104 r_Map_LoadTextures;
105 end;
107 procedure r_Render_FreeTextures;
108 begin
109 r_Map_FreeTextures;
110 end;
112 function r_Render_LoadFont (const name: AnsiString): TGLFont;
113 var info: TFontInfo; skiphack: Integer;
114 begin
115 result := nil;
116 if name = 'STD' then skiphack := 144 else skiphack := 0;
117 if r_Font_LoadInfoFromFile(GameWad + ':FONTS/' + name + 'TXT', info) then
118 result := r_Textures_LoadFontFromFile(GameWad + ':FONTS/' + name + 'FONT', info, skiphack, true);
119 if result = nil then
120 e_logwritefln('failed to load font %s', [name]);
121 end;
123 procedure r_Render_Load;
124 const
125 WeapName: array [0..WP_LAST] of AnsiString = ('KASTET', 'SAW', 'PISTOL', 'SHOTGUN1', 'SHOTGUN2', 'MGUN', 'RLAUNCHER', 'PGUN', 'BFG', 'SPULEMET', 'FLAMETHROWER');
126 var
127 i: Integer;
128 begin
129 menuBG := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/TITLE');
130 stdfont := r_Render_LoadFont('STD');
131 smallfont := r_Render_LoadFont('SMALL');
132 menufont := r_Render_LoadFont('MENU');
133 hud := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/HUD');
134 hudbg := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/HUDBG');
135 hudhp[false] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/MED2');
136 hudhp[true] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/BMED');
137 hudap := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/ARMORHUD');
138 for i := 0 to WP_LAST do
139 hudwp[i] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/' + WeapName[i]);
140 hudkey[0] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/KEYR');
141 hudkey[1] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/KEYG');
142 hudkey[2] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/KEYB');
143 hudair := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/AIRBAR');
144 hudjet := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/JETBAR');
145 r_Map_Load;
146 end;
148 procedure r_Render_Free;
149 var i: Integer;
150 begin
151 r_Map_Free;
152 hudjet.Free;
153 hudair.Free;
154 hudkey[0].Free;
155 hudkey[1].Free;
156 hudkey[2].Free;
157 for i := 0 to WP_LAST do
158 begin
159 if hudwp[i] <> nil then
160 hudwp[i].Free;
161 hudwp[i] := nil;
162 end;
163 hudap.Free;
164 hudhp[true].Free;
165 hudhp[false].Free;
166 hudbg.Free;
167 hud.Free;
168 menufont.Free;
169 smallfont.Free;
170 stdfont.Free;
171 menuBG.Free;
172 end;
174 {$IFDEF ENABLE_SYSTEM}
175 function GetInfo (): TGLDisplayInfo;
176 var info: TGLDisplayInfo;
177 begin
178 info := Default(TGLDisplayInfo);
179 info.w := Max(1, gRC_Width);
180 info.h := Max(1, gRC_Height);
181 info.bpp := Max(1, gBPP);
182 info.fullscreen := gRC_FullScreen;
183 info.maximized := gRC_Maximized;
184 info.major := 1;
185 info.minor := 1;
186 info.profile := TGLProfile.Compat;
187 result := info;
188 end;
189 {$ENDIF}
191 procedure r_Render_Initialize;
192 begin
193 {$IFDEF ENABLE_SYSTEM}
194 if sys_SetDisplayModeGL(GetInfo()) = False then
195 raise Exception.Create('Failed to set videomode on startup.');
196 sys_EnableVSync(gVSync);
197 {$ENDIF}
198 r_Textures_Initialize;
199 r_Map_Initialize;
200 end;
202 procedure r_Render_Finalize;
203 begin
204 r_Map_Finalize;
205 r_Textures_Finalize;
206 end;
208 procedure r_Render_Update;
209 begin
210 r_Map_Update;
211 end;
213 procedure SetupMatrix;
214 begin
215 glViewport(0, 0, gScreenWidth, gScreenHeight);
216 glScissor(0, 0, gScreenWidth, gScreenHeight);
217 glMatrixMode(GL_PROJECTION);
218 glLoadIdentity;
219 glOrtho(0, gScreenWidth, gScreenHeight, 0, 0, 1);
220 glMatrixMode(GL_MODELVIEW);
221 glLoadIdentity;
222 end;
224 procedure r_Render_DrawHUD (x, y: Integer; p: TPlayer);
225 var t: TGLTexture; s: AnsiString;
226 begin
227 ASSERT(p <> nil);
229 r_Draw_TextureRepeat(hud, x, y, 196, 240, false, 255, 255, 255, 255, false);
230 r_Draw_Text(p.name, x + 16, y + 8, 255, 0, 0, 255, smallfont); // TODO draw at center
232 t := hudhp[R_BERSERK in p.FRulez];
233 r_Draw_Texture(t, x + 35, y + 45, t.width, t.height, false, 255, 255, 255, 255, false);
234 r_Draw_Texture(hudap, x + 34, y + 77, hudap.width, hudap.height, false, 255, 255, 255, 255, false);
236 r_Draw_Text(IntToStr(MAX(0, p.health)), x + 105{178}, y + 40, 255, 0, 0, 255, menufont); // TODO draw at center
237 r_Draw_Text(IntToStr(MAX(0, p.armor)), x + 105{178}, y + 68, 255, 0, 0, 255, menufont); // TODO draw at center
239 case p.CurrWeap of
240 WEAPON_KASTET, WEAPON_SAW: s := '--';
241 else s := IntToStr(p.GetAmmoByWeapon(p.CurrWeap));
242 end;
243 r_Draw_Text(s, x + 105, y + 158, 255, 0, 0, 255, menufont); // TODO draw at center
245 if p.CurrWeap <= WP_LAST then
246 begin
247 t := hudwp[p.CurrWeap];
248 r_Draw_Texture(t, x + 18, y + 160, t.width, t.height, false, 255, 255, 255, 255, false);
249 end;
251 if R_KEY_RED in p.FRulez then
252 r_Draw_Texture(hudkey[0], x + 76, y + 214, 16, 16, false, 255, 255, 255, 255, false);
253 if R_KEY_GREEN in p.FRulez then
254 r_Draw_Texture(hudkey[1], x + 93, y + 214, 16, 16, false, 255, 255, 255, 255, false);
255 if R_KEY_BLUE in p.FRulez then
256 r_Draw_Texture(hudkey[2], x + 110, y + 214, 16, 16, false, 255, 255, 255, 255, false);
258 if p.JetFuel > 0 then
259 begin
260 r_Draw_Texture(hudair, x, y + 116, hudair.width, hudair.height, false, 255, 255, 255, 255, false);
261 if p.air > 0 then
262 r_Draw_FillRect(x + 14, y + 116 + 4, x + 14 + 168 * p.air div AIR_MAX, y + 116 + 4 + 4, 0, 0, 196, 255);
263 r_Draw_Texture(hudjet, x, y + 126, hudjet.width, hudjet.height, false, 255, 255, 255, 255, false);
264 r_Draw_FillRect(x + 14, y + 126 + 4, x + 14 + 168 * p.JetFuel div JET_MAX, y + 126 + 4 + 4, 208, 0, 0, 255);
265 end
266 else
267 begin
268 r_Draw_Texture(hudair, x, y + 124, hudair.width, hudair.height, false, 255, 255, 255, 255, false);
269 if p.air > 0 then
270 r_Draw_FillRect(x + 14, y + 124 + 4, x + 14 + 168 * p.air div AIR_MAX, y + 124 + 4 + 4, 0, 0, 196, 255);
271 end;
272 end;
274 procedure r_Render_DrawHUDArea (x, y, w, h: Integer; p: TPlayer);
275 var s: AnsiString;
276 begin
277 r_Draw_TextureRepeat(hudbg, x, y, w, h, false, 255, 255, 255, 255, false);
279 if p <> nil then
280 r_Render_DrawHUD(x + w - 196 + 2, y, p);
282 if gShowPing and g_Game_IsClient then
283 begin
284 s := _lc[I_GAME_PING_HUD] + IntToStr(NetPeer.lastRoundTripTime) + _lc[I_NET_SLIST_PING_MS];
285 r_Draw_Text(s, x + 4, y + 242, 255, 255, 255, 255, stdfont);
286 end;
288 if p.Spectator then
289 begin
290 r_Draw_Text(_lc[I_PLAYER_SPECT], x + 4, y + 242, 255, 255, 255, 255, stdfont);
291 r_Draw_Text(_lc[I_PLAYER_SPECT2], x + 4, y + 258, 255, 255, 255, 255, stdfont);
292 r_Draw_Text(_lc[I_PLAYER_SPECT1], x + 4, y + 274, 255, 255, 255, 255, stdfont);
293 if p.NoRespawn then
294 r_Draw_Text(_lc[I_PLAYER_SPECT1S], x + 4, y + 290, 255, 255, 255, 255, stdfont);
295 end;
296 end;
298 procedure r_Render_DrawView (x, y, w, h: Integer; p: TPlayer);
299 begin
300 if p <> nil then
301 r_Map_Draw(x, y, w, h, p.obj.x + PLAYER_RECT_CX, p.obj.y + PLAYER_RECT_CY, p)
302 else
303 r_Map_Draw(x, y, w, h, 0, 0, nil);
305 // TODO draw stats
307 if p <> nil then
308 begin
309 if p.Spectator and p.NoRespawn then
310 begin
311 // TODO draw at center
312 r_Draw_Text(_lc[I_PLAYER_SPECT4], x div 2 + w div 2, y div 2 + h div 2, 255, 255, 255, 255, stdfont);
313 end;
314 end;
315 end;
317 procedure r_Render_DrawPlayerView (x, y, w, h: Integer; p: TPlayer);
318 begin
319 r_Render_DrawView(x, y, w - 196, h, p);
320 r_Render_DrawHUDArea(x + w - 196, y, 196, h, p);
321 end;
323 procedure r_Render_Draw;
324 begin
325 if gExit = EXIT_QUIT then
326 exit;
328 SetupMatrix;
330 glClearColor(0.0, 0.0, 0.0, 0.0);
331 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
333 glColor4ub(255, 255, 255, 255);
335 //e_LogWritefln('r_render_draw: %sx%s', [gScreenWidth, gScreenHeight]);
337 if gGameOn or (gState = STATE_FOLD) then
338 begin
339 // TODO setup player view
340 // TODO setup sectator mode
341 // TODO setup player hear point
342 // TODO setup player view siz
344 // TODO draw player view + setup screen coords
345 r_Render_DrawPlayerView(0, 0, gScreenWidth, gScreenHeight, gPlayer1);
347 // TODO draw holmes inspector
349 // TODO draw messages
350 // TODO draw stats (?)
351 // TODO draw spectator hud
352 end;
354 if gPauseMain and gGameOn {$IFDEF ENABLE_MENU}and (g_ActiveWindow = nil){$ENDIF} then
355 begin
356 // TODO draw pause screen
357 end;
359 if not gGameOn then
360 begin
361 case gState of
362 STATE_MENU: ; // TODO draw menu bg
363 STATE_FOLD: ;
364 STATE_INTERCUSTOM: ;
365 STATE_INTERSINGLE: ;
366 STATE_ENDPIC: ;
367 STATE_SLIST: ;
368 end;
369 end;
371 {$IFDEF ENABLE_MENU}
372 if g_ActiveWindow <> nil then
373 begin
374 // TODO draw menu widgets
375 end;
376 {$ENDIF}
378 // TODO draw console
380 // TODO draw holmes interface
382 glFinish();
383 glFlush();
384 sys_Repaint;
385 end;
387 procedure r_Render_Resize (w, h: Integer);
388 begin
389 gWinSizeX := w;
390 gWinSizeY := h;
391 gRC_Width := w;
392 gRC_Height := h;
393 gScreenWidth := w;
394 gScreenHeight := h;
395 end;
397 procedure r_Render_Apply;
398 begin
399 {$IFDEF ENABLE_SYSTEM}
400 if sys_SetDisplayModeGL(GetInfo()) then
401 e_LogWriteln('resolution changed')
402 else
403 e_LogWriteln('resolution not changed');
404 sys_EnableVSync(gVSync)
405 {$ENDIF}
406 end;
408 function r_Render_WriteScreenShot (filename: String): Boolean;
409 begin
410 Result := False;
411 end;
413 {$IFDEF ENABLE_GIBS}
414 function r_Render_GetGibRect (m, id: Integer): TRectWH;
415 begin
416 result := r_Map_GetGibSize(m, id);
417 end;
418 {$ENDIF}
420 {$IFDEF ENABLE_GFX}
421 procedure r_Render_QueueEffect (AnimType, X, Y: Integer);
422 begin
423 r_Map_NewGFX(AnimType, X, Y);
424 end;
425 {$ENDIF}
427 {$IFDEF ENABLE_TOUCH}
428 procedure r_Render_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean);
429 begin
430 founded := False;
431 end;
432 {$ENDIF}
434 {$IFDEF ENABLE_MENU}
435 procedure r_Render_GetControlSize (ctrl: TGUIControl; out w, h: Integer);
436 begin
437 w := 0; h := 0;
438 end;
440 procedure r_Render_GetLogoSize (out w, h: Integer);
441 begin
442 w := 0; h := 0;
443 end;
445 procedure r_Render_GetMaxFontSize (BigFont: Boolean; out w, h: Integer);
446 begin
447 w := 0; h := 0;
448 end;
450 procedure r_Render_GetStringSize (BigFont: Boolean; str: String; out w, h: Integer);
451 begin
452 w := 0; h := 0;
453 end;
454 {$ENDIF}
456 procedure r_Render_DrawLoading (force: Boolean);
457 begin
458 end;
460 end.