Skip to content

Commit d1aabd3

Browse files
committed
overhaul structure
1 parent 3cbcc43 commit d1aabd3

57 files changed

Lines changed: 805 additions & 2154 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ jobs:
5151

5252
- name: RUN TESTS
5353
if: runner.os == 'Linux'
54-
run: ctest --preset default -C release
54+
run: ctest --preset default --build-config release

.vscode/launch.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
"configurations": [
44
{
55
"name": "Launch Debug (Windows)",
6-
"type": "cppvsdbg",
6+
"type": "cppdbg",
77
"request": "launch",
88
"program": "${workspaceFolder}\\build\\default\\Debug\\main.exe",
99
"cwd": "${workspaceFolder}\\build\\default\\Debug",
1010
"args": [],
11-
"stopAtEntry": false,
1211
"environment": [],
13-
"console": "newExternalWindow",
12+
"MIMode": "gdb",
13+
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
14+
"setupCommands": [
15+
{
16+
"description": "Enable pretty-printing for gdb",
17+
"text": "-enable-pretty-printing",
18+
"ignoreFailures": true
19+
}
20+
],
1421
"preLaunchTask": "Build Debug"
1522
},
1623
{
@@ -20,9 +27,8 @@
2027
"program": "${workspaceFolder}\\build\\default\\Release\\main.exe",
2128
"cwd": "${workspaceFolder}\\build\\default\\Release",
2229
"args": [],
23-
"stopAtEntry": false,
2430
"environment": [],
25-
"console": "newExternalWindow",
31+
"console": "integratedTerminal",
2632
"preLaunchTask": "Build Release"
2733
}
2834
]

README.md

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,7 @@
55
<dl>
66
<dd>
77
This is a C++ application that implements various <a href="https://en.wikipedia.org/wiki/Sorting_algorithm">sorting algorithms</a>
8-
using colors and shapes to represent how the underlying data structure is changing.
9-
</dd>
10-
</dl>
11-
12-
<dl>
13-
<dd>
14-
<details closed>
15-
<summary><b>Time Complexity</b></summary>
16-
17-
| Name | Best | Average | Worst |
18-
|:-----------|:-------------------|:-------------------|:-------------------|
19-
| Bubble | O(n) | O(n<sup>2</sup>) | O(n<sup>2</sup>) |
20-
| Cocktail | O(n) | O(n<sup>2</sup>) | O(n<sup>2</sup>) |
21-
| Heap | O(n log(n)) | O(n log(n)) | O(n log(n)) |
22-
| Insertion | O(n) | O(n<sup>2</sup>) | O(n<sup>2</sup>) |
23-
| Merge | O(n log(n)) | O(n log(n)) | O(n log(n)) |
24-
| Quick | O(n log(n)) | O(n log(n)) | O(n<sup>2</sup>) |
25-
| Radix | O(nk) | O(nk) | O(nk) |
26-
| Selection | O(n<sup>2</sup>) | O(n<sup>2</sup>) | O(n<sup>2</sup>) |
27-
28-
</details>
8+
using colors and shapes to represent changes in the data structure.
299
</dd>
3010
</dl>
3111

@@ -34,8 +14,6 @@
3414
<details open>
3515
<summary>Show</summary>
3616
<img width="598px" height="600px" src="https://github.com/user-attachments/assets/53180fca-318b-4c76-9f2f-363413b2bc00" alt="image">
37-
38-
3917
<br>
4018
</details>
4119

@@ -57,10 +35,11 @@ cmake --preset default
5735

5836
# BUILD DEBUG (RUN TESTS)
5937
cmake --build --preset debug
60-
ctest --preset default -C debug
38+
ctest --preset default --build-config debug
6139

6240
# BUILD RELEASE
6341
cmake --build --preset release
42+
ctest --preset default --build-config release
6443

6544
```
6645

core/include/app/App.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
#ifndef APP_H
22
#define APP_H
33

4+
#include <memory>
5+
#include <vector>
6+
#include <string>
7+
#include <ctime>
8+
#include <cstdlib>
9+
410
#include <SDL3/SDL.h>
11+
#include <imgui.h>
12+
#include <imgui_impl_sdl3.h>
13+
#include <imgui_impl_sdlrenderer3.h>
514

615
#include <AppContext.hpp>
16+
#include <ListManager.hpp>
17+
#include <Renderer.hpp>
18+
#include <SortManager.hpp>
719
#include <UserInterface.hpp>
820

921
namespace Application {
@@ -37,11 +49,8 @@ namespace Application {
3749
bool CreateWindowAndRenderer(const char* title);
3850
void RenderScene(float deltaTime);
3951

40-
int width = WINDOW_WIDTH;
41-
int height = WINDOW_HEIGHT;
42-
4352
SDL_Window* window = nullptr;
44-
SDL_Renderer* renderer = nullptr;
53+
SDL_Renderer* sdl_renderer = nullptr;
4554

4655
float deltaTime = 0.0f;
4756
float lastFrameTime = 0.0f;

core/include/app/AppContext.hpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
#pragma once
1+
#ifndef APP_CONTEXT_H
2+
#define APP_CONTEXT_H
23

4+
#include <atomic>
35
#include <memory>
6+
#include <vector>
47

5-
#include <Rect.hpp>
6-
#include <Sort.hpp>
8+
#include <ListManager.hpp>
9+
#include <Renderer.hpp>
10+
#include <SortManager.hpp>
711

812
#define WINDOW_WIDTH 800
913
#define WINDOW_HEIGHT 800
14+
#define WINDOW_FLAGS SDL_WINDOW_RESIZABLE
15+
1016
#define MINIMUM_WINDOW_WIDTH 400
1117
#define MINIMUM_WINDOW_HEIGHT 400
1218

13-
#define DEFAULT_SORTING_DELAY_SECONDS 0.1f
14-
1519
namespace Application {
1620

1721
struct AppContext {
18-
std::unique_ptr<Rectangle[]> items;
22+
std::unique_ptr<Renderer> renderer;
23+
std::unique_ptr<SortManager> sortManager;
24+
std::unique_ptr<ListManager> listManager;
1925

20-
float elapsedTime; // ELAPSED TIME SINCE LAST ITERATION STEP (SECONDS)
21-
float delayTime; // ITERATION DELAY TIME (SECONDS)
26+
std::atomic<bool> isSortingFlag { false };
2227

23-
bool isSorting = false;
24-
int sortId = 0;
25-
int stepIndex = 0;
26-
std::unique_ptr<SortSequence> sequence;
28+
float elapsedTime; // ELAPSED TIME SINCE LAST ITERATION STEP (SECONDS)
29+
float delayTimeNormalized; // NORMALIZED ITERATION DELAY TIME (SECONDS)
2730

2831
AppContext() {
2932
elapsedTime = 0.0f;
30-
delayTime = DEFAULT_SORTING_DELAY_SECONDS;
33+
delayTimeNormalized = 0.75f;
3134
}
3235
};
3336

3437
}
38+
39+
#endif

core/include/app/UserInterface.hpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
#pragma once
1+
#ifndef USER_INTERFACE_H
2+
#define USER_INTERFACE_H
3+
4+
#include <atomic>
5+
#include <vector>
26

37
#include <imgui.h>
48
#include <imgui_internal.h>
@@ -8,6 +12,44 @@
812

913
namespace UserInterface {
1014

11-
void RenderGUI(Application::AppContext* appContext, int width, int height);
15+
inline void SetCustomTheme() {
16+
ImGuiStyle& style = ImGui::GetStyle();
17+
ImVec4* colors = style.Colors;
18+
ImGui::StyleColorsDark();
19+
20+
// ROUNDING
21+
style.WindowRounding = 4.0f;
22+
style.FrameRounding = 4.0f;
23+
style.ChildRounding = 6.0f;
24+
style.PopupRounding = 4.0f;
25+
style.GrabRounding = 4.0f;
26+
style.ScrollbarRounding = 6.0f;
27+
28+
// WINDOW
29+
style.WindowBorderSize = 0.0f;
30+
style.WindowTitleAlign = ImVec2(0.5f, 0.5f); // WINDOW TITLE CENTERED
31+
32+
style.ItemSpacing = ImVec2(8.0f, 6.0f);
33+
style.ItemInnerSpacing = ImVec2(6.0f, 4.0f);
34+
style.ScrollbarSize = 18.0f;
35+
style.FramePadding = ImVec2(10.0f, 6.0f);
36+
style.FrameBorderSize = 0.0f;
37+
style.GrabMinSize = 10.0f;
38+
39+
// TITLE (PANEL)
40+
colors[ImGuiCol_TitleBg] = ImVec4(0.16f, 0.29f, 0.48f, 1.00f);
41+
colors[ImGuiCol_TitleBgActive] = ImVec4(0.16f, 0.29f, 0.48f, 1.00f);
42+
colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.16f, 0.29f, 0.48f, 1.00f);
43+
}
44+
45+
void CreateShuffleButton(Application::AppContext* appContext);
46+
47+
void CreateSortButton(Application::AppContext* appContext);
48+
49+
void CreateSortSelectionDropdown(Application::AppContext* appContext);
50+
51+
void RenderGUI(Application::AppContext* appContext);
52+
53+
}
1254

13-
}
55+
#endif
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef LIST_MANAGER_H
2+
#define LIST_MANAGER_H
3+
4+
#include <algorithm>
5+
#include <vector>
6+
7+
#include <SDL3/SDL.h>
8+
9+
#include <Rect.hpp>
10+
11+
class ListManager {
12+
public:
13+
ListManager(int windowWidth, int windowHeight, int listCount = 32) {
14+
this->windowWidth = windowWidth;
15+
this->windowHeight = windowHeight;
16+
this->listCount = listCount;
17+
CreateList();
18+
}
19+
20+
void CreateList();
21+
22+
void Resize(int width, int height);
23+
24+
void ResizeRectangles();
25+
26+
void Shuffle();
27+
28+
std::vector<Rect::Rectangle>& GetItems() { return rects; }
29+
30+
private:
31+
int listCount;
32+
int windowWidth;
33+
int windowHeight;
34+
35+
int margin = 24;
36+
int rectPadding = 2;
37+
38+
std::vector<Rect::Rectangle> rects;
39+
};
40+
41+
#endif

core/include/renderer/Renderer.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef RENDERER_H
2+
#define RENDERER_H
3+
4+
#include <vector>
5+
6+
#include <SDL3/SDL.h>
7+
8+
#include <Color.hpp>
9+
#include <ListManager.hpp>
10+
#include <Rect.hpp>
11+
12+
class Renderer {
13+
public:
14+
Renderer(SDL_Renderer* renderer) {
15+
this->renderer = renderer;
16+
};
17+
18+
void DrawList(const std::vector<Rect::Rectangle>& items, ListManager& listManager);
19+
20+
void DrawRect(const Rect::Rectangle& rect);
21+
22+
private:
23+
SDL_Renderer* renderer;
24+
};
25+
26+
#endif

core/include/sorting/Sort.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef SORT_H
2+
#define SORT_H
3+
4+
#include <memory>
5+
#include <vector>
6+
7+
#include <Rect.hpp>
8+
9+
// FORWARD DECLARATIONS
10+
namespace Rect {
11+
enum class State;
12+
struct Rectangle;
13+
}
14+
15+
struct SortStep {
16+
std::unique_ptr<std::vector<Rect::Rectangle>> rects;
17+
};
18+
19+
struct SortSequence {
20+
int stepCount = 0;
21+
std::unique_ptr<std::vector<SortStep>> steps;
22+
};
23+
24+
namespace Sort {
25+
26+
class Sort {
27+
public:
28+
virtual ~Sort() = default;
29+
30+
virtual const char* GetName() const = 0;
31+
32+
virtual void SetSequence(SortSequence* sequence, std::vector<Rect::Rectangle> rects) = 0;
33+
34+
protected:
35+
static void PushStep(SortSequence& sequence, std::vector<Rect::Rectangle> rects);
36+
37+
static void FirstStep(SortSequence& sequence, const std::vector<Rect::Rectangle>& rects);
38+
39+
static void LastStep(SortSequence& sequence, std::vector<Rect::Rectangle>& rects);
40+
};
41+
42+
}
43+
44+
#endif

0 commit comments

Comments
 (0)