source: trunk/orbital/orbital.cpp @ 1492

Last change on this file since 1492 was 1492, checked in by touky, 9 years ago

Added Lolnament (Working Title) V0.1a in Orbital, for quick tests purposes.

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