dungeon_crawler

view prototype/src/cmdcon.cc @ 26:21999ef6636b

lalala
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 04:07:51 +0300
parents 527fede30057
children 2fc004802739
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 const char *path = datafile_path(FONT_FILENAME);
23 if(!path) {
24 fprintf(stderr, "failed to locate font file: %s\n", FONT_FILENAME);
25 return false;
26 }
28 printf("loading font: %s\n", path);
29 if(!(font = dtx_open_font(path, 12))) {
30 fprintf(stderr, "failed to open font file: %s\n", path);
31 return false;
32 }
33 dtx_use_font(font, 12);
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 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77 glMatrixMode(GL_PROJECTION);
78 glPushMatrix();
79 glLoadIdentity();
80 //glOrtho(0, cfg.width, cfg.height, 0, -1, 1);
81 glOrtho(-cfg.width / 2.0, cfg.width / 2.0, -cfg.height / 2.0, cfg.height / 2.0, -1, 1);
82 glMatrixMode(GL_MODELVIEW);
83 glPushMatrix();
84 glLoadIdentity();
86 //glTranslatef(100, 100, 0);
88 glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT);
89 glUseProgram(0);
90 glDisable(GL_DEPTH_TEST);
91 glDisable(GL_LIGHTING);
92 glDisable(GL_CULL_FACE);
93 glEnable(GL_BLEND);
94 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
96 glBegin(GL_QUADS);
97 glColor4f(0.4, 0.4, 0.4, 0.5);
98 glVertex2f(-10, -10);
99 glVertex2f(10, -10);
100 glVertex2f(10, 10);
101 glVertex2f(-10, 10);
102 glEnd();
104 glColor4f(0.2, 0.9, 0.3, 1.0);
105 dtx_string(cmdline.c_str());
106 dtx_flush();
108 glPopAttrib();
110 glMatrixMode(GL_PROJECTION);
111 glPopMatrix();
112 glMatrixMode(GL_MODELVIEW);
113 glPopMatrix();
114 }
116 static bool cmd_toggle_wire(std::stringstream &strin)
117 {
118 static bool wire;
119 wire = !wire;
120 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
121 return true;
122 }
124 static bool cmd_quit(std::stringstream &strin)
125 {
126 exit(0);
127 }
129 static struct {
130 std::string name;
131 bool (*func)(std::stringstream &strin);
132 } cmd[] = {
133 {"wire", cmd_toggle_wire},
134 {"quit", cmd_quit},
135 {"exit", cmd_quit}
136 };
137 #define NUM_CMD (int)(sizeof cmd / sizeof *cmd)
139 static void runcmd()
140 {
141 std::stringstream strin(cmdline);
143 std::string token;
144 strin >> token;
146 for(int i=0; i<NUM_CMD; i++) {
147 if(token == cmd[i].name) {
148 cmd[i].func(strin);
149 return;
150 }
151 }
153 std::cerr << "unrecognized command: " << token << std::endl;
154 }
156 static void complete()
157 {
158 }