From c3a2849f772a318d7fa505c3d08fb7abdc41cbcf Mon Sep 17 00:00:00 2001
From: Rtch90 <ritchie.cunningham@protonmail.com>
Date: Tue, 23 Jan 2018 18:54:35 +0000
Subject: [PATCH] [Add] Spin keys. [Change] Make stars render faster and use
 more realistic star type light colours.

---
 README              |  9 ++++++-
 src/libs.h          |  1 +
 src/main.cpp        |  2 +-
 src/player.cpp      |  2 ++
 src/sector_view.cpp |  8 +++---
 src/star.cpp        | 60 +++++++++++++++++++++++----------------------
 src/star_system.cpp | 13 ++++++++++
 src/star_system.h   |  1 +
 src/world_view.cpp  |  3 +--
 9 files changed, 62 insertions(+), 37 deletions(-)

diff --git a/README b/README
index 3827069..b57b6c0 100644
--- a/README
+++ b/README
@@ -29,6 +29,13 @@ X - Down thruster.
 Rotation:
 ~~~~~~~~~
 
+Q               - Spin Left.
+E               - Spin Right.
+<Left Cursor>   - Yaw Left.
+<Right Cursor>  - Yaw Right.
+<Up Cursor>     - Pitch up.
+<Down Cursor>   - Pitch Down.
+
 You can rotate your craft either with the cursor keys or by holding down the
 right mouse button and moving the mouse. Only mouse rotation is available
 in the external view.
@@ -42,7 +49,7 @@ External View:
 Special:
 ~~~~~~~~
 
-ESC - quit.
+ctrl-q - quit.
 i   - show some ugly debug info.
 
 
diff --git a/src/libs.h b/src/libs.h
index 8af5be3..cb4708b 100644
--- a/src/libs.h
+++ b/src/libs.h
@@ -8,6 +8,7 @@
 #include <ode/ode.h>
 #include <float.h>
 #include <limits>
+#include <time.h>
 
 #ifdef _WIN32
 #include <windows.h>
diff --git a/src/main.cpp b/src/main.cpp
index efe88cb..14163a9 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -169,7 +169,7 @@ void L3D::HandleEvents(void) {
     Gui::HandleSDLEvent(&event);
     switch(event.type) {
     case SDL_KEYDOWN:
-      if(event.key.keysym.sym == SDLK_ESCAPE) L3D::Quit();
+      if(KeyState(SDLK_LCTRL) && (event.key.keysym.sym == SDLK_q)) L3D::Quit();
       if(event.key.keysym.sym == SDLK_i) L3D::showDebugInfo = !L3D::showDebugInfo;
 #ifdef DEBUG
       if(event.key.keysym.sym == SDLK_F12) {
diff --git a/src/player.cpp b/src/player.cpp
index 90b90e2..62b4cd7 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -133,6 +133,8 @@ void Player::PollControls(void) {
       if(L3D::KeyState(SDLK_UP))    angThrust.x += -1;
       if(L3D::KeyState(SDLK_DOWN))  angThrust.x +=  1;
     }
+    if(L3D::KeyState(SDLK_q)) angThrust.z += 1;
+    if(L3D::KeyState(SDLK_e)) angThrust.z -= 1;
     /* Rotation damping. */
     vector3d damping = time_accel*CalcRotDamping();
 
diff --git a/src/sector_view.cpp b/src/sector_view.cpp
index 79dd62f..7a47a17 100644
--- a/src/sector_view.cpp
+++ b/src/sector_view.cpp
@@ -175,10 +175,10 @@ void SectorView::Update(void) {
   if(L3D::KeyState(SDLK_RIGHT))      m_px   += 1*frameTime;
   if(L3D::KeyState(SDLK_UP))         m_py   += 1*frameTime;
   if(L3D::KeyState(SDLK_DOWN))       m_py   -= 1*frameTime;
-  if(L3D::KeyState(SDLK_EQUALS))     m_zoom *= pow(0.5, frameTime);
-  if(L3D::KeyState(SDLK_MINUS))      m_zoom *= pow(2.0, frameTime);
-  if(m_zoomInButton->IsPressed())    m_zoom *= pow(0.5, frameTime);
-  if(m_zoomOutButton->IsPressed())   m_zoom *= pow(2.0, frameTime);
+  if(L3D::KeyState(SDLK_EQUALS))     m_zoom *= pow(0.5f, frameTime);
+  if(L3D::KeyState(SDLK_MINUS))      m_zoom *= pow(2.0f, frameTime);
+  if(m_zoomInButton->IsPressed())    m_zoom *= pow(0.5f, frameTime);
+  if(m_zoomOutButton->IsPressed())   m_zoom *= pow(2.0f, frameTime);
   m_zoom = CLAMP(m_zoom, 0.1, 5.0);
 
   if(L3D::MouseButtonState(3)) {
diff --git a/src/star.cpp b/src/star.cpp
index 13e79d7..9b729b2 100644
--- a/src/star.cpp
+++ b/src/star.cpp
@@ -17,37 +17,11 @@ void Star::SetPosition(vector3d p) {
   pos = p;
 }
 
-static void DrawCorona(double rad, vector3d& pos, const float col[3]) {
-  glPushMatrix();
-  /* Face the camera damnit! */
-  vector3d zaxis = vector3d::Normalize(pos);
-  vector3d xaxis = vector3d::Normalize(vector3d::Cross(zaxis, vector3d(0, 1, 0)));
-  vector3d yaxis = vector3d::Cross(zaxis, xaxis);
-  matrix4x4d rot = matrix4x4d::MakeRotMatrix(xaxis, yaxis, zaxis).InverseOf();
-  glMultMatrixd(&rot[0]);
-
-  glEnable(GL_BLEND);
-  glDisable(GL_CULL_FACE);
-  glBegin(GL_TRIANGLE_FAN);
-    glColor4f(col[0], col[1], col[2], 1);
-    glVertex3f(0, 0, 0);
-    glColor4f(0, 0, 0, 0);
-    for(float ang = 0; ang < 2*M_PI; ang += 0.2) {
-      glVertex3f(rad*sin(ang), rad*cos(ang), 0);
-    }
-    glVertex3f(0, rad, 0);
-  glEnd();
-  glEnable(GL_CULL_FACE);
-  glDisable(GL_BLEND);
-  glPopMatrix();
-}
-
 void Star::Render(const Frame* a_camFrame) {
   glDisable(GL_LIGHTING);
   glDisable(GL_DEPTH_TEST);
   glPushMatrix();
 
-  /* TODO duplicates code from Planet.cpp, not good. */
   double rad = radius;
   vector3d fpos = GetPositionRelTo(a_camFrame);
   double len = fpos.Length();
@@ -60,9 +34,37 @@ void Star::Render(const Frame* a_camFrame) {
   
   glTranslatef(fpos.x, fpos.y, fpos.z);
   
-  glColor3fv(StarSystem::starColors[type]);
-  gluSphere(L3D::gluQuadric, rad, 100, 100);
-  DrawCorona(rad*4, fpos, StarSystem::starColors[type]);
+  {
+    const float* col = StarSystem::starRealColors[type];
+    /* Face the darn camera. */
+    vector3d zaxis = vector3d::Normalize(fpos);
+    vector3d xaxis = vector3d::Normalize(vector3d::Cross(vector3d(0, 1, 0), zaxis));
+    vector3d yaxis = vector3d::Cross(zaxis, xaxis);
+    matrix4x4d rot = matrix4x4d::MakeRotMatrix(xaxis, yaxis, zaxis).InverseOf();
+    glMultMatrixd(&rot[0]);
+
+    glEnable(GL_BLEND);
+    glBegin(GL_TRIANGLE_FAN);
+    glColor4f(col[0], col[1], col[2], 1);
+    glVertex3f(0, 0, 0);
+    glColor4f(0, 0, 0, 0);
+    for(float ang = 0; ang < 2*M_PI; ang += 0.2) {
+      glVertex3f(4*rad*sin(ang), 4*rad*cos(ang), 0);
+    }
+    glVertex3f(0, 4*rad, 0);
+    glEnd();
+    glDisable(GL_BLEND);
+
+    glBegin(GL_TRIANGLE_FAN);
+    glColor4f(col[0], col[1], col[2], 1);
+    glVertex3f(0, 0, 0);
+    for(float ang = 0; ang < 2*M_PI; ang += 0.1) {
+      glVertex3f(rad*sin(ang), rad*cos(ang), 0);
+    }
+    glVertex3f(0, rad, 0);
+    glEnd();
+  }
+
   glPopMatrix();
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_LIGHTING);
diff --git a/src/star_system.cpp b/src/star_system.cpp
index 18ccee7..314c596 100644
--- a/src/star_system.cpp
+++ b/src/star_system.cpp
@@ -18,6 +18,19 @@ float StarSystem::starColors[][3] = {
   { 0.4, 0.4, 0.8 }  /* White dwarf. */
 };
 
+/* Indexed by enum type. */
+float StarSystem::starRealColors[][3] = {
+  { 0, 0, 0 }, /* Gravpoint. */
+  { 1.0, 0.2, 0.0 },  /* M */
+  { 1.0, 0.7, 0.1 },  /* K */
+  { 1.0, 1.0, 0.9 },  /* G */
+  { 1.0, 1.0, 1.0 },  /* F */
+  { 1.0, 1.0, 1.0 },  /* A */
+  { 0.7, 0.7, 1.0 },  /* B */
+  { 1.0, 0.7, 1.0 },  /* O */
+  { 1.0, 1.0, 1.0 },  /* White Dwarf. */
+};
+
 static const struct SBodySubTypeInfo {
   StarSystem::BodySuperType supertype;
   int         mass[2]; /* Min, max % sol for stars, unused for planets. */
diff --git a/src/star_system.h b/src/star_system.h
index e75fa1a..c832080 100644
--- a/src/star_system.h
+++ b/src/star_system.h
@@ -29,6 +29,7 @@ public:
   int GetNumStars(void) const { return m_numStars; }
 
   static float starColors[][3];
+  static float starRealColors[][3];
 
   struct Orbit {
     void KeplerPosAtTime(double t, double* dist, double* ang);
diff --git a/src/world_view.cpp b/src/world_view.cpp
index 8659e10..2dd4439 100644
--- a/src/world_view.cpp
+++ b/src/world_view.cpp
@@ -145,11 +145,10 @@ void WorldView::Draw3D(void) {
     lightPos[2] = lpos.z;
     lightPos[3] = 0;
     
-    const float* col = StarSystem::starColors[L3D::currentSystem->rootBody->type];
+    const float* col = StarSystem::starRealColors[L3D::currentSystem->rootBody->type];
     float lightCol[4] = { col[0], col[1], col[2], 0 };
     float ambCol[4]   = { col[0]*0.1, col[1]*0.1, col[2]*0.1, 0 };
 
-    //glColor3fv(StarSystem::starColors[(*i).primaryStarClass]);
     glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightCol);
     glLightfv(GL_LIGHT0, GL_AMBIENT, ambCol);