From 7f582aab480b6d0280751d5bcacc54437cba2cc5 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Sun, 12 Jun 2022 13:17:57 +0300 Subject: [PATCH] gl: draw console --- src/game/Doom2DF.lpr | 1 + src/game/renders/opengl/r_console.pas | 178 +++++++++++++++++++++++++ src/game/renders/opengl/r_draw.pas | 10 +- src/game/renders/opengl/r_render.pas | 9 +- src/game/renders/opengl/r_textures.pas | 2 + 5 files changed, 192 insertions(+), 8 deletions(-) create mode 100644 src/game/renders/opengl/r_console.pas diff --git a/src/game/Doom2DF.lpr b/src/game/Doom2DF.lpr index 5eb2229..d5e4376 100644 --- a/src/game/Doom2DF.lpr +++ b/src/game/Doom2DF.lpr @@ -204,6 +204,7 @@ uses r_map in 'renders/opengl/r_map.pas', r_fonts in 'renders/opengl/r_fonts.pas', r_common in 'renders/opengl/r_common.pas', + r_console in 'renders/opengl/r_console.pas', r_gui in 'renders/opengl/r_gui.pas', {$ELSE} {$FATAL render driver not selected} diff --git a/src/game/renders/opengl/r_console.pas b/src/game/renders/opengl/r_console.pas new file mode 100644 index 0000000..60b14c1 --- /dev/null +++ b/src/game/renders/opengl/r_console.pas @@ -0,0 +1,178 @@ +(* Copyright (C) Doom 2D: Forever Developers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License ONLY. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *) +{$INCLUDE ../../../shared/a_modes.inc} +unit r_console; + +interface + + procedure r_Console_Initialize; + procedure r_Console_Finalize; + + procedure r_Console_Load; + procedure r_Console_Free; + + procedure r_Console_Update; + procedure r_Console_Draw (MessagesOnly: Boolean); + +implementation + + uses + Math, utils, conbuf, + g_game, g_options, g_console, + r_draw, r_textures, r_fonts, r_common + ; + + var + Background: TGLTexture; + + ConsoleTrans: Single; + ChatTop: BOOLEAN; + + Cons_Y: SmallInt; + ConsoleHeight: Single; + ConsoleStep: Single; + Cons_Shown: Boolean; // draw console + + procedure r_Console_Initialize; + begin + Cons_Y := Round(-Floor(gScreenHeight * ConsoleHeight)); + end; + + procedure r_Console_Finalize; + begin + end; + + procedure r_Console_Load; + begin + Background := r_Textures_LoadFromFile(GameWad + ':TEXTURES/CONSOLE'); + end; + + procedure r_Console_Free; + begin + Background.Free; + end; + + procedure r_Console_Update; + var step, miny: Integer; + begin + step := MAX(1, Round(Floor(gScreenHeight * ConsoleHeight) * ConsoleStep)); + miny := Round(-Floor(gScreenHeight * ConsoleHeight)); + if gConsoleShow then + Cons_Y := Cons_Y + step + else + Cons_Y := Cons_Y - step; + Cons_Y := MIN(MAX(Cons_Y, miny), 0); + Cons_Shown := Cons_Y > miny; + end; + + procedure r_Console_DrawLog; + var cw, ch, ty, skip: Integer; sp, ep: LongWord; + + procedure PutLine (sp, ep: LongWord); + var p: LongWord; w, chw: Integer; + begin + p := sp; w := 0; chw := 0; + while (p <> ep) and (w + chw <= gScreenWidth - 8) do + begin + chw := stdfont.GetWidth(cbufAt(p)); + INC(w, chw); + cbufNext(p); + end; + if p <> ep then + PutLine(p, ep); + if skip = 0 then + begin + ep := p; p := sp; w := 2; + while p <> ep do + begin + r_Draw_Text(cbufAt(p), w, ty, 255, 255, 255, 255, stdfont); + chw := stdfont.GetWidth(cbufAt(p)); + INC(w, chw); + cbufNext(p); + end; + DEC(ty, ch); + end + else + begin + DEC(skip); + end; + end; + + begin + cw := stdfont.GetMaxWidth(); + ch := stdfont.GetMaxHeight(); + ty := Floor(gScreenHeight * ConsoleHeight) - 4 - 2 * ch - Abs(Cons_Y); + skip := conSkipLines; + cbufLastLine(sp, ep); + repeat + PutLine(sp, ep); + until (ty + ch <= 0) or (cbufLineUp(sp, ep) = false); + end; + + procedure r_Console_Draw (MessagesOnly: Boolean); + var cw, ch, y, i: Integer; s: AnsiString; + begin + cw := stdfont.GetMaxWidth(); + ch := stdfont.GetMaxHeight(); + + if ChatTop and gChatShow then y := ch else y := 0; + + for i := 0 to High(MsgArray) do + if MsgArray[i].time > 0 then + r_Draw_Text(MsgArray[i].msg, 0, y + ch * i, 255, 255, 255, 255, stdfont); + + if MessagesOnly = false then + begin + if gChatShow then + begin + if ChatTop then y := 0 else y := gScreenHeight - ch - 1; + if gChatTeam then s := 'say team> ' else s := 'say> '; + r_Draw_Text(s + Line, 0, y, 255, 255, 255, 255, stdfont); + r_Draw_Text('_', (CPos + 4) * cw, y, 255, 255, 255, 255, stdfont); + end; + + if Cons_Shown then + begin + if gDebugMode then + begin + // TODO draw debug string + end; + + if Background <> nil then + r_Draw_Texture(Background, 0, Cons_Y, gScreenWidth, Floor(gScreenHeight * ConsoleHeight), false, 255, 255, 255, Round((1.0 - ConsoleTrans) * 255), false); + + r_Console_DrawLog; + + r_Draw_Text('> ' + Line, 0, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - ch - 4, 255, 255, 255, 255, stdfont); + r_Draw_Text('_', (CPos + 1) * cw, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - 21, 255, 255, 255, 255, stdfont); + end; + end; + end; + +initialization + conRegVar('chat_at_top', @ChatTop, 'draw chat at top border', 'draw chat at top border'); + conRegVar('console_height', @ConsoleHeight, 0.0, 1.0, 'set console size', 'set console size'); + conRegVar('console_step', @ConsoleStep, 0.0, 1.0, 'set console animation speed', 'set console animation speed'); + conRegVar('console_trans', @ConsoleTrans, 0.0, 1.0, 'set console transparency', 'set console transparency'); +{$IFDEF ANDROID} + ChatTop := True; + ConsoleHeight := 0.35; +{$ELSE} + ChatTop := False; + ConsoleHeight := 0.5; +{$ENDIF} + ConsoleStep := 0.07; + ConsoleTrans := 0.1; +end. diff --git a/src/game/renders/opengl/r_draw.pas b/src/game/renders/opengl/r_draw.pas index 0091169..bdd6071 100644 --- a/src/game/renders/opengl/r_draw.pas +++ b/src/game/renders/opengl/r_draw.pas @@ -259,16 +259,16 @@ implementation end; procedure r_Draw_Text (const text: AnsiString; x, y: Integer; r, g, b, a: Byte; f: TGLFont); - var i, xoff: Integer; t: TGLTexture; ch: AnsiChar; + var i, xoff, spc: Integer; t: TGLTexture; ch: AnsiChar; begin - xoff := x; + xoff := x; spc := MAX(0, f.GetSpace()); for i := 1 to Length(text) do begin ch := text[i]; t := f.GetChar(ch); if t <> nil then r_Draw_Texture(t, xoff, y, t.width, t.height, false, r, g, b, a, false); - Inc(xoff, f.GetWidth(ch) + f.GetSpace()); + Inc(xoff, f.GetWidth(ch) + spc); end; end; @@ -280,12 +280,10 @@ implementation len := Length(text); if len > 0 then begin - spc := f.GetSpace(); + spc := MAX(0, f.GetSpace()); for i := 1 to len - 1 do Inc(w, f.GetWidth(text[i]) + spc); Inc(w, f.GetWidth(text[len])); - if spc < 0 then - Inc(w, spc) end; end; diff --git a/src/game/renders/opengl/r_render.pas b/src/game/renders/opengl/r_render.pas index 6e9b96c..fb65ab5 100644 --- a/src/game/renders/opengl/r_render.pas +++ b/src/game/renders/opengl/r_render.pas @@ -84,7 +84,7 @@ implementation e_log, utils, g_game, g_options, g_console, g_player, g_weapons, g_language, g_net, - r_draw, r_textures, r_fonts, r_common, r_map + r_draw, r_textures, r_fonts, r_common, r_console, r_map ; type @@ -135,6 +135,7 @@ implementation hudkey[2] := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/KEYB'); hudair := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/AIRBAR'); hudjet := r_Textures_LoadFromFile(GameWAD + ':TEXTURES/JETBAR'); + r_Console_Load; r_Map_Load; {$IFDEF ENABLE_MENU} r_GUI_Load; @@ -148,6 +149,7 @@ implementation r_GUI_Free; {$ENDIF} r_Map_Free; + r_Console_Free; hudjet.Free; hudair.Free; hudkey[0].Free; @@ -193,17 +195,20 @@ implementation sys_EnableVSync(gVSync); {$ENDIF} r_Textures_Initialize; + r_Console_Initialize; r_Map_Initialize; end; procedure r_Render_Finalize; begin r_Map_Finalize; + r_Console_Finalize; r_Textures_Finalize; end; procedure r_Render_Update; begin + r_Console_Update; r_Map_Update; end; @@ -403,7 +408,7 @@ implementation end; {$ENDIF} - // TODO draw console + r_Console_Draw(false); // TODO draw holmes interface diff --git a/src/game/renders/opengl/r_textures.pas b/src/game/renders/opengl/r_textures.pas index 8df881a..64d4f53 100644 --- a/src/game/renders/opengl/r_textures.pas +++ b/src/game/renders/opengl/r_textures.pas @@ -779,6 +779,8 @@ implementation result := self.info.ch[c].w; if result = 0 then result := self.info.w; + if self.info.kern < 0 then + result := result + self.info.kern; end; function TGLFont.GetMaxWidth (): Integer; -- 2.29.2