forked from lucidspriter/SpriterPlusPlus
/
cf25imagefile.cpp
171 lines (149 loc) · 5.72 KB
/
cf25imagefile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "cf25imagefile.h"
#include "spriterengine/global/settings.h"
#include "spriterengine/objectinfo/universalobjectinterface.h"
namespace SpriterEngine
{
Cf25ImageFile::Cf25ImageFile(std::string initialFilePath, point initialDefaultPivot, LPRDATA rdPtr, Extension *ext) :
ImageFile(initialFilePath,initialDefaultPivot),
rdPtr(rdPtr), ext(ext)
{
}
Cf25ImageFile::~Cf25ImageFile()
{
if (sprite.IsValid())
{
sprite.Delete();
}
}
void Cf25ImageFile::renderSprite(UniversalObjectInterface * spriteInfo)
{
//if not loaded yet and sprite source is not empty and the corresponding sprite exists, load the sprite
if (ext->SpriteSource.size()>0 && ext->SpriteSource.count(path()) && !ext->SpriteSource[path()].loaded)
{
cSurface source;
//if external sprite, load it into surface
if (ext->SpriteSource[path()].external)
{
std::wstring ws;
std::wstring fullPath;
string filepath = path();
ws.assign(filepath.begin(), filepath.end());
fullPath = ext->SpriteSource[path()].path;
if (ext->LoadImageFile(rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv, sprite, fullPath))
{
ext->SpriteSource[path()].loaded = true;
}
else
{
//set error
}
}
//if the sprite from the active object is available and exists, clone it and release active object sprite
else if (LockImageSurface(ext->SpriteSource[path()].pObj->roHo.hoAdRunHeader->rhIdAppli, ext->SpriteSource[path()].imageNumber, source))
{
sprite.Clone(source);
ext->SpriteSource[path()].loaded = true;
UnlockImageSurface(source);
}
}
int w = sprite.GetWidth();
int h = sprite.GetHeight();
float posX = spriteInfo->getPivot().x*w;
float posY = spriteInfo->getPivot().y*h;
POINT center = { (LONG)posX, (LONG)posY };
DWORD flags = rdPtr->rs->rsEffect & EFFECTFLAG_ANTIALIAS ? (STRF_RESAMPLE_TRANSP) : 0UL;
LPSURFACE psw = WinGetSurface((int)rdPtr->rHo.hoAdRunHeader->rhIdEditWin);
double alpha = spriteInfo->getAlpha();
DWORD dwEffect = rdPtr->rs->rsEffect;// dwEffect et dwEffectParam = values to pass to BlitEx
DWORD dwEffectParam = rdPtr->rs->rsEffectParam;
DWORD dwRGBAOld = 0;// to restore old alpha value
CEffectEx* pEffectEx = NULL;
if (alpha != 1.0)
{
DWORD objectAlpha = 255;
switch (dwEffect & BOP_MASK) {
// Shader -> RGBA coefficient is stored in the effect
case BOP_EFFECTEX:
{
pEffectEx = (CEffectEx*)dwEffectParam;
if (pEffectEx != NULL)
{
// Get alpha coef
dwRGBAOld = pEffectEx->GetRGBA();
objectAlpha = (dwRGBAOld >> 24);
// Apply sprite alpha
objectAlpha = (DWORD)(objectAlpha * alpha);
// Modify effect alpha
pEffectEx->SetRGBA((dwRGBAOld & 0x00FFFFFF) | (objectAlpha << 24));
}
}
break;
// Legacy semi-transparency => the old semitransparency coefficient is in effectParam
case BOP_BLEND:
// Convert legacy semi-transp coef into alpha coef
#define SEMITRANSPTOALPHA(s) ((s==128) ? 0:(255-s*2))
objectAlpha = SEMITRANSPTOALPHA(dwEffectParam);
// Apply sprite alpha
objectAlpha = (DWORD)(objectAlpha * alpha);
// Store as RGBA coefficient in effectParam
dwEffect &= ~BOP_MASK;
dwEffect |= (BOP_COPY | BOP_RGBAFILTER);
dwEffectParam = (0x00FFFFFF | (objectAlpha << 24));
break;
// Other effect
default:
// effectParam = RGBA coef?
if (dwEffect & BOP_RGBAFILTER)
{
// Get alpha
objectAlpha = (((DWORD)dwEffectParam) >> 24);
// Apply sprite alpha
objectAlpha = (DWORD)(objectAlpha * alpha);
// Set alpha in RGBA coef
dwEffectParam = ((dwEffectParam & 0x00FFFFFF) | (objectAlpha << 24));
}
// No
else
{
// Apply alpha to 255
objectAlpha *= alpha;
// Store RGBA coef in effectParam
dwEffectParam = (0x00FFFFFF | (objectAlpha << 24));
dwEffect |= BOP_RGBAFILTER;
}
break;
}
}
// Blit sprite
sprite.BlitEx(*psw, spriteInfo->getPosition().x, spriteInfo->getPosition().y,
spriteInfo->getScale().x, spriteInfo->getScale().y, 0, 0, w, h, ¢er, (float)toDegrees(spriteInfo->getAngle()),
(dwEffect & EFFECTFLAG_TRANSPARENT) ? BMODE_TRANSP : BMODE_OPAQUE,
BlitOp(dwEffect & EFFECT_MASK),dwEffectParam, flags);
// Restore effect alpha if it was modified
if (pEffectEx != NULL)
pEffectEx->SetRGBA(dwRGBAOld);
//calculate display rectangle
RECT minR = { 0, 0, 0, 0 };
POINT inPoints[4];
POINT outPoints[4];
inPoints[0] = { -center.x, -center.y };
inPoints[1] = { -center.x, (1 - spriteInfo->getPivot().y)*h };
inPoints[2] = { (1 - spriteInfo->getPivot().x)*w, -center.y };
inPoints[3] = { (1 - spriteInfo->getPivot().x)*w, (1 - spriteInfo->getPivot().y)*h };
for (int i = 0; i < 4; i++)
{
outPoints[i].x = (inPoints[i].x* spriteInfo->getScale().x * std::cos(spriteInfo->getAngle())) + (inPoints[i].y * spriteInfo->getScale().y * std::sin(spriteInfo->getAngle()));
outPoints[i].y = (-inPoints[i].x * spriteInfo->getScale().x * std::sin(spriteInfo->getAngle())) + (inPoints[i].y * spriteInfo->getScale().y * std::cos(spriteInfo->getAngle()));
outPoints[i].x += spriteInfo->getPosition().x;
outPoints[i].y += spriteInfo->getPosition().y;
}
minR.left = min(min(min(outPoints[0].x, outPoints[1].x), outPoints[2].x), outPoints[3].x);
minR.right = max(max(max(outPoints[0].x, outPoints[1].x), outPoints[2].x), outPoints[3].x);
minR.top = min(min(min(outPoints[0].y, outPoints[1].y), outPoints[2].y), outPoints[3].y);
minR.bottom = max(max(max(outPoints[0].y, outPoints[1].y), outPoints[2].y), outPoints[3].y);
ext->displayRect.left = min(ext->displayRect.left, minR.left);
ext->displayRect.right = max(ext->displayRect.right, minR.right);
ext->displayRect.bottom = max(ext->displayRect.bottom, minR.bottom);
ext->displayRect.top = min(ext->displayRect.top, minR.top);
}
}