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@24: if(!(font = dtx_open_font(path, 12))) { nuclear@24: fprintf(stderr, "failed to open font file: %s\n", path); nuclear@24: return false; nuclear@24: } nuclear@24: dtx_use_font(font, 12); 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@24: cmdline.erase(cmdline.back()); 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@24: glOrtho(0, cfg.width, -cfg.height, 0, -1, 1); nuclear@24: glMatrixMode(GL_MODELVIEW); nuclear@24: glPushMatrix(); nuclear@24: glLoadIdentity(); nuclear@24: nuclear@24: glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); nuclear@24: glEnable(GL_BLEND); nuclear@24: glDisable(GL_DEPTH_TEST); nuclear@24: glDisable(GL_LIGHTING); nuclear@24: glUseProgram(0); nuclear@24: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); nuclear@24: nuclear@24: glBegin(GL_QUADS); nuclear@24: glColor4f(0.4, 0.4, 0.4, 0.5); nuclear@24: glVertex2f(-10, -10); nuclear@24: glVertex2f(10, -10); nuclear@24: glVertex2f(10, 10); nuclear@24: glVertex2f(-10, 10); nuclear@24: glEnd(); nuclear@24: nuclear@24: glTranslatef(cfg.width / 2, cfg.height / 2, 0); nuclear@24: 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