141 lines
3.3 KiB
C
141 lines
3.3 KiB
C
#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("assets2/");
|
|
int nfiles = countFiles("assets2/");
|
|
|
|
// 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/3*2, SCREEN_HEIGHT};
|
|
SDL_Rect thumbs_rect = {40, 40, 40, 40};
|
|
int min_zoom = 1, max_zoom = 100;
|
|
int zoom = 25;
|
|
|
|
|
|
for(int i = 0; i < nfiles; i++){
|
|
gridHelper(&thumbs_rect, i, thumbs_container, 1, 0);
|
|
//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);
|
|
}
|
|
|
|
bool quit = false;
|
|
SDL_Event e;
|
|
while(!quit){
|
|
while(SDL_PollEvent(&e)!=0){
|
|
if(e.type == SDL_QUIT){
|
|
quit = true;
|
|
}
|
|
if(e.type == SDL_MOUSEWHEEL){
|
|
if(e.wheel.y > 0){ // scroll up
|
|
if(zoom < max_zoom && currentKeyStates[SDL_SCANCODE_LCTRL]){
|
|
zoom++;
|
|
}
|
|
}
|
|
else if(e.wheel.y < 0){ // scroll down
|
|
if(zoom > min_zoom && currentKeyStates[SDL_SCANCODE_LCTRL]){
|
|
zoom--;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
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, zoom, 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();
|
|
IMG_Quit();
|
|
return 0;
|
|
}
|