1
2
3
4
5
6
7 :(before "End Globals")
8 int Display_row = 0;
9 int Display_column = 0;
10
11 :(before "End Includes")
12 #define CHECK_SCREEN \
13 ¦ if (!tb_is_active()) { \
14 ¦ ¦ if (Run_tests) \
15 ¦ ¦ ¦ raise << maybe(current_recipe_name()) << "tried to print to real screen in a test!\n" << end(); \
16 ¦ ¦ else \
17 ¦ ¦ ¦ raise << maybe(current_recipe_name()) << "tried to print to real screen before 'open-console' or after 'close-console'\n" << end(); \
18 ¦ ¦ break; \
19 ¦ }
20 #define CHECK_CONSOLE \
21 ¦ if (!tb_is_active()) { \
22 ¦ ¦ if (Run_tests) \
23 ¦ ¦ ¦ raise << maybe(current_recipe_name()) << "tried to read event from real keyboard/mouse in a test!\n" << end(); \
24 ¦ ¦ else \
25 ¦ ¦ ¦ raise << maybe(current_recipe_name()) << "tried to read event from real keyboard/mouse before 'open-console' or after 'close-console'\n" << end(); \
26 ¦ ¦ break; \
27 ¦ }
28
29 :(before "End Primitive Recipe Declarations")
30 OPEN_CONSOLE,
31 :(before "End Primitive Recipe Numbers")
32 put(Recipe_ordinal, "open-console", OPEN_CONSOLE);
33 :(before "End Primitive Recipe Checks")
34 case OPEN_CONSOLE: {
35 break;
36 }
37 :(before "End Primitive Recipe Implementations")
38 case OPEN_CONSOLE: {
39 tb_init();
40 std::setvbuf(stdout, NULL, _IONBF, 0);
41 Display_row = Display_column = 0;
42 int width = tb_width();
43 int height = tb_height();
44 if (width > 222 || height > 222) tb_shutdown();
45 if (width > 222)
46 ¦ raise << "sorry, Mu doesn't support windows wider than 222 characters in console mode. Please resize your window.\n" << end();
47 if (height > 222)
48 ¦ raise << "sorry, Mu doesn't support windows taller than 222 characters in console mode. Please resize your window.\n" << end();
49 break;
50 }
51
52 :(before "End Primitive Recipe Declarations")
53 CLOSE_CONSOLE,
54 :(before "End Primitive Recipe Numbers")
55 put(Recipe_ordinal, "close-console", CLOSE_CONSOLE);
56 :(before "End Primitive Recipe Checks")
57 case CLOSE_CONSOLE: {
58 break;
59 }
60 :(before "End Primitive Recipe Implementations")
61 case CLOSE_CONSOLE: {
62 tb_clear();
63 tb_shutdown();
64 break;
65 }
66
67 :(before "End Teardown")
68 tb_shutdown();
69
70 :(before "End Primitive Recipe Declarations")
71 CLEAR_DISPLAY,
72 :(before "End Primitive Recipe Numbers")
73 put(Recipe_ordinal, "clear-display", CLEAR_DISPLAY);
74 :(before "End Primitive Recipe Checks")
75 case CLEAR_DISPLAY: {
76 break;
77 }
78 :(before "End Primitive Recipe Implementations")
79 case CLEAR_DISPLAY: {
80 CHECK_SCREEN;
81 tb_clear();
82 Display_row = Display_column = 0;
83 break;
84 }
85
86 :(before "End Primitive Recipe Declarations")
87 PRINT_CHARACTER_TO_DISPLAY,
88 :(before "End Primitive Recipe Numbers")
89 put(Recipe_ordinal, "print-character-to-display", PRINT_CHARACTER_TO_DISPLAY);
90 :(before "End Primitive Recipe Checks")
91 case PRINT_CHARACTER_TO_DISPLAY: {
92 if (inst.ingredients.empty()) {
93 ¦ raise << maybe(get(Recipe, r).name) << "'print-character-to-display' requires at least one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
94 ¦ break;
95 }
96 if (!is_mu_number(inst.ingredients.at(0))) {
97 ¦ raise << maybe(get(Recipe, r).name) << "first ingredient of 'print-character-to-display' should be a character, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
98 ¦ break;
99 }
100 if (SIZE(inst.ingredients) > 1) {
101 ¦ if (!is_mu_number(inst.ingredients.at(1))) {
102 ¦ ¦ raise << maybe(get(Recipe, r).name) << "second ingredient of 'print-character-to-display' should be a foreground color number, but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
103 ¦ ¦ break;
104 ¦ }
105 }
106 if (SIZE(inst.ingredients) > 2) {
107 ¦ if (!is_mu_number(inst.ingredients.at(2))) {
108 ¦ ¦ raise << maybe(get(Recipe, r).name) << "third ingredient of 'print-character-to-display' should be a background color number, but got '" << inst.ingredients.at(2).original_string << "'\n" << end();
109 ¦ ¦ break;
110 ¦ }
111 }
112 break;
113 }
114 :(before "End Primitive Recipe Implementations")
115 case PRINT_CHARACTER_TO_DISPLAY: {
116 CHECK_SCREEN;
117 int h=tb_height(), w=tb_width();
118 int height = (h >= 0) ? h : 0;
119 int width = (w >= 0) ? w : 0;
120 int c = ingredients.at(0).at(0);
121 int color = TB_BLACK;
122 if (SIZE(ingredients) > 1) {
123 ¦ color = ingredients.at(1).at(0);
124 }
125 int bg_color = TB_BLACK;
126 if (SIZE(ingredients) > 2) {
127 ¦ bg_color = ingredients.at(2).at(0);
128 ¦ if (bg_color == 0) bg_color = TB_BLACK;
129 }
130 tb_print(c, color, bg_color);
131
132 if (c == '\n') {
133 ¦ if (Display_row < height-1) ++Display_row;
134 }
135 else if (c == '\r') {
136 ¦ Display_column = 0;
137 }
138 else if (c == '\b') {
139 ¦ if (Display_column > 0) --Display_column;
140 }
141 else {
142 ¦ ++Display_column;
143 ¦ if (Display_column >= width) {
144 ¦ ¦ Display_column = 0;
145 ¦ ¦ if (Display_row < height-1) ++Display_row;
146 ¦ }
147 }
148 break;
149 }
150
151 :(before "End Primitive Recipe Declarations")
152 CURSOR_POSITION_ON_DISPLAY,
153 :(before "End Primitive Recipe Numbers")
154 put(Recipe_ordinal, "cursor-position-on-display", CURSOR_POSITION_ON_DISPLAY);
155 :(before "End Primitive Recipe Checks")
156 case CURSOR_POSITION_ON_DISPLAY: {
157 break;
158 }
159 :(before "End Primitive Recipe Implementations")
160 case CURSOR_POSITION_ON_DISPLAY: {
161 CHECK_SCREEN;
162 products.resize(2);
163 products.at(0).push_back(Display_row);
164 products.at(1).push_back(Display_column);
165 break;
166 }
167
168 :(before "End Primitive Recipe Declarations")
169 MOVE_CURSOR_ON_DISPLAY,
170 :(before "End Primitive Recipe Numbers")
171 put(Recipe_ordinal, "move-cursor-on-display", MOVE_CURSOR_ON_DISPLAY);
172 :(before "End Primitive Recipe Checks")
173 case MOVE_CURSOR_ON_DISPLAY: {
174 if (SIZE(inst.ingredients) != 2) {
175 ¦ raise << maybe(get(Recipe, r).name) << "'move-cursor-on-display' requires two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
176 ¦ break;
177 }
178 if (!is_mu_number(inst.ingredients.at(0))) {
179 ¦ raise << maybe(get(Recipe, r).name) << "first ingredient of 'move-cursor-on-display' should be a row number, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
180 ¦ break;
181 }
182 if (!is_mu_number(inst.ingredients.at(1))) {
183 ¦ raise << maybe(get(Recipe, r).name) << "second ingredient of 'move-cursor-on-display' should be a column number, but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
184 ¦ break;
185 }
186 break;
187 }
188 :(before "End Primitive Recipe Implementations")
189 case MOVE_CURSOR_ON_DISPLAY: {
190 CHECK_SCREEN;
191 Display_row = ingredients.at(0).at(0);
192 Display_column = ingredients.at(1).at(0);
193 tb_set_cursor(Display_column, Display_row);
194 break;
195 }
196
197 :(before "End Primitive Recipe Declarations")
198 MOVE_CURSOR_DOWN_ON_DISPLAY,
199 :(before "End Primitive Recipe Numbers")
200 put(Recipe_ordinal, "move-cursor-down-on-display", MOVE_CURSOR_DOWN_ON_DISPLAY);
201 :(before "End Primitive Recipe Checks")
202 case MOVE_CURSOR_DOWN_ON_DISPLAY: {
203 break;
204 }
205 :(before "End Primitive Recipe Implementations")
206 case MOVE_CURSOR_DOWN_ON_DISPLAY: {
207 CHECK_SCREEN;
208 int h=tb_height();
209 int height = (h >= 0) ? h : 0;
210 if (Display_row < height-1) {
211 ¦ ++Display_row;
212 ¦ tb_set_cursor(Display_column, Display_row);
213 }
214 break;
215 }
216
217 :(before "End Primitive Recipe Declarations")
218 MOVE_CURSOR_UP_ON_DISPLAY,
219 :(before "End Primitive Recipe Numbers")
220 put(Recipe_ordinal, "move-cursor-up-on-display", MOVE_CURSOR_UP_ON_DISPLAY);
221 :(before "End Primitive Recipe Checks")
222 case MOVE_CURSOR_UP_ON_DISPLAY: {
223 break;
224 }
225 :(before "End Primitive Recipe Implementations")
226 case MOVE_CURSOR_UP_ON_DISPLAY: {
227 CHECK_SCREEN;
228 if (Display_row > 0) {
229 ¦ --Display_row;
230 ¦ tb_set_cursor(Display_column, Display_row);
231 }
232 break;
233 }
234
235 :(before "End Primitive Recipe Declarations")
236 MOVE_CURSOR_RIGHT_ON_DISPLAY,
237 :(before "End Primitive Recipe Numbers")
238 put(Recipe_ordinal, "move-cursor-right-on-display", MOVE_CURSOR_RIGHT_ON_DISPLAY);
239 :(before "End Primitive Recipe Checks")
240 case MOVE_CURSOR_RIGHT_ON_DISPLAY: {
241 break;
242 }
243 :(before "End Primitive Recipe Implementations")
244 case MOVE_CURSOR_RIGHT_ON_DISPLAY: {
245 CHECK_SCREEN;
246 int w=tb_width();
247 int width = (w >= 0) ? w : 0;
248 if (Display_column < width-1) {
249 ¦ ++Display_column;
250 ¦ tb_set_cursor(Display_column, Display_row);
251 }
252 break;
253 }
254
255 :(before "End Primitive Recipe Declarations")
256 MOVE_CURSOR_LEFT_ON_DISPLAY,
257 :(before "End Primitive Recipe Numbers")
258 put(Recipe_ordinal, "move-cursor-left-on-display", MOVE_CURSOR_LEFT_ON_DISPLAY);
259 :(before "End Primitive Recipe Checks")
260 case MOVE_CURSOR_LEFT_ON_DISPLAY: {
261 break;
262 }
263 :(before "End Primitive Recipe Implementations")
264 case MOVE_CURSOR_LEFT_ON_DISPLAY: {
265 CHECK_SCREEN;
266 if (Display_column > 0) {
267 ¦ --Display_column;
268 ¦ tb_set_cursor(Display_column, Display_row);
269 }
270 break;
271 }
272
273
274 :(before "End $print 10/newline Special-cases")
275 else if (tb_is_active()) {
276 move_cursor_to_start_of_next_line_on_display();
277 }
278 :(code)
279 void move_cursor_to_start_of_next_line_on_display() {
280 if (Display_row < tb_height()-1) ++Display_row;
281 else Display_row = 0;
282 Display_column = 0;
283 tb_set_cursor(Display_column, Display_row);
284 }
285
286 :(before "End Primitive Recipe Declarations")
287 DISPLAY_WIDTH,
288 :(before "End Primitive Recipe Numbers")
289 put(Recipe_ordinal, "display-width", DISPLAY_WIDTH);
290 :(before "End Primitive Recipe Checks")
291 case DISPLAY_WIDTH: {
292 break;
293 }
294 :(before "End Primitive Recipe Implementations")
295 case DISPLAY_WIDTH: {
296 CHECK_SCREEN;
297 products.resize(1);
298 products.at(0).push_back(tb_width());
299 break;
300 }
301
302 :(before "End Primitive Recipe Declarations")
303 DISPLAY_HEIGHT,
304 :(before "End Primitive Recipe Numbers")
305 put(Recipe_ordinal, "display-height", DISPLAY_HEIGHT);
306 :(before "End Primitive Recipe Checks")
307 case DISPLAY_HEIGHT: {
308 break;
309 }
310 :(before "End Primitive Recipe Implementations")
311 case DISPLAY_HEIGHT: {
312 CHECK_SCREEN;
313 products.resize(1);
314 products.at(0).push_back(tb_height());
315 break;
316 }
317
318
319
320 :(before "End Primitive Recipe Declarations")
321 WAIT_FOR_SOME_INTERACTION,
322 :(before "End Primitive Recipe Numbers")
323 put(Recipe_ordinal, "wait-for-some-interaction", WAIT_FOR_SOME_INTERACTION);
324 :(before "End Primitive Recipe Checks")
325 case WAIT_FOR_SOME_INTERACTION: {
326 break;
327 }
328 :(before "End Primitive Recipe Implementations")
329 case WAIT_FOR_SOME_INTERACTION: {
330 CHECK_SCREEN;
331 tb_event event;
332 tb_poll_event(&event);
333 break;
334 }
335
336 :(before "End Primitive Recipe Declarations")
337 CHECK_FOR_INTERACTION,
338 :(before "End Primitive Recipe Numbers")
339 put(Recipe_ordinal, "check-for-interaction", CHECK_FOR_INTERACTION);
340 :(before "End Primitive Recipe Checks")
341 case CHECK_FOR_INTERACTION: {
342 break;
343 }
344 :(before "End Primitive Recipe Implementations")
345 case CHECK_FOR_INTERACTION: {
346 CHECK_CONSOLE;
347 products.resize(2);
348 tb_event event;
349 int event_type = tb_peek_event(&event, 5);
350 if (event_type == TB_EVENT_KEY && event.ch) {
351 ¦ products.at(0).push_back(0);
352 ¦ products.at(0).push_back(event.ch);
353 ¦ products.at(0).push_back(0);
354 ¦ products.at(0).push_back(0);
355 ¦ products.at(1).push_back(true);
356 ¦ break;
357 }
358
359 if (event_type == TB_EVENT_KEY && event.key < 0xff) {
360 ¦ products.at(0).push_back(0);
361 ¦ if (event.key == TB_KEY_CTRL_C) {
362 ¦ ¦
363 ¦ ¦ tb_set_cursor(tb_width()-1, tb_height()-1);
364 ¦ ¦ cout << "\r\n";
365 ¦ ¦ tb_shutdown();
366 ¦ ¦ exit(1);
367 ¦ }
368 ¦ if (event.key == TB_KEY_BACKSPACE2) event.key = TB_KEY_BACKSPACE;
369 ¦ if (event.key == TB_KEY_CARRIAGE_RETURN) event.key = TB_KEY_NEWLINE;
370 ¦ products.at(0).push_back(event.key);
371 ¦ products.at(0).push_back(0);
372 ¦ products.at(0).push_back(0);
373 ¦ products.at(1).push_back(true);
374 ¦ break;
375 }
376
377 if (event_type == TB_EVENT_KEY) {
378 ¦ products.at(0).push_back(1);
379 ¦ products.at(0).push_back(event.key);
380 ¦ products.at(0).push_back(0);
381 ¦ products.at(0).push_back(0);
382 ¦ products.at(1).push_back(true);
383 ¦ break;
384 }
385 if (event_type == TB_EVENT_MOUSE) {
386 ¦ products.at(0).push_back(2);
387 ¦ products.at(0).push_back(event.key);
388 ¦ products.at(0).push_back(event.y);
389 ¦ products.at(0).push_back(event.x);
390 ¦ products.at(1).push_back(true);
391 ¦ break;
392 }
393 if (event_type == TB_EVENT_RESIZE) {
394 ¦ products.at(0).push_back(3);
395 ¦ products.at(0).push_back(event.w);
396 ¦ products.at(0).push_back(event.h);
397 ¦ products.at(0).push_back(0);
398 ¦ products.at(1).push_back(true);
399 ¦ break;
400 }
401 assert(event_type == 0);
402 products.at(0).push_back(0);
403 products.at(0).push_back(0);
404 products.at(0).push_back(0);
405 products.at(0).push_back(0);
406 products.at(1).push_back(false);
407 break;
408 }
409
410 :(before "End Primitive Recipe Declarations")
411 INTERACTIONS_LEFT,
412 :(before "End Primitive Recipe Numbers")
413 put(Recipe_ordinal, "interactions-left?", INTERACTIONS_LEFT);
414 :(before "End Primitive Recipe Checks")
415 case INTERACTIONS_LEFT: {
416 break;
417 }
418 :(before "End Primitive Recipe Implementations")
419 case INTERACTIONS_LEFT: {
420 CHECK_CONSOLE;
421 products.resize(1);
422 products.at(0).push_back(tb_event_ready());
423 break;
424 }
425
426
427
428 :(before "End Primitive Recipe Declarations")
429 CLEAR_LINE_ON_DISPLAY,
430 :(before "End Primitive Recipe Numbers")
431 put(Recipe_ordinal, "clear-line-on-display", CLEAR_LINE_ON_DISPLAY);
432 :(before "End Primitive Recipe Checks")
433 case CLEAR_LINE_ON_DISPLAY: {
434 break;
435 }
436 :(before "End Primitive Recipe Implementations")
437 case CLEAR_LINE_ON_DISPLAY: {
438 CHECK_SCREEN;
439 int width = tb_width();
440 for (int x = Display_column; x < width; ++x)
441 ¦ tb_print(' ', TB_WHITE, TB_BLACK);
442 tb_set_cursor(Display_column, Display_row);
443 break;
444 }
445
446 :(before "End Primitive Recipe Declarations")
447 CLEAR_DISPLAY_FROM,
448 :(before "End Primitive Recipe Numbers")
449 put(Recipe_ordinal, "clear-display-from", CLEAR_DISPLAY_FROM);
450 :(before "End Primitive Recipe Checks")
451 case CLEAR_DISPLAY_FROM: {
452 break;
453 }
454 :(before "End Primitive Recipe Implementations")
455 case CLEAR_DISPLAY_FROM: {
456 CHECK_SCREEN;
457
458 int row = ingredients.at(0).at(0);
459 int column = ingredients.at(1).at(0);
460 int left = ingredients.at(2).at(0);
461 int right = ingredients.at(3).at(0);
462 int height=tb_height();
463 for (; row < height; ++row, column=left) {
464 ¦ tb_set_cursor(column, row);
465 ¦ for (; column <= right; ++column)
466 ¦ ¦ tb_print(' ', TB_WHITE, TB_BLACK);
467 }
468 tb_set_cursor(Display_column, Display_row);
469 break;
470 }