Update
[anarch.git] / main_terminal.c
blobd204e1a0fc33404fb04845b41567794f1474dca1
1 /**
2 @file main_terminal.c
4 WARNING: VERY EXPERIMENTAL
6 This is Linux terminal implementation of the game front end. If you replace
7 the input methods, it will most likely run in other terminals as well. This
8 needs root priviledges (sudo) to work (because we need to read keyboard and
9 mouse inputs)! This frontend is more of an experiment, don't expect it to work
10 perfectly and everywhere.
12 by Miloslav Ciz (drummyfish), 2019
14 Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
15 plus a waiver of all other intellectual property. The goal of this work is
16 be and remain completely in the public domain forever, available for any use
17 whatsoever.
20 #ifndef USE_LINUX_FRAMEBUFFER
21 #define USE_LINUX_FRAMEBUFFER 0
22 #endif
24 // IMPORTANT: You must set these files correctly:
25 #define DEV_KEYBOARD "/dev/input/event3"
26 #define DEV_MOUSE "/dev/input/event1"
27 #define DEV_TTY "/dev/tty3"
28 #define DEV_FRAMBUFFER "/dev/fb0"
30 #include <stdint.h>
31 #include <unistd.h>
32 #include <signal.h>
33 #include <fcntl.h>
34 #include <linux/input.h>
35 #include <stdio.h>
36 #include <sys/time.h>
37 #include <time.h>
39 #include "smallinput.h"
41 #if USE_LINUX_FRAMEBUFFER
42 #include <stdlib.h>
43 #include <linux/fb.h>
44 #include <linux/kd.h>
45 #include <sys/mman.h>
46 #include <sys/ioctl.h>
47 #endif
49 #if USE_LINUX_FRAMEBUFFER
50 #define SFG_SCREEN_RESOLUTION_X 640
51 #define SFG_SCREEN_RESOLUTION_Y 480
52 #else
53 #define SFG_SCREEN_RESOLUTION_X 127
54 #define SFG_SCREEN_RESOLUTION_Y 42
55 #endif
57 #define SFG_DITHERED_SHADOW 1
58 #define SFG_FPS 30
60 #include "game.h"
62 #define SCREENSIZE ((SFG_SCREEN_RESOLUTION_X + 1) * SFG_SCREEN_RESOLUTION_Y + 1)
64 char screen[SCREENSIZE];
65 uint8_t *screenUnsigned = (uint8_t *) screen;
67 const char shades[] = // adjust according to your terminal
69 ' ','.','-',':','\\','h','M','@', // grey
70 '`','.',',',';','/','r','=','n' // non-grey
71 };
73 uint32_t timeStart;
75 uint32_t getTime()
77 struct timeval now;
78 gettimeofday(&now, NULL);
79 return now.tv_sec * 1000 + now.tv_usec / 1000;
82 void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
84 #if USE_LINUX_FRAMEBUFFER
85 screenUnsigned[y * SFG_SCREEN_RESOLUTION_X + x] =
86 colorIndex;
87 #else
88 screen[y * (SFG_SCREEN_RESOLUTION_X + 1) + x] =
89 shades[(colorIndex > 7) * 8 + colorIndex % 8];
90 #endif
93 uint32_t SFG_getTimeMs()
95 return getTime() - timeStart;
98 void SFG_save(uint8_t data[SFG_SAVE_SIZE])
102 uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
104 return 0;
107 void SFG_sleepMs(uint16_t timeMs)
109 usleep(timeMs * 1000);
112 void SFG_getMouseOffset(int16_t *x, int16_t *y)
114 int32_t a,b;
116 input_getMousePos(&a,&b);
117 *x = a;
118 *y = b;
119 input_setMousePos(0,0);
122 void SFG_processEvent(uint8_t event, uint8_t data)
126 int8_t SFG_keyPressed(uint8_t key)
128 switch (key)
130 case SFG_KEY_UP: return input_getKey('w') || input_getKey(SMALLINPUT_ARROW_UP); break;
131 case SFG_KEY_RIGHT: return input_getKey('d') || input_getKey(SMALLINPUT_ARROW_RIGHT); break;
132 case SFG_KEY_DOWN: return input_getKey('s') || input_getKey(SMALLINPUT_ARROW_DOWN); break;
133 case SFG_KEY_LEFT: return input_getKey('a') || input_getKey(SMALLINPUT_ARROW_LEFT); break;
134 case SFG_KEY_A: return input_getKey('j') || input_getKey(SMALLINPUT_RETURN) || input_getKey(SMALLINPUT_MOUSE_L); break;
135 case SFG_KEY_B: return input_getKey('k') || input_getKey(SMALLINPUT_CTRL); break;
136 case SFG_KEY_C: return input_getKey('l'); break;
137 case SFG_KEY_MAP: return input_getKey(SMALLINPUT_TAB); break;
138 case SFG_KEY_JUMP: return input_getKey(' '); break;
139 case SFG_KEY_MENU: return input_getKey(SMALLINPUT_ESCAPE); break;
140 case SFG_KEY_NEXT_WEAPON: return input_getKey('2'); break;
141 case SFG_KEY_PREVIOUS_WEAPON: return input_getKey('1'); break;
142 case SFG_KEY_CYCLE_WEAPON: return input_getKey('f'); break;
143 case SFG_KEY_TOGGLE_FREELOOK: return input_getKey(SMALLINPUT_MOUSE_R); break;
144 default: return 0; break;
148 void SFG_setMusic(uint8_t value)
152 void SFG_playSound(uint8_t soundIndex, uint8_t volume)
156 int running = 1;
158 void handleSignal(int signal)
160 #if !USE_LINUX_FRAMEBUFFER
161 puts("\033[?25h"); // show cursor
162 #endif
163 running = 0;
166 int main()
168 signal(SIGINT,handleSignal);
169 signal(SIGQUIT,handleSignal);
170 signal(SIGTERM,handleSignal);
172 timeStart = getTime();
174 input_init(SMALLINPUT_MODE_NORMAL,0,0);
175 SFG_init();
177 screen[SCREENSIZE - 1] = 0; // string terminator
179 #if USE_LINUX_FRAMEBUFFER
181 #define CHECK(w,err,msg)\
182 if ((w) == (err)) { puts(msg); return 1; }
184 int tty = open(DEV_TTY,O_RDWR);
185 CHECK(tty,-1,"couldn't open TTY device")
187 CHECK(ioctl(tty,KDSETMODE,KD_GRAPHICS),-1,"couldn't set graphic mode")
189 int fb = open(DEV_FRAMBUFFER,O_RDWR);
190 CHECK(fb,-1,"couldn't open framebuffer device")
192 struct fb_fix_screeninfo fixInfo;
193 struct fb_var_screeninfo varInfo;
195 CHECK(ioctl(fb,FBIOGET_FSCREENINFO,&fixInfo),-1,"couldn't get fixInfo")
196 CHECK(ioctl(fb,FBIOGET_VSCREENINFO,&varInfo),-1,"couldn't get varInfo")
198 int bpp = varInfo.bits_per_pixel / 8;
199 uint64_t screenSize = varInfo.xres * varInfo.yres * bpp;
201 char *fbScreen = mmap(0,screenSize,PROT_READ | PROT_WRITE,MAP_SHARED,fb,0);
203 CHECK(fbScreen,MAP_FAILED,"couldn't map framebuffer")
205 int r = varInfo.red.offset / 8,
206 g = varInfo.green.offset / 8,
207 b = varInfo.blue.offset / 8,
208 t = varInfo.transp.offset / 8;
210 #else
211 for (uint16_t i = 1; i <= SFG_SCREEN_RESOLUTION_Y; ++i)
212 screen[i * (SFG_SCREEN_RESOLUTION_X + 1) - 1] = '\n';
214 setvbuf(stdout, NULL, _IOFBF, SCREENSIZE + 1);
216 for (uint8_t i = 0; i < 100; ++i) // clear screen
217 putchar('\n');
219 puts("\033[?25l"); // hide cursor
220 #endif
222 while (running)
224 input_update();
226 #if USE_LINUX_FRAMEBUFFER
228 char *p = fbScreen;
230 int linePad = fixInfo.line_length - SFG_SCREEN_RESOLUTION_X * bpp;
232 int index = 0;
234 for (int y = 0; y < SFG_SCREEN_RESOLUTION_Y; ++y)
236 for (int x = 0; x < SFG_SCREEN_RESOLUTION_X; ++x)
238 // inefficient, should be a precomputed RGB32 palette
239 uint16_t c = paletteRGB565[screenUnsigned[index]];
241 *(p + b) = (c << 3) & 0xf8;
242 *(p + g) = (c >> 3) & 0xfc;
243 *(p + r) = (c >> 8) & 0xf8;
244 p += bpp;
245 index++;
248 p += linePad;
251 #else
252 puts("\033[0;0H"); // move cursor to 0;0
253 puts(screen);
254 fflush(stdout);
255 #endif
257 if (!SFG_mainLoopBody())
258 running = 0;
260 // if (SFG_game.frame > 1000) break; // uncomment for testing, prevents locking
264 input_end();
266 #if USE_LINUX_FRAMEBUFFER
267 munmap(screen,screenSize);
269 ioctl(tty,KDSETMODE,KD_TEXT); // set back to text mode
271 close(fb);
272 close(tty);
273 #else
274 puts("\033[?25h"); // show cursor
275 #endif