Blame demo/src/common.c

28a314
28a314
#include <string.h></string.h>
8bc1f1
#include <stdio.h></stdio.h>
28a314
#include <stdlib.h></stdlib.h>
28a314
28a314
#include <helianthus.h></helianthus.h>
28a314
28a314
#include "common.h"
28a314
28a314
f8dca4
static int answer = 0;
28a314
static char buffer[4096] = {};
3954ba
static Sound beep;
28a314
28a314
28a314
static void scanDirectoryRecursive(const char *path, char **outTextBegin, char *outTextEnd, int level) {
28a314
	Directory d = openDirectory(path);
28a314
	if (d) {
28a314
		int count = directoryGetCount(d);
28a314
		
28a314
		int lenPrefix = level*4 + 1;
28a314
		
28a314
		int lenPath = strlen(path) + 1;
28a314
		int lenName = 0;
28a314
		for(int i = 0; i < count; ++i) {
28a314
			int ln = strlen(directoryGet(d, i));
28a314
			if (ln > lenName) lenName = ln;
28a314
		}
28a314
		int lenLine = lenPrefix + lenName + 1;
28a314
		
28a314
		char *subpath = calloc(1, lenPath + lenName + 1);
28a314
		strcpy(subpath, path);
28a314
		subpath[lenPath - 1] = '/';
28a314
		
28a314
		for(int i = 0; i < count; ++i) {
28a314
			if (*outTextBegin + lenLine > outTextEnd) break;
28a314
			
28a314
			const char *name = directoryGet(d, i);
28a314
			
28a314
			for(int i = 0; i < lenPrefix; ++i) *((*outTextBegin)++) = '-';
28a314
			char *typeSign = *outTextBegin - 1;
28a314
			strcpy(*outTextBegin, name);
28a314
			*outTextBegin += strlen(name);
28a314
			*((*outTextBegin)++) = '\n';
28a314
			
28a314
			strcpy(subpath + lenPath, name);
28a314
			if (directoryExists(subpath)) {
28a314
				*typeSign = '>';
28a314
				scanDirectoryRecursive(subpath, outTextBegin, outTextEnd, level+1);
28a314
			} else
28a314
			if (fileExists(subpath)) {
28a314
				*typeSign = '*';
28a314
			} else {
28a314
				*typeSign = '#';
28a314
			}
28a314
		}
28a314
		
28a314
		free(subpath);
28a314
		closeDirectory(d);
28a314
	}
28a314
}
28a314
28a314
28a314
void commonInit() {
28a314
	char *bufPtr = buffer;
28a314
	scanDirectoryRecursive("data", &bufPtr, bufPtr + sizeof(buffer) - 1, 0);
3954ba
	beep = createSound("data/sound/beep.ogg");
28a314
}
28a314
28a314
void commonDraw() {
deef1d
	saveState();
3954ba
	
da4619
	noFill();
28a314
	textFontDefault();
28a314
	textSize(16);
8bc1f1
	text(buffer, 16, 48);
8bc1f1
	
f8dca4
	char fpsbuf[1024] = {};
f8dca4
	sprintf(fpsbuf, "answer: %d, fps: %6.2f", answer, 1/worldGetFrameTime());
8bc1f1
	text(fpsbuf, 16, 16);
3954ba
	
3954ba
	if (mouseWentDown("left"))
3954ba
		soundPlay(beep, FALSE);
3954ba
	
534343
	if (mouseWentDown("middle"))
519bc5
		askTextEx("Test\ntext input", buffer, sizeof(buffer), keyDown("any shift"), keyDown("any ctrl"));
3954ba
	
f8dca4
	if (mouseWentDown("right")) {
519bc5
		if (keyDown("any ctrl")) {
f8dca4
			answer = questionBox3("Test question box3\nwith test message.", "cancel", "no", "yes");
f8dca4
		} else
519bc5
		if (keyDown("any shift")) {
f8dca4
			answer = questionBox("Test question box\nwith test message.", "no", "yes");
f8dca4
		} else {
f8dca4
			messageBox("Test message box\nwith test message.");
f8dca4
		}
f8dca4
	}
3954ba
	
deef1d
	restoreState();
28a314
}