-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort_array.c
201 lines (148 loc) · 3.97 KB
/
sort_array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <SDL2/SDL.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "sort_array.h"
struct sort_array {
//model
long size;
int *data;
//view
SDL_Window *window;
SDL_Renderer *renderer;
};
// view functions
static int sort_array_init_view(sort_array *arr);
static void sort_array_free_view(sort_array *arr);
static void render_sort_array(sort_array *arr, unsigned int delay);
void sort_array_print(sort_array *arr)
{
for(int i=0; i < arr->size; i++)
printf("%d, ",arr->data[i]);
printf("\n");
}
/* create a sort_array corresponding to a permutation of size s*/
sort_array* sort_array_new (long s, bool is_identity)
{
assert(s > 0);
sort_array* arr = malloc(sizeof(sort_array));
assert(arr);
// init model FIXME: put it in a function
arr->size = s;
arr->data = malloc(s*sizeof(int));
assert(arr->data);
for(int i=0; i < s; i++)
arr->data[i]=i+1;
if( !is_identity )
shuffle(arr);
// init view
int exit = sort_array_init_view(arr);
assert(!exit);
return arr;
}
/* free a sort_array created by sort_array_new */
void sort_array_free (sort_array *arr)
{
assert(arr);
free(arr->data);
sort_array_free_view(arr);
free(arr);
}
void shuffle (sort_array *arr)
{
for(int i=0; i < arr->size; i++)
{
int j = rand()%(arr->size - i) + i;
if( i != j)
sort_array_swap(arr, i, j, 0);
}
}
int sort_array_size (sort_array *arr)
{
return arr->size;
}
/* return the value of element at index i in arr */
int sort_array_get (sort_array *arr, long i)
{
assert(0 <= i && i < arr->size);
return arr->data[i];
}
/* set the value of the element at index i in arr to v */
void sort_array_set (sort_array *arr, long i, int v, unsigned int delay)
{
assert(0 <= i && i < arr->size);
arr->data[i]=v;
render_sort_array(arr, delay);
}
void sort_array_swap(sort_array *arr, long i, long j, unsigned int delay)
{
assert(0 <= i && i < arr->size);
assert(0 <= j && j < arr->size);
int tmp = arr->data[i];
arr->data[i] = arr->data[j];
arr->data[j] = tmp;
render_sort_array(arr, delay);
}
/***************************/
/* array display functions */
/***************************/
static int sort_array_init_view(sort_array *arr)
{
if(0 != SDL_Init(SDL_INIT_VIDEO))
{
fprintf(stderr, "Erreur SDL_Init : %s", SDL_GetError());
goto Quit;
}
arr->window = SDL_CreateWindow("Sorting Algorithm Visualizer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(NULL == arr->window)
{
fprintf(stderr, "Erreur SDL_CreateWindow : %s", SDL_GetError());
goto Quit;
}
arr->renderer = SDL_CreateRenderer(arr->window, -1, SDL_RENDERER_ACCELERATED);
if(NULL == arr->renderer)
{
fprintf(stderr, "Erreur SDL_CreateRenderer : %s", SDL_GetError());
goto Quit;
}
return 0;
Quit:
if(NULL != arr->renderer)
SDL_DestroyRenderer(arr->renderer);
if(NULL != arr->window)
SDL_DestroyWindow(arr->window);
SDL_Quit();
return 1;
}
static void sort_array_free_view(sort_array *arr)
{
SDL_DestroyRenderer(arr->renderer);
SDL_DestroyWindow(arr->window);
SDL_Quit();
}
static void clear_screen(sort_array *arr)
{
SDL_Color bg = {63, 63, 63, SDL_ALPHA_OPAQUE };
SDL_SetRenderDrawColor( arr->renderer, bg.r, bg.g, bg.b, bg.a );
SDL_RenderClear( arr->renderer );
}
static void render_sort_array(sort_array *arr, unsigned int delay)
{
clear_screen(arr);
SDL_Color fg = {220, 220, 204, SDL_ALPHA_OPAQUE };
SDL_SetRenderDrawColor( arr->renderer, fg.r, fg.g, fg.b, fg.a );
const int width = SCREEN_WIDTH/arr->size;
int height;
for(int i=0; i < arr->size; i++) {
height = (SCREEN_HEIGHT*arr->data[i])/arr->size;
SDL_Rect rect = {.x = i*width,
.w = width,
.h = height,
.y = SCREEN_HEIGHT-height };
SDL_RenderFillRect(arr->renderer, &rect);
}
SDL_RenderPresent(arr->renderer);
SDL_Delay(delay);
}