forked from smcameron/space-nerds-in-space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snis_graph.c
989 lines (823 loc) · 23.2 KB
/
snis_graph.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <string.h>
#include <gtk/gtk.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#ifndef WITHOUTOPENGL
#include <gtk/gtkgl.h>
#include <GL/glew.h>
#include <png.h>
#endif
#include "snis_font.h"
#define SNIS_GRAPH_DECLARE_GLOBALS
#include "snis_graph.h"
#undef SNIS_GRAPH_DECLARE_GLOBALS
#include "liang-barsky.h"
#include "bline.h"
#include "build_bug_on.h"
#include "mtwist.h"
#include "mathutils.h"
#include "graph_dev.h"
#include "ui_colors.h"
#include "string-utils.h"
#define TOTAL_COLORS (NCOLORS + NSPARKCOLORS + NRAINBOWCOLORS + NSHADESOFGRAY * (NSHADECOLORS + 1) + \
(NGRADIENTS * NTOTAL_GRADIENT_SHADES) + MAX_USER_COLORS)
GdkColor huex[TOTAL_COLORS];
/* first index of user colors: Note this will break if user colors
* aren't at the end of the huex array
*/
#define USER_COLOR ((sizeof(huex) / sizeof(huex[0])) - MAX_USER_COLORS)
int nuser_colors;
extern struct my_vect_obj **gamefont[];
extern int font_scale[];
extern int letter_spacing[];
extern int font_lineheight[];
struct snis_graph_viewport {
int x_offset, y_offset, width, height;
};
static struct snis_graph_context {
float xscale, yscale;
struct liang_barsky_clip_window c;
int extent_width, extent_height; /* size of drawing area used in snis_draw_* functions */
int screen_width, screen_height; /* size of screen in pixels */
struct snis_graph_viewport vp_3d; /* in extent coords */
int hue; /* current color, index into huex[] and glhue[] */
int has_scale;
int has_viewport;
} sgc;
/* size of the extent we draw to with the drawing commands */
void sng_set_extent_size(int width, int height)
{
sgc.extent_width = width;
sgc.extent_height = height;
}
/* pixel size of the target screen */
void sng_set_screen_size(int width, int height)
{
sgc.screen_width = width;
sgc.screen_height = height;
graph_dev_set_screen_size(width, height);
sgc.xscale = (float)sgc.screen_width / (float)sgc.extent_width;
sgc.yscale = (float)sgc.screen_height / (float)sgc.extent_height;
/* sgc.has_scale = (sgc.screen_width != sgc.extent_width || sgc.screen_height != sgc.extent_height); */
/* hack in fixed aspect ratio for now */
if (sgc.yscale < sgc.xscale)
sgc.xscale = sgc.yscale;
else
sgc.yscale = sgc.xscale;
sgc.has_scale = 1;
graph_dev_set_extent_scale(sgc.xscale, sgc.yscale);
/* update the viewport in graph_dev as they are in screen coords */
graph_dev_set_3d_viewport(sgc.vp_3d.x_offset * sgc.xscale, sgc.vp_3d.y_offset * sgc.yscale,
sgc.vp_3d.width * sgc.xscale, sgc.vp_3d.height * sgc.yscale);
}
void sng_set_3d_viewport(int x_offset, int y_offset, int width, int height)
{
sgc.vp_3d.x_offset = x_offset;
sgc.vp_3d.y_offset = y_offset;
sgc.vp_3d.width = width;
sgc.vp_3d.height = height;
graph_dev_set_3d_viewport(x_offset * sgc.xscale, y_offset * sgc.yscale,
width * sgc.xscale, height * sgc.yscale);
}
void sng_set_scale(float xscale, float yscale)
{
sgc.xscale = xscale;
sgc.yscale = yscale;
}
void sng_set_clip_window(int x1, int y1, int x2, int y2)
{
sgc.c.x1 = x1;
sgc.c.y1 = y1;
sgc.c.x2 = x2;
sgc.c.y2 = y2;
}
void sng_current_draw_line(float x1, float y1, float x2, float y2)
{
if (!clip_line(&sgc.c, &x1, &y1, &x2, &y2))
return;
graph_dev_draw_line(x1 * sgc.xscale, y1 * sgc.yscale, x2 * sgc.xscale, y2 * sgc.yscale);
}
void sng_current_draw_thick_line(float x1, float y1, float x2, float y2)
{
float sx1, sy1, sx2, sy2, dx, dy;
if (!clip_line(&sgc.c, &x1, &y1, &x2, &y2))
return;
if (fabs(x1 - x2) > fabs(y1 - y2)) {
dx = 0;
dy = 1;
} else {
dx = 1;
dy = 0;
}
sx1 = x1 * sgc.xscale;
sx2 = x2 * sgc.xscale;
sy1 = y1 * sgc.yscale;
sy2 = y2 * sgc.yscale;
graph_dev_draw_line(sx1, sy1, sx2, sy2);
graph_dev_draw_line(sx1 - dx, sy1 - dy, sx2 - dx, sy2 - dy);
graph_dev_draw_line(sx1 + dx, sy1 + dy, sx2 + dx, sy2 + dy);
}
static int clip_rectangle(float *x, float *y, float *width, float *height)
{
float x2, y2;
x2 = *x + *width;
y2 = *y + *height;
if (x2 < sgc.c.x1)
return 0;
if (*x > sgc.c.x2)
return 0;
if (y2 < sgc.c.y1)
return 0;
if (*y > sgc.c.y2)
return 0;
if (*x < sgc.c.x1)
*x = sgc.c.x1;
if (x2 > sgc.c.x2)
x2 = sgc.c.x2;
if (*y < sgc.c.y1)
*y = sgc.c.y1;
if (y2 > sgc.c.y2)
y2 = sgc.c.y2;
*width = x2 - *x;
*height = y2 - *y;
return 1;
}
void sng_current_draw_rectangle(int filled, float x, float y, float width, float height)
{
if (!clip_rectangle(&x, &y, &width, &height))
return;
graph_dev_draw_rectangle(filled, x * sgc.xscale, y * sgc.yscale,
width * sgc.xscale, height * sgc.yscale);
}
void sng_current_draw_bright_line(float x1, float y1, float x2, float y2, int color)
{
float sx1, sy1, sx2, sy2, dx, dy;
if (fabs(x1 - x2) > fabs(y1 - y2)) {
dx = 0;
dy = 1;
} else {
dx = 1;
dy = 0;
}
if (!clip_line(&sgc.c, &x1, &y1, &x2, &y2))
return;
sx1 = x1 * sgc.xscale;
sx2 = x2 * sgc.xscale;
sy1 = y1 * sgc.yscale;
sy2 = y2 * sgc.yscale;
sng_set_foreground(WHITE);
graph_dev_draw_line(sx1, sy1, sx2, sy2);
sng_set_foreground(color);
graph_dev_draw_line(sx1 - dx, sy1 - dy, sx2 - dx, sy2 - dy);
graph_dev_draw_line(sx1 + dx, sy1 + dy, sx2 + dx, sy2 + dy);
}
void sng_current_draw_arc(int filled, float x, float y, float width, float height, float angle1, float angle2)
{
graph_dev_draw_arc(filled, x * sgc.xscale, y * sgc.yscale, width * sgc.xscale,
height * sgc.yscale, angle1, angle2);
}
void sng_dotted_line_plot_func(int x, int y, void *context)
{
struct sng_dotted_plot_func_context *c = context;
c->i = (c->i + 1) % 10;
if (c->i != 0)
return;
graph_dev_draw_point(x, y);
}
void sng_electric_line_plot_func(int x, int y, void *context)
{
if (snis_randn(100) < 10)
graph_dev_draw_point(x, y);
}
static void sng_bright_electric_line_plot_func(int x, int y, void *context)
{
struct sng_dotted_plot_func_context *c = context;
if (snis_randn(100) < 20) {
sng_set_foreground(c->i);
graph_dev_draw_point(x, y);
}
}
void sng_draw_dotted_line(float x1, float y1, float x2, float y2)
{
struct sng_dotted_plot_func_context context;
context.i = 0;
if (!clip_line(&sgc.c, &x1, &y1, &x2, &y2))
return;
bline(x1 * sgc.xscale, y1 * sgc.yscale, x2 * sgc.xscale, y2 * sgc.yscale,
sng_dotted_line_plot_func, &context);
}
void sng_draw_electric_line(float x1, float y1, float x2, float y2)
{
struct sng_dotted_plot_func_context context;
context.i = 0;
if (!clip_line(&sgc.c, &x1, &y1, &x2, &y2))
return;
bline(x1 * sgc.xscale, y1 * sgc.yscale, x2 * sgc.xscale, y2 * sgc.yscale,
sng_electric_line_plot_func, &context);
}
static void sng_draw_bright_white_electric_line(float x1, float y1, float x2, float y2, int color)
{
struct sng_dotted_plot_func_context context;
context.i = color;
if (!clip_line(&sgc.c, &x1, &y1, &x2, &y2))
return;
bline(x1 * sgc.xscale, y1 * sgc.yscale, x2 * sgc.xscale, y2 * sgc.yscale,
sng_bright_electric_line_plot_func, &context);
}
void sng_draw_laser_line(float x1, float y1, float x2, float y2, int color)
{
float dx, dy;
if (fabs(x1 - x2) > fabs(y1 - y2)) {
dx = 0;
dy = 1;
} else {
dx = 1;
dy = 0;
}
if (!clip_line(&sgc.c, &x1, &y1, &x2, &y2))
return;
sng_draw_bright_white_electric_line(x1, y1, x2, y2, color);
sng_set_foreground(color);
sng_draw_electric_line(x1 - dx, y1 - dy, x2 - dx, y2 - dy);
sng_draw_electric_line(x1 + dx, y1 + dy, x2 + dx, y2 + dy);
}
void sng_draw_vect_obj(struct my_vect_obj *v, float x, float y)
{
int i;
float x1, y1, x2, y2;
for (i = 0; i < v->npoints-1; i++) {
if (v->p[i+1].x == LINE_BREAK)
i += 2;
x1 = x + v->p[i].x;
y1 = y + v->p[i].y;
x2 = x + v->p[i + 1].x;
y2 = y + v->p[i + 1].y;
sng_current_draw_line(x1, y1, x2, y2);
}
}
/* Draws a letter in the given font at an absolute x,y coords on the screen. */
float sng_abs_xy_draw_letter(struct my_vect_obj **font, unsigned char letter, float x, float y)
{
int i;
float x1, y1, x2, y2;
float minx, maxx, diff;
if (letter == ' ' || letter == '\n' || letter == '\t' || font[letter] == NULL)
return abs(font['Z']->p[0].x - font['Z']->p[1].x);
minx = x + font[letter]->p[0].x;
maxx = minx;
for (i = 0; i < font[letter]->npoints-1; i++) {
if (font[letter]->p[i+1].x == LINE_BREAK)
i += 2;
x1 = x + font[letter]->p[i].x;
y1 = y + font[letter]->p[i].y;
x2 = x + font[letter]->p[i + 1].x;
y2 = y + font[letter]->p[i + 1].y;
if (x1 < minx)
minx = x1;
if (x2 < minx)
minx = x2;
if (x1 > maxx)
maxx = x1;
if (x2 > maxx)
maxx = x2;
if (x1 > 0 && x2 > 0)
sng_current_draw_line(x1, y1, x2, y2);
}
diff = fabs(maxx - minx);
/* if (diff == 0)
return (abs(font['Z']->p[0].x - font['Z']->p[1].x) / 4); */
return diff;
}
/* Used for floating labels in the game. */
/* Draws a string at an absolute x,y position on the screen. */
void sng_abs_xy_draw_string(char *s, int font, float x, float y)
{
int i, dx;
float deltax = 0;
for (i=0;s[i];i++) {
dx = (letter_spacing[font]) + sng_abs_xy_draw_letter(gamefont[font], s[i], x + deltax, y);
deltax += dx;
}
}
void sng_string_bounding_box(char *s, int font, float *bbx1, float *bby1, float *bbx2, float *bby2)
{
struct my_vect_obj **fontobj = gamefont[font];
int i;
*bbx1 = *bbx2 = *bby1 = *bby2 = 0;
for (i=0;s[i];i++) {
unsigned char letter = s[i];
if (letter == ' ' || letter == '\n' || letter == '\t' || fontobj[letter] == NULL) {
letter = 'Z';
}
/* figure out the letter size based on the bouding box */
*bbx2 += fontobj[letter]->bbx2 - fontobj[letter]->bbx1;
/* add between character space */
if (i!=0) *bbx2 += letter_spacing[font];
if (i==0 || fontobj[letter]->bby1 < *bby1) *bby1=fontobj[letter]->bby1;
if (i==0 || fontobj[letter]->bby2 > *bby2) *bby2=fontobj[letter]->bby2;
}
}
/* Used for floating labels in the game. */
/* Draws a string centered at x,y position on the screen. */
void sng_center_xy_draw_string(char *s, int font, float x, float y)
{
float bbx1, bby1, bbx2, bby2;
sng_string_bounding_box(s, font, &bbx1, &bby1, &bbx2, &bby2);
float ox = x - (bbx2 + bbx1)/2.0;
float oy = y - (bby2 + bby1)/2.0;
int i, dx;
float deltax = 0;
for (i=0;s[i];i++) {
dx = (letter_spacing[font]) + sng_abs_xy_draw_letter(gamefont[font], s[i], ox + deltax, oy);
deltax += dx;
}
}
void sng_abs_xy_draw_string_with_cursor(char *s, int font, float x, float y, int cursor_pos, int cursor_on)
{
int i;
float dx;
float deltax = 0;
if (!cursor_on) {
sng_abs_xy_draw_string(s, font, x, y);
return;
}
for (i = 0; s[i]; i++) {
if (i == cursor_pos)
sng_abs_xy_draw_letter(gamefont[font], '_', x + deltax, y);
dx = (letter_spacing[font]) + sng_abs_xy_draw_letter(gamefont[font], s[i], x + deltax, y);
deltax += dx;
}
if (i == cursor_pos)
sng_abs_xy_draw_letter(gamefont[font], '_', x + deltax, y);
}
void sng_draw_point(float x, float y)
{
graph_dev_draw_point(x * sgc.xscale, y * sgc.yscale);
}
/* from http://stackoverflow.com/a/6930407
0 <= h < 360, 0 <= s <= 1, , 0 <= v <= 1 */
static void hsv2rgb(double h, double s, double v, GdkColor* rgb)
{
double hh, p, q, t, ff;
long i;
if(s <= 0.0) { // < is bogus, just shuts up warnings
rgb->red = v * 65535;
rgb->green = v * 65535;
rgb->blue = v * 65535;
return;
}
hh = h;
if(hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = v * (1.0 - s);
q = v * (1.0 - (s * ff));
t = v * (1.0 - (s * (1.0 - ff)));
switch(i) {
case 0:
rgb->red = v * 65535;
rgb->green = t * 65535;
rgb->blue = p * 65535;
break;
case 1:
rgb->red = q * 65535;
rgb->green = v * 65535;
rgb->blue = p * 65535;
break;
case 2:
rgb->red = p * 65535;
rgb->green = v * 65535;
rgb->blue = t * 65535;
break;
case 3:
rgb->red = p * 65535;
rgb->green = q * 65535;
rgb->blue = v * 65535;
break;
case 4:
rgb->red = t * 65535;
rgb->green = p * 65535;
rgb->blue = v * 65535;
break;
case 5:
default:
rgb->red = v * 65535;
rgb->green = p * 65535;
rgb->blue = q * 65535;
break;
}
}
int BLUE = 0;
int GREEN = 0;
int CYAN = 0;
struct gradient_color {
int *color_index;
double h;
double s;
double v;
};
static struct gradient_color gradient_colors[] = {
{&BLUE, 240, 1, 1},
{&GREEN, 120, 1, 1},
{&CYAN, 180, 1, 1}
};
int sng_add_user_color(uint8_t r, uint8_t g, uint8_t b)
{
/* Check and see if color is already known */
int i;
uint16_t r2, g2, b2;
r2 = r << 8;
g2 = g << 8;
b2 = b << 8;
for (i = 0; i < TOTAL_COLORS; i++) {
if (huex[i].red == r2 &&
huex[i].green == g2 &&
huex[i].blue == b2) {
return i;
}
}
/* color is not known, need to try to add it. */
if (MAX_USER_COLORS - nuser_colors <= 0)
return -1; /* no room to add it */
/* add it */
i = USER_COLOR + nuser_colors;
huex[i].red = (uint16_t) r2;
huex[i].green = (uint16_t) g2;
huex[i].blue = (uint16_t) b2;
nuser_colors++;
return nuser_colors - 1 + USER_COLOR;
}
static struct user_color_entry {
char name[20];
int index;
} *user_color;
static int nuser_color_entries;
static int lookup_user_color(char *colorname)
{
int i;
for (i = 0; i < nuser_color_entries; i++)
if (strcmp(user_color[i].name, colorname) == 0)
return user_color[i].index;
return -1;
}
static int parse_user_color_line(char *filename, char *line, int ln,
struct user_color_entry *e)
{
int rc;
unsigned int r, g, b;
char name[100], ui_component[100], colorname[100];
if (line[0] == '#')
return 1; /* comment */
clean_spaces(line);
remove_trailing_whitespace(line);
if (strcmp(line, "") == 0)
return 1; /* comment (blank line) */
memset(name, 0, sizeof(name));
rc = sscanf(line, "color %[^ ] #%02x%02x%02x",
name, &r, &g, &b);
if (rc == 4) {
name[19] = '\0';
memcpy(e->name, name, 20);
e->index = sng_add_user_color(r, g, b);
if (e->index < 0) {
fprintf(stderr, "%s:%d Failed to add user color '%s'\n",
filename, ln, name);
return -1;
}
return 0;
} else {
rc = sscanf(line, "%[^ ] #%02x%02x%02x",
ui_component, &r, &g, &b);
if (rc == 4) {
e->index = sng_add_user_color(r, g, b);
if (e->index < 0) {
fprintf(stderr, "%s:%d Failed to modify ui component color '%s'\n",
filename, ln, line);
return -1;
}
modify_ui_color(ui_component, e->index);
return 0;
} else {
rc = sscanf(line, "%[^ ] %[^ ]",
ui_component, colorname);
if (rc == 2) {
e->index = lookup_user_color(colorname);
if (e->index < 0) {
fprintf(stderr,
"%s:%d Failed to modify ui component color '%s'\n",
filename, ln, line);
return -1;
}
modify_ui_color(ui_component, e->index);
return 0;
}
}
}
fprintf(stderr, "%s:%d: Syntax error: '%s'\n", filename, ln, line);
return -1;
}
static void sng_read_user_colors(char *filename)
{
FILE *f;
char line[1000], *l;
int rc, ln = 0;
struct user_color_entry entry;
if (!filename)
return;
user_color = malloc(sizeof(*user_color) * MAX_USER_COLORS);
memset(user_color, 0, sizeof(*user_color) * MAX_USER_COLORS);
f = fopen(filename, "r");
if (!f) {
fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
goto bailout;
}
while (!feof(f)) {
l = fgets(line, 1000, f);
if (!l)
break;
line[strlen(line) - 1] = '\0';
ln++;
rc = parse_user_color_line(filename, line, ln, &entry);
switch (rc) {
case 0:
user_color[nuser_color_entries] = entry;
nuser_color_entries++;
continue;
case 1: /* comment */
continue;
case -1: /* error of some kind */
continue;
}
}
bailout:
if (f)
fclose(f);
if (user_color)
free(user_color);
return;
}
void sng_setup_colors(void *gtk_widget, char *user_color_file)
{
int i;
BUILD_ASSERT(ARRAY_SIZE(gradient_colors) == NGRADIENTS);
/* values extracted from gdk_color_parse */
huex[WHITE].red = 65535;
huex[WHITE].green = 65535;
huex[WHITE].blue = 65535;
huex[BLACK].red = 0;
huex[BLACK].green = 0;
huex[BLACK].blue = 0;
huex[LIMEGREEN].red = 12850;
huex[LIMEGREEN].green = 52685;
huex[LIMEGREEN].blue = 12850;
huex[DARKGREEN].red = 0;
huex[DARKGREEN].green = 25700;
huex[DARKGREEN].blue = 0;
huex[YELLOW].red = 65535;
huex[YELLOW].green = 65535;
huex[YELLOW].blue = 0;
huex[RED].red = 65535;
huex[RED].green = 0;
huex[RED].blue = 0;
huex[ORANGE].red = 65535;
huex[ORANGE].green = 42405;
huex[ORANGE].blue = 0;
huex[MAGENTA].red = 65535;
huex[MAGENTA].green = 0;
huex[MAGENTA].blue = 65535;
huex[DARKRED].red = 35723;
huex[DARKRED].green = 0;
huex[DARKRED].blue = 0;
huex[AMBER].red = 65535;
huex[AMBER].green = 42405;
huex[AMBER].blue = 0;
huex[DARKTURQUOISE].red = 0;
huex[DARKTURQUOISE].green = 52942;
huex[DARKTURQUOISE].blue = 53713;
huex[ORANGERED].red = 65535;
huex[ORANGERED].green = 17733;
huex[ORANGERED].blue = 0;
for (i = 0; i < NSHADESOFGRAY; i++) {
huex[GRAY + i].red = (i * 32767 * 2) / 256;
huex[GRAY + i].green = (i * 32767 * 2) / 256;
huex[GRAY + i].blue = (i * 32767 * 2) / 256;
}
for (i = 1; i <= NSHADECOLORS; i++) {
int j, r, g, b;
r = snis_randn(32767);
g = snis_randn(32767);
b = snis_randn(32767);
for (j = 0; j < NSHADESOFGRAY / 2; j++) {
int index;
float f;
f = (float) j / (float) (NSHADESOFGRAY / 2.0);
index = GRAY + (i * NSHADESOFGRAY) + j;
huex[index].red = (f * (float) r);
huex[index].green = (f * (float) g);
huex[index].blue = (f * (float) b);
}
for (j = NSHADESOFGRAY / 2; j < NSHADESOFGRAY; j++) {
int index;
float f;
f = (float) (j - NSHADESOFGRAY / 2) / (float) NSHADESOFGRAY / 2.0;
index = GRAY + (i * NSHADESOFGRAY) + j;
huex[index].red = r + (f * ((32767.0 * 2.0) - (float) r));
huex[index].green = g + (f * ((32767.0 * 2.0) - (float) g));
huex[index].blue = b + (f * ((32767.0 * 2.0) - (float) b));
}
}
int grad_index = GRADIENTS;
for (i=0; i<NGRADIENTS; i++ ) {
int j;
double h = gradient_colors[i].h;
double s = gradient_colors[i].s;
double v = gradient_colors[i].v;
/* add the shades from black to color */
for (j=0; j<NGRADIENT_SHADES; j++) {
double f = j/(double)NGRADIENT_SHADES;
double fi = 1.0 - f;
hsv2rgb(h, s + (1.0-s)*fi, v * f, &huex[grad_index]);
grad_index++;
}
/* add the pure color */
hsv2rgb(h, s, v, &huex[grad_index]);
*gradient_colors[i].color_index = grad_index;
grad_index++;
/* add the shades from color to white */
for (j=1; j<=NGRADIENT_SHADES; j++) {
double f = (NGRADIENT_SHADES-j)/(double)NGRADIENT_SHADES;
double fi = 1.0 - f;
hsv2rgb(h, s * f, v + (1.0-v)*fi, &huex[grad_index]);
grad_index++;
}
}
sng_read_user_colors(user_color_file);
fixup_ui_color(BLUE_FIXUP, BLUE);
fixup_ui_color(GREEN_FIXUP, GREEN);
fixup_ui_color(CYAN_FIXUP, CYAN);
graph_dev_setup_colors(gtk_widget, huex, TOTAL_COLORS);
}
void sng_set_foreground_alpha(int c, float a)
{
sgc.hue = c;
graph_dev_set_color(&huex[c], a);
}
void sng_set_foreground(int c)
{
sgc.hue = c;
graph_dev_set_color(&huex[c], -1);
}
struct sng_color sng_get_foreground()
{
struct sng_color color;
color.red = huex[sgc.hue].red / 65535.0;
color.green = huex[sgc.hue].green / 65535.0;
color.blue = huex[sgc.hue].blue / 65535.0;
return color;
}
struct sng_color sng_get_color(int c)
{
struct sng_color color;
color.red = huex[c].red / 65535.0;
color.green = huex[c].green / 65535.0;
color.blue = huex[c].blue / 65535.0;
return color;
}
void sng_set_context(void *gdk_drawable, void *gdk_gc)
{
graph_dev_set_context(gdk_drawable, gdk_gc);
}
void sng_draw_circle(int filled, float x, float y, float r)
{
sng_current_draw_arc(filled, x - r, y - r, r * 2, r * 2, 0, 2.0*M_PI);
}
char *sng_load_png_texture(const char *filename, int flipVertical, int flipHorizontal, int pre_multiply_alpha,
int *w, int *h, int *hasAlpha, char *whynot, int whynotlen)
{
#ifndef WITHOUTOPENGL
int i, j, bit_depth, color_type, row_bytes, image_data_row_bytes;
png_byte header[8];
png_uint_32 tw, th;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_infop end_info = NULL;
png_byte *image_data = NULL;
FILE *fp = fopen(filename, "rb");
if (!fp) {
snprintf(whynot, whynotlen, "Failed to open '%s': %s",
filename, strerror(errno));
return 0;
}
if (fread(header, 1, 8, fp) != 8) {
snprintf(whynot, whynotlen, "Failed to read 8 byte header from '%s'\n",
filename);
goto cleanup;
}
if (png_sig_cmp(header, 0, 8)) {
snprintf(whynot, whynotlen, "'%s' isn't a png file.",
filename);
goto cleanup;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png_ptr) {
snprintf(whynot, whynotlen,
"png_create_read_struct() returned NULL");
goto cleanup;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
snprintf(whynot, whynotlen,
"png_create_info_struct() returned NULL");
goto cleanup;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info) {
snprintf(whynot, whynotlen,
"2nd png_create_info_struct() returned NULL");
goto cleanup;
}
if (setjmp(png_jmpbuf(png_ptr))) {
snprintf(whynot, whynotlen, "libpng encounted an error");
goto cleanup;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
/*
* PNG_TRANSFORM_STRIP_16 |
* PNG_TRANSFORM_PACKING forces 8 bit
* PNG_TRANSFORM_EXPAND forces to expand a palette into RGB
*/
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL);
png_get_IHDR(png_ptr, info_ptr, &tw, &th, &bit_depth, &color_type, NULL, NULL, NULL);
if (bit_depth != 8) {
snprintf(whynot, whynotlen, "load_png_texture only supports 8-bit image channel depth");
goto cleanup;
}
if (color_type != PNG_COLOR_TYPE_RGB && color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
snprintf(whynot, whynotlen, "load_png_texture only supports RGB and RGBA");
goto cleanup;
}
if (w)
*w = tw;
if (h)
*h = th;
int has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
if (hasAlpha)
*hasAlpha = has_alpha;
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
image_data_row_bytes = row_bytes;
/* align to 4 byte boundary */
if (image_data_row_bytes & 0x03)
image_data_row_bytes += 4 - (image_data_row_bytes & 0x03);
png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
image_data = malloc(image_data_row_bytes * th * sizeof(png_byte) + 15);
if (!image_data) {
snprintf(whynot, whynotlen, "malloc failed in load_png_texture");
goto cleanup;
}
int bytes_per_pixel = (color_type == PNG_COLOR_TYPE_RGB_ALPHA ? 4 : 3);
for (i = 0; i < th; i++) {
png_byte *src_row;
png_byte *dest_row = image_data + i * image_data_row_bytes;
if (flipVertical)
src_row = row_pointers[th - i - 1];
else
src_row = row_pointers[i];
if (flipHorizontal) {
for (j = 0; j < tw; j++) {
png_byte *src = src_row + bytes_per_pixel * j;
png_byte *dest = dest_row + bytes_per_pixel * (tw - j - 1);
memcpy(dest, src, bytes_per_pixel);
}
} else {
memcpy(dest_row, src_row, row_bytes);
}
if (has_alpha && pre_multiply_alpha) {
for (j = 0; j < tw; j++) {
png_byte *pixel = dest_row + bytes_per_pixel * j;
float alpha = pixel[3] / 255.0;
pixel[0] = pixel[0] * alpha;
pixel[1] = pixel[1] * alpha;
pixel[2] = pixel[2] * alpha;
}
}
}
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return (char *)image_data;
cleanup:
if (image_data)
free(image_data);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
#else
snprintf(whynot, whynotlen, "load_png_texture: compiled without opengl support.");
#endif
return 0;
}