Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # MakeFile #
- BIN = Pond
- CC = arm-buildroot-linux-uclibcgnueabi-gcc
- SRC = main.c
- OBJ = main.o
- CFLAGS = -Wall -g -02 -fomit-frampointer -lSDL -lSDL_ttf -lSDL_image -ldl -lstdc++ -lm -lgcc_s -lc
- $(BIN): $(OBJ)
- $(CC) -o $@ $^ $(CFLAGS)
- clean: $(SRC)
- rm -f $(BIN) $(OBJ)
- # main.c #
- #include <SDL/SDL.h> // Main SDL Header
- #include <SDL/SDL_ttf.h> // True Type Font Header
- #include <SDL/SDL_image.h>
- #include <dirent.h>
- #include <dlfcn.h>
- #include "Defines.h" // Header with defines
- #include <string.h>
- #include <unistd.h>
- #include <sys/statvfs.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdio.h>
- typedef int boolean;
- enum { false, true };
- char* BASE_DIR = "PondLewder/";
- char* SPLASH_PATH="Assets/Images/splash.png";
- char* BACKGROUND_PATH = "Assets/Images/background.png";
- char* RING_PATH = "Assets/Images/ring.png";
- char* RING_SELECTED_PATH = "Assets/Images/ring_s.png";
- char* MAIN_TITLE_PATH = "Assets/Images/main_menu.png";
- char* BACKUP_DIR = "LF/Bulk/PondLewderBackups";
- char* DISNEY_FONT_PATH = "Assets/Fonts/disney_font.ttf";
- char* DOOM_FONT_PATH = "Assets/Fonts/doom_font.ttf";
- char* CURRENT_DIR_PATH = "";
- char* CART_PRODUCT_ID; // We will set this on start up
- char* currentCopy;
- unsigned long DISK_TOTAL;
- unsigned long DISK_AVALIABLE;
- unsigned long DISK_FREE;
- // Option Settings
- boolean isShowingHidden = false;
- struct ImageStruct {
- SDL_Surface* screen;
- float x;
- float y;
- float orgX;
- float orgY;
- int dir;
- };
- struct AppStruct{
- char name[100];
- char category[20];
- char type[20];
- char productID[20];
- char packageID[40];
- char version[20];
- char locale[10];
- char developer[30];
- char publisher[30];
- char appSo[40];
- char path[200];
- char icon[200];
- char hidden[10];
- // Should we push memory lulz??
- SDL_Surface* screen;
- int x;
- int y;
- };
- struct ImageStruct g_DropLet;
- struct AppStruct Apps[300];
- /* DECLARES FOR BTIO */
- typedef int* (*tdBTIO_SendCommand)(void*,int,void*, int);
- typedef int* (*tdBTIO_Init)(void (*)(void*));
- typedef int* (*RunApp)();
- struct LFController {char AButton; char BButton; char HomeButton; char HintButton; char WandPos; char VPos;
- char JoyX; char JoyY; char AccelX; char unknown; char AccelY; char unknown1; char AccelZ;
- char Counter; char Batt; char unknown2;};
- typedef void* (*arbitrary)();
- SDL_Surface* g_SplashScreen; // Splash Image
- SDL_Surface* g_Background; // Background Image
- SDL_Surface* g_Screen; // Back buffer
- SDL_Surface* g_AppRingSel;
- SDL_Surface* g_AppRingUnSel;
- SDL_Surface* g_MainMenuTitle;
- SDL_Surface* g_OptionsTitle;
- SDL_Surface* g_OptionsBg;
- SDL_Event g_Event; // SDL event struct for input
- SDL_Surface* tempIcon;
- int g_Timer; // Timer
- char* BUILD_DATE = "BUILD: v1.02-10/01/2016";
- // Functions to handle different states of app
- void Menu();
- void Game();
- void Exit();
- // Init and Shutdown functions
- void Init();
- void Shutdown();
- // Helper functions for app states
- void DrawScreen(SDL_Surface*);
- void DrawImage(SDL_Surface*, int, int);
- SDL_Surface* LoadImage(char*);
- void ClearScreen();
- void DisplayText(char*, char*, int, int, int, int, int, int, int);
- void HandleMenuInput();
- void HandleGameInput();
- void HandleExitInput();
- void LaunchApp(char*);
- void MountApp(char* path);
- void RemoveMount();
- void PrintText(char*);
- void BackupCart();
- void GetAppsInDir(char*);
- void ReadMetaToApp(char*);
- char* getcwd(char*, size_t);
- void removeChar(char *, char);
- int CopyFile(char*, char*);
- void CopyCart();
- void CopyDir(char*, char*);
- boolean isCopying = false;
- int menuState = 0;
- //0 Init, 1 Splash, 2 Menu
- int appLoc = 0; // Location of where app should be
- int textLoc = 1; // Location of PrintText on screen
- int appCounter = 0; // Count for apps in array
- int running = 1; // Main loop
- int selected = 0; // App Selected in menu
- int page = 0; // Menu App Page
- void* handle; // Handle for bt
- void PrintText( char* text){
- char tempString[500];
- char tempText[400];
- strcpy(tempString, "drawtext-fb /dev/fb0 /var/fonts/monotext8x16.rgb \"");
- strcat(tempString, text);
- strcat(tempString, "\" ");
- sprintf(tempText, "%d", textLoc);
- strcat(tempString, tempText);
- strcat(tempString, " 5 0\n");
- system(tempString);
- //printf(tempString);
- textLoc++;
- }
- void CallbackFunction()
- {
- printf("This is our callback!");
- }
- boolean aPressed = false;
- boolean bPressed = false;
- boolean homePressed = false;
- boolean hintPressed = false;
- boolean leftPressed = false;
- boolean rightPressed = false;
- boolean upPressed = false;
- boolean downPressed = false;
- void GetCurrentDir(){
- char cwd[1024];
- if (getcwd(cwd, sizeof(cwd)) != NULL){
- printf("Current working dir: %s\n", cwd);
- } else {
- printf("Failed to get current dir!");
- }
- }
- void LoaderCallBackFunction(void* callback, struct LFController* LFC, int length)
- {
- if (LFC->JoyY > 0xC0 && upPressed == false)
- {
- // Joy Up
- upPressed = true;
- if(selected - 4 < 0){
- selected = 0;
- } else {
- selected = selected - 4;
- }
- } else if(LFC->JoyY < 0xC0){
- upPressed = false;
- }
- if (LFC->JoyY < 0x3F && downPressed == false)
- {
- // Joy Down
- downPressed = true;
- if(selected + 4 > appCounter){
- selected = appCounter - 1;
- } else {
- selected = selected + 4;
- }
- } else if (LFC->JoyY > 0x3F){
- downPressed = false;
- }
- if (LFC->JoyX > 0xC0 && rightPressed == false)
- {
- // Joy Right
- rightPressed = true;
- if(selected + 1 > appCounter){
- selected = appCounter - 1;
- } else {
- selected++;
- }
- } else if (LFC->JoyX < 0xC0){
- rightPressed = false;
- }
- if (LFC->JoyX < 0x3F && leftPressed == false)
- {
- // Joy Left
- leftPressed = true;
- if(selected - 1 < 0){
- selected = 0;
- } else {
- selected--;
- }
- } else if (LFC->JoyX > 0x3F){
- leftPressed = false;
- }
- if (LFC->AButton == 0x01 && aPressed == false)
- {
- // A Pressed
- aPressed = true;
- char tempString[100];
- sprintf(tempString, "%s", Apps[selected].path);
- removeChar(tempString, '.');
- printf("Mounting %s: %s\n", Apps[selected].name, tempString);
- MountApp(tempString);
- } else if (LFC->AButton == 0x00) {
- aPressed = false;
- }
- if (LFC->BButton == 0x01 && bPressed == false)
- {
- // B Pressed
- bPressed = true;
- BackupCart();
- /*char tempString[100];
- sprintf(tempString, "%s/%s", Apps[selected].path, Apps[selected].appSo);
- printf("Launching %s: %s\n", Apps[selected].name, tempString);
- LaunchApp(tempString);*/
- } else if (LFC->BButton == 0x00){
- bPressed = false;
- }
- if (LFC->HomeButton == 0x01 && homePressed == false)
- {
- // Home Pressed
- homePressed = true;
- running = 0;
- } else {
- homePressed = false;
- }
- if (LFC->HintButton == 0x01 && hintPressed == false)
- {
- // Hint Pressed
- hintPressed = true;
- if(menuState){
- menuState = 0;
- } else {
- menuState = 1;
- }
- } else if (LFC->HintButton == 0x00){
- hintPressed = false;
- }
- if(selected > (page * 12) + 11){
- page++;
- } else if (selected < page * 12) {
- page--;
- }
- }
- void LaunchApp(char* path){
- char* error;
- void* appHandle = dlopen (path, RTLD_LAZY);
- if (!appHandle) {
- fputs (dlerror(), stderr);
- } else {
- printf("IT LOADED!");
- RunApp RunCreateApp = (RunApp)dlsym(appHandle, "CreateApp");
- if ((error = dlerror()) != NULL) {
- fputs(error, stderr);
- fputs(" @ RunApp\n", stderr);
- dlclose(appHandle);
- } else {
- printf("CREATED SYMBOL!");
- }
- appLoc = 3;
- RunCreateApp();
- //appLoc = 0;
- }
- }
- void MountApp(char* path){
- // Remove symlink
- RemoveMount();
- // Create symlink to current path!
- char tempString[1024];
- sprintf(tempString, "ln -s %s /LF/Cart/0x99999999", path);
- system(tempString);
- //printf(tempString);
- running = 0;
- }
- void RemoveMount(){
- // Remove symlink
- system("rm -f /LF/Cart/0x99999999");
- }
- void InitControls(){
- char *error;
- // Open libBRIO.so
- handle = dlopen ("/usr/lib/libBTIO.so", RTLD_LAZY);
- if (!handle) {
- fputs (dlerror(), stderr);
- exit(1);
- }
- tdBTIO_SendCommand BTIO_SendCommand = (tdBTIO_SendCommand)dlsym(handle, "BTIO_SendCommand");
- if ((error = dlerror()) != NULL) {
- fputs(error, stderr);
- fputs(" @ BTIO_QueryForDevices\n", stderr);
- exit(1);
- }
- tdBTIO_Init BTIO_Init = (tdBTIO_Init)dlsym(handle, "BTIO_Init");
- if ((error = dlerror()) != NULL) {
- fputs(error, stderr);
- fputs(" @ BTIO_QueryForDevices\n", stderr);
- exit(1);
- }
- /* Init BTIO */
- int* ControllerNum = BTIO_Init((void*)&CallbackFunction);//param seems to be *void callback pointer?
- printf("BTIO_Init returned val of %d \n", ControllerNum);
- int* SendComRet = 0;
- SendComRet = BTIO_SendCommand(ControllerNum,1,(void*)&LoaderCallBackFunction,4);
- //printf("Send Command returned val of %d \n", SendComRet);
- SendComRet = BTIO_SendCommand(ControllerNum,2,(void*)&LoaderCallBackFunction,4);
- //printf("Send Command returned val of %d \n", SendComRet);
- SendComRet = BTIO_SendCommand(ControllerNum,0,(void*)&LoaderCallBackFunction,4);
- //printf("Send Command returned val of %d \n", SendComRet);
- SendComRet = BTIO_SendCommand(ControllerNum,9,(void*)&LoaderCallBackFunction,4);
- //printf("Send Command returned val of %d \n", SendComRet);
- }
- void Init()
- {
- // We should probs also read config here aswell...
- char tempString[200];
- // Call this to fix frame buffer offset
- system("drawrect-fb /dev/fb0 0 0 1 1 50 0x802020");
- system("led-control --beat 2");
- PrintText(BUILD_DATE);
- PrintText("THIS SOFTWARE IS FREE! IF YOU PAID FOR IT DEMAND A REFUND!");
- PrintText("**********************************");
- PrintText("* PondLewder Homebrew Launcher *");
- PrintText("* v1.02 *");
- PrintText("* Written by Joshtech *");
- PrintText("**********************************");
- PrintText(" Special Thanks to is0-mick");
- PrintText(" Keep your Safety Googles on!");
- PrintText("");
- PrintText(" WWW.SPIFFYHACKS.COM ");
- GetCurrentDir();
- //PrintText("WE WILL PUT A DEPENDANCY CHECK HERE! - TODO");
- //PrintText("IF SOMETHING IS NOT FOUND IT WILL DOWNLOAD THEM");
- PrintText("UNMOUNTING ANY EMULATED CARTS!");
- RemoveMount();
- PrintText("");
- PrintText("*Initalize SDL*");
- // Initilize SDL Video
- if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ){
- sprintf(tempString," SDL Init: FAILED! Reason: %s\n", SDL_GetError());
- PrintText(tempString);
- PrintText(" TERMINATING APP!");
- sleep(10);
- exit(1);
- } else {
- PrintText(" SDL_Init: OK!");
- }
- PrintText("*Initialize TTF*");
- // Initialize the true type font library
- if( TTF_Init() < 0)
- {
- sprintf(tempString, " TTF_Init: FAILED! Reason: %s\n", TTF_GetError());
- PrintText(tempString);
- PrintText(" TERMINATING APP!");
- sleep(10);
- exit(1);
- } else {
- PrintText(" TTF_Init: OK!");
- }
- PrintText("*Initialize SDL_Image*");
- // Initialize the SDL_Image library with png support
- int imgFlags = IMG_INIT_PNG;
- if ( !(IMG_Init( imgFlags ) & imgFlags) ){
- sprintf(tempString, " IMG_Init: FAILED! Reason: %s\n", SDL_GetError());
- PrintText(tempString);
- PrintText(" TERMINATING APP!");
- sleep(10);
- exit(1);
- } else {
- PrintText(" IMG_Init: OK!");
- }
- PrintText("*Remove Cursor*");
- if ( SDL_ShowCursor(SDL_DISABLE) < 0 ) // Stupid cursor on the screen
- {
- sprintf(tempString, " SDL_ShowCursor: FAILED! Reason: %s\n", SDL_GetError());
- PrintText(tempString);
- PrintText(" TERMINATING APP!");
- sleep(10);
- exit(1);
- } else {
- PrintText(" SDL_ShowCursor(SDL_DISABLE): OK!");
- }
- PrintText("*Initialize SDL_TIMER*");
- // Get the number of ticks since SDL was initialized
- g_Timer = SDL_GetTicks();
- if ( g_Timer < 0 ){
- sprintf(tempString," SDL_Timer: FAILED! Reason: %s\n", SDL_GetError());
- PrintText(tempString);
- PrintText(" TERMINATING APP!");
- sleep(10);
- exit(1);
- } else {
- PrintText(" SDL_Timer: OK!");
- }
- PrintText("*Load Image Assets*");
- // We will start by loading Splash screen
- g_SplashScreen = LoadImage("/PondLewder/Assets/Images/splash.png");
- g_Background = LoadImage("/PondLewder/Assets/Images/background.png");;
- g_AppRingSel = LoadImage("/PondLewder/Assets/Images/ring_s.png");
- g_AppRingUnSel = LoadImage("/PondLewder/Assets/Images/ring.png");
- g_MainMenuTitle = LoadImage("/PondLewder/Assets/Images/main_menu.png");
- g_OptionsTitle = LoadImage("/PondLewder/Assets/Images/options.png");
- g_OptionsBg = LoadImage("/PondLewder/Assets/Images/options_bg.png");
- if ( g_SplashScreen == NULL || g_Background == NULL || g_AppRingSel == NULL || g_AppRingUnSel == NULL || g_MainMenuTitle == NULL || g_OptionsTitle == NULL || g_OptionsBg == NULL){
- sprintf(tempString, " IMG_Load: FAILED! Reason: %s\n", SDL_GetError());
- PrintText(tempString);
- PrintText(" TERMINATING APP!");
- sleep(10);
- exit(1);
- } else {
- PrintText(" IMG_Load: LOADED ALL ASSETS OK!");
- }
- PrintText("*Setting Video Mode*");
- sprintf(tempString, " SDL_SetVideoMode: Width:%d x Height:%d", WIDTH, HEIGHT);
- PrintText(tempString);
- PrintText("");
- PrintText(" Get ready for 88 m/ph Marty! OK! Doc!");
- sleep(4);
- g_Screen = SDL_SetVideoMode( WIDTH, HEIGHT, 32, SDL_SWSURFACE | SDL_FULLSCREEN | SDL_PREALLOC );
- //g_Screen = SDL_SetVideoMode( 1024, 600, 32, SDL_SWSURFACE | SDL_FULLSCREEN | SDL_PREALLOC ); // For LeapPad Ultra
- // Now we will check if g_Screen was set
- if ( g_Screen == NULL ){
- sprintf(tempString, "SDL_SetVideoMode: FAILED! Reason: %s\n", SDL_GetError());
- PrintText(tempString);
- PrintText("TERMINATING APP!");
- sleep(10);
- exit(1);
- } else {
- printf("SDL_SetVideoMode: Width:%d x Height:%d\n", WIDTH, HEIGHT);
- }
- textLoc = 1;
- }
- SDL_Surface* LoadImage(char* file)
- {
- // Load Image at specified path
- SDL_Surface* loadedImage = IMG_Load(file);
- //printf("imageLoaded good");
- if(loadedImage == NULL)
- {
- // Dispose of old surface
- SDL_FreeSurface(loadedImage);
- }
- return loadedImage;
- }
- // Lets scan ALL folders from given directory!
- int CheckIfCart(){
- DIR *d;
- struct dirent *dir;
- d = opendir("/LF/Cart");
- int isCart = 0; // 0 is no cart, 1 cart inserted, 2 emulated cart (This can happen if a cart is in when startup unmount is called)
- if(d)
- {
- while ((dir = readdir(d)) != 0)
- {
- // If folder has 0x99999999 then a emulated cart is already loaded
- if(strcmp(dir->d_name,"0x99999999") == 1 ){
- isCart = 2;
- } else if(strcmp(dir->d_name,".") && strcmp(dir->d_name,"..") && strcmp(dir->d_name,"lost+found") && strcmp(dir->d_name,".Trash-1000")){
- // This should be the cart prouctID
- isCart = 1;
- CART_PRODUCT_ID = dir->d_name; // We will set it here!
- }
- }
- closedir(d);
- }
- return isCart;
- }
- int CopyFile(char *from, char *to)
- {
- int fd_to, fd_from;
- char buf[4096];
- ssize_t nread;
- int saved_errno;
- fd_from = open(from, O_RDONLY);
- if (fd_from < 0)
- return -1;
- fd_to = open(to, O_WRONLY | O_CREAT | O_EXCL, 0666);
- if (fd_to < 0)
- goto out_error;
- while (nread = read(fd_from, buf, sizeof buf), nread > 0)
- {
- char *out_ptr = buf;
- ssize_t nwritten;
- do {
- nwritten = write(fd_to, out_ptr, nread);
- if (nwritten >= 0)
- {
- nread -= nwritten;
- out_ptr += nwritten;
- }
- else if (errno != EINTR)
- {
- goto out_error;
- }
- } while (nread > 0);
- }
- if (nread == 0)
- {
- if (close(fd_to) < 0)
- {
- fd_to = -1;
- goto out_error;
- }
- close(fd_from);
- /* Success! */
- return 0;
- }
- out_error:
- saved_errno = errno;
- close(fd_from);
- if (fd_to >= 0)
- close(fd_to);
- errno = saved_errno;
- return -1;
- }
- void LoadCart(){
- // First thing we must do is check if a cart is in
- if(CheckIfCart()){
- // There is a cart, now lets read meta.inf from cart... Should know the product id from checking cart...
- printf("Cart Product ID is: %s\n",CART_PRODUCT_ID);
- // We will create a tempString for formating!
- char tempString[300];
- sprintf(tempString, "/LF/Cart/%s/meta.inf", CART_PRODUCT_ID);
- ReadMetaToApp(tempString); // Read the carts meta.inf
- sprintf(tempString, "/LF/Cart/%s", Apps[appCounter].productID); // We will read the productid from the app from now on
- strcpy(Apps[appCounter].path, tempString); // We have to manually add path since it's a cart
- // Now the res of the image on the cart is actually larger than normal... instead of resizeing we can "load" it from the systems cache!
- // System stores cart cache in /LF/Bulk/Data/Local/All/CartIconCache with the packageID.png so lets set that as the icon path :D
- sprintf(tempString, "/LF/Bulk/Data/Local/All/CartIconCache/%s.png", Apps[appCounter].packageID);
- printf(tempString);
- // Since splash screen loads icons programatically we will do it manually here to bypass cart icon
- Apps[appCounter].screen = LoadImage(tempString);
- if(Apps[appCounter].screen == NULL){
- //printf("DIDN'T LOAD IMAGE! TRYING MISSING ICON");
- Apps[appCounter].screen = LoadImage("/PondLewder/Assets/Images/missing_cache.png");
- if(Apps[appCounter].screen == NULL){
- printf("FAILED TO LOAD MISSINGICON! I GIVE UP!");
- }
- }
- // Increase app count so we can continue to load other apps for the list!
- appCounter++;
- } else if(CheckIfCart() == 2) {
- printf("There is no cart inserted and a cart is being emulated!\n");
- } else {
- printf("There is no cart or emulated cart!\n");
- }
- }
- void BackupCart(){
- // First thing we must do is check if a cart is in
- if(CheckIfCart()){
- menuState = 2;
- CopyCart();
- menuState = 0;
- } else if(CheckIfCart() == 2) {
- printf("There is no cart inserted and a cart is being emulated!\n");
- } else {
- printf("There is no cart or emulated cart!\n");
- }
- }
- void CopyCart(){
- // We will hard code copy for the time being
- DIR *d;
- struct dirent *dir;
- char temp[200];
- sprintf(temp, "mkdir /%s", BACKUP_DIR); // We should probs put checks if they exist create them but meh for now
- system(temp);
- d = opendir("/LF/Cart");
- if(d)
- {
- while ((dir = readdir(d)) != 0)
- {
- // If there is a directory on the cart...
- if(dir->d_type == 4 && strcmp(dir->d_name,".") && strcmp(dir->d_name,"..") && strcmp(dir->d_name,"lost+found") && strcmp(dir->d_name,".Trash-1000") ){
- // Well this will obviously be the productID dir of the cart!
- // So lets create it in /LF/Bulk/PondLewderBackups
- // We need to make sure the dir is create first
- sprintf(temp, "mkdir /%s/%s", BACKUP_DIR, dir->d_name); // To lazy will use system call
- system(temp);
- sprintf(temp, "touch /%s/%s/.PondLewder", BACKUP_DIR, dir->d_name); // To lazy will use system call
- system(temp);
- currentCopy = dir->d_name;
- CopyDir("/LF/Cart", dir->d_name);
- } else { // Then it's a file
- //printf("%s\n", dir->d_name);
- }
- }
- closedir(d);
- }
- }
- void CopyDir(char* base, char* path){
- DIR *d;
- struct dirent *dir;
- char temp[200];
- sprintf(temp, "%s/%s", base, path);
- d = opendir(temp);
- if(d)
- {
- while ((dir = readdir(d)) != 0)
- {
- // If there is a directory on the cart...
- if(dir->d_type == 4 && strcmp(dir->d_name,".") && strcmp(dir->d_name,"..") && strcmp(dir->d_name,"lost+found") && strcmp(dir->d_name,".Trash-1000") ){
- sprintf(temp, "%s/%s", path, dir->d_name);
- char tempSystem[200];
- // We will create this new folder in the game dir
- sprintf(tempSystem, "mkdir /%s/%s", BACKUP_DIR, temp); // To lazy will use system call
- system(tempSystem);
- CopyDir(base, temp);
- } else if (strcmp(dir->d_name,".") && strcmp(dir->d_name,"..") && strcmp(dir->d_name,"lost+found") && strcmp(dir->d_name,".Trash-1000")) { // Then it's a file
- char from[300];
- char to[300];
- sprintf(from, "%s/%s/%s", base, path, dir->d_name);
- sprintf(to, "/%s/%s/%s", BACKUP_DIR, path, dir->d_name);
- //printf("%s\n", to);
- currentCopy = to;
- CopyFile(from, to);
- }
- }
- closedir(d);
- }
- }
- void ReadVolume(){
- struct statvfs fiData;
- if((statvfs("/LF/Bulk", &fiData)) < 0) {
- printf("Failed to get drive info!\n");
- } else {
- DISK_TOTAL = fiData.f_blocks * fiData.f_frsize / 1024;
- DISK_AVALIABLE = fiData.f_bavail * fiData.f_frsize / 1024;
- DISK_FREE = fiData.f_bfree * fiData.f_frsize / 1024;
- printf("Location: %s\n", ".");
- printf("Total: %lu\n", DISK_TOTAL);
- printf("Avaliable: %lu\n", DISK_AVALIABLE);
- printf("Free: %lu\n", DISK_FREE);
- printf("Used: %lu\n", (DISK_TOTAL - DISK_FREE));
- }
- }
- void Shutdown()
- {
- ClearScreen();
- // Shutdown the true type font library.
- TTF_Quit();
- // Free our surfaces.
- SDL_FreeSurface(g_Background);
- SDL_FreeSurface(g_DropLet.screen);
- SDL_FreeSurface(g_Screen);
- // Tell SDL to shutdown and free and resources it was using.
- SDL_Quit();
- }
- void Menu()
- {
- DrawScreen(g_Background);
- DisplayText("/PondLewder/Assets/Fonts/disney_font.ttf", BUILD_DATE, 1, 1, 12, 255, 255, 255, 255);
- DisplayText("/PondLewder/Assets/Fonts/disney_font.ttf", "Pre-Alpha Build - WWW.SPIFFYHACKS.COM", 1, 30, 12, 0, 0, 0, 255);
- char tempString[300];
- sprintf(tempString, "FREE SPACE: %lu USED SPACE: %lu TOTAL SPACE: %lu <- THIS IS TO BE FIXED, SORRY PEOPLE!", DISK_FREE, DISK_TOTAL - DISK_FREE, DISK_TOTAL);
- DisplayText("/PondLewder/Assets/Fonts/disney_font.ttf", tempString, 250, 1, 12, 255, 0, 100, 255);
- switch(menuState){
- case 0: // Displaying apps
- DrawImage(g_MainMenuTitle, 470, 16);
- int disp;
- for(disp = 12 * page; disp < (12 * page) + 12 && disp < appCounter; disp++){
- if(Apps[disp].screen != NULL){
- DrawImage(Apps[disp].screen, Apps[disp].x, Apps[disp].y); //256x256
- //printf("Selected:%d & Disp:%d & Page:%d\n", selected, disp, page); // For debug
- if(disp == selected){
- DrawImage(g_AppRingSel, Apps[disp].x, Apps[disp].y); //256x256
- } else {
- DrawImage(g_AppRingUnSel, Apps[disp].x, Apps[disp].y); //256x256
- }
- DisplayText("/PondLewder/Assets/Fonts/doom_font.ttf", Apps[disp].name, Apps[disp].x + (256 / 2) - (strlen(Apps[disp].name) * 3) , Apps[disp].y + (256 - 65), 15, 0, 0, 0, 255);
- }
- }
- break;
- case 1: // Options
- // Need to write more of options... later tho will keep current code... tho since this code is in release we will put text to explain that options are still to come
- DrawImage(g_OptionsBg, 22, 102);
- DrawImage(g_OptionsTitle, 470, 16);
- DisplayText("/PondLewder/Assets/Fonts/disney_font.ttf", "Pre-Alpha Build - WWW.SPIFFYHACKS.COM", 0, 30, 12, 0, 0, 0, 255);
- break;
- case 2: // Copying File
- DisplayText("/PondLewder/Assets/Fonts/disney_font.ttf", "Pre-Alpha Build - WWW.SPIFFYHACKS.COM", 0, 30, 12, 0, 0, 0, 255);
- DisplayText("/PondLewder/Assets/Fonts/disney_font.ttf", "COPYING... PLEASE WAIT!", 0, 360, 100, 255, 0, 0, 255);
- //DisplayText("/PondLewder/Assets/Fonts/disney_font.ttf", currentCopy, 0, 500, 20, 255, 0, 0, 255);
- }
- SDL_UpdateRect(g_Screen, 0, 0, 0, 0);
- }
- void SplashScreen(){
- // Update screen to show splash
- GetAppsInDir(".."); // This is to check whole system KEK
- //GetAppsInDir("/LF/Bulk/ProgramFiles");
- printf("Found %d apps\n", appCounter);
- // This might push memory limits... will see
- int i;
- int x = 0;
- int y = 0;
- for (i =0; i < appCounter; i++){
- // We will set "Page" locations here!!
- switch (x){
- case 0:
- Apps[i].x = 10;
- break;
- case 1:
- Apps[i].x = 10 + (256 * x);
- break;
- case 2:
- Apps[i].x = 10 + (256 * x);
- break;
- case 3:
- Apps[i].x = 10 + (256 * x);
- break;
- }
- switch (y){
- case 0:
- Apps[i].y = 65;
- break;
- case 1:
- Apps[i].y = 20 + (256 * y);
- break;
- case 2:
- Apps[i].y = (256 * y) - 23;
- }
- if(x < 3){
- x++;
- } else {
- x = 0;
- if(y < 2){
- y++;
- } else {
- y = 0;
- }
- }
- char tempChar[150];
- sprintf(tempChar, "%s/%s", Apps[i].path, Apps[i].icon);
- //printf("%s\n", tempChar);
- if(Apps[i].screen == NULL){
- Apps[i].screen = LoadImage(tempChar);
- if(Apps[i].screen == NULL){
- //printf("DIDN'T LOAD IMAGE! TRYING MISSING ICON");
- Apps[i].screen = LoadImage("/PondLewder/Assets/Images/missing_icon.png");
- if(Apps[i].screen == NULL){
- printf("FAILED TO LOAD MISSINGICON! I GIVE UP!");
- }
- }
- } else {
- printf("App already has icon... Cache Cart Icon?\n");
- }
- }
- // For debuging kek
- /*for (i =0; i< appCounter; i++){
- printf("\nApp Details:\n");
- printf("%s\n", Apps[i].name);
- printf("%s\n", Apps[i].type);
- printf("%s\n", Apps[i].category);
- printf("%s\n", Apps[i].productID);
- printf("%s\n", Apps[i].packageID);
- printf("%s\n", Apps[i].appSo);
- printf("%s\n", Apps[i].version);
- printf("%s\n", Apps[i].locale);
- printf("%s\n", Apps[i].developer);
- printf("%s\n", Apps[i].publisher);
- printf("%s\n", Apps[i].icon);
- printf("%s\n", Apps[i].hidden);
- printf("%s\n", Apps[i].path);
- }*/
- appLoc = 2;
- // We don't need this anymore maybe!
- SDL_FreeSurface(g_SplashScreen);
- }
- char* ReturnType(int type){
- switch (type){
- case 8:
- return "FILE";
- break;
- case 4:
- return "DIR";
- break;
- case 3:
- return "SYM";
- break;
- }
- return NULL;
- }
- void removeChar(char *str, char garbage) {
- char *src, *dst;
- for (src = dst = str; *src != '\0'; src++) {
- *dst = *src;
- if (*dst != garbage) dst++;
- }
- *dst = '\0';
- }
- char *trim(char *str)
- {
- char *end;
- // Trim leading space
- while(isspace(*str)) str++;
- if(*str == 0) // All spaces?
- return str;
- // Trim trailing space
- end = str + strlen(str) - 1;
- while(end > str && isspace(*end)) end--;
- // Write new null terminator
- *(end+1) = 0;
- return str;
- }
- // Function to parse meta.infs ONLY! kek
- void ReadMetaToApp(char* path){
- //printf("Reading %s\n", path);
- FILE *fr;
- char line[100];
- // Open meta.inf
- fr = fopen (path, "rt");
- // Lets read line for line yeh?
- while(fgets(line, 100, fr) != NULL)
- {
- // Parse line for line
- char* tempVar;
- tempVar = strtok(line, "=");
- if(tempVar){
- // For name
- if(!strcmp(tempVar, "Name")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].name, trim(tempVar));
- }
- }
- // For Type
- if(!strcmp(tempVar, "Type")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].type, trim(tempVar));
- }
- }
- // For ProductID
- if(!strcmp(tempVar, "ProductID")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].productID, trim(tempVar));
- }
- }
- // For PackageID
- if(!strcmp(tempVar, "PackageID")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].packageID, trim(tempVar));
- }
- }
- // For Version
- if(!strcmp(tempVar, "Version")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].version, trim(tempVar));
- }
- }
- // For Locale
- if(!strcmp(tempVar, "Locale")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].locale, trim(tempVar));
- }
- }
- // For Publisher
- if(!strcmp(tempVar, "Publisher")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].publisher, trim(tempVar));
- }
- }
- // For Developer
- if(!strcmp(tempVar, "Developer")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].developer, trim(tempVar));
- }
- }
- // For AppSo
- if(!strcmp(tempVar, "AppSo")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].appSo, trim(tempVar));
- }
- }
- // For Icon
- if(!strcmp(tempVar, "Icon")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].icon, trim(tempVar));
- }
- }
- // For Category
- if(!strcmp(tempVar, "Category")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].category, trim(tempVar));
- }
- }
- // For Hidden
- if(!strcmp(tempVar, "Hidden")){
- tempVar = strtok(NULL, "=");
- if(tempVar){
- // Do stuff with data
- removeChar(tempVar, '"');
- strcpy(Apps[appCounter].hidden, trim(tempVar));
- }
- }
- }
- /*tempVar = strtok(NULL, "=");
- if(tempVar){
- printf("Second: %s\n", tempVar);
- }
- printf ("%s", line);*/
- }
- fclose(fr); /* close the file prior to exiting the routine */
- }
- // Lets scan ALL folders from given directory!
- void GetAppsInDir(char* path){
- DIR *d;
- struct dirent *dir;
- d = opendir(path);
- if(d)
- {
- while ((dir = readdir(d)) != 0)
- {
- char temp[150];
- if(dir->d_type == 4 && strcmp(dir->d_name,".") && strcmp(dir->d_name,"..") && strcmp(dir->d_name,"Cart") ){
- //printf("%s\n", dir->d_name); // For debug
- sprintf(temp, "%s/%s", path, dir->d_name);
- GetAppsInDir(temp);
- }
- if(strcmp(dir->d_name,"meta.inf") == 0){ // If folder has a meta.inf it might be an app
- //Add to app Counter
- //This is where we will read the inf
- sprintf(temp, "%s/%s", path, dir->d_name);
- //printf("%s\n", temp); // Debug to be removed
- ReadMetaToApp(temp);
- //Assign path to APP
- strcpy(Apps[appCounter].path, path);
- //printf("Read %s without issues\n", temp);
- // Display Findings for visual awesomeness
- sprintf(temp, "SCANNING:%s/", path);
- DrawScreen(g_SplashScreen);
- DisplayText("/PondLewder/Assets/Fonts/disney_font.ttf", BUILD_DATE, 1, 1, 12, 0, 0, 0, 255);
- DisplayText("/PondLewder/Assets/Fonts/doom_font.ttf",temp, 1280 / 2 - (strlen(temp) * 3), 720-25, 20, 0, 0, 0, 255);
- DisplayText("/PondLewder/Assets/Fonts/doom_font.ttf",Apps[appCounter].name, 1280 / 2 - (strlen(Apps[appCounter].name) * 4), 720-55, 30, 0, 0, 0, 255);
- SDL_UpdateRect(g_Screen, 0, 0, 0, 0);
- appCounter++; // Add 1 to app counter, for total count + array indexing
- }
- }
- closedir(d);
- }
- }
- // This function draws a "screen" fullscreen
- void DrawScreen(SDL_Surface* screen)
- {
- // These structures tell SDL_BlitSurface() the location of what
- // we want to blit and the destination we want it blitted to.
- // Presently, we blit the entire surface to the entire screen.
- SDL_Rect destination = { 0, 0, 0, 0 };
- // This just 'block-image transfers' our bitmap to our window.
- SDL_BlitSurface(screen, NULL, g_Screen, &destination);
- }
- // This function draws and image at a set location <- Since SDL 1.2 doesn't have scaleing we must have
- // set image sizes already
- void DrawImage(SDL_Surface* screen, int x, int y)
- {
- SDL_Rect destination = { x, y, 0, 0 }; // <- Doesn't matter kek
- // This just 'block-image transfers' our bitmap to our window.
- SDL_BlitSurface(screen, NULL, g_Screen, &destination);
- }
- // This function simply clears the back buffer to black.
- void ClearScreen()
- {
- // This function just fills a surface with a given color. The
- // first 0 tells SDL to fill the whole surface. The second 0
- // is for black.
- SDL_FillRect(g_Screen, 0, 0);
- }
- // Changes a surface's alpha value, by altering per-pixel alpha if necessary.
- void SetSurfaceAlpha (SDL_Surface *surface, Uint8 alpha)
- {
- SDL_PixelFormat* fmt = surface->format;
- // If surface has no alpha channel, just set the surface alpha.
- if( fmt->Amask == 0 ) {
- SDL_SetAlpha( surface, SDL_SRCALPHA, alpha );
- }
- // Else change the alpha of each pixel.
- else {
- unsigned bpp = fmt->BytesPerPixel;
- // Scaling factor to clamp alpha to [0, alpha].
- float scale = alpha / 255.0f;
- SDL_LockSurface(surface);
- int y, x;
- for (y = 0; y < surface->h; ++y)
- for (x = 0; x < surface->w; ++x) {
- // Get a pointer to the current pixel.
- Uint32* pixel_ptr = (Uint32 *)(
- (Uint8 *)surface->pixels
- + y * surface->pitch
- + x * bpp
- );
- // Get the old pixel components.
- Uint8 r, g, b, a;
- SDL_GetRGBA( *pixel_ptr, fmt, &r, &g, &b, &a );
- // Set the pixel with the new alpha.
- *pixel_ptr = SDL_MapRGBA( fmt, r, g, b, scale * a );
- }
- SDL_UnlockSurface(surface);
- }
- }
- // Modified Display text to set background transparent XD
- void DisplayText(char* fontFile, char* text, int x, int y, int size, int fR,
- int fG, int fB, int alpha)
- {
- // Open our font and set its size to the given parameter.
- TTF_Font* font = TTF_OpenFont(fontFile, size);
- if ( font == NULL )
- {
- printf("TTF_OpenFont: FAILED! Reason: %s\n", TTF_GetError());
- }
- SDL_Color foreground = { fR, fG, fB}; // Text color.
- //SDL_Color background = { bR, bG, bB }; // Color of what's behind the text. <- No need for this
- // This renders our text to a temporary surface. There
- // are other text functions, but this one looks nice.
- SDL_Surface* temp = TTF_RenderText_Blended(font, text, foreground);
- SetSurfaceAlpha( temp, alpha );
- // A structure storing the destination of our text.
- SDL_Rect destination = { x, y, 0, 0 };
- // Blit the text surface to our window surface, the NULL specifies the whole surface.
- SDL_BlitSurface(temp, NULL, g_Screen, &destination);
- // Always free memory!
- SDL_FreeSurface(temp);
- // Close the font.
- TTF_CloseFont(font);
- }
- int CreateApp()
- {
- //char tempString[100];
- // Main loop
- while(running){
- switch (appLoc)
- {
- case 0:
- Init();
- InitControls();
- ReadVolume();
- LoadCart();
- appLoc = 1;
- break;
- case 1:
- SplashScreen();
- break;
- case 2:
- Menu();
- break;
- case 3: //Launched app
- break;
- }
- }
- // We should dispose everything
- Shutdown();
- exit(0);
- return 0;
- }
- int main(int argc, char **argv){
- CreateApp();
- exit(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment