console.cpp
上传用户:qccn516
上传日期:2013-05-02
资源大小:3382k
文件大小:8k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /* Console
  2.  *
  3.  * Copyright (C) 2003-2004, Alexander Zaprjagaev <frustum@frustum.org>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  */
  19. #include "engine.h"
  20. #include "console.h"
  21. Console::Console(const char *name,FILE *file) : Font(name), current_line(0), last_history(0), current_history(0), activity(0), time(1.0), file(file) {
  22. for(int i = 0; i < NUM_LINES; i++) {
  23. lines[i].str = new char[1024];
  24. lines[i].str[0] = '';
  25. }
  26. for(int i = 0; i < HISTORY; i++) {
  27. history[i] = new char[1024];
  28. history[i][0] = '';
  29. }
  30. cmd[0] = '';
  31. cmd_ptr = cmd;
  32. }
  33. Console::~Console() {
  34. for(int i = 0; i < NUM_LINES; i++) delete lines[i].str;
  35. for(int i = 0; i < HISTORY; i++) delete history[i];
  36. }
  37. /*
  38.  */
  39. void Console::printf(float x,float y,const char *str,...) {
  40. char buf[4096];
  41. va_list argptr;
  42. va_start(argptr,str);
  43. vsprintf(buf,str,argptr);
  44. va_end(argptr);
  45. puts(x,y,buf);
  46. }
  47. /*
  48.  */
  49. void Console::printf(const char *str,...) {
  50. char buf[4096];
  51. va_list argptr;
  52. va_start(argptr,str);
  53. vsprintf(buf,str,argptr);
  54. va_end(argptr);
  55. printf(0,1,0,"%s",buf);
  56. }
  57. void Console::printf(float r,float g,float b,const char *str,...) {
  58. char buf[4096];
  59. va_list argptr;
  60. va_start(argptr,str);
  61. vsprintf(buf,str,argptr);
  62. va_end(argptr);
  63. Line *line = &lines[current_line];
  64. line->r = r;
  65. line->g = g;
  66. line->b = b;
  67. char *d = line->str;
  68. while(*d) d++; // end of line
  69. for(char *s = buf; *s; s++) {
  70. if(*s != 'n' && d - line->str < 100) *d++ = *s;
  71. else {
  72. if(*s != 'n') {
  73. char *e = d - 1;
  74. while(e > line->str && !strchr(" t",*e)) e--;
  75. if(e > line->str) {
  76. s -= d - e;
  77. d = e;
  78. }
  79. }
  80. *d = '';
  81. if(++current_line == NUM_LINES) current_line = 0;
  82. line = &lines[current_line];
  83. line->r = r;
  84. line->g = g;
  85. line->b = b;
  86. d = line->str;
  87. *d = '';
  88. }
  89. }
  90. *d = '';
  91. ::printf("%s",buf);
  92. if(file) {
  93. fprintf(file,"%s",buf);
  94. fflush(file);
  95. }
  96. }
  97. /*
  98.  */
  99. void Console::render(float ifps,int num) {
  100. time += ifps;
  101. if(height < step * num + step) num = height / step - 1; // fluent on/off
  102. if(activity && time < 0.1) num = (int)((float)num * time * 10.0);
  103. else if(!activity) {
  104. if(time > 0.1) return;
  105. num = (int)((float)num * (0.1 - time) * 10.0);
  106. }
  107. glDisable(GL_TEXTURE_2D);
  108. glBlendFunc(GL_ZERO,GL_ONE_MINUS_SRC_ALPHA);
  109. glColor4f(0,0,0,0.3);
  110. glBegin(GL_QUADS);
  111. glVertex2f(0,0);
  112. glVertex2f(0,step * num + step);
  113. glVertex2f(width,step * num + step);
  114. glVertex2f(width,0);
  115. glEnd();
  116. glEnable(GL_TEXTURE_2D);
  117. for(int i = 0; i < num; i++) {
  118. if(current_line - num + i < 0) {
  119. Line *line = &lines[NUM_LINES + current_line - num + i];
  120. glColor3f(line->r,line->g,line->b);
  121. puts(step / 4,step * i,line->str);
  122. } else {
  123. Line *line = &lines[current_line - num + i];
  124. glColor3f(line->r,line->g,line->b);
  125. puts(step / 4,step * i,line->str);
  126. }
  127. }
  128. glColor3f(1,1,1);
  129. if(time - (int)time < 0.5) printf(step / 4,step * num,CONSOLE_CMD"%s_",cmd);
  130. else printf(step / 4,step * num,CONSOLE_CMD"%s",cmd);
  131. glColor3f(0,1,0);
  132. if(activity && time > 0.1) printf(width - step * 6,0,"FPS: %0.1f",1.0 / ifps);
  133. glColor3f(1,1,1);
  134. }
  135. /*
  136.  */
  137. void Console::keyPress(int key) {
  138. if(key == '`') {
  139. activity = !activity;
  140. time = 0;
  141. return;
  142. }
  143. if(!activity) return;
  144. if(key == 'n') {
  145. if(cmd_ptr != cmd) {
  146. printf(1,1,1,CONSOLE_CMD"%sn",cmd);
  147. if(!strncmp(cmd,"clear",5)) {
  148. for(int i = 0; i < NUM_LINES; i++) lines[i].str[0] = '';
  149. } else if(!strncmp(cmd,"ls",2)) {
  150. printf("variables: ");
  151. for(int i = 0; i < (int)variables.size(); i++) printf("%s%s",variables[i]->name,i == (int)variables.size() - 1 ? "" : ", ");
  152. printf("ncommands: ");
  153. for(int i = 0; i < (int)commands.size(); i++) printf("%s%s",commands[i]->name,i == (int)commands.size() - 1 ? "" : ", ");
  154. printf("n");
  155. } else if(!strncmp(cmd,"set",3)) {
  156. cmd_ptr = cmd + 3;
  157. while(*cmd_ptr == ' ') cmd_ptr++;
  158. if(!*cmd_ptr) printf("usage: set <variable> <value>n");
  159. else {
  160. char name[1024];
  161. sscanf(cmd_ptr,"%s",name);
  162. Variable *v = NULL;
  163. for(int i = 0; i < (int)variables.size(); i++) {
  164. if(!strncmp(cmd_ptr,variables[i]->name,strlen(variables[i]->name))) {
  165. v = variables[i];
  166. break;
  167. }
  168. }
  169. if(v) {
  170. if(v->type == BOOL) {
  171. char val[128];
  172. if(sscanf(cmd_ptr,"%s %s",name,val) == 2) {
  173. if(!strcmp(val,"on")) *v->b = 1;
  174. else if(!strcmp(val,"off")) *v->b = 0;
  175. else if(!strcmp(val,"1")) *v->b = 1;
  176. else if(!strcmp(val,"0")) *v->b = 0;
  177. }
  178. printf("%s set to %dn",name,*v->b);
  179. } else if(v->type == INT) {
  180. sscanf(cmd_ptr,"%s %d",name,v->i);
  181. printf("%s set to %dn",name,*v->i);
  182. } else if(v->type == FLOAT) {
  183. sscanf(cmd_ptr,"%s %f",name,v->f);
  184. printf("%s set to %gn",name,*v->f);
  185. }
  186. } else printf(1,0,0,"unknown variable "%s"n",name);
  187. }
  188. } else {
  189. Command *c = NULL;
  190. int argc = 1;
  191. static char *argv[3];
  192. static char argv_buf[3][64];
  193. argv[0] = argv_buf[0];
  194. argv[1] = argv_buf[1];
  195. argv[2] = argv_buf[2];
  196. char *s = cmd;
  197. char *d = argv[0];
  198. *d = '';
  199. while(1) {
  200. if(*s != ' ' && *s != 't') *d++ = *s;
  201. else if(d != argv[argc - 1]) {
  202. *d = '';
  203. if(argc < 3) argc++;
  204. d = argv[argc - 1];
  205. }
  206. if(*s == '') break;
  207. s++;
  208. }
  209. for(int i = 0; i < (int)commands.size(); i++) {
  210. if(!strcmp(commands[i]->name,argv[0])) {
  211. c = commands[i];
  212. break;
  213. }
  214. }
  215. if(!c) printf(1,0,0,"unknown command "%s"n",argv[0]);
  216. else c->func(argc,argv,c->data);
  217. }
  218. strcpy(history[current_history++],cmd);
  219. if(current_history == HISTORY) current_history = 0;
  220. last_history = current_history;
  221. cmd[0] = '';
  222. cmd_ptr = cmd;
  223. }
  224. } else if(key == 'b') {
  225. if(cmd_ptr > cmd) {
  226. cmd_ptr--;
  227. *cmd_ptr = '';
  228. }
  229. } else if(key == '1') {
  230. last_history--;
  231. if(last_history < 0) last_history = HISTORY - 1;
  232. if(last_history == current_history || history[last_history][0] == '') {
  233. last_history++;
  234. if(last_history == HISTORY) last_history = 0;
  235. }
  236. strcpy(cmd,history[last_history]);
  237. cmd_ptr = cmd + strlen(cmd);
  238. } else if(key == '2') {
  239. if(last_history == current_history) {
  240. cmd[0] = '';
  241. cmd_ptr = cmd;
  242. } else {
  243. last_history++;
  244. if(last_history == HISTORY) last_history = 0;
  245. strcpy(cmd,history[last_history]);
  246. cmd_ptr = cmd + strlen(cmd);
  247. }
  248. } else {
  249. *cmd_ptr++ = key;
  250. *cmd_ptr = '';
  251. }
  252. }
  253. /*
  254.  */
  255. int Console::getActivity() {
  256. return activity;
  257. }
  258. /*
  259.  */
  260. void Console::addBool(const char *name,int *b) {
  261. Variable *v = new Variable;
  262. v->type = BOOL;
  263. v->b = b;
  264. strcpy(v->name,name);
  265. variables.push_back(v);
  266. }
  267. void Console::addInt(const char *name,int *i) {
  268. Variable *v = new Variable;
  269. v->type = INT;
  270. v->i = i;
  271. strcpy(v->name,name);
  272. variables.push_back(v);
  273. }
  274. void Console::addFloat(const char *name,float *f) {
  275. Variable *v = new Variable;
  276. v->type = FLOAT;
  277. v->f = f;
  278. strcpy(v->name,name);
  279. variables.push_back(v);
  280. }
  281. void Console::addCommand(const char *name,void (*func)(int,char**,void*),void *data) {
  282. Command *c = new Command;
  283. c->func = func;
  284. c->data = data;
  285. strcpy(c->name,name);
  286. commands.push_back(c);
  287. }