erebus
diff src/main.cc @ 37:db8a90307386
implemented console and rudimentary commandline parser
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 09 Jun 2014 16:01:00 +0300 |
parents | d15ee526daa6 |
children | fd45cf0fc912 |
line diff
1.1 --- a/src/main.cc Mon Jun 09 07:28:03 2014 +0300 1.2 +++ b/src/main.cc Mon Jun 09 16:01:00 2014 +0300 1.3 @@ -9,6 +9,7 @@ 1.4 #include <drawtext.h> 1.5 #include "opengl.h" 1.6 #include "erebus.h" 1.7 +#include "console.h" 1.8 1.9 using namespace std::chrono; 1.10 1.11 @@ -25,6 +26,7 @@ 1.12 static void reshape(int x, int y); 1.13 static void keyb(unsigned char key, int x, int y); 1.14 static void keyb_up(unsigned char key, int x, int y); 1.15 +static void skeyb(int key, int x, int y); 1.16 static void mouse(int bn, int st, int x, int y); 1.17 static void motion(int x, int y); 1.18 static void sball_button(int bn, int st); 1.19 @@ -32,6 +34,7 @@ 1.20 static int next_pow2(int x); 1.21 static void sighandler(int s); 1.22 static bool parse_args(int argc, char **argv); 1.23 +static void con_parse(const char *line); 1.24 1.25 static int win_width, win_height, width, height, rtex_width, rtex_height; 1.26 static unsigned int rtex; 1.27 @@ -48,7 +51,9 @@ 1.28 1.29 static std::vector<char*> sfiles; 1.30 1.31 +#define FONTSZ 22 1.32 static dtx_font *font; 1.33 +static Console con; 1.34 1.35 int main(int argc, char **argv) 1.36 { 1.37 @@ -66,6 +71,7 @@ 1.38 glutReshapeFunc(reshape); 1.39 glutKeyboardFunc(keyb); 1.40 glutKeyboardUpFunc(keyb_up); 1.41 + glutSpecialFunc(skeyb); 1.42 glutMouseFunc(mouse); 1.43 glutMotionFunc(motion); 1.44 glutSpaceballButtonFunc(sball_button); 1.45 @@ -89,11 +95,20 @@ 1.46 width = glutGet(GLUT_WINDOW_WIDTH) / opt_imgscale; 1.47 height = glutGet(GLUT_WINDOW_HEIGHT) / opt_imgscale; 1.48 1.49 - //if(!(font = dtx_open_font("/usr/share/fonts/opentype/linux-libertine/LinLibertine_R.otf", 22))) { 1.50 + //if(!(font = dtx_open_font("/usr/share/fonts/opentype/linux-libertine/LinLibertine_R.otf", FONTSZ))) { 1.51 if(!(font = dtx_open_font_glyphmap("data/serif.glyphmap"))) { 1.52 fprintf(stderr, "warning: failed to load font!\n"); 1.53 } 1.54 1.55 + //dtx_font *confont = dtx_open_font("/usr/share/fonts/truetype/droid/DroidSansMono.ttf", 14); 1.56 + dtx_font *confont = dtx_open_font_glyphmap("data/mono.glyphmap"); 1.57 + if(confont) { 1.58 + con.set_font(confont, 14); 1.59 + } else { 1.60 + con.set_font(font, FONTSZ); 1.61 + } 1.62 + con.set_command_func(con_parse); 1.63 + 1.64 if(!(erb = erb_init())) { 1.65 return false; 1.66 } 1.67 @@ -256,6 +271,10 @@ 1.68 glTexCoord2f(0, 0); glVertex2f(-1, 1); 1.69 glEnd(); 1.70 1.71 + // draw the console 1.72 + con.update(); 1.73 + con.draw(); 1.74 + 1.75 // draw progress information etc... 1.76 if(show_status) { 1.77 display_statusbar(status); 1.78 @@ -268,6 +287,7 @@ 1.79 static void display_statusbar(const erb_render_status &status) 1.80 { 1.81 if(!font) return; 1.82 + dtx_use_font(font, FONTSZ); 1.83 1.84 bool show_progress = opt_samples > 0; 1.85 1.86 @@ -368,22 +388,41 @@ 1.87 { 1.88 switch(key) { 1.89 case 27: 1.90 - end_frame(); 1.91 - exit(0); 1.92 + if(con.is_visible()) { 1.93 + con.hide(); 1.94 + glutPostRedisplay(); 1.95 + } else { 1.96 + end_frame(); 1.97 + exit(0); 1.98 + } 1.99 + break; 1.100 1.101 case ' ': 1.102 - begin_frame(0); 1.103 - break; 1.104 - 1.105 - case '\b': 1.106 - printf("saving image.\n"); 1.107 - save_image(); 1.108 + if(!con.is_visible()) { 1.109 + begin_frame(0); 1.110 + } else { 1.111 + con.input_key(' '); 1.112 + glutPostRedisplay(); 1.113 + } 1.114 break; 1.115 1.116 case '`': 1.117 + con.set_visible(!con.is_visible()); 1.118 + glutPostRedisplay(); 1.119 + break; 1.120 + 1.121 + case '~': 1.122 show_status = !show_status; 1.123 glutPostRedisplay(); 1.124 break; 1.125 + 1.126 + default: 1.127 + // otherwise if the console is visible, let them through 1.128 + if(con.is_visible()) { 1.129 + con.input_key(key); 1.130 + glutPostRedisplay(); 1.131 + return; // don't pass anything to the erb input handler 1.132 + } 1.133 } 1.134 1.135 if(erb_input_keyboard(erb, key, true)) { 1.136 @@ -398,6 +437,55 @@ 1.137 } 1.138 } 1.139 1.140 +static void skeyb(int key, int x, int y) 1.141 +{ 1.142 + if(key == GLUT_KEY_F12) { 1.143 + printf("saving image...\n"); 1.144 + save_image(); 1.145 + return; 1.146 + } 1.147 + 1.148 + if(con.is_visible()) { 1.149 + switch(key) { 1.150 + case GLUT_KEY_F8: 1.151 + con.debug(); 1.152 + return; 1.153 + 1.154 + case GLUT_KEY_LEFT: 1.155 + con.input_key(Console::KEY_LEFT); 1.156 + break; 1.157 + case GLUT_KEY_RIGHT: 1.158 + con.input_key(Console::KEY_RIGHT); 1.159 + break; 1.160 + case GLUT_KEY_UP: 1.161 + con.input_key(Console::KEY_UP); 1.162 + break; 1.163 + case GLUT_KEY_DOWN: 1.164 + con.input_key(Console::KEY_DOWN); 1.165 + break; 1.166 + case GLUT_KEY_HOME: 1.167 + con.input_key(Console::KEY_HOME); 1.168 + break; 1.169 + case GLUT_KEY_END: 1.170 + con.input_key(Console::KEY_END); 1.171 + break; 1.172 + case GLUT_KEY_INSERT: 1.173 + con.input_key(Console::KEY_INS); 1.174 + break; 1.175 + case GLUT_KEY_PAGE_UP: 1.176 + con.input_key(Console::KEY_PGUP); 1.177 + break; 1.178 + case GLUT_KEY_PAGE_DOWN: 1.179 + con.input_key(Console::KEY_PGDOWN); 1.180 + break; 1.181 + 1.182 + default: 1.183 + return; 1.184 + } 1.185 + glutPostRedisplay(); 1.186 + } 1.187 +} 1.188 + 1.189 static void mouse(int bn, int st, int x, int y) 1.190 { 1.191 if(erb_input_mouse_button(erb, bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y)) { 1.192 @@ -483,3 +571,14 @@ 1.193 1.194 return true; 1.195 } 1.196 + 1.197 +static void con_parse(const char *line) 1.198 +{ 1.199 + printf("got line: %s\n", line); 1.200 + 1.201 + if(erb_proc_cmd(erb, line) == -1) { 1.202 + con.puts("invalid command\n"); 1.203 + } else { 1.204 + begin_frame(0); 1.205 + } 1.206 +}