/
huffsdl.c
405 lines (302 loc) · 8.94 KB
/
huffsdl.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
File: huffsdl.c
Description: functions for huffman tree drawing
usage: huffsdl /path/to/textfile.txt
expects ASCII / utf-8 text file.
Knuth layout algorithm will handle any binary tree
currently "pseudo Reingold Tilford" assumes tree is complete (eg all huffman)
*/
#include "huffsdl.h"
int main(int argc, char *argv[])
{
node *chars, *q=NULL, *root;
int c;
unsigned mode;
double offset;
/* build huffman tree */
chars = getCharCounts(argv[1]);
q = orderInputQ(chars, q);
root = buildTree(q);
/* Display */
if(argc!=2){
fprintf(stderr, "incorrect usage, try eg"
"./huffman textfile.txt");
exit(1);
}
printf("\nenter number for layout type:\n");
printf("0. Knuth, 1. pseudo-Reingold-Tilford\n");
printf("(1 is not 100%% correct as RT but better ");
printf("than Knuth in all cases I tried) :");
while(scanf("%u", &mode)!=1 || mode > 1){
printf("invalid choice, try again\n");
do{/*clear out user input*/
c=getchar();
} while(c!='\n');
}
if(mode == 0){
knuth_getCoords(root, 0);
} else {
rt_draw_1(root, 0);
offset = rt_1_1_move_onscreen(root);
root->offset = offset;
root->x += offset;
rt_2_petrify(root,0);
}
doDisplay(root);
/*clear up other memory */
freeTree(root);
free(chars);
return 0;
}
void initRand(void){
time_t now;
time(&now);
srand((int)now);
}
void doDisplay(node *tree)
{
SDL_Simplewin sw;
fntrow font[FNTCHARS][FNTHEIGHT];
unsigned drawcounter=0;
/*set up screen*/
SDL_myInit(&sw);
Neill_SDL_ReadFont(font, FONTFILE);
do { /* draw and animate tree */
SDL_Delay(SDL_LOOP_DELAY);
drawcounter += SDL_LOOP_DELAY;
Neill_SDL_Events(&sw);
if(drawcounter>= LIGHT_CHANGE_DELAY){
drawcounter=0;
drawTree(&sw, tree, font);
Neill_SDL_DrawString(&sw, font, "Xmas (Binary) Tree",
0, TITLE_POS);
/* update window once */
SDL_RenderPresent(sw.renderer);
SDL_UpdateWindowSurface(sw.win);
}
}
while (!sw.finished);
}
void SDL_myInit(SDL_Simplewin *sw)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
SDL_Quit();
exit(1);
}
sw->finished = 0;
sw->win= SDL_CreateWindow("SDL Window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WIN_W, WIN_H,
/*SDL_WINDOW_INPUT_GRABBED*/
0);
if(sw->win == NULL){
fprintf(stderr, "\nUnable to initialize SDL Window: %s\n",
SDL_GetError());
SDL_Quit();
exit(1);
}
sw->renderer = SDL_CreateRenderer(sw->win, -1, 0);
if(sw->renderer == NULL){
fprintf(stderr, "\nUnable to initialize SDL Renderer: %s\n",
SDL_GetError());
SDL_Quit();
exit(1);
}
/* Set screen bg colour */
SDL_SetRenderDrawColor(sw->renderer, COL_WIN_BG, OPAQUE);
SDL_RenderClear(sw->renderer);
SDL_RenderPresent(sw->renderer);
SDL_SetRenderDrawBlendMode(sw->renderer, SDL_BLENDMODE_BLEND);
}
void drawTree(SDL_Simplewin *sw, node *tree,
fntrow fnt[FNTCHARS][FNTHEIGHT])
{
cart parent, this;
if(tree==NULL){
return;
}
drawTree(sw, tree->c0, fnt);
drawTree(sw, tree->c1, fnt);
if(tree->y > 0){
this = getTreeCoord(tree);
parent = getTreeCoord(tree->parent);
SDL_SetRenderDrawColor(sw->renderer, COL_TREE_GREEN, OPAQUE);
SDL_RenderDrawLine(sw->renderer, parent.x, parent.y,
this.x, this.y);
if(tree->chr){
Neill_SDL_DrawChar(sw, fnt, tree->chr,
this.x-(FNTWIDTH/2), this.y);
} else {
setRandColour(sw);
Neill_SDL_RenderFillCircle(sw->renderer, this.x, this.y, CIRCRAD);
SDL_SetRenderDrawColor(sw->renderer, COL_WHITE, OPAQUE);
/*drawCirc(sw, this, CIRCRAD);*/
}
}
}
cart getTreeCoord (node *this)
{
cart coord;
coord.x = (this->x * FNTHEIGHT) + PAD_W;
coord.y = (this->y * FNTWIDTH) + PAD_H;
return coord;
}
void setRandColour(SDL_Simplewin *sw)
{
unsigned cols[LIGHT_COLS][RGB_MMBRS]
= {{COL_LIGHT_RED}, {COL_LIGHT_BLUE}, {COL_LIGHT_WHITE},
{COL_LIGHT_GREEN}};
int rnd;
rnd = rand() % LIGHT_COLS;
SDL_SetRenderDrawColor(sw->renderer,
cols[rnd][0],cols[rnd][1],cols[rnd][2],
OPAQUE);
}
/* Layout functions */
void knuth_getCoords(node *tree, int y)
{
static int x = 0;
if(tree==NULL){
return;
}
knuth_getCoords(tree->c0, y+LAYER_HEIGHT);
tree->x = x;
x+= KNUTH_SPACE;
tree->y = y;
knuth_getCoords(tree->c1, y+LAYER_HEIGHT);
}
/* (pseudo) Reingold-Tilford tree layout implementation */
contour *rt_draw_1(node *this, int depth)
{
/* TODO assumes full tree for the moment: handle 1 child case */
contour *contL, *contR, *cont;
double space, xTarget;
if(this==NULL){
return NULL;
}
contL = rt_draw_1(this->c0, depth+LAYER_HEIGHT);
contR = rt_draw_1(this->c1, depth+LAYER_HEIGHT);
this->y = depth;
/* get pos relative to children */
xTarget=0;
if(this->c0!=NULL && this->c1!=NULL){
xTarget = this-> c0->x + (this->c1->x - this-> c0->x)/2;
}
/* If first sibling, centre over children */
if(this->bit==0){
this->x = xTarget;
/* if 2nd, position relative to sibling and move children to centre */
} else if(this->parent!=NULL){
this->x = this->parent->c0->x + RT_SPACE;
this->offset = this->x - xTarget;
}
/* get distance to push nodes apart to fit subtrees */
space = rt_1_pushApart(contL, contR);
/* push apart children and recentre*/
if(space>0){ /*there will always be children if space>0*/
this->x += space/2;
this->offset += space/2;
this->c1->x += space;
this->c1->offset += space;
}
cont = rt_1_trackContour(this, contL, contR, space);
if(depth==0){
freeContour(cont);
}
return cont;
}
void freeContour(contour *cont)
{
contour *temp;
while(cont!=NULL){
temp = cont;
cont = cont->next;
free(temp);
}
}
double rt_1_pushApart(contour *l, contour *r)
{
double push = 0;
while(l!=NULL && r!=NULL){
if(l->xmax >= r->xmin + push){
push = (l->xmax - r->xmin) + RT_SPACE;
}
l=l->next;
r=r->next;
}
return push;
}
contour *rt_1_trackContour(node *this, contour *l, contour *r, double space)
{
/* combines "task 2" and "task 3" of RT pass 1
ie - tracks L&R "contours" of subtrees to check for overlap
(for recursive implementation it seemed to me more efficient/clear to
do tasks 2 & 3 together using linked list of min/max per level rather
than making/ following threads)
*/
contour *ret=NULL, *temp;
double xmin, xmax;
/*add this level*/
ret = rt_1_addContour(ret, this->x, this->x);
while(l!=NULL || r!=NULL){
/* continue to the bottom of tree even if depths are unequal*/
r = r==NULL ? l : r;
l = l==NULL ? r : l;
xmin = l->xmin < r->xmin + space ? l->xmin : r->xmin + space;
xmax = l->xmax > r->xmax + space ? l->xmax : r->xmax + space;
/* printf("lmin %f rmin %f min%f\n", l->xmin, r->xmin, xmin);
printf("lmax %f rmax %f max%f\n", l->xmax, r->xmax, xmax);
*/
ret = rt_1_addContour(ret, xmin, xmax);
temp = l;
l = l->next;
free(temp);
temp = r;
r = r->next;
if(r!=l){
free(temp);
}
}
return ret;
}
contour *rt_1_addContour(contour *top, double xmin, double xmax)
{
contour *cont, *this;
myCalloc(cont, 1, sizeof(contour), MEMERR);
cont->xmin = xmin;
cont->xmax = xmax;
/* add to contour list */
if(top==NULL){
return cont;
}
this=top;
while(this->next!=NULL){
this= this->next;
}
this->next = cont;
return top;
}
double rt_1_1_move_onscreen(node *tree)
{
double temp, min;
if(tree==NULL){
return 0;
}
temp = rt_1_1_move_onscreen(tree->c0);
min = tree->x < temp ? tree->x : temp;
temp = rt_1_1_move_onscreen(tree->c1);
return min < temp ? min : temp;
}
/* positions are "set in stone" - kept term from RT paper */
void rt_2_petrify(node *tree, double offset)
{
if(tree==NULL){
return;
}
tree->x += offset;
offset += tree->offset;
rt_2_petrify(tree->c0, offset);
rt_2_petrify(tree->c1, offset);
}