Finish lecture 82: Implement the World class

Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
2025-09-02 08:15:18 -05:00
parent 9ca8cd1e25
commit 7d2cd86fd4
15 changed files with 296 additions and 9 deletions

View File

@@ -0,0 +1,8 @@
#include "EntryPoint.h"
#include "framework/Application.h"
int main() {
ly::Application* app = GetApplication();
app->Run();
delete app;
}

View File

@@ -0,0 +1,65 @@
#include "framework/Application.h"
#include "framework/Core.h"
#include "framework/World.h"
namespace ly {
Application::Application():
mWindow{sf::VideoMode(1440,1024), "LightYears"},
mTargetFrameRate{60.0f},
mTickClock{},
currentWorld{nullptr}
{
}
void Application::Run()
{
mTickClock.restart();
float accumulatedTime = 0.0f;
float targetDeltaTime = 1.f / mTargetFrameRate;
while (mWindow.isOpen()) {
sf::Event windowEvent;
while (mWindow.pollEvent(windowEvent)) {
if (windowEvent.type == sf::Event::EventType::Closed) {
mWindow.close();
}
}
float frameDeltaTime = mTickClock.restart().asSeconds();
accumulatedTime += frameDeltaTime;
while (accumulatedTime > targetDeltaTime) {
accumulatedTime -= targetDeltaTime;
TickInternal(targetDeltaTime);
RenderInternal();
}
}
}
void Application::TickInternal(float deltaTime)
{
Tick(deltaTime);
if (currentWorld) {
currentWorld->TickInternal(deltaTime);
}
}
void Application::Tick(float deltaTime)
{
}
void Application::RenderInternal()
{
mWindow.clear();
Render();
mWindow.display();
}
void Application::Render()
{
sf::RectangleShape rect{ sf::Vector2f{100,100} };
rect.setFillColor(sf::Color::Green);
rect.setOrigin(50, 50);
rect.setPosition(mWindow.getSize().x / 2, mWindow.getSize().y / 2);
mWindow.draw(rect);
}
}

View File

View File

@@ -0,0 +1,36 @@
#include "framework/World.h"
#include "framework/Core.h"
namespace ly {
World::World(Application* owningApp) : mOwningApp{ owningApp }, mBeganPlay{ false } {
}
void World::BeginPlayInternal() {
if (!mBeganPlay) {
mBeganPlay = true;
BeginPlay();
}
}
World::~World()
{
}
void World::TickInternal(float deltaTime)
{
Tick(deltaTime);
}
void World::BeginPlay()
{
LOG("Began Play");
}
void World::Tick(float deltaTime)
{
LOG("Ticking at frame rate: %f", 1.f / deltaTime);
}
}