added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / dsys / cmd.cpp
1 /*
2 This file is part of 3dengfx demosystem.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /* demosystem script controlled commands
22  *
23  * Author: John Tsiombikas 2005
24  */
25
26 #include <cassert>
27 #include "cmd.hpp"
28 #include "script.h"
29 #include "dsys.hpp"
30 #include "part.hpp"
31 #include "fx.hpp"
32 #include "common/err_msg.h"
33
34 using namespace dsys;
35 using namespace cmd;
36
37 // command handler prototypes
38 static bool start_part(const char *pname, const char **args);
39 static bool end_part(const char *pname, const char **args);
40 static bool rename_part(const char *pname, const char **args);
41 static bool set_render_target(const char *pname, const char **args);
42 static bool set_clear(const char *pname, const char **args);
43 static bool end(const char *unused, const char **args);
44 static bool effect(const char *fxname, const char **args);
45
46 static bool (*ftbl[64])(const char*, const char**);
47
48 void cmd::register_commands() {
49         ftbl[CMD_START_PART] = start_part;
50         ftbl[CMD_END_PART] = end_part;
51         ftbl[CMD_RENAME_PART] = rename_part;
52         ftbl[CMD_SET_RTARGET] = set_render_target;
53         ftbl[CMD_SET_CLEAR] = set_clear;
54         ftbl[CMD_END] = end;
55         ftbl[CMD_FX] = effect;
56 }
57
58 bool cmd::command(CommandType cmd_id, const char *pname, const char **args) {
59         assert(ftbl[cmd_id]);
60
61         return ftbl[cmd_id](pname, args);
62 }
63
64 static bool start_part(const char *pname, const char **args) {
65         Part *part = get_part(pname);
66         if(part) {
67                 info("start_part(%s)", pname);
68                 start_part(part);
69                 return true;
70         }
71         return false;
72 }
73
74 static bool end_part(const char *pname, const char **args) {
75         Part *part = get_part(pname);
76         if(part) {
77                 info("end_part(%s)", pname);
78                 stop_part(part);
79                 return true;
80         }
81         return false;
82 }
83
84 static bool rename_part(const char *pname, const char **args) {
85         Part *part = get_part(pname);
86         if(part && args[0]) {
87                 info("rename_part(%s, %s)", pname, args[0]);
88                 remove_part(part);
89                 part->set_name(args[0]);
90                 add_part(part);
91                 return true;
92         }
93         return false;
94 }
95
96 static bool set_render_target(const char *pname, const char **args) {
97         Part *part = get_part(pname);
98         if(part && args[0]) {
99                 int tnum;
100
101                 // check for valid render target specifier (fb, t0, t1, t2, t3)
102                 if(!strcmp(args[0], "fb")) {
103                         tnum = (int)RT_FB;
104                 } else {
105                         if(args[0][0] != 't' || !isdigit(args[0][1]) || args[0][2] ||
106                                 (tnum = atoi(args[0]+1)) < 0 || tnum > 3) {
107                                 return false;
108                         }
109                 }
110
111                 info("set_rtarg(%s, %s)", pname, args[0]);
112                 part->set_target((RenderTarget)tnum);
113                 return true;
114         }
115         return false;
116 }
117
118 static bool set_clear(const char *pname, const char **args) {
119         Part *part = get_part(pname);
120         if(part && args[0]) {
121                 bool enable;
122                 if(!strcmp(args[0], "true")) {
123                         enable = true;
124                 } else if(!strcmp(args[0], "false")) {
125                         enable = false;
126                 } else {
127                         return false;
128                 }
129
130                 info("set_clear(%s, %s)", pname, enable ? "true" : "false");
131                 part->set_clear(enable);
132                 return true;
133         }
134         return false;
135 }
136
137 static bool end(const char *unused, const char **args) {
138         if(unused && *unused) return false;
139
140         info("end");
141         end_demo();
142         return true;
143 }
144
145 static bool effect(const char *fxname, const char **args) {
146         ImageFx *fx;
147         
148         if(!strcmp(fxname, "neg")) {
149                 fx = new FxNegative;
150         } else if(!strcmp(fxname, "flash")) {
151                 fx = new FxFlash;
152         } else if(!strcmp(fxname, "overlay")) {
153                 fx = new FxOverlay;
154         } else if(!strcmp(fxname, "fade")) {
155                 fx = new FxFade;
156         } else {
157                 error("unknown effect: %s, ignoring", fxname);
158                 return false;
159         }
160
161         if(!fx->parse_script_args(args)) {
162                 error("fx(%s): invalid syntax", fxname);
163                 delete fx;
164                 return false;
165         }
166
167         info("fx(%s)", fxname);
168         add_image_fx(fx);
169
170         return true;
171 }