2023-04-29 14:22:25 +00:00
|
|
|
#ifndef ENGINE_INCLUDE_WINDOW_H_
|
|
|
|
#define ENGINE_INCLUDE_WINDOW_H_
|
2022-09-02 11:06:59 +00:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <string>
|
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
#include <SDL.h>
|
2023-04-29 14:56:49 +00:00
|
|
|
#include <glm/vec2.hpp>
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
#include "inputs/keyboard.hpp"
|
|
|
|
#include "inputs/mouse.hpp"
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
namespace engine {
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
class Window {
|
2023-04-29 14:56:49 +00:00
|
|
|
public:
|
2023-04-29 14:22:25 +00:00
|
|
|
Window(const std::string& title, bool resizable = true,
|
|
|
|
bool fullscreen = true);
|
|
|
|
Window(const Window&) = delete;
|
|
|
|
Window& operator=(const Window&) = delete;
|
|
|
|
~Window();
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2023-04-29 14:56:49 +00:00
|
|
|
SDL_Window* GetHandle() const;
|
2022-09-17 00:22:35 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// Return the title name
|
2023-04-29 14:56:49 +00:00
|
|
|
std::string GetTitle() const;
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// Update the window state to capture any events that have occurred.
|
|
|
|
// Run this on every frame.
|
2023-04-29 14:56:49 +00:00
|
|
|
void GetInputAndEvents();
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:56:49 +00:00
|
|
|
void SetTitle(std::string title);
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// Hides the window (it will appear closed to the user).
|
2023-04-29 14:56:49 +00:00
|
|
|
void Hide();
|
2023-04-29 14:22:25 +00:00
|
|
|
// Shows the window again.
|
2023-04-29 14:56:49 +00:00
|
|
|
void Show();
|
2023-04-29 14:22:25 +00:00
|
|
|
// Raises the window above other windows and sets the input focus
|
2023-04-29 14:56:49 +00:00
|
|
|
void Focus();
|
2023-04-29 14:22:25 +00:00
|
|
|
// Returns true if the window has focus
|
2023-04-29 14:56:49 +00:00
|
|
|
bool HasFocus() const;
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// Sets the close flag, check this with shouldClose()
|
2023-04-29 14:56:49 +00:00
|
|
|
void SetCloseFlag();
|
2023-04-29 14:22:25 +00:00
|
|
|
// Returns true if the window should remain open
|
2023-04-29 14:56:49 +00:00
|
|
|
bool IsRunning() const;
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:56:49 +00:00
|
|
|
void SetFullscreen(bool fullscreen, bool exclusive = false);
|
|
|
|
void ToggleFullscreen();
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:56:49 +00:00
|
|
|
bool IsFullscreen() const;
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// Relative mouse mode captures the cursor for FPS style use.
|
|
|
|
// Returns false if unsupported.
|
2023-04-29 14:56:49 +00:00
|
|
|
bool SetRelativeMouseMode(bool enabled);
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// returns true if relative mouse mode is enabled
|
2023-04-29 14:56:49 +00:00
|
|
|
bool MouseCaptured();
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// window events
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// Returns true if the window was just resized during the previous frame
|
2023-04-29 14:56:49 +00:00
|
|
|
bool GetWindowResized() const;
|
2023-04-29 14:22:25 +00:00
|
|
|
// Set the window resized flag (to recalculate aspect ratios and such)
|
2023-04-29 14:56:49 +00:00
|
|
|
void SetResizedFlag();
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// keyboard events
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// returns true if key is down
|
2023-04-29 14:56:49 +00:00
|
|
|
bool GetKey(inputs::Key key) const;
|
2023-04-29 14:22:25 +00:00
|
|
|
// returns true if key was just pressed
|
2023-04-29 14:56:49 +00:00
|
|
|
bool GetKeyPress(inputs::Key key) const;
|
2023-04-29 14:22:25 +00:00
|
|
|
// returns true if key was just released
|
2023-04-29 14:56:49 +00:00
|
|
|
bool GetKeyRelease(inputs::Key key) const;
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
// mouse events
|
|
|
|
|
|
|
|
// returns true if button is down
|
2023-04-29 14:56:49 +00:00
|
|
|
bool GetButton(inputs::MouseButton button) const;
|
2023-04-29 14:22:25 +00:00
|
|
|
// returns true if button was just pressed
|
2023-04-29 14:56:49 +00:00
|
|
|
bool GetButtonPress(inputs::MouseButton button) const;
|
2023-04-29 14:22:25 +00:00
|
|
|
// returns true if button was just released
|
2023-04-29 14:56:49 +00:00
|
|
|
bool GetButtonRelease(inputs::MouseButton button) const;
|
2023-04-29 14:22:25 +00:00
|
|
|
|
|
|
|
// retrieves x coordinate of the mouse
|
2023-04-29 14:56:49 +00:00
|
|
|
int GetMouseX() const;
|
2023-04-29 14:22:25 +00:00
|
|
|
// retrieves y coordinate of the mouse
|
2023-04-29 14:56:49 +00:00
|
|
|
int GetMouseY() const;
|
2023-04-29 14:22:25 +00:00
|
|
|
// retrieves mouse x coordinate normalised for OpenGL
|
|
|
|
float getMouseNormX() const;
|
|
|
|
// retrieves mouse y coordinate normalised for OpenGL
|
|
|
|
float getMouseNormY() const;
|
|
|
|
// retrieves dx of the mouse since the last frame
|
|
|
|
int getMouseDX() const;
|
|
|
|
// retrieves dy of the mouse since the last frame
|
|
|
|
int getMouseDY() const;
|
|
|
|
// retrieves amount scrolled vertically
|
|
|
|
float getMouseScrollX() const;
|
|
|
|
// retrieves amount scrolled horizontally
|
|
|
|
float getMouseScrollY() const;
|
|
|
|
|
|
|
|
// joystick/gamepad events (maybe), other misc events
|
|
|
|
|
|
|
|
// returns the performance counter value in nanoseconds;
|
|
|
|
uint64_t getNanos() const;
|
|
|
|
// get the time recorded at the end of the last frame
|
|
|
|
uint64_t getLastFrameStamp() const;
|
|
|
|
|
|
|
|
// returns the number of frames elapsed since window creation
|
|
|
|
uint64_t getFrameCount() const;
|
|
|
|
uint64_t getStartTime() const;
|
2023-04-29 14:56:49 +00:00
|
|
|
float dt() const; // returns delta time in seconds
|
2023-04-29 14:22:25 +00:00
|
|
|
uint64_t getFPS() const;
|
|
|
|
uint64_t getAvgFPS() const;
|
|
|
|
|
|
|
|
void resetAvgFPS();
|
|
|
|
|
|
|
|
bool infoBox(const std::string& title, const std::string& msg);
|
|
|
|
|
|
|
|
/* STATIC METHODS */
|
|
|
|
static void errorBox(const std::string& message);
|
|
|
|
|
2023-04-29 14:56:49 +00:00
|
|
|
private:
|
2023-04-29 14:22:25 +00:00
|
|
|
SDL_Window* m_handle;
|
|
|
|
|
|
|
|
bool m_shouldClose = false;
|
|
|
|
|
|
|
|
std::string m_title;
|
|
|
|
|
|
|
|
bool m_resizable;
|
|
|
|
|
|
|
|
bool m_fullscreen = false;
|
|
|
|
bool m_justResized = false;
|
|
|
|
bool m_keyboardFocus = true;
|
|
|
|
|
|
|
|
// size in screen coordinates
|
|
|
|
glm::ivec2 m_winSize = glm::vec2(1024, 768);
|
|
|
|
|
|
|
|
// performance counter frequency
|
|
|
|
uint64_t m_counterFreq;
|
|
|
|
|
|
|
|
// number of frames swapped
|
|
|
|
uint64_t m_frames = 0;
|
|
|
|
// frame count offset for fpsAvg
|
|
|
|
uint64_t m_avgFpsStartCount = 0;
|
|
|
|
// in nanoseconds
|
|
|
|
uint64_t m_startTime;
|
|
|
|
// in nanoseconds
|
|
|
|
uint64_t m_lastFrameStamp;
|
|
|
|
// in nanoseconds; elapsed time between frames
|
2023-04-29 14:56:49 +00:00
|
|
|
uint64_t m_lastFrameTime = 1; // not 0 to avoid division by zero
|
2023-04-29 14:22:25 +00:00
|
|
|
// in nanoseconds
|
|
|
|
uint64_t m_avgFpsStart;
|
|
|
|
|
|
|
|
// input stuff
|
|
|
|
|
2023-04-29 14:56:49 +00:00
|
|
|
enum class ButtonDelta { SAME = 0, PRESSED, RELEASED };
|
2023-04-29 14:22:25 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
std::array<bool, SDL_NUM_SCANCODES> keys;
|
|
|
|
std::array<enum ButtonDelta, SDL_NUM_SCANCODES> deltas;
|
2023-04-29 14:56:49 +00:00
|
|
|
} m_keyboard{};
|
2023-04-29 14:22:25 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
std::array<bool, static_cast<int>(inputs::MouseButton::M_SIZE)> buttons;
|
|
|
|
std::array<enum ButtonDelta, 8> deltas;
|
|
|
|
Sint32 x;
|
|
|
|
Sint32 y;
|
|
|
|
Sint32 dx;
|
|
|
|
Sint32 dy;
|
|
|
|
float xscroll;
|
|
|
|
float yscroll;
|
|
|
|
bool captured = false;
|
2023-04-29 14:56:49 +00:00
|
|
|
} m_mouse{};
|
2023-04-29 14:22:25 +00:00
|
|
|
|
|
|
|
// private methods
|
|
|
|
|
|
|
|
void onResize(Sint32 width, Sint32 height);
|
|
|
|
void resetInputDeltas();
|
|
|
|
|
|
|
|
// event methods (like callbacks)
|
|
|
|
|
|
|
|
void onWindowEvent(SDL_WindowEvent& e);
|
|
|
|
void onKeyEvent(SDL_KeyboardEvent& e);
|
|
|
|
void onMouseButtonEvent(SDL_MouseButtonEvent& e);
|
|
|
|
void onMouseMotionEvent(SDL_MouseMotionEvent& e);
|
|
|
|
void onMouseWheelEvent(SDL_MouseWheelEvent& e);
|
|
|
|
};
|
2022-09-17 00:22:35 +00:00
|
|
|
|
2023-04-29 14:56:49 +00:00
|
|
|
} // namespace engine
|
2023-04-29 14:22:25 +00:00
|
|
|
|
|
|
|
#endif
|