Skip to content

Commit

Permalink
Adds a new "Context" class that splits the functionality of the "Wind…
Browse files Browse the repository at this point in the history
…ow" class. (#202)

* Adds a new "Context" class that splits the functionality of the "Window" class.

* Removes include tchar.h

* ConsoleSink -> systemConsoleSink

* Includes needed Apple headers in Context.
  • Loading branch information
Kenix3 committed May 7, 2023
1 parent 987590f commit 19958ec
Show file tree
Hide file tree
Showing 37 changed files with 582 additions and 514 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "Utils/Directory.h"


class File
class DiskFile
{
public:
static bool Exists(const fs::path& filePath)
Expand Down
1 change: 1 addition & 0 deletions include/libultraship/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "resource/Archive.h"
#include "resource/ResourceManager.h"
#include "core/Context.h"
#include "core/Window.h"
#include "debug/CrashHandler.h"
#include "menu/Console.h"
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ set(Source_Files__Core
${CMAKE_CURRENT_SOURCE_DIR}/core/Window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/core/ConsoleVariable.h
${CMAKE_CURRENT_SOURCE_DIR}/core/ConsoleVariable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/core/Context.h
${CMAKE_CURRENT_SOURCE_DIR}/core/Context.cpp
)

source_group("Core" FILES ${Source_Files__Core})
Expand Down
5 changes: 4 additions & 1 deletion src/audio/AudioPlayer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "AudioPlayer.h"

namespace Ship {
AudioPlayer::AudioPlayer() : mInitialized(false){};
AudioPlayer::AudioPlayer(std::string backend) : mInitialized(false), mBackend(backend){};

bool AudioPlayer::Init(void) {
mInitialized = DoInit();
Expand All @@ -12,4 +12,7 @@ bool AudioPlayer::IsInitialized(void) {
return mInitialized;
}

std::string AudioPlayer::GetBackend() {
return mBackend;
}
} // namespace Ship
6 changes: 5 additions & 1 deletion src/audio/AudioPlayer.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#pragma once
#include "stdint.h"
#include "stddef.h"
#include <string>

namespace Ship {
class AudioPlayer {

public:
AudioPlayer();
AudioPlayer(std::string backend);

bool Init(void);
virtual int Buffered(void) = 0;
Expand All @@ -19,10 +20,13 @@ class AudioPlayer {
return 44100;
}

std::string GetBackend();

protected:
virtual bool DoInit(void) = 0;

private:
std::string mBackend;
bool mInitialized;
};
} // namespace Ship
Expand Down
2 changes: 1 addition & 1 deletion src/audio/PulseAudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static void PasWriteComplete(void* userData) {
*(bool*)userData = true;
}

PulseAudioPlayer::PulseAudioPlayer() {
PulseAudioPlayer::PulseAudioPlayer(std::string backend) : AudioPlayer(backend) {
}

bool PulseAudioPlayer::DoInit() {
Expand Down
2 changes: 1 addition & 1 deletion src/audio/PulseAudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Ship {
class PulseAudioPlayer : public AudioPlayer {
public:
PulseAudioPlayer();
PulseAudioPlayer(std::string backend);
int Buffered() override;
int GetDesiredBuffered() override;
void Play(const uint8_t* buff, size_t len) override;
Expand Down
2 changes: 1 addition & 1 deletion src/audio/SDLAudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <spdlog/spdlog.h>

namespace Ship {
SDLAudioPlayer::SDLAudioPlayer() {
SDLAudioPlayer::SDLAudioPlayer(std::string backend) : AudioPlayer(backend) {
}

bool SDLAudioPlayer::DoInit(void) {
Expand Down
2 changes: 1 addition & 1 deletion src/audio/SDLAudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Ship {
class SDLAudioPlayer : public AudioPlayer {
public:
SDLAudioPlayer();
SDLAudioPlayer(std::string backend);

int Buffered(void);
int GetDesiredBuffered(void);
Expand Down
3 changes: 2 additions & 1 deletion src/audio/WasapiAudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const IID IID_IAudioClient = __uuidof(IAudioClient);
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);

namespace Ship {
WasapiAudioPlayer::WasapiAudioPlayer() : mRefCount(1), mBufferFrameCount(0), mInitialized(false), mStarted(false){};
WasapiAudioPlayer::WasapiAudioPlayer(std::string backend)
: mRefCount(1), mBufferFrameCount(0), mInitialized(false), mStarted(false), AudioPlayer(backend){};

void WasapiAudioPlayer::ThrowIfFailed(HRESULT res) {
if (FAILED(res)) {
Expand Down
2 changes: 1 addition & 1 deletion src/audio/WasapiAudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using namespace Microsoft::WRL;
namespace Ship {
class WasapiAudioPlayer : public AudioPlayer, public IMMNotificationClient {
public:
WasapiAudioPlayer();
WasapiAudioPlayer(std::string backend);

int Buffered(void);
int GetDesiredBuffered(void);
Expand Down
8 changes: 4 additions & 4 deletions src/controller/ControlDeck.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "ControlDeck.h"

#include "core/Window.h"
#include "core/Context.h"
#include "Controller.h"
#include "DummyController.h"
#include <Utils/StringHelper.h>
Expand All @@ -26,7 +26,7 @@ void ControlDeck::ScanDevices() {
mPortList.clear();
mDevices.clear();

auto controlDeck = Window::GetInstance()->GetControlDeck();
auto controlDeck = Context::GetInstance()->GetControlDeck();

// Always load controllers that need their device indices zero based first because we add some other devices
// afterward.
Expand Down Expand Up @@ -109,7 +109,7 @@ void ControlDeck::WriteToPad(OSContPad* pad) const {
StringHelper::Sprintf("Controllers.%s.Slot_%d." key, device->GetGuid().c_str(), virtualSlot, __VA_ARGS__)

void ControlDeck::LoadSettings() {
std::shared_ptr<Mercury> config = Window::GetInstance()->GetConfig();
std::shared_ptr<Mercury> config = Context::GetInstance()->GetConfig();

for (auto const& val : config->rjson["Controllers"]["Deck"].items()) {
int32_t slot = std::stoi(val.key().substr(5));
Expand Down Expand Up @@ -200,7 +200,7 @@ void ControlDeck::LoadSettings() {
}

void ControlDeck::SaveSettings() {
std::shared_ptr<Mercury> config = Window::GetInstance()->GetConfig();
std::shared_ptr<Mercury> config = Context::GetInstance()->GetConfig();

for (size_t i = 0; i < mPortList.size(); i++) {
std::shared_ptr<Controller> backend = mDevices[mPortList[i]];
Expand Down
3 changes: 2 additions & 1 deletion src/controller/KeyboardController.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "controller/KeyboardController.h"
#include "core/Context.h"

#if __APPLE__
#include <SDL_keyboard.h>
Expand Down Expand Up @@ -71,7 +72,7 @@ const std::string KeyboardController::GetButtonName(int32_t portIndex, int32_t n
if (find == mappings.end()) {
return "Unknown";
}
const char* name = Window::GetInstance()->GetKeyName(find->first);
const char* name = Context::GetInstance()->GetWindow()->GetKeyName(find->first);
return strlen(name) == 0 ? "Unknown" : name;
}

Expand Down
16 changes: 8 additions & 8 deletions src/core/ConsoleVariable.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "ConsoleVariable.h"

#include <functional>
#include <Utils/File.h>
#include <Utils/DiskFile.h>
#include <misc/Utils.h>
#include "Mercury.h"
#include "core/Window.h"
#include "core/Context.h"

namespace Ship {

Expand Down Expand Up @@ -161,13 +161,13 @@ void ConsoleVariable::RegisterColor24(const char* name, Color_RGB8 defaultValue)
}

void ConsoleVariable::ClearVariable(const char* name) {
std::shared_ptr<Mercury> conf = Ship::Window::GetInstance()->GetConfig();
std::shared_ptr<Mercury> conf = Ship::Context::GetInstance()->GetConfig();
mVariables.erase(name);
conf->erase(StringHelper::Sprintf("CVars.%s", name));
}

void ConsoleVariable::Save() {
std::shared_ptr<Mercury> conf = Ship::Window::GetInstance()->GetConfig();
std::shared_ptr<Mercury> conf = Ship::Context::GetInstance()->GetConfig();

for (const auto& variable : mVariables) {
const std::string key = StringHelper::Sprintf("CVars.%s", variable.first.c_str());
Expand Down Expand Up @@ -199,7 +199,7 @@ void ConsoleVariable::Save() {
}

void ConsoleVariable::Load() {
std::shared_ptr<Mercury> conf = Ship::Window::GetInstance()->GetConfig();
std::shared_ptr<Mercury> conf = Ship::Context::GetInstance()->GetConfig();
conf->reload();

LoadFromPath("", conf->rjson["CVars"].items());
Expand Down Expand Up @@ -256,9 +256,9 @@ void ConsoleVariable::LoadFromPath(
}
}
void ConsoleVariable::LoadLegacy() {
auto conf = Ship::Window::GetPathRelativeToAppDirectory("cvars.cfg");
if (File::Exists(conf)) {
const auto lines = File::ReadAllLines(conf);
auto conf = Ship::Context::GetPathRelativeToAppDirectory("cvars.cfg");
if (DiskFile::Exists(conf)) {
const auto lines = DiskFile::ReadAllLines(conf);

for (const std::string& line : lines) {
std::vector<std::string> cfg = StringHelper::Split(line, " = ");
Expand Down
3 changes: 0 additions & 3 deletions src/core/ConsoleVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

namespace Ship {
typedef enum class ConsoleVariableType { Integer, Float, String, Color, Color24 } ConsoleVariableType;
typedef union CVarValue {

} CVarValue;

typedef struct CVar {
const char* Name;
Expand Down

0 comments on commit 19958ec

Please sign in to comment.