sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
window.h
1#ifndef __WINDOW_H__
2#define __WINDOW_H__
3
4#include "app.h"
5#include "image.h"
6#include "timer.h"
7#include "gamepad.h"
8#include "joystick.h"
9
10namespace sgltk {
11
15enum class WINDOW_MODE {
19 WINDOWED = 0,
23 FULLSCREEN = SDL_WINDOW_FULLSCREEN,
27 FAKE_FULLSCREEN = SDL_WINDOW_FULLSCREEN_DESKTOP
28};
29
34class Window {
35 bool running;
36 static unsigned int cnt;
37 SDL_GLContext context;
38 const Uint8 *keys;
39 bool mouse_relative;
40 unsigned int fps_time;
41 std::vector<std::string> keys_pressed;
42 EXPORT static std::map<unsigned int, std::shared_ptr<Gamepad> > gamepad_instance_id_map;
43 EXPORT static std::map<unsigned int, std::shared_ptr<Joystick> > joystick_instance_id_map;
44public:
48 int gl_maj;
52 int gl_min;
56 int width;
60 int height;
64 double delta_time;
68 SDL_Window *window;
69
79 EXPORT Window(const std::string& title, int res_x, int res_y, int offset_x, int offset_y, unsigned int flags = 0);
80 EXPORT ~Window();
81
87 EXPORT void set_icon(const std::string& filename);
93 EXPORT void set_icon(const sgltk::Image& icon);
98 EXPORT void set_title(const std::string& title);
103 EXPORT void set_resizable(bool on);
108 EXPORT void take_screenshot(sgltk::Image& image);
114 EXPORT void grab_mouse(bool on);
123 EXPORT int get_display_index();
129 EXPORT bool set_display_mode(const SDL_DisplayMode& mode);
135 EXPORT bool fullscreen_mode(sgltk::WINDOW_MODE mode);
144 EXPORT void set_relative_mode(bool on);
150 EXPORT void set_mouse_position(int x, int y);
155 EXPORT void set_cursor_visibility(bool show);
160 EXPORT bool get_cursor_visibility();
165 EXPORT void poll_events();
172 EXPORT virtual void handle_gamepad_added(std::shared_ptr<sgltk::Gamepad> gamepad);
178 EXPORT virtual void handle_gamepad_removed(unsigned int gamepad_id);
186 EXPORT virtual void handle_gamepad_button(std::shared_ptr<sgltk::Gamepad> gamepad, int button);
196 EXPORT virtual void handle_gamepad_button_press(std::shared_ptr<sgltk::Gamepad> gamepad,
197 int button, bool pressed);
205 EXPORT virtual void handle_gamepad_axis(std::shared_ptr<sgltk::Gamepad> gamepad,
206 unsigned int axis, int value);
214 EXPORT virtual void handle_gamepad_axis_change(std::shared_ptr<sgltk::Gamepad> gamepad,
215 unsigned int axis, int value);
222 EXPORT virtual void handle_joystick_added(std::shared_ptr<sgltk::Joystick> joystick);
228 EXPORT virtual void handle_joystick_removed(unsigned int joystick_id);
236 EXPORT virtual void handle_joystick_button(std::shared_ptr<sgltk::Joystick> joystick, int button);
246 EXPORT virtual void handle_joystick_button_press(std::shared_ptr<sgltk::Joystick> joystick,
247 int button, bool pressed);
255 EXPORT virtual void handle_joystick_axis(std::shared_ptr<sgltk::Joystick> joystick,
256 unsigned int axis, int value);
264 EXPORT virtual void handle_joystick_axis_change(std::shared_ptr<sgltk::Joystick> joystick,
265 unsigned int axis, int value);
273 EXPORT virtual void handle_joystick_hat(std::shared_ptr<sgltk::Joystick> joystick,
274 unsigned int hat, unsigned int value);
282 EXPORT virtual void handle_joystick_hat_change(std::shared_ptr<sgltk::Joystick> joystick,
283 unsigned int hat, unsigned int value);
293 EXPORT virtual void handle_joystick_ball_motion(std::shared_ptr<sgltk::Joystick> joystick,
294 unsigned int ball,
295 int xrel, int yrel);
302 EXPORT virtual void handle_keyboard(const std::string& key);
309 EXPORT virtual void handle_key_press(const std::string& key, bool pressed);
318 EXPORT virtual void handle_mouse_motion(int x, int y);
325 EXPORT virtual void handle_mouse_wheel(int x, int y);
336 EXPORT virtual void handle_mouse_button(int x, int y, int button,
337 bool down, int clicks);
342 EXPORT virtual void handle_resize();
347 EXPORT virtual void handle_exit();
352 EXPORT virtual void display();
359 EXPORT void run(unsigned int fps = 0);
363 EXPORT void stop();
364};
365
366}
367
368#endif
Manages images.
Definition image.h:12
virtual void handle_joystick_added(std::shared_ptr< sgltk::Joystick > joystick)
This function is called by poll_events() to handle the addition of new joysticks. This function shoul...
Definition window.cpp:374
virtual void handle_gamepad_axis_change(std::shared_ptr< sgltk::Gamepad > gamepad, unsigned int axis, int value)
This function is called by poll_events() for every axis value change. This function should be overrid...
Definition window.cpp:371
double delta_time
The time it took to draw the last frame.
Definition window.h:64
int get_display_index()
Returns the index of the display that currently contains the window. This index corresponds to the in...
Definition window.cpp:105
virtual void handle_key_press(const std::string &key, bool pressed)
This function is called by poll_events() once for every key pressed or released. This function should...
Definition window.cpp:404
void set_relative_mode(bool on)
Activates the relative mouse motion mode In the relative mouse motion mode the cursor is invisible an...
Definition window.cpp:132
virtual void handle_mouse_motion(int x, int y)
This function is called by poll_events() to handle mouse motion. This function should be overridden.
Definition window.cpp:407
void set_mouse_position(int x, int y)
Sets the mouse position inside the window.
Definition window.cpp:137
void poll_events()
Polls all events and calls the handlers. Called by the run function.
Definition window.cpp:157
virtual void handle_mouse_button(int x, int y, int button, bool down, int clicks)
This function is called by poll_events() to handle mouse button presses. This function should be over...
Definition window.cpp:413
virtual void handle_mouse_wheel(int x, int y)
This function is called by poll_events() to handle mouse wheel movements. This function should be ove...
Definition window.cpp:410
virtual void handle_joystick_hat(std::shared_ptr< sgltk::Joystick > joystick, unsigned int hat, unsigned int value)
This function is called by poll_events() every frame for every hat of every joystick....
Definition window.cpp:392
void set_title(const std::string &title)
Set window title.
Definition window.cpp:86
bool set_display_mode(const SDL_DisplayMode &mode)
Changes the window display mode when in fullscreen mode.
Definition window.cpp:114
void stop()
Stops the main loop.
Definition window.cpp:461
int gl_min
The minor OpenGL version number.
Definition window.h:52
void set_cursor_visibility(bool show)
Sets mouse cursor visibility.
Definition window.cpp:141
virtual void handle_gamepad_removed(unsigned int gamepad_id)
This function is called by poll_events() to handle the removal of gamepads. This function should be o...
Definition window.cpp:359
virtual void handle_joystick_hat_change(std::shared_ptr< sgltk::Joystick > joystick, unsigned int hat, unsigned int value)
This function is called by poll_events() for every hat value change. This function should be overridd...
Definition window.cpp:395
Window(const std::string &title, int res_x, int res_y, int offset_x, int offset_y, unsigned int flags=0)
Definition window.cpp:9
int height
The height of the window surface.
Definition window.h:60
int width
The width of the window surface.
Definition window.h:56
virtual void handle_exit()
This function is called by run when the window is being closed. This function should be overridden.
Definition window.cpp:422
virtual void handle_joystick_axis_change(std::shared_ptr< sgltk::Joystick > joystick, unsigned int axis, int value)
This function is called by poll_events() for every axis value change. This function should be overrid...
Definition window.cpp:389
void run(unsigned int fps=0)
Starts the main loop. This function calls poll_events() and display()
Definition window.cpp:431
bool fullscreen_mode(sgltk::WINDOW_MODE mode)
Changes the window mode.
Definition window.cpp:123
virtual void handle_joystick_removed(unsigned int joystick_id)
This function is called by poll_events() to handle the removal of joysticks. This function should be ...
Definition window.cpp:377
virtual void handle_gamepad_button_press(std::shared_ptr< sgltk::Gamepad > gamepad, int button, bool pressed)
This function is called by poll_events() once for every gamepad button press or release....
Definition window.cpp:365
virtual void handle_gamepad_axis(std::shared_ptr< sgltk::Gamepad > gamepad, unsigned int axis, int value)
This function is called by poll_events() every frame for every axis of every gamepad....
Definition window.cpp:368
virtual void handle_joystick_ball_motion(std::shared_ptr< sgltk::Joystick > joystick, unsigned int ball, int xrel, int yrel)
This function is called by poll_events() for every ball that has changed value change....
Definition window.cpp:398
virtual void handle_gamepad_added(std::shared_ptr< sgltk::Gamepad > gamepad)
This function is called by poll_events() to handle the addition of new gamepads. This function should...
Definition window.cpp:356
void set_resizable(bool on)
Sets or removes the ability to resize the window.
Definition window.cpp:90
void take_screenshot(sgltk::Image &image)
Takes a screenshot of the window.
Definition window.cpp:94
bool get_cursor_visibility()
Returns mouse cursor visibility status.
Definition window.cpp:149
virtual void handle_gamepad_button(std::shared_ptr< sgltk::Gamepad > gamepad, int button)
This function is called by poll_events() every frame for every gamepad button currently being pressed...
Definition window.cpp:362
void grab_mouse(bool on)
Sets the window to grab the mouse When activate the mouse can not leave the window boundaries.
Definition window.cpp:101
virtual void handle_joystick_button_press(std::shared_ptr< sgltk::Joystick > joystick, int button, bool pressed)
This function is called by poll_events() once for every joystick button pressed or released....
Definition window.cpp:383
int gl_maj
The manjor OpenGL version number.
Definition window.h:48
virtual void handle_joystick_axis(std::shared_ptr< sgltk::Joystick > joystick, unsigned int axis, int value)
This function is called by poll_events() every frame for every axis of every joystick....
Definition window.cpp:386
virtual void handle_joystick_button(std::shared_ptr< sgltk::Joystick > joystick, int button)
This function is called by poll_events() every frame for every joystick button currently being presse...
Definition window.cpp:380
virtual void handle_keyboard(const std::string &key)
This function is called by poll_events() every frame for every key currently being pressed....
Definition window.cpp:401
virtual void handle_resize()
This function is called by run when the window is resized. This function should be overridden.
Definition window.cpp:419
SDL_Window * window
The window surface.
Definition window.h:68
virtual void display()
This function is called by run() to draw a frame. This function should be overridden.
Definition window.cpp:426
void set_icon(const std::string &filename)
Sets the window icon.
Definition window.cpp:77