Initial commit

This commit is contained in:
Pòsweg 2020-07-05 08:38:35 +02:00
commit 39f69ad7d7
17 changed files with 342 additions and 0 deletions

96
.gitignore vendored Normal file
View File

@ -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

9
Makefile Normal file
View File

@ -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

BIN
assets/radial_gradient-black.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

BIN
assets/radial_gradient-blue.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

BIN
assets/radial_gradient-brown.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

BIN
assets/radial_gradient-green.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

BIN
assets/radial_gradient-pink.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

BIN
assets/radial_gradient-purple.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

BIN
assets/radial_gradient-red.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

BIN
assets/radial_gradient-yellow.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

58
src/fs.c Normal file
View File

@ -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;
}
}

13
src/fs.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef __FS_H__
#define __FS_H__
#include <dirent.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
int countFiles(char *path);
char **listFiles(char *path);
#endif

8
src/grid.c Normal file
View File

@ -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);
}

8
src/grid.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef __GRID_H__
#define __GRID_H__
#include <SDL2/SDL.h>
int gridHelper(SDL_Rect* rect, int index, SDL_Rect container, int zoom, int page);
#endif

125
src/main.c Normal file
View File

@ -0,0 +1,125 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <stdbool.h>
#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;
}

13
src/texture.c Normal file
View File

@ -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;
}

12
src/texture.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef __TEXTURE_H__
#define __TEXTURE_H__
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
SDL_Texture *PSX_loadTexture(char *path);
SDL_Rect PSX_GetTextureRect(SDL_Texture* texture);
#endif