sav

Sorting Algorithms Visualized
Index Commits Files Refs README LICENSE
commit 4fbfd72b67cf2806bdb9d72e629c027a47055364
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Thu, 24 Mar 2022 20:21:08 -0300

First commit

Diffstat:
AMakefile | 17+++++++++++++++++
Adrw.c | 1+
Adrw.h | 5+++++
Asav.c | 145+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Autil.c | 14++++++++++++++
Autil.h | 9+++++++++
6 files changed, 191 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,17 @@
+CC := gcc
+CLIBS :=
+CFLAGS := `sdl2-config --libs --cflags` -lSDL2_image -lm
+SRCS := "sav.c util.c"
+OBJS := $(SRCS:.c=.o)
+
+TARGET := sav
+
+.PHONY: all clean
+
+all: $(TARGET)
+
+$(TARGET): $(OBJS)
+    $(CC) $(CFLAGS) -o $@ $^
+
+clean:
+    rm -f $(OBJS)
diff --git a/drw.c b/drw.c
@@ -0,0 +1 @@
+#include "drw.h"
diff --git a/drw.h b/drw.h
@@ -0,0 +1,5 @@
+#ifndef __DRAW_H__
+#define __DRAW_H__
+
+
+#endif // __DRAW_H__
diff --git a/sav.c b/sav.c
@@ -0,0 +1,145 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <time.h>
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_video.h>
+#include <SDL2/SDL_image.h>
+
+#include "drw.h"
+#include "util.h"
+
+#define WINDOW_TITLE "Sorting Algorithms Visualized"
+#define WINDOW_WIDTH     0
+#define WINDOW_HEIGHT     0
+
+#define X_BORDER    50
+#define Y_BORDER    50
+#define RECT_WIDTH    5
+
+#define INIT_SIZE 50
+
+typedef struct {
+    SDL_Window *window;
+    SDL_Renderer *rend;
+    int w, h;
+    size_t x_border, y_border;
+} Root;
+
+Root root;
+static size_t x = 0;    
+
+void setup(void) {
+    SDL_Init(SDL_INIT_VIDEO);
+
+    // Create an application window with the following settings:
+    root.window = SDL_CreateWindow(
+        WINDOW_TITLE,                    // window title
+        SDL_WINDOWPOS_UNDEFINED,        // initial x position
+        SDL_WINDOWPOS_UNDEFINED,        // initial y position
+        WINDOW_WIDTH,                    // width, in pixels
+        WINDOW_HEIGHT,                    // height, in pixels
+        SDL_WINDOW_OPENGL | 
+        SDL_WINDOW_RESIZABLE            // window flags
+    );
+
+    root.x_border = 5;
+    root.y_border = 5;
+
+    root.rend = SDL_CreateRenderer(root.window, -1, SDL_RENDERER_ACCELERATED);
+
+    if (!root.window) end("SDL: window cannot be created");
+
+    SDL_SetRenderDrawColor(root.rend, 32, 32, 32, 0);
+    SDL_RenderClear(root.rend);
+    SDL_RenderPresent(root.rend);
+    SDL_RenderClear(root.rend);
+}
+
+void cleanup(void) {
+    SDL_DestroyRenderer(root.rend);
+    SDL_DestroyWindow(root.window);
+    SDL_Quit();
+}
+
+void drw_element(size_t height) {
+    SDL_Rect r;
+
+    r.x = X_BORDER + x; // bottom left + x
+    r.y = root.h - Y_BORDER + 1; // bottom
+    r.w = RECT_WIDTH; // fixed width
+    r.h = -height;
+
+    SDL_RenderDrawRect(root.rend, &r);
+    SDL_SetRenderDrawColor(root.rend, 255, 0, 0, 0);
+    SDL_RenderFillRect(root.rend, &r);
+
+    SDL_SetRenderDrawColor(root.rend, 0, 0, 0, 0);
+    SDL_RenderDrawLine(root.rend,
+            x + X_BORDER,
+            root.h - Y_BORDER,
+            x + X_BORDER,
+            root.h - Y_BORDER - height);
+
+    x += RECT_WIDTH;
+}
+
+void bubble_sort(int *arr, size_t len)
+{
+    if(arr == NULL) return;
+
+    size_t swaps, top;
+    top = len;
+    for(size_t i = 0; i < len; ++i) {
+        for(size_t j = 0; j < (top - 1); j++) {
+            if(arr[j] > arr[j + 1]) {
+                swap(&arr[j], &arr[j + 1]);
+                swaps++;
+            } else if(arr[top - 1] == (arr[top] - 1)) {
+                len--;
+            }
+        }
+        if(swaps == 0) break;
+        top--;
+    }
+}
+
+int main (void) {
+    setup();
+
+    size_t len = 100;
+    int arr[len];
+    size_t arr_max = 400;
+
+    srand((unsigned int)time(NULL));
+    for(size_t i = 0; i < len; i++)
+        while(!(arr[i] = rand() % arr_max))
+            arr[i] = (rand() % arr_max);
+
+    bool run = true;
+    while(run) {
+        SDL_Event event;
+
+        while (SDL_PollEvent(&event)) {
+            if(event.window.event == SDL_WINDOWEVENT_CLOSE) {
+                event.type = SDL_QUIT;
+                SDL_PushEvent(&event);
+                run = false;
+                break;
+            }
+        }
+        SDL_SetRenderDrawColor(root.rend, 28, 28, 28, 0);
+        SDL_RenderPresent(root.rend);
+        SDL_RenderClear(root.rend);
+        SDL_GetWindowSize(root.window, &root.w, &root.h);
+
+        for(size_t i = 0; i < len; i++)
+            drw_element(arr[i]);
+
+        SDL_RenderPresent(root.rend);
+        x = 0;
+    }
+    
+    cleanup();
+    return 0;
+}
diff --git a/util.c b/util.c
@@ -0,0 +1,14 @@
+#include "util.h"
+
+void end(const char *msg) {
+    fprintf("%s\n", msg);
+    exit(1);
+}
+
+void swap(int *a, int *b)
+{
+    int tmp;
+    tmp = (*a);
+    (*a) = (*b);
+    (*b) = tmp;
+}
diff --git a/util.h b/util.h
@@ -0,0 +1,9 @@
+#ifndef __UTIL_H__
+#define  __UTIL_H__
+
+#include <stdio.h>
+
+void end(const char *msg);
+void swap(int *a, int *b);
+
+#endif // __UTIL_H__