README: mention SDL2 backend
[rofl0r-concol.git] / console.c
blob58b7b37acd418864354e200b427dc866a3ef9bf8
1 #include "console.h"
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <unistd.h>
6 //generic algos used by all 3 backends
8 int console_setcolors(struct Console* self, rgb_t bgcolor, rgb_t fgcolor) {
9 return
10 console_setcolor(self, 0, bgcolor) +
11 console_setcolor(self, 1, fgcolor);
14 void console_setautomove(Console* c, int automove) {
15 c->automove = !!automove;
18 void console_unblink(Console* c) {
19 if(c->isblinking)
20 console_blink_cursor(c);
23 mouse_event console_getmouse(Console* c) {
24 return c->mouse;
27 void console_getcursor(struct Console* self, int* x, int* y) {
28 *x = self->cursor.x;
29 *y = self->cursor.y;
32 void console_scrollup(Console* c) {
33 (void) c;
36 void console_linebreak(Console* c) {
37 console_unblink(c);
38 c->cursor.x = 0;
39 c->cursor.y++;
40 if(c->cursor.y == c->dim.y) {
41 console_scrollup(c);
42 c->cursor.y--;
46 void console_advance_cursor(Console* c, int steps) {
47 // unblink former position
48 console_unblink(c);
50 if(c->cursor.x + steps >= c->dim.x)
51 console_linebreak(c);
52 else if(c->cursor.x + steps < 0) {
53 if(c->cursor.y) {
54 c->cursor.x = c->dim.x - 1;
55 c->cursor.y--;
57 } else
58 c->cursor.x += steps;
61 void console_fill(Console *c, rect* area, int ch) {
62 int x, y;
63 for(y = area->topleft.y; y <= area->bottomright.y; y++) {
64 for(x = area->topleft.x; x <= area->bottomright.x; x++) {
65 console_goto(c, x, y);
66 console_putchar(c, ch, 0);
69 console_draw(c);
72 static void cursor_move_do(int which, int* dest, int max) {
73 if(which) {
74 if(*dest + which >= 0 && *dest + which < max)
75 *dest += which;
79 static void cursor_move(Console* c, int x, int y) {
80 int which, *dest, max;
81 console_unblink(c);
82 which = x; dest = &c->cursor.x; max = c->dim.x;
83 cursor_move_do(which, dest, max);
84 which = y; dest = &c->cursor.y; max = c->dim.y;
85 cursor_move_do(which, dest, max);
86 console_blink_cursor(c);
89 void console_cursor_up(Console* c) {
90 cursor_move(c, 0, -1);
93 void console_cursor_down(Console* c) {
94 cursor_move(c, 0, 1);
97 void console_cursor_left(Console* c) {
98 cursor_move(c, -1, 0);
101 void console_cursor_right(Console* c) {
102 cursor_move(c, 1, 0);
105 void console_printf(Console* c, const char* fmt, ...) {
106 char dest[512];
107 va_list ap;
108 console_initoutput(c);
109 va_start(ap, fmt);
110 ssize_t x, result = vsnprintf(dest, sizeof(dest), fmt, ap);
111 va_end(ap);
112 for(x = 0; x < result; x++) {
113 console_printchar(c, dest[x], 0);
117 enum ConsoleBackend console_getbackendtype(Console *c) {
118 return c->backendtype;