openclonk/src/graphics/C4FacetEx.cpp

169 lines
4.6 KiB
C++
Raw Normal View History

2009-05-08 13:28:41 +00:00
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 1998-2000, Matthes Bender
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
2016-04-03 18:18:29 +00:00
* Copyright (c) 2009-2016, The OpenClonk Team and contributors
2009-05-08 13:28:41 +00:00
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
2009-05-08 13:28:41 +00:00
*
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
* See accompanying file "TRADEMARK" for details.
2009-05-08 13:28:41 +00:00
*
* To redistribute this file separately, substitute the full license texts
* for the above references.
2009-05-08 13:28:41 +00:00
*/
/* A facet that can hold its own surface and also target coordinates */
#include "C4Include.h"
#include "graphics/C4FacetEx.h"
#include "graphics/C4Draw.h"
2009-05-08 13:28:41 +00:00
#include "lib/C4Rect.h"
#include "c4group/C4Group.h"
2009-05-08 13:28:41 +00:00
void C4TargetFacet::Set(C4Surface * nsfc, float nx, float ny, float nwdt, float nhgt, float ntx, float nty, float Zoom)
{
Set(nsfc, nx, ny, nwdt, nhgt, ntx, nty, Zoom, ntx, nty);
}
void C4TargetFacet::Set(C4Surface * nsfc, float nx, float ny, float nwdt, float nhgt, float ntx, float nty, float Zoom, float prx, float pry)
2010-03-28 18:58:01 +00:00
{
C4Facet::Set(nsfc, nx, ny, nwdt, nhgt);
TargetX = ntx; TargetY = nty; this->Zoom = Zoom;
ParRefX = prx; ParRefY = pry;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
2011-10-03 14:07:07 +00:00
void C4TargetFacet::Set(C4Surface * nsfc, const C4Rect & r, float ntx, float nty, float Zoom)
2010-03-28 18:58:01 +00:00
{
Set(nsfc, r.x, r.y, r.Wdt, r.Hgt, ntx, nty, Zoom);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4TargetFacet::SetRect(C4TargetRect &rSrc)
2010-03-28 18:58:01 +00:00
{
X=rSrc.x; Y=rSrc.y; Wdt=rSrc.Wdt; Hgt=rSrc.Hgt;
TargetX=rSrc.tx; TargetY=rSrc.ty;
ParRefX=rSrc.tx; TargetY=rSrc.ty;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// ------------------------
// C4FacetSurface
bool C4FacetSurface::Create(int iWdt, int iHgt, int iWdt2, int iHgt2)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
Clear();
// Create surface
Face.Default();
if (!Face.Create(iWdt,iHgt)) return false;
2009-05-08 13:28:41 +00:00
// Set facet
if (iWdt2==C4FCT_Full) iWdt2=Face.Wdt; if (iWdt2==C4FCT_Height) iWdt2=Face.Hgt; if (iWdt2==C4FCT_Width) iWdt2=Face.Wdt;
if (iHgt2==C4FCT_Full) iHgt2=Face.Hgt; if (iHgt2==C4FCT_Height) iHgt2=Face.Hgt; if (iHgt2==C4FCT_Width) iHgt2=Face.Wdt;
Set(&Face,0,0,iWdt2,iHgt2);
return true;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
bool C4FacetSurface::CreateClrByOwner(C4Surface *pBySurface)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
Clear();
// create surface
if (!Face.CreateColorByOwner(pBySurface)) return false;
2009-05-08 13:28:41 +00:00
// set facet
Set(&Face,0,0,Face.Wdt,Face.Hgt);
2009-05-08 13:28:41 +00:00
// success
return true;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
bool C4FacetSurface::Load(C4Group &hGroup, const char *szName, int iWdt, int iHgt, bool fNoErrIfNotFound, int iFlags)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
Clear();
// Entry name
char szFilename[_MAX_FNAME+1];
SCopy(szName,szFilename,_MAX_FNAME);
char *szExt = GetExtension(szFilename);
if (!*szExt)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// no extension: Default to extension that is found as file in group
const char * const extensions[] = { "png", "bmp", "jpeg", "jpg", nullptr };
2009-05-08 13:28:41 +00:00
int i = 0; const char *szExt;
2010-01-25 04:00:59 +00:00
while ((szExt = extensions[i++]))
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
EnforceExtension(szFilename, szExt);
if (hGroup.FindEntry(szFilename)) break;
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// Load surface
if (!Face.Load(hGroup,szFilename,false,fNoErrIfNotFound, iFlags)) return false;
2009-05-08 13:28:41 +00:00
// Set facet
if (iWdt==C4FCT_Full) iWdt=Face.Wdt; if (iWdt==C4FCT_Height) iWdt=Face.Hgt; if (iWdt==C4FCT_Width) iWdt=Face.Wdt;
if (iHgt==C4FCT_Full) iHgt=Face.Hgt; if (iHgt==C4FCT_Height) iHgt=Face.Hgt; if (iHgt==C4FCT_Width) iHgt=Face.Wdt;
Set(&Face,0,0,iWdt,iHgt);
return true;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
bool C4FacetSurface::CopyFromSfcMaxSize(C4Surface &srcSfc, int32_t iMaxSize, uint32_t dwColor)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// safety
if (!srcSfc.Wdt || !srcSfc.Hgt) return false;
Clear();
// no scale?
bool fNeedsScale = !(srcSfc.Wdt <= iMaxSize && srcSfc.Hgt <= iMaxSize);
if (!fNeedsScale && !dwColor)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// no change necessary; just copy then
Face.Copy(srcSfc);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
else
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// must scale down or colorize. Just blit.
C4Facet fctSource;
fctSource.Set(&srcSfc, 0,0,srcSfc.Wdt,srcSfc.Hgt);
int32_t iTargetWdt, iTargetHgt;
if (fNeedsScale)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
if (fctSource.Wdt > fctSource.Hgt)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
iTargetWdt = iMaxSize;
iTargetHgt = fctSource.Hgt * iTargetWdt / fctSource.Wdt;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
else
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
iTargetHgt = iMaxSize;
iTargetWdt = fctSource.Wdt * iTargetHgt / fctSource.Hgt;
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
else
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
iTargetWdt = fctSource.Wdt;
iTargetHgt = fctSource.Hgt;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
if (dwColor) srcSfc.SetClr(dwColor);
Create(iTargetWdt, iTargetHgt);
2011-10-03 14:30:18 +00:00
pDraw->Blit(&srcSfc, 0.0f,0.0f,float(fctSource.Wdt),float(fctSource.Hgt),
2009-05-08 13:28:41 +00:00
&Face, 0,0,iTargetWdt,iTargetHgt);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
Set(&Face, 0,0, Face.Wdt, Face.Hgt);
return true;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4FacetSurface::Grayscale(int32_t iOffset)
2010-03-28 18:58:01 +00:00
{
2011-10-03 14:30:18 +00:00
if (!pDraw || !Surface || !Wdt || !Hgt) return;
pDraw->Grayscale(Surface, iOffset);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
bool C4FacetSurface::EnsureOwnSurface()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// is it a link?
if (Surface != &Face)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// then recreate in same size
C4Facet fctOld = *this;
if (!Create(fctOld.Wdt, fctOld.Hgt)) return false;
fctOld.Draw(*this);
}
2010-03-28 18:58:01 +00:00
return true;
}
2009-05-08 13:28:41 +00:00