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.
119 lines
4.0 KiB
119 lines
4.0 KiB
|
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.
|
||
|
|
*/
|
||
|
|
|
||
|
14 years ago
|
#include "Sdl2Application.h"
|
||
|
14 years ago
|
|
||
|
|
#include "Context.h"
|
||
|
14 years ago
|
#include "ExtensionWrangler.h"
|
||
|
14 years ago
|
|
||
|
14 years ago
|
namespace Magnum { namespace Platform {
|
||
|
14 years ago
|
|
||
|
14 years ago
|
Sdl2Application::Sdl2Application(int, char**, const std::string& name, const Math::Vector2<GLsizei>& size): _redraw(true) {
|
||
|
14 years ago
|
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||
|
|
Error() << "Cannot initialize SDL.";
|
||
|
|
exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
/* Request OpenGL 3.2 */
|
||
|
14 years ago
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||
|
14 years ago
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
||
|
14 years ago
|
|
||
|
|
/* Enable double buffering and 24bt depth buffer */
|
||
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||
|
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||
|
|
|
||
|
|
window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||
|
|
size.x(), size.y(), SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
|
||
|
|
if(!window) {
|
||
|
|
Error() << "Cannot create window.";
|
||
|
|
exit(2);
|
||
|
|
}
|
||
|
|
|
||
|
|
context = SDL_GL_CreateContext(window);
|
||
|
|
|
||
|
|
/* This must be enabled, otherwise (on my NVidia) it crashes when creating
|
||
|
|
VAO. WTF. */
|
||
|
14 years ago
|
ExtensionWrangler::initialize(ExtensionWrangler::ExperimentalFeatures::Enable);
|
||
|
14 years ago
|
|
||
|
|
/* Push resize event, so viewportEvent() is called at startup */
|
||
|
|
SDL_Event* sizeEvent = new SDL_Event;
|
||
|
|
sizeEvent->type = SDL_WINDOWEVENT;
|
||
|
|
sizeEvent->window.event = SDL_WINDOWEVENT_RESIZED;
|
||
|
|
sizeEvent->window.data1 = size.x();
|
||
|
|
sizeEvent->window.data2 = size.y();
|
||
|
|
SDL_PushEvent(sizeEvent);
|
||
|
14 years ago
|
|
||
|
|
c = new Context;
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
Sdl2Application::~Sdl2Application() {
|
||
|
14 years ago
|
delete c;
|
||
|
|
|
||
|
14 years ago
|
SDL_GL_DeleteContext(context);
|
||
|
|
SDL_DestroyWindow(window);
|
||
|
|
SDL_Quit();
|
||
|
|
}
|
||
|
|
|
||
|
14 years ago
|
int Sdl2Application::exec() {
|
||
|
14 years ago
|
for(;;) {
|
||
|
|
SDL_Event event;
|
||
|
|
|
||
|
|
while(SDL_PollEvent(&event)) {
|
||
|
|
switch(event.type) {
|
||
|
|
case SDL_WINDOWEVENT:
|
||
|
|
switch(event.window.event) {
|
||
|
|
case SDL_WINDOWEVENT_RESIZED:
|
||
|
|
viewportEvent({event.window.data1, event.window.data2});
|
||
|
|
_redraw = true;
|
||
|
|
break;
|
||
|
|
case SDL_WINDOWEVENT_EXPOSED:
|
||
|
|
_redraw = true;
|
||
|
|
break;
|
||
|
|
} break;
|
||
|
14 years ago
|
case SDL_KEYDOWN:
|
||
|
14 years ago
|
keyPressEvent(static_cast<Key>(event.key.keysym.sym), Modifiers(), {});
|
||
|
14 years ago
|
break;
|
||
|
|
case SDL_KEYUP:
|
||
|
14 years ago
|
keyReleaseEvent(static_cast<Key>(event.key.keysym.sym), Modifiers(), {});
|
||
|
14 years ago
|
break;
|
||
|
14 years ago
|
case SDL_MOUSEBUTTONDOWN:
|
||
|
14 years ago
|
mousePressEvent(static_cast<MouseButton>(event.button.button), Modifiers(), {event.button.x, event.button.y});
|
||
|
14 years ago
|
break;
|
||
|
14 years ago
|
case SDL_MOUSEBUTTONUP:
|
||
|
14 years ago
|
mouseReleaseEvent(static_cast<MouseButton>(event.button.button), Modifiers(), {event.button.x, event.button.y});
|
||
|
14 years ago
|
break;
|
||
|
|
case SDL_MOUSEWHEEL:
|
||
|
14 years ago
|
if(event.wheel.y != 0)
|
||
|
|
mousePressEvent(event.wheel.y < 0 ? MouseButton::WheelUp : MouseButton::WheelDown, Modifiers(), {event.wheel.x, event.wheel.y});
|
||
|
14 years ago
|
break;
|
||
|
|
case SDL_MOUSEMOTION:
|
||
|
14 years ago
|
mouseMotionEvent(Modifiers(), {event.motion.x, event.motion.y});
|
||
|
14 years ago
|
break;
|
||
|
14 years ago
|
case SDL_QUIT:
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if(_redraw) {
|
||
|
|
_redraw = false;
|
||
|
14 years ago
|
drawEvent();
|
||
|
14 years ago
|
} else Corrade::Utility::sleep(5);
|
||
|
|
}
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
}}
|