commit 27fae5ae15878b8f830d19228643544dd7b5187a
parent 0b116edc2908fd23a1ca4b787309cfe2275a4018
Author: klewer-martin <martin.cachari@gmail.com>
Date: Wed, 13 Apr 2022 20:00:18 -0300
Module reorganiztion
Diffstat:
9 files changed, 117 insertions(+), 52 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,7 +1,7 @@
CC := cc
CLIBS := `sdl2-config --libs --cflags`
CFLAGS := -lSDL2_ttf -lm -Werror -pedantic -ansi -std=c99 -g -pthread
-SRCS := main.c sav.c util.c sort.c drw.c sdl_extra.c
+SRCS := main.c sav.c util.c sort.c drw.c sdl_extra.c array.c
OBJS := $(SRCS:.c=.o)
LIBS := array.h status.h
diff --git a/array.c b/array.c
@@ -0,0 +1,10 @@
+#include "array.h"
+
+#include <time.h>
+
+void
+shuffle(Arr *arr) {
+ srand((unsigned int)time(NULL));
+ for(size_t i = 0; i < arr->len; i++)
+ while(!(arr->v[i] = rand() % ARR_MAX));
+}
diff --git a/array.h b/array.h
@@ -11,4 +11,6 @@ typedef struct {
size_t len;
} Arr;
+void shuffle(Arr *arr);
+
#endif
diff --git a/drw.c b/drw.c
@@ -164,3 +164,56 @@ Drw_destroy(Drw *drw) {
free(drw->bar_text);
free(drw);
}
+
+void drw_array_graph_sav_free(Drw *drw, Arr *arr) {
+ int x, w, h;
+
+ SDL_GetWindowSize(drw->win, &w, &h);
+
+ SDL_SetRenderDrawColor(drw->rend, 29, 28, 28, 0);
+ SDL_RenderClear(drw->rend);
+
+ size_t i;
+ for(i = x = 0; i < arr->len; i++, x += RECT_WIDTH) {
+ if(i == sav->sel) drw_element_color(drw, x, h, arr->v[i], SEL_COLOR);
+ else if(i == sav->cmp) drw_element_color(drw, x, h, arr->v[i], CMP_COLOR);
+ else drw_element_color(drw, x, h, arr->v[i], NORM_COLOR);
+ }
+ /* drw_status_bar(drw, sav); */
+}
+
+void
+void drw_status_bar_sav_free(Drw *drw, SAV *sav) {
+ SDL_Rect rect;
+ int bar_border = 2;
+
+ rect.x = bar_border; /* top left + x */
+ rect.y = drw->h - bar_border; /* top left + y, (y < 0) */
+ rect.w = drw->w - (2 * bar_border); /* fixed width */
+ rect.h = -BAR_HEIGHT;
+
+ SDL_RenderDrawRect(drw->rend, &rect);
+
+ /* TODO: Make a variable to store statusbar background color */
+ SDL_SetRenderDrawColor(drw->rend, 0, 0, 0, 0); /* RGBA */
+ SDL_RenderFillRect(drw->rend, &rect);
+
+ if(sav->status == UPDATE) {
+ /* sprintf(drw->bar_text, "Press SPACE to start sorting the array or ESC/q to quit"); */
+ snprintf(drw->bar_text, drw->bar_text_len - 2,
+ "SORTING (%s sort) L: %ld, C: %ld, S: %ld",
+ algo_strings[sav->sel_algo], sav->arr->len, sav->cmps,
+ sav->swps);
+
+ drw_text(drw, drw->bar_text, 0, drw->h - drw->font_size - 5);
+ } else if(sav->status == SORTED) {
+ snprintf(drw->bar_text, drw->bar_text_len - 2,
+ "SORTED (%s sort) done in %.2fs, L: %ld, C: %ld, S: %ld, extra storage used: %ld Bytes",
+ algo_strings[sav->sel_algo],
+ (double)(sav->tf - sav->ti) / CLOCKS_PER_SEC,
+ sav->arr->len, sav->cmps, sav->swps, sav->B_used);
+
+ drw_text(drw, drw->bar_text, 0, drw->h - drw->font_size - 5);
+ }
+ memset(drw->bar_text, 0, sizeof(char) * drw->bar_text_len);
+}
diff --git a/drw.h b/drw.h
@@ -50,4 +50,7 @@ void drw_text(Drw *drw, char *text, int x, int y);
/* void drw_array_graph(Drw *drw, SAV *sav); */
/* void drw_status_bar(Drw *drw, SAV *sav); */
+void drw_array_graph_sav_free(Drw *, Arr *);
+void drw_status_bar_sav_free(Drw *drw, SAV *sav);
+
#endif // __DRAW_H__
diff --git a/main.c b/main.c
@@ -5,6 +5,10 @@
#include "sort.h"
#include "util.h"
#include "sdl_extra.h"
+#include "array.h"
+
+void check_events(Drw *, SAV *);
+void *routine_wrapper(void *);
void *
routine_wrapper(void *arg) {
@@ -24,13 +28,6 @@ routine_wrapper(void *arg) {
return NULL;
}
-void
-shuffle(Arr *arr) {
- srand((unsigned int)time(NULL));
- for(size_t i = 0; i < arr->len; i++)
- while(!(arr->v[i] = rand() % ARR_MAX));
-}
-
int
main (void) {
SAV *sav;
@@ -82,3 +79,46 @@ main (void) {
Drw_destroy(drw);
return 0;
}
+
+void
+check_events(Drw *drw, SAV *sav) {
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch(event.type) {
+ case SDL_QUIT: sav->status = STOP; break;
+ case SDL_KEYDOWN:
+ switch(event.key.keysym.scancode) {
+ /* case SDL_SCANCODE_EQUALS: */
+ /* if(speed > SPEED_MAX) speed -= SPEED_STEP; */
+ /* break; */
+ /* case SDL_SCANCODE_MINUS: */
+ /* if(speed < SPEED_MIN) speed += SPEED_STEP; */
+ /* break; */
+ /* case SDL_SCANCODE_P: */
+ /* if(status == PAUSE) *status = RUN; */
+ /* else *status = PAUSE; */
+ /* break; */
+ case SDL_SCANCODE_R:
+ if(sav->status == SORTED) sav->status = RESTART;
+ else break;
+ default: break;
+ }
+ case SDL_WINDOWEVENT:
+ switch(event.window.event) {
+ case SDL_WINDOWEVENT_RESIZED:
+ SDL_Log("Window resized to %dx%d", event.window.data1, event.window.data2);
+ drw->w = event.window.data1;
+ drw->h = event.window.data2;
+
+ /* set new window borders */
+ drw->x_border = (drw->w / 2) - ((sav->arr->len * RECT_WIDTH) / 2);
+ drw->y_border = (drw->h / 2) - (ARR_MAX / 2);
+
+ break;
+ default: break;
+ }
+ default: break;
+ }
+ }
+}
+
diff --git a/sav.c b/sav.c
@@ -39,45 +39,3 @@ SAV_destroy(SAV *sav) {
free(sav->arr);
free(sav);
}
-
-void
-check_events(Drw *drw, SAV *sav) {
- SDL_Event event;
- while (SDL_PollEvent(&event)) {
- switch(event.type) {
- case SDL_QUIT: sav->status = STOP; break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.scancode) {
- /* case SDL_SCANCODE_EQUALS: */
- /* if(speed > SPEED_MAX) speed -= SPEED_STEP; */
- /* break; */
- /* case SDL_SCANCODE_MINUS: */
- /* if(speed < SPEED_MIN) speed += SPEED_STEP; */
- /* break; */
- /* case SDL_SCANCODE_P: */
- /* if(status == PAUSE) *status = RUN; */
- /* else *status = PAUSE; */
- /* break; */
- case SDL_SCANCODE_R:
- if(sav->status == SORTED) sav->status = RESTART;
- else break;
- default: break;
- }
- case SDL_WINDOWEVENT:
- switch(event.window.event) {
- case SDL_WINDOWEVENT_RESIZED:
- SDL_Log("Window resized to %dx%d", event.window.data1, event.window.data2);
- drw->w = event.window.data1;
- drw->h = event.window.data2;
-
- /* set new window borders */
- drw->x_border = (drw->w / 2) - ((sav->arr->len * RECT_WIDTH) / 2);
- drw->y_border = (drw->h / 2) - (ARR_MAX / 2);
-
- break;
- default: break;
- }
- default: break;
- }
- }
-}
diff --git a/sav.h b/sav.h
@@ -25,7 +25,7 @@ typedef struct {
clock_t ti, tf;
status_t status;
sort_t sel_algo;
-} SAV;
+} SAV; // Sort?
extern char *algo_strings[SORT_MAX_ALGORITHMS];
diff --git a/sdl_extra.h b/sdl_extra.h
@@ -18,7 +18,6 @@
status_t SDL_setup(SDL_Window **win, SDL_Renderer **rend);
status_t SDL_cleanup(SDL_Window *win, SDL_Renderer *rend);
-status_t SDL_check_events(Drw *drw, SAV *sav);
#endif