dungeon_crawler

view prototype/src/cmdcon.cc @ 63:7f52d6310317

fixed design issue with datafile_path
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 02 Oct 2012 04:52:59 +0300
parents 2fc004802739
children
line source
1 #include <stdlib.h>
2 #include <iostream>
3 #include <sstream>
4 #include <string>
5 #include "opengl.h"
6 #include "cmdcon.h"
7 #include "drawtext.h"
8 #include "datapath.h"
9 #include "cfg.h"
11 #define FONT_FILENAME "droid_sans_mono.ttf"
13 static void runcmd();
14 static void complete();
16 static struct dtx_font *font;
18 std::string cmdline;
20 bool init_cmdcon()
21 {
22 std::string path = datafile_path(FONT_FILENAME);
23 if(path.empty()) {
24 fprintf(stderr, "failed to locate font file: %s\n", FONT_FILENAME);
25 return false;
26 }
28 printf("loading font: %s\n", path.c_str());
29 if(!(font = dtx_open_font(path.c_str(), 14))) {
30 fprintf(stderr, "failed to open font file: %s\n", path.c_str());
31 return false;
32 }
33 dtx_use_font(font, 14);
34 return true;
35 }
37 void cleanup_cmdcon()
38 {
39 if(font) {
40 dtx_close_font(font);
41 }
42 }
44 void cmdcon_keypress(int key)
45 {
46 if(isprint(key)) {
47 cmdline.push_back(key);
48 return;
49 }
51 switch(key) {
52 case '\n':
53 case '\r':
54 runcmd();
55 cmdline.clear();
56 break;
58 case '\t':
59 complete();
60 break;
62 case '\b':
63 if(!cmdline.empty()) {
64 cmdline.pop_back(); // <-- C++11
65 }
66 break;
68 default:
69 break;
70 }
71 }
73 void draw_cmdcon()
74 {
75 glMatrixMode(GL_PROJECTION);
76 glPushMatrix();
77 glLoadIdentity();
78 glOrtho(0, cfg.width, cfg.height, 0, -1, 1);
79 glMatrixMode(GL_MODELVIEW);
80 glPushMatrix();
81 glLoadIdentity();
83 glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_POLYGON_BIT);
84 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
85 glUseProgram(0);
86 glDisable(GL_DEPTH_TEST);
87 glDisable(GL_LIGHTING);
88 glDisable(GL_CULL_FACE);
89 glEnable(GL_BLEND);
90 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
92 glBegin(GL_QUADS);
93 glColor4f(0.2, 0.2, 0.2, 0.6);
94 glVertex2f(0, 0);
95 glVertex2f(0, dtx_line_height() * 1.5);
96 glVertex2f(cfg.width, dtx_line_height() * 1.5);
97 glVertex2f(cfg.width, 0);
98 glEnd();
100 glTranslatef(0, dtx_line_height(), 0);
101 glScalef(1, -1, 1);
103 glColor4f(0.2, 0.9, 0.3, 1.0);
104 dtx_string(cmdline.c_str());
105 dtx_flush();
107 glPopAttrib();
109 glMatrixMode(GL_PROJECTION);
110 glPopMatrix();
111 glMatrixMode(GL_MODELVIEW);
112 glPopMatrix();
113 }
115 static bool cmd_toggle_wire(std::stringstream &strin)
116 {
117 static bool wire;
118 wire = !wire;
119 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
120 return true;
121 }
123 static bool cmd_quit(std::stringstream &strin)
124 {
125 exit(0);
126 }
128 static struct {
129 std::string name;
130 bool (*func)(std::stringstream &strin);
131 } cmd[] = {
132 {"wire", cmd_toggle_wire},
133 {"quit", cmd_quit},
134 {"exit", cmd_quit}
135 };
136 #define NUM_CMD (int)(sizeof cmd / sizeof *cmd)
138 static void runcmd()
139 {
140 std::stringstream strin(cmdline);
142 std::string token;
143 strin >> token;
145 for(int i=0; i<NUM_CMD; i++) {
146 if(token == cmd[i].name) {
147 cmd[i].func(strin);
148 return;
149 }
150 }
152 std::cerr << "unrecognized command: " << token << std::endl;
153 }
155 static void complete()
156 {
157 }