Blob Blame Raw

#include <stdlib.h>

#include <helianthus.h>

#include "common.h"


static const double convexShape[][2] = {
	{  0, 10 },
	{  0,  3 },
	{  8,  0 },
	{ 10,  3 },
	{ 10, 10 } };

static const double nonConvexShape[][2] = {
	{  0,  0 },
	{ 10,  0 },
	{ 10,  3 },
	{  3,  3 },
	{  3,  5 },
	{  7,  5 },
	{  7,  7 },
	{  3,  7 },
	{  3, 10 },
	{  0, 10 } };


static int enableConvex = TRUE;
static int enableFill = TRUE;
static int enableStroke = TRUE;
static int enableTexture = TRUE;
static int enableStrokeTex = TRUE;
static int enableAA = TRUE;

static Animation anim;


static unsigned long long frameTimes[20];
static int frameIndex;


static void drawRandom(const double *shape, int count) {
	double h = windowGetHeight();
	double w = windowGetWidth();
	srand(0);
	for(int i = 0; i < 1000; ++i) {
		double size = 16 + randomFloat()*(h/2 - 16);
		double x = randomFloat()*(w - size) + size/2;
		double y = randomFloat()*(h - size) + size/2;
		double a = randomFloat()*360;
		
		saveState();
		translate(x, y);
		zoom(size/10);
		rotate(a);
		translate(-5, -5);
		moveTo(shape[0], shape[1]);
		for(int k = 2; k < count; k += 2)
			lineTo(shape[k], shape[k+1]);
		closePath();
		restoreState();
	}
}


static void drawGrid(const double *shape, int count) {
	saveState();
	const double s = 16;
	double angle = windowGetSeconds()*20;
	zoom(64/s);
	for(int i = 0; i < 8; ++i) {
		for(int j = 0; j < 16; ++j) {
			saveState();
			translate(s/2, s/2);
			rotate(angle);
			translate(3-s/2, 3-s/2);
			moveTo(shape[0], shape[1]);
			for(int k = 2; k < count; k += 2)
				lineTo(shape[k], shape[k+1]);
			closePath();
			restoreState();
			translate(s, 0);
		}
		translate(-16*s, s);
	}
	restoreState();
}


void benchmarkInit() {
	anim = createAnimationEx("data/sprite/bricks-tile.png", TRUE, TRUE, TRUE);
}


void benchmarkDraw() {
	saveState();
	noFill();
	noStroke();
	disableAntialiasing();
	if (enableFill) fill(colorByRGBA(1, 1, 1, 1));
	if (enableStroke) stroke(colorByRGBA(0.5, 0.5, 0.5, 1));
	if (enableTexture) fillTexture(anim, 0, 0, 16, 16, FALSE);
	if (enableStrokeTex) strokeTexture(anim, 0, 0, 16, 16, TRUE);
	if (enableAA) enableAntialiasing();
	
	const double *shape = enableConvex ? *convexShape : *nonConvexShape;
	int count = enableConvex ? (int)(sizeof(convexShape)/sizeof(**convexShape))
	                         : (int)(sizeof(nonConvexShape)/sizeof(**nonConvexShape));
	
	drawRandom(shape, count);
	drawGrid(shape, count);
	restoreState();
	
	int frameCount = (int)(sizeof(frameTimes)/sizeof(*frameTimes));
	unsigned long long t = windowGetMonotonicMilliseconds();
	unsigned long long dt = t - frameTimes[frameIndex];
	frameTimes[frameIndex] = t;
	frameIndex = (frameIndex + 1)%frameCount;
	double fps = dt > 0 ? frameCount*1000.0/(double)dt : 0.0;
	
	saveState();
	textFontDefault();
	textSize(16);
	textf(16, 16, "fps: %6.2f", fps);
	text( 16, 48,
		"c - convex\n"
		"f - fill\n"
		"s - stroke\n"
		"t - texture\n"
		"y - stroke texture\n"
		"a - antialiasing" );
	restoreState();
	
	if (keyWentDown("c")) enableConvex = !enableConvex;
	if (keyWentDown("f")) enableFill = !enableFill;
	if (keyWentDown("s")) enableStroke = !enableStroke;
	if (keyWentDown("t")) enableTexture = !enableTexture;
	if (keyWentDown("y")) enableStrokeTex = !enableStrokeTex;
	if (keyWentDown("a")) enableAA = !enableAA;
}