File: mouseimg.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (422 lines) | stat: -rw-r--r-- 11,796 bytes parent folder | download | duplicates (3)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* 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 3 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
 * aint32 with this program; if not, write to the Free Software
 *
 *
 * Based on the original sources
 *   Faery Tale II -- The Halls of the Dead
 *   (c) 1993-1996 The Wyrmkeep Entertainment Co.
 */

#include "saga2/saga2.h"
#include "saga2/blitters.h"
#include "saga2/fta.h"
#include "saga2/mouseimg.h"
#include "saga2/panel.h"

namespace Saga2 {

enum {
	gaugeWidth = 46,
	gaugeImageWidth = gaugeWidth + 2,
    gaugeImageHeight = 5,
    gaugeOneThird = gaugeWidth / 3,
    gaugeTwoThirds = gaugeWidth * 2 / 3
};

extern gFont *mainFont;

extern uint8 *closeBx1ImageData;
extern uint8 *closeBx2ImageData;
extern uint8 *usePtrImageData;
extern uint8 *xPointerImageData;
extern uint8 *arrowImageData;
extern uint8 *grabPtrImageData;
extern uint8 *attakPtrImageData;
extern uint8 *centerActorIndicatorImageData;
extern uint8 *pgUpImageData;
extern uint8 *pgDownImageData;
extern uint8 *pgLeftImageData;
extern uint8 *pgRightImageData;
extern uint8 *autoWalkImageData;
extern uint8 *gaugeImageData;


/* ===================================================================== *
   Globals
 * ===================================================================== */

const int maxMouseTextLen = 80;

static char mouseText[maxMouseTextLen] = { "" };  //  Current mouse text string

static StaticPoint16 mouseImageOffset = {0, 0};           //  Hotspot on mouse image

static gPixelMap  *mouseImage = nullptr;  //  Current mouse cursor image

static gPixelMap *textImage = nullptr;   //  Current mouse text image
static gPixelMap *combinedImage = nullptr;  //   Combine mouse text
//  and image

static int textImageCenteredCol;       //  The pixel column in the text
//  image to be centered under
//  the mouse pointer

static int gaugeNumerator,             //  Current mouse gauge values
       gaugeDenominator;

static uint8 gaugeGrayMap[7] =
{ 0x00, 0x18, 0x14, 0x12, 0x16, 0x17, 0x0E };   //  Gray

static uint8 gaugeColorMapArray[3][7] = {
	{ 0x00, 0x18, 0xE2, 0xD1, 0xE5, 0xE7, 0xC9 },   //  Green
	{ 0x00, 0x18, 0x5C, 0x5A, 0x5E, 0x60, 0x49 },   //  Yellow
	{ 0x00, 0x18, 0x83, 0x81, 0x86, 0x72, 0x39 },   //  Red
};

static struct MouseCurosrs {
	uint8 **data;
	int w;
	int h;
} mouseCursorData[] = {
	{ &closeBx1ImageData,	12, 12 },
	{ &closeBx2ImageData,	12, 12 },
	{ &usePtrImageData,		15, 15 },
	{ &xPointerImageData,	15, 15 },
	{ &arrowImageData,		11, 17 },
	{ &grabPtrImageData,	16, 13 },
	{ &attakPtrImageData,	23, 23 },
	{ &centerActorIndicatorImageData, 9, 9 },
	{ &pgUpImageData,		16, 16 },
	{ &pgDownImageData,		16, 16 },
	{ &pgLeftImageData,		16, 16 },
	{ &pgRightImageData,	16, 16 },
	{ &autoWalkImageData,	15, 15 },
	{ &gaugeImageData,		gaugeImageWidth, gaugeImageHeight }
};

gStaticImage *mouseCursors[kMouseMax];

static uint8 gaugeImageBuffer[gaugeImageWidth * gaugeImageHeight];
static gPixelMap *gaugeImage = nullptr;

static bool showGauge = false;

/* ===================================================================== *
   Functions
 * ===================================================================== */

//-----------------------------------------------------------------------
//	Create a new bitmap of several images stacked on top of each other

void initCursors() {
	for (int i = 0; i < kMouseMax; i++)
		mouseCursors[i] = new gStaticImage(mouseCursorData[i].w, mouseCursorData[i].h, *mouseCursorData[i].data);

	mouseImage = mouseCursors[kMouseArrowImage];
	gaugeImage = new gPixelMap;
	gaugeImage->_size.x = gaugeImageWidth;
	gaugeImage->_size.y = gaugeImageHeight;
	gaugeImage->_data = gaugeImageBuffer;

	textImage = new gPixelMap;
	combinedImage = new gPixelMap;
}

void freeCursors() {
	for (int i = 0; i < kMouseMax; i++)
		delete mouseCursors[i];

	delete gaugeImage;
	delete textImage;
	delete combinedImage;
}

void createStackedImage(gPixelMap **newImage, int *newImageCenter, gPixelMap **imageArray, int *imageCenterArray, int images) {
	assert(images != 0);

	if (*newImage)
		delete *newImage;

	*newImage = new gPixelMap;

	(*newImage)->_size.x = 0;
	(*newImage)->_size.y = 0;
	*newImageCenter = 0;

	for (int i = 0; i < images; i++) {
		if (imageCenterArray[i] > *newImageCenter)
			*newImageCenter = imageCenterArray[i];
	}

	for (int i = 0; i < images; i++) {
		int16  rightImageBoundary;

		(*newImage)->_size.y += imageArray[i]->_size.y;

		rightImageBoundary = *newImageCenter + (imageArray[i]->_size.x - imageCenterArray[i]);

		if (rightImageBoundary > (*newImage)->_size.x)
			(*newImage)->_size.x = rightImageBoundary;
	}

	(*newImage)->_size.y += images - 1;

	int newImageBytes = (*newImage)->bytes();

	(*newImage)->_data = (uint8 *)malloc(newImageBytes) ;

	memset((*newImage)->_data, 0, newImageBytes);

	int newImageRow = 0;
	for (int i = 0; i < images; i++) {
		gPixelMap *currentImage = imageArray[i];

		TBlit((*newImage), currentImage, *newImageCenter - imageCenterArray[i], newImageRow);

		newImageRow += currentImage->_size.y + 1;
	}
}

//-----------------------------------------------------------------------
//	Dispose of an image created with createStackedImage

inline void disposeStackedImage(gPixelMap **image) {
	if (*image) {
		free((*image)->_data);
		(*image)->_data = nullptr;

		delete *image;
		*image = nullptr;
	}
}

//-----------------------------------------------------------------------
//  Combine the mouse image and the mouse text image into a single
//  pixel map and reset the global mouse cursor to use the new combined
//	image.

void cleanupMousePointer() {
	if (combinedImage->_data != nullptr)
		disposeStackedImage(&combinedImage);
}

void setupMousePointer() {
	int  imageIndex = 1;
	gPixelMap  *imageArray[3];
	int imageCenterArray[3];
	Point16 imageOffset;            //  mouse image offset
	int combinedImageCenter;

	imageArray[0] = mouseImage;
	imageCenterArray[0] = mouseImage->_size.x / 2;

	if (mouseText[0] != '\0') {
		imageArray[imageIndex] = textImage;
		imageCenterArray[imageIndex] = textImageCenteredCol;
		imageIndex++;
	}

	if (showGauge) {
		imageArray[imageIndex] = gaugeImage;
		imageCenterArray[imageIndex] = gaugeImage->_size.x / 2;
		imageIndex++;
	}

	if (combinedImage->_data != nullptr)
		disposeStackedImage(&combinedImage);

	createStackedImage(&combinedImage, &combinedImageCenter, imageArray, imageCenterArray, imageIndex);

	imageOffset.x = combinedImageCenter - mouseImage->_size.x / 2;
	imageOffset.y = 0;

	//  Set the combined image as the new mouse cursor
	g_vm->_pointer->hide();
	g_vm->_pointer->setImage(*combinedImage, mouseImageOffset.x - imageOffset.x, mouseImageOffset.y - imageOffset.y);
	g_vm->_pointer->show();
}

//-----------------------------------------------------------------------
//	Setup a new mouse cursor image

void setMouseImage(int id, int16 x, int16 y) {
	setMouseImage(*mouseCursors[id], x, y);
}

void setMouseImage(gPixelMap &img, int16 x, int16 y) {
	if (mouseImage != &img
	        ||  mouseImageOffset.x != x
	        ||  mouseImageOffset.y != y) {
		mouseImage = &img;
		mouseImageOffset.x = x;
		mouseImageOffset.y = y;

		setupMousePointer();
	}
}

//-----------------------------------------------------------------------
//	Dispose of old text

inline void disposeText() {
	mouseText[0] = '\0';

	//  Free the memory previously allocated to hold the text image
	//  bitmap
	if (textImage == nullptr)
		return;

	if (textImage->_data != nullptr) {
		free(textImage->_data);
		textImage->_data = nullptr;
	}
}

//-----------------------------------------------------------------------
//	Build a new text image

void setNewText(char *text) {
	//  Dispose of old text image
	disposeText();

	Common::strlcpy(mouseText, text, maxMouseTextLen);

	//  Compute the size of the text bitmap
	textImage->_size.y = mainFont->height + 2;
	textImage->_size.x = TextWidth(mainFont, text, -1, 0) + 2;

	//  Allocate a new buffer for the text image bitmap
	int16 textImageBytes = textImage->bytes();

	textImage->_data = (uint8 *)malloc(textImageBytes);
	memset(textImage->_data, 0, textImageBytes);

	gPort textImagePort;  //  gPort used to draw text onto bitmap

	//  Intialize the text image port
	textImagePort.setMap(textImage);
	textImagePort.setMode(kDrawModeReplace);
	textImagePort.setColor(11);
	textImagePort.setOutlineColor(24);
	textImagePort.setFont(mainFont);
	textImagePort.setStyle(kTextStyleOutline);
	textImagePort.moveTo(1, 1);

	//  Draw the text
	textImagePort.drawText(text, -1);

	//  Compute relative position of text
	Point16 mousePos;
	g_vm->_pointer->getImageCurPos(mousePos);

	int mouseImageCenter = mousePos.x + mouseImageOffset.x + mouseImage->_size.x / 2;
	textImageCenteredCol = textImage->_size.x / 2;
	if (mouseImageCenter - textImageCenteredCol < 5) {
		textImageCenteredCol = mouseImageCenter - 5;
	} else if (mouseImageCenter + (textImage->_size.x - textImageCenteredCol) >= kScreenWidth - 5) {
		textImageCenteredCol = textImage->_size.x - ((kScreenWidth - 5) - mouseImageCenter);
	}
}

//-----------------------------------------------------------------------
//  Setup a new text string to display on the mouse cursor

void setMouseText(char *text) {
	if (text != nullptr) {
		if (strcmp(text, mouseText) == 0) return;

		setNewText(text);
		setupMousePointer();
	} else if (mouseText[0] != '\0') {
		disposeText();
		setupMousePointer();
	}
}

//  Formatted version of setMouseText, for gTools use...

void setMouseTextF(char *format, ...) {
	if (format == nullptr) {
		setMouseText(nullptr);
		g_vm->_toolBase->_mouseHintSet = true;
	} else {
		char        lineBuf[128];
		va_list     argptr;

		va_start(argptr, format);
		Common::vsprintf_s(lineBuf, format, argptr);
		va_end(argptr);

		setMouseText(lineBuf);
		g_vm->_toolBase->_mouseHintSet = true;
	}
}

//-----------------------------------------------------------------------
//	Set the gauge value for the mouse pointer (automatically turns
//	gauge on if necessary ).

void setMouseGauge(int numerator, int denominator) {
	assert(denominator != 0);
	assert(numerator <= denominator);

	if (showGauge && numerator == gaugeNumerator && denominator == gaugeDenominator)
		return;

	gaugeNumerator = numerator;
	gaugeDenominator = denominator;

	int gaugePos = (numerator * gaugeWidth + denominator - 1) / denominator;

	uint8 *gaugeColorMap;

	if (gaugePos <= gaugeOneThird)
		gaugeColorMap = gaugeColorMapArray[2];
	else if (gaugePos <= gaugeTwoThirds)
		gaugeColorMap = gaugeColorMapArray[1];
	else
		gaugeColorMap = gaugeColorMapArray[0];

	int gaugeImageIndex = 0;

	for (int y = 0; y < gaugeImageHeight; y++) {
		for (int x = 0; x < gaugeImageWidth; x++) {
			uint8 *gaugeMap = x < gaugePos + 1 ? gaugeColorMap : gaugeGrayMap;

			gaugeImageBuffer[gaugeImageIndex] = gaugeMap[mouseCursors[kMouseGaugeImage]->_data[gaugeImageIndex]];

			gaugeImageIndex++;
		}
	}

	showGauge = true;

	setupMousePointer();
}

//-----------------------------------------------------------------------
//	Turn off the gauge on the mouse pointer

void clearMouseGauge() {
	showGauge = false;

	setupMousePointer();
}

} // end of namespace Saga2