diff --git a/engine/Makefile b/engine/Makefile index 6d9ab38..fbad43f 100644 --- a/engine/Makefile +++ b/engine/Makefile @@ -1,7 +1,7 @@ CC = gcc PREFIX = ./ BINARY=timesynk -OBJS = main.o c_extra.o state_menu.o ui/elements.o sdl/timer.o sdl/r_soft.o sdl/r_gl.o sdl/interface.o sdl/sdl_extra.o sdl/spritesheets.o sdl/font.o resources/badfont_large_png.o resources/badfont_medium_png.o resources/ui_png.o resources/menu_bg_png.o data.o +OBJS = main.o c_extra.o state_menu.o ui/elements.o sdl/timer.o sdl/r_soft.o sdl/r_gl.o sdl/interface.o sdl/sdl_extra.o sdl/spritesheets.o sdl/font.o resources/badfont_large_png.o resources/badfont_medium_png.o resources/ui_png.o resources/menu_bg_png.o data.o llist.o fio.o DEBUG = -g CFLAGS = -Wall -c $(DEBUG) SDL_LFLAGS = -lGL -lSDL -lSDL_image diff --git a/engine/fio.c b/engine/fio.c new file mode 100644 index 0000000..11bb4dc --- /dev/null +++ b/engine/fio.c @@ -0,0 +1,71 @@ +#include "fio.h" +#include +#include +#include +#include + +/* +================================ +struct LList *dirToLList(const char *dir_name, int opts) + +This function takes a given directory path and attempts to populate a linked list(LList) with the contents of the directory (excluding ".." and "."). The opts argument must be provided with F_DIRS, F_FILES, or both F_DIRS|F_FILES. This limits the items added to the linked list to only directories, only files, or both. + +================================ +*/ +struct LList *dirToLList(const char *dir_name, int opts) { + struct dirent *entry; + DIR *dir = opendir(dir_name); + if (dir == NULL) + return NULL; + + struct LList *start = NULL; + struct LList *llist = NULL; + struct LList *llist_last = NULL; + while ((entry = readdir(dir)) != NULL) { + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + continue; + int entry_size = strlen(entry->d_name)+1; + // ugly + char file_path[strlen(dir_name)+entry_size]; + strcpy(file_path, dir_name); + file_path[strlen(dir_name)] = '/'; + memcpy(file_path+strlen(dir_name)+1, entry->d_name, entry_size); + // ok, let's get file properties and see if we want it + struct stat file_stat; + stat(file_path, &file_stat); + switch(file_stat.st_mode & S_IFMT) { + case S_IFDIR: + if (~opts & F_DIRS) + continue; + break; + case S_IFREG: + if (~opts & F_FILES) + continue; + break; + } + // if we got here, then the entry is valid + llist = newLList(); + llist->data = malloc(entry_size); + llist->size = entry_size; + memcpy(llist->data, entry->d_name, entry_size); + if (start == NULL) + start = llist; + if (llist_last != NULL) + llist_last->next = llist; + llist_last = llist; + } + closedir(dir); + return start; +} + +/* +================================ +int fileExists(const char *filename) + +A simple function that checks if the given filename exists. +================================ +*/ +int fileExists(const char *filename) { + struct stat buffer; + return (stat (filename, &buffer) == 0); +} diff --git a/engine/fio.h b/engine/fio.h new file mode 100644 index 0000000..eaa0bf4 --- /dev/null +++ b/engine/fio.h @@ -0,0 +1,9 @@ +#ifndef FIO_H +#define FIO_H +#include "llist.h" +#define F_DIRS 1 +#define F_FILES 2 + +struct LList *dirToLList(const char *dir_name, int opts); +int fileExists(const char *filename); +#endif diff --git a/engine/globals.h b/engine/globals.h index 1eb1c30..1ed50b4 100644 --- a/engine/globals.h +++ b/engine/globals.h @@ -3,11 +3,14 @@ #include "ts_event.h" #include "state_menu.h" #include "../data.h" +#include "llist.h" struct Table *g_settings; int g_running; +struct LList *g_modules_list; + /* video-related globals */ void *g_screen; // void pointer for SDL_Surface or others, set by interface int g_video_width; diff --git a/engine/llist.c b/engine/llist.c new file mode 100644 index 0000000..1098d7f --- /dev/null +++ b/engine/llist.c @@ -0,0 +1,20 @@ +/****** llist.c + This file contains the functions for accessing and manipulating the linked list struct, LList. +******/ +#include +#include "llist.h" + +struct LList *newLList() { + struct LList *llist = malloc(sizeof(struct LList)); + llist->size = 0; + llist->next = NULL; + llist->data = NULL; + return llist; +} + +int freeLList(struct LList *llist) { + if (llist->data != NULL) + free(llist->data); + free(llist); + return 0; +} diff --git a/engine/llist.h b/engine/llist.h new file mode 100644 index 0000000..8d21a8e --- /dev/null +++ b/engine/llist.h @@ -0,0 +1,11 @@ +#ifndef LLIST_H +#define LLIST_H +struct LList { + int size; + struct LList *next; + void *data; +}; + +struct LList *newLList(); +int freeLList(struct LList *llist); +#endif diff --git a/engine/main.c b/engine/main.c index 2e6fcaf..6a333bc 100644 --- a/engine/main.c +++ b/engine/main.c @@ -1,5 +1,6 @@ #include "globals.h" #include "interfaces.h" +#include "fio.h" int main(int argc, char *argv[]) { // let's load in our config file @@ -19,6 +20,18 @@ int main(int argc, char *argv[]) { addTablePairInt(g_settings, "v_framecap", 0); addTablePairInt(g_settings, "tickrate", 40000000); } + // let's search for modules! + if ((g_modules_list = dirToLList("modules", F_DIRS)) == NULL) + printf("ERR: no modules found\n"); + printf("modules:\n"); + struct LList *llist = g_modules_list; + while (llist) { + if (strcmp((char*)llist->data, getTablePairValueString(g_settings, "module")) == 0) + printf("* "); + printf("%s\n", (char*)llist->data); + llist = llist->next; + } + // initialize our interface system (ncurses, SDL, etc.) if (interfaceInit() == 1) { return 1; diff --git a/engine/settings.tsc b/engine/settings.tsc index 2ef66b4..16cb841 100644 --- a/engine/settings.tsc +++ b/engine/settings.tsc @@ -1,3 +1,6 @@ +;; module options +; name of last module loaded +module xibalba ;; video options v_width 1024 v_height 768