raze/source/core/psky.cpp

110 lines
3.2 KiB
C++
Raw Normal View History

2022-01-12 22:33:44 +00:00
/*
** pskies.cpp
** Handling Build skies
**
**---------------------------------------------------------------------------
** Copyright 2019 Christoph Oelckers
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
**
*/
#include "psky.h"
#include "gamefuncs.h"
#include "tarray.h"
#include "texinfo.h"
#include "texturemanager.h"
2022-01-12 22:33:44 +00:00
static TArray<SkyDefinition> skies;
static SkyDefinition *FindSky(FTextureID texid)
2022-01-12 22:33:44 +00:00
{
for (auto& sky : skies)
if (texid == sky.texid) return &sky;
2022-01-12 22:33:44 +00:00
return nullptr;
}
void addSky(SkyDefinition& sky, FTextureID texid)
2022-01-12 22:33:44 +00:00
{
SkyDefinition* old = FindSky(texid);
2022-01-12 22:33:44 +00:00
sky.texid = texid;
2022-01-12 22:33:44 +00:00
if (sky.scale == 0) sky.scale = 1.f;
if (old) *old = sky;
else skies.Push(sky);
}
SkyDefinition getSky(FTextureID texid)
2022-01-12 22:33:44 +00:00
{
SkyDefinition result;
auto sky = FindSky(texid);
2022-01-12 22:33:44 +00:00
if (sky) result = *sky;
else
{
sky = FindSky(FNullTextureID());
2022-01-12 22:33:44 +00:00
if (sky)
result = *sky;
else
{
result = {};
result.scale = 1.f;
}
auto tex = TexMan.GetGameTexture(texid);
if (tex->isValid())
2022-01-12 22:33:44 +00:00
{
int w = (int)tex->GetDisplayWidth();
if (result.lognumtiles == 0 || w >= 256)
{
if (w < 512) result.lognumtiles = 2;
else if (w < 1024) result.lognumtiles = 1;
else result.lognumtiles = 0;
}
2022-01-12 22:33:44 +00:00
}
}
return result;
}
void defineSky(const char* tilename, int lognumtiles, const int16_t* tileofs, int yoff, float yscale, int yoff2)
2022-01-12 22:33:44 +00:00
{
FTextureID texid = FNullTextureID();
if (tilename)
{
texid = TexMan.CheckForTexture(tilename, ETextureType::Any);
if (!texid.isValid()) return;
}
2022-01-12 22:33:44 +00:00
SkyDefinition sky;
sky.baselineofs = yoff2 == 0x7fffffff ? yoff : yoff2;
sky.lognumtiles = lognumtiles;
sky.scale = yscale;
memset(sky.offsets, 0, sizeof(sky.offsets));
if (tileofs) memcpy(sky.offsets, tileofs, 2 << lognumtiles);
addSky(sky, texid);
2022-11-30 13:56:56 +00:00
}