OpenClonk
C4FacetEx.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* A facet that can hold its own surface and also target coordinates */
19 
20 #include "C4Include.h"
21 #include "graphics/C4FacetEx.h"
22 #include "graphics/C4Draw.h"
23 
24 #include "lib/C4Rect.h"
25 #include "c4group/C4Group.h"
26 
27 void C4TargetFacet::Set(C4Surface * nsfc, float nx, float ny, float nwdt, float nhgt, float ntx, float nty, float Zoom)
28 {
29  Set(nsfc, nx, ny, nwdt, nhgt, ntx, nty, Zoom, ntx, nty);
30 }
31 
32 void C4TargetFacet::Set(C4Surface * nsfc, float nx, float ny, float nwdt, float nhgt, float ntx, float nty, float Zoom, float prx, float pry)
33 {
34  C4Facet::Set(nsfc, nx, ny, nwdt, nhgt);
35  TargetX = ntx; TargetY = nty; this->Zoom = Zoom;
36  ParRefX = prx; ParRefY = pry;
37 }
38 
39 void C4TargetFacet::Set(C4Surface * nsfc, const C4Rect & r, float ntx, float nty, float Zoom)
40 {
41  Set(nsfc, r.x, r.y, r.Wdt, r.Hgt, ntx, nty, Zoom);
42 }
43 
45 {
46  X=rSrc.x; Y=rSrc.y; Wdt=rSrc.Wdt; Hgt=rSrc.Hgt;
47  TargetX=rSrc.tx; TargetY=rSrc.ty;
48  ParRefX=rSrc.tx; TargetY=rSrc.ty;
49 }
50 
51 // ------------------------
52 // C4FacetSurface
53 
54 bool C4FacetSurface::Create(int iWdt, int iHgt, int iWdt2, int iHgt2)
55 {
56  Clear();
57  // Create surface
58  Face.Default();
59  if (!Face.Create(iWdt,iHgt)) return false;
60  // Set facet
61  if (iWdt2 == C4FCT_Full || iWdt2 == C4FCT_Width)
62  iWdt2 = Face.Wdt;
63  else if (iWdt2 == C4FCT_Height)
64  iWdt2 = Face.Hgt;
65  if (iHgt2 == C4FCT_Full || iHgt2 == C4FCT_Height)
66  iHgt2 = Face.Hgt;
67  else if (iHgt2 == C4FCT_Width)
68  iHgt2 = Face.Wdt;
69  Set(&Face,0,0,iWdt2,iHgt2);
70  return true;
71 }
72 
74 {
75  Clear();
76  // create surface
77  if (!Face.CreateColorByOwner(pBySurface)) return false;
78  // set facet
79  Set(&Face,0,0,Face.Wdt,Face.Hgt);
80  // success
81  return true;
82 }
83 
84 bool C4FacetSurface::Load(C4Group &hGroup, const char *szName, int iWdt, int iHgt, bool fNoErrIfNotFound, int iFlags)
85 {
86  Clear();
87  // Entry name
88  char szFilename[_MAX_FNAME_LEN];
89  SCopy(szName,szFilename,_MAX_FNAME);
90  char *szExt = GetExtension(szFilename);
91  if (!*szExt)
92  {
93  // no extension: Default to extension that is found as file in group
94  const char * const extensions[] = { "png", "bmp", "jpeg", "jpg", nullptr };
95  int i = 0; const char *szExt;
96  while ((szExt = extensions[i++]))
97  {
98  EnforceExtension(szFilename, szExt);
99  if (hGroup.FindEntry(szFilename)) break;
100  }
101  }
102  // Load surface
103  if (!Face.Load(hGroup,szFilename,false,fNoErrIfNotFound, iFlags)) return false;
104  // Set facet
105  if (iWdt == C4FCT_Full || iWdt == C4FCT_Width)
106  iWdt = Face.Wdt;
107  else if (iWdt == C4FCT_Height)
108  iWdt = Face.Hgt;
109  if (iHgt == C4FCT_Full || iHgt == C4FCT_Height)
110  iHgt = Face.Hgt;
111  else if (iHgt == C4FCT_Width)
112  iHgt = Face.Wdt;
113  Set(&Face,0,0,iWdt,iHgt);
114  return true;
115 }
116 
117 bool C4FacetSurface::CopyFromSfcMaxSize(C4Surface &srcSfc, int32_t iMaxSize, uint32_t dwColor)
118 {
119  // safety
120  if (!srcSfc.Wdt || !srcSfc.Hgt) return false;
121  Clear();
122  // no scale?
123  bool fNeedsScale = !(srcSfc.Wdt <= iMaxSize && srcSfc.Hgt <= iMaxSize);
124  if (!fNeedsScale && !dwColor)
125  {
126  // no change necessary; just copy then
127  Face.Copy(srcSfc);
128  }
129  else
130  {
131  // must scale down or colorize. Just blit.
132  C4Facet fctSource;
133  fctSource.Set(&srcSfc, 0,0,srcSfc.Wdt,srcSfc.Hgt);
134  int32_t iTargetWdt, iTargetHgt;
135  if (fNeedsScale)
136  {
137  if (fctSource.Wdt > fctSource.Hgt)
138  {
139  iTargetWdt = iMaxSize;
140  iTargetHgt = fctSource.Hgt * iTargetWdt / fctSource.Wdt;
141  }
142  else
143  {
144  iTargetHgt = iMaxSize;
145  iTargetWdt = fctSource.Wdt * iTargetHgt / fctSource.Hgt;
146  }
147  }
148  else
149  {
150  iTargetWdt = fctSource.Wdt;
151  iTargetHgt = fctSource.Hgt;
152  }
153  if (dwColor) srcSfc.SetClr(dwColor);
154  Create(iTargetWdt, iTargetHgt);
155  pDraw->Blit(&srcSfc, 0.0f,0.0f,float(fctSource.Wdt),float(fctSource.Hgt),
156  &Face, 0,0,iTargetWdt,iTargetHgt);
157  }
158  Set(&Face, 0,0, Face.Wdt, Face.Hgt);
159  return true;
160 }
161 
162 void C4FacetSurface::Grayscale(int32_t iOffset)
163 {
164  if (!pDraw || !Surface || !Wdt || !Hgt) return;
165  pDraw->Grayscale(Surface, iOffset);
166 }
167 
169 {
170  // is it a link?
171  if (Surface != &Face)
172  {
173  // then recreate in same size
174  C4Facet fctOld = *this;
175  if (!Create(fctOld.Wdt, fctOld.Hgt)) return false;
176  fctOld.Draw(*this);
177  }
178  return true;
179 }
180 
C4Draw * pDraw
Definition: C4Draw.cpp:42
const int C4FCT_Full
Definition: C4FacetEx.h:26
const int C4FCT_Width
Definition: C4FacetEx.h:28
const int C4FCT_Height
Definition: C4FacetEx.h:27
#define _MAX_FNAME
#define _MAX_FNAME_LEN
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:118
void EnforceExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:286
bool Blit(C4Surface *sfcSource, float fx, float fy, float fwdt, float fhgt, C4Surface *sfcTarget, float tx, float ty, float twdt, float thgt, bool fSrcColKey=false, const C4BltTransform *pTransform=nullptr)
Definition: C4Draw.cpp:301
void Grayscale(C4Surface *sfcSfc, int32_t iOffset=0)
Definition: C4Draw.cpp:695
void Set(C4Surface &rSfc)
Definition: C4Facet.cpp:459
C4Surface * Surface
Definition: C4Facet.h:117
float Hgt
Definition: C4Facet.h:118
float Wdt
Definition: C4Facet.h:118
void Draw(C4Facet &cgo, bool fAspect=true, int32_t iPhaseX=0, int32_t iPhaseY=0, bool fTransparent=true)
Definition: C4Facet.cpp:154
float Y
Definition: C4Facet.h:118
float X
Definition: C4Facet.h:118
bool Load(C4Group &hGroup, const char *szName, int iWdt, int iHgt, bool fNoErrIfNotFound, int iFlags)
Definition: C4FacetEx.cpp:84
bool Create(int iWdt, int iHgt, int iWdt2=C4FCT_Full, int iHgt2=C4FCT_Full)
Definition: C4FacetEx.cpp:54
void Clear()
Definition: C4FacetEx.h:44
bool CreateClrByOwner(C4Surface *pBySurface)
Definition: C4FacetEx.cpp:73
void Set(const C4Facet &cpy)
Definition: C4FacetEx.h:46
bool CopyFromSfcMaxSize(C4Surface &srcSfc, int32_t iMaxSize, uint32_t dwColor=0u)
Definition: C4FacetEx.cpp:117
bool EnsureOwnSurface()
Definition: C4FacetEx.cpp:168
void Grayscale(int32_t iOffset=0)
Definition: C4FacetEx.cpp:162
bool FindEntry(const char *wildcard, StdStrBuf *filename=nullptr, size_t *size=nullptr)
Definition: C4Group.cpp:2211
Definition: C4Rect.h:28
int32_t y
Definition: C4Rect.h:30
int32_t Hgt
Definition: C4Rect.h:30
int32_t Wdt
Definition: C4Rect.h:30
int32_t x
Definition: C4Rect.h:30
bool CreateColorByOwner(C4Surface *pBySurface)
Definition: C4Surface.cpp:255
bool Copy(C4Surface &fromSfc)
Definition: C4Surface.cpp:184
bool Load(C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags)
int Wdt
Definition: C4Surface.h:65
void SetClr(DWORD toClr)
Definition: C4Surface.h:132
bool Create(int iWdt, int iHgt, int iFlags=0)
Definition: C4Surface.cpp:161
int Hgt
Definition: C4Surface.h:65
void Default()
Definition: C4Surface.cpp:71
float TargetY
Definition: C4Facet.h:165
void SetRect(C4TargetRect &rSrc)
Definition: C4FacetEx.cpp:44
float TargetX
Definition: C4Facet.h:165
float Zoom
Definition: C4Facet.h:165
float ParRefY
Definition: C4Facet.h:177
void Set(const C4Facet &cpy)
Definition: C4Facet.h:182
float ParRefX
Definition: C4Facet.h:177
int32_t tx
Definition: C4Rect.h:79
int32_t ty
Definition: C4Rect.h:79