commit 39f69ad7d780ae7e49372b1fcbed2b62ec87055f Author: Pòsweg Date: Sun Jul 5 08:38:35 2020 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7de9058 --- /dev/null +++ b/.gitignore @@ -0,0 +1,96 @@ +# Created by https://www.toptal.com/developers/gitignore/api/vim,c,linux +# Edit at https://www.toptal.com/developers/gitignore?templates=vim,c,linux + +### C ### +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### Vim ### +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +# End of https://www.toptal.com/developers/gitignore/api/vim,c,linux + +main +build diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ed280fb --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +SOURCES = src/*.c +LIBS = -lSDL2 -lSDL2_image +CFLAGS = -DDEBUG + +main: $(SOURCES) src/*.h + gcc $(SOURCES) $(LIBS) $(CFLAGS) -o main + +run: main + ./main diff --git a/assets/radial_gradient-black.jpg b/assets/radial_gradient-black.jpg new file mode 100755 index 0000000..fed924a Binary files /dev/null and b/assets/radial_gradient-black.jpg differ diff --git a/assets/radial_gradient-blue.jpg b/assets/radial_gradient-blue.jpg new file mode 100755 index 0000000..f551ab4 Binary files /dev/null and b/assets/radial_gradient-blue.jpg differ diff --git a/assets/radial_gradient-brown.jpg b/assets/radial_gradient-brown.jpg new file mode 100755 index 0000000..897e81f Binary files /dev/null and b/assets/radial_gradient-brown.jpg differ diff --git a/assets/radial_gradient-green.jpg b/assets/radial_gradient-green.jpg new file mode 100755 index 0000000..6adc81d Binary files /dev/null and b/assets/radial_gradient-green.jpg differ diff --git a/assets/radial_gradient-pink.jpg b/assets/radial_gradient-pink.jpg new file mode 100755 index 0000000..29b6597 Binary files /dev/null and b/assets/radial_gradient-pink.jpg differ diff --git a/assets/radial_gradient-purple.jpg b/assets/radial_gradient-purple.jpg new file mode 100755 index 0000000..2678f78 Binary files /dev/null and b/assets/radial_gradient-purple.jpg differ diff --git a/assets/radial_gradient-red.jpg b/assets/radial_gradient-red.jpg new file mode 100755 index 0000000..d2cdf8d Binary files /dev/null and b/assets/radial_gradient-red.jpg differ diff --git a/assets/radial_gradient-yellow.jpg b/assets/radial_gradient-yellow.jpg new file mode 100755 index 0000000..1c82307 Binary files /dev/null and b/assets/radial_gradient-yellow.jpg differ diff --git a/src/fs.c b/src/fs.c new file mode 100644 index 0000000..f168420 --- /dev/null +++ b/src/fs.c @@ -0,0 +1,58 @@ +#include "fs.h" + +int countFiles(char *path){ + DIR *dp; + int i = 0; + struct dirent *ep; + dp = opendir (path); + + if (dp != NULL){ + while (ep = readdir (dp)) i++; + + (void) closedir (dp); + } + else{ + fprintf(stderr, "Unable to open directory \"%s\".\n", path); + } + + return i - 2; +} + +char **listFiles(char *path){ + int nfiles = countFiles(path); + + if(nfiles < 0){ + fprintf(stderr, "There are no files in the specified assets directory.\n"); + return 0; + } + + + DIR *dp; + struct dirent *ep; + dp = opendir(path); + + if (dp != NULL) { + char **strings = (char **) malloc(sizeof(char **) * nfiles); + + int i = 0; // For assigning the string to the string array + + /* print all the files and directories within directory */ + while ((ep = readdir (dp)) != NULL) { + if(strcmp(ep->d_name,".") && strcmp(ep->d_name,"..")){ + // All this means is allocate space for the length of the string + // plus the \0 character having in mind the size of each character + strings[i] = malloc((strlen(ep->d_name) + strlen(path) + 1) * sizeof(char)); + //strcpy(strings[i],path); + strcpy(strings[i],"assets/"); + strcat(strings[i],ep->d_name); + i++; // if not inside it will give size error since it counts . and .. + if(i == nfiles) return strings; + } + } + closedir (dp); + } else { + /* could not open directory */ + perror ("could not open directory\n"); + return 0; + } +} diff --git a/src/fs.h b/src/fs.h new file mode 100644 index 0000000..c2ba74b --- /dev/null +++ b/src/fs.h @@ -0,0 +1,13 @@ +#ifndef __FS_H__ +#define __FS_H__ + +#include +#include +#include +#include +#include + +int countFiles(char *path); +char **listFiles(char *path); + +#endif diff --git a/src/grid.c b/src/grid.c new file mode 100644 index 0000000..cbf027d --- /dev/null +++ b/src/grid.c @@ -0,0 +1,8 @@ +#include "grid.h" + +int gridHelper(SDL_Rect* rect, int index, SDL_Rect screen, int zoom, int page){ + rect->y = 0; + rect->x = screen.w/20 * index; + + //printf("index %i -- x = %i | y = %i | w = %i | h = %i\n", index, rect->x, rect->y, rect->w, rect->h); +} diff --git a/src/grid.h b/src/grid.h new file mode 100644 index 0000000..ef11653 --- /dev/null +++ b/src/grid.h @@ -0,0 +1,8 @@ +#ifndef __GRID_H__ +#define __GRID_H__ + +#include + +int gridHelper(SDL_Rect* rect, int index, SDL_Rect container, int zoom, int page); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..fea8ed9 --- /dev/null +++ b/src/main.c @@ -0,0 +1,125 @@ +#include +#include +#include +#include + +#define _DEBUG + +#include "texture.h" +#include "fs.h" +#include "grid.h" + +#define SCREEN_WIDTH 640 +#define SCREEN_HEIGHT 480 + +int main(int argc, char* args[]) { + SDL_Window* window = NULL; + SDL_Renderer* renderer = NULL; + + /* SDL Initialization */ + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + fprintf(stderr, "Could not initialize SDL2: %s\n", SDL_GetError()); + return 1; + } + if (IMG_Init(IMG_INIT_JPG || IMG_INIT_PNG) < 0) { + fprintf(stderr, "Could not initialize SDL2_image: %s\n", IMG_GetError()); + } + + window = SDL_CreateWindow("pscsp", + SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + SCREEN_WIDTH, SCREEN_HEIGHT, + SDL_WINDOW_SHOWN + ); + if (window == NULL) { + fprintf(stderr, "Could not create window: %s\n", SDL_GetError()); + return 1; + } + + renderer = SDL_CreateRenderer( + window, + -1, + SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC + ); + if (renderer == NULL) { + fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError()); + return 1; + } + + const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL); + + /* Init of the game */ + + // Get image file names and their count + char ** imageNames = listFiles("assets/"); + int nfiles = countFiles("assets/"); + + // Allocate space for the images + SDL_Texture **thumbs = (SDL_Texture **) malloc(sizeof(SDL_Texture *) * nfiles); + + // Load the images (and free the strings allocated before) + for(int i = 0; i < nfiles; i++){ + thumbs[i] = NULL; + thumbs[i] = IMG_LoadTexture(renderer, imageNames[i]); + + if(thumbs[i] == NULL){ + fprintf(stderr, "Error on loading image at path \"%s\".\n", imageNames[i]); + } + else{ + fprintf(stderr, "Loaded image on at path \"%s\".\n", imageNames[i]); + } + + free(imageNames[i]); + imageNames[i] = NULL; + } + + SDL_Rect thumbs_container = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT}; + SDL_Rect thumbs_rect = {40, 40, 40, 40}; + + for(int i = 0; i < nfiles; i++){ + gridHelper(&thumbs_rect, i, thumbs_container, 1, 0); + printf("index %i -- x = %i | y = %i | w = %i | h = %i\n", + i, thumbs_rect.x, thumbs_rect.y, thumbs_rect.w, thumbs_rect.h); + } + + bool quit = false; + SDL_Event e; + while(!quit){ + while(SDL_PollEvent(&e)!=0){ + if(e.type == SDL_QUIT){ + quit = true; + } + } + if(currentKeyStates[SDL_SCANCODE_ESCAPE]){ + return 0; + } + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF); + SDL_RenderClear(renderer); + + // Thumbnail grid rendering + //thumbs_rect.x = 0; + //thumbs_rect.y = 0; + for(int i = 0; i < nfiles; i++){ + gridHelper(&thumbs_rect, i, thumbs_container, 1, 0); + SDL_RenderCopy(renderer, thumbs[i], NULL, &thumbs_rect); + } + + SDL_RenderPresent(renderer); + } + + // Deallocations + printf("\nDeallocating assets...\n"); + for(int i = 0; i < nfiles; i++){ + SDL_DestroyTexture(thumbs[i]); + thumbs[i] = NULL; + } + + SDL_DestroyWindow(window); + window = NULL; + SDL_DestroyRenderer(renderer); + renderer = NULL; + + SDL_Quit(); + return 0; +} diff --git a/src/texture.c b/src/texture.c new file mode 100644 index 0000000..57355d1 --- /dev/null +++ b/src/texture.c @@ -0,0 +1,13 @@ +#include "texture.h" + +SDL_Texture *PSX_loadTexture(char *path){ + SDL_Texture *texture = NULL; + + fprintf(stderr, "Image file string not valid: It's NULL\n"); + + #ifdef DEBUG + printf("Loaded \"%s\"\n", path); + #endif + + return texture; +} diff --git a/src/texture.h b/src/texture.h new file mode 100644 index 0000000..08f7dd6 --- /dev/null +++ b/src/texture.h @@ -0,0 +1,12 @@ +#ifndef __TEXTURE_H__ +#define __TEXTURE_H__ + +#include +#include +#include + +SDL_Texture *PSX_loadTexture(char *path); + +SDL_Rect PSX_GetTextureRect(SDL_Texture* texture); + +#endif