Tscope5
graphics.c
Go to the documentation of this file.
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 #include "../include/tscope5/graphics.h"
18 #include "../include/tscope5/graphics_internal.h"
19 #include "../include/tscope5/system_internal.h"
20 #include "../include/tscope5/textio_internal.h"
21 
22 #include <allegro5/allegro_color.h>
23 
24 
42 
43 
44 
45 
60 int ts5_set_coordinate_system(const int coordinate_system)
61 {
62  ts5_check_graphics("ts5_set_coordinate_system");
63  ts5_log(TS5_LOGLEVEL_4, "ts5_set_coordinate_system(%d)\n", coordinate_system);
64 
65  int retval = _ts5_status.graphics_status.coordinate_system;
66 
67  if (coordinate_system != TS5_CARTESIAN_COORDINATES && coordinate_system != TS5_DISPLAY_COORDINATES) {
68  ts5_fatal("ts5_set_coordinate_system: coordinate_system should be 0 or 1 (is %d)\n",
69  coordinate_system);
70  }
71 
72  _ts5_status.graphics_status.coordinate_system = coordinate_system;
73 
74  ts5_log(TS5_LOGLEVEL_4, "ts5_set_coordinate_system: set coordinate_system for next display to %d (was %d)\n",
75  _ts5_status.graphics_status.coordinate_system, retval);
76 
77  return retval;
78 }
79 
80 
87 {
88  ts5_check_graphics("ts5_get_coordinate_system");
89  ts5_log(TS5_LOGLEVEL_4, "ts5_get_coordinate_system()\n");
90 
91  return _ts5_status.graphics_status.coordinate_system;
92 }
93 
94 
112 int ts5_set_coordinate_scale(const int coordinate_scale)
113 {
114  ts5_check_graphics("ts5_set_coordinate_scale");
115  ts5_log(TS5_LOGLEVEL_4, "ts5_set_coordinate_scale(%d)\n", coordinate_scale);
116 
117  int retval = _ts5_status.graphics_status.coordinate_scale;
118 
119  if (coordinate_scale != TS5_RELATIVE_COORDINATES && coordinate_scale != TS5_ABSOLUTE_COORDINATES) {
120  ts5_fatal("ts5_set_coordinate_scale: coordinate_scale should be 0 or 1 (is %d)\n", coordinate_scale);
121  }
122 
123  _ts5_status.graphics_status.coordinate_scale = coordinate_scale;
124 
125  ts5_log(TS5_LOGLEVEL_4, "ts5_set_coordinate_scale: set coordinate_scale for next display to %d (was %d)\n",
126  _ts5_status.graphics_status.coordinate_scale, retval);
127 
128  return retval;
129 }
130 
131 
138 {
139  ts5_check_graphics("ts5_get_coordinate_scale");
140  ts5_log(TS5_LOGLEVEL_4, "ts5_get_coordinate_scale()\n");
141 
142  return _ts5_status.graphics_status.coordinate_scale;
143 }
144 
145 
156 void ts5_get_target_size(double *w, double *h)
157 {
158  ts5_check_graphics("ts5_get_target_size");
159  ts5_log(TS5_LOGLEVEL_4, "ts5_get_target_size(%p,%p)\n", w, h);
160 
161  double ww, hh;
162 
163  if (_ts5_status.graphics_status.coordinate_scale==TS5_ABSOLUTE_COORDINATES) {
164  ww = _ts5_status.graphics_status.target_width;
165  hh = _ts5_status.graphics_status.target_height;
166  }
167  else {
168  ww = 1.0;
169  hh = 1.0;
170  }
171 
172  if (w) {
173  *w = ww;
174  }
175 
176  if (h) {
177  *h = hh;
178  }
179 }
180 
181 
190 {
191  ts5_check_graphics("ts5_get_target_width");
192  ts5_log(TS5_LOGLEVEL_4, "ts5_get_target_width()\n");
193 
194  double w;
195  ts5_get_target_size(&w, NULL);
196 
197  return w;
198 }
199 
200 
209 {
210  ts5_check_graphics("ts5_get_target_height");
211  ts5_log(TS5_LOGLEVEL_4, "ts5_get_target_height()\n");
212 
213  double h;
214  ts5_get_target_size(NULL, &h);
215 
216  return h;
217 
218 }
219 
220 
241 {
242  ts5_check_graphics("ts5_relative_to_absolute_coordinate_x");
243  ts5_log(TS5_LOGLEVEL_6, "ts5_relative_to_absolute_coordinate_x(%f)\n", x);
244 
245  double retval;
246 
247  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
248  retval = (x * (_ts5_status.graphics_status.target_width) / 2.0);
249  }
250  else {
251  retval = (x * _ts5_status.graphics_status.target_width);
252  }
253 
254  return retval;
255 }
256 
257 
270 {
271  ts5_check_graphics("ts5_relative_to_absolute_coordinate_y");
272  ts5_log(TS5_LOGLEVEL_6, "ts5_relative_to_absolute_coordinate_y(%f)\n", y);
273 
274  double retval;
275 
276  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
277  retval = (y * (_ts5_status.graphics_status.target_height) / 2.0);
278  }
279  else {
280  retval = (y * _ts5_status.graphics_status.target_height);
281  }
282 
283  return retval;
284 }
285 
296 {
297  ts5_check_graphics("ts5_absolute_to_relative_coordinate_x");
298  ts5_log(TS5_LOGLEVEL_6, "ts5_absolute_to_relative_coordinate_x(%f)\n", x);
299 
300  double retval;
301 
302  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
303  retval = (x / (_ts5_status.graphics_status.target_width) * 2.0);
304  }
305  else {
306  retval = (x / _ts5_status.graphics_status.target_width);
307  }
308 
309  return retval;
310 }
311 
312 
323 {
324  ts5_check_graphics("ts5_absolute_to_relative_coordinate_y");
325  ts5_log(TS5_LOGLEVEL_6, "ts5_absolute_to_relative_coordinate_y(%f)\n", y);
326 
327  double retval;
328 
329  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
330  retval = (y / (_ts5_status.graphics_status.target_height) * 2.0);
331  }
332  else {
333  retval = (y / _ts5_status.graphics_status.target_height);
334  }
335 
336  return retval;
337 }
338 
339 
350 {
351  ts5_check_graphics("ts5_cartesian_to_display_coordinate_x");
352  ts5_log(TS5_LOGLEVEL_6, "ts5_cartesian_to_display_coordinate_x(%f)\n", x);
353 
354  return (_ts5_status.graphics_status.target_width) / 2.0 + x;
355 }
356 
357 
368 {
369  ts5_check_graphics("ts5_cartesian_to_display_coordinate_y");
370  ts5_log(TS5_LOGLEVEL_6, "ts5_cartesian_to_display_coordinate_y(%f)\n", y);
371 
372  return (_ts5_status.graphics_status.target_height) / 2.0 - y;
373 }
374 
375 
386 {
387  ts5_check_graphics("ts5_display_to_cartesian_coordinate_x");
388  ts5_log(TS5_LOGLEVEL_6, "ts5_display_to_cartesian_coordinate_x(%f)\n", x);
389 
390  return x - (_ts5_status.graphics_status.target_width) / 2.0;
391 }
392 
393 
404 {
405  ts5_check_graphics("ts5_display_to_cartesian_coordinate_y");
406  ts5_log(TS5_LOGLEVEL_6, "ts5_display_to_cartesian_coordinate_y(%f)\n", y);
407 
408  return (_ts5_status.graphics_status.target_height) / 2.0 - y;
409 }
410 
411 
413 
414 
415 
416 
428 
429 
430 
431 
442 TS5_COLOR ts5_make_rgb_color(const double r, const double g, const double b, const double a)
443 {
444  ts5_check_graphics("ts5_make_rgb_color");
445  ts5_log(TS5_LOGLEVEL_5, "ts5_make_rgb_color(%f,%f,%f,%f)\n", r, g, b, a);
446 
447  if (r < 0.0|| g < 0.0 || b < 0.0 || a < 0.0 || r > 1.0 || g > 1.0 || b > 1.0 || a > 1.0) {
448  ts5_fatal("ts5_make_rgb_color: color values must be between 0.0 and 1.0\n");
449  }
450 
451  return al_map_rgba_f(r, g, b, a);
452 }
453 
454 
465 TS5_COLOR ts5_make_hsl_color(const double h, const double s, const double l, const double a)
466 {
467  ts5_check_graphics("ts5_make_hsl_color");
468  ts5_log(TS5_LOGLEVEL_5, "ts5_make_hsl_color(%f,%f,%f,%f)\n", h, s, l, a);
469 
470  if (h < 0.0 || s < 0.0 || l < 0.0 || a < 0.0 || h > 360.0 || s > 1.0 || l > 1.0 || a > 1.0) {
471  ts5_fatal("ts5_makehslcolor: h:0.0-360.0, s:0.0-1.0, l:0.0-1.0, a:0.0-1.0\n");
472  }
473 
474  float r, g, b;
475  al_color_hsl_to_rgb(h, s, l, &r, &g, &b);
476 
477  return al_map_rgba_f(r, g, b, a);
478 }
479 
480 
491 TS5_COLOR ts5_make_hsv_color(const double h, const double s, const double v, const double a)
492 {
493  ts5_check_graphics("ts5_make_hsv_color");
494  ts5_log(TS5_LOGLEVEL_5, "ts5_make_hsv_color(%f,%f,%f,%f)\n", h, s, v, a);
495 
496  if (h < 0.0 || s < 0.0 || v < 0.0 || a < 0.0 || h > 360.0 || s > 1.0 || v > 1.0 || a > 1.0) {
497  ts5_fatal("ts5_makehsvcolor: h:0.0-360.0, s:0.0-1.0, v:0.0-1.0, a:0.0-1.0\n");
498  }
499 
500  float r, g, b;
501  al_color_hsv_to_rgb(h, s, v, &r, &g, &b);
502 
503  return al_map_rgba_f(r, g, b, a);
504 }
505 
506 
537 TS5_COLOR ts5_make_named_color(const char *name, const double a)
538 {
539  ts5_check_graphics("ts5_make_named_color");
540  ts5_log(TS5_LOGLEVEL_5, "ts5_make_named_color(%s,%f)\n", name, a);
541 
542  float r, g, b;
543 
544  if (!al_color_name_to_rgb(name, &r, &g, &b)) {
545  ts5_fatal("ts5_make_named_color: color name %s not found\n", name);
546  }
547 
548  return al_map_rgba_f(r, g, b, a);
549 }
550 
551 
553 
554 
555 
556 
573 
574 
575 
576 
590 TS5_COLOR ts5_set_foreground_color(const TS5_COLOR foreground_color)
591 {
592  ts5_check_graphics("ts5_set_foreground_color");
593  ts5_log(TS5_LOGLEVEL_4, "ts5_set_foreground_color((%f,%f,%f,%f))\n",
594  foreground_color.r, foreground_color.g, foreground_color.b,
595  foreground_color.a);
596 
597  TS5_COLOR oldcolor = _ts5_status.graphics_status.foreground_color;
598  _ts5_status.graphics_status.foreground_color = foreground_color;
599 
600  unsigned char r, g, b, a;
601  al_unmap_rgba(foreground_color, &r, &g, &b, &a);
602  ts5_log(TS5_LOGLEVEL_4, "ts5_set_foreground_color r:%f, g:%f, b:%f, alpha:%f\n",
603  r/256, g/256, b/256, a/256);
604 
605  return oldcolor;
606 }
607 
608 
615 {
616  ts5_check_graphics("ts5_get_foreground_color");
617  ts5_log(TS5_LOGLEVEL_4, "ts5_get_foreground_color()\n");
618 
619  return _ts5_status.graphics_status.foreground_color;
620 }
621 
622 
634 TS5_COLOR ts5_set_background_color(const TS5_COLOR background_color)
635 {
636  ts5_check_graphics("ts5_set_background_color");
637  ts5_log(TS5_LOGLEVEL_4, "ts5_set_background_color((%f,%f,%f,%f))\n",
638  background_color.r, background_color.g, background_color.b,
639  background_color.a);
640 
641  TS5_COLOR oldcolor = _ts5_status.graphics_status.background_color;
642  _ts5_status.graphics_status.background_color = background_color;
643 
644  unsigned char r, g, b, a;
645  al_unmap_rgba(background_color, &r, &g, &b, &a);
646  ts5_log(TS5_LOGLEVEL_4, "ts5_set_background_color r:%f, g:%f, b:%f, alpha:%f\n",
647  r/256, g/256, b/256, a/256);
648 
649  return oldcolor;
650 }
651 
652 
659 {
660  ts5_check_graphics("ts5_get_background_color");
661  ts5_log(TS5_LOGLEVEL_4, "ts5_get_background_color()\n");
662 
663  return _ts5_status.graphics_status.background_color;
664 }
665 
666 
678 double ts5_set_drawing_thickness(double drawing_thickness)
679 {
680  ts5_check_graphics("ts5_set_drawing_thickness");
681  ts5_log(TS5_LOGLEVEL_4, "ts5_set_drawing_thickness(%f)\n", drawing_thickness);
682 
683  double retval = _ts5_status.graphics_status.drawing_thickness;
684 
685  if (drawing_thickness < 0.0) {
686  drawing_thickness = -1.0;
687  }
688 
689  _ts5_status.graphics_status.drawing_thickness = drawing_thickness;
690 
691  ts5_log(TS5_LOGLEVEL_4, "ts5_set_drawing_thickness: set drawing_thickness to %f (was %f)\n",
692  _ts5_status.graphics_status.drawing_thickness, retval);
693 
694  return retval;
695 }
696 
697 
704 {
705  ts5_check_graphics("ts5_get_drawing_thickness");
706  ts5_log(TS5_LOGLEVEL_4, "ts5_get_drawing_thickness()\n");
707 
708  return _ts5_status.graphics_status.drawing_thickness;
709 }
710 
711 
723 int ts5_set_fill_mode(const int fill_mode)
724 {
725  ts5_check_graphics("ts5_set_fill_mode");
726  ts5_log(TS5_LOGLEVEL_4, "ts5_set_fill_mode(%d)\n", fill_mode);
727 
728  int retval = _ts5_status.graphics_status.fill_mode;
729 
730  if (fill_mode != TS5_FILL_ON && fill_mode != TS5_FILL_OFF) {
731  ts5_fatal("ts5_set_fill_mode: fill_mode should be TS5_FILL_OFF (off) or TS5_FILL_ON (on)\n");
732  }
733 
734  _ts5_status.graphics_status.fill_mode = fill_mode;
735 
736  ts5_log(TS5_LOGLEVEL_4, "ts5_set_fill_mode: set mode to %d (was %d)\n",
737  _ts5_status.graphics_status.fill_mode, retval);
738 
739  return retval;
740 }
741 
742 
749 {
750  ts5_check_graphics("ts5_get_fill_mode");
751  ts5_log(TS5_LOGLEVEL_4, "ts5_get_fill_mode()\n");
752 
753  return _ts5_status.graphics_status.fill_mode;
754 }
755 
756 
766 int ts5_set_font_type(const int font_type)
767 {
768  ts5_check_graphics("ts5_set_font_type");
769  ts5_log(TS5_LOGLEVEL_4, "ts5_set_font_type(%d)\n", font_type);
770 
771  int retval = _ts5_status.graphics_status.font_type;
772 
773  if (font_type != TS5_COURIER && font_type != TS5_ARIAL && font_type != TS5_TIMES) {
774  ts5_fatal("ts5_set_font_type: font_type should be TS5_COURIER, TS5_ARIAL or TS5_TIMES\n");
775  }
776 
777  _ts5_status.graphics_status.font_type = font_type;
778 
779  ts5_log(TS5_LOGLEVEL_4, "ts5_set_font_type: set font type to %d (was %d)\n",
780  _ts5_status.graphics_status.font_type, retval);
781 
782  _ts5_status.graphics_status.font_index =
783  _ts5_status.graphics_status.font_type * TS5_FONTSTYLES * TS5_FONTSIZES +
784  _ts5_status.graphics_status.font_style * TS5_FONTSIZES +
785  _ts5_status.graphics_status.font_size - 8;
786 
787  return retval;
788 }
789 
790 
797 {
798  ts5_check_graphics("ts5_get_font_type");
799  ts5_log(TS5_LOGLEVEL_4, "ts5_get_font_type()\n");
800 
801  return _ts5_status.graphics_status.font_type;
802 }
803 
804 
814 int ts5_set_font_style(const int font_style)
815 {
816  ts5_check_graphics("ts5_set_font_style");
817  ts5_log(TS5_LOGLEVEL_4, "ts5_set_font_style(%d)\n", font_style);
818 
819  int retval = _ts5_status.graphics_status.font_style;
820 
821  if (font_style != TS5_REGULAR && font_style != TS5_BOLD &&
822  font_style != TS5_ITALIC && font_style != TS5_BOLD_ITALIC) {
823  ts5_fatal("ts5_set_font_style: font_style should be TS5_REGULAR, TS5_BOLD,",
824  " TS5_ITALIC or TS5_BOLD_ITALIC\n");
825  }
826 
827  _ts5_status.graphics_status.font_style = font_style;
828 
829  ts5_log(TS5_LOGLEVEL_4, "ts5_set_font_style: set font style to %d (was %d)\n",
830  _ts5_status.graphics_status.font_style, retval);
831 
832  _ts5_status.graphics_status.font_index =
833  _ts5_status.graphics_status.font_type * TS5_FONTSTYLES * TS5_FONTSIZES +
834  _ts5_status.graphics_status.font_style * TS5_FONTSIZES +
835  _ts5_status.graphics_status.font_size - 8;
836 
837  return retval;
838 }
839 
840 
847 {
848  ts5_check_graphics("ts5_get_font_style");
849  ts5_log(TS5_LOGLEVEL_4, "ts5_get_font_style()\n");
850 
851  return _ts5_status.graphics_status.font_style;
852 }
853 
854 
864 int ts5_set_font_size(int font_size)
865 {
866  ts5_check_graphics("ts5_set_font_size");
867  ts5_log(TS5_LOGLEVEL_4, "ts5_set_font_size(%d)\n", font_size);
868 
869  int retval = _ts5_status.graphics_status.font_size;
870 
871  if (font_size < 8 || font_size > 107) {
872  ts5_fatal("ts5_set_font_size: font_size should be between 8 and 107\n");
873  }
874 
875  _ts5_status.graphics_status.font_size = font_size;
876 
877  ts5_log(TS5_LOGLEVEL_4, "ts5_set_font_size: set font size to %d (was %d)\n",
878  _ts5_status.graphics_status.font_size, retval);
879 
880  _ts5_status.graphics_status.font_index =
881  _ts5_status.graphics_status.font_type * TS5_FONTSTYLES * TS5_FONTSIZES +
882  _ts5_status.graphics_status.font_style * TS5_FONTSIZES +
883  _ts5_status.graphics_status.font_size - 8;
884 
885  return retval;
886 }
887 
888 
895 {
896  ts5_check_graphics("ts5_get_font_size");
897  ts5_check_textio("ts5_get_font_size");
898  ts5_log(TS5_LOGLEVEL_4, "ts5_get_font_size()\n");
899 
900  return _ts5_status.graphics_status.font_size;
901 }
902 
903 
913 int ts5_set_font_index(const int font_index)
914 {
915  ts5_check_graphics("ts5_set_font_index");
916  ts5_log(TS5_LOGLEVEL_4, "ts5_set_font_index(%d)\n", font_index);
917 
918  int retval = _ts5_status.graphics_status.font_index;
919 
920  if (font_index < 0 || font_index >= TS5_NFONTS) {
921  ts5_fatal("ts5_set_font_index: font_index should be between 0 and TS5_NFONTS-1\n");
922  }
923 
924  _ts5_status.graphics_status.font_index = font_index;
925 
926  // update size etc.
927  _ts5_status.graphics_status.font_size = (font_index % TS5_FONTSIZES) + 8;
928  _ts5_status.graphics_status.font_style = (font_index / TS5_FONTSIZES) % TS5_FONTSTYLES;
929  _ts5_status.graphics_status.font_type = font_index / (TS5_FONTSIZES * TS5_FONTSTYLES);
930 
931  ts5_log(TS5_LOGLEVEL_4, "ts5_set_font_index: set font index to %d (was %d)\n",
932  _ts5_status.graphics_status.font_index, retval);
933 
934  return retval;
935 }
936 
937 
944 {
945  ts5_check_graphics("ts5_get_font_index");
946  ts5_log(TS5_LOGLEVEL_4, "ts5_get_font_index()\n");
947 
948  return _ts5_status.graphics_status.font_index;
949 }
950 
951 
961 int ts5_set_text_alignment(const int text_alignment)
962 {
963  ts5_check_graphics("ts5_set_text_alignment");
964  ts5_log(TS5_LOGLEVEL_4, "ts5_set_text_alignment(%d)\n", text_alignment);
965 
966  int retval = _ts5_status.graphics_status.text_alignment;
967 
968  if (text_alignment != TS5_ALIGN_LEFT && text_alignment != TS5_ALIGN_CENTER &&
969  text_alignment != TS5_ALIGN_RIGHT) {
970 
971  ts5_fatal("ts5_set_text_alignment: text_alignment should be TS5_ALIGN_LEFT, TS5_ALIGN_CENTER",
972  " or TS5_ALIGN_RIGHT\n");
973  }
974 
975  _ts5_status.graphics_status.text_alignment = text_alignment;
976 
977  ts5_log(TS5_LOGLEVEL_4, "ts5_set_text_alignment: set alignment to %d (was %d)\n",
978  _ts5_status.graphics_status.text_alignment, retval);
979 
980  return retval;
981 }
982 
983 
990 {
991  ts5_check_graphics("ts5_get_text_alignment");
992  ts5_log(TS5_LOGLEVEL_4, "ts5_get_text_alignment()\n");
993 
994  return _ts5_status.graphics_status.text_alignment;
995 }
996 
997 
999 
1000