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