165 lines
4.8 KiB
C
165 lines
4.8 KiB
C
//
|
|
// Created by bruno on 4/24/25.
|
|
//
|
|
|
|
#include <dirent.h>
|
|
#include "util.h"
|
|
//#include "font.h"
|
|
|
|
//The window we'll be rendering to
|
|
SDL_Window *window = NULL;
|
|
volatile bool running = true;
|
|
|
|
bool debugMode = false;
|
|
|
|
//The surface contained by the window
|
|
SDL_Renderer *mainRenderer = NULL;
|
|
|
|
SDL_Rect screenRect;
|
|
|
|
SDL_Texture *createFlippedTexture(SDL_Renderer *renderer, SDL_Texture *src, SDL_RendererFlip flip) {
|
|
int w, h;
|
|
SDL_QueryTexture(src, NULL, NULL, &w, &h);
|
|
|
|
SDL_Texture *renderTarget = SDL_GetRenderTarget(renderer);
|
|
|
|
SDL_Texture *target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
|
|
SDL_SetRenderTarget(renderer, target);
|
|
SDL_RenderCopyEx(renderer, src, NULL, NULL, 0, NULL, flip);
|
|
SDL_SetRenderTarget(renderer, renderTarget);
|
|
|
|
return target;
|
|
}
|
|
|
|
SDL_Texture *createRotatedTexture(SDL_Renderer *renderer, SDL_Texture *src, double angle) {
|
|
int w, h;
|
|
SDL_QueryTexture(src, NULL, NULL, &w, &h);
|
|
SDL_Texture *renderTarget = SDL_GetRenderTarget(renderer);
|
|
|
|
SDL_Texture *target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
|
|
SDL_SetRenderTarget(renderer, target);
|
|
SDL_RenderCopyEx(renderer, src, NULL, NULL, angle, NULL, SDL_FLIP_NONE);
|
|
SDL_SetRenderTarget(renderer, renderTarget);
|
|
|
|
return target;
|
|
}
|
|
|
|
SDL_Texture *ScaleTexture(SDL_Renderer *renderer, SDL_Texture *src, int newWidth, int newHeight) {
|
|
SDL_Texture *scaledTex = SDL_CreateTexture(renderer,
|
|
SDL_PIXELFORMAT_RGBA8888,
|
|
SDL_TEXTUREACCESS_TARGET,
|
|
newWidth,
|
|
newHeight);
|
|
|
|
if (!scaledTex) {
|
|
SDL_Log("Failed to create target texture: %s", SDL_GetError());
|
|
return nullptr;
|
|
}
|
|
|
|
// Save current render target
|
|
SDL_Texture *oldTarget = SDL_GetRenderTarget(renderer);
|
|
|
|
SDL_SetRenderTarget(renderer, scaledTex);
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_Rect dst = {0, 0, newWidth, newHeight};
|
|
SDL_RenderCopy(renderer, src, NULL, &dst);
|
|
|
|
SDL_SetRenderTarget(renderer, oldTarget); // Restore
|
|
|
|
return scaledTex;
|
|
}
|
|
|
|
void DrawThickRect(SDL_Renderer *renderer, SDL_Rect rect, int thickness) {
|
|
for (int i = 0; i < thickness; i++) {
|
|
SDL_Rect r = {rect.x - i, rect.y - i, rect.w + i * 2, rect.h + i * 2};
|
|
SDL_RenderDrawRect(renderer, &r);
|
|
}
|
|
}
|
|
|
|
void renderBar(SDL_Renderer *renderer,
|
|
int x, int y, int width, int height,
|
|
int maxValue, int currentValue,
|
|
SDL_Color barColor, int margin) {
|
|
if (maxValue <= 0) return; // Avoid division by zero
|
|
|
|
// Clamp value
|
|
if (currentValue < 0) currentValue = 0;
|
|
if (currentValue == 0) {
|
|
return;
|
|
}
|
|
if (currentValue > maxValue) currentValue = maxValue;
|
|
|
|
// Calculate filled width based on currentValue
|
|
int filledWidth = (width * currentValue) / maxValue;
|
|
|
|
// Bar rectangle
|
|
SDL_Rect barRect = {x, y, filledWidth, height};
|
|
|
|
// Background rectangle with margin
|
|
SDL_Rect bgRect = {
|
|
x - margin,
|
|
y - margin,
|
|
width + margin * 2,
|
|
height + margin * 2
|
|
};
|
|
|
|
// Draw background (black)
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
SDL_RenderFillRect(renderer, &bgRect);
|
|
|
|
// Draw bar with provided color
|
|
SDL_SetRenderDrawColor(renderer, barColor.r, barColor.g, barColor.b, barColor.a);
|
|
SDL_RenderFillRect(renderer, &barRect);
|
|
|
|
char barString[20];
|
|
sprintf(barString, "%d/%d", currentValue, maxValue);
|
|
|
|
//renderText(mainRenderer, fonts[3], barString, width / 2, margin);
|
|
}
|
|
|
|
int cmpstringp(const void *p1, const void *p2) {
|
|
return strcmp(*(const char **) p1, *(const char **) p2);
|
|
}
|
|
|
|
|
|
// Helper function to iterate over sorted entries in a directory
|
|
void iterateSortedDir(const char *path, DirEntryCallback callback, SDL_Renderer *renderer) {
|
|
DIR *dir = opendir(path);
|
|
if (!dir) {
|
|
perror("opendir");
|
|
return;
|
|
}
|
|
|
|
struct dirent *entry;
|
|
char **names = NULL;
|
|
size_t count = 0;
|
|
|
|
// Collect file names
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
if (entry->d_name[0] == '.') continue;
|
|
names = realloc(names, sizeof(char *) * (count + 1));
|
|
if (!names) {
|
|
perror("realloc");
|
|
closedir(dir);
|
|
return;
|
|
}
|
|
names[count++] = strdup(entry->d_name);
|
|
}
|
|
closedir(dir);
|
|
|
|
// Sort entries
|
|
qsort(names, count, sizeof(char *), cmpstringp);
|
|
|
|
// Call the user-provided function for each file
|
|
if (names != NULL) {
|
|
for (size_t i = 0; i < count; i++) {
|
|
if (names[i] != NULL) {
|
|
callback(names[i], renderer);
|
|
free(names[i]);
|
|
}
|
|
}
|
|
free(names);
|
|
}
|
|
} |