Skip to content

Commit

Permalink
Merge pull request #244 from kimkulling/kimkulling/fix_2d_coords
Browse files Browse the repository at this point in the history
Renderbackend: Rework Canvas 2D-Rendering
  • Loading branch information
kimkulling authored Nov 13, 2024
2 parents a690096 + 2e927c1 commit dfca9b8
Show file tree
Hide file tree
Showing 53 changed files with 1,327 additions and 588 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: |
sudo add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu `lsb_release -sc` main universe restricted multiverse"
sudo apt-get update -y -qq
sudo apt-get install libsdl2-dev libglm-dev libglew-dev libgtest-dev
sudo apt-get install libsdl2-dev libglm-dev libglew-dev libgtest-dev googletest
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
Expand Down
2 changes: 1 addition & 1 deletion contrib/cppcore
26 changes: 21 additions & 5 deletions samples/02_Demo2D/Demo2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static constexpr c8 Tag[] = "ModelLoadingApp";
//-------------------------------------------------------------------------------------------------
class Demo2DApp : public App::AppBase {
TransformMatrixBlock mTransformMatrix;
CanvasRenderer *mCanvasRenderer;
CanvasRenderer *mCanvasRenderer;

public:
Demo2DApp(int argc, char *argv[]) :
Expand All @@ -66,7 +66,7 @@ class Demo2DApp : public App::AppBase {

protected:
bool onCreate() override {
Properties::Settings *baseSettings(AppBase::getSettings());
Properties::Settings *baseSettings = AppBase::getSettings();
if (baseSettings == nullptr) {
return false;
}
Expand All @@ -75,8 +75,9 @@ class Demo2DApp : public App::AppBase {
if (!AppBase::onCreate()) {
return false;
}

mCanvasRenderer = new CanvasRenderer(2, 0, 0, 1024, 768);

mCanvasRenderer = AppBase::getCanvasRenderer();

mCanvasRenderer->selectLayer(0);
const Color4 Red(1, 0, 0, 0);
mCanvasRenderer->setColor(Red);
Expand Down Expand Up @@ -109,6 +110,8 @@ class Demo2DApp : public App::AppBase {

mCanvasRenderer->drawRect(100, 1000, 110, 124, true);

mCanvasRenderer->drawText(300, 100, "Test");

return true;
}

Expand All @@ -134,4 +137,17 @@ class Demo2DApp : public App::AppBase {
}
};

OSRE_MAIN(Demo2DApp)
int main(int argc, char *argv[]) {
Demo2DApp myApp(argc, argv);
if (!myApp.create()) {
return 1;
}

while (myApp.handleEvents()) {
myApp.update();
myApp.requestNextFrame();
}
myApp.destroy();

return 0;
}
5 changes: 4 additions & 1 deletion src/Editor/src/Gui/UIElementsWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ bool UIElementsWin32::init() {

// needed for the RichEdit control in the about/help dialog
lib = LoadLibrary("riched20.dll");
osre_assert(lib != nullptr);
if (lib == nullptr) {
osre_assert(lib != nullptr);
return false;
}

// load windows common controls library to get XP style
InitCommonControls();
Expand Down
7 changes: 4 additions & 3 deletions src/Editor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ int main(int argc, char *argv[]) {
std::cout << "Editor version 0.1\n";

OsreEdApp osreApp(argc, argv);
if (!osreApp.initWindow(100, 100, 1024, 768, "OSRE-Ed",
WindowMode::Windowed,
WindowType::Child,
if (!osreApp.initWindow(100, 100, 1024, 768, "OSRE-Ed",
WindowMode::Windowed,
WindowType::Child,
RenderBackendType::OpenGLRenderBackend)) {
osre_error(Tag, "Cannot open the window.");
return AppError;
}

Expand Down
7 changes: 4 additions & 3 deletions src/Engine/Animation/AnimatorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ using VectorChannelArray = ::cppcore::TArray<VectorChannel>;
template <class T>
struct AnimatorBase {
void operator () ( T &out, const T &a, const T &b, f32 d ) const {
out = a + ( b - a ) * d;
out = a + (b - a) * d;
}
};

/// @brief
enum class TransformCommandType {
Invalid = -1,
RotateXCommandPositive = 0,
Expand Down Expand Up @@ -166,5 +167,5 @@ class OSRE_EXPORT AnimationControllerBase {
AnimationControllerBase() = default;
};

}
}
} // namespace Animation
} // namespace OSRE
41 changes: 28 additions & 13 deletions src/Engine/App/AppBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "RenderBackend/Pipeline.h"
#include "RenderBackend/RenderBackendService.h"
#include "RenderBackend/TransformMatrixBlock.h"
#include "RenderBackend/2D/CanvasRenderer.h"
#include "App/CameraComponent.h"
#include "RenderBackend/MaterialBuilder.h"

Expand Down Expand Up @@ -82,9 +83,7 @@ AppBase::AppBase(i32 argc, const c8 *argv[], const String &supportedArgs, const
mStage(nullptr),
mMouseEvListener(nullptr),
mKeyboardEvListener(nullptr),
mIds(nullptr),
mStageMode(StageMode::Stage3D),
mShutdownRequested(false) {
mIds(nullptr) {
mSettings->setString(Properties::Settings::RenderAPI, "opengl");
mSettings->setBool(Properties::Settings::PollingMode, true);
}
Expand Down Expand Up @@ -133,7 +132,7 @@ void AppBase::update() {
if (mAppState == State::Created) {
mAppState = State::Running;
}

onUpdate();
}

Expand Down Expand Up @@ -167,7 +166,7 @@ void AppBase::requestNextFrame() {
}

bool AppBase::handleEvents() {
if (mPlatformInterface != nullptr) {
if (mPlatformInterface == nullptr) {
osre_debug(Tag, "AppBase::PlatforInterface not in proper state: not nullptr.");
return false;
}
Expand Down Expand Up @@ -240,6 +239,10 @@ void AppBase::setWindowsTitle(const String &title) {
}
}

RenderBackend::CanvasRenderer* AppBase::getCanvasRenderer() const {
return (CanvasRenderer*) mCanvasRenderer;
}

bool AppBase::onCreate() {
if (mAppState != State::Uninited) {
osre_debug(Tag, "AppBase::State not in expected state: Uninited.");
Expand All @@ -251,14 +254,14 @@ bool AppBase::onCreate() {
mEnvironment = new Common::Environment;

// create the asset registry
AssetRegistry *registry = AssetRegistry::create();
if (registry != nullptr) {
if (AssetRegistry::create() == nullptr) {
osre_debug(Tag, "Cannot create asset registry.");
return false;
}

//Create the platform interface instance
// Create the platform interface instance
mPlatformInterface = Platform::PlatformInterface::create(mSettings);
if (mPlatformInterface != nullptr) {
if (mPlatformInterface == nullptr) {
osre_error(Tag, "Pointer to platform interface is nullptr.");
return false;
}
Expand All @@ -268,13 +271,13 @@ bool AppBase::onCreate() {
return false;
}

//Register any available platform-specific log streams
// Register any available platform-specific log streams
Common::AbstractLogStream *stream = Platform::PlatformPluginFactory::createPlatformLogStream();
if (stream != nullptr) {
Logger::getInstance()->registerLogStream(stream);
}

//Create the render back-end
// Create the render back-end
mRbService = new RenderBackendService();
ServiceProvider::setService(ServiceType::RenderService, mRbService);
mRbService->setSettings(mSettings, false);
Expand All @@ -293,9 +296,9 @@ bool AppBase::onCreate() {

mPlatformInterface->getPlatformEventHandler()->setRenderBackendService(mRbService);

// enable render-back-end
// Enable render-back-end
RenderBackend::CreateRendererEventData *data = new CreateRendererEventData(mPlatformInterface->getRootWindow());
data->m_pipeline = mRbService->createDefault3DPipeline();
data->RequestedPipeline = mRbService->createDefault3DPipeline();
mRbService->sendEvent(&RenderBackend::OnCreateRendererEvent, data);

mTimer = Platform::PlatformInterface::getInstance()->getTimer();
Expand Down Expand Up @@ -323,6 +326,14 @@ bool AppBase::onCreate() {

App::AssetRegistry::registerAssetPathInBinFolder("assets", "assets");

Rect2ui rect;
mPlatformInterface->getRootWindow()->getWindowsRect(rect);
mCanvasRenderer = new CanvasRenderer(2, (i32)rect.getX1(), (i32)rect.getY1(), (i32)rect.getWidth(), (i32)rect.getHeight());
if (!mCanvasRenderer->create()) {
osre_error(Tag, "Error while creating the canvas renderer.");
return false;
}

mAppState = State::Created;
osre_debug(Tag, "Set application state to Created.");

Expand Down Expand Up @@ -361,6 +372,9 @@ bool AppBase::onDestroy() {
delete mIds;
mIds = nullptr;

delete mCanvasRenderer;
mCanvasRenderer = nullptr;

delete mMouseEvListener;
mMouseEvListener = nullptr;

Expand Down Expand Up @@ -412,3 +426,4 @@ void AppBase::getResolution(ui32 &width, ui32 &height) {

} // Namespace App
} // Namespace OSRE

28 changes: 22 additions & 6 deletions src/Engine/App/AppBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ namespace Properties {

namespace RenderBackend {
struct TransformMatrixBlock;
struct IRenderPath;

class CanvasRenderer;
}

namespace App {
Expand All @@ -62,20 +65,23 @@ class AppBase;
//-------------------------------------------------------------------------------------------------
/// @ingroup Engine
///
/// @brief
/// @brief This class implements the keyboard event listener.
//-------------------------------------------------------------------------------------------------
class OSRE_EXPORT KeyboardEventListener : public Platform::OSEventListener {
public:
/// @brief The class constructor.
KeyboardEventListener() :
OSEventListener("App/KeyboardEventListener"),
mLast(Platform::KEY_UNKNOWN) {
clearKeyMap();
}

~KeyboardEventListener() override {
// empty
}
/// @brief The class destructor.
~KeyboardEventListener() override = default;

/// @brief The event handler.
/// @param osEvent The os-specific event.
/// @param data The event-related data.
void onOSEvent(const Common::Event &osEvent, const Common::EventData *data) override {
auto keyData = (Platform::KeyboardButtonEventData *)data;
if (osEvent == Platform::KeyboardButtonDownEvent) {
Expand All @@ -87,14 +93,20 @@ class OSRE_EXPORT KeyboardEventListener : public Platform::OSEventListener {
}
}

/// @brief Returns true, when the key is pressed
/// @param key The key to look for
/// @return true for is pressed.
bool isKeyPressed(Platform::Key key) const {
return mKeymap[key] == 1;
}

/// @brief Will return the latest pressed key.
/// @return The latest pressed key.
Platform::Key getLastKey() const {
return mLast;
}

/// @brief Clearn the map.
void clearKeyMap() {
::memset(mKeymap, 0, sizeof(char) * Platform::KEY_LAST);
}
Expand Down Expand Up @@ -250,6 +262,9 @@ class OSRE_EXPORT AppBase {
/// @return The keyboard listener.
virtual KeyboardEventListener *getKeyboardEventListener() const;

/// @brief
virtual RenderBackend::CanvasRenderer *getCanvasRenderer() const;

protected:
/// @brief The onCreate callback, override this for your own creation stuff.
/// @return true if successful, false if not.
Expand Down Expand Up @@ -290,8 +305,9 @@ class OSRE_EXPORT AppBase {
MouseEventListener *mMouseEvListener;
KeyboardEventListener *mKeyboardEvListener;
Common::Ids *mIds;
StageMode mStageMode;
bool mShutdownRequested;
StageMode mStageMode = StageMode::Stage3D;
bool mShutdownRequested = false;
RenderBackend::IRenderPath *mCanvasRenderer = nullptr;
};

inline Stage *AppBase::getStage() const {
Expand Down
12 changes: 7 additions & 5 deletions src/Engine/App/AppCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,22 @@ template<class T> class TAbstractCtrlBase;

/// @brief Describes the requested render API for the backend.
enum class RenderBackendType {
Invalid = -1, ///< Invalid render API.
OpenGLRenderBackend = 0, ///< OpenGL render API.
VulkanRenderBackend ///< Vulkan render API.
VulkanRenderBackend, ///< Vulkan render API.
Count ///< Number of render APIs.
};

struct MouseInputState {
i32 mRelX, mRelY, mAbsX, mAbsY;
ui32 mLastX, mLastY;
cppcore::TBitField<ui32> mMouseButtonState;
i32 RelX, RelY, AbsX, AbsY;
ui32 LastX, LastY;
cppcore::TBitField<ui32> MouseButtonState;

MouseInputState();
};

inline MouseInputState::MouseInputState() :
mRelX(0), mRelY(0), mAbsX(0), mAbsY(0), mLastX(0), mLastY(0), mMouseButtonState() {
RelX(0), RelY(0), AbsX(0), AbsY(0), LastX(0), LastY(0), MouseButtonState() {
// empty
}

Expand Down
5 changes: 3 additions & 2 deletions src/Engine/App/Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ bool RenderComponent::onRender(RenderBackendService *renderBackendSrv) {
return true;
}

LightComponent::LightComponent(Entity *owner)
: Component(owner, ComponentType::LightComponentType), mLight(nullptr) {}
LightComponent::LightComponent(Entity *owner) : Component(owner, ComponentType::LightComponentType) {
// empty
}

void LightComponent::setLight(RenderBackend::Light *light) {
mLight = light;
Expand Down
Loading

0 comments on commit dfca9b8

Please sign in to comment.