1 #include "viewportMainScreen.h"
2 #include "playerInfo.h"
3 #include "preferenceManager.h"
4 #include "spaceObjects/playerSpaceship.h"
5 
GuiViewportMainScreen(GuiContainer * owner,string id)6 GuiViewportMainScreen::GuiViewportMainScreen(GuiContainer* owner, string id)
7 : GuiViewport3D(owner, id)
8 {
9     uint8_t flags = PreferencesManager::get("main_screen_flags","7").toInt();
10 
11     if (flags & flag_callsigns)
12       showCallsigns();
13     if (flags & flag_headings)
14       showHeadings();
15     if (flags & flag_spacedust)
16       showSpacedust();
17 
18     first_person = PreferencesManager::get("first_person") == "1";
19 }
20 
onDraw(sf::RenderTarget & window)21 void GuiViewportMainScreen::onDraw(sf::RenderTarget& window)
22 {
23     if (my_spaceship)
24     {
25         P<SpaceObject> target_ship = my_spaceship->getTarget();
26         float target_camera_yaw = my_spaceship->getRotation();
27         switch(my_spaceship->main_screen_setting)
28         {
29         case MSS_Back: target_camera_yaw += 180; break;
30         case MSS_Left: target_camera_yaw -= 90; break;
31         case MSS_Right: target_camera_yaw += 90; break;
32         case MSS_Target:
33             if (target_ship)
34             {
35                 sf::Vector2f target_camera_diff = my_spaceship->getPosition() - target_ship->getPosition();
36                 target_camera_yaw = sf::vector2ToAngle(target_camera_diff) + 180;
37             }
38             break;
39         default: break;
40         }
41         camera_pitch = 30.0f;
42 
43         float camera_ship_distance = 420.0f;
44         float camera_ship_height = 420.0f;
45         if (first_person)
46         {
47             camera_ship_distance = -my_spaceship->getRadius();
48             camera_ship_height = my_spaceship->getRadius() / 10.f;
49             camera_pitch = 0;
50         }
51         sf::Vector2f cameraPosition2D = my_spaceship->getPosition() + sf::vector2FromAngle(target_camera_yaw) * -camera_ship_distance;
52         sf::Vector3f targetCameraPosition(cameraPosition2D.x, cameraPosition2D.y, camera_ship_height);
53 #ifdef DEBUG
54         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
55         {
56             targetCameraPosition.x = my_spaceship->getPosition().x;
57             targetCameraPosition.y = my_spaceship->getPosition().y;
58             targetCameraPosition.z = 3000.0;
59             camera_pitch = 90.0f;
60         }
61 #endif
62         if (first_person)
63         {
64             camera_position = targetCameraPosition;
65             camera_yaw = target_camera_yaw;
66         }
67         else
68         {
69             camera_position = camera_position * 0.9f + targetCameraPosition * 0.1f;
70             camera_yaw += sf::angleDifference(camera_yaw, target_camera_yaw) * 0.1f;
71         }
72     }
73     GuiViewport3D::onDraw(window);
74 }
75