Some more hopefully working progress
This commit is contained in:
@@ -18,7 +18,7 @@ void audio_callback(void *userdata, Uint8 *stream, int len) {
|
||||
SynthVoice *voice = &audio->synthVoices[v];
|
||||
if (voice->volume == 0 || voice->frequency == 0) continue;
|
||||
|
||||
float sample = 0.0f;
|
||||
float sample;
|
||||
float t = (float) voice->phase / 255.0f * 2.0f - 1.0f;
|
||||
|
||||
switch (voice->waveform) {
|
||||
|
81
util/util.c
81
util/util.c
@@ -2,6 +2,7 @@
|
||||
// Created by bruno on 4/24/25.
|
||||
//
|
||||
|
||||
#include <dirent.h>
|
||||
#include "util.h"
|
||||
//#include "font.h"
|
||||
|
||||
@@ -9,16 +10,20 @@
|
||||
SDL_Window *window = NULL;
|
||||
volatile bool running = true;
|
||||
|
||||
//The surface contained by the window
|
||||
SDL_Renderer *renderer = NULL;
|
||||
bool debugMode = false;
|
||||
|
||||
SDL_Texture* createFlippedTexture(SDL_Renderer* renderer, SDL_Texture* src, SDL_RendererFlip flip) {
|
||||
//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_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);
|
||||
@@ -26,12 +31,12 @@ SDL_Texture* createFlippedTexture(SDL_Renderer* renderer, SDL_Texture* src, SDL_
|
||||
return target;
|
||||
}
|
||||
|
||||
SDL_Texture* createRotatedTexture(SDL_Renderer* renderer, SDL_Texture* src, double angle) {
|
||||
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_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);
|
||||
@@ -39,8 +44,8 @@ SDL_Texture* createRotatedTexture(SDL_Renderer* renderer, SDL_Texture* src, doub
|
||||
return target;
|
||||
}
|
||||
|
||||
SDL_Texture* ScaleTexture(SDL_Renderer* renderer, SDL_Texture* src, int newWidth, int newHeight) {
|
||||
SDL_Texture* scaledTex = SDL_CreateTexture(renderer,
|
||||
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,
|
||||
@@ -52,13 +57,13 @@ SDL_Texture* ScaleTexture(SDL_Renderer* renderer, SDL_Texture* src, int newWidth
|
||||
}
|
||||
|
||||
// Save current render target
|
||||
SDL_Texture* oldTarget = SDL_GetRenderTarget(renderer);
|
||||
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_Rect dst = {0, 0, newWidth, newHeight};
|
||||
SDL_RenderCopy(renderer, src, NULL, &dst);
|
||||
|
||||
SDL_SetRenderTarget(renderer, oldTarget); // Restore
|
||||
@@ -66,9 +71,9 @@ SDL_Texture* ScaleTexture(SDL_Renderer* renderer, SDL_Texture* src, int newWidth
|
||||
return scaledTex;
|
||||
}
|
||||
|
||||
void DrawThickRect(SDL_Renderer* renderer, SDL_Rect rect, int thickness) {
|
||||
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_Rect r = {rect.x - i, rect.y - i, rect.w + i * 2, rect.h + i * 2};
|
||||
SDL_RenderDrawRect(renderer, &r);
|
||||
}
|
||||
}
|
||||
@@ -76,8 +81,7 @@ void DrawThickRect(SDL_Renderer* renderer, SDL_Rect rect, int thickness) {
|
||||
void renderBar(SDL_Renderer *renderer,
|
||||
int x, int y, int width, int height,
|
||||
int maxValue, int currentValue,
|
||||
SDL_Color barColor, int margin)
|
||||
{
|
||||
SDL_Color barColor, int margin) {
|
||||
if (maxValue <= 0) return; // Avoid division by zero
|
||||
|
||||
// Clamp value
|
||||
@@ -91,7 +95,7 @@ void renderBar(SDL_Renderer *renderer,
|
||||
int filledWidth = (width * currentValue) / maxValue;
|
||||
|
||||
// Bar rectangle
|
||||
SDL_Rect barRect = { x, y, filledWidth, height };
|
||||
SDL_Rect barRect = {x, y, filledWidth, height};
|
||||
|
||||
// Background rectangle with margin
|
||||
SDL_Rect bgRect = {
|
||||
@@ -112,5 +116,50 @@ void renderBar(SDL_Renderer *renderer,
|
||||
char barString[20];
|
||||
sprintf(barString, "%d/%d", currentValue, maxValue);
|
||||
|
||||
//renderText(renderer, fonts[3], barString, width / 2, margin);
|
||||
//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);
|
||||
}
|
||||
}
|
12
util/util.h
12
util/util.h
@@ -11,7 +11,9 @@ extern SDL_Window *window;
|
||||
extern volatile bool running;
|
||||
|
||||
//The surface contained by the window
|
||||
extern SDL_Renderer *renderer;
|
||||
extern SDL_Renderer *mainRenderer;
|
||||
|
||||
extern SDL_Rect screenRect;
|
||||
|
||||
typedef enum {
|
||||
ORIENT_LEFT_DOWN,
|
||||
@@ -25,6 +27,8 @@ typedef enum {
|
||||
ORIENT_DIRECTION_COUNT
|
||||
} OrientDirection;
|
||||
|
||||
extern bool debugMode;
|
||||
|
||||
SDL_Texture *createRotatedTexture(SDL_Renderer *renderer, SDL_Texture *src, double angle);
|
||||
|
||||
SDL_Texture *createFlippedTexture(SDL_Renderer *renderer, SDL_Texture *src, SDL_RendererFlip flip);
|
||||
@@ -33,6 +37,12 @@ SDL_Texture* ScaleTexture(SDL_Renderer* renderer, SDL_Texture* src, int newWidth
|
||||
|
||||
void DrawThickRect(SDL_Renderer* renderer, SDL_Rect rect, int thickness);
|
||||
|
||||
// Define a function pointer type for your callback
|
||||
typedef void (*DirEntryCallback)(const char *filename, SDL_Renderer *renderer);
|
||||
|
||||
// Helper function to iterate over sorted entries in a directory
|
||||
void iterateSortedDir(const char *path, DirEntryCallback callback, SDL_Renderer *renderer);
|
||||
|
||||
void renderBar(SDL_Renderer *renderer,
|
||||
int x, int y, int width, int height,
|
||||
int maxValue, int currentValue,
|
||||
|
Reference in New Issue
Block a user