Skip to content

Commit 2bf181b

Browse files
committed
added color
1 parent a46809e commit 2bf181b

File tree

7 files changed

+65
-12
lines changed

7 files changed

+65
-12
lines changed

core/include/utils/App.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#define MINIMUM_WINDOW_WIDTH 400
1313
#define MINIMUM_WINDOW_HEIGHT 400
1414

15+
#define DEFAULT_SORTING_DELAY_MILLISECONDS 100
16+
1517
typedef struct {
1618
int width;
1719
int height;

core/include/utils/Rect.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
#ifndef RECT_H
22
#define RECT_H
33

4+
typedef struct {
5+
uint8_t r;
6+
uint8_t g;
7+
uint8_t b;
8+
} rgb_color;
9+
10+
#define rect_base_color rgb_color { .r = 50, .g = 50, .b = 50 }
11+
12+
// OTHER COLORS TO BETTER DISTINGUISH THE SORTING PROCESS
13+
#define rect_green_color rgb_color { .r = 0, .g = 255, .b = 75 }
14+
#define rect_blue_color rgb_color { .r = 0, .g = 180, .b = 225 }
15+
#define rect_orange_color rgb_color { .r = 255, .g = 150, .b = 0 }
16+
417
typedef struct {
518
int value;
619
int width;
720
int height;
821
int start_x; // TOP-LEFT X COORDINATE
922
int start_y; // TOP-LEFT Y COORDINATE
23+
rgb_color rect_color;
1024
} Rectangle;
1125

1226
#endif

core/include/utils/Sort.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
#include <List.hpp>
77
#include <Rect.hpp>
88

9+
typedef struct {
10+
int value;
11+
rgb_color rect_color;
12+
} SortStep;
13+
914
typedef struct {
1015
int stepCount;
11-
int* steps;
16+
SortStep* steps;
1217
} SortSequence;
1318

1419
#define create_sort_sequence(count) { .stepCount = count };

core/src/sorts/BubbleSort.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ namespace BubbleSort {
3131
stepCount++;
3232
}
3333
}
34-
if (!swapped) break;
34+
if (!swapped) {
35+
// INCREMENT FOR FINAL STEP
36+
stepCount++;
37+
break;
38+
}
3539
}
3640

3741
free(array);
@@ -43,7 +47,7 @@ namespace BubbleSort {
4347
int stepCount = GetStepCount_BubbleSort(items);
4448

4549
SortSequence sort = create_sort_sequence(stepCount);
46-
sort.steps = (int*) malloc(LIST_SIZE * stepCount * sizeof(int));
50+
sort.steps = (SortStep*) malloc(LIST_SIZE * stepCount * sizeof(SortStep));
4751

4852
Rectangle* array = (Rectangle*) malloc(LIST_SIZE * sizeof(Rectangle));
4953
memcpy(array, items, LIST_SIZE * sizeof(Rectangle));
@@ -63,15 +67,33 @@ namespace BubbleSort {
6367

6468
// RECORD THE SORTING STEP (SNAPSHOT OF THE ARRAY)
6569
for (int i = 0; i < LIST_SIZE; i++) {
70+
bool isOrdered = i >= (LIST_SIZE - index);
71+
bool isCurrent = i == j;
72+
bool isChecking = i == (j + 1);
73+
6674
offset = (currentStep * LIST_SIZE) + i;
67-
sort.steps[offset] = array[i].value;
75+
sort.steps[offset].value = array[i].value;
76+
77+
// SET RECTANGLE COLOR
78+
sort.steps[offset].rect_color =
79+
isOrdered ? rect_green_color :
80+
isCurrent ? rect_blue_color :
81+
isChecking ? rect_orange_color : rect_base_color;
6882
}
6983
currentStep++;
7084
}
7185
}
7286

7387
// BREAK WHEN NO ELEMENTS SWAPPED (OPTIMIZED)
74-
if (!swapped) break;
88+
if (!swapped) {
89+
// RECORD FINAL STEP
90+
for (int i = 0; i < LIST_SIZE; i++) {
91+
offset = (currentStep * LIST_SIZE) + i;
92+
sort.steps[offset].value = array[i].value;
93+
sort.steps[offset].rect_color = rect_green_color;
94+
}
95+
break;
96+
}
7597
}
7698

7799
free(array);
@@ -86,7 +108,8 @@ namespace BubbleSort {
86108
int offset;
87109
for (int i = 0; i < LIST_SIZE; i++) {
88110
offset = (index * LIST_SIZE) + i;
89-
items[i].value = sequence.steps[offset];
111+
items[i].value = sequence.steps[offset].value;
112+
items[i].rect_color = sequence.steps[offset].rect_color;
90113
}
91114
return false;
92115
}

core/src/utils/List.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void CreateList(SDL_Renderer *renderer, Rectangle* items, int window_width, int
2020
items[i].height = (i + 1) * (drawable_height / LIST_SIZE);
2121
items[i].start_x = LIST_MARGIN_MINIMUM + (i * (rect_width + LIST_RECT_PADDING)) + width_offset;
2222
items[i].start_y = LIST_MARGIN_MINIMUM + (drawable_height - rect_height) + height_offset;
23+
items[i].rect_color = rect_base_color;
2324
}
2425
}
2526

@@ -53,6 +54,11 @@ void ShuffleList(Rectangle* items) {
5354
items[i].value = items[j].value;
5455
items[j].value = temp;
5556
}
57+
58+
// RESET THE RECTANGLE COLOR
59+
for (int i = 0; i < LIST_SIZE; i++) {
60+
items[i].rect_color = rect_base_color;
61+
}
5662
}
5763

5864
void DrawList(SDL_Renderer *renderer, Rectangle* items) {
@@ -73,7 +79,7 @@ static void DrawRect(SDL_Renderer *renderer, Rectangle* items, int index) {
7379
.h = (float) items[index].height,
7480
};
7581

76-
// DRAW Rectangle
77-
SDL_SetRenderDrawColor(renderer, 50, 50, 50, SDL_ALPHA_OPAQUE_FLOAT);
82+
// DRAW RECTANGLE
83+
SDL_SetRenderDrawColor(renderer, items[index].rect_color.r, items[index].rect_color.g, items[index].rect_color.b, SDL_ALPHA_OPAQUE_FLOAT);
7884
SDL_RenderFillRect(renderer, &rect_item);
7985
}

src/main.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
3636
appContext->height = WINDOW_HEIGHT;
3737
appContext->isSorting = false;
3838
appContext->lastTime = 0;
39-
appContext->delayInMilliseconds = 50;
39+
appContext->delayInMilliseconds = DEFAULT_SORTING_DELAY_MILLISECONDS;
4040
*appstate = appContext;
4141

4242
if (!SDL_CreateWindowAndRenderer("Sorting Visualizer", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &appContext->window, &appContext->renderer)) {
@@ -95,6 +95,9 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
9595
ImGui::Begin("Control Panel", NULL, ImGuiWindowFlags_NoCollapse);
9696

9797
if (ImGui::Button("Shuffle", ImVec2(120, 20))) {
98+
appContext->isSorting = false;
99+
appContext->lastTime = 0;
100+
appContext->stepIndex = 0;
98101
ShuffleList(appContext->items);
99102
ResizeList(appContext->renderer, appContext->items, appContext->width, appContext->height);
100103
}
@@ -127,8 +130,8 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
127130
// DELAY ITERATING THE SORTING STEP
128131
static SDL_Time current = 0;
129132
SDL_GetCurrentTime(&current);
130-
int currentTime = (int) SDL_NS_TO_MS(current);
131-
int elapsedTime = currentTime - appContext->lastTime;
133+
int currentTime = abs((int) SDL_NS_TO_MS(current));
134+
int elapsedTime = abs(currentTime - appContext->lastTime);
132135
if(elapsedTime > appContext->delayInMilliseconds) {
133136
if(IncrementStep(appContext->sortId, appContext->stepIndex, appContext->sequence, appContext->items)) {
134137
appContext->isSorting = false;

test/sorts/BubbleSort.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ TEST(BubbleSort_Test, GetSequence_CorrectOrder) {
5656
int offset;
5757
for (int i = 0; i < LIST_SIZE; i++) {
5858
offset = (lastStep * LIST_SIZE) + i;
59-
EXPECT_EQ(sequence.steps[offset], (i + 1));
59+
EXPECT_EQ(sequence.steps[offset].value, (i + 1));
6060
}
6161
}

0 commit comments

Comments
 (0)