source: trunk/orbital/orbital.cpp @ 1363

Last change on this file since 1363 was 1363, checked in by sam, 9 years ago

orbital: add the two parallax starfields.

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