commit 0b116edc2908fd23a1ca4b787309cfe2275a4018
parent 7b58aefa91c06a7c6eaaf39914e208e8c192e6a3
Author: klewer-martin <martin.cachari@gmail.com>
Date: Wed, 13 Apr 2022 19:40:42 -0300
Modules reorganiztion
Diffstat:
M | Makefile | | | 3 | ++- |
A | array.h | | | 14 | ++++++++++++++ |
M | drw.c | | | 24 | ++++++++++++++++++------ |
M | drw.h | | | 20 | +++++++++++--------- |
M | main.c | | | 29 | +++++++++++------------------ |
M | sav.c | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++-------- |
M | sav.h | | | 13 | +++++++++---- |
M | sdl_extra.c | | | 67 | +++++++++++++------------------------------------------------------ |
M | sdl_extra.h | | | 28 | +++++++++++++--------------- |
M | sort.c | | | 3 | ++- |
M | sort.h | | | 1 | - |
A | status.h | | | 19 | +++++++++++++++++++ |
M | util.h | | | 27 | +-------------------------- |
13 files changed, 160 insertions(+), 143 deletions(-)
diff --git a/Makefile b/Makefile
@@ -3,6 +3,7 @@ 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
OBJS := $(SRCS:.c=.o)
+LIBS := array.h status.h
TARGET := sav
@@ -10,7 +11,7 @@ TARGET := sav
all: $(TARGET) clean
-$(TARGET): $(OBJS)
+$(TARGET): $(OBJS) $(HEADERS) $(LIBS)
$(CC) $(CLIBS) $(CFLAGS) -o $@ $^
%.o: %.c
diff --git a/array.h b/array.h
@@ -0,0 +1,14 @@
+#ifndef __ARRAY_H__
+#define __ARRAY_H__
+
+#include <stdlib.h>
+
+#define ARR_LEN 128
+#define ARR_MAX 500
+
+typedef struct {
+ int *v;
+ size_t len;
+} Arr;
+
+#endif
diff --git a/drw.c b/drw.c
@@ -38,7 +38,7 @@ drw_array_graph(Drw *drw, SAV *sav) {
else if(i == sav->cmp) drw_element_color(drw, x, h, sav->arr->v[i], CMP_COLOR);
else drw_element_color(drw, x, h, sav->arr->v[i], NORM_COLOR);
}
- drw_status_bar(drw, sav);
+ /* drw_status_bar(drw, sav); */
}
void
@@ -77,7 +77,8 @@ drw_status_bar(Drw *drw, SAV *sav) {
memset(drw->bar_text, 0, sizeof(char) * drw->bar_text_len);
}
-void drw_text(Drw *drw, char *text, int x, int y) {
+void
+drw_text(Drw *drw, char *text, int x, int y) {
drw->text_surface = TTF_RenderText_Blended(drw->font, text, drw->text_color);
drw->text_texture = SDL_CreateTextureFromSurface(drw->rend, drw->text_surface);
@@ -89,13 +90,22 @@ void drw_text(Drw *drw, char *text, int x, int y) {
SDL_RenderCopy(drw->rend, drw->text_texture, NULL, &drw->bar_text_rect);
}
-status_t DRW_New(SDL_Renderer *rend, SDL_Window *win, Drw **drw) {
+status_t
+Drw_new(Drw **drw) {
+ SDL_Renderer *rend;
+ SDL_Window *win;
+ TTF_Font *font;
+
if((*drw = (Drw *)malloc(sizeof(Drw))) == NULL)
return ERROR_MEMORY_ALLOC;
- TTF_Font *font = TTF_OpenFont(FONT_NAME, FONT_SIZE);
+ SDL_setup(&win, &rend);
+ (*drw)->rend = rend;
+ (*drw)->win = win;
+
+ font = TTF_OpenFont(FONT_NAME, FONT_SIZE);
- if(!font) {
+ if(font == NULL) {
fprintf(stderr, "TTF_OpenFont: %s\n", TTF_GetError());
return ERROR_OPENING_FONT;
}
@@ -145,9 +155,11 @@ status_t DRW_New(SDL_Renderer *rend, SDL_Window *win, Drw **drw) {
return OK;
}
-void DRW_Destroy(Drw *drw) {
+void
+Drw_destroy(Drw *drw) {
if(drw == NULL) return;
+ SDL_cleanup(drw->win, drw->rend);
TTF_CloseFont(drw->font);
free(drw->bar_text);
free(drw);
diff --git a/drw.h b/drw.h
@@ -4,16 +4,16 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
-#include "sav.h"
+/* #include "sav.h" */
#include "util.h"
-#include "sort.h"
+/* #include "sort.h" */
+#include "sdl_extra.h"
#define SEL_COLOR 0x00FF0000 // RGBA (A not used rn)
#define CMP_COLOR 0x00FFFF00
#define NORM_COLOR 0xFF000000
#define FONT_SIZE 12
-/* #define FONT_NAME "/usr/share/fonts/termite-style/Ubuntu.ttf" */
#define FONT_NAME "/home/mk/.local/share/fonts/VictorMono-Bold.ttf"
#define FONT_COLOR 0xBBBBBB
@@ -22,7 +22,10 @@
#define WIN_MIN_W 800
#define WIN_MIN_H 600
-#define WINDOW_TITLE "SAV - Sorting Algorithms Visualized"
+#define X_BORDER 40
+#define Y_BORDER 40
+#define TOP_BORDER 50
+#define RECT_WIDTH 5
typedef struct {
SDL_Renderer *rend;
@@ -37,15 +40,14 @@ typedef struct {
char *bar_text;
} Drw;
-status_t DRW_New(SDL_Renderer *rend, SDL_Window *win, Drw **drw);
-void DRW_Destroy(Drw *drw);
+status_t Drw_new(Drw **drw);
+void Drw_destroy(Drw *drw);
void drw_element(SDL_Renderer *rend, int x, int y, int h);
void drw_element_color(Drw *drw, int x, int y, int h, unsigned int col);
-void drw_array_graph(Drw *drw, SAV *sav);
-void drw_status_bar(Drw *drw, SAV *sav);
void drw_text(Drw *drw, char *text, int x, int y);
-void loading_animation(Drw *drw, SAV *sav);
+/* void drw_array_graph(Drw *drw, SAV *sav); */
+/* void drw_status_bar(Drw *drw, SAV *sav); */
#endif // __DRAW_H__
diff --git a/main.c b/main.c
@@ -35,16 +35,12 @@ int
main (void) {
SAV *sav;
Drw *drw;
- SDL_Renderer *rend;
- SDL_Window *win;
pthread_t p1;
status_t st;
- setup(&win, &rend);
-
- if((st = SAV_New(&sav)) != OK) goto end;
- if((st = DRW_New(rend, win, &drw)) != OK) goto end;
+ if((st = SAV_new(&sav)) != OK) goto end;
+ if((st = Drw_new(&drw)) != OK) goto end;
/* assigns random values to array */
shuffle(sav->arr);
@@ -55,20 +51,19 @@ main (void) {
/* selecting the sorting algorithms */
sav->sel_algo = QUICK_SORT;
- sav->status = RUN;
-
/* main loop */
+ sav->status = RUN;
while(sav->status != STOP) {
- check_events(drw, sav);
+ /* check_events(drw, sav); */
if(sav->status == UPDATE) {
- drw_array_graph(drw, sav);
+ /* drw_array_graph(drw, sav); */
sav->status = RUN;
- SDL_RenderPresent(rend);
+ SDL_RenderPresent(drw->rend);
}
if(sav->status == SORTED) {
/* p1 ended */
- drw_array_graph(drw, sav);
- SDL_RenderPresent(rend);
+ /* drw_array_graph(drw, sav); */
+ SDL_RenderPresent(drw->rend);
}
if(sav->status == RESTART) {
/* this state can only be achived if p1 ended */
@@ -80,12 +75,10 @@ main (void) {
}
}
-end:
+ end:
pthread_join(p1, NULL);
- SAV_Destroy(sav);
- DRW_Destroy(drw);
-
- cleanup(win, rend);
+ SAV_destroy(sav);
+ Drw_destroy(drw);
return 0;
}
diff --git a/sav.c b/sav.c
@@ -1,9 +1,4 @@
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdbool.h>
-
-#include "drw.h"
-#include "util.h"
+#include "sav.h"
char *algo_strings[SORT_MAX_ALGORITHMS] = {
"bubble",
@@ -12,7 +7,8 @@ char *algo_strings[SORT_MAX_ALGORITHMS] = {
"quick"
};
-status_t SAV_New(SAV **sav) {
+status_t
+SAV_new(SAV **sav) {
if((*sav = (SAV *)malloc(sizeof(SAV))) == NULL)
return ERROR_MEMORY_ALLOC;
@@ -35,10 +31,53 @@ status_t SAV_New(SAV **sav) {
return 0;
}
-void SAV_Destroy(SAV *sav) {
+void
+SAV_destroy(SAV *sav) {
if(sav == NULL) return;
free(sav->arr->v);
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
@@ -1,11 +1,16 @@
#ifndef __SAV_H__
#define __SAV_H__
-#include "util.h"
-
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdbool.h>
#include <time.h>
#include <SDL2/SDL.h>
+#include "drw.h"
+#include "util.h"
+#include "array.h"
+
typedef enum {
BUBBLE_SORT = 0,
INSERTION_SORT,
@@ -24,7 +29,7 @@ typedef struct {
extern char *algo_strings[SORT_MAX_ALGORITHMS];
-status_t SAV_New(SAV **sav);
-void SAV_Destroy(SAV *sav);
+status_t SAV_new(SAV **sav);
+void SAV_destroy(SAV *sav);
#endif
diff --git a/sdl_extra.c b/sdl_extra.c
@@ -1,55 +1,13 @@
#include "sdl_extra.h"
-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;
- }
- }
-}
-
-void
-setup(SDL_Window **win, SDL_Renderer **rend) {
+status_t
+SDL_setup(SDL_Window **win, SDL_Renderer **rend) {
int min_w, min_h;
SDL_Init(SDL_INIT_VIDEO);
*win = SDL_CreateWindow(
- WINDOW_TITLE,
+ WIN_TITLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
0,
@@ -59,14 +17,10 @@ setup(SDL_Window **win, SDL_Renderer **rend) {
*rend = SDL_CreateRenderer(*win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- if(TTF_Init()==-1) {
- printf("TTF_Init: %s\n", TTF_GetError());
- exit(2);
- }
-
/* TODO: return error codes */
- if (*win == NULL) end("SDL: window cannot be created");
- else if (*rend == NULL) end("SDL: renderer cannot be created");
+ if (*win == NULL) return ERROR_NULL_POINTER;
+ else if (*rend == NULL) return ERROR_NULL_POINTER;
+ else if(TTF_Init() == -1) return ERROR_SDL_FONT_INIT;
SDL_SetRenderDrawColor(*rend, 32, 32, 32, 0);
SDL_RenderClear(*rend);
@@ -78,12 +32,17 @@ setup(SDL_Window **win, SDL_Renderer **rend) {
SDL_SetWindowMinimumSize(*win, min_w, min_h);
/* SDL_SetWindowMaximumSize(*win, min_w, min_h); */
+ return OK;
}
-void
-cleanup(SDL_Window *win, SDL_Renderer *rend) {
+status_t
+SDL_cleanup(SDL_Window *win, SDL_Renderer *rend) {
+ if(win == NULL || rend == NULL)
+ return ERROR_NULL_POINTER;
+
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
TTF_Quit();
SDL_Quit();
+ return OK;
}
diff --git a/sdl_extra.h b/sdl_extra.h
@@ -1,26 +1,24 @@
#ifndef __SDL_EXTRA_H__
#define __SDL_EXTRA_H__
-#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
-/* #include <SDL2/SDL_video.h> */
-/* #include <SDL2/SDL_image.h> */
-/* typedef struct { */
-/* status_t status; */
-/* int speed; */
-/* size_t sel, cmp, swap; */
-/* void (*sort)(int *arr, size_t len); */
-/* } Sav; */
-
-#include "util.h"
+#include "array.h"
+#include "status.h"
#include "drw.h"
+#include "sav.h"
+
+#define X_BORDER 40
+#define Y_BORDER 40
+#define TOP_BORDER 50
+#define RECT_WIDTH 5
+
+#define WIN_TITLE "SAV - Sorting Algorithms Visualized"
-/* void check_events(status_t *status); */
-void check_events(Drw *drw, SAV *sav);
-void setup(SDL_Window **win, SDL_Renderer **rend);
-void cleanup(SDL_Window *win, SDL_Renderer *rend);
+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
diff --git a/sort.c b/sort.c
@@ -164,8 +164,9 @@ void
quick_sort(SAV *sav, int low, int high) {
int pivot;
- wait_main_thread(&(sav->status));
if(sav->status == STOP) return;
+ else if(sav->status == PAUSE)
+ wait_main_thread(&(sav->status));
if ((high - low) > 0) {
quick_sort_partition(sav, low, &pivot, high);
diff --git a/sort.h b/sort.h
@@ -5,7 +5,6 @@
#include <stdbool.h>
#include "sav.h"
-#include "util.h"
void bubble_sort(SAV *);
void insertion_sort(SAV *);
diff --git a/status.h b/status.h
@@ -0,0 +1,19 @@
+#ifndef __STATUS_H__
+#define __STATUS_H__
+
+typedef enum {
+ OK = 0,
+ RUN,
+ PAUSE,
+ UPDATE,
+ ERROR_MEMORY_ALLOC,
+ ERROR_OPENING_FONT,
+ ERROR_SDL_FONT_INIT,
+ ERROR_NULL_POINTER,
+ ERROR_DRW,
+ SORTED,
+ RESTART,
+ STOP
+} status_t;
+
+#endif
diff --git a/util.h b/util.h
@@ -4,32 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
-#define ARR_LEN 128
-#define ARR_MAX 500
-
-#define X_BORDER 40
-#define Y_BORDER 40
-#define TOP_BORDER 50
-
-#define RECT_WIDTH 5
-
-typedef enum {
- OK = 0,
- RUN,
- PAUSE,
- UPDATE,
- ERROR_MEMORY_ALLOC,
- ERROR_OPENING_FONT,
- ERROR_DRW,
- SORTED,
- RESTART,
- STOP
-} status_t;
-
-typedef struct {
- int *v;
- size_t len;
-} Arr;
+#include "status.h"
void end(const char *msg);
void swap(int *a, int *b);