20 #include "../include/tscope5/textio.h"
21 #include "../include/tscope5/display.h"
22 #include "../include/tscope5/graphics.h"
23 #include "../include/tscope5/textio_internal.h"
24 #include "../include/tscope5/system_internal.h"
25 #include "../include/tscope5/keyboard_internal.h"
26 #include "../include/tscope5/graphics_internal.h"
29 #include <allegro5/allegro_font.h>
30 #include <allegro5/allegro_ttf.h>
66 double ts5_printf(
double x,
double y,
const char *format, ...)
69 ts5_log(TS5_LOGLEVEL_5,
"ts5_printf(%f,%f,...)\n", x, y);
71 if (_ts5_status.graphics.coordinate_scale
72 == TS5_RELATIVE_COORDINATES) {
77 if (_ts5_status.graphics.coordinate_system
78 == TS5_CARTESIAN_COORDINATES) {
81 + _ts5_status.graphics.font_size / 2.0);
84 x *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
85 y *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
92 buf = al_ustr_new(
"");
93 al_ustr_vappendf(buf, format, ap);
97 if (!_ts5_status.display[_ts5_status.active_display].multisampling) {
98 font = _ts5_status.graphics.font->aliased_font;
101 font = _ts5_status.graphics.font->multisampled_font;
105 _ts5_status.graphics.foreground_color,
106 x, y, _ts5_status.graphics.text_alignment, buf);
108 width = al_get_ustr_width(_ts5_status.graphics.font->aliased_font, buf);
111 if (_ts5_status.graphics.coordinate_scale
112 == TS5_RELATIVE_COORDINATES) {
113 width/=_ts5_status.graphics.target_width;
131 const char *format, ...)
134 ts5_log(TS5_LOGLEVEL_5,
"ts5_printf_justify(%f,%f,%f,%f,...)\n",
137 if (_ts5_status.graphics.coordinate_scale
138 == TS5_RELATIVE_COORDINATES) {
144 if (_ts5_status.graphics.coordinate_system
145 == TS5_CARTESIAN_COORDINATES) {
149 + _ts5_status.graphics.font_size / 2.0);
152 x1 *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
153 x2 *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
154 y *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
155 diff *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
160 va_start(ap, format);
161 buf = al_ustr_new(
"");
162 al_ustr_vappendf(buf, format, ap);
166 if (!_ts5_status.display[_ts5_status.active_display].multisampling) {
167 font = _ts5_status.graphics.font->aliased_font;
170 font = _ts5_status.graphics.font->multisampled_font;
173 al_draw_justified_ustr(font,
174 _ts5_status.graphics.foreground_color,
176 _ts5_status.graphics.text_alignment, buf);
192 ts5_log(TS5_LOGLEVEL_5,
"ts5_printf_ustr(%f,%f,...)\n", x, y);
194 if (_ts5_status.graphics.coordinate_scale
195 == TS5_RELATIVE_COORDINATES) {
200 if (_ts5_status.graphics.coordinate_system
201 == TS5_CARTESIAN_COORDINATES) {
204 + _ts5_status.graphics.font_size / 2.0);
207 x *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
208 y *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
211 if (!_ts5_status.display[_ts5_status.active_display].multisampling) {
212 font = _ts5_status.graphics.font->aliased_font;
215 font = _ts5_status.graphics.font->multisampled_font;
219 _ts5_status.graphics.foreground_color,
220 x, y, _ts5_status.graphics.text_alignment, ustr);
223 width = al_get_ustr_width(_ts5_status.graphics.font->aliased_font, ustr);
225 if (_ts5_status.graphics.coordinate_scale
226 == TS5_RELATIVE_COORDINATES) {
227 width/=_ts5_status.graphics.target_width;
230 width /= _ts5_status.display[_ts5_status.active_display].sampling_factor;
247 const TS5_USTR *ustr)
250 ts5_log(TS5_LOGLEVEL_5,
"ts5_printf_justify_ustr(%f,%f,%f,%f,...)\n",
253 if (_ts5_status.graphics.coordinate_scale
254 == TS5_RELATIVE_COORDINATES) {
260 if (_ts5_status.graphics.coordinate_system
261 == TS5_CARTESIAN_COORDINATES) {
265 + _ts5_status.graphics.font_size / 2.0);
268 x1 *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
269 x2 *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
270 y *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
271 diff *= _ts5_status.display[_ts5_status.active_display].sampling_factor;
274 if (!_ts5_status.display[_ts5_status.active_display].multisampling) {
275 font = _ts5_status.graphics.font->aliased_font;
278 font = _ts5_status.graphics.font->multisampled_font;
281 al_draw_justified_ustr(font,
282 _ts5_status.graphics.foreground_color,
284 _ts5_status.graphics.text_alignment, ustr);
297 int ts5_scanf(
double x,
double y,
const char *format, ...)
301 ts5_log(TS5_LOGLEVEL_5,
"ts5_scanf(%f,%f,...)\n", x, y);
303 char str[TS5_MAX_CHAR];
304 int done = 0, pos = 0;
306 TS5_COLOR fg = _ts5_status.graphics.foreground_color;
307 TS5_COLOR bg = _ts5_status.graphics.background_color;
309 ALLEGRO_EVENT_QUEUE *event_queue;
310 event_queue = al_create_event_queue();
313 ts5_fatal(
"ts5_scanf: could not create event cue for keyboard\n");
317 al_register_event_source(event_queue, al_get_keyboard_event_source());
322 al_wait_for_event(event_queue, &event);
324 if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
329 ascii =
event.keyboard.unichar;
330 if (ascii == 3 || ascii == 13) {
333 else if (ascii == 8 || ascii == 127) {
337 else if (ascii == 0) {
345 if (pos > TS5_MAX_CHAR) {
355 al_unregister_event_source(event_queue, al_get_keyboard_event_source());
356 al_destroy_event_queue(event_queue);
360 va_start(args, format);
361 i = vsscanf(str, format, args);
378 ts5_log(TS5_LOGLEVEL_5,
"ts5_get_text_width(...)\n");
384 va_start(ap, format);
385 buf = al_ustr_new(
"");
386 al_ustr_vappendf(buf, format, ap);
389 width = al_get_ustr_width(_ts5_status.graphics.font->aliased_font, buf);
392 if (_ts5_status.graphics.coordinate_scale
393 == TS5_RELATIVE_COORDINATES) {
394 width/=_ts5_status.graphics.target_width;
411 ts5_log(TS5_LOGLEVEL_5,
"ts5_get_text_width_ustr(...)\n");
414 width = al_get_ustr_width(_ts5_status.graphics.font->aliased_font, ustr);
416 if (_ts5_status.graphics.coordinate_scale
417 == TS5_RELATIVE_COORDINATES) {
418 width/=_ts5_status.graphics.target_width;
452 ts5_log(TS5_LOGLEVEL_4,
"ts5_read_font(%s,%f)\n", fontfile, size);
454 ts5_log(TS5_LOGLEVEL_2,
"ts5_read_font: loading font %s %f\n",
458 font = (TS5_FONT *)al_malloc(
sizeof(TS5_FONT));
459 font->aliased_font = al_load_ttf_font(fontfile, -size, 0);
460 font->multisampled_font = al_load_ttf_font(fontfile,
461 -size * _ts5_status.display[_ts5_status.active_display].sampling_factor, 0);
463 if (!font->aliased_font) {
464 ts5_fatal(
"ts5_read_font: could not load font %s %d\n", fontfile,
465 _ts5_status.graphics.font_size);
482 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font(%p)\n", font);
485 ts5_fatal(
"ts5_set_font: font is a NULL pointer\n");
488 TS5_FONT *retval = _ts5_status.graphics.font;
489 _ts5_status.graphics.font = font;
490 _ts5_status.graphics.font_index = TS5_USERFONT;
492 _ts5_status.graphics.font_size =
493 al_get_font_line_height(_ts5_status.graphics.font->aliased_font);
510 ts5_log(TS5_LOGLEVEL_2,
"ts5_free_font(%p)\n", font);
513 al_destroy_font(font->aliased_font);
514 al_destroy_font(font->multisampled_font);
516 ts5_log(TS5_LOGLEVEL_2,
"ts5_free_font: removed font\n");
void ts5_printf_justify(double x1, double x2, double y, double diff, const char *format,...)
Write justified text to the active bitmap.
TS5_FONT * ts5_set_font(TS5_FONT *font)
Set a user defined font.
int ts5_scanf(double x, double y, const char *format,...)
Read text from the keyboard.
void ts5_printf_justify_ustr(double x1, double x2, double y, double diff, const TS5_USTR *ustr)
Write justified UTF-8 encoded text to the active bitmap.
void ts5_check_textio(char *calling_function)
Do some checks at the start of each textio function.
double ts5_get_text_width_ustr(TS5_USTR *ustr)
Get the width of a UTF-8 string without printing it.
double ts5_flip_display()
Make what has been drawn visible on the screen.
double ts5_printf(double x, double y, const char *format,...)
Write text to the active bitmap.
TS5_COLOR ts5_set_foreground_color(const TS5_COLOR foreground_color)
Set the foreground color.
TS5_FONT * ts5_read_font(const char *fontfile, double size)
Load a user defined font.
double ts5_printf_ustr(double x, double y, const TS5_USTR *ustr)
Write UTF-8 encoded text to the active bitmap.
double ts5_relative_to_absolute_coordinate_x(const double x)
Convert a relative horizontal coordinate into an absolute horizontal coordinate.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
double ts5_cartesian_to_display_coordinate_x(const double x)
Convert a horizontal Cartesian coordinate into a horizontal display coordinate.
double ts5_cartesian_to_display_coordinate_y(const double y)
Convert a vertical Cartesian coordinate into a vertical display coordinate.
void ts5_check_keyboard(char *calling_function)
Do some checks at the start of each keyboard function.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
double ts5_relative_to_absolute_coordinate_y(const double y)
Convert a relative vertical coordinate into an absolute vertical coordinate.
double ts5_get_text_width(const char *format,...)
Get the width of a string without printing it.
void ts5_free_font(TS5_FONT *font)
Free the memory used by a user defined font.