source: trunk/orbital/orbital.cpp @ 1520

Last change on this file since 1520 was 1520, checked in by sam, 11 years ago

build: fix the VS2010 build and add build/ and binaries/ to the dist
tarball.

File size: 9.4 KB
Line 
1//
2// Orbital
3//
4// Copyright: (c) 2009-2012 Cédric Lecacheur <jordx@free.fr>
5//            (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
6//            (c) 2012 Sam Hocevar <sam@hocevar.net>
7//
8
9#if defined HAVE_CONFIG_H
10#   include "config.h"
11#endif
12
13#if defined _WIN32
14#   include <direct.h>
15#endif
16
17#if defined _XBOX
18#   define _USE_MATH_DEFINES /* for M_PI */
19#   include <xtl.h>
20#   undef near /* Fuck Microsoft */
21#   undef far /* Fuck Microsoft again */
22#elif defined _WIN32
23#   define _USE_MATH_DEFINES /* for M_PI */
24#   define WIN32_LEAN_AND_MEAN
25#   include <windows.h>
26#   undef near /* Fuck Microsoft */
27#   undef far /* Fuck Microsoft again */
28#else
29#   include <cmath>
30#endif
31
32#if USE_SDL && defined __APPLE__
33#   include <SDL_main.h>
34#endif
35
36#include "core.h"
37#include "loldebug.h"
38
39using namespace lol;
40
41#include "orbital.h"
42
43Orbital::Orbital(bool editor)
44  : m_model(1.f),
45    m_stick(0),
46    m_angle(0.f),
47    m_zoom(5.f),
48    m_editor(editor)
49{
50#if 0
51    int max_x = 20;
52    int max_y = 20;
53    for (int j = 0; j < max_x; j++)
54    {
55        for (int i = 0; i < max_y; i++)
56        {
57            m.Compile(((i + j) % 2) ? "sc.0,.1,.2,1,scb.0,.1,.2,1"
58                                        : "sc.0,.0,.1,1,scb.0,.0,.1,1");
59            m.Compile("ac4,2,44,40,0,0,ty-1,ad4,40,0,ry45");
60            m.Scale(vec3(std::sqrt(0.5f)));
61            m.Translate(vec3(   i * 44 - (44 * (max_x * 0.5f)), 0,
62                                j * 44 - (44 * (max_y * 0.5f))));
63            m.Flush();
64        }
65    }
66#endif
67
68#if 0
69    /* Yellow sphere */
70    m.Compile("sc1,1,0,1,asph10,30,20,24");
71    m.Compile("t0,0,60,fl");
72
73    /* Pink box */
74    m.Compile("sc1,0,1,1,afcb10,10,10,1,rx45,rz45");
75    m.Compile("t-20,20,0,fl");
76
77    /* Large meteor */
78    m.Compile("sc0,0,0.3,1,afcb30,30,30,5,ry45,rx45,afcb30,30,30,5");
79    m.Compile("t40,40,0,fl");
80
81    /* Orange/white alien */
82    m.Compile("sc1,0.7,0,1,afcb12,3,10,0.4,tz3,sc1,1,1,1,afcb2,10,10,0.4");
83    m.Compile("t0,40,-20,fl");
84    //m.Compile("rx20,ry30,t0,40,-20,fl");
85
86    /* Orange fire */
87    m.Compile("sc1,1,0,1,scb1,0,0,0,at4,1,s1.5,1,4,tz-13,ad6,5.8,1");
88    m.Compile("t-40,40,0,fl");
89
90    /* Lasers */
91    m.Compile("sc1,1,1,1,scb0,0,0,1,aq8,1,sx0.25,tx-3,sc1,0,0,1,scb0,0,0,1,aq8,1,tx4,sz50,sx0.3,tz-200,mx,as10,12,8,1,1,ty60,fl");
92#endif
93
94    //m_particlesystem = new ParticleSystem();
95    //Ticker::Ref(m_particlesystem);
96
97    m_small_stars = new StarField(100, 1.f, 20.f);
98    Ticker::Ref(m_small_stars);
99    m_large_stars = new StarField(100, 2.f, 10.f);
100    Ticker::Ref(m_large_stars);
101
102    /* Create a camera that matches the settings of XNA Orbital */
103    m_camera = new Camera(vec3(0.f, 600.f, 50.f),
104                          vec3(0.f, 0.f, 50.f),
105                          vec3(0, 0, -1));
106    m_camera->SetRotation(quat::fromeuler_yxz(0.f, -30.f, 0.f));
107    m_camera->SetOrtho(1280.f / 3, 960.f / 3, -1000.f, 1000.f);
108    Ticker::Ref(m_camera);
109
110    if (m_editor)
111    {
112        /* Grey/red bonus */
113        //m.Compile("[sc#9ac afcb7 4 7 0.6 sc#f04 afcb4 7 4 0.6]");
114        #if 0
115        m.Compile("sc#d91"
116                  "[afcb30 3 3 .8]"
117                  "sc#cca"
118                  "[afcb10 10 10 1]"
119                  "[afcb.4 .4 12 .4 tz3 tx8 mx]"
120                  "[afcb.4 .4 12 .4 tz3 tx12 mx]"
121                  "sc#69c9"
122                  "[afcb5 8 8 1 ty3 tz3]");
123         #endif
124         m.Compile("sc#f00 scb#f00 [acg10 5 8 8 2 2 0 0 ch.4]");
125    }
126    else
127    {
128        /* Add tanks */
129        for (int j = 0; j < 2; j++)
130        for (int i = 0; i < 2; i++)
131        {
132            m_tanks << new Tank();
133            m_tanks.Last()->m_position = vec3(i * 80.f, 0, j * 80.f + 120.f);
134            m_tanks.Last()->SetTarget(vec3(i * 160.f - 200.f, 0, j * 160.f - 130.f));
135            Ticker::Ref(m_tanks.Last());
136        }
137
138        //m_snakes.Push(new Snake(3));
139        //Ticker::Ref(m_snakes.Last());
140        //m_snakes.Push(new Snake(8));
141        //Ticker::Ref(m_snakes.Last());
142        m_snakes.Push(new Snake(33));
143        Ticker::Ref(m_snakes.Last());
144
145        /* Add players */
146        for (int i = 0; i < 2; i++)
147        {
148            m_players << new Player(i);
149            Ticker::Ref(m_players.Last());
150        }
151    }
152
153        m_Lolnament = NULL;//new Lolnament();
154        Ticker::Ref(m_Lolnament);
155
156    m_ready = false;
157}
158
159void Orbital::TickGame(float seconds)
160{
161    WorldEntity::TickGame(seconds);
162
163    if (Input::GetButtonState(27 /*SDLK_ESCAPE*/))
164        Ticker::Shutdown();
165
166#if 0
167    if (m_auto_cam_timer > 0.0f)
168        m_auto_cam_timer -= seconds;
169
170    //Doing the query with actual values, cause I want to stay SDL-free for now.
171
172    int HMovement = Input::GetButtonState(275 /*SDLK_RIGHT*/) - Input::GetButtonState(276 /*SDLK_LEFT*/);
173    int VMovement = Input::GetButtonState(274 /*SDLK_DOWN*/) - Input::GetButtonState(273 /*SDLK_UP*/);
174    int RMovement = Input::GetButtonState(280 /*SDLK_PAGEUP*/) - Input::GetButtonState(281 /*SDLK_PAGEDOWN*/);
175
176    vec3 new_angular_velocity = vec3(0.0f);
177
178    if (VMovement != 0 || HMovement != 0 || RMovement != 0)
179    {
180        new_angular_velocity = vec3(HMovement, VMovement, RMovement) * 50.0f;
181        m_auto_cam_timer = 2.0f;
182    }
183    else if (m_auto_cam_timer <= 0.0f)
184    {
185        /* Order is yaw, pitch, roll */
186        new_angular_velocity = clamp(-m_angle, vec3(40.f, -20.f, -40.f),
187                                               vec3(40.f,  20.f,  40.f));
188    }
189
190    m_angular_velocity += (new_angular_velocity - m_angular_velocity)
191                          * (seconds / (seconds + 0.3f));
192    m_angle += m_angular_velocity * seconds;
193
194    /* TODO: implement "vec3 % float" or "fmod(vec3, float)" */
195    for (int n = 0; n < 3; n++)
196    {
197        if (m_angle[n] > 180.f)
198            m_angle[n] -= 360.f;
199        else if (m_angle[n] < -180.f)
200            m_angle[n] += 360.f;
201    }
202
203    /* Yaw around Y, Pitch around X, Roll around Z. Since it's the camera
204     * we want to move, use the inverse transform. */
205    mat4 anim = mat4(1.f / quat::fromeuler_yxz(m_angle));
206#endif
207
208#if 0
209    vec3 new_camera_pos = vec3(0,0,0);
210
211    for (int i = 0; i < m_tanks.Count(); i++)
212        new_camera_pos += m_tanks[i]->m_position;
213    for (int i = 0; i < m_snakes.Count(); i++)
214        new_camera_pos += m_snakes[i]->GetBarycenter();
215    for (int i = 0; i < m_players.Count(); i++)
216        new_camera_pos += m_players[i]->m_position;
217
218    new_camera_pos /= (float)(m_players.Count() + m_tanks.Count() + m_snakes.Count());
219    new_camera_pos += vec3(0.f, 0.f, -50.f);
220
221    new_camera_pos = m_camera->GetTarget() + (new_camera_pos - m_camera->GetTarget()) * (seconds / (seconds + 0.15f));
222    m_camera->SetPosition(new_camera_pos + vec3(0.f, 600.f, 0.f));
223    m_camera->SetTarget(new_camera_pos);
224#endif
225
226    if (m_editor)
227    {
228        float rightleft = 0.f, updown = 0.f, inout = 0.f;
229
230        if (!m_stick)
231            m_stick = Input::TrackStick();
232        if (m_stick && m_stick->GetAxisCount() >= 4)
233        {
234            rightleft += 1.f * m_stick->GetAxis(2);
235            updown += -1.f * m_stick->GetAxis(3);
236            inout += -1.f * m_stick->GetAxis(1);
237        }
238
239        if (rightleft * rightleft + updown * updown > 0.2f)
240        {
241            float angle = lol::atan2(updown, rightleft);
242            angle = (int)(angle / ((float)M_PI / 4.f) + 8.5f) * ((float)M_PI / 4.f);
243            rightleft = lol::cos(angle);
244            updown = lol::sin(angle);
245        }
246        else
247        {
248            rightleft = updown = 0.f;
249        }
250
251        m_model = mat4::fromeuler_zyx(0.f, 5.f * rightleft, 5.f * updown)
252                   * m_model;
253        m_zoom *= 1.f + seconds * 0.8f * inout;
254        if (m_zoom > 10.f) m_zoom = 10.f;
255        else if (m_zoom < 0.1f) m_zoom = 0.1f;
256        m_angle += 50.f * seconds;
257    }
258    else
259    {
260    }
261}
262
263void Orbital::TickDraw(float seconds)
264{
265    WorldEntity::TickDraw(seconds);
266
267    if (!m_ready)
268    {
269        m.MeshConvert();
270
271        /* FIXME: this object never cleans up */
272        m_ready = true;
273    }
274
275    Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
276
277    if (m_editor)
278    {
279        mat4 anim1 = mat4::rotate(m_angle, vec3(1.f, 0.f, 0.f));
280        mat4 anim2 = mat4::rotate(m_angle, vec3(0.f, 1.f, 0.f));
281        mat4 anim3 = mat4::rotate(m_angle, vec3(0.f, 0.f, 1.f));
282
283        m.Render(mat4::scale(vec3(m_zoom))
284                  * mat4::translate(0.f, 10.f, 10.f)
285                  * m_model);
286
287        m.Render(mat4::translate(-150.f, -350.f, 0.f) * anim1);
288        m.Render(mat4::translate(-150.f, -200.f, 0.f) * anim2);
289        m.Render(mat4::translate(-150.f, -50.f, 0.f) * anim3);
290        m.Render(mat4::translate(-150.f, 100.f, 0.f));
291    }
292}
293
294Orbital::~Orbital()
295{
296    for (int i = 0; i < m_tanks.Count(); i++)
297        Ticker::Unref(m_tanks[i]);
298    for (int i = 0; i < m_players.Count(); i++)
299        Ticker::Unref(m_players[i]);
300    for (int i = 0; i < m_snakes.Count(); i++)
301        Ticker::Unref(m_snakes[i]);
302    if (m_stick)
303        Input::UntrackStick(m_stick);
304    //Ticker::Unref(m_particlesystem);
305    Ticker::Unref(m_small_stars);
306    Ticker::Unref(m_large_stars);
307    Ticker::Unref(m_camera);
308    Ticker::Unref(m_Lolnament);
309}
310
311int main(int argc, char **argv)
312{
313    //Application app("Orbital", ivec2(1280, 720), 60.0f);
314    Application app("Orbital", ivec2(800, 600), 60.0f);
315
316#if defined _MSC_VER && !defined _XBOX
317    _chdir("..");
318#elif defined _WIN32 && !defined _XBOX
319    _chdir("../..");
320#endif
321
322    //new DebugFps(5, 5);
323    new Orbital(argc > 1);
324    app.ShowPointer(false);
325
326    app.Run();
327
328    return EXIT_SUCCESS;
329}
330
Note: See TracBrowser for help on using the repository browser.