nuclear@24: #include nuclear@24: #include nuclear@24: #include nuclear@24: #include nuclear@24: #include "opengl.h" nuclear@24: #include "cmdcon.h" nuclear@24: #include "drawtext.h" nuclear@24: #include "datapath.h" nuclear@24: #include "cfg.h" nuclear@24: nuclear@24: #define FONT_FILENAME "droid_sans_mono.ttf" nuclear@24: nuclear@24: static void runcmd(); nuclear@24: static void complete(); nuclear@24: nuclear@24: static struct dtx_font *font; nuclear@24: nuclear@24: std::string cmdline; nuclear@24: nuclear@24: bool init_cmdcon() nuclear@24: { nuclear@24: const char *path = datafile_path(FONT_FILENAME); nuclear@24: if(!path) { nuclear@24: fprintf(stderr, "failed to locate font file: %s\n", FONT_FILENAME); nuclear@24: return false; nuclear@24: } nuclear@24: nuclear@26: printf("loading font: %s\n", path); nuclear@29: if(!(font = dtx_open_font(path, 14))) { nuclear@24: fprintf(stderr, "failed to open font file: %s\n", path); nuclear@24: return false; nuclear@24: } nuclear@29: dtx_use_font(font, 14); nuclear@24: return true; nuclear@24: } nuclear@24: nuclear@24: void cleanup_cmdcon() nuclear@24: { nuclear@24: if(font) { nuclear@24: dtx_close_font(font); nuclear@24: } nuclear@24: } nuclear@24: nuclear@24: void cmdcon_keypress(int key) nuclear@24: { nuclear@24: if(isprint(key)) { nuclear@24: cmdline.push_back(key); nuclear@24: return; nuclear@24: } nuclear@24: nuclear@24: switch(key) { nuclear@24: case '\n': nuclear@24: case '\r': nuclear@24: runcmd(); nuclear@24: cmdline.clear(); nuclear@24: break; nuclear@24: nuclear@24: case '\t': nuclear@24: complete(); nuclear@24: break; nuclear@24: nuclear@24: case '\b': nuclear@24: if(!cmdline.empty()) { nuclear@25: cmdline.pop_back(); // <-- C++11 nuclear@24: } nuclear@24: break; nuclear@24: nuclear@24: default: nuclear@24: break; nuclear@24: } nuclear@24: } nuclear@24: nuclear@24: void draw_cmdcon() nuclear@24: { nuclear@24: glMatrixMode(GL_PROJECTION); nuclear@24: glPushMatrix(); nuclear@24: glLoadIdentity(); nuclear@29: glOrtho(0, cfg.width, cfg.height, 0, -1, 1); nuclear@24: glMatrixMode(GL_MODELVIEW); nuclear@24: glPushMatrix(); nuclear@24: glLoadIdentity(); nuclear@24: nuclear@29: glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_POLYGON_BIT); nuclear@29: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); nuclear@26: glUseProgram(0); nuclear@24: glDisable(GL_DEPTH_TEST); nuclear@24: glDisable(GL_LIGHTING); nuclear@26: glDisable(GL_CULL_FACE); nuclear@26: glEnable(GL_BLEND); nuclear@24: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); nuclear@24: nuclear@24: glBegin(GL_QUADS); nuclear@29: glColor4f(0.2, 0.2, 0.2, 0.6); nuclear@29: glVertex2f(0, 0); nuclear@29: glVertex2f(0, dtx_line_height() * 1.5); nuclear@29: glVertex2f(cfg.width, dtx_line_height() * 1.5); nuclear@29: glVertex2f(cfg.width, 0); nuclear@24: glEnd(); nuclear@24: nuclear@29: glTranslatef(0, dtx_line_height(), 0); nuclear@29: glScalef(1, -1, 1); nuclear@29: nuclear@24: glColor4f(0.2, 0.9, 0.3, 1.0); nuclear@24: dtx_string(cmdline.c_str()); nuclear@24: dtx_flush(); nuclear@24: nuclear@24: glPopAttrib(); nuclear@24: nuclear@24: glMatrixMode(GL_PROJECTION); nuclear@24: glPopMatrix(); nuclear@24: glMatrixMode(GL_MODELVIEW); nuclear@24: glPopMatrix(); nuclear@24: } nuclear@24: nuclear@24: static bool cmd_toggle_wire(std::stringstream &strin) nuclear@24: { nuclear@24: static bool wire; nuclear@24: wire = !wire; nuclear@24: glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL); nuclear@24: return true; nuclear@24: } nuclear@24: nuclear@24: static bool cmd_quit(std::stringstream &strin) nuclear@24: { nuclear@24: exit(0); nuclear@24: } nuclear@24: nuclear@24: static struct { nuclear@24: std::string name; nuclear@24: bool (*func)(std::stringstream &strin); nuclear@24: } cmd[] = { nuclear@24: {"wire", cmd_toggle_wire}, nuclear@24: {"quit", cmd_quit}, nuclear@24: {"exit", cmd_quit} nuclear@24: }; nuclear@24: #define NUM_CMD (int)(sizeof cmd / sizeof *cmd) nuclear@24: nuclear@24: static void runcmd() nuclear@24: { nuclear@24: std::stringstream strin(cmdline); nuclear@24: nuclear@24: std::string token; nuclear@24: strin >> token; nuclear@24: nuclear@24: for(int i=0; i