Blaze/Blaze/AppState.cpp

88 lines
2.3 KiB
C++

#include "AppState.h"
#include <SDL3/SDL_log.h>
#include "EntityManager.h"
#include "GlobalMemory.h"
#include "MiscData.h"
#include "RenderDevice.h"
#include "TextureManager.h"
bool AppState::isInit() const
{
return window and renderDevice and renderDevice->isInit();
}
void AppState::destroy()
{
if ( !isInit() ) return;
renderDevice->waitIdle();
Take( miscData )->destroy( *renderDevice );
Take( entityManager )->destroy();
Take( renderDevice )->destroy();
SDL_DestroyWindow( Take( window ) );
}
AppState::AppState( SDL_Window* window, RenderDevice* renderDevice, EntityManager* entityManager, MiscData* miscData )
: window{ window }
, renderDevice{ renderDevice }
, entityManager{ entityManager }
, miscData{ miscData }
, sprintfBuffer{ 0 }
{}
AppState* AppState_Create( GlobalMemory* memory, uint32_t const width, uint32_t const height )
{
SDL_Window* window =
SDL_CreateWindow( "Blaze Test", static_cast<int>( width ), static_cast<int>( height ), SDL_WINDOW_VULKAN );
if ( !window )
{
SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "%s", SDL_GetError() );
return nullptr;
}
RenderDevice* renderDevice = RenderDevice_Create( memory, { .window = window } );
if ( !renderDevice or !renderDevice->isInit() )
{
SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "RenderDevice failed to init" );
SDL_DestroyWindow( window );
return nullptr;
}
EntityManager* entityManager = EntityManager_Create( memory, renderDevice, 1000 );
if ( !entityManager )
{
SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "EntityManager failed to init" );
renderDevice->destroy();
SDL_DestroyWindow( window );
return nullptr;
}
auto* miscDataAllocation = memory->allocate( sizeof( MiscData ) );
MiscData* miscData = new ( miscDataAllocation ) MiscData{};
if ( !miscData->init( *renderDevice ) )
{
SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "MiscData failed to init" );
entityManager->destroy();
renderDevice->destroy();
SDL_DestroyWindow( window );
return nullptr;
}
auto* allocation = memory->allocate( sizeof( AppState ) );
AppState* appState = new ( allocation ) AppState{ window, renderDevice, entityManager, miscData };
return appState;
}
AppState::~AppState()
{
ASSERT( !isInit() );
}