mirror of
https://github.com/NoelFB/blah.git
synced 2025-09-13 13:24:26 +08:00
made std::shared_ptr and std::functional optional
This commit is contained in:
23
src/app.cpp
23
src/app.cpp
@ -112,18 +112,12 @@ namespace
|
||||
}
|
||||
};
|
||||
|
||||
BackBuffer app_backbuffer;
|
||||
TargetRef app_backbuffer_ref = TargetRef(&app_backbuffer, [](BackBuffer*) {});
|
||||
TargetRef app_backbuffer;
|
||||
}
|
||||
|
||||
bool App::run(const Config* c)
|
||||
{
|
||||
BLAH_ASSERT(!app_is_running, "The Application is already running");
|
||||
BLAH_ASSERT(c != nullptr, "The Application requires a valid Config");
|
||||
BLAH_ASSERT(c->name != nullptr, "The Application Name cannot be null");
|
||||
BLAH_ASSERT(c->width > 0 && c->height > 0, "The Width and Height must be larget than 0");
|
||||
BLAH_ASSERT(c->max_updates > 0, "Max Updates must be >= 1");
|
||||
BLAH_ASSERT(c->target_framerate > 0, "Target Framerate must be >= 1");
|
||||
|
||||
// copy config over
|
||||
app_config = *c;
|
||||
@ -136,9 +130,19 @@ bool App::run(const Config* c)
|
||||
if (app_config.renderer_type == RendererType::None)
|
||||
app_config.renderer_type = Renderer::default_type();
|
||||
|
||||
// exit out if setup is wrong
|
||||
BLAH_ASSERT(c != nullptr, "The Application requires a valid Config");
|
||||
BLAH_ASSERT(c->name != nullptr, "The Application Name cannot be null");
|
||||
BLAH_ASSERT(c->width > 0 && c->height > 0, "The Width and Height must be larget than 0");
|
||||
BLAH_ASSERT(c->max_updates > 0, "Max Updates must be >= 1");
|
||||
BLAH_ASSERT(c->target_framerate > 0, "Target Framerate must be >= 1");
|
||||
if (app_is_running || c == nullptr || c->width <= 0 || c->height <= 0 || c->max_updates <= 0 || c->target_framerate <= 0)
|
||||
return false;
|
||||
|
||||
// default values
|
||||
app_is_running = true;
|
||||
app_is_exiting = false;
|
||||
app_backbuffer = TargetRef(new BackBuffer());
|
||||
|
||||
// initialize the system
|
||||
if (!Platform::init(app_config))
|
||||
@ -207,6 +211,7 @@ bool App::run(const Config* c)
|
||||
// clear static state
|
||||
app_is_running = false;
|
||||
app_is_exiting = false;
|
||||
app_backbuffer = TargetRef();
|
||||
|
||||
Time::ticks = 0;
|
||||
Time::seconds = 0;
|
||||
@ -286,7 +291,7 @@ Point App::get_backbuffer_size()
|
||||
{
|
||||
BLAH_ASSERT_RUNNING();
|
||||
if (Renderer::instance)
|
||||
return Point(app_backbuffer.width(), app_backbuffer.height());
|
||||
return Point(app_backbuffer->width(), app_backbuffer->height());
|
||||
return Point(0, 0);
|
||||
}
|
||||
|
||||
@ -318,7 +323,7 @@ const RendererFeatures& App::renderer()
|
||||
const TargetRef& App::backbuffer()
|
||||
{
|
||||
BLAH_ASSERT_RUNNING();
|
||||
return app_backbuffer_ref;
|
||||
return app_backbuffer;
|
||||
}
|
||||
|
||||
void System::open_url(const char* url)
|
||||
|
@ -7,10 +7,10 @@ using namespace Blah;
|
||||
|
||||
void Log::info(const char* format, ...)
|
||||
{
|
||||
char msg[BLAH_MESSAGE];
|
||||
char msg[max_length];
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
vsnprintf(msg, sizeof(char) * BLAH_MESSAGE, format, ap);
|
||||
vsnprintf(msg, sizeof(char) * max_length, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (App::config().on_log)
|
||||
@ -25,10 +25,10 @@ void Log::info(const char* format, ...)
|
||||
|
||||
void Log::warn(const char* format, ...)
|
||||
{
|
||||
char msg[BLAH_MESSAGE];
|
||||
char msg[max_length];
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
vsnprintf(msg, sizeof(char) * BLAH_MESSAGE, format, ap);
|
||||
vsnprintf(msg, sizeof(char) * max_length, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (App::config().on_log)
|
||||
@ -43,10 +43,10 @@ void Log::warn(const char* format, ...)
|
||||
|
||||
void Log::error(const char* format, ...)
|
||||
{
|
||||
char msg[BLAH_MESSAGE];
|
||||
char msg[max_length];
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
vsnprintf(msg, sizeof(char) * BLAH_MESSAGE, format, ap);
|
||||
vsnprintf(msg, sizeof(char) * max_length, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (App::config().on_log)
|
||||
|
@ -13,10 +13,9 @@ MeshRef Mesh::create()
|
||||
return MeshRef();
|
||||
}
|
||||
|
||||
VertexFormat::VertexFormat(std::initializer_list<VertexAttribute> attributes, int stride)
|
||||
VertexFormat::VertexFormat(const StackVector<VertexAttribute, 16>& attr, int stride)
|
||||
{
|
||||
for (auto& it : attributes)
|
||||
this->attributes.push_back(it);
|
||||
attributes = attr;
|
||||
|
||||
if (stride <= 0)
|
||||
{
|
||||
|
@ -160,9 +160,9 @@ void Packer::pack()
|
||||
sources[index++] = &m_entries[i];
|
||||
|
||||
std::sort(sources.begin(), sources.end(), [](Packer::Entry* a, Packer::Entry* b)
|
||||
{
|
||||
return a->packed.w * a->packed.h > b->packed.w * b->packed.h;
|
||||
});
|
||||
{
|
||||
return a->packed.w * a->packed.h > b->packed.w * b->packed.h;
|
||||
});
|
||||
}
|
||||
|
||||
// make sure the largest isn't too large
|
||||
|
@ -14,9 +14,9 @@ namespace
|
||||
{
|
||||
InputState g_empty_state;
|
||||
ControllerState g_empty_controller;
|
||||
Vector<WeakRef<ButtonBinding>> g_buttons;
|
||||
Vector<WeakRef<AxisBinding>> g_axes;
|
||||
Vector<WeakRef<StickBinding>> g_sticks;
|
||||
Vector<Ref<ButtonBinding>> g_buttons;
|
||||
Vector<Ref<AxisBinding>> g_axes;
|
||||
Vector<Ref<StickBinding>> g_sticks;
|
||||
String g_clipboard;
|
||||
}
|
||||
|
||||
@ -82,40 +82,42 @@ void Input::update_bindings()
|
||||
{
|
||||
for (int i = 0; i < g_buttons.size(); i++)
|
||||
{
|
||||
if (g_buttons[i].use_count() <= 0)
|
||||
// we're the only user, so remove it
|
||||
if (g_buttons[i].use_count() <= 1)
|
||||
{
|
||||
g_buttons.erase(i);
|
||||
i--;
|
||||
}
|
||||
else if (auto binding = g_buttons[i].lock())
|
||||
// keep updating
|
||||
else
|
||||
{
|
||||
binding->update();
|
||||
g_buttons[i]->update();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < g_axes.size(); i++)
|
||||
{
|
||||
if (g_axes[i].use_count() <= 0)
|
||||
if (g_axes[i].use_count() <= 1)
|
||||
{
|
||||
g_axes.erase(i);
|
||||
i--;
|
||||
}
|
||||
else if (auto binding = g_axes[i].lock())
|
||||
else
|
||||
{
|
||||
binding->update();
|
||||
g_axes[i]->update();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < g_sticks.size(); i++)
|
||||
{
|
||||
if (g_sticks[i].use_count() <= 0)
|
||||
if (g_sticks[i].use_count() <= 1)
|
||||
{
|
||||
g_sticks.erase(i);
|
||||
i--;
|
||||
}
|
||||
else if (auto binding = g_sticks[i].lock())
|
||||
else
|
||||
{
|
||||
binding->update();
|
||||
g_sticks[i]->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -397,22 +399,22 @@ void Input::set_clipboard(const String& text)
|
||||
|
||||
ButtonBindingRef Input::register_binding(const ButtonBinding& binding)
|
||||
{
|
||||
auto result = std::make_shared<ButtonBinding>(binding);
|
||||
g_buttons.push_back(WeakRef<ButtonBinding>(result));
|
||||
auto result = Ref<ButtonBinding>(new ButtonBinding(binding));
|
||||
g_buttons.push_back(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
AxisBindingRef Input::register_binding(const AxisBinding& binding)
|
||||
{
|
||||
auto result = std::make_shared<AxisBinding>(binding);
|
||||
g_axes.push_back(WeakRef<AxisBinding>(result));
|
||||
auto result = Ref<AxisBinding>(new AxisBinding(binding));
|
||||
g_axes.push_back(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
StickBindingRef Input::register_binding(const StickBinding& binding)
|
||||
{
|
||||
auto result = std::make_shared<StickBinding>(binding);
|
||||
g_sticks.push_back(WeakRef<StickBinding>(result));
|
||||
auto result = Ref<StickBinding>(new StickBinding(binding));
|
||||
g_sticks.push_back(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -11,16 +11,16 @@
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
// for File Reading/Writing
|
||||
// for File Reading / Writing
|
||||
#include <filesystem>
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
// Windows requires a few extra includes
|
||||
#if _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <windows.h> // for the following includes
|
||||
#include <winuser.h> // for SetProcessDPIAware
|
||||
#include <shellapi.h> // for file explore
|
||||
#include <SDL_syswm.h>
|
||||
#include <shellapi.h> // for ShellExecute for dir_explore
|
||||
#include <SDL_syswm.h> // for SDL_SysWMinfo for D3D11
|
||||
#endif
|
||||
|
||||
// Macro defined by X11 conflicts with MouseButton enum
|
||||
@ -40,50 +40,19 @@ namespace Blah
|
||||
} g_platform;
|
||||
|
||||
// Blah SDL2 File
|
||||
class SDL2File : public File
|
||||
struct SDL2File : public File
|
||||
{
|
||||
private:
|
||||
SDL_RWops* m_handle;
|
||||
|
||||
public:
|
||||
SDL2File(SDL_RWops* handle)
|
||||
{
|
||||
m_handle = handle;
|
||||
}
|
||||
|
||||
~SDL2File()
|
||||
{
|
||||
if (m_handle)
|
||||
SDL_RWclose(m_handle);
|
||||
}
|
||||
|
||||
size_t length() override
|
||||
{
|
||||
return SDL_RWsize(m_handle);
|
||||
}
|
||||
|
||||
size_t position() override
|
||||
{
|
||||
return SDL_RWtell(m_handle);
|
||||
}
|
||||
|
||||
size_t seek(size_t position) override
|
||||
{
|
||||
return SDL_RWseek(m_handle, position, RW_SEEK_SET);
|
||||
}
|
||||
|
||||
size_t read(unsigned char* buffer, size_t length) override
|
||||
{
|
||||
return SDL_RWread(m_handle, buffer, sizeof(char), length);
|
||||
}
|
||||
|
||||
size_t write(const unsigned char* buffer, size_t length) override
|
||||
{
|
||||
return SDL_RWwrite(m_handle, buffer, sizeof(char), length);
|
||||
}
|
||||
SDL_RWops* handle;
|
||||
SDL2File(SDL_RWops* handle) : handle(handle) { }
|
||||
~SDL2File() { if (handle) SDL_RWclose(handle); }
|
||||
size_t length() override { return SDL_RWsize(handle); }
|
||||
size_t position() override { return SDL_RWtell(handle); }
|
||||
size_t seek(size_t position) override { return SDL_RWseek(handle, position, RW_SEEK_SET); }
|
||||
size_t read(unsigned char* buffer, size_t length) override { return SDL_RWread(handle, buffer, sizeof(char), length); }
|
||||
size_t write(const unsigned char* buffer, size_t length) override { return SDL_RWwrite(handle, buffer, sizeof(char), length); }
|
||||
};
|
||||
|
||||
void sdl_log(void* userdata, int category, SDL_LogPriority priority, const char* message)
|
||||
void blah_sdl_log(void* userdata, int category, SDL_LogPriority priority, const char* message)
|
||||
{
|
||||
if (priority <= SDL_LOG_PRIORITY_INFO)
|
||||
Log::info(message);
|
||||
@ -93,7 +62,7 @@ namespace Blah
|
||||
Log::error(message);
|
||||
}
|
||||
|
||||
int sdl_find_joystick_index(SDL_Joystick** joysticks, SDL_JoystickID instance_id)
|
||||
int blah_sdl_find_joystick_index(SDL_Joystick** joysticks, SDL_JoystickID instance_id)
|
||||
{
|
||||
for (int i = 0; i < Input::max_controllers; i++)
|
||||
if (joysticks[i] != nullptr && SDL_JoystickInstanceID(joysticks[i]) == instance_id)
|
||||
@ -101,7 +70,7 @@ namespace Blah
|
||||
return -1;
|
||||
}
|
||||
|
||||
int sdl_find_gamepad_index(SDL_GameController** gamepads, SDL_JoystickID instance_id)
|
||||
int blah_sdl_find_gamepad_index(SDL_GameController** gamepads, SDL_JoystickID instance_id)
|
||||
{
|
||||
for (int i = 0; i < Input::max_controllers; i++)
|
||||
{
|
||||
@ -131,7 +100,7 @@ bool Platform::init(const Config& config)
|
||||
// TODO:
|
||||
// control this via some kind of config flag
|
||||
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
|
||||
SDL_LogSetOutputFunction(sdl_log, nullptr);
|
||||
SDL_LogSetOutputFunction(blah_sdl_log, nullptr);
|
||||
|
||||
// Get SDL version
|
||||
SDL_version version;
|
||||
@ -331,7 +300,7 @@ void Platform::update(InputState& state)
|
||||
}
|
||||
else if (event.type == SDL_JOYDEVICEREMOVED)
|
||||
{
|
||||
auto index = sdl_find_joystick_index(g_platform.joysticks, event.jdevice.which);
|
||||
auto index = blah_sdl_find_joystick_index(g_platform.joysticks, event.jdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
if (SDL_IsGameController(index) == SDL_FALSE)
|
||||
@ -343,7 +312,7 @@ void Platform::update(InputState& state)
|
||||
}
|
||||
else if (event.type == SDL_JOYBUTTONDOWN)
|
||||
{
|
||||
auto index = sdl_find_joystick_index(g_platform.joysticks, event.jdevice.which);
|
||||
auto index = blah_sdl_find_joystick_index(g_platform.joysticks, event.jdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
if (SDL_IsGameController(index) == SDL_FALSE)
|
||||
@ -352,7 +321,7 @@ void Platform::update(InputState& state)
|
||||
}
|
||||
else if (event.type == SDL_JOYBUTTONUP)
|
||||
{
|
||||
auto index = sdl_find_joystick_index(g_platform.joysticks, event.jdevice.which);
|
||||
auto index = blah_sdl_find_joystick_index(g_platform.joysticks, event.jdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
if (SDL_IsGameController(index) == SDL_FALSE)
|
||||
@ -361,7 +330,7 @@ void Platform::update(InputState& state)
|
||||
}
|
||||
else if (event.type == SDL_JOYAXISMOTION)
|
||||
{
|
||||
auto index = sdl_find_joystick_index(g_platform.joysticks, event.jdevice.which);
|
||||
auto index = blah_sdl_find_joystick_index(g_platform.joysticks, event.jdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
if (SDL_IsGameController(index) == SDL_FALSE)
|
||||
@ -392,7 +361,7 @@ void Platform::update(InputState& state)
|
||||
}
|
||||
else if (event.type == SDL_CONTROLLERDEVICEREMOVED)
|
||||
{
|
||||
auto index = sdl_find_gamepad_index(g_platform.gamepads, event.cdevice.which);
|
||||
auto index = blah_sdl_find_gamepad_index(g_platform.gamepads, event.cdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
state.controllers[index].on_disconnect();
|
||||
@ -401,7 +370,7 @@ void Platform::update(InputState& state)
|
||||
}
|
||||
else if (event.type == SDL_CONTROLLERBUTTONDOWN)
|
||||
{
|
||||
auto index = sdl_find_gamepad_index(g_platform.gamepads, event.cdevice.which);
|
||||
auto index = blah_sdl_find_gamepad_index(g_platform.gamepads, event.cdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
Button button = Button::None;
|
||||
@ -413,7 +382,7 @@ void Platform::update(InputState& state)
|
||||
}
|
||||
else if (event.type == SDL_CONTROLLERBUTTONUP)
|
||||
{
|
||||
auto index = sdl_find_gamepad_index(g_platform.gamepads, event.cdevice.which);
|
||||
auto index = blah_sdl_find_gamepad_index(g_platform.gamepads, event.cdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
Button button = Button::None;
|
||||
@ -425,7 +394,7 @@ void Platform::update(InputState& state)
|
||||
}
|
||||
else if (event.type == SDL_CONTROLLERAXISMOTION)
|
||||
{
|
||||
auto index = sdl_find_gamepad_index(g_platform.gamepads, event.cdevice.which);
|
||||
auto index = blah_sdl_find_gamepad_index(g_platform.gamepads, event.cdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
Axis axis = Axis::None;
|
||||
@ -597,41 +566,41 @@ FileRef Platform::file_open(const char* path, FileMode mode)
|
||||
|
||||
bool Platform::file_exists(const char* path)
|
||||
{
|
||||
return fs::is_regular_file(path);
|
||||
return std::filesystem::is_regular_file(path);
|
||||
}
|
||||
|
||||
bool Platform::file_delete(const char* path)
|
||||
{
|
||||
return fs::remove(path);
|
||||
return std::filesystem::remove(path);
|
||||
}
|
||||
|
||||
bool Platform::dir_create(const char* path)
|
||||
{
|
||||
return fs::create_directories(path);
|
||||
return std::filesystem::create_directories(path);
|
||||
}
|
||||
|
||||
bool Platform::dir_exists(const char* path)
|
||||
{
|
||||
return fs::is_directory(path);
|
||||
return std::filesystem::is_directory(path);
|
||||
}
|
||||
|
||||
bool Platform::dir_delete(const char* path)
|
||||
{
|
||||
return fs::remove_all(path) > 0;
|
||||
return std::filesystem::remove_all(path) > 0;
|
||||
}
|
||||
|
||||
void Platform::dir_enumerate(Vector<FilePath>& list, const char* path, bool recursive)
|
||||
{
|
||||
if (fs::is_directory(path))
|
||||
if (std::filesystem::is_directory(path))
|
||||
{
|
||||
if (recursive)
|
||||
{
|
||||
for (auto& p : fs::recursive_directory_iterator(path))
|
||||
for (auto& p : std::filesystem::recursive_directory_iterator(path))
|
||||
list.emplace_back(p.path().string().c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto& p : fs::directory_iterator(path))
|
||||
for (auto& p : std::filesystem::directory_iterator(path))
|
||||
list.emplace_back(p.path().string().c_str());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user