1 /* --------------------------------------------------------------------
2 EXTREME TUXRACER
3 
4 Copyright (C) 1999-2001 Jasmin F. Patry (Tuxracer)
5 Copyright (C) 2010 Extreme Tux Racer Team
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 ---------------------------------------------------------------------*/
17 
18 #ifdef HAVE_CONFIG_H
19 #include <etr_config.h>
20 #endif
21 
22 #include "tools.h"
23 #include "tux.h"
24 #include "ogl.h"
25 #include "font.h"
26 #include "textures.h"
27 #include "keyframe.h"
28 #include "tool_frame.h"
29 #include "tool_char.h"
30 #include "env.h"
31 #include "winsys.h"
32 #include <GL/glu.h>
33 
34 CGluCamera GluCamera;
35 
CCamera()36 CCamera::CCamera() {
37 	xview = 0;
38 	yview = 0;
39 	zview = 4;
40 	vhead = 0;
41 	vpitch = 0;
42 
43 	fore = false;
44 	back = false;
45 	left = false;
46 	right = false;
47 	up = false;
48 	down = false;
49 	headleft = false;
50 	headright = false;
51 	pitchup = false;
52 	pitchdown = false;
53 }
54 
XMove(GLfloat step)55 void CCamera::XMove(GLfloat step) {
56 	zview += (float)std::sin(-vhead * 3.14 / 180) * step;
57 	xview += (float)std::cos(-vhead * 3.14 / 180) * step;
58 }
59 
YMove(GLfloat step)60 void CCamera::YMove(GLfloat step) {
61 	yview += step;
62 }
63 
ZMove(GLfloat step)64 void CCamera::ZMove(GLfloat step) {
65 	xview += (float)std::sin(vhead * 3.14 / 180) * step;
66 	zview += (float)std::cos(vhead * 3.14 / 180) * step;
67 }
68 
RotateHead(GLfloat step)69 void CCamera::RotateHead(GLfloat step) {
70 	vhead += step;
71 }
72 
RotatePitch(GLfloat step)73 void CCamera::RotatePitch(GLfloat step) {
74 	vpitch += step;
75 }
76 
Update(float timestep)77 void CCamera::Update(float timestep) {
78 	if (fore)		ZMove(-2 * timestep);
79 	if (back)		ZMove(2 * timestep);
80 	if (left)		XMove(-1 * timestep);
81 	if (right)		XMove(1 * timestep);
82 	if (up)			YMove(1 * timestep);
83 	if (down)		YMove(-1 * timestep);
84 	if (headleft)	RotateHead(5 * timestep);
85 	if (headright)	RotateHead(-5 * timestep);
86 	if (pitchup)	RotatePitch(-2 * timestep);
87 	if (pitchdown)	RotatePitch(2 * timestep);
88 
89 	glLoadIdentity();
90 	glRotatef(-vpitch, 1.0, 0.0, 0.0);
91 	glRotatef(-vhead, 0.0, 1.0, 0.0);
92 	glTranslatef(-xview, -yview, -zview);
93 }
94 
95 
CGluCamera()96 CGluCamera::CGluCamera() {
97 	angle = 0.0;
98 	distance = 3.0;
99 	turnright = false;
100 	turnleft = false;
101 	nearer = false;
102 	farther = false;
103 }
104 
Update(float timestep)105 void CGluCamera::Update(float timestep) {
106 	if (turnright) angle += timestep * 2000;
107 	if (turnleft) angle -= timestep * 2000;
108 	if (nearer) distance -= timestep * 100;
109 	if (farther) distance += timestep * 100;
110 	double xx = distance * std::sin(angle * M_PI / 180);
111 	double zz = distance * std::sin((90 - angle) * M_PI / 180);
112 	glLoadIdentity();
113 	gluLookAt(xx, 0, zz, 0, 0, 0, 0, 1, 0);
114 }
115 
116 // --------------------------------------------------------------------
117 //				tools
118 // --------------------------------------------------------------------
119 
120 CTools Tools;
121 
122 static bool finalstage = false;
123 static bool charchanged = false;
124 static bool framechanged = false;
125 static std::string char_dir;
126 static std::string char_file;
127 static std::string frame_file;
128 
129 static const TLight toollight = {
130 	{0.45f, 0.53f, 0.75f, 1.f},
131 	{1.f,   0.9f,  1.f,   1.f},
132 	{0.6f,  0.6f,  0.6f,  1.f},
133 	{1.f,   2.f,   2.f,   0.f},
134 	true
135 };
136 static int tool_mode = 0;
137 
DrawQuad(float x,float y,float w,float h,float scrheight,const sf::Color & col,int frame)138 void DrawQuad(float x, float y, float w, float h, float scrheight, const sf::Color& col, int frame) {
139 	glDisable(GL_TEXTURE_2D);
140 	glColor(col);
141 	const GLfloat vtx[] = {
142 		x - frame, scrheight - y - h - frame,
143 		x + w + frame, scrheight - y - h - frame,
144 		x + w + frame, scrheight - y + frame,
145 		x - frame, scrheight - y + frame
146 	};
147 	glEnableClientState(GL_VERTEX_ARRAY);
148 
149 	glVertexPointer(2, GL_FLOAT, 0, vtx);
150 	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
151 
152 	glDisableClientState(GL_VERTEX_ARRAY);
153 	glEnable(GL_TEXTURE_2D);
154 }
155 
DrawChanged()156 void DrawChanged() {
157 	DrawQuad(Winsys.resolution.width - 120, 10, 100, 22, Winsys.resolution.height, colRed, 0);
158 	FT.SetProps("normal", 18, colBlack);
159 	FT.DrawString(Winsys.resolution.width - 110, 8, "changed");
160 }
161 
SetToolLight()162 void SetToolLight() {
163 	toollight.Enable(GL_LIGHT0);
164 	glEnable(GL_LIGHTING);
165 }
166 
QuitTool()167 void QuitTool() {
168 	if (!charchanged && !framechanged) State::manager.RequestQuit();
169 	else finalstage = true;
170 }
171 
SetToolMode(int newmode)172 void SetToolMode(int newmode) {
173 	if (newmode == tool_mode) return;
174 	if (newmode > 2) tool_mode = 0;
175 	else tool_mode = newmode;
176 	switch (tool_mode) {
177 		case 0:
178 			break;
179 		case 1:
180 			break;
181 		case 2:
182 			break;
183 	}
184 }
185 
CharHasChanged()186 bool CharHasChanged() {return charchanged;}
FrameHasChanged()187 bool FrameHasChanged() {return framechanged;}
188 
ToolsFinalStage()189 bool ToolsFinalStage() {
190 	return finalstage;
191 }
192 
SetCharChanged(bool val)193 void SetCharChanged(bool val) {
194 	charchanged = val;
195 }
196 
SetFrameChanged(bool val)197 void SetFrameChanged(bool val) {
198 	framechanged = val;
199 }
200 
SaveToolCharacter()201 void SaveToolCharacter() {
202 	if (!charchanged) return;
203 	TestChar.SaveCharNodes(char_dir, char_file);
204 	charchanged = false;
205 }
206 
ReloadToolCharacter()207 void ReloadToolCharacter() {
208 	TestChar.Load(char_dir, char_file, true);
209 	charchanged = false;
210 }
211 
SaveToolFrame()212 void SaveToolFrame() {
213 	if (!framechanged) return;
214 	TestFrame.SaveTest(char_dir, frame_file);
215 	framechanged = false;
216 }
217 
SetParameter(const std::string & dir,const std::string & file)218 void CTools::SetParameter(const std::string& dir, const std::string& file) {
219 	char_dir = param.char_dir + SEP + dir;
220 	char_file = "shape.lst";
221 	frame_file = file;
222 }
223 
Enter()224 void CTools::Enter() {
225 	if (TestChar.Load(char_dir, char_file, true) == false) {
226 		Message("could not load 'shape.lst'");
227 		Winsys.Terminate();
228 	}
229 	if (TestFrame.Load(char_dir, frame_file) == false) {
230 		Message("could not load 'frame.lst'");
231 		Winsys.Terminate();
232 	}
233 	charchanged = false;
234 	framechanged = false;
235 
236 	InitCharTools();
237 	InitFrameTools();
238 }
239 
Keyb(sf::Keyboard::Key key,bool release,int x,int y)240 void CTools::Keyb(sf::Keyboard::Key key, bool release, int x, int y) {
241 	switch (tool_mode) {
242 		case 0:
243 			CharKeys(key, release, x, y);
244 			break;
245 		case 1:
246 			SingleFrameKeys(key, release, x, y);
247 			break;
248 		case 2:
249 			SequenceKeys(key, release, x, y);
250 			break;
251 	}
252 }
253 
Mouse(int button,int state,int x,int y)254 void CTools::Mouse(int button, int state, int x, int y) {
255 	switch (tool_mode) {
256 		case 0:
257 			CharMouse(button, state, x, y);
258 			break;
259 		case 1:
260 			SingleFrameMouse(button, state, x, y);
261 			break;
262 		case 2:
263 			SequenceMouse(button, state, x, y);
264 			break;
265 	}
266 }
267 
Motion(int x,int y)268 void CTools::Motion(int x, int y) {
269 	switch (tool_mode) {
270 		case 0:
271 			CharMotion(x, y);
272 			break;
273 		case 1:
274 			SingleFrameMotion(x, y);
275 			break;
276 		case 2:
277 			SequenceMotion(x, y);
278 			break;
279 	}
280 }
281 
Loop(float time_step)282 void CTools::Loop(float time_step) {
283 	switch (tool_mode) {
284 		case 0:
285 			RenderChar(time_step);
286 			break;
287 		case 1:
288 			RenderSingleFrame(time_step);
289 			break;
290 		case 2:
291 			RenderSequence(time_step);
292 			break;
293 	}
294 }
295