graphics updates
[voxelands-alt.git] / src / core / sys_console.c
blob6f50a2928340ad15f7fd76973fa06b0187150d15
1 /************************************************************************
2 * sys_console.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
22 #ifdef WIN32
24 void sys_console_print(char* str, int newline)
28 void sys_console_printf(char* fmt, ...)
32 void sys_console_init()
36 void sys_console_exit()
40 #else
42 /* at present this is just an over-complicated printf
43 * it should become an interective shell for dealing with the server */
45 #include <stdio.h>
46 #include <stdarg.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <sys/ioctl.h>
51 /* add a single line of text to the system console */
52 static void sys_console_addline(char* str)
54 printf("%s\n",str);
57 /* print text to the system console */
58 void sys_console_print(char* str, int newline)
60 char buff[2048];
61 int l;
62 int i;
63 int p;
64 char* b;
65 char* e;
66 int max;
67 int cols = 80;
68 #ifdef TIOCGSIZE
69 struct ttysize ts;
70 ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
71 cols = ts.ts_cols;
72 #else
73 #ifdef TIOCGWINSZ
74 struct winsize ts;
75 ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
76 cols = ts.ws_col;
77 #endif
78 #endif
79 max = cols*4;
81 if (max > 2047) {
82 max = 2047;
83 cols = 511;
86 b = str;
87 while (b && b[0]) {
88 e = utf8_strchr(b,'\n',NULL);
89 if (e) {
90 l = e-b;
91 p = 1;
92 if (l > max) {
93 e = b+max;
94 l = max;
95 p = 0;
97 strncpy(buff,b,l);
98 buff[l] = 0;
99 i = l;
100 l = utf8_strlen(buff);
101 while (l > cols) {
102 utf8_dec(buff,&i);
103 l = utf8_offset(buff,i);
104 buff[i] = 0;
106 if (p)
107 utf8_inc(b,&i);
108 b += i;
109 }else{
110 if (strlen(b) > max) {
111 strncpy(buff,b,max);
112 buff[max] = 0;
113 i = l;
114 l = utf8_strlen(buff);
115 while (l > cols) {
116 utf8_dec(buff,&i);
117 l = utf8_offset(buff,i);
118 buff[i] = 0;
120 b += strlen(buff);
121 }else{
122 strcpy(buff,b);
123 b = NULL;
126 sys_console_addline(buff);
130 /* print formatted text to the system console */
131 void sys_console_printf(char* fmt, ...)
133 char buff[1024];
134 va_list ap;
136 if (!fmt)
137 return;
139 va_start(ap, fmt);
141 if (vsnprintf(buff, 1024, fmt, ap) >= 1024) {
142 va_end(ap);
143 return;
146 va_end(ap);
148 sys_console_print(buff, 0);
151 void sys_console_init()
153 sys_console_print("\n\n",0);
154 sys_console_print("___ ___ __\n",0);
155 sys_console_print("\\ \\ / /______ __ ____ | | ___ ____ ___ ______\n",0);
156 sys_console_print(" \\ \\/ / _ \\ \\/ // __ \\| |/ _ \\ / \\| \\/ ___/\n",0);
157 sys_console_print(" \\ /| |_| | \\ ___/| | _ \\ | \\ | |___ \\\n",0);
158 sys_console_print(" \\ / \\___ >_/\\ > __ >__|_/ \\ >__| /___/____ >\n",0);
159 sys_console_print(" \\/ \\/ \\/ \\/ \\/ \\/ \\/\n",0);
160 sys_console_printf("\nVersion: %s\n",VERSION);
163 void sys_console_exit()
167 #endif