dungeon_crawler

diff prototype/src/cmdcon.cc @ 24:e122ba214ee1

implementing the command console
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Aug 2012 18:03:11 +0300
parents
children 527fede30057
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/prototype/src/cmdcon.cc	Thu Aug 23 18:03:11 2012 +0300
     1.3 @@ -0,0 +1,153 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <iostream>
     1.6 +#include <sstream>
     1.7 +#include <string>
     1.8 +#include "opengl.h"
     1.9 +#include "cmdcon.h"
    1.10 +#include "drawtext.h"
    1.11 +#include "datapath.h"
    1.12 +#include "cfg.h"
    1.13 +
    1.14 +#define FONT_FILENAME	"droid_sans_mono.ttf"
    1.15 +
    1.16 +static void runcmd();
    1.17 +static void complete();
    1.18 +
    1.19 +static struct dtx_font *font;
    1.20 +
    1.21 +std::string cmdline;
    1.22 +
    1.23 +bool init_cmdcon()
    1.24 +{
    1.25 +	const char *path = datafile_path(FONT_FILENAME);
    1.26 +	if(!path) {
    1.27 +		fprintf(stderr, "failed to locate font file: %s\n", FONT_FILENAME);
    1.28 +		return false;
    1.29 +	}
    1.30 +
    1.31 +	if(!(font = dtx_open_font(path, 12))) {
    1.32 +		fprintf(stderr, "failed to open font file: %s\n", path);
    1.33 +		return false;
    1.34 +	}
    1.35 +	dtx_use_font(font, 12);
    1.36 +	return true;
    1.37 +}
    1.38 +
    1.39 +void cleanup_cmdcon()
    1.40 +{
    1.41 +	if(font) {
    1.42 +		dtx_close_font(font);
    1.43 +	}
    1.44 +}
    1.45 +
    1.46 +void cmdcon_keypress(int key)
    1.47 +{
    1.48 +	if(isprint(key)) {
    1.49 +		cmdline.push_back(key);
    1.50 +		return;
    1.51 +	}
    1.52 +
    1.53 +	switch(key) {
    1.54 +	case '\n':
    1.55 +	case '\r':
    1.56 +		runcmd();
    1.57 +		cmdline.clear();
    1.58 +		break;
    1.59 +
    1.60 +	case '\t':
    1.61 +		complete();
    1.62 +		break;
    1.63 +
    1.64 +	case '\b':
    1.65 +		if(!cmdline.empty()) {
    1.66 +			cmdline.erase(cmdline.back());
    1.67 +		}
    1.68 +		break;
    1.69 +
    1.70 +	default:
    1.71 +		break;
    1.72 +	}
    1.73 +}
    1.74 +
    1.75 +void draw_cmdcon()
    1.76 +{
    1.77 +	glMatrixMode(GL_PROJECTION);
    1.78 +	glPushMatrix();
    1.79 +	glLoadIdentity();
    1.80 +	glOrtho(0, cfg.width, -cfg.height, 0, -1, 1);
    1.81 +	glMatrixMode(GL_MODELVIEW);
    1.82 +	glPushMatrix();
    1.83 +	glLoadIdentity();
    1.84 +
    1.85 +	glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT);
    1.86 +	glEnable(GL_BLEND);
    1.87 +	glDisable(GL_DEPTH_TEST);
    1.88 +	glDisable(GL_LIGHTING);
    1.89 +	glUseProgram(0);
    1.90 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    1.91 +
    1.92 +	glBegin(GL_QUADS);
    1.93 +	glColor4f(0.4, 0.4, 0.4, 0.5);
    1.94 +	glVertex2f(-10, -10);
    1.95 +	glVertex2f(10, -10);
    1.96 +	glVertex2f(10, 10);
    1.97 +	glVertex2f(-10, 10);
    1.98 +	glEnd();
    1.99 +
   1.100 +	glTranslatef(cfg.width / 2, cfg.height / 2, 0);
   1.101 +
   1.102 +	glColor4f(0.2, 0.9, 0.3, 1.0);
   1.103 +	dtx_string(cmdline.c_str());
   1.104 +	dtx_flush();
   1.105 +
   1.106 +	glPopAttrib();
   1.107 +
   1.108 +	glMatrixMode(GL_PROJECTION);
   1.109 +	glPopMatrix();
   1.110 +	glMatrixMode(GL_MODELVIEW);
   1.111 +	glPopMatrix();
   1.112 +}
   1.113 +
   1.114 +static bool cmd_toggle_wire(std::stringstream &strin)
   1.115 +{
   1.116 +	static bool wire;
   1.117 +	wire = !wire;
   1.118 +	glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
   1.119 +	return true;
   1.120 +}
   1.121 +
   1.122 +static bool cmd_quit(std::stringstream &strin)
   1.123 +{
   1.124 +	exit(0);
   1.125 +}
   1.126 +
   1.127 +static struct {
   1.128 +	std::string name;
   1.129 +	bool (*func)(std::stringstream &strin);
   1.130 +} cmd[] = {
   1.131 +	{"wire", cmd_toggle_wire},
   1.132 +	{"quit", cmd_quit},
   1.133 +	{"exit", cmd_quit}
   1.134 +};
   1.135 +#define NUM_CMD		(int)(sizeof cmd / sizeof *cmd)
   1.136 +
   1.137 +static void runcmd()
   1.138 +{
   1.139 +	std::stringstream strin(cmdline);
   1.140 +
   1.141 +	std::string token;
   1.142 +	strin >> token;
   1.143 +
   1.144 +	for(int i=0; i<NUM_CMD; i++) {
   1.145 +		if(token == cmd[i].name) {
   1.146 +			cmd[i].func(strin);
   1.147 +			return;
   1.148 +		}
   1.149 +	}
   1.150 +
   1.151 +	std::cerr << "unrecognized command: " << token << std::endl;
   1.152 +}
   1.153 +
   1.154 +static void complete()
   1.155 +{
   1.156 +}