File: Surface.cpp

package info (click to toggle)
amphetamine 0.8.10-13
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 604 kB
  • ctags: 1,601
  • sloc: cpp: 9,020; makefile: 145; sh: 31
file content (259 lines) | stat: -rw-r--r-- 6,217 bytes parent folder | download | duplicates (6)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "Surface.hpp"
#include "Shape.hpp"
#include "ShapeLd.hpp"
#include "Clut.hpp"
#include "ConstVal.hpp"
#include <memory.h>

const	short	kNoCharSpace = 20;
const	char	kNoCharChar = '_';

extern	CSystem	*gSystem;
extern	CShapeManager *gShapeManager;
extern	CClutManager	*gClutManager;
extern	tConstValues	*gConst;

CGraphicSurface::CGraphicSurface(short dx, short dy)
{
	buffer = gSystem->AllocateBuffer(dx, dy);
	gSystem->SetBufferPalette(buffer);

	width = dx;
	height = dy;

	numGraphics = 0;
}

CGraphicSurface::~CGraphicSurface()
{
	for (short n = 0; n < numGraphics; n ++) {
		if (graphicTypes[n] == kGraphicEmbedded) free_graphic_file(graphics[n]);
	}
	gSystem->DisposeBuffer(buffer);
}

unsigned char	*CGraphicSurface::GetSurfacePtr(short *pitch)
{
	unsigned char	*tmp = gSystem->GetBufferPtr(buffer, pitch);

	return tmp;
}

void	CGraphicSurface::ReleaseSurface()
{
	gSystem->ReleaseBufferPtr(buffer);
}

void	CGraphicSurface::InsertGraphic(char *filename, Graphic_file *graphic, tRect *position)
{
	if (filename) {
		graphics[numGraphics] = read_graphic_file(filename);

		SwapBlackWhite(graphics[numGraphics]);

		graphicsPositions[numGraphics].left = 0;
		graphicsPositions[numGraphics].top = 0;
		graphicsPositions[numGraphics].right = graphics[numGraphics]->width;
		graphicsPositions[numGraphics].bottom = graphics[numGraphics]->height;
		graphicTypes[numGraphics] = kGraphicEmbedded;

		numGraphics ++;
	}
	if (graphic && position) {
		graphics[numGraphics] = graphic;

		graphicsPositions[numGraphics].left = position->left;
		graphicsPositions[numGraphics].top = position->top;
		graphicsPositions[numGraphics].right = position->right;
		graphicsPositions[numGraphics].bottom = position->bottom;
		graphicTypes[numGraphics] = kGraphicReferenced;

		numGraphics ++;
	}
}

void	CGraphicSurface::PaintGraphic(short num, short left, short top, short modus)
{
	short	j, k;
	short	bottom, right;
	unsigned char	*baseAddr, *linePtr, *sourcePtr, *sourceLinePtr;
	short	pitch;
	
	baseAddr = gSystem->GetBufferPtr(buffer, &pitch);

// Luke hat hier das -1 entfernt
	bottom = MIN(top + graphicsPositions[num].bottom - graphicsPositions[num].top, height);
	right = MIN(left + graphicsPositions[num].right - graphicsPositions[num].left, pitch);

	baseAddr += top * pitch + left;
	sourcePtr = graphics[num]->bitmap + graphicsPositions[num].top * graphics[num]->width + graphicsPositions[num].left;

	for (j = top; j < bottom; j ++) {
		k = right - left;
		
		sourceLinePtr = sourcePtr;
		linePtr = baseAddr;
		
		if (modus == kShapemodusNormal) {
			memcpy(linePtr, sourcePtr, k);
		} else {
			while (k) {
				gClutManager->SetPixel((unsigned char *)sourceLinePtr, (unsigned char *)linePtr, modus, 0);
				linePtr++;
				sourceLinePtr++;
				k--;
			}
		}
		baseAddr += pitch;
		sourcePtr += graphics[num]->width;
	}

	gSystem->ReleaseBufferPtr(buffer);
}



#define INT_TO_FIXED(i) ((i) << 16)
#define FIXED_TO_INT(f) ((f) >> 16)
#define FIXED_TO_FLOAT(f) (((double) (f)) * 1.52587890625e-5)
#define FLOAT_TO_FIXED(f) ((long) ((f) * 65536.0))


void	CGraphicSurface::DrawAntialiasedLine(short x1, short y1, short x2, short y2, unsigned char color, short modus)
{	
	unsigned char	*baseAddr;
	short	pitch;

	baseAddr = gSystem->GetBufferPtr(buffer, &pitch);

	x1 = MAX(0, x1);
	x1 = MIN(pitch, x1);
	x2 = MAX(0, x2);
	x2 = MIN(pitch, x2);
	y1 = MAX(0, y1);
	y1 = MIN(height -1, y1);
	y2 = MAX(0, y2);
	y2 = MIN(height -1, y2);

	short	dx = x2 - x1, dy = y2 - y1, dmax;
	long	ex, ey, curx = 0, cury = 0;
	long	lastx, lasty;
	short	counter;

	if (abs(dx) > abs(dy)) {
		ex = FLOAT_TO_FIXED(SIGN(dx) * 1.0);
		ey = FLOAT_TO_FIXED((double)dy / (double)abs(dx));
		counter = SIGN(dx) * dx;
	}else{
		ex = FLOAT_TO_FIXED((double)dx / (double)abs(dy));
		ey = FLOAT_TO_FIXED(SIGN(dy) * 1.0);
		counter = SIGN(dy) * dy;
	}
	curx = INT_TO_FIXED(x1);
	cury = INT_TO_FIXED(y1);

	lastx = INT_TO_FIXED(x1);
	lasty = INT_TO_FIXED(y1);



	baseAddr += (FIXED_TO_INT(lasty) * pitch + FIXED_TO_INT(lastx));

	while (counter) {
		gClutManager->SetPixel((unsigned char *)&color, baseAddr, modus, 0);

		curx += ex;
		cury += ey;
		counter --;

		if (lasty >> 16 != cury >> 16) {
			baseAddr += (SIGN(cury - lasty)) * pitch;
			lasty = cury;
		}
		if (lastx >> 16 != curx >> 16) {
			baseAddr += SIGN(curx - lastx);
			lastx = curx;
		}
	}
	gSystem->ReleaseBufferPtr(buffer);
}


void	CGraphicSurface::PaintRect(short left, short top, short right, short bottom, unsigned char color, short modus)
{
	short	j, k;
	unsigned char	*baseAddr;
	short	pitch;

	baseAddr = gSystem->GetBufferPtr(buffer, &pitch);

	left = MAX(0, left);
	top = MAX(0, top);
	left = MIN(pitch, left);
	top = MIN(height, top);

	right = MAX(0, right);
	bottom = MAX(0, bottom);
	right = MIN(pitch, right);
	bottom = MIN(height, bottom);


	baseAddr += top * pitch;

	for (j = top; j < bottom; j ++) {
		for (k = left; k < right; k ++) {
			gClutManager->SetPixel((unsigned char *)&color, baseAddr + k, modus, 0);
		}
		baseAddr += pitch;
	}

	gSystem->ReleaseBufferPtr(buffer);
}

void	CGraphicSurface::DrawString(short left, short top, char *text, short modus)
{
	short	n = 0, x, startWord, startShapePos;
	short	shapePos = left;
	short	dx, unused;
	CShape	*shape;
	unsigned char	*unused2;

	tRect	clipRect;

	clipRect.left = clipRect.top = 0;
	clipRect.right = width; clipRect.bottom = height;

	while (text && text[n] != '\0') {
		startWord = n;
		startShapePos = shapePos;
		x = n;
		while (text[x] != kNoCharChar && text[x] != '\0') {
			shape = gShapeManager->FindShape((short)text[x], 0);
			if (shape) unused2 = shape->AllowPixelAccess(dx, unused); else dx = 0;
			shapePos += dx;
			x ++;
		}
		if (shapePos >= width) {
			top += gConst->kTextYDistance;
			shapePos = left;
		}else shapePos = startShapePos;

		x = startWord;
		while (text[x] != kNoCharChar && text[x] != '\0') {
			shape = gShapeManager->FindShape((short)text[x], 0);
			if (shape) shapePos += shape->RenderShape(shapePos, top, &clipRect, modus, 0, this);
			x ++;
		}

		if (text[x] == kNoCharChar) {
			shapePos += kNoCharSpace;
			x ++;
		}
		n = x;
	}
}

void	CGraphicSurface::FlipToScreen(short left, short top)
{
	gSystem->FlipSurfaces(buffer, width, height, left, top);
}