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