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