Skip to content

Commit

Permalink
close EE-311
Browse files Browse the repository at this point in the history
  • Loading branch information
stohrendorf committed Aug 14, 2021
1 parent 60d7900 commit 1ec6e1e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 8 deletions.
16 changes: 15 additions & 1 deletion script_stubs/engine/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ class TrackInfo:
def __init__(self, soundid: int, tracktype: TrackType, /):
...


class LevelSequenceItem:
...


class Level(LevelSequenceItem):
def __init__(
self, *,
Expand All @@ -75,22 +77,34 @@ class Level(LevelSequenceItem):
):
...


class TitleMenu(Level):
...


class Video(LevelSequenceItem):
def __init__(self, name: str):
...


class Cutscene(LevelSequenceItem):
def __init__(
self, *,
name: str,
track: TR1TrackId,
camera_rot: float,
weapon_swap: bool = False,
flip_rooms: bool=False,
flip_rooms: bool = False,
camera_pos_x: Optional[int] = None,
camera_pos_z: Optional[int] = None,
):
...


class SplashScreen(LevelSequenceItem):
def __init__(
self, *,
path: str,
duration_seconds: int,
):
...
9 changes: 8 additions & 1 deletion scripts/tr1/level_sequence.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from engine import TR1TrackId, TR1ItemId, Video, Cutscene, Level, TitleMenu, WeaponType
from engine import TR1TrackId, TR1ItemId, Video, Cutscene, Level, TitleMenu, SplashScreen, WeaponType

title_menu = TitleMenu(
name="TITLE",
Expand Down Expand Up @@ -375,4 +375,11 @@
},
),
Video("END.RPL"),
*(
SplashScreen(
path=f"{name}.PCX",
duration_seconds=15,
)
for name in ("END", "CRED1", "CRED2", "CRED3")
),
]
15 changes: 9 additions & 6 deletions src/engine/presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ void Presenter::playVideo(const std::filesystem::path& path)
m_soundEngine->getSoLoud(),
[&](const std::shared_ptr<gl::TextureHandle<gl::Texture2D<gl::SRGBA8>>>& textureHandle)
{
glfwPollEvents();
m_window->updateWindowSize();
if(m_window->isMinimized())
if(update())
return true;

m_renderer->getCamera()->setScreenSize(m_window->getViewport());
Expand Down Expand Up @@ -383,9 +381,7 @@ void Presenter::scaleSplashImage()

void Presenter::drawLoadingScreen(const std::string& state)
{
glfwPollEvents();
m_window->updateWindowSize();
if(m_window->isMinimized())
if(update())
return;

if(m_screenOverlay == nullptr)
Expand Down Expand Up @@ -524,4 +520,11 @@ void Presenter::disableScreenOverlay()
{
m_screenOverlay.reset();
}

bool Presenter::update()
{
glfwPollEvents();
m_window->updateWindowSize();
return m_window->isMinimized();
}
} // namespace engine
2 changes: 2 additions & 0 deletions src/engine/presenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class Presenter final

void disableScreenOverlay();

bool update();

private:
const std::unique_ptr<gl::Window> m_window;

Expand Down
5 changes: 5 additions & 0 deletions src/engine/py_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ PYBIND11_EMBEDDED_MODULE(engine, m)
py::arg("drop_inventory") = py::set{},
py::arg("track") = std::nullopt);

py::class_<engine::script::SplashScreen,
engine::script::LevelSequenceItem,
std::shared_ptr<engine::script::SplashScreen>>(m, "SplashScreen", py::is_final{})
.def(py::init<std::string, int>(), py::kw_only{}, py::arg("path"), py::arg("duration_seconds"));

py::enum_<engine::objects::TriggerState>(m, "ActivationState")
.value("INACTIVE", engine::objects::TriggerState::Inactive)
.value("DEACTIVATED", engine::objects::TriggerState::Deactivated)
Expand Down
71 changes: 71 additions & 0 deletions src/engine/script/reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
#include "engine/objects/modelobject.h"
#include "engine/player.h"
#include "engine/presenter.h"
#include "engine/throttler.h"
#include "engine/world/world.h"
#include "loader/file/level/level.h"
#include "render/scene/materialmanager.h"
#include "render/scene/mesh.h"
#include "render/scene/rendercontext.h"
#include "render/scene/renderer.h"

#include <boost/range/adaptors.hpp>
#include <gl/framebuffer.h>
#include <gl/texture2d.h>

namespace engine::script
{
Expand Down Expand Up @@ -164,4 +171,68 @@ TitleMenu::TitleMenu(const std::string& name,
: Level{name, 0, useAlternativeLara, titles, itemTitles, inventory, dropInventory, track, false, WeaponType::None}
{
}

SplashScreen::SplashScreen(std::string path, int durationSeconds)
: m_path{path}
, m_durationSeconds{durationSeconds}
{
Expects(m_durationSeconds > 0);
}

SplashScreen::~SplashScreen() = default;

std::pair<RunResult, std::optional<size_t>> SplashScreen::run(Engine& engine, const std::shared_ptr<Player>& /*player*/)
{
const auto end = std::chrono::high_resolution_clock::now() + std::chrono::seconds{m_durationSeconds};
Throttler throttler{};

glm::ivec2 size{-1, -1};
auto image = std::make_shared<gl::TextureHandle<gl::Texture2D<gl::SRGBA8>>>(
gl::CImgWrapper{util::ensureFileExists(engine.getRootPath() / "data" / "tr1" / "DATA" / m_path)}.toTexture());
std::shared_ptr<render::scene::Mesh> mesh;

render::scene::RenderContext context{render::scene::RenderMode::Full, std::nullopt};
while(std::chrono::high_resolution_clock::now() < end)
{
if(engine.getPresenter().update() || engine.getPresenter().shouldClose())
break;

engine.getPresenter().getInputHandler().update();
if(engine.getPresenter().getInputHandler().hasDebouncedAction(hid::Action::Menu))
break;

if(size != engine.getPresenter().getViewport())
{
size = engine.getPresenter().getViewport();

// scale splash image so that its aspect ratio is preserved, but is completely visible
const auto targetSize = glm::vec2{size};
const auto sourceSize = glm::vec2{image->getTexture()->size()};
const float splashScale = std::min(targetSize.x / sourceSize.x, targetSize.y / sourceSize.y);

auto scaledSourceSize = sourceSize * splashScale;
auto sourceOffset = (targetSize - scaledSourceSize) / 2.0f;
mesh = render::scene::createScreenQuad(
sourceOffset, scaledSourceSize, engine.getPresenter().getMaterialManager()->getBackdrop(), m_path.string());
mesh->bind(
"u_input",
[&image](const render::scene::Node& /*node*/, const render::scene::Mesh& /*mesh*/, gl::Uniform& uniform)
{ uniform.set(image); });
mesh->bind(
"u_screenSize",
[targetSize](const render::scene::Node& /*node*/, const render::scene::Mesh& /*mesh*/, gl::Uniform& uniform)
{ uniform.set(targetSize); });
}

gl::Framebuffer::unbindAll();
engine.getPresenter().getRenderer().clear(
gl::api::ClearBufferMask::ColorBufferBit | gl::api::ClearBufferMask::DepthBufferBit, {0, 0, 0, 0}, 1);
mesh->render(context);
engine.getPresenter().swapBuffers();

throttler.wait();
}

return {RunResult::NextLevel, std::nullopt};
}
} // namespace engine::script
27 changes: 27 additions & 0 deletions src/engine/script/reflection.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <boost/log/trivial.hpp>
#include <filesystem>
#include <gl/pixel.h>
#include <gl/soglb_fwd.h>
#include <unordered_set>

namespace engine
Expand All @@ -19,6 +21,12 @@ class Engine;
class Player;
} // namespace engine

namespace render::scene
{
class Mesh;
class MaterialManager;
} // namespace render::scene

namespace engine::script
{
struct ObjectInfo
Expand Down Expand Up @@ -195,4 +203,23 @@ class Cutscene : public LevelSequenceItem
return false;
}
};

class SplashScreen : public LevelSequenceItem
{
private:
const std::filesystem::path m_path;
const int m_durationSeconds;

public:
explicit SplashScreen(std::string path, int durationSeconds);

~SplashScreen() override;

std::pair<RunResult, std::optional<size_t>> run(Engine& engine, const std::shared_ptr<Player>& player) override;

[[nodiscard]] bool isLevel(const std::filesystem::path& /*path*/) const override
{
return false;
}
};
} // namespace engine::script

0 comments on commit 1ec6e1e

Please sign in to comment.