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
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() {
3954ba
	pushDrawingState();
3954ba
	
da4619
	noFill();
28a314
	textFontDefault();
28a314
	textSize(16);
8bc1f1
	text(buffer, 16, 48);
8bc1f1
	
8bc1f1
	char fpsbuf[256] = {};
8bc1f1
	sprintf(fpsbuf, "%6.2f", 1/worldGetFrameTime());
8bc1f1
	text(fpsbuf, 16, 16);
3954ba
	
3954ba
	if (mouseWentDown("left"))
3954ba
		soundPlay(beep, FALSE);
3954ba
	
534343
	if (mouseWentDown("middle"))
534343
		askTextEx("Test\ntext input", buffer, sizeof(buffer), TRUE, FALSE);
3954ba
	
3954ba
	if (mouseWentDown("right"))
3954ba
		messageBox("Test message box\nwith test message.");
3954ba
	
3954ba
	popDrawingState();
28a314
}