From aab9c00a40aebab672c35d10258404b6eea517e7 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Mon, 7 Jun 2021 20:15:24 +0300 Subject: [PATCH] render: separate gfx logic and drawing --- src/game/Doom2DF.lpr | 1 + src/game/g_gfx.pas | 179 ++++++++++++------------------------- src/game/opengl/r_game.pas | 4 +- src/game/opengl/r_gfx.pas | 91 +++++++++++++++++++ 4 files changed, 152 insertions(+), 123 deletions(-) create mode 100644 src/game/opengl/r_gfx.pas diff --git a/src/game/Doom2DF.lpr b/src/game/Doom2DF.lpr index 3286f29..87b41f8 100644 --- a/src/game/Doom2DF.lpr +++ b/src/game/Doom2DF.lpr @@ -155,6 +155,7 @@ uses r_console in 'opengl/r_console.pas', r_game in 'opengl/r_game.pas', + r_gfx in 'opengl/r_gfx.pas', {$IFDEF USE_FMOD} fmod in '../lib/FMOD/fmod.pas', diff --git a/src/game/g_gfx.pas b/src/game/g_gfx.pas index bf8fc5a..3a9c79c 100644 --- a/src/game/g_gfx.pas +++ b/src/game/g_gfx.pas @@ -64,8 +64,6 @@ procedure g_GFX_OnceAnim (X, Y: Integer; Anim: TAnimation; AnimType: Byte = 0); procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true); procedure g_GFX_Update (); -procedure g_GFX_Draw (); - var gpart_dbg_enabled: Boolean = true; @@ -75,6 +73,64 @@ var //WARNING: only for Holmes! function awmIsSetHolmes (x, y: Integer): Boolean; inline; + type (* private state *) + TPartType = (Blood, Spark, Bubbles, Water); + TPartState = (Free, Normal, Stuck, Sleeping); + TFloorType = (Wall, LiquidIn, LiquidOut); + // Wall: floorY is just before floor + // LiquidIn: floorY is liquid *start* (i.e. just in a liquid) + // LiquidOut: floorY is liquid *end* (i.e. just out of a liquid) + TEnvType = (EAir, ELiquid, EWall); // where particle is now + + // note: this MUST be record, so we can keep it in + // dynamic array and has sequential memory access pattern + PParticle = ^TParticle; + TParticle = record + x, y: Integer; + oldX, oldY: Integer; + velX, velY: Single; + accelX, accelY: Single; + state: TPartState; + particleType: TPartType; + red, green, blue: Byte; + alpha: Byte; + time, liveTime, waitTime: Word; + stickDX: Integer; // STATE_STICK: -1,1: stuck to a wall; 0: stuck to ceiling + justSticked: Boolean; // not used + floorY: Integer; // actually, floor-1; `Unknown`: unknown + floorType: TFloorType; + env: TEnvType; // where particle is now + ceilingY: Integer; // actually, ceiling+1; `Unknown`: unknown + wallEndY: Integer; // if we stuck to a wall, this is where wall ends + + //k8: sorry, i have to emulate virtual methods this way, 'cause i haet `Object` + procedure thinkerBloodAndWater (); + procedure thinkerSpark (); + procedure thinkerBubble (); + + procedure findFloor (force: Boolean=false); // this updates `floorY` if forced or Unknown + procedure findCeiling (force: Boolean=false); // this updates `ceilingY` if forced or Unknown + + procedure freeze (); inline; // remove velocities and acceleration + procedure sleep (); inline; // switch to sleep mode + + function checkAirStreams (): Boolean; // `true`: affected by air stream + + function alive (): Boolean; inline; + procedure die (); inline; + procedure think (); inline; + end; + + TOnceAnim = record + AnimType: Byte; + x, y: Integer; + oldX, oldY: Integer; + Animation: TAnimation; + end; + + var (* private state *) + Particles: array of TParticle = nil; + OnceAnims: array of TOnceAnim = nil; implementation @@ -88,66 +144,7 @@ uses const Unknown = Integer($7fffffff); - -type - TPartType = (Blood, Spark, Bubbles, Water); - TPartState = (Free, Normal, Stuck, Sleeping); - TFloorType = (Wall, LiquidIn, LiquidOut); - // Wall: floorY is just before floor - // LiquidIn: floorY is liquid *start* (i.e. just in a liquid) - // LiquidOut: floorY is liquid *end* (i.e. just out of a liquid) - TEnvType = (EAir, ELiquid, EWall); // where particle is now - - // note: this MUST be record, so we can keep it in - // dynamic array and has sequential memory access pattern - PParticle = ^TParticle; - TParticle = record - x, y: Integer; - oldX, oldY: Integer; - velX, velY: Single; - accelX, accelY: Single; - state: TPartState; - particleType: TPartType; - red, green, blue: Byte; - alpha: Byte; - time, liveTime, waitTime: Word; - stickDX: Integer; // STATE_STICK: -1,1: stuck to a wall; 0: stuck to ceiling - justSticked: Boolean; // not used - floorY: Integer; // actually, floor-1; `Unknown`: unknown - floorType: TFloorType; - env: TEnvType; // where particle is now - ceilingY: Integer; // actually, ceiling+1; `Unknown`: unknown - wallEndY: Integer; // if we stuck to a wall, this is where wall ends - - //k8: sorry, i have to emulate virtual methods this way, 'cause i haet `Object` - procedure thinkerBloodAndWater (); - procedure thinkerSpark (); - procedure thinkerBubble (); - - procedure findFloor (force: Boolean=false); // this updates `floorY` if forced or Unknown - procedure findCeiling (force: Boolean=false); // this updates `ceilingY` if forced or Unknown - - procedure freeze (); inline; // remove velocities and acceleration - procedure sleep (); inline; // switch to sleep mode - - function checkAirStreams (): Boolean; // `true`: affected by air stream - - function alive (): Boolean; inline; - procedure die (); inline; - procedure think (); inline; - end; - - TOnceAnim = record - AnimType: Byte; - x, y: Integer; - oldX, oldY: Integer; - Animation: TAnimation; - end; - - var - Particles: array of TParticle = nil; - OnceAnims: array of TOnceAnim = nil; MaxParticles: Integer = 0; CurrentParticle: Integer = 0; // awakeMap has one bit for each map grid cell; on g_Mark, @@ -1688,64 +1685,4 @@ begin end; end; - -procedure g_GFX_Draw (); - var - a, len, fx, fy: Integer; -begin - if not gpart_dbg_enabled then exit; - - if (Particles <> nil) then - begin - glDisable(GL_TEXTURE_2D); - if (g_dbg_scale < 0.6) then glPointSize(1) - else if (g_dbg_scale > 1.3) then glPointSize(g_dbg_scale+1) - else glPointSize(2); - glDisable(GL_POINT_SMOOTH); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - glBegin(GL_POINTS); - - len := High(Particles); - for a := 0 to len do - begin - with Particles[a] do - begin - if not alive then continue; - if (x >= sX) and (y >= sY) and (x <= sX+sWidth) and (sY <= sY+sHeight) then - begin - fx := nlerp(oldx, x, gLerpFactor); - fy := nlerp(oldy, y, gLerpFactor); - glColor4ub(red, green, blue, alpha); - glVertex2f(fx+0.37, fy+0.37); - end; - end; - end; - - glEnd(); - - glDisable(GL_BLEND); - end; - - if (OnceAnims <> nil) then - begin - len := High(OnceAnims); - for a := 0 to len do - begin - if (OnceAnims[a].Animation <> nil) then - begin - with OnceAnims[a] do - begin - fx := nlerp(oldx, x, gLerpFactor); - fy := nlerp(oldy, y, gLerpFactor); - Animation.Draw(x, y, TMirrorType.None); - end; - end; - end; - end; -end; - - end. diff --git a/src/game/opengl/r_game.pas b/src/game/opengl/r_game.pas index 6dd62a2..d2ead67 100644 --- a/src/game/opengl/r_game.pas +++ b/src/game/opengl/r_game.pas @@ -32,7 +32,7 @@ implementation g_textures, e_input, e_sound, g_language, g_console, g_menu, g_triggers, g_player, g_options, g_monsters, g_map, g_panel, g_window, g_items, g_weapons, g_gfx, g_phys, g_net, g_gui, g_netmaster, - g_game, r_console + g_game, r_console, r_gfx ; var @@ -1247,7 +1247,7 @@ begin drawOther('monsters', @g_Monsters_Draw); drawOther('itemdrop', @g_Items_DrawDrop); drawPanelType('*door', PANEL_CLOSEDOOR, g_rlayer_door); - drawOther('gfx', @g_GFX_Draw); + drawOther('gfx', @r_GFX_Draw); drawOther('flags', @g_Map_DrawFlags); drawPanelType('*acid1', PANEL_ACID1, g_rlayer_acid1); drawPanelType('*acid2', PANEL_ACID2, g_rlayer_acid2); diff --git a/src/game/opengl/r_gfx.pas b/src/game/opengl/r_gfx.pas new file mode 100644 index 0000000..6b27ec3 --- /dev/null +++ b/src/game/opengl/r_gfx.pas @@ -0,0 +1,91 @@ +(* 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_gfx; + +interface + + procedure r_GFX_Draw; + +implementation + + uses + {$INCLUDE ../nogl/noGLuses.inc} + SysUtils, Classes, Math, + utils, + e_graphics, + g_game, + g_gfx + ; + +procedure r_GFX_Draw; + var + a, len, fx, fy: Integer; +begin + if not gpart_dbg_enabled then exit; + + if (Particles <> nil) then + begin + glDisable(GL_TEXTURE_2D); + if (g_dbg_scale < 0.6) then glPointSize(1) + else if (g_dbg_scale > 1.3) then glPointSize(g_dbg_scale+1) + else glPointSize(2); + glDisable(GL_POINT_SMOOTH); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glBegin(GL_POINTS); + + len := High(Particles); + for a := 0 to len do + begin + with Particles[a] do + begin + if not alive then continue; + if (x >= sX) and (y >= sY) and (x <= sX+sWidth) and (sY <= sY+sHeight) then + begin + fx := nlerp(oldx, x, gLerpFactor); + fy := nlerp(oldy, y, gLerpFactor); + glColor4ub(red, green, blue, alpha); + glVertex2f(fx+0.37, fy+0.37); + end; + end; + end; + + glEnd(); + + glDisable(GL_BLEND); + end; + + if (OnceAnims <> nil) then + begin + len := High(OnceAnims); + for a := 0 to len do + begin + if (OnceAnims[a].Animation <> nil) then + begin + with OnceAnims[a] do + begin + fx := nlerp(oldx, x, gLerpFactor); + fy := nlerp(oldy, y, gLerpFactor); + Animation.Draw(x, y, TMirrorType.None); + end; + end; + end; + end; +end; + +end. -- 2.29.2