Commit c9a48655 authored by Philip Trettner's avatar Philip Trettner

Added GlfwApp first version

parent a1ea0c22
......@@ -47,3 +47,12 @@ if (TARGET assimp)
else()
message(STATUS "target 'assimp' not found, disabling glow-extras-assimp")
endif()
# GLFW App
# requires 'aion'
if (TARGET aion)
add_subdirectory(glfw)
target_link_libraries(glow-extras PUBLIC glow-extras-glfw)
else()
message(STATUS "target 'aion' not found, disabling glow-extras-glfw")
endif()
cmake_minimum_required(VERSION 3.0)
file(GLOB_RECURSE SOURCE_FILES "*.cc")
file(GLOB_RECURSE HEADER_FILES "*.hh")
add_library(glow-extras-glfw ${GLOW_LINK_TYPE} ${SOURCE_FILES} ${HEADER_FILES})
target_include_directories(glow-extras-glfw PUBLIC ./)
target_compile_options(glow-extras-glfw PRIVATE ${GLOW_EXTRAS_DEF_OPTIONS})
target_link_libraries(glow-extras-glfw PUBLIC
glow
aion
AntTweakBar
fmt
glow-extras-timing
glow-extras-material
glow-extras-pipeline
glow-extras-camera
)
This diff is collapsed.
#pragma once
#include <string>
#include <vector>
#include <glm/vec2.hpp>
#include <glow/common/property.hh>
#include <glow/common/shared.hh>
#include <glow/fwd.hh>
struct GLFWwindow;
struct CTwBar;
typedef struct CTwBar TwBar; // structure CTwBar is not exposed.
namespace glow
{
namespace pipeline
{
struct RenderPass;
GLOW_SHARED(class, RenderingPipeline);
}
namespace camera
{
GLOW_SHARED(class, GenericCamera);
}
namespace glfw
{
enum class CursorMode
{
/// normal behavior
Normal,
/// normal behavior but hardware cursor is hidden
Hidden,
/// virtual unrestricted cursor, real cursor is hidden and locked to center
Disabled,
};
/**
* @brief The GlfwApp can be used to efficiently build small sample applications based on glfw
*
* Derive your own class from GlfwApp and override the functions you need:
* - init(...): initialize and allocate all your resources and objects
* - update(...): called with a constant rate (default 60 Hz, configurable) before rendering
* - render(...): called as fast as possible (affected by vsync)
* - renderPass(...): if rendering pipeline enabled (default), default render(...) will call this (RECOMMENDED)
* - onResize(...): called when window is resized
* - onClose(...): called when app is closed
* - input: see onKey/onChar/onMouseXYZ/onFileDrop (return true if you handled the event, if base returned true, you
* should probably return as well)
* be sure to call base function unless you know what you do!
*
* Additional important functions:
* - setUpdateRate(...): set the update rate
* - window(): get the GLFW window
* - tweakbar(): get the AntTweakBar instance
* - setWindowWidth/Height(...): set initial window size before run(...)
*
* Notes:
* - if you use primitive/occlusion queries, use setQueryStats(false);
*
* Usage:
* int main(int argc, char *argv[])
* {
* MyGlfwApp app;
* return app.run(argc, argv); // automatically sets up GLOW and GLFW and everything
* }
*/
class GlfwApp
{
private:
std::string mTitle = "GLFW/GLOW Application"; ///< window title
int mUpdateRate = 60; ///< rate at which update(...) is called
int mMaxFrameSkip = 4; ///< maximum number of update(...) steps that are performed without rendering
GLFWwindow* mWindow = nullptr; ///< current GLFW window
TwBar* mTweakbar = nullptr; ///< main tweakbar window
int mWindowWidth = 640; ///< window width, only set before run!
int mWindowHeight = 480; ///< window height, only set before run!
bool mDumpTimingsOnShutdown = true; ///< if true, dumps AION timings on shutdown
double mMouseX = -1.0; ///< cursor X in pixels
double mMouseY = -1.0; ///< cursor Y in pixels
CursorMode mCursorMode = CursorMode::Normal; ///< cursor mode
bool mDrawTweakbars = true; ///< if true, draws tweakbars
bool mVSync = true; ///< if true, enables vsync
double mOutputStatsInterval = 5.0; ///< number of seconds between stats output (0.0 for never)
bool mQueryStats = true; ///< if true, queries stats (vertices, fragments, ...)
bool mUseDefaultRendering = true; ///< if true, uses default rendering pipeline setup
double mCurrentTime = 0.0; ///< current frame time (starts with 0)
// Default graphics
private:
camera::SharedGenericCamera mCamera; ///< default camera
pipeline::SharedRenderingPipeline mPipeline; ///< default pipeline
SharedPrimitiveQuery mPrimitiveQuery; ///< nr of primitives per frame
SharedOcclusionQuery mOcclusionQuery; ///< nr of pixels per frame
SharedTimerQuery mRenderStartQuery; ///< start timestamp
SharedTimerQuery mRenderEndQuery; ///< end timestamp
public:
GLOW_PROPERTY(UpdateRate);
GLOW_PROPERTY(MaxFrameSkip);
GLOW_GETTER(Title);
GLOW_PROPERTY(WindowWidth);
GLOW_PROPERTY(WindowHeight);
GLOW_PROPERTY(DumpTimingsOnShutdown);
GLOW_PROPERTY(CursorMode);
GLOW_PROPERTY(DrawTweakbars);
GLOW_PROPERTY(VSync);
GLOW_PROPERTY(OutputStatsInterval);
GLOW_PROPERTY(QueryStats);
GLOW_PROPERTY(UseDefaultRendering);
float getCurrentTime() const { return mCurrentTime; }
double getCurrentTimeD() const { return mCurrentTime; }
GLOW_GETTER(Camera);
GLOW_GETTER(Pipeline);
void setTitle(std::string const& title);
glm::vec2 getMousePosition() const { return {mMouseX, mMouseY}; }
GLFWwindow* window() const { return mWindow; }
TwBar* tweakbar() const { return mTweakbar; }
public:
/// sets the current clipboard content
void setClipboardString(std::string const& s);
/// gets the current clipboard content
std::string getClipboardString() const;
protected:
/// Called once GLOW is initialized. Allocated your resources and init your logic here.
virtual void init();
/// Called with at 1 / getUpdateRate() Hz (timestep)
virtual void update(float elapsedSeconds);
/// Called as fast as possible for rendering (elapsedSeconds is not fixed here)
virtual void render(float elapsedSeconds);
/// When using the builtin rendering pipeline, this is called for every pass in every render step
virtual void renderPass(pipeline::RenderPass const& pass, float elapsedSeconds);
/// Called once in the beginning after (init) and whenever the window size changed
virtual void onResize(int w, int h);
/// Called at the end, when application is closed
virtual void onClose();
/// Called whenever a key is pressed
virtual bool onKey(int key, int scancode, int action, int mods);
/// Called whenever a character is entered (unicode)
virtual bool onChar(unsigned int codepoint, int mods);
/// Called whenever the mouse position changes
virtual bool onMousePosition(double x, double y);
/// Called whenever a mouse button is pressed
virtual bool onMouseButton(double x, double y, int button, int action, int mods);
/// Called whenever the mouse is scrolled
virtual bool onMouseScroll(double sx, double sy);
/// Called whenever the mouse enters the window
virtual bool onMouseEnter();
/// Called whenever the mouse leaves the window
virtual bool onMouseExit();
/// Called whenever the window gets focus
virtual bool onFocusGain();
/// Called whenever the window loses focus
virtual bool onFocusLost();
/// Called whenever files are dropped (drag'n'drop), parameters is file paths
virtual bool onFileDrop(std::vector<std::string> const& files);
public:
/// Initializes GLFW and GLOW, and runs until window is closed
int run(int argc, char* argv[]);
};
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment