source: trunk/orbital/orbital.cpp @ 1434

Last change on this file since 1434 was 1434, checked in by sam, 10 years ago

orbital: enhance the language with [] contexts.

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