diff --git a/src/dynamic_body.cpp b/src/dynamic_body.cpp index 45c451d..8245cfc 100644 --- a/src/dynamic_body.cpp +++ b/src/dynamic_body.cpp @@ -17,12 +17,14 @@ void DynamicBody::Save(void) { using namespace Serializer::Write; ModelBody::Save(); wr_vector3d(GetAngVelocity()); + wr_vector3d(GetVelocity()); } void DynamicBody::Load(void) { using namespace Serializer::Read; ModelBody::Load(); SetAngVelocity(rd_vector3d()); + SetVelocity(rd_vector3d()); } void DynamicBody::Enable(void) { diff --git a/src/generic_system_view.cpp b/src/generic_system_view.cpp index a1c6327..717ec19 100644 --- a/src/generic_system_view.cpp +++ b/src/generic_system_view.cpp @@ -27,13 +27,15 @@ GenericSystemView::GenericSystemView(void) : View() { } void GenericSystemView::Draw3D(void) { + int playerLocSecX, playerLocSecY, playerLocSysIdx; + L3D::currentSystem->GetPos(&playerLocSecX, &playerLocSecY, &playerLocSysIdx); StarSystem* s = L3D::GetSelectedSystem(); if(s && !s->IsSystem(px, py, pidx)) { s->GetPos(&px, &py, &pidx); Sector sec(px, py); - Sector psec(L3D::playerLocSecX, L3D::playerLocSecY); - const float dist = Sector::DistanceBetween(&sec,pidx, &psec, L3D::playerLocSysIdx); + Sector psec(playerLocSecX, playerLocSecY); + const float dist = Sector::DistanceBetween(&sec,pidx, &psec,playerLocSysIdx); char buf[256]; snprintf(buf, sizeof(buf), "Dist. %.2f light years.", dist); diff --git a/src/l3d.h b/src/l3d.h index ba7197a..eeeb071 100644 --- a/src/l3d.h +++ b/src/l3d.h @@ -67,20 +67,14 @@ public: static MTRand rng; static void HyperspaceTo(StarSystem* destination); - enum CamType { CAM_FRONT, CAM_REAR, CAM_EXTERNAL }; enum MapView { MAP_NOMAP, MAP_SECTOR, MAP_SYSTEM }; - static void SetCamType(enum CamType); static void SetMapView(enum MapView); - static enum CamType GetCamType(void) { return camType; } static enum MapView GetMapView(void) { return mapView; } static void SetView(View* v); static View* GetView(void) { return currentView; } static StarSystem* GetSelectedSystem(void); static bool showDebugInfo; - static int playerLocSecX; - static int playerLocSecY; - static int playerLocSysIdx; static Player* player; static SectorView* sectorView; static SystemInfoView* systemInfoView; @@ -101,7 +95,6 @@ private: static double gameTime; static StarSystem* selectedSystem; - static enum CamType camType; static enum MapView mapView; static float timeAccel; static float frameTime; diff --git a/src/main.cpp b/src/main.cpp index db0e207..ab4800b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,6 @@ sigc::signal L3D::onMouseButtonDown; char L3D::keyState[SDLK_LAST]; char L3D::mouseButton[5]; int L3D::mouseMotion[2]; -enum L3D::CamType L3D::camType; enum L3D::MapView L3D::mapView; Player* L3D::player; View* L3D::currentView; @@ -50,9 +49,6 @@ MTRand L3D::rng; double L3D::gameTime; float L3D::frameTime; GLUquadric* L3D::gluQuadric; -int L3D::playerLocSecX; -int L3D::playerLocSecY; -int L3D::playerLocSysIdx; bool L3D::showDebugInfo; void L3D::Init(IniConfig& config) { @@ -145,12 +141,6 @@ void L3D::SetTimeAccel(float s) { timeAccel = s; } -void L3D::SetCamType(enum CamType c) { - camType = c; - mapView = MAP_NOMAP; - SetView(worldView); -} - void L3D::SetMapView(enum MapView v) { mapView = v; if(v == MAP_SECTOR) @@ -446,7 +436,6 @@ void L3D::HyperspaceTo(StarSystem* dest) { float ang = rng.Double(M_PI); L3D::player->SetPosition(vector3d(sin(ang)*AU, cos(ang)*AU,0)); L3D::player->SetVelocity(vector3d(0.0)); - dest->GetPos(&L3D::playerLocSecX, &L3D::playerLocSecY, &L3D::playerLocSysIdx); } void L3D::Serialize(void) { @@ -455,6 +444,8 @@ void L3D::Serialize(void) { wr_double(gameTime); StarSystem::Serialize(currentSystem); Space::Serialize(); + sectorView->Save(); + worldView->Save(); } void L3D::Unserialize(void) { @@ -470,6 +461,8 @@ void L3D::Unserialize(void) { L3D::player = 0; } Space::Unserialize(); + sectorView->Load(); + worldView->Load(); } IniConfig::IniConfig(const char* filename) { diff --git a/src/player.cpp b/src/player.cpp index 62b4cd7..4036dc1 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -7,8 +7,6 @@ #include "space_station_view.h" Player::Player(ShipType::Type shipType) : Ship(shipType) { - m_external_view_rotx = m_external_view_roty = 0; - m_external_view_dist = 200; m_mouseCMov[0] = m_mouseCMov[1] = 0; m_equipment.Set(Equip::SLOT_ENGINE, 0, Equip::DRIVE_CLASS1); UpdateMass(); @@ -20,7 +18,7 @@ Player::~Player(void) { } void Player::Render(const Frame* camFrame) { - if(L3D::GetCamType() == L3D::CAM_EXTERNAL) { + if(L3D::worldView->GetCamType() == WorldView::CAM_EXTERNAL) { Ship::Render(camFrame); } else { glPushMatrix(); @@ -37,22 +35,6 @@ void Player::SetDockedWith(SpaceStation* s, int port) { } } -vector3d Player::GetExternalViewTranslation(void) { - vector3d p = vector3d(0, 0, m_external_view_dist); - p = matrix4x4d::RotateXMatrix(-DEG2RAD(m_external_view_rotx)) * p; - p = matrix4x4d::RotateYMatrix(-DEG2RAD(m_external_view_roty)) * p; - matrix4x4d m; - GetRotMatrix(m); - p = m*p; - //printf("%f,%f,%f\n", p.x, p.y, p.z); - return p; -} - -void Player::ApplyExternalViewRotation(matrix4x4d& m) { - m = matrix4x4d::RotateXMatrix(-DEG2RAD(m_external_view_rotx)) * m; - m = matrix4x4d::RotateYMatrix(-DEG2RAD(m_external_view_roty)) * m; -} - void Player::TimeStepUpdate(const float timeStep) { if(GetFlightState() == Ship::FLYING) { /* When world view not selected. */ @@ -82,18 +64,19 @@ void Player::PollControls(void) { float ta2 = time_accel*time_accel; polledControlsThisTurn = true; - - if(L3D::GetCamType() == L3D::CAM_EXTERNAL) { - if(L3D::KeyState(SDLK_UP)) m_external_view_rotx -= 45*frameTime; - if(L3D::KeyState(SDLK_DOWN)) m_external_view_rotx += 45*frameTime; - if(L3D::KeyState(SDLK_LEFT)) m_external_view_roty -= 45*frameTime; - if(L3D::KeyState(SDLK_RIGHT)) m_external_view_roty += 45*frameTime; - if(L3D::KeyState(SDLK_EQUALS)) m_external_view_dist -= 400*frameTime; - if(L3D::KeyState(SDLK_MINUS)) m_external_view_dist += 400*frameTime; - m_external_view_dist = MAX(50, m_external_view_dist); + + if(L3D::worldView->GetCamType() == WorldView::CAM_EXTERNAL) { + if(L3D::KeyState(SDLK_UP)) L3D::worldView->m_externalViewRotX -= 45*frameTime; + if(L3D::KeyState(SDLK_DOWN)) L3D::worldView->m_externalViewRotX += 45*frameTime; + if(L3D::KeyState(SDLK_LEFT)) L3D::worldView->m_externalViewRotY -= 45*frameTime; + if(L3D::KeyState(SDLK_RIGHT)) L3D::worldView->m_externalViewRotY += 45*frameTime; + if(L3D::KeyState(SDLK_EQUALS)) L3D::worldView->m_externalViewDist -= 400*frameTime; + if(L3D::KeyState(SDLK_MINUS)) L3D::worldView->m_externalViewDist += 400*frameTime; + L3D::worldView->m_externalViewDist = MAX(50, L3D::worldView->m_externalViewDist); /* When landed don't let external view look from below. */ - if(GetFlightState() == LANDED) m_external_view_rotx = CLAMP(m_external_view_rotx, -170.0, -10); + if(GetFlightState() == LANDED) + L3D::worldView->m_externalViewRotX = CLAMP(L3D::worldView->m_externalViewRotX, -170.0, -10); } if((time_accel == 0) || GetDockedWith() || @@ -127,7 +110,7 @@ void Player::PollControls(void) { if(L3D::KeyState(SDLK_SPACE) || (L3D::MouseButtonState(1) && L3D::MouseButtonState(1) && L3D::MouseButtonState(3))) SetGunState(0,1); else SetGunState(0,0); - if(L3D::GetCamType() != L3D::CAM_EXTERNAL) { + if(L3D::worldView->GetCamType() != WorldView::CAM_EXTERNAL) { if(L3D::KeyState(SDLK_LEFT)) angThrust.y += 1; if(L3D::KeyState(SDLK_RIGHT)) angThrust.y += -1; if(L3D::KeyState(SDLK_UP)) angThrust.x += -1; @@ -169,7 +152,7 @@ void Player::DrawHUD(const Frame* cam_frame) { /* Object labels. */ { for(std::list::iterator i = Space::bodies.begin(); i != Space::bodies.end(); ++i) { - if((L3D::GetCamType() != L3D::CAM_EXTERNAL) && (*i == this)) continue; + if((L3D::worldView->GetCamType() != WorldView::CAM_EXTERNAL) && (*i == this)) continue; Body* b = *i; vector3d _pos = b->GetPositionRelTo(cam_frame); if(_pos.z < 0 @@ -210,7 +193,7 @@ void Player::DrawHUD(const Frame* cam_frame) { } /* Normal crosshairs. */ - if(L3D::GetCamType() == L3D::CAM_FRONT) { + if(L3D::worldView->GetCamType() == WorldView::CAM_FRONT) { float px = Gui::Screen::GetWidth()/2.0; float py = Gui::Screen::GetHeight()/2.0; glBegin(GL_LINES); diff --git a/src/player.h b/src/player.h index a180a84..248d7a8 100644 --- a/src/player.h +++ b/src/player.h @@ -12,15 +12,11 @@ public: virtual void Render(const Frame* camFrame); void DrawHUD(const Frame* cam_frame); virtual void SetDockedWith(SpaceStation*, int port); - vector3d GetExternalViewTranslation(void); - void ApplyExternalViewRotation(matrix4x4d &m); void TimeStepUpdate(const float timeStep); private: void DrawTargetSquares(); void DrawTargetSquare(const Body* const target); float m_mouseCMov[2]; - float m_external_view_rotx, m_external_view_roty; - float m_external_view_dist; bool polledControlsThisTurn; }; diff --git a/src/sector_view.cpp b/src/sector_view.cpp index 7a47a17..ffa04b8 100644 --- a/src/sector_view.cpp +++ b/src/sector_view.cpp @@ -5,6 +5,7 @@ #include "sector.h" #include "system_info_view.h" #include "player.h" +#include "serializer.h" SectorView::SectorView(void) : GenericSystemView() { SetTransparency(true); @@ -40,6 +41,30 @@ SectorView::~SectorView(void) { glDeleteLists(m_gluDiskDlist, 1); } +void SectorView::Save(void) { + using namespace Serializer::Write; + wr_float(m_zoom); + wr_int(m_secx); + wr_int(m_secy); + wr_int(m_selected); + wr_float(m_px); + wr_float(m_py); + wr_float(m_rot_x); + wr_float(m_rot_z); +} + +void SectorView::Load(void) { + using namespace Serializer::Read; + m_zoom = rd_float(); + m_secx = rd_int(); + m_secy = rd_int(); + m_selected = rd_int(); + m_px = rd_float(); + m_py = rd_float(); + m_rot_x = rd_float(); + m_rot_z = rd_float(); +} + void SectorView::OnClickSystemInfo(void) { L3D::SetView(L3D::systemInfoView); } @@ -109,6 +134,8 @@ void SectorView::PutText(std::string& text) { } void SectorView::DrawSector(int sx, int sy) { + int playerLocSecX, playerLocSecY, playerLocSysIdx; + L3D::currentSystem->GetPos(&playerLocSecX, &playerLocSecY, &playerLocSysIdx); Sector s = Sector(sx, sy); glColor3f(0, .8, 0); glBegin(GL_LINE_LOOP); @@ -135,7 +162,7 @@ void SectorView::DrawSector(int sx, int sy) { glRotatef(-m_rot_x, 1, 0, 0); glCallList(m_gluDiskDlist); /* Player location indicator. */ - if((sx == L3D::playerLocSecX) && (sy == L3D::playerLocSecY) && (num == L3D::playerLocSysIdx)) { + if((sx == playerLocSecX) && (sy == playerLocSecY) && (num == playerLocSysIdx)) { shipstats_t stats; L3D::player->CalcStats(&stats); glColor3f(0, 0, 1); diff --git a/src/sector_view.h b/src/sector_view.h index e658236..402304d 100644 --- a/src/sector_view.h +++ b/src/sector_view.h @@ -14,6 +14,8 @@ public: virtual void Update(void); virtual void Draw3D(void); bool GetSelectedSystem(int* sector_x, int* sector_y, int* system_idx); + virtual void Save(void); + virtual void Load(void); private: void DrawSector(int x, int y); void PutText(std::string& text); diff --git a/src/ship_cpanel.cpp b/src/ship_cpanel.cpp index 6ab45bf..d8dbed0 100644 --- a/src/ship_cpanel.cpp +++ b/src/ship_cpanel.cpp @@ -4,6 +4,7 @@ #include "space_station_view.h" #include "player.h" #include "info_view.h" +#include "world_view.h" ShipCpanel::ShipCpanel(void) : Gui::Fixed(640, 64) { Gui::Screen::AddBaseWidget(this, 0, 0); @@ -49,9 +50,9 @@ ShipCpanel::ShipCpanel(void) : Gui::Fixed(640, 64) { Gui::MultiStateImageButton* cam_button = new Gui::MultiStateImageButton(); g->Add(cam_button); cam_button->SetSelected(true); - cam_button->AddState(L3D::CAM_FRONT, "icons/cam_front.png"); - cam_button->AddState(L3D::CAM_REAR, "icons/cam_rear.png"); - cam_button->AddState(L3D::CAM_EXTERNAL, "icons/cam_external.png"); + cam_button->AddState(WorldView::CAM_FRONT, "icons/cam_front.png"); + cam_button->AddState(WorldView::CAM_REAR, "icons/cam_rear.png"); + cam_button->AddState(WorldView::CAM_EXTERNAL, "icons/cam_external.png"); cam_button->SetShortcut(SDLK_F1, KMOD_NONE); cam_button->onClick.connect(sigc::mem_fun(this, &ShipCpanel::OnChangeCamView)); Add(cam_button, 2, 2); @@ -73,9 +74,11 @@ ShipCpanel::ShipCpanel(void) : Gui::Fixed(640, 64) { info_button->onClick.connect(sigc::mem_fun(this, &ShipCpanel::OnChangeInfoView)); Add(info_button, 66, 2); - Gui::ImageButton* comms_button = new Gui::ImageButton("icons/comms_f4.png"); - //g->Add(comms_button); + Gui::MultiStateImageButton* comms_button = new Gui::MultiStateImageButton(); + g->Add(comms_button); + comms_button->SetSelected(false); comms_button->SetShortcut(SDLK_F4, KMOD_NONE); + comms_button->AddState(0, "icons/comms_f4.png"); comms_button->onClick.connect(sigc::mem_fun(this, &ShipCpanel::OnClickComms)); Add(comms_button, 98, 2); @@ -118,7 +121,8 @@ void ShipCpanel::SetScannerWidget(Widget* w) { } void ShipCpanel::OnChangeCamView(Gui::MultiStateImageButton* b) { - L3D::SetCamType((enum L3D::CamType)b->GetState()); + L3D::worldView->SetCamType((enum WorldView::CamType)b->GetState()); + L3D::SetView(L3D::worldView); } void ShipCpanel::OnChangeInfoView(Gui::MultiStateImageButton* b) { @@ -134,7 +138,7 @@ void ShipCpanel::OnClickTimeaccel(Gui::ISelectable* i, double step) { L3D::SetTimeAccel(step); } -void ShipCpanel::OnClickComms(void) { +void ShipCpanel::OnClickComms(Gui::MultiStateImageButton* b) { if(L3D::player->GetDockedWith()) L3D::SetView(L3D::spaceStationView); } diff --git a/src/ship_cpanel.h b/src/ship_cpanel.h index d87f564..4f51a27 100644 --- a/src/ship_cpanel.h +++ b/src/ship_cpanel.h @@ -15,7 +15,7 @@ private: void OnChangeMapView(Gui::MultiStateImageButton* b); void OnChangeInfoView(Gui::MultiStateImageButton* b); void OnClickTimeaccel(Gui::ISelectable* i, double step); - void OnClickComms(void); + void OnClickComms(Gui::MultiStateImageButton* b); Widget* m_scannerWidget; Gui::Label* m_clock; diff --git a/src/view.h b/src/view.h index 896bb63..872fb90 100644 --- a/src/view.h +++ b/src/view.h @@ -37,6 +37,8 @@ virtual void Draw3D(void) = 0; /* For checking key states, mouse stuff. */ virtual void Update(void) = 0; + virtual void Save(void) { } + virtual void Load(void) { } protected: /* Each view can put some buttons in the bottom right of the cpanel. */ Gui::Fixed* m_rightButtonBar; diff --git a/src/world_view.cpp b/src/world_view.cpp index 7d12a49..1fd61d0 100644 --- a/src/world_view.cpp +++ b/src/world_view.cpp @@ -5,6 +5,7 @@ #include "space.h" #include "space_station.h" #include "ship_cpanel.h" +#include "serializer.h" const float WorldView::PICK_OBJECT_RECT_SIZE = 20.0f; @@ -15,7 +16,10 @@ WorldView::WorldView(void): View() { GetSize(size); labelsOn = true; + m_camType = CAM_FRONT; SetTransparency(true); + m_externalViewRotX = m_externalViewRotY = 0; + m_externalViewDist = 200; commsOptions = new Fixed(size[0], size[1]/2); commsOptions->SetTransparency(true); @@ -71,6 +75,41 @@ WorldView::WorldView(void): View() { glEndList(); } +void WorldView::Save(void) { + using namespace Serializer::Write; + wr_float(m_externalViewRotX); + wr_float(m_externalViewRotY); + wr_float(m_externalViewDist); + wr_int((int)m_camType); +} + +void WorldView::Load(void) { + using namespace Serializer::Read; + m_externalViewRotX = rd_float(); + m_externalViewRotY = rd_float(); + m_externalViewDist = rd_float(); + m_camType = (CamType)rd_int(); +} + +void WorldView::SetCamType(enum CamType c) { + m_camType = c; +} + +vector3d WorldView::GetExternalViewTranslation(void) { + vector3d p = vector3d(0, 0, m_externalViewDist); + p = matrix4x4d::RotateXMatrix(-DEG2RAD(m_externalViewRotX)) * p; + p = matrix4x4d::RotateYMatrix(-DEG2RAD(m_externalViewRotY)) * p; + matrix4x4d m; + L3D::player->GetRotMatrix(m); + p = m*p; + return p; +} + +void WorldView::ApplyExternalViewRotation(matrix4x4d& m) { + m = matrix4x4d::RotateXMatrix(-DEG2RAD(m_externalViewRotX)) * m; + m = matrix4x4d::RotateYMatrix(-DEG2RAD(m_externalViewRotY)) * m; +} + void WorldView::OnChangeWheelsState(Gui::MultiStateImageButton* b) { if(!L3D::player->SetWheelState(b->GetState())) { b->StatePrev(); @@ -114,15 +153,15 @@ void WorldView::Draw3D(void) { matrix4x4d camRot = matrix4x4d::Identity(); - if(L3D::GetCamType() == L3D::CAM_FRONT) { + if(m_camType == CAM_FRONT) { cam_frame.SetPosition(L3D::player->GetPosition()); - } else if(L3D::GetCamType() == L3D::CAM_REAR) { + } else if(m_camType == CAM_REAR) { camRot.RotateY(M_PI); //glRotatef(180.0f, 0, 1, 0); cam_frame.SetPosition(L3D::player->GetPosition()); } else { /* CAM_EXTERNAL */ - cam_frame.SetPosition(L3D::player->GetPosition() + L3D::player->GetExternalViewTranslation()); - L3D::player->ApplyExternalViewRotation(camRot); + cam_frame.SetPosition(L3D::player->GetPosition() + GetExternalViewTranslation()); + ApplyExternalViewRotation(camRot); } { diff --git a/src/world_view.h b/src/world_view.h index a5ff2e5..c90a05f 100644 --- a/src/world_view.h +++ b/src/world_view.h @@ -14,6 +14,16 @@ public: void UpdateCommsOptions(void); bool GetShowLabels(void) { return labelsOn; } void DrawBgStars(void); + vector3d GetExternalViewTranslation(void); + void ApplyExternalViewRotation(matrix4x4d& m); + virtual void Save(void); + virtual void Load(void); + enum CamType { CAM_FRONT, CAM_REAR, CAM_EXTERNAL }; + void SetCamType(enum CamType); + enum CamType GetCamType(void) { return m_camType; } + + float m_externalViewRotX, m_externalViewRotY; + float m_externalViewDist; private: Gui::Button* AddCommsOption(const std::string msg, int ypos); void OnClickHyperspace(void); @@ -28,5 +38,6 @@ private: Gui::Label* flightStatus; Gui::ImageButton* launchButton; bool labelsOn; + enum CamType m_camType; };