aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/gfx/image/vectorimagerenderer.cpp
blob: 1b7fe29cdfab7f6bdbc6add2b7ddedc3cbc595e3 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

/*
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

// -----------------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------------

#include "sword25/gfx/image/vectorimagerenderer.h"
#include "sword25/gfx/image/vectorimage.h"

#if 0 // TODO
#include "agg_conv_curve.h"
#include "agg_path_storage.h"
#include "agg_conv_stroke.h"
#endif

namespace Sword25 {

#if 0 // TODO
// -----------------------------------------------------------------------------
// CompoundShape
// -----------------------------------------------------------------------------

class CompoundShape {
public:
	CompoundShape(const BS_VectorImageElement &VectorImageElement) :
		m_ImageElement(VectorImageElement),
		m_Path(VectorImageElement.GetPaths()),
		m_Affine(),
		m_Curve(m_Path),
		m_Trans(m_Curve, m_Affine)
	{}

	unsigned operator [](unsigned i) const {
		return m_ImageElement.GetPathInfo(i).GetID();
	}

	unsigned paths() const {
		return m_ImageElement.GetPathCount();
	}

	void rewind(unsigned path_id) {
		m_Trans.rewind(path_id);
	}

	unsigned vertex(double *x, double *y) {
		return m_Trans.vertex(x, y);
	}

private:
	const BS_VectorImageElement                                &m_ImageElement;
	agg::path_storage                                           m_Path;
	agg::trans_affine                                           m_Affine;
	agg::conv_curve<agg::path_storage>                          m_Curve;
	agg::conv_transform< agg::conv_curve<agg::path_storage> >   m_Trans;
};


// -----------------------------------------------------------------------------
// StyleHandler
// -----------------------------------------------------------------------------

class StyleHandler {
public:
	StyleHandler(const BS_VectorImageElement &VectorImageElement) : m_ImageElement(VectorImageElement) {}

	bool is_solid(unsigned int style) const {
		return true;
	}

	const agg::rgba8 &color(unsigned style) const {
		return m_ImageElement.GetFillStyleColor(style);
	}

	void generate_span(agg::rgba8 *span, int x, int y, unsigned len, unsigned style) {
		// Wird nicht benutzt
		return;
	}

private:
	const BS_VectorImageElement &m_ImageElement;
};

BS_VectorImageRenderer::BS_VectorImageRenderer() :
	PixelFormat(rbuf) {

}


bool BS_VectorImageRenderer::Render(const BS_VectorImage &VectorImage,
                                    float ScaleFactorX, float ScaleFactorY,
                                    unsigned int &Width, unsigned int &Height,
                                    byte *ImageData,
                                    float LineScaleFactor,
                                    bool NoAlphaShapes) {
	Width = static_cast<unsigned int>(VectorImage.GetWidth() * ScaleFactorX);
	Height = static_cast<unsigned int>(VectorImage.GetHeight() * ScaleFactorY);

	ImageData.resize(Width * Height * 4);
	memset(&ImageData[0], 0, ImageData.size());
	rbuf.attach(reinterpret_cast<agg::int8u *>(&ImageData[0]), Width, Height, Width * 4);

	BaseRenderer.attach(PixelFormat);
	ScanlineRenderer.attach(BaseRenderer);

	// Die SWF-Shapes sind h�ufig nicht am Ursprung (0, 0) ausgerichtet, daher wird die Shape vor dem Rendern derart verschoben, dass
	// sich die linke obere Ecke der Bounding-Box im Ursprung befindet. Danach wird die Skalierung angewandt.
	Scale = agg::trans_affine_translation(- VectorImage.GetBoundingBox().left, - VectorImage.GetBoundingBox().top);
	Scale *= agg::trans_affine_scaling(ScaleFactorX, ScaleFactorY);

	for (unsigned int element = 0; element < VectorImage.GetElementCount(); ++element) {
		const BS_VectorImageElement &CurImageElement = VectorImage.GetElement(element);

		CompoundShape ImageCompoundShape(CurImageElement);
		StyleHandler ImageStyleHandler(CurImageElement);
		agg::conv_transform<CompoundShape> Shape(ImageCompoundShape, Scale);
		agg::conv_stroke<agg::conv_transform<CompoundShape> > Stroke(Shape);

		// Fill shape
		//----------------------
		CompoundRasterizer.clip_box(0, 0, Width, Height);
		CompoundRasterizer.reset();
		for (unsigned int i = 0; i < CurImageElement.GetPathCount(); ++i) {
			unsigned int FillStyle0 = CurImageElement.GetPathInfo(i).GetFillStyle0();
			unsigned int FillStyle1 = CurImageElement.GetPathInfo(i).GetFillStyle1();

			if (NoAlphaShapes) {
				if (FillStyle0 != 0 && CurImageElement.GetFillStyleColor(FillStyle0 - 1).a != 255) FillStyle0 = 0;
				if (FillStyle1 != 0 && CurImageElement.GetFillStyleColor(FillStyle1 - 1).a != 255) FillStyle1 = 0;
			}

			if (FillStyle0 != 0 || FillStyle1 != 0) {
				CompoundRasterizer.styles(FillStyle0 - 1, FillStyle1 - 1);
				CompoundRasterizer.add_path(Shape, CurImageElement.GetPathInfo(i).GetID());
			}
		}
		agg::render_scanlines_compound_layered(CompoundRasterizer, Scanline, BaseRenderer, Alloc, ImageStyleHandler);


		// Draw strokes
		//----------------------
		Rasterizer.clip_box(0, 0, Width, Height);
		Stroke.line_join(agg::round_join);
		Stroke.line_cap(agg::round_cap);
		for (unsigned int i = 0; i < CurImageElement.GetPathCount(); ++i) {
			Rasterizer.reset();

			unsigned int CurrentLineStyle = CurImageElement.GetPathInfo(i).GetLineStyle();
			if (CurrentLineStyle != 0) {
				Stroke.width(ScaleFactorX * CurImageElement.GetLineStyleWidth(CurrentLineStyle - 1) * LineScaleFactor);
				Rasterizer.add_path(Stroke, CurImageElement.GetPathInfo(i).GetID());
				ScanlineRenderer.color(CurImageElement.GetLineStyleColor(CurrentLineStyle - 1));
				// HACK
				// Die SWF-Frames enthalten zum Teil Reste von gr�nen Linien, die wohl von Bernd als Umriss benutzt wurden.
				// Damit diese Reste nicht st�rend auffallen werden gr�ne Linien schlichtweg ignoriert.
				if (!(CurImageElement.GetLineStyleColor(CurrentLineStyle - 1).a == 255 &&
				        CurImageElement.GetLineStyleColor(CurrentLineStyle - 1).r == 0 &&
				        CurImageElement.GetLineStyleColor(CurrentLineStyle - 1).g == 255 &&
				        CurImageElement.GetLineStyleColor(CurrentLineStyle - 1).b == 0))
					agg::render_scanlines(Rasterizer, Scanline, ScanlineRenderer);
			}
		}
	}

	return true;
}

#else

BS_VectorImageRenderer::BS_VectorImageRenderer() {}

bool BS_VectorImageRenderer::Render(const BS_VectorImage &VectorImage,
                                    float ScaleFactorX, float ScaleFactorY,
                                    unsigned int &Width, unsigned int &Height,
                                    byte *ImageData,
                                    float LineScaleFactor,
                                    bool NoAlphaShapes) {
	return true;
}
#endif

} // End of namespace Sword25