From aeeea56e97d3004fc14c8dbd175784b7e8cddfd3 Mon Sep 17 00:00:00 2001 From: Gliese852 Date: Sun, 6 Sep 2020 11:05:23 +0300 Subject: [PATCH 1/3] Fix crashes when hyperjump with a map opened Fixes #4901 System map: add a check to see if we are in hyperspace Sector map: - move the drawing of the player's indicator from DrawNearSector up to Draw3D - calculate the interpolated position of the player's indicator during a hyperjump - don't display current system during hyperjump (lua-side) - fix selected system indicator (green circle), it pointed not to the the selected system, but to the current system Also: - add easing function into utils.cpp to smooth the movement of the player indicator - move fuel reduction from the start of the hyperjump to the end - force handling of lua events immediately after entering system --- data/libs/Equipment.lua | 2 +- data/libs/Ship.lua | 6 +-- data/pigui/modules/hyperjump-planner.lua | 13 +++-- data/pigui/modules/system-view-ui.lua | 2 +- src/Game.cpp | 8 ++++ src/SectorView.cpp | 61 +++++++++++++++++------- src/SectorView.h | 2 +- src/SystemView.cpp | 2 +- src/utils.h | 12 +++++ 9 files changed, 78 insertions(+), 30 deletions(-) diff --git a/data/libs/Equipment.lua b/data/libs/Equipment.lua index 7f294d4611a..9b415f26987 100644 --- a/data/libs/Equipment.lua +++ b/data/libs/Equipment.lua @@ -320,7 +320,7 @@ HyperdriveType.HyperjumpTo = function (self, ship, destination) return ship:InitiateHyperjumpTo(destination, warmup_time, duration, sounds), fuel_use, duration end -HyperdriveType.OnEnterHyperspace = function (self, ship) +HyperdriveType.OnLeaveHyperspace = function (self, ship) if ship:hasprop('nextJumpFuelUse') then local amount = ship.nextJumpFuelUse ship:RemoveEquip(self.fuel, amount) diff --git a/data/libs/Ship.lua b/data/libs/Ship.lua index 725c6bdfd51..92b7ff9e190 100644 --- a/data/libs/Ship.lua +++ b/data/libs/Ship.lua @@ -1015,12 +1015,9 @@ local onEnterSystem = function (ship) end end end -end - -local onLeaveSystem = function (ship) local engine = ship:GetEquip("engine", 1) if engine then - engine:OnEnterHyperspace(ship) + engine:OnLeaveHyperspace(ship) end end @@ -1036,7 +1033,6 @@ local onShipDestroyed = function (ship, attacker) end Event.Register("onEnterSystem", onEnterSystem) -Event.Register("onLeaveSystem", onLeaveSystem) Event.Register("onShipDestroyed", onShipDestroyed) Event.Register("onGameStart", onGameStart) Serializer:Register("ShipClass", serialize, unserialize) diff --git a/data/pigui/modules/hyperjump-planner.lua b/data/pigui/modules/hyperjump-planner.lua index 5df15ab53ee..73d7ebd30d0 100644 --- a/data/pigui/modules/hyperjump-planner.lua +++ b/data/pigui/modules/hyperjump-planner.lua @@ -74,6 +74,10 @@ local function showInfo() local total_duration = 0 local total_distance = 0 + textIcon(icons.display_navtarget, lui.CURRENT_SYSTEM) + ui.sameLine() + -- we can only have the current path in normal space + if current_path then local start = current_path -- Tally up totals for the entire jump plan for _,jump in pairs(hyperjump_route) do @@ -86,11 +90,12 @@ local function showInfo() start = jump.path end - textIcon(icons.display_navtarget, lui.CURRENT_SYSTEM) - ui.sameLine() if ui.selectable(current_system.name .. " (" .. current_path.sectorX .. ", " .. current_path.sectorY .. ", " .. current_path.sectorZ ..")") then sectorView:SwitchToPath(current_path) end + else -- no current path => we are hyperjumping => no current system + ui.text("---") + end textIcon(icons.route_destination, lui.FINAL_TARGET) @@ -337,8 +342,8 @@ function hyperJumpPlanner.display() end local drive = table.unpack(player:GetEquip("engine")) or nil local fuel_type = drive and drive.fuel or Equipment.cargo.hydrogen - current_system = Game.system - current_path = current_system.path + current_system = Game.system -- will be nil during the hyperjump + current_path = Game.system and current_system.path -- will be nil during the hyperjump current_fuel = player:CountEquip(fuel_type,"cargo") map_selected_path = sectorView:GetSelectedSystemPath() route_jumps = sectorView:GetRouteSize() diff --git a/data/pigui/modules/system-view-ui.lua b/data/pigui/modules/system-view-ui.lua index c00b8aa44f2..b20c819eb51 100644 --- a/data/pigui/modules/system-view-ui.lua +++ b/data/pigui/modules/system-view-ui.lua @@ -566,7 +566,7 @@ local hideSystemViewWindows = false local function displaySystemViewUI() player = Game.player local current_view = Game.CurrentView() - if current_view == "system" and not Game.InHyperspace() then + if current_view == "system" then if dummyFrames > 0 then -- do it a few frames, because imgui need a few frames to make the correct window size -- first, doing some one-time actions here diff --git a/src/Game.cpp b/src/Game.cpp index 9ec88b36e6e..d4667958f95 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -289,6 +289,14 @@ void Game::TimeStep(float step) if (Pi::game->GetTime() >= m_hyperspaceEndTime) { SwitchToNormalSpace(); m_player->EnterSystem(); + // event "onEnterSystem(player)" is in the event queue after p_player->EnterSystem() + // in this callback, for example, fuel is reduced + // but event handling has already passed, and will only be in the next frame + // it turns out that we are already in the system, but the fuel has not yet been reduced + // and then the drawing of the views will go, and inconsistencies may occur, + // for example, the sphere of the hyperjump range will be too large + // so we forcefully call event handling again + LuaEvent::Emit(); RequestTimeAccel(TIMEACCEL_1X); } else m_hyperspaceProgress += step; diff --git a/src/SectorView.cpp b/src/SectorView.cpp index d1ad591fccc..39c2c1c67dd 100644 --- a/src/SectorView.cpp +++ b/src/SectorView.cpp @@ -263,9 +263,6 @@ void SectorView::Draw3D() modelview.Translate(-FFRAC(m_pos.x) * Sector::SIZE, -FFRAC(m_pos.y) * Sector::SIZE, -FFRAC(m_pos.z) * Sector::SIZE); m_renderer->SetTransform(modelview); - RefCountedPtr playerSec = GetCached(m_current); - const vector3f playerPos = Sector::SIZE * vector3f(float(m_current.sectorX), float(m_current.sectorY), float(m_current.sectorZ)) + playerSec->m_systems[m_current.systemIndex].GetPosition(); - if (m_zoomClamped <= FAR_THRESHOLD) DrawNearSectors(modelview); else @@ -295,8 +292,45 @@ void SectorView::Draw3D() trans.Rotate(DEG2RAD(m_rotZ), 0.f, 0.f, 1.f); trans.Translate(-(m_pos.x) * Sector::SIZE, -(m_pos.y) * Sector::SIZE, -(m_pos.z) * Sector::SIZE); + // calculate the player's location (it will be interpolated between systems during a hyperjump) + const Sector::System currentSystem = GetCached(m_current)->m_systems[m_current.systemIndex]; + vector3f playerPos = Sector::SIZE * vector3f(float(m_current.sectorX), float(m_current.sectorY), float(m_current.sectorZ)) + currentSystem.GetPosition(); + float currentStarSize = StarSystem::starScale[currentSystem.GetStarType(0)]; + if (!m_inSystem) { + // we are in hyperspace, interpolate + // m_current has been set to the destination path + // destination system data: + const SystemPath dstPath = Pi::game->GetHyperspaceDest(); + const Sector::System dstSystem = GetCached(dstPath)->m_systems[dstPath.systemIndex]; + const vector3f dstPos = Sector::SIZE * vector3f(float(dstPath.sectorX), float(dstPath.sectorY), float(dstPath.sectorZ)) + dstSystem.GetPosition(); + // jumpProcess: 0.0f (at source) .. 1.0f (at destination) + // smoothing speed at the ends with cubic interpolation + const float jumpProcess = InOutCubicEasing(Pi::game->GetHyperspaceProgress() / Pi::game->GetHyperspaceDuration()); + // player location indicator's size of the star, on which it is drawn + // so we interpolate it too + const float dstStarSize = StarSystem::starScale[dstSystem.GetStarType(0)]; + // interpolating player location indicator's position and size + playerPos = playerPos.Lerp(dstPos, jumpProcess); + currentStarSize = currentStarSize + (dstStarSize - currentStarSize) * jumpProcess; + } + + // the next function assumes that m_renderer->(matrix4x4f::Identity()), + // so we run this function before we touch m_renderer again DrawRouteLines(playerPos, trans); + // player location indicator + // starting with the "trans" matrix, see above, adding translation to playerPos + matrix4x4f pTrans = trans * matrix4x4f::Translation(playerPos.x, playerPos.y, playerPos.z); + // rotate in the opposite direction, so that the disk always looks like a circle + pTrans.Rotate(DEG2RAD(-m_rotZ), 0, 0, 1); + pTrans.Rotate(DEG2RAD(-m_rotX), 1, 0, 0); + // corretion to the size of the current star, and hardcoded "3", the size of the player's indicator + pTrans.Scale(currentStarSize * 3.); + m_renderer->SetTransform(pTrans); + m_renderer->SetDepthRange(0.2, 1.0); + m_disk->SetColor(Color(0, 0, 204)); + m_disk->Draw(m_renderer); + UIView::Draw3D(); } @@ -534,13 +568,10 @@ void SectorView::DrawNearSectors(const matrix4x4f &modelview) PROFILE_SCOPED() m_visibleFactions.clear(); - RefCountedPtr playerSec = GetCached(m_current); - const vector3f playerPos = Sector::SIZE * vector3f(float(m_current.sectorX), float(m_current.sectorY), float(m_current.sectorZ)) + playerSec->m_systems[m_current.systemIndex].GetPosition(); - for (int sx = -DRAW_RAD; sx <= DRAW_RAD; sx++) { for (int sy = -DRAW_RAD; sy <= DRAW_RAD; sy++) { for (int sz = -DRAW_RAD; sz <= DRAW_RAD; sz++) { - DrawNearSector(int(floorf(m_pos.x)) + sx, int(floorf(m_pos.y)) + sy, int(floorf(m_pos.z)) + sz, playerPos, + DrawNearSector(int(floorf(m_pos.x)) + sx, int(floorf(m_pos.y)) + sy, int(floorf(m_pos.z)) + sz, modelview * matrix4x4f::Translation(Sector::SIZE * sx, Sector::SIZE * sy, Sector::SIZE * sz)); } } @@ -778,7 +809,7 @@ void SectorView::DrawRouteLines(const vector3f &playerAbsPos, const matrix4x4f & } } -void SectorView::DrawNearSector(const int sx, const int sy, const int sz, const vector3f &playerAbsPos, const matrix4x4f &trans) +void SectorView::DrawNearSector(const int sx, const int sy, const int sz, const matrix4x4f &trans) { PROFILE_SCOPED() m_renderer->SetTransform(trans); @@ -887,6 +918,8 @@ void SectorView::DrawNearSector(const int sx, const int sy, const int sz, const if (i->IsSameSystem(m_selected)) { if (m_selected != m_current && !bInRoute) { + const vector3f playerAbsPos = Sector::SIZE * vector3f(float(m_current.sectorX), float(m_current.sectorY), float(m_current.sectorZ)) + + GetCached(m_current)->m_systems[m_current.systemIndex].GetPosition(); m_selectedLine.SetStart(vector3f(0.f, 0.f, 0.f)); m_selectedLine.SetEnd(playerAbsPos - sysAbsPos); m_selectedLine.Draw(m_renderer, m_solidState); @@ -916,15 +949,8 @@ void SectorView::DrawNearSector(const int sx, const int sy, const int sz, const const Uint8 *col = StarSystem::starColors[(*i).GetStarType(0)]; AddStarBillboard(systrans, vector3f(0.f), Color(col[0], col[1], col[2], 255), 0.5f); - // player location indicator - if (m_inSystem && bIsCurrentSystem) { - m_renderer->SetDepthRange(0.2, 1.0); - m_disk->SetColor(Color(0, 0, 204)); - m_renderer->SetTransform(systrans * matrix4x4f::ScaleMatrix(3.f)); - m_disk->Draw(m_renderer); - } // selected indicator - if (bIsCurrentSystem) { + if (i->IsSameSystem(m_selected)) { m_renderer->SetDepthRange(0.1, 1.0); m_disk->SetColor(Color(0, 204, 0)); m_renderer->SetTransform(systrans * matrix4x4f::ScaleMatrix(2.f)); @@ -937,6 +963,7 @@ void SectorView::DrawNearSector(const int sx, const int sy, const int sz, const m_renderer->SetTransform(systrans * matrix4x4f::ScaleMatrix(2.f)); m_disk->Draw(m_renderer); } + // hyperspace range sphere if (bIsCurrentSystem && m_jumpSphere && m_playerHyperspaceRange > 0.0f) { const matrix4x4f sphTrans = trans * matrix4x4f::Translation(i->GetPosition().x, i->GetPosition().y, i->GetPosition().z); m_renderer->SetTransform(sphTrans * matrix4x4f::ScaleMatrix(m_playerHyperspaceRange)); @@ -1035,7 +1062,7 @@ void SectorView::Update() m_current = Pi::game->GetSpace()->GetStarSystem()->GetPath(); } else { m_inSystem = false; - m_current = Pi::player->GetHyperspaceDest(); + m_current = Pi::game->GetHyperspaceSource(); } const float frameTime = Pi::GetFrameTime(); diff --git a/src/SectorView.h b/src/SectorView.h index 966486a1de3..4fafcf85e43 100644 --- a/src/SectorView.h +++ b/src/SectorView.h @@ -119,7 +119,7 @@ class SectorView : public UIView, public DeleteEmitter { }; void DrawNearSectors(const matrix4x4f &modelview); - void DrawNearSector(const int sx, const int sy, const int sz, const vector3f &playerAbsPos, const matrix4x4f &trans); + void DrawNearSector(const int sx, const int sy, const int sz, const matrix4x4f &trans); void PutSystemLabels(RefCountedPtr sec, const vector3f &origin, int drawRadius); void DrawFarSectors(const matrix4x4f &modelview); diff --git a/src/SystemView.cpp b/src/SystemView.cpp index 09868aa9d72..9cf1d526514 100644 --- a/src/SystemView.cpp +++ b/src/SystemView.cpp @@ -547,7 +547,7 @@ void SystemView::Draw3D() } // glLineWidth(1); - if (m_game->GetSpace()->GetStarSystem()->GetPath().IsSameSystem(m_game->GetSectorView()->GetSelected())) { + if (m_game->IsNormalSpace() && m_game->GetSpace()->GetStarSystem()->GetPath().IsSameSystem(m_game->GetSectorView()->GetSelected())) { // draw ships if (m_shipDrawing != OFF) { RefreshShips(); diff --git a/src/utils.h b/src/utils.h index dfe5ae227d5..89d043fb63c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -269,6 +269,18 @@ static inline Uint32 ceil_pow2(Uint32 v) return v; } +// easing from https://github.com/Michaelangel007/easing#tldr-shut-up-and-show-me-the-code +// p should go from 0.0 to 1.0 +inline float InOutCubicEasing(float p) +{ + float m = p - 1; + float t = p * 2; + if (t < 1) + return p * t * t; + else + return 1 + m * m * m * 4; +} + void hexdump(const unsigned char *buf, int bufsz); #endif /* _UTILS_H */ From 0057619c624e6087f9d5c8d442cf9b6311f1d3e5 Mon Sep 17 00:00:00 2001 From: Gliese852 Date: Sun, 6 Sep 2020 12:48:00 +0300 Subject: [PATCH 2/3] Indent hyperjump-planner.lua --- data/pigui/modules/hyperjump-planner.lua | 156 +++++++++++------------ 1 file changed, 78 insertions(+), 78 deletions(-) diff --git a/data/pigui/modules/hyperjump-planner.lua b/data/pigui/modules/hyperjump-planner.lua index 73d7ebd30d0..c422f84f525 100644 --- a/data/pigui/modules/hyperjump-planner.lua +++ b/data/pigui/modules/hyperjump-planner.lua @@ -78,21 +78,21 @@ local function showInfo() ui.sameLine() -- we can only have the current path in normal space if current_path then - local start = current_path - -- Tally up totals for the entire jump plan - for _,jump in pairs(hyperjump_route) do - local status, distance, fuel, duration = player:GetHyperspaceDetails(start, jump.path) + local start = current_path + -- Tally up totals for the entire jump plan + for _,jump in pairs(hyperjump_route) do + local status, distance, fuel, duration = player:GetHyperspaceDetails(start, jump.path) - total_fuel = total_fuel + fuel - total_duration = total_duration + duration - total_distance = total_distance + distance + total_fuel = total_fuel + fuel + total_duration = total_duration + duration + total_distance = total_distance + distance - start = jump.path - end + start = jump.path + end - if ui.selectable(current_system.name .. " (" .. current_path.sectorX .. ", " .. current_path.sectorY .. ", " .. current_path.sectorZ ..")") then - sectorView:SwitchToPath(current_path) - end + if ui.selectable(current_system.name .. " (" .. current_path.sectorX .. ", " .. current_path.sectorY .. ", " .. current_path.sectorZ ..")") then + sectorView:SwitchToPath(current_path) + end else -- no current path => we are hyperjumping => no current system ui.text("---") end @@ -189,84 +189,84 @@ end local function showJumpRoute() if ui.collapsingHeader(lui.ROUTE_JUMPS, {"DefaultOpen"}) then mainButton(icons.forward, lui.ADD_JUMP, - function() - sectorView:AddToRoute(map_selected_path) - updateHyperspaceTarget() - selected_jump = #hyperjump_route + function() + sectorView:AddToRoute(map_selected_path) + updateHyperspaceTarget() + selected_jump = #hyperjump_route end) ui.sameLine() mainButton(icons.current_line, lui.REMOVE_JUMP, - function() - local new_route = {} - local new_count = 0 - if selected_jump then - sectorView:RemoveRouteItem(selected_jump) - end - updateHyperspaceTarget() + function() + local new_route = {} + local new_count = 0 + if selected_jump then + sectorView:RemoveRouteItem(selected_jump) + end + updateHyperspaceTarget() end) ui.sameLine() mainButton(icons.current_periapsis, lui.MOVE_UP, - function() - if selected_jump then - if sectorView:MoveRouteItemUp(selected_jump) then - selected_jump = selected_jump - 1 - end + function() + if selected_jump then + if sectorView:MoveRouteItemUp(selected_jump) then + selected_jump = selected_jump - 1 end - updateHyperspaceTarget() + end + updateHyperspaceTarget() end) ui.sameLine() mainButton(icons.current_apoapsis, lui.MOVE_DOWN, - function() - if selected_jump then - if sectorView:MoveRouteItemDown(selected_jump) then - selected_jump = selected_jump + 1 - end + function() + if selected_jump then + if sectorView:MoveRouteItemDown(selected_jump) then + selected_jump = selected_jump + 1 end - updateHyperspaceTarget() + end + updateHyperspaceTarget() end) ui.sameLine() mainButton(icons.retrograde_thin, lui.CLEAR_ROUTE, - function() - sectorView:ClearRoute() - updateHyperspaceTarget() + function() + sectorView:ClearRoute() + updateHyperspaceTarget() end) ui.sameLine() mainButton(icons.hyperspace, lui.AUTO_ROUTE, - function() - local result = sectorView:AutoRoute() - if result == "NO_DRIVE" then - mb.OK(lui.NO_DRIVE) - elseif result == "NO_VALID_ROUTE" then - mb.OK(lui.NO_VALID_ROUTE) - end - updateHyperspaceTarget() + function() + local result = sectorView:AutoRoute() + if result == "NO_DRIVE" then + mb.OK(lui.NO_DRIVE) + elseif result == "NO_VALID_ROUTE" then + mb.OK(lui.NO_VALID_ROUTE) + end + updateHyperspaceTarget() end) ui.sameLine() mainButton(icons.search_lens, lui.CENTER_ON_SYSTEM, - function() - if selected_jump then - sectorView:GotoSystemPath(hyperjump_route[selected_jump].path) - end + function() + if selected_jump then + sectorView:GotoSystemPath(hyperjump_route[selected_jump].path) + end end) ui.separator() local clicked ui.child("routelist", function() - for jumpIndex, jump in pairs(hyperjump_route) do - ui.withStyleColors({["Text"] = jump.color}, + for jumpIndex, jump in pairs(hyperjump_route) do + ui.withStyleColors({["Text"] = jump.color}, function() if ui.selectable(jump.textLine, jumpIndex == selected_jump) then clicked = jumpIndex end - end) - end -- for + end) + end -- for end --function ) @@ -293,13 +293,13 @@ function hyperJumpPlanner.updateInRoute(path) end local function showHyperJumpPlannerWindow() - textIcon(icons.route) - ui.sameLine() - ui.text(lui.HYPERJUMP_ROUTE) - ui.separator() - showInfo() - ui.separator() - showJumpRoute() + textIcon(icons.route) + ui.sameLine() + ui.text(lui.HYPERJUMP_ROUTE) + ui.separator() + showInfo() + ui.separator() + showJumpRoute() end -- showHyperJumpPlannerWindow function hyperJumpPlanner.Dummy() @@ -337,17 +337,17 @@ end function hyperJumpPlanner.display() player = Game.player if not textIconSize then - textIconSize = ui.calcTextSize("H") - textIconSize.x = textIconSize.y -- make square + textIconSize = ui.calcTextSize("H") + textIconSize.x = textIconSize.y -- make square end - local drive = table.unpack(player:GetEquip("engine")) or nil - local fuel_type = drive and drive.fuel or Equipment.cargo.hydrogen - current_system = Game.system -- will be nil during the hyperjump - current_path = Game.system and current_system.path -- will be nil during the hyperjump - current_fuel = player:CountEquip(fuel_type,"cargo") - map_selected_path = sectorView:GetSelectedSystemPath() - route_jumps = sectorView:GetRouteSize() - showHyperJumpPlannerWindow() + local drive = table.unpack(player:GetEquip("engine")) or nil + local fuel_type = drive and drive.fuel or Equipment.cargo.hydrogen + current_system = Game.system -- will be nil during the hyperjump + current_path = Game.system and current_system.path -- will be nil during the hyperjump + current_fuel = player:CountEquip(fuel_type,"cargo") + map_selected_path = sectorView:GetSelectedSystemPath() + route_jumps = sectorView:GetRouteSize() + showHyperJumpPlannerWindow() end -- hyperJumpPlanner.display function hyperJumpPlanner.setSectorView(sv) @@ -361,14 +361,14 @@ function hyperJumpPlanner.onShipEquipmentChanged(ship, equipment) end function hyperJumpPlanner.onEnterSystem(ship) - -- remove the first jump if it's the current system (and enabled to do so) - -- this should be the case if you are following a route and want the route to be - -- updated as you make multiple jumps - if ship:IsPlayer() and remove_first_if_current then - if route_jumps > 0 and hyperjump_route[1].path:IsSameSystem(Game.system.path) then - sectorView:RemoveRouteItem(1) - end + -- remove the first jump if it's the current system (and enabled to do so) + -- this should be the case if you are following a route and want the route to be + -- updated as you make multiple jumps + if ship:IsPlayer() and remove_first_if_current then + if route_jumps > 0 and hyperjump_route[1].path:IsSameSystem(Game.system.path) then + sectorView:RemoveRouteItem(1) end + end updateHyperspaceTarget() end From 5490eb55ad0bc6ad32836574fa8271a7197568d6 Mon Sep 17 00:00:00 2001 From: Webster Sheets Date: Wed, 9 Sep 2020 11:29:31 -0400 Subject: [PATCH 3/3] Move easing to AnimationCurves.h --- src/AnimationCurves.h | 33 +++++++++++++++++++++++++++++++++ src/SectorView.cpp | 3 ++- src/utils.h | 12 ------------ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/AnimationCurves.h b/src/AnimationCurves.h index fe30f4643a3..f4f451ca811 100644 --- a/src/AnimationCurves.h +++ b/src/AnimationCurves.h @@ -5,6 +5,8 @@ #define ANIMATIONCURVES_H #include "FloatComparison.h" +#include "Pi.h" +#include namespace AnimationCurves { @@ -26,6 +28,37 @@ namespace AnimationCurves { if (newDelta * delta < 0) cur = target; } + // easing from https://github.com/Michaelangel007/easing#tldr-shut-up-and-show-me-the-code + // p should go from 0.0 to 1.0 + inline float InOutQuadraticEasing(float p) + { + float m = p - 1.0; + float t = p * 2.0; + if (t < 1) + return p * t; + else + return 1.0 - m * m * 2.0; + } + + // easing from https://github.com/Michaelangel007/easing#tldr-shut-up-and-show-me-the-code + // p should go from 0.0 to 1.0 + inline float InOutCubicEasing(float p) + { + float m = p - 1.0; + float t = p * 2.0; + if (t < 1) + return p * t * t; + else + return 1.0 + m * m * m * 4.0; + } + + // easing from https://github.com/Michaelangel007/easing#tldr-shut-up-and-show-me-the-code + // p should go from 0.0 to 1.0 + inline float InOutSineEasing(float p) + { + return 0.5 * (1.0 - std::cos(p * M_PI)); + } + } // namespace AnimationCurves #endif diff --git a/src/SectorView.cpp b/src/SectorView.cpp index 39c2c1c67dd..70621637414 100644 --- a/src/SectorView.cpp +++ b/src/SectorView.cpp @@ -3,6 +3,7 @@ #include "SectorView.h" +#include "AnimationCurves.h" #include "Game.h" #include "GameConfig.h" #include "GameSaveError.h" @@ -305,7 +306,7 @@ void SectorView::Draw3D() const vector3f dstPos = Sector::SIZE * vector3f(float(dstPath.sectorX), float(dstPath.sectorY), float(dstPath.sectorZ)) + dstSystem.GetPosition(); // jumpProcess: 0.0f (at source) .. 1.0f (at destination) // smoothing speed at the ends with cubic interpolation - const float jumpProcess = InOutCubicEasing(Pi::game->GetHyperspaceProgress() / Pi::game->GetHyperspaceDuration()); + const float jumpProcess = AnimationCurves::InOutCubicEasing(Pi::game->GetHyperspaceProgress() / Pi::game->GetHyperspaceDuration()); // player location indicator's size of the star, on which it is drawn // so we interpolate it too const float dstStarSize = StarSystem::starScale[dstSystem.GetStarType(0)]; diff --git a/src/utils.h b/src/utils.h index 89d043fb63c..dfe5ae227d5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -269,18 +269,6 @@ static inline Uint32 ceil_pow2(Uint32 v) return v; } -// easing from https://github.com/Michaelangel007/easing#tldr-shut-up-and-show-me-the-code -// p should go from 0.0 to 1.0 -inline float InOutCubicEasing(float p) -{ - float m = p - 1; - float t = p * 2; - if (t < 1) - return p * t * t; - else - return 1 + m * m * m * 4; -} - void hexdump(const unsigned char *buf, int bufsz); #endif /* _UTILS_H */