engine/include/window.hpp

192 lines
5.0 KiB
C++
Raw Normal View History

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
2023-05-01 12:14:46 +00:00
float GetMouseNormX() const;
2023-04-29 14:22:25 +00:00
// retrieves mouse y coordinate normalised for OpenGL
2023-05-01 12:14:46 +00:00
float GetMouseNormY() const;
2023-04-29 14:22:25 +00:00
// retrieves dx of the mouse since the last frame
2023-05-01 12:14:46 +00:00
int GetMouseDX() const;
2023-04-29 14:22:25 +00:00
// retrieves dy of the mouse since the last frame
2023-05-01 12:14:46 +00:00
int GetMouseDY() const;
2023-04-29 14:22:25 +00:00
// retrieves amount scrolled vertically
2023-05-01 12:14:46 +00:00
float GetMouseScrollX() const;
2023-04-29 14:22:25 +00:00
// retrieves amount scrolled horizontally
2023-05-01 12:14:46 +00:00
float GetMouseScrollY() const;
2023-04-29 14:22:25 +00:00
// joystick/gamepad events (maybe), other misc events
// returns the performance counter value in nanoseconds;
2023-05-01 12:14:46 +00:00
uint64_t GetNanos() const;
2023-04-29 14:22:25 +00:00
// get the time recorded at the end of the last frame
2023-05-01 12:14:46 +00:00
uint64_t GetLastFrameStamp() const;
2023-04-29 14:22:25 +00:00
// returns the number of frames elapsed since window creation
2023-05-01 12:14:46 +00:00
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-05-01 12:14:46 +00:00
uint64_t GetFPS() const;
uint64_t GetAvgFPS() const;
2023-04-29 14:22:25 +00:00
2023-05-01 12:14:46 +00:00
void ResetAvgFPS();
2023-04-29 14:22:25 +00:00
2023-05-01 12:14:46 +00:00
bool InfoBox(const std::string& title, const std::string& msg);
2023-04-29 14:22:25 +00:00
/* STATIC METHODS */
2023-05-01 12:14:46 +00:00
static void ErrorBox(const std::string& message);
2023-04-29 14:22:25 +00:00
2023-04-29 14:56:49 +00:00
private:
2023-05-01 12:14:46 +00:00
SDL_Window* handle_;
2023-04-29 14:22:25 +00:00
2023-05-01 12:14:46 +00:00
bool should_close_ = false;
2023-04-29 14:22:25 +00:00
2023-05-01 12:14:46 +00:00
std::string title_;
2023-04-29 14:22:25 +00:00
2023-05-01 12:14:46 +00:00
bool resizable_;
2023-04-29 14:22:25 +00:00
2023-05-01 12:14:46 +00:00
bool fullscreen_ = false;
bool just_resized_ = false;
bool keyboard_focus_ = true;
2023-04-29 14:22:25 +00:00
// size in screen coordinates
2023-05-01 12:14:46 +00:00
glm::ivec2 win_size_ = glm::vec2(1024, 768);
2023-04-29 14:22:25 +00:00
// performance counter frequency
2023-05-01 12:14:46 +00:00
uint64_t counter_freq_;
2023-04-29 14:22:25 +00:00
// number of frames swapped
2023-05-01 12:14:46 +00:00
uint64_t frames_ = 0;
2023-04-29 14:22:25 +00:00
// frame count offset for fpsAvg
2023-05-01 12:14:46 +00:00
uint64_t avg_fps_start_count_ = 0;
2023-04-29 14:22:25 +00:00
// in nanoseconds
2023-05-01 12:14:46 +00:00
uint64_t start_time_;
2023-04-29 14:22:25 +00:00
// in nanoseconds
2023-05-01 12:14:46 +00:00
uint64_t last_frame_stamp_;
2023-04-29 14:22:25 +00:00
// in nanoseconds; elapsed time between frames
2023-05-01 12:14:46 +00:00
uint64_t last_frame_time_ = 1; // not 0 to avoid division by zero
2023-04-29 14:22:25 +00:00
// in nanoseconds
2023-05-01 12:14:46 +00:00
uint64_t avg_fps_start_;
2023-04-29 14:22:25 +00:00
// input stuff
2023-05-01 12:14:46 +00:00
enum class ButtonDelta { kSame = 0, kPressed, kReleased };
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-05-01 12:14:46 +00:00
} 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-05-01 12:14:46 +00:00
} mouse_{};
2023-04-29 14:22:25 +00:00
// private methods
2023-05-01 12:14:46 +00:00
void OnResize(Sint32 width, Sint32 height);
void ResetInputDeltas();
2023-04-29 14:22:25 +00:00
// event methods (like callbacks)
2023-05-01 12:14:46 +00:00
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);
2023-04-29 14:22:25 +00:00
};
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