dungeon_crawler

view prototype/src/cmdcon.cc @ 25:527fede30057

- fixed sphere rendering (PointLight::draw) - added config argument options - fixed macosx compilation
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 02:16:08 +0300
parents e122ba214ee1
children 21999ef6636b
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 if(!(font = dtx_open_font(path, 12))) {
29 fprintf(stderr, "failed to open font file: %s\n", path);
30 return false;
31 }
32 dtx_use_font(font, 12);
33 return true;
34 }
36 void cleanup_cmdcon()
37 {
38 if(font) {
39 dtx_close_font(font);
40 }
41 }
43 void cmdcon_keypress(int key)
44 {
45 if(isprint(key)) {
46 cmdline.push_back(key);
47 return;
48 }
50 switch(key) {
51 case '\n':
52 case '\r':
53 runcmd();
54 cmdline.clear();
55 break;
57 case '\t':
58 complete();
59 break;
61 case '\b':
62 if(!cmdline.empty()) {
63 cmdline.pop_back(); // <-- C++11
64 }
65 break;
67 default:
68 break;
69 }
70 }
72 void draw_cmdcon()
73 {
74 glMatrixMode(GL_PROJECTION);
75 glPushMatrix();
76 glLoadIdentity();
77 glOrtho(0, cfg.width, -cfg.height, 0, -1, 1);
78 glMatrixMode(GL_MODELVIEW);
79 glPushMatrix();
80 glLoadIdentity();
82 glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT);
83 glEnable(GL_BLEND);
84 glDisable(GL_DEPTH_TEST);
85 glDisable(GL_LIGHTING);
86 glUseProgram(0);
87 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
89 glBegin(GL_QUADS);
90 glColor4f(0.4, 0.4, 0.4, 0.5);
91 glVertex2f(-10, -10);
92 glVertex2f(10, -10);
93 glVertex2f(10, 10);
94 glVertex2f(-10, 10);
95 glEnd();
97 glTranslatef(cfg.width / 2, cfg.height / 2, 0);
99 glColor4f(0.2, 0.9, 0.3, 1.0);
100 dtx_string(cmdline.c_str());
101 dtx_flush();
103 glPopAttrib();
105 glMatrixMode(GL_PROJECTION);
106 glPopMatrix();
107 glMatrixMode(GL_MODELVIEW);
108 glPopMatrix();
109 }
111 static bool cmd_toggle_wire(std::stringstream &strin)
112 {
113 static bool wire;
114 wire = !wire;
115 glPolygonMode(GL_FRONT_AND_BACK, wire ? GL_LINE : GL_FILL);
116 return true;
117 }
119 static bool cmd_quit(std::stringstream &strin)
120 {
121 exit(0);
122 }
124 static struct {
125 std::string name;
126 bool (*func)(std::stringstream &strin);
127 } cmd[] = {
128 {"wire", cmd_toggle_wire},
129 {"quit", cmd_quit},
130 {"exit", cmd_quit}
131 };
132 #define NUM_CMD (int)(sizeof cmd / sizeof *cmd)
134 static void runcmd()
135 {
136 std::stringstream strin(cmdline);
138 std::string token;
139 strin >> token;
141 for(int i=0; i<NUM_CMD; i++) {
142 if(token == cmd[i].name) {
143 cmd[i].func(strin);
144 return;
145 }
146 }
148 std::cerr << "unrecognized command: " << token << std::endl;
149 }
151 static void complete()
152 {
153 }