Skip to content

Commit

Permalink
attempt at making glfw work in main thread (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
liavt committed Jul 23, 2017
1 parent 8173c95 commit 9e1bd76
Show file tree
Hide file tree
Showing 24 changed files with 376 additions and 211 deletions.
4 changes: 2 additions & 2 deletions demos/Cleaning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ int main() {
});
module.addComponent(f);

os::SignalModule sigModule = os::SignalModule();
instance.addModule(sigModule);
os::ErrorModule errModule = os::ErrorModule();
instance.addModule(errModule);

instance.start();
} catch( const std::exception& e ) {
Expand Down
4 changes: 2 additions & 2 deletions demos/ProgressBars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ int main() {
gfx::WindowModule module = gfx::WindowModule(config);
instance.addModule(module);

os::SignalModule sigModule = os::SignalModule();
instance.addModule(sigModule);
os::ErrorModule errModule = os::ErrorModule();
instance.addModule(errModule);

instance.start();
} catch( const std::exception& e ) {
Expand Down
4 changes: 2 additions & 2 deletions demos/Rotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ int main() {
});
module.addComponent(f);

os::SignalModule sigModule = os::SignalModule();
instance.addModule(sigModule);
os::ErrorModule errModule = os::ErrorModule();
instance.addModule(errModule);

mc::Initializer i(instance);
while (instance.isRunning()) {
Expand Down
4 changes: 2 additions & 2 deletions demos/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ int main() {
gfx::WindowModule module = gfx::WindowModule(config);
instance.addModule(module);

os::SignalModule sigModule = os::SignalModule();
instance.addModule(sigModule);
os::ErrorModule errModule = os::ErrorModule();
instance.addModule(errModule);

instance.start();
} catch (const std::exception& e) {
Expand Down
5 changes: 5 additions & 0 deletions include/MACE/Core/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ namespace mc {
*/
MACE__DECLARE_ERROR(InvalidState);

/**
Thrown when the process has no remaining memory available
*/
MACE__DECLARE_ERROR(OutOfMemory);

/**
Thrown when the operating system throws an error
*/
Expand Down
2 changes: 0 additions & 2 deletions include/MACE/Core/Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ namespace mc {
Instance* instance = nullptr;
};



/**
Core class of MACE, managing `Modules`. To start running MACE, simply call `mc::MACE::start(const long long ups)`
<p>
Expand Down
6 changes: 3 additions & 3 deletions include/MACE/Graphics/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ namespace mc {

Enums::Format colorFormat = Enums::Format::BGR;
if (mat.channels() == 1) {
colorFormat = Enums::Format::LUMINANCE;
colorFormat = Enums::Format::RED;
} else if (mat.channels() == 2) {
colorFormat = Enums::Format::LUMINANCE_ALPHA;
colorFormat = Enums::Format::RG;
} else if (mat.channels() == 4) {
colorFormat = Enums::Format::BGRA;
}
Expand Down Expand Up @@ -364,7 +364,7 @@ namespace mc {

bind();

getImage(Enums::Format::BGR, Enums::Type::UNSIGNED_BYTE, img.data);
readPixels(img.data);

cv::flip(img, img, 0);

Expand Down
15 changes: 15 additions & 0 deletions include/MACE/Graphics/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ namespace mc {
Namespace containing classes used for Graphics in MACE.
*/
namespace gfx {
/**
All enum constants used by any graphics class are stored in this namespace for organization.
<p>
To prevent name conflicts, it is recommended to use `using` statements on individual enums,
instead of the entire namespace:
{@code
using namespace mc::gfx::Enums;//not recommended
using mc::gfx::Enums::Format;//recommended
}
@note A MACE header file will never bring any enum from this namespace into `gfx`
*/
namespace Enums {
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions include/MACE/Graphics/OGL/OGL33Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ namespace mc {
OGL33Renderer();
~OGL33Renderer() noexcept override = default;

void onResize(const Size width, const Size height) override;
void onInit(const Size originalWidth, const Size originalHeight) override;
void onResize(gfx::WindowModule* win, const Size width, const Size height) override;
void onInit(gfx::WindowModule* win) override;
void onSetUp(gfx::WindowModule* win) override;
void onTearDown(gfx::WindowModule* win) override;
void onDestroy() override;
Expand Down
6 changes: 3 additions & 3 deletions include/MACE/Graphics/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ namespace mc {
*/
virtual void setRefreshColor(const float r, const float g, const float b, const float a = 1.0f) = 0;

virtual void onResize(const Size width, const Size height) = 0;
virtual void onInit(const Size originalWidth, const Size originalHeight) = 0;
virtual void onResize(gfx::WindowModule* win, const Size width, const Size height) = 0;
virtual void onInit(gfx::WindowModule* win) = 0;
virtual void onSetUp(gfx::WindowModule* win) = 0;
virtual void onTearDown(gfx::WindowModule* win) = 0;
virtual void onDestroy() = 0;
Expand All @@ -192,7 +192,7 @@ namespace mc {
@internal
@opengl
*/
void resize(const Size width, const Size height);
void resize(gfx::WindowModule* win, const Size width, const Size height);

/**
@internal
Expand Down
64 changes: 57 additions & 7 deletions include/MACE/Graphics/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,39 @@ namespace mc {
class GraphicsContext;

/**
Thrown when GLFW (windowing library) throws an error
Thrown when GLFW (windowing library) throws an error and no other `Error` subclass is more specific.
*/
MACE__DECLARE_ERROR(Window);

/**
Thrown when the requested `Renderer` is unavailable on the
system or it does not support a feature that was used, such
as a `Painter` function.
*/
MACE__DECLARE_ERROR(UnsupportedRenderer);

/**
Thrown when a function that requires a renderer context can not
find one.
<p>
Most functions that require a renderer context will be marked in the docs.
<p>
All `Entity` callbacks except for Entity::update are called with a renderer
context.
@remark Due to limitations by graphics APIs, functions that require a `Renderer` context may not throw this error immediately, or the error line and file will not be correct. Using the `gfx::getCurrentWindow()` function will always throw the correct error immediately if no context is found.
@see gfx::getCurrentWindow()
@see gfx::Enums::ContextType
*/
MACE__DECLARE_ERROR(NoRendererContext);

namespace Enums {
/**
Hints to the `WindowModule` which `GraphicsContext` to create.
@remark Some contexes are not available on certain platforms. An `UnsupportedRendererError` is thrown in these cases.
@see NoRendererContextError
*/
enum class ContextType {
AUTOMATIC,
BEST_OGL,
Expand Down Expand Up @@ -98,16 +126,10 @@ namespace mc {
}
#endif//MACE_EXPOSE_GLFW

void create();

const LaunchConfig& getLaunchConfig() const;

void setTitle(const std::string& newTitle);

void init() override;
void update() override;
void destroy() override;

std::string getName() const override;

bool isDestroyed() const;
Expand All @@ -131,6 +153,14 @@ namespace mc {

std::unique_ptr<gfx::GraphicsContext> context;

void create();

void configureThread();

void init() override;
void update() override;
void destroy() override;

void clean() final;

//these are for the Entity inheritence
Expand All @@ -142,9 +172,29 @@ namespace mc {
void threadCallback();
};//WindowModule

/**
Retrieves the `WindowModule` from the `Renderer` context in this thread if exists, throws an error otherwise.
@opengl
@return The `WindowModule` in this thread. Will never be `nullptr`.
@throw NoRendererContext if this thread does not contain a `Renderer` context.
*/
WindowModule* getCurrentWindow();

#ifdef MACE_EXPOSE_GLFW
/**
Grabs the `WindowModule` that owns a `GLFWwindow` if it exists.
<p>
The `GLFWwindow` stores a custom user pointer to the `WindowModule` that created it.
<p>
This can be called from any thread.
@param win `GLFWwindow` to convert. Must not be null. Must have been created by a `WindowModule`
@return The `WindowModule` that owns `win`. Guarenteed to never be `nullptr`
@throw NullPointer if `win` is `nullptr`
@throw NoRendererContext if `win` was not created by a `WindowModule`
@internal
*/
WindowModule* convertGLFWWindowToModule(GLFWwindow* win);
#endif

Expand Down
8 changes: 7 additions & 1 deletion include/MACE/MACE.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ The above copyright notice and this permission notice shall be included in all c
/**
Namespace for everything in MACE. This includes constants, typedefs, tests, classes, and variables.
<p>
Preproccessor definitions will start with `MACE_` and `MACE_INCLUDED` will be defined to equal `true` if MACE is included.
All macros used by MACE are prefixed with `MACE_`. Internal macros which should not be used are prefixed with `MACE__`
<p>
No symbol in `mc` conflict directly with symbols found in `std` (C++11) or any dependency in MACE.
<p>
It is usually safe to type `using namespace mc`. However, be weary that this may create name conflicts with external libraries.
@note No MACE header file will bring all of `mc` into the global namespace.
@todo Add a proper logger
*/
namespace mc {}
Expand Down
47 changes: 29 additions & 18 deletions include/MACE/Utility/Signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,54 @@ The above copyright notice and this permission notice shall be included in all c

#include <MACE/Core/Instance.h>
#include <MACE/Core/Constants.h>
#include <exception>

namespace mc {
namespace os {
class SignalError: public Error {
class MACE__GET_ERROR_NAME(Signal): public Error {
public:
using Error::Error;
};

#define MACE__SIGNAL_ERROR(name) class Signal##name##Error : public SignalError{public: using SignalError::SignalError;}
MACE__SIGNAL_ERROR(Interrupt);
MACE__SIGNAL_ERROR(Terminate);
MACE__SIGNAL_ERROR(IllegalInstruction);
MACE__SIGNAL_ERROR(Abort);
MACE__SIGNAL_ERROR(FloatingPoint);
MACE__SIGNAL_ERROR(SegmentFault);
#define MACE__SIGNAL_ERROR(name) class MACE__GET_ERROR_NAME(name) : public MACE__GET_ERROR_NAME(Signal) {public: using MACE__GET_ERROR_NAME(Signal) :: MACE__GET_ERROR_NAME(Signal) ;}
MACE__SIGNAL_ERROR(SignalInterrupt);
MACE__SIGNAL_ERROR(SignalTerminate);
MACE__SIGNAL_ERROR(SignalIllegalInstruction);
MACE__SIGNAL_ERROR(SignalAbort);
MACE__SIGNAL_ERROR(SignalFloatingPoint);
MACE__SIGNAL_ERROR(SignalSegmentFault);

//the following are only thrown on POSIX systems, but can be caught from any system just in case
MACE__SIGNAL_ERROR(Hangup);
MACE__SIGNAL_ERROR(Kill);
MACE__SIGNAL_ERROR(Stop);
MACE__SIGNAL_ERROR(Alarm);
MACE__SIGNAL_ERROR(TerminalStop);
MACE__SIGNAL_ERROR(TerminalInput);
MACE__SIGNAL_ERROR(TerminalOutput);
MACE__SIGNAL_ERROR(SignalHangup);
MACE__SIGNAL_ERROR(SignalKill);
MACE__SIGNAL_ERROR(SignalStop);
MACE__SIGNAL_ERROR(SignalAlarm);
MACE__SIGNAL_ERROR(SignalTerminalStop);
MACE__SIGNAL_ERROR(SignalTerminalInput);
MACE__SIGNAL_ERROR(SignalTerminalOutput);

#undef MACE__SIGNAL_ERROR//MACE__SIGNAL_ERROR

void signalHandle[[noreturn]](int sig);

class SignalModule: public Module {
public:
virtual void init() override;
virtual void update() override;
virtual void destroy() override;

virtual std::string getName() const override;
};//SignalModule

class ErrorModule: public SignalModule {
public:
void init() override;
void update() override;
void destroy() override;

std::string getName() const override;
};//SignalModule
private:
std::unexpected_handler oldUnexpected;
std::terminate_handler oldTerminate;
};
}//os
}//mc

Expand Down
16 changes: 8 additions & 8 deletions src/Graphics/OGL/OGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,28 @@ namespace mc {
while ((result = glGetError()) != GL_NO_ERROR) {
switch (result) {
case GL_INVALID_ENUM:
throw OpenGLError(std::string(message) + ": GL_INVALID_ENUM: An unacceptable value is specified for an enumerated argument", line, file);
throw MACE__GET_ERROR_NAME(OpenGL) (std::string(message) + ": GL_INVALID_ENUM: An unacceptable value is specified for an enumerated argument", line, file);
break;
case GL_INVALID_VALUE:
throw OpenGLError(std::string(message) + ": GL_INVALID_VALUE: A numeric argument is out of range", line, file);
throw MACE__GET_ERROR_NAME(OpenGL) (std::string(message) + ": GL_INVALID_VALUE: A numeric argument is out of range", line, file);
break;
case GL_INVALID_OPERATION:
throw OpenGLError(std::string(message) + ": GL_INVALID_OPERATION: The specified operation is not allowed in the current state", line, file);
throw MACE__GET_ERROR_NAME(OpenGL) (std::string(message) + ": GL_INVALID_OPERATION: The specified operation is not allowed in the current state", line, file);
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
throw OpenGLError(std::string(message) + ": GL_INVALID_FRAMEBUFFER_OPERATION: The command is trying to render to or read from the framebuffer while the currently bound framebuffer is not framebuffer complete (i.e. the return value from glCheckFramebufferStatus is not GL_FRAMEBUFFER_COMPLETE)", line, file);
throw MACE__GET_ERROR_NAME(OpenGL) (std::string(message) + ": GL_INVALID_FRAMEBUFFER_OPERATION: The command is trying to render to or read from the framebuffer while the currently bound framebuffer is not framebuffer complete (i.e. the return value from glCheckFramebufferStatus is not GL_FRAMEBUFFER_COMPLETE)", line, file);
break;
case GL_STACK_OVERFLOW:
throw OpenGLError(std::string(message) + ": GL_STACK_OVERFLOW: A stack pushing operation cannot be done because it would overflow the limit of that stack's size", line, file);
throw MACE__GET_ERROR_NAME(OpenGL) (std::string(message) + ": GL_STACK_OVERFLOW: A stack pushing operation cannot be done because it would overflow the limit of that stack's size", line, file);
break;
case GL_STACK_UNDERFLOW:
throw OpenGLError(std::string(message) + ": GL_STACK_UNDERFLOW: A stack popping operation cannot be done because the stack is already at its lowest point", line, file);
throw MACE__GET_ERROR_NAME(OpenGL) (std::string(message) + ": GL_STACK_UNDERFLOW: A stack popping operation cannot be done because the stack is already at its lowest point", line, file);
break;
case GL_OUT_OF_MEMORY:
throw OpenGLError(std::string(message) + ": GL_OUT_OF_MEMORY: There is not enough memory left to execute the command", line, file);
throw MACE__GET_ERROR_NAME(OutOfMemory) (std::string(message) + ": GL_OUT_OF_MEMORY: There is not enough memory left to execute the command", line, file);
break;
default:
throw OpenGLError(std::string(message) + ": OpenGL has errored with an error code of " + std::to_string(result), line, file);
throw MACE__GET_ERROR_NAME(OpenGL) (std::string(message) + ": OpenGL has errored with an error code of " + std::to_string(result), line, file);
break;
}
}
Expand Down
Loading

0 comments on commit 9e1bd76

Please sign in to comment.