mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
218 lines
6.9 KiB
218 lines
6.9 KiB
|
14 years ago
|
#ifndef Magnum_Platform_Sdl2Application_h
|
||
|
|
#define Magnum_Platform_Sdl2Application_h
|
||
|
14 years ago
|
/*
|
||
|
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
|
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
|
Magnum is free software: you can redistribute it and/or modify
|
||
|
|
it under the terms of the GNU Lesser General Public License version 3
|
||
|
|
only, as published by the Free Software Foundation.
|
||
|
|
|
||
|
|
Magnum is distributed in the hope that it will be useful,
|
||
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
|
GNU Lesser General Public License version 3 for more details.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** @file
|
||
|
14 years ago
|
* @brief Class Magnum::Platform::Sdl2Application
|
||
|
14 years ago
|
*/
|
||
|
|
|
||
|
14 years ago
|
#include "Math/Vector2.h"
|
||
|
14 years ago
|
#include "Magnum.h"
|
||
|
14 years ago
|
|
||
|
14 years ago
|
#include <SDL2/SDL.h>
|
||
|
|
#include <SDL2/SDL_scancode.h>
|
||
|
14 years ago
|
#include <Corrade/Containers/EnumSet.h>
|
||
|
14 years ago
|
|
||
|
14 years ago
|
#include "AbstractApplication.h"
|
||
|
14 years ago
|
|
||
|
14 years ago
|
#include "magnumCompatibility.h"
|
||
|
|
|
||
|
14 years ago
|
namespace Magnum {
|
||
|
|
|
||
|
|
class Context;
|
||
|
|
|
||
|
14 years ago
|
namespace Platform {
|
||
|
14 years ago
|
|
||
|
|
/** @nosubgrouping
|
||
|
14 years ago
|
@brief SDL2 application
|
||
|
14 years ago
|
|
||
|
14 years ago
|
Supports keyboard and mouse handling.
|
||
|
14 years ago
|
|
||
|
|
You need to implement at least drawEvent() and viewportEvent() to be able to
|
||
|
|
draw on the screen.
|
||
|
|
*/
|
||
|
14 years ago
|
class Sdl2Application: public AbstractApplication {
|
||
|
14 years ago
|
public:
|
||
|
|
/**
|
||
|
|
* @brief Constructor
|
||
|
|
* @param argc Count of arguments of `main()` function
|
||
|
|
* @param argv Arguments of `main()` function
|
||
|
|
* @param title Window title
|
||
|
|
* @param size Window size
|
||
|
|
*
|
||
|
|
* Creates centered non-resizable window with double-buffered
|
||
|
14 years ago
|
* OpenGL 3.2 context with 24bit depth buffer.
|
||
|
14 years ago
|
*/
|
||
|
14 years ago
|
Sdl2Application(int argc, char** argv, const std::string& title = "Magnum SDL2 application", const Math::Vector2<GLsizei>& size = Math::Vector2<GLsizei>(800, 600));
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
14 years ago
|
* @brief Destructor
|
||
|
14 years ago
|
*
|
||
|
|
* Deletes context and destroys the window.
|
||
|
|
*/
|
||
|
14 years ago
|
~Sdl2Application();
|
||
|
14 years ago
|
|
||
|
14 years ago
|
int exec() override;
|
||
|
14 years ago
|
|
||
|
|
/** @{ @name Drawing functions */
|
||
|
|
|
||
|
|
protected:
|
||
|
14 years ago
|
/** @copydoc GlutApplication::viewportEvent() */
|
||
|
14 years ago
|
virtual void viewportEvent(const Math::Vector2<GLsizei>& size) = 0;
|
||
|
|
|
||
|
14 years ago
|
/** @copydoc GlutApplication::drawEvent() */
|
||
|
14 years ago
|
virtual void drawEvent() = 0;
|
||
|
|
|
||
|
14 years ago
|
/** @copydoc GlutApplication::swapBuffers() */
|
||
|
14 years ago
|
inline void swapBuffers() { SDL_GL_SwapWindow(window); }
|
||
|
|
|
||
|
14 years ago
|
/** @copydoc GlutApplication::redraw() */
|
||
|
14 years ago
|
inline void redraw() { _redraw = true; }
|
||
|
|
|
||
|
|
/*@}*/
|
||
|
|
|
||
|
14 years ago
|
/** @{ @name Keyboard handling */
|
||
|
|
public:
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief %Modifier
|
||
|
|
*
|
||
|
|
* @see Modifiers, keyPressEvent(), keyReleaseEvent(),
|
||
|
|
* mousePressEvent(), mouseReleaseEvent(), mouseMotionEvent()
|
||
|
|
*/
|
||
|
|
enum class Modifier: unsigned int {};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Set of modifiers
|
||
|
|
*
|
||
|
|
* @see keyPressEvent(), keyReleaseEvent()
|
||
|
|
*/
|
||
|
|
typedef Corrade::Containers::EnumSet<Modifier, unsigned int> Modifiers;
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
|
* @brief Key
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* @see keyPressEvent(), keyReleaseEvent()
|
||
|
|
*/
|
||
|
|
enum class Key: SDL_Keycode {
|
||
|
|
Up = SDLK_UP, /**< Up arrow */
|
||
|
|
Down = SDLK_DOWN, /**< Down arrow */
|
||
|
|
Left = SDLK_LEFT, /**< Left arrow */
|
||
|
|
Right = SDLK_RIGHT, /**< Right arrow */
|
||
|
|
Plus = SDLK_PLUS, /**< Plus */
|
||
|
|
Minus = SDLK_MINUS /**< Minus */
|
||
|
|
};
|
||
|
|
|
||
|
|
protected:
|
||
|
|
/**
|
||
|
|
* @brief Key press event
|
||
|
|
* @param key Key pressed
|
||
|
14 years ago
|
* @param modifiers Active modifiers (not yet implemented)
|
||
|
|
* @param position Cursor position (not yet implemented)
|
||
|
14 years ago
|
*/
|
||
|
14 years ago
|
virtual void keyPressEvent(Key key, Modifiers modifiers, const Math::Vector2<int>& position);
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief Key release event
|
||
|
14 years ago
|
* @param key Key released
|
||
|
14 years ago
|
* @param modifiers Active modifiers (not yet implemented)
|
||
|
|
* @param position Cursor position (not yet implemented)
|
||
|
14 years ago
|
*/
|
||
|
14 years ago
|
virtual void keyReleaseEvent(Key key, Modifiers modifiers, const Math::Vector2<int>& position);
|
||
|
14 years ago
|
|
||
|
|
/*@}*/
|
||
|
|
|
||
|
14 years ago
|
/** @{ @name Mouse handling */
|
||
|
|
|
||
|
|
public:
|
||
|
|
/**
|
||
|
|
* @brief Mouse button
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* @see mouseEvent()
|
||
|
|
*/
|
||
|
|
enum class MouseButton: Uint8 {
|
||
|
|
Left = SDL_BUTTON_LEFT, /**< Left button */
|
||
|
|
Middle = SDL_BUTTON_MIDDLE, /**< Middle button */
|
||
|
14 years ago
|
Right = SDL_BUTTON_RIGHT, /**< Right button */
|
||
|
|
WheelUp = 4, /**< Wheel up */
|
||
|
|
WheelDown = 5 /**< Wheel down */
|
||
|
14 years ago
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Mouse state
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* @see mouseEvent()
|
||
|
|
*/
|
||
|
|
enum class MouseState: Uint8 {
|
||
|
|
Pressed = SDL_PRESSED, /**< Button pressed */
|
||
|
|
Released = SDL_RELEASED /**< Button released */
|
||
|
|
};
|
||
|
|
|
||
|
|
protected:
|
||
|
|
/**
|
||
|
14 years ago
|
* @brief Mouse press event
|
||
|
|
* @param button Button pressed
|
||
|
14 years ago
|
* @param modifiers Active modifiers (not yet implemented)
|
||
|
14 years ago
|
* @param position Cursor position
|
||
|
14 years ago
|
*
|
||
|
14 years ago
|
* Called when mouse button is pressed. Default implementation does
|
||
|
|
* nothing.
|
||
|
|
*/
|
||
|
14 years ago
|
virtual void mousePressEvent(MouseButton button, Modifiers modifiers, const Math::Vector2<int>& position);
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief Mouse release event
|
||
|
|
* @param button Button released
|
||
|
14 years ago
|
* @param modifiers Active modifiers (not yet implemented)
|
||
|
14 years ago
|
* @param position Cursor position
|
||
|
|
*
|
||
|
|
* Called when mouse button is released. Default implementation does
|
||
|
|
* nothing.
|
||
|
14 years ago
|
*/
|
||
|
14 years ago
|
virtual void mouseReleaseEvent(MouseButton button, Modifiers modifiers, const Math::Vector2<int>& position);
|
||
|
14 years ago
|
|
||
|
|
/**
|
||
|
|
* @brief Mouse motion event
|
||
|
14 years ago
|
* @param modifiers Active modifiers (not yet implemented)
|
||
|
14 years ago
|
* @param position Mouse position relative to the window
|
||
|
|
*
|
||
|
|
* Called when mouse is moved. Default implementation does nothing.
|
||
|
|
*/
|
||
|
14 years ago
|
virtual void mouseMotionEvent(Modifiers modifiers, const Math::Vector2<int>& position);
|
||
|
14 years ago
|
|
||
|
|
/*@}*/
|
||
|
|
|
||
|
14 years ago
|
private:
|
||
|
|
SDL_Window* window;
|
||
|
|
SDL_GLContext context;
|
||
|
|
|
||
|
14 years ago
|
Context* c;
|
||
|
|
|
||
|
14 years ago
|
bool _redraw;
|
||
|
|
};
|
||
|
|
|
||
|
14 years ago
|
CORRADE_ENUMSET_OPERATORS(Sdl2Application::Modifiers)
|
||
|
14 years ago
|
|
||
|
14 years ago
|
/* Implementations for inline functions with unused parameters */
|
||
|
14 years ago
|
inline void Sdl2Application::keyPressEvent(Key, Modifiers, const Math::Vector2<int>&) {}
|
||
|
|
inline void Sdl2Application::keyReleaseEvent(Key, Modifiers, const Math::Vector2<int>&) {}
|
||
|
|
inline void Sdl2Application::mousePressEvent(MouseButton, Modifiers, const Math::Vector2<int>&) {}
|
||
|
|
inline void Sdl2Application::mouseReleaseEvent(MouseButton, Modifiers, const Math::Vector2<int>&) {}
|
||
|
|
inline void Sdl2Application::mouseMotionEvent(Modifiers, const Math::Vector2<int>&) {}
|
||
|
14 years ago
|
|
||
|
14 years ago
|
}}
|
||
|
|
|
||
|
|
#endif
|