Blob Blame Raw

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <helianthus.h>

#include "common.h"


static char buffer[4096] = {};
static Sound beep;


static void scanDirectoryRecursive(const char *path, char **outTextBegin, char *outTextEnd, int level) {
	Directory d = openDirectory(path);
	if (d) {
		int count = directoryGetCount(d);
		
		int lenPrefix = level*4 + 1;
		
		int lenPath = strlen(path) + 1;
		int lenName = 0;
		for(int i = 0; i < count; ++i) {
			int ln = strlen(directoryGet(d, i));
			if (ln > lenName) lenName = ln;
		}
		int lenLine = lenPrefix + lenName + 1;
		
		char *subpath = calloc(1, lenPath + lenName + 1);
		strcpy(subpath, path);
		subpath[lenPath - 1] = '/';
		
		for(int i = 0; i < count; ++i) {
			if (*outTextBegin + lenLine > outTextEnd) break;
			
			const char *name = directoryGet(d, i);
			
			for(int i = 0; i < lenPrefix; ++i) *((*outTextBegin)++) = '-';
			char *typeSign = *outTextBegin - 1;
			strcpy(*outTextBegin, name);
			*outTextBegin += strlen(name);
			*((*outTextBegin)++) = '\n';
			
			strcpy(subpath + lenPath, name);
			if (directoryExists(subpath)) {
				*typeSign = '>';
				scanDirectoryRecursive(subpath, outTextBegin, outTextEnd, level+1);
			} else
			if (fileExists(subpath)) {
				*typeSign = '*';
			} else {
				*typeSign = '#';
			}
		}
		
		free(subpath);
		closeDirectory(d);
	}
}


void commonInit() {
	char *bufPtr = buffer;
	scanDirectoryRecursive("data", &bufPtr, bufPtr + sizeof(buffer) - 1, 0);
	beep = createSound("data/sound/beep.ogg");
}

void commonDraw() {
	pushDrawingState();
	
	noFill();
	textFontDefault();
	textSize(16);
	text(buffer, 16, 48);
	
	char fpsbuf[256] = {};
	sprintf(fpsbuf, "%6.2f", 1/worldGetFrameTime());
	text(fpsbuf, 16, 16);
	
	if (mouseWentDown("left"))
		soundPlay(beep, FALSE);
	
	if (mouseWentDown("middle"))
		askTextEx("Test\ntext input", buffer, sizeof(buffer), TRUE, FALSE);
	
	if (mouseWentDown("right"))
		messageBox("Test message box\nwith test message.");
	
	popDrawingState();
}