Skip to content

Commit

Permalink
Move TexMan.GetCanvas and GetTextureCanvas to common
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas authored and coelckers committed Jul 25, 2022
1 parent 7538223 commit e2778ba
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 43 deletions.
46 changes: 46 additions & 0 deletions src/common/2d/v_2ddrawer.cpp
Expand Up @@ -41,6 +41,7 @@
#include "v_draw.h"
#include "v_video.h"
#include "fcolormap.h"
#include "texturemanager.h"

static F2DDrawer drawer = F2DDrawer();
F2DDrawer* twod = &drawer;
Expand Down Expand Up @@ -1228,3 +1229,48 @@ F2DVertexBuffer::F2DVertexBuffer()
};
mVertexBuffer->SetFormat(1, 3, sizeof(F2DDrawer::TwoDVertex), format);
}

//==========================================================================
//
//
//
//==========================================================================

TArray<FCanvas*> AllCanvases;

class InitTextureCanvasGC
{
public:
InitTextureCanvasGC()
{
GC::AddMarkerFunc([]() {
for (auto canvas : AllCanvases)
GC::Mark(canvas);
});
}
};

FCanvas* GetTextureCanvas(const FString& texturename)
{
FTextureID textureid = TexMan.CheckForTexture(texturename, ETextureType::Wall, FTextureManager::TEXMAN_Overridable);
if (textureid.isValid())
{
// Only proceed if the texture is a canvas texture.
auto tex = TexMan.GetGameTexture(textureid);
if (tex && tex->GetTexture()->isCanvas())
{
FCanvasTexture* canvasTex = static_cast<FCanvasTexture*>(tex->GetTexture());
if (!canvasTex->Canvas)
{
static InitTextureCanvasGC initCanvasGC; // Does the common code have a natural init function this could be moved to?

canvasTex->Canvas = Create<FCanvas>();
canvasTex->Canvas->Tex = canvasTex;
canvasTex->Canvas->Drawer.SetSize(tex->GetTexelWidth(), tex->GetTexelHeight());
AllCanvases.Push(canvasTex->Canvas);
}
return canvasTex->Canvas;
}
}
return nullptr;
}
9 changes: 9 additions & 0 deletions src/common/scripting/interface/vmnatives.cpp
Expand Up @@ -552,6 +552,15 @@ DEFINE_ACTION_FUNCTION_NATIVE(_TexMan, UseGamePalette, UseGamePalette)
ACTION_RETURN_INT(UseGamePalette(texid));
}

FCanvas* GetTextureCanvas(const FString& texturename);

DEFINE_ACTION_FUNCTION(_TexMan, GetCanvas)
{
PARAM_PROLOGUE;
PARAM_STRING(texturename);
ACTION_RETURN_POINTER(GetTextureCanvas(texturename));
}

//=====================================================================================
//
// FFont exports
Expand Down
1 change: 0 additions & 1 deletion src/common/textures/textures.h
Expand Up @@ -44,7 +44,6 @@
#include "hw_texcontainer.h"
#include "floatrect.h"
#include "refcounted.h"
#include <memory>

typedef TMap<int, bool> SpriteHits;
class FImageSource;
Expand Down
3 changes: 0 additions & 3 deletions src/d_main.cpp
Expand Up @@ -2926,16 +2926,13 @@ static void Doom_CastSpriteIDToString(FString* a, unsigned int b)


extern DThinker* NextToThink;
extern TArray<FCanvas*> AllCanvases;

static void GC_MarkGameRoots()
{
GC::Mark(staticEventManager.FirstEventHandler);
GC::Mark(staticEventManager.LastEventHandler);
for (auto Level : AllLevels())
Level->Mark();
for (auto canvas : AllCanvases)
GC::Mark(canvas);

// Mark players.
for (int i = 0; i < MAXPLAYERS; i++)
Expand Down
25 changes: 0 additions & 25 deletions src/r_data/r_canvastexture.cpp
Expand Up @@ -110,31 +110,6 @@ void SetCameraToTexture(AActor *viewpoint, const FString &texturename, double fo
}
}

TArray<FCanvas*> AllCanvases;

FCanvas* GetTextureCanvas(const FString& texturename)
{
FTextureID textureid = TexMan.CheckForTexture(texturename, ETextureType::Wall, FTextureManager::TEXMAN_Overridable);
if (textureid.isValid())
{
// Only proceed if the texture is a canvas texture.
auto tex = TexMan.GetGameTexture(textureid);
if (tex && tex->GetTexture()->isCanvas())
{
FCanvasTexture* canvasTex = static_cast<FCanvasTexture*>(tex->GetTexture());
if (!canvasTex->Canvas)
{
canvasTex->Canvas = Create<FCanvas>();
canvasTex->Canvas->Tex = canvasTex;
canvasTex->Canvas->Drawer.SetSize(tex->GetTexelWidth(), tex->GetTexelHeight());
AllCanvases.Push(canvasTex->Canvas);
}
return canvasTex->Canvas;
}
}
return nullptr;
}

//==========================================================================
//
// FCanvasTextureInfo :: UpdateAll
Expand Down
2 changes: 0 additions & 2 deletions src/r_data/r_canvastexture.h
Expand Up @@ -24,5 +24,3 @@ struct FCanvasTextureInfo
void Mark();

};

extern TArray<FCanvas*> AllCanvases;
11 changes: 0 additions & 11 deletions src/scripting/vmthunks.cpp
Expand Up @@ -102,17 +102,6 @@ DEFINE_ACTION_FUNCTION_NATIVE(_TexMan, SetCameraTextureAspectRatio, SetCameraTex
return 0;
}

FCanvas* GetTextureCanvas(const FString& texturename);

DEFINE_ACTION_FUNCTION(_TexMan, GetCanvas)
{
PARAM_PROLOGUE;
PARAM_STRING(texturename);
FCanvas* canvas = GetTextureCanvas(texturename);
if (numret > 0) ret[0].SetPointer(canvas);
return numret;
}

//=====================================================================================
//
// sector_t exports
Expand Down
1 change: 0 additions & 1 deletion wadsrc/static/zscript/doombase.zs
Expand Up @@ -28,7 +28,6 @@ extend struct TexMan
{
native static void SetCameraToTexture(Actor viewpoint, String texture, double fov);
native static void SetCameraTextureAspectRatio(String texture, double aspectScale, bool useTextureRatio = true);
native static Canvas GetCanvas(String texture);
deprecated("3.8", "Use Level.ReplaceTextures() instead") static void ReplaceTextures(String from, String to, int flags)
{
level.ReplaceTextures(from, to, flags);
Expand Down
1 change: 1 addition & 0 deletions wadsrc/static/zscript/engine/base.zs
Expand Up @@ -307,6 +307,7 @@ struct TexMan
native static int CheckRealHeight(TextureID tex);
native static bool OkForLocalization(TextureID patch, String textSubstitute);
native static bool UseGamePalette(TextureID tex);
native static Canvas GetCanvas(String texture);
}

/*
Expand Down

0 comments on commit e2778ba

Please sign in to comment.