aboutsummaryrefslogtreecommitdiff
path: root/engines/testbed/graphics.cpp
blob: f79a8f98a11269a39c614391baef0188c5b8bb7a (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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#include "common/events.h"

#include "testbed/graphics.h"
#include "testbed/testsuite.h"

#include "graphics/cursorman.h"
#include "graphics/fontman.h"
#include "graphics/surface.h"
#include "graphics/VectorRendererSpec.h"

namespace Testbed {

byte GFXTestSuite::_palette[3 * 4] = {0, 0, 0, 0, 255, 255, 255, 0, 255, 255, 255, 0};

GFXTestSuite::GFXTestSuite() {
	// Initialize color palettes
	// The fourth field is for alpha channel which is unused
	// Assuming 8bpp as of now
	g_system->setPalette(_palette, 0, 3);
	g_system->grabPalette(_palette, 0, 3);
	
	// Add tests here
	
	// Blitting buffer on screen
	// addTest("BlitBitmaps", &GFXtests::copyRectToScreen);
	
	// GFX Transcations
	// addTest("FullScreenMode", &GFXtests::fullScreenMode);
	// addTest("AspectRatio", &GFXtests::aspectRatio);
	// addTest("IconifyingWindow", &GFXtests::iconifyWindow);
	
	// Mouse Layer tests (Palettes and movements)
	addTest("PalettizedCursors", &GFXtests::palettizedCursors);
	// TODO: need to fix it
	addTest("ScaledCursors", &GFXtests::scaledCursors);
	
	// Effects
	// addTest("shakingEffect", &GFXtests::shakingEffect);
	// addTest("focusRectangle", &GFXtests::focusRectangle);

	// TODO: unable to notice any change, make it noticable
	addTest("Overlays", &GFXtests::overlayGraphics);
}

const char *GFXTestSuite::getName() const {
	return "GFX";
}

void GFXTestSuite::setCustomColor(uint r, uint g, uint b) {
	_palette[8] = r; 
	_palette[9] = g;
	_palette[10] = b;
	g_system->setPalette(_palette, 0, 3);
	g_system->grabPalette(_palette, 0, 3);
}

// Helper functions used by GFX tests

void GFXtests::drawCursor(bool cursorPaletteDisabled, const char *gfxModeName, int cursorTargetScale) {	
	byte palette[3 * 4]; // Black, white and yellow
	byte buffer[11][11];
	
	palette[0] = palette[1] = palette[2] = 0;
	palette[4] = palette[5] = palette[6] = 255;
	palette[8] = palette[9] = 255;
	palette[10] = 0;
		
	memset(&buffer[0][0], 2, 11 * 11);
	CursorMan.pushCursorPalette(palette, 0, 3);
	
	// Mark the hotspot
	for (int i = 0; i < 11; i++) {
		buffer[i][i] = 0;
		buffer[10 - i][i] = 0;
	}
	
	CursorMan.pushCursor(&buffer[0][0], 11, 11, 5, 5, 1);
	CursorMan.showMouse(true);
	
	if (cursorPaletteDisabled) {
		CursorMan.disableCursorPalette(true);
	}
	
	g_system->updateScreen();

	if (gfxModeName) {
		Common::Point pt(0, 100);
		char scaleFactor[10];
		snprintf(scaleFactor, 10, "%dx", cursorTargetScale);
		Common::String info = "GFX Mode:";
		info = info + gfxModeName  + " Cursor scaled by:" + scaleFactor;
		Testsuite::clearScreen();
		Testsuite::writeOnScreen(info, pt);
	}
}

/**
 * Sets up mouse loop, exits when user clicks any of the mouse button
 */
void GFXtests::setupMouseLoop(bool disableCursorPalette, const char *gfxModeName, int cursorTargetScale) {

	bool isFeaturePresent;
	isFeaturePresent = g_system->hasFeature(OSystem::kFeatureCursorHasPalette);

	if (isFeaturePresent) {
		
		GFXtests::drawCursor(disableCursorPalette, gfxModeName, cursorTargetScale);

		Common::EventManager *eventMan = g_system->getEventManager();
		Common::Event event;
		Common::Point pt(0, 100);

		bool quitLoop = false;
		uint32 lastRedraw = 0;
		const uint32 waitTime = 1000 / 45;	
		
		Testsuite::clearScreen();
		Common::String info = disableCursorPalette ? "Using Game Palette" : "Using cursor palette";
		info += "to render the cursor, Click to finish";
		
		Testsuite::writeOnScreen(info, pt);

		while (!quitLoop) {
			while (eventMan->pollEvent(event)) {
	
				if (lastRedraw + waitTime < g_system->getMillis()) {
					g_system->updateScreen();
					lastRedraw = g_system->getMillis();
				}

				switch (event.type) {
				case Common::EVENT_MOUSEMOVE:
					break;
				case Common::EVENT_LBUTTONDOWN:
				case Common::EVENT_RBUTTONDOWN:
					Testsuite::clearScreen();
					Testsuite::writeOnScreen("Mouse Clicked", pt);
					printf("Mouse Clicked\n");
					g_system->delayMillis(1000);
					quitLoop = true;
					Testsuite::clearScreen();
					Testsuite::writeOnScreen("TestFinished", pt);
					g_system->delayMillis(1000);
					break;
				default:	
					;// Ignore any other event

				}
			}
		}
	} else {
		Testsuite::displayMessage("feature not supported");
	}
}

void GFXtests::mouseMovements() {
	// Testing Mouse Movements now!
	Common::Point pt(0, 100);
	Testsuite::writeOnScreen("Moving mouse automatically from (0, 0) to (100, 100)", pt);
	g_system->warpMouse(0, 0);
	g_system->updateScreen();
	g_system->delayMillis(1000);

	for (int i = 0; i <= 100; i++) {
		g_system->delayMillis(20);
		g_system->warpMouse(i, i);
		g_system->updateScreen();
	}
	
	Testsuite::clearScreen();
	Testsuite::writeOnScreen("Mouse Moved to (100, 100)", pt);
	g_system->delayMillis(1000);	
}

void GFXtests::unsetMouse() {
	// Popping cursor
	CursorMan.popCursorPalette();
	CursorMan.popCursor();
}

/**
 * Used by aspectRatio()
 */

void GFXtests::drawEllipse(int cx, int cy, int a, int b) {	
	
	// Take a buffer of screen size

	byte buffer[200][320] = {{0}};
	float theta;
	int x, y, x1, y1;

	// Illuminate the center
	buffer[cx][cy] = 1;
	
	// Illuminate the points lying on ellipse

	for (theta = 0; theta <= PI / 2; theta += PI / 360  ) {
		x = (int)(b * sin(theta) + 0.5);
		y = (int)(a * cos(theta) + 0.5);
		
		// This gives us four points
		
		x1 = x + cx;
		y1 = y + cy;
		
		buffer[x1][y1] = 1;

		x1 = (-1) * x + cx;
		y1 = y + cy;
		
		buffer[x1][y1] = 1;
		
		x1 = x + cx;
		y1 = (-1) * y + cy;
		
		buffer[x1][y1] = 1;

		x1 = (-1) * x + cx;
		y1 = (-1) * y + cy;
		
		buffer[x1][y1] = 1;
	}

	g_system->copyRectToScreen(&buffer[0][0], 320, 0, 0, 320, 200);
	g_system->updateScreen();
}

// GFXtests go here

/**
 * Tests the fullscreen mode by: toggling between fullscreen and windowed mode
 */

bool GFXtests::fullScreenMode() {
	
	Common::Point pt(0, 100);
	Common::Rect rect = Testsuite::writeOnScreen("Testing fullscreen mode", pt);
	
	bool isFeaturePresent;
	bool isFeatureEnabled;
	bool passed = true;
	Common::String prompt;
	OptionSelected shouldSelect;

	isFeaturePresent = g_system->hasFeature(OSystem::kFeatureFullscreenMode);

	if (isFeaturePresent) {
		// Toggle
		isFeatureEnabled = g_system->getFeatureState(OSystem::kFeatureFullscreenMode);
		shouldSelect = isFeatureEnabled ? kOptionLeft : kOptionRight;
		
		g_system->delayMillis(1000);
		
		if (isFeatureEnabled) {
			printf("LOG: Current Mode is Fullsecreen\n");
		} else {
			printf("LOG: Current Mode is Windowed\n");
		}

		prompt = " Which mode do you see currently ?  ";
		
		if (!Testsuite::handleInteractiveInput(prompt, "Fullscreen", "Windowed", shouldSelect)) {
			// User selected incorrect current state
			passed = false;
			printf("LOG: g_system->getFeatureState() failed\n");
		}

		g_system->beginGFXTransaction();
		g_system->setFeatureState(OSystem::kFeatureFullscreenMode, !isFeatureEnabled);
		g_system->endGFXTransaction();

		// Current state should be now !isFeatureEnabled
		isFeatureEnabled = g_system->getFeatureState(OSystem::kFeatureFullscreenMode);
		shouldSelect = isFeatureEnabled ? kOptionLeft : kOptionRight;
			
		g_system->delayMillis(1000);
		
		prompt = "  Which screen mode do you see now ?   ";
		
		if (!Testsuite::handleInteractiveInput(prompt, "Fullscreen", "Windowed", shouldSelect)) {
			// User selected incorrect mode
			passed = false;
			printf("LOG: g_system->setFeatureState() failed\n");
		}
		
		g_system->beginGFXTransaction();
		g_system->setFeatureState(OSystem::kFeatureFullscreenMode, !isFeatureEnabled);
		g_system->endGFXTransaction();
		
		g_system->delayMillis(1000);
		
		prompt = "This should be your initial state. Is it?";
		
		if (!Testsuite::handleInteractiveInput(prompt, "Yes, it is", "Nopes", shouldSelect)) {
			// User selected incorrect mode
			printf("LOG: switching back to initial state failed\n");
			passed = false;
		}

	} else {
		Testsuite::displayMessage("feature not supported");
	}

	Testsuite::clearScreen();
	return passed;
}

/**
 * Tests the aspect ratio correction by: drawing an ellipse, when corrected the ellipse should render to a circle
 */

bool GFXtests::aspectRatio() {
	// Draw an ellipse on the screen
	
	drawEllipse(100, 160, 72, 60);
	
	Common::Point pt(0, 180);
	Testsuite::writeOnScreen("Testing Aspect Ratio Correction!", pt);
	
	bool isFeaturePresent;
	bool isFeatureEnabled;
	bool passed;
	Common::String prompt;
	OptionSelected shouldSelect;

	isFeaturePresent = g_system->hasFeature(OSystem::kFeatureAspectRatioCorrection);
	isFeatureEnabled = g_system->getFeatureState(OSystem::kFeatureAspectRatioCorrection);
	g_system->delayMillis(1000);

	if (isFeaturePresent) {
		// Toggle
		shouldSelect = isFeatureEnabled ? kOptionLeft : kOptionRight;
		prompt = " What does the curve on screen appears to you ?";
		if (!Testsuite::handleInteractiveInput(prompt, "Circle", "Ellipse", shouldSelect)) {
			// User selected incorrect option
			passed = false;
			printf("LOG: Aspect Ratio Correction failed\n");
		}
		
		g_system->beginGFXTransaction();
		g_system->setFeatureState(OSystem::kFeatureAspectRatioCorrection, !isFeatureEnabled);
		g_system->endGFXTransaction();
		
		g_system->delayMillis(1000);
		
		shouldSelect = !isFeatureEnabled ? kOptionLeft : kOptionRight;
		prompt = " What does the curve on screen appears to you ?";
		if (!Testsuite::handleInteractiveInput(prompt, "Circle", "Ellipse", shouldSelect)) {
			// User selected incorrect option
			passed = false;
			printf("LOG: Aspect Ratio Correction failed\n");
		}
		
		g_system->beginGFXTransaction();
		g_system->setFeatureState(OSystem::kFeatureAspectRatioCorrection, isFeatureEnabled);
		g_system->endGFXTransaction();
	} else {
		Testsuite::displayMessage("feature not supported");
	}

	g_system->delayMillis(500);
	Testsuite::clearScreen();
	return passed;
}

/**
 * Tests Palettized cursors.
 * Method: Create a yellow colored cursor, should be able to move it. Once you click test terminates
 */

bool GFXtests::palettizedCursors() {
	
	bool passed = true;
	
	Testsuite::displayMessage("Testing Cursors. You should expect to see a yellow colored square cursor.\n"
	"You should be able to move it. The test finishes when the mouse(L/R) is clicked");
	
	// Testing with cursor Palette
	setupMouseLoop();
	// Test Automated Mouse movements (warp)
	mouseMovements();
	// done. Pop cursor now
	unsetMouse();
	
	if (Testsuite::handleInteractiveInput("Which color did the cursor appeared to you?", "Yellow", "Any other", kOptionRight)) {
		printf("LOG: Couldn't use cursor palette for rendering cursor\n");
		passed = false;
	}	

	// Testing with game Palette
	GFXTestSuite::setCustomColor(255, 0, 0);
	setupMouseLoop(true);
	// done. Pop cursor now
	unsetMouse();
	
	if (Testsuite::handleInteractiveInput("Which color did the cursor appeared to you?", "Red", "Any other", kOptionRight)) {
		printf("LOG: Couldn't use Game palette for rendering cursor\n");
		passed = false;
	}	
	g_system->delayMillis(1000);

	if (!Testsuite::handleInteractiveInput("Did Cursor tests went as you were expecting?")) {
		passed = false;
	}
	return passed;
}


/**
 * This basically blits the screen by the contents of its buffer.
 *
 */
bool GFXtests::copyRectToScreen() {
	Testsuite::displayMessage("Testing Blitting a Bitmap to screen.\n"
	"You should expect to see a 20x40 yellow horizontal rectangle centred at the screen.");

	GFXTestSuite::setCustomColor(255, 255, 0);
	byte buffer[20 * 40];
	memset(buffer, 2, 20 * 40);

	uint x = g_system->getWidth() / 2 - 20;
	uint y = g_system->getHeight() / 2 - 10;

	g_system->copyRectToScreen(buffer, 40, x, y, 40, 20);
	g_system->updateScreen();
	g_system->delayMillis(1000);

	Common::Rect rect(x, y, x+40, y+20);
	Testsuite::clearScreen(rect);
	
	if (Testsuite::handleInteractiveInput("Did the test worked as you were expecting?", "Yes", "No", kOptionRight)) {
		return false;
	}

	return true;

}

/**
 * Testing feature : Iconifying window
 * It is expected the screen minimizes when this feature is enabled 
 */
bool GFXtests::iconifyWindow() {
	
	Testsuite::displayMessage("Testing Iconify Window mode.\n If the feature is supported by the backend,"
	"you should expect the window to be minimized. However you would manually need to de-iconify.");

	Common::Point pt(0, 100);
	Common::Rect rect = Testsuite::writeOnScreen("Testing Iconifying window", pt);
	
	bool isFeaturePresent;
	bool isFeatureEnabled;

	isFeaturePresent = g_system->hasFeature(OSystem::kFeatureIconifyWindow);
	isFeatureEnabled = g_system->getFeatureState(OSystem::kFeatureIconifyWindow);
	g_system->delayMillis(1000);

	if (isFeaturePresent) {
		// Toggle

		g_system->beginGFXTransaction();
		g_system->setFeatureState(OSystem::kFeatureIconifyWindow, !isFeatureEnabled);
		g_system->endGFXTransaction();

		g_system->delayMillis(1000);
		
		g_system->beginGFXTransaction();
		g_system->setFeatureState(OSystem::kFeatureIconifyWindow, isFeatureEnabled);
		g_system->endGFXTransaction();
	} else {
		Testsuite::displayMessage("feature not supported");
	}
	
	if (Testsuite::handleInteractiveInput("Did the test worked as you were expecting?", "Yes", "No", kOptionRight)) {
		return false;
	}

	Testsuite::clearScreen(rect);
	return true;
}

/**
 * Testing feature: Scaled cursors
 */

bool GFXtests::scaledCursors() {

	// TODO : Understand and fix the problem relating scaled cursors

	const OSystem::GraphicsMode *gfxMode = g_system->getSupportedGraphicsModes();
	while (gfxMode->name) {
		// for every graphics mode display cursors for cursorTargetScale 1, 2 and 3
		// Switch Graphics mode
		// FIXME: Doesn't works:
		// if (g_system->setGraphicsMode(gfxMode->id)) {
		if (1) {
			g_system->updateScreen();
			
			setupMouseLoop(false, gfxMode->name, 1);
			unsetMouse();
			
			setupMouseLoop(false, gfxMode->name, 2);
			unsetMouse();
			
			setupMouseLoop(false, gfxMode->name, 3);
			unsetMouse();
		
		} else {
			printf("Switching to graphics mode %s failed\n", gfxMode->name);
		}
		CursorMan.popAllCursors();
		gfxMode++;
	}

	return true;
}

bool GFXtests::shakingEffect() {
	Common::Point pt(0, 100);
	Testsuite::writeOnScreen("If Shaking effect works,this should shake!", pt);
	int times = 35;
	while (times--) {
		g_system->setShakePos(10);
		g_system->updateScreen();
		g_system->setShakePos(0);
		g_system->updateScreen();
	}
	g_system->delayMillis(1500);

	if (Testsuite::handleInteractiveInput("Did the test worked as you were expecting?", "Yes", "No", kOptionRight)) {
		printf("LOG: Shaking  Effect didn't worked");
		return false;
	}
	Testsuite::clearScreen();
	return true;
}

bool GFXtests::focusRectangle() {

	Testsuite::displayMessage("Testing : Setting and hiding Focus \n"
	"If this feature is implemented, the focus should be toggled between the two rectangles on the corners");
	Testsuite::clearScreen();

	const Graphics::Font &font(*FontMan.getFontByUsage(Graphics::FontManager::kConsoleFont));

	Graphics::Surface *screen = g_system->lockScreen();
	int screenHeight = g_system->getHeight();
	int screenWidth = g_system->getWidth();

	int height = font.getFontHeight();
	int width = screenWidth / 2;

	Common::Rect rectLeft(0, 0, width, height * 2);
	screen->fillRect(rectLeft, kColorWhite);
	font.drawString(screen, "Focus 1", rectLeft.left, rectLeft.top, width, kColorBlack, Graphics::kTextAlignLeft);

	Common::Rect rectRight(screenWidth - width, screenHeight - height * 2 , screenWidth, screenHeight);
	screen->fillRect(rectRight, kColorWhite);
	font.drawString(screen, "Focus 2", rectRight.left, rectRight.top, width, kColorBlack, Graphics::kTextAlignRight);
	g_system->unlockScreen();
	g_system->updateScreen();

	g_system->clearFocusRectangle();

	g_system->setFocusRectangle(rectLeft);
	g_system->updateScreen();
	
	g_system->delayMillis(1000);

	g_system->setFocusRectangle(rectRight);
	g_system->updateScreen();
	
	g_system->clearFocusRectangle();

	if (Testsuite::handleInteractiveInput("Did you noticed a variation in focus?", "Yes", "No", kOptionRight)) {
		printf("LOG: Focus Rectangle feature doesn't works. Check platform.\n");
	}

	return true;
}

bool GFXtests::overlayGraphics() {
	
	// TODO: ifind out if this is required?
	g_system->beginGFXTransaction();
	g_system->initSize(640, 400);
	g_system->endGFXTransaction();


	Graphics::PixelFormat pf = g_system->getOverlayFormat();
	
	OverlayColor buffer[20 * 40];
	OverlayColor value = pf.RGBToColor(0, 255, 0);

	for (int i = 0; i < 20 * 40; i++) {
		buffer[i] = value;
	}
	
	// FIXME: Not Working.
	g_system->copyRectToOverlay(buffer, 40, 100, 100, 40, 20);
	g_system->showOverlay();
	g_system->updateScreen();
	
	g_system->delayMillis(1000);
	
	g_system->hideOverlay();
	g_system->updateScreen();

	if (Testsuite::handleInteractiveInput("Did you see a green overlayed rectangle?", "Yes", "No", kOptionRight)) {
		printf("LOG: Overlay Rectangle feature doesn't works\n");
		return false;
	}
	return true;
}

}