Finished grid layout

This commit is contained in:
Pòsweg 2020-07-05 12:04:23 +02:00
parent 2852b6ed32
commit ef90d94569
3 changed files with 25 additions and 14 deletions

View File

@ -43,7 +43,7 @@ char **listFiles(char *path){
// plus the \0 character having in mind the size of each character // plus the \0 character having in mind the size of each character
strings[i] = malloc((strlen(ep->d_name) + strlen(path) + 1) * sizeof(char)); strings[i] = malloc((strlen(ep->d_name) + strlen(path) + 1) * sizeof(char));
//strcpy(strings[i],path); //strcpy(strings[i],path);
strcpy(strings[i],"assets/"); strcpy(strings[i],path);
strcat(strings[i],ep->d_name); strcat(strings[i],ep->d_name);
i++; // if not inside it will give size error since it counts . and .. i++; // if not inside it will give size error since it counts . and ..
if(i == nfiles) return strings; if(i == nfiles) return strings;

View File

@ -1,8 +1,21 @@
#include "grid.h" #include "grid.h"
int gridHelper(SDL_Rect* rect, int index, SDL_Rect screen, int zoom, int page){ int gridHelper(SDL_Rect* rect, int index, SDL_Rect screen, int zoom, int page){
rect->y = 0; #define MARGIN 20
rect->x = screen.w/10 * index * zoom / 10; #define MIN_PADDING 10
//printf("index %i -- x = %i | y = %i | w = %i | h = %i\n", index, rect->x, rect->y, rect->w, rect->h); int x_amount = (screen.w - MARGIN * 2) / (MIN_PADDING + rect->w);
//int padding = ((screen.w - (MARGIN * 2) - (rect->w * x_amount)) / (x_amount-1));
//
int x_offset = ((screen.w - MARGIN * 2) / x_amount);
int y_offset = ((screen.w - MARGIN * 2) / x_amount);
int center = (x_offset - rect->w) / 2;
screen.w = 200;
rect->w = 40 + zoom;
rect->h = 40 + zoom;
rect->y = MARGIN + (y_offset * (index / x_amount)) + center;
rect->x = MARGIN + (x_offset * (index - (x_amount * (index / x_amount)))) + center;
} }

View File

@ -51,8 +51,8 @@ int main(int argc, char* args[]) {
/* Init of the game */ /* Init of the game */
// Get image file names and their count // Get image file names and their count
char ** imageNames = listFiles("assets/"); char ** imageNames = listFiles("assets2/");
int nfiles = countFiles("assets/"); int nfiles = countFiles("assets2/");
// Allocate space for the images // Allocate space for the images
SDL_Texture **thumbs = (SDL_Texture **) malloc(sizeof(SDL_Texture *) * nfiles); SDL_Texture **thumbs = (SDL_Texture **) malloc(sizeof(SDL_Texture *) * nfiles);
@ -66,14 +66,14 @@ int main(int argc, char* args[]) {
fprintf(stderr, "Error on loading image at path \"%s\".\n", imageNames[i]); fprintf(stderr, "Error on loading image at path \"%s\".\n", imageNames[i]);
} }
else{ else{
fprintf(stderr, "Loaded image on at path \"%s\".\n", imageNames[i]); //fprintf(stderr, "Loaded image on at path \"%s\".\n", imageNames[i]);
} }
free(imageNames[i]); free(imageNames[i]);
imageNames[i] = NULL; imageNames[i] = NULL;
} }
SDL_Rect thumbs_container = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT}; SDL_Rect thumbs_container = {0, 0, SCREEN_WIDTH/3*2, SCREEN_HEIGHT};
SDL_Rect thumbs_rect = {40, 40, 40, 40}; SDL_Rect thumbs_rect = {40, 40, 40, 40};
int min_zoom = 1, max_zoom = 100; int min_zoom = 1, max_zoom = 100;
int zoom = 25; int zoom = 25;
@ -81,8 +81,7 @@ int main(int argc, char* args[]) {
for(int i = 0; i < nfiles; i++){ for(int i = 0; i < nfiles; i++){
gridHelper(&thumbs_rect, i, thumbs_container, 1, 0); gridHelper(&thumbs_rect, i, thumbs_container, 1, 0);
printf("index %i -- x = %i | y = %i | w = %i | h = %i\n", //printf("index %3i -- x = %5i | y = %4i | w = %3i | h = %3i\n",i, thumbs_rect.x, thumbs_rect.y, thumbs_rect.w, thumbs_rect.h);
i, thumbs_rect.x, thumbs_rect.y, thumbs_rect.w, thumbs_rect.h);
} }
bool quit = false; bool quit = false;
@ -94,15 +93,13 @@ int main(int argc, char* args[]) {
} }
if(e.type == SDL_MOUSEWHEEL){ if(e.type == SDL_MOUSEWHEEL){
if(e.wheel.y > 0){ // scroll up if(e.wheel.y > 0){ // scroll up
if(zoom < max_zoom){ if(zoom < max_zoom && currentKeyStates[SDL_SCANCODE_LCTRL]){
zoom++; zoom++;
printf("zoom = %i\n", zoom);
} }
} }
else if(e.wheel.y < 0){ // scroll down else if(e.wheel.y < 0){ // scroll down
if(zoom > min_zoom){ if(zoom > min_zoom && currentKeyStates[SDL_SCANCODE_LCTRL]){
zoom--; zoom--;
printf("zoom = %i\n", zoom);
} }
} }
} }
@ -138,5 +135,6 @@ int main(int argc, char* args[]) {
renderer = NULL; renderer = NULL;
SDL_Quit(); SDL_Quit();
IMG_Quit();
return 0; return 0;
} }