Blame demo/src/benchmark.c

b4b587
b4b587
#include <stdlib.h></stdlib.h>
b4b587
b4b587
#include <helianthus.h></helianthus.h>
b4b587
b4b587
#include "common.h"
b4b587
b4b587
b4b587
static const double convexShape[][2] = {
b4b587
	{  0, 10 },
b4b587
	{  0,  3 },
b4b587
	{  8,  0 },
b4b587
	{ 10,  3 },
b4b587
	{ 10, 10 } };
b4b587
b4b587
static const double nonConvexShape[][2] = {
b4b587
	{  0,  0 },
b4b587
	{ 10,  0 },
b4b587
	{ 10,  3 },
b4b587
	{  3,  3 },
b4b587
	{  3,  5 },
b4b587
	{  7,  5 },
b4b587
	{  7,  7 },
b4b587
	{  3,  7 },
b4b587
	{  3, 10 },
b4b587
	{  0, 10 } };
b4b587
b4b587
b4b587
static int enableConvex = TRUE;
b4b587
static int enableFill = TRUE;
b4b587
static int enableStroke = TRUE;
b4b587
static int enableTexture = TRUE;
b4b587
static int enableStrokeTex = TRUE;
b4b587
static int enableAA = TRUE;
b4b587
b4b587
static Animation anim;
b4b587
b4b587
b4b587
static unsigned long long frameTimes[20];
b4b587
static int frameIndex;
b4b587
b4b587
b4b587
static void drawRandom(const double *shape, int count) {
b4b587
	double h = windowGetHeight();
b4b587
	double w = windowGetWidth();
b4b587
	srand(0);
b4b587
	for(int i = 0; i < 1000; ++i) {
b4b587
		double size = 16 + randomFloat()*(h/2 - 16);
b4b587
		double x = randomFloat()*(w - size) + size/2;
b4b587
		double y = randomFloat()*(h - size) + size/2;
b4b587
		double a = randomFloat()*360;
b4b587
		
b4b587
		saveState();
b4b587
		translate(x, y);
b4b587
		zoom(size/10);
b4b587
		rotate(a);
b4b587
		translate(-5, -5);
b4b587
		moveTo(shape[0], shape[1]);
b4b587
		for(int k = 2; k < count; k += 2)
b4b587
			lineTo(shape[k], shape[k+1]);
b4b587
		closePath();
b4b587
		restoreState();
b4b587
	}
b4b587
}
b4b587
b4b587
b4b587
static void drawGrid(const double *shape, int count) {
b4b587
	saveState();
b4b587
	const double s = 16;
b4b587
	double angle = windowGetSeconds()*20;
b4b587
	zoom(64/s);
b4b587
	for(int i = 0; i < 8; ++i) {
b4b587
		for(int j = 0; j < 16; ++j) {
b4b587
			saveState();
b4b587
			translate(s/2, s/2);
b4b587
			rotate(angle);
b4b587
			translate(3-s/2, 3-s/2);
b4b587
			moveTo(shape[0], shape[1]);
b4b587
			for(int k = 2; k < count; k += 2)
b4b587
				lineTo(shape[k], shape[k+1]);
b4b587
			closePath();
b4b587
			restoreState();
b4b587
			translate(s, 0);
b4b587
		}
b4b587
		translate(-16*s, s);
b4b587
	}
b4b587
	restoreState();
b4b587
}
b4b587
b4b587
b4b587
void benchmarkInit() {
b4b587
	anim = createAnimationEx("data/sprite/bricks-tile.png", TRUE, TRUE, TRUE);
b4b587
}
b4b587
b4b587
b4b587
void benchmarkDraw() {
b4b587
	saveState();
b4b587
	noFill();
b4b587
	noStroke();
b4b587
	disableAntialiasing();
b4b587
	if (enableFill) fill(colorByRGBA(1, 1, 1, 1));
b4b587
	if (enableStroke) stroke(colorByRGBA(0.5, 0.5, 0.5, 1));
b4b587
	if (enableTexture) fillTexture(anim, 0, 0, 16, 16, FALSE);
b4b587
	if (enableStrokeTex) strokeTexture(anim, 0, 0, 16, 16, TRUE);
b4b587
	if (enableAA) enableAntialiasing();
b4b587
	
b4b587
	const double *shape = enableConvex ? *convexShape : *nonConvexShape;
b4b587
	int count = enableConvex ? (int)(sizeof(convexShape)/sizeof(**convexShape))
b4b587
	                         : (int)(sizeof(nonConvexShape)/sizeof(**nonConvexShape));
b4b587
	
b4b587
	drawRandom(shape, count);
b4b587
	drawGrid(shape, count);
b4b587
	restoreState();
b4b587
	
b4b587
	int frameCount = (int)(sizeof(frameTimes)/sizeof(*frameTimes));
b4b587
	unsigned long long t = windowGetMonotonicMilliseconds();
b4b587
	unsigned long long dt = t - frameTimes[frameIndex];
b4b587
	frameTimes[frameIndex] = t;
b4b587
	frameIndex = (frameIndex + 1)%frameCount;
b4b587
	double fps = dt > 0 ? frameCount*1000.0/(double)dt : 0.0;
b4b587
	
b4b587
	saveState();
b4b587
	textFontDefault();
b4b587
	textSize(16);
b4b587
	textf(16, 16, "fps: %6.2f", fps);
b4b587
	text( 16, 48,
b4b587
		"c - convex\n"
b4b587
		"f - fill\n"
b4b587
		"s - stroke\n"
b4b587
		"t - texture\n"
b4b587
		"y - stroke texture\n"
b4b587
		"a - antialiasing" );
b4b587
	restoreState();
b4b587
	
b4b587
	if (keyWentDown("c")) enableConvex = !enableConvex;
b4b587
	if (keyWentDown("f")) enableFill = !enableFill;
b4b587
	if (keyWentDown("s")) enableStroke = !enableStroke;
b4b587
	if (keyWentDown("t")) enableTexture = !enableTexture;
b4b587
	if (keyWentDown("y")) enableStrokeTex = !enableStrokeTex;
b4b587
	if (keyWentDown("a")) enableAA = !enableAA;
b4b587
}
b4b587