source: trunk/orbital/orbital.cpp @ 1308

Last change on this file since 1308 was 1308, checked in by touky, 8 years ago

Added GetButtonState() in Input class.
Added Escape button in to quit Orbital.
Added Camera control with some little damping sweetness in Camera logic in main Tick.
First Commit \o/

File size: 6.3 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 USE_SDL && defined __APPLE__
16#   include <SDL_main.h>
17#endif
18
19#include "core.h"
20#include "loldebug.h"
21
22using namespace std;
23using namespace lol;
24
25#include "orbital.h"
26#include "mesh.h"
27#include "particlesystem.h"
28#include "camera.h"
29
30Orbital::Orbital()
31{
32    for (int j = 0; j < 20; j++)
33    {
34        for (int i = 0; i < 20; i++)
35        {
36            m.SendCommand(((i + j) % 2) ? "sc.3,.7,.9,1,scb.3,.7,.9,1"
37                                        : "sc.3,.3,.7,1,scb.3,.3,.7,1");
38            m.SendCommand("ac4,2,44,40,0,0,ty-1,ad4,40,0,ry45");
39            m.Scale(vec3(std::sqrt(0.5f)));
40            m.Translate(vec3(i * 44 - 440, -50, j * 44 - 440));
41            m.Flush();
42        }
43    }
44
45    /* Yellow sphere */
46    m.SendCommand("sc1,1,0,1,asph10,30,20,24");
47    m.SendCommand("t0,0,-60,fl");
48
49    /* Pink box */
50    m.SendCommand("sc1,0,1,1,afcb10,10,10,1,rx45,rz45");
51    m.SendCommand("t-20,-20,0,fl");
52
53    /* Large meteor */
54    m.SendCommand("sc0,0,0.3,1,afcb30,30,30,5,ry45,rx45,afcb30,30,30,5");
55    m.SendCommand("t40,40,0,fl");
56
57    /* Grey/red bonus */
58    m.SendCommand("sc0.6,0.7,0.8,1,afcb7,4,7,0.6,sc1,0,0.3,1,afcb4,7,4,0.6");
59    m.SendCommand("t-40,20,-30,fl");
60
61    /* Orange/white alien */
62    m.SendCommand("sc1,0.7,0,1,afcb12,3,10,0.4,tz3,sc1,1,1,1,afcb2,10,10,0.4");
63    m.SendCommand("rx20,ry30,t0,40,-20,fl");
64
65    /* Tank body + tank head */
66    m.SendCommand("sc0.1,0.1,0,1,ab6,6,15,ty-2,sc1,1,1,1,afcb4,5,16,0.4,tx4,tx60,mx,tx120,fl,sc0.2,0.7,0,1,afcb8,7,10,0.4,tz-4,tx60,fl");
67    m.SendCommand("sc0.2,0.7,0,1,afcb3,6,10,0.4,tx-8,afcb3,6,10,0.4,tx4,ty10,tx60,fl,sc1,1,1,1,afcb3,6,10,0.4,rx-30,ty10,tx60,fl");
68
69    /* Orange player ship */
70    m.SendCommand("sc1,.5,0,1");
71    m.SendCommand("afcb5,1,3,0.6,fl,sc1,1,1,1,afcb1,5,3,0.6,tz-1,irb");
72    m.SendCommand("sc1,.5,0,1");
73    m.SendCommand("afcb3,6,7,.4,t0,0,7,sc1,1,1,1,afcb3,4,4,.4,t4,0,-4,mx,fl");
74    m.SendCommand("sc1,.5,0,1");
75    m.SendCommand("afcb3,6,5,.4,sc1,1,1,1,afcb2,3,9,.4,fl");
76    m.SendCommand("scb1,1,1,1,ac4,15,.2,.6,1,1,tz-2,ac4,15,.2,.6,1,1,rx90,t0,-2,-7,fl");
77
78    /* Orange fire */
79    m.SendCommand("sc1,1,0,1,scb1,0,0,0,at4,1,s1.5,1,4,tz-13,ad6,5.8,1");
80    m.SendCommand("t-40,-40,0,fl");
81
82    /* Lasers */
83    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");
84
85    /* Thrusters */
86    m.SendCommand("sc0,1,1,scb0,0,0,1,ac5,15,0,1.5,0,1,ac7,35,1.1,4,0,1,rx90,t-3,0,27,mx");
87    m.SendCommand("fl");
88
89    m_particlesystem = new ParticleSystem();
90    m_camera = new Camera(vec3(0, 200, 300),
91                          vec3(0, 0, 0),
92                          vec3(0, 1, 0));
93
94        m_auto_cam_timer = 0.0f;
95
96        m_horizontal_angle = 0;
97        m_vertical_angle = 0;
98        m_roll_angle = 0;
99
100    m_horizontal_angle_speed = 0.0f;
101    m_vertical_angle_speed = 0.0f;
102    m_roll_angle_speed = 0.0f;
103
104    m_ready = false;
105
106    Ticker::Ref(m_particlesystem);
107    Ticker::Ref(m_camera);
108}
109
110void Orbital::TickGame(float deltams)
111{
112    WorldEntity::TickGame(deltams);
113
114        //TEMP : Convert DT to seconds
115        deltams /= 1000.0f;
116
117        if (Input::GetButtonState(27 /*SDLK_ESCAPE*/))
118        Ticker::Shutdown();
119
120        if (m_auto_cam_timer > 0.0f)
121                m_auto_cam_timer -= deltams;
122
123        //Doing the query with actual values, cause I want to stay SDL-free for now.
124        int HMovement = Input::GetButtonState(276 /*SDLK_LEFT*/) - Input::GetButtonState(275 /*SDLK_RIGHT*/);
125        int VMovement = Input::GetButtonState(273 /*SDLK_UP*/) - Input::GetButtonState(274 /*SDLK_DOWN*/);
126        int RMovement = Input::GetButtonState(256 /*SDLK_KP0*/) - Input::GetButtonState(305 /*SDLK_RCTRL*/);
127
128    float new_horizontal_angle_speed = 0.0f;
129    float new_vertical_angle_speed = 0.0f;
130    float new_roll_angle_speed = 0.0f;
131
132        if (HMovement != 0 || VMovement != 0 || RMovement != 0)
133        {
134                new_horizontal_angle_speed = ((float)HMovement) * 50.0f;
135                new_vertical_angle_speed = ((float)VMovement) * 50.0f;
136                new_roll_angle_speed = ((float)RMovement) * 50.0f;
137                m_auto_cam_timer = 2.0f;
138        }
139        else if (m_auto_cam_timer <= 0.0f)
140        {
141                new_horizontal_angle_speed = 40.0f;
142                new_vertical_angle_speed = max<float>(min<float>(-m_vertical_angle, 20.0f), -20.0f);
143                new_roll_angle_speed = max<float>(min<float>(-m_roll_angle, 40.0f), -40.0f);
144        }
145
146        m_horizontal_angle_speed += (new_horizontal_angle_speed - m_horizontal_angle_speed) * (deltams / (deltams + 0.3f));
147        m_vertical_angle_speed += (new_vertical_angle_speed - m_vertical_angle_speed) * (deltams / (deltams + 0.3f));
148        m_roll_angle_speed += (new_roll_angle_speed - m_roll_angle_speed) * (deltams / (deltams + 0.3f));
149
150        m_horizontal_angle += m_horizontal_angle_speed * deltams;
151    m_vertical_angle += new_vertical_angle_speed * deltams;
152    m_roll_angle += new_roll_angle_speed * deltams;
153
154        if (m_horizontal_angle > 180.0f)
155                m_horizontal_angle -= 360.0f;
156        else if (m_horizontal_angle < -180.0f)
157                m_horizontal_angle += 360.0f;
158        if (m_vertical_angle > 180.0f)
159                m_vertical_angle -= 360.0f;
160        else if (m_vertical_angle < -180.0f)
161                m_vertical_angle += 360.0f;
162        if (m_roll_angle > 180.0f)
163                m_roll_angle -= 360.0f;
164        else if (m_roll_angle < -180.0f)
165                m_roll_angle += 360.0f;
166
167    mat4 animH = mat4::rotate(m_horizontal_angle, vec3(0, 1, 0));
168    mat4 animV = mat4::rotate(m_vertical_angle, vec3(1, 0, 0));
169    mat4 animR = mat4::rotate(m_roll_angle, vec3(0, 0, 1));
170    mat4 model = mat4::translate(vec3(0));
171
172    m_modelview = m_camera->GetViewMatrix() * model * animV * animR * animH;
173    m_proj = m_camera->GetProjMatrix();
174    m_normalmat = transpose(inverse(mat3(m_modelview)));
175}
176
177void Orbital::TickDraw(float deltams)
178{
179    WorldEntity::TickDraw(deltams);
180
181    if (!m_ready)
182    {
183        m.SendCommand("irb");
184
185        /* FIXME: this object never cleans up */
186        m_ready = true;
187    }
188
189    Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
190
191    m.Render(m_modelview, m_proj, m_normalmat);
192}
193
194Orbital::~Orbital()
195{
196    Ticker::Unref(m_particlesystem);
197    Ticker::Unref(m_camera);
198}
199
200int main(int argc, char **argv)
201{
202    Application app("Orbital", ivec2(800, 600), 60.0f);
203
204#if defined _MSC_VER && !defined _XBOX
205    _chdir("..");
206#elif defined _WIN32 && !defined _XBOX
207    _chdir("../..");
208#endif
209
210    new DebugFps(5, 5);
211    new Orbital();
212    app.ShowPointer(false);
213
214    app.Run();
215
216    return EXIT_SUCCESS;
217}
218
Note: See TracBrowser for help on using the repository browser.