Gamp v0.0.7-36-g24b1eb6
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
RedSquareES2.cpp

This C++ example stub showcases the simple Launcher01.hpp mainloop framework integration.

This C++ example stub showcases the simple Launcher01.hpp mainloop framework integration. It creates a window, provides keyboard interaction and uses RedSquareES2 gamp::wt::RenderListener for rendering.

/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright Gothel Software e.K.
*
* SPDX-License-Identifier: MIT
*
* This Source Code Form is subject to the terms of the MIT License
* If a copy of the MIT was not distributed with this file,
* you can obtain one at https://opensource.org/license/mit/.
*/
#include <gamp/Gamp.hpp>
#include <cstdio>
#include <cmath>
#include <memory>
#include <gamp/Gamp.hpp>
#include "../demos/RedSquareES2.hpp"
#include "../demos/GLLauncher01.hpp"
using namespace jau::math;
using namespace jau::math::util;
using namespace gamp::wt;
using namespace gamp::wt::event;
class Example : public RedSquareES2 {
private:
class MyKeyListener : public KeyListener {
private:
RedSquareES2& m_parent;
public:
MyKeyListener(RedSquareES2& p) : m_parent(p) {}
void keyPressed(KeyEvent& e, const KeyboardTracker& kt) override {
jau::fprintf_td(e.when().to_ms(), stdout, "KeyPressed: %s; keys %zu\n", e.toString().c_str(), kt.pressedKeyCodes().bitCount());
if( e.keySym() == VKeyCode::VK_ESCAPE ) {
WindowRef win = e.source().lock();
if( win ) {
win->dispose(e.when());
}
} else if( e.keySym() == VKeyCode::VK_PAUSE || e.keySym() == VKeyCode::VK_P ) {
m_parent.animating() = !m_parent.animating();
} else if( e.keySym() == VKeyCode::VK_W ) {
WindowRef win = e.source().lock();
jau::fprintf_td(e.when().to_ms(), stdout, "Source: %s\n", win ? win->toString().c_str() : "null");
}
}
void keyReleased(KeyEvent& e, const KeyboardTracker& kt) override {
jau::fprintf_td(e.when().to_ms(), stdout, "KeyRelease: %s; keys %zu\n", e.toString().c_str(), kt.pressedKeyCodes().bitCount());
}
};
typedef std::shared_ptr<MyKeyListener> MyKeyListenerRef;
public:
m_kl(std::make_shared<MyKeyListener>(*this)) { }
bool init(const WindowRef& win, const jau::fraction_timespec& when) override {
if( !RedSquareES2::init(win, when) ) {
return false;
}
win->addKeyListener(m_kl);
return true;
}
void dispose(const WindowRef& win, const jau::fraction_timespec& when) override {
win->removeKeyListener(m_kl);
}
};
int main(int argc, char *argv[]) // NOLINT(bugprone-exception-escape)
{
return launch("RedSquareES2.hpp",
std::make_shared<Example>(), argc, argv);
}
int launch(std::string_view sfile, const GLLaunchProps &props, const RenderListenerRef &demo, int argc, char *argv[])
int main(int argc, char *argv[])
Definition GearsES2.cpp:232
std::shared_ptr< MyKeyListener > MyKeyListenerRef
void dispose(const WindowRef &win, const jau::fraction_timespec &when) override
Notifies the listener to perform the release of all renderer resources per context,...
Definition GearsES2.cpp:225
bool init(const WindowRef &, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
void dispose(const WindowRef &, const jau::fraction_timespec &) override
Notifies the listener to perform the release of all renderer resources per context,...
bool animating() const noexcept
std::string toString() const noexcept
Definition KeyEvent.hpp:855
constexpr VKeyCode keySym() const noexcept
Returns the virtual key symbol reflecting the current keyboard layout.
Definition KeyEvent.hpp:798
Listener for multiple KeyEvent.
Definition KeyEvent.hpp:894
virtual const PressedKeyCodes & pressedKeyCodes() const noexcept=0
constexpr const WindowWeakPtr & source() const noexcept
Definition Event.hpp:85
constexpr const jau::fraction_timespec & when() const noexcept
Definition Event.hpp:84
size_t bitCount() const noexcept
Definition bitfield.hpp:176
@ verbose
Verbose operations (debugging).
std::shared_ptr< Window > WindowRef
Definition Event.hpp:36
@ VK_ESCAPE
Constant for the ESCAPE function key.
Definition KeyEvent.hpp:129
@ VK_PAUSE
Constant for the PAUSE function key.
Definition KeyEvent.hpp:115
int fprintf_td(const uint64_t elapsed_ms, FILE *stream, const char *format,...) noexcept
Convenient fprintf() invocation, prepending the given elapsed_ms timestamp.
Definition debug.cpp:270
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...
constexpr uint64_t to_ms() const noexcept
Returns time in milliseconds.