openspades/Sources/Gui/SDLAsyncRunner.cpp

293 lines
6.9 KiB
C++
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
2013-08-18 16:18:06 +09:00
#include "SDLAsyncRunner.h"
#include "../Core/Thread.h"
#include "../Client/Client.h"
#include "../Client/AsyncRenderer.h"
#include "../Core/ConcurrentDispatch.h"
2014-03-16 23:07:57 +09:00
#include <Core/AutoLocker.h>
2013-08-18 16:18:06 +09:00
namespace spades {
namespace gui {
SDLAsyncRunner::SDLAsyncRunner():
rendererErrorOccured(false){
2013-11-22 22:50:14 +09:00
currentView = NULL;
2014-03-16 23:07:57 +09:00
PulledState state;
state.acceptsTextInput = false;
state.needsAbsoluteMouseCoord = true;
this->state = state;
2013-08-18 16:18:06 +09:00
}
SDLAsyncRunner::~SDLAsyncRunner(){
}
class SDLAsyncRunner::ClientThread: public Thread {
public:
SDLAsyncRunner *runner;
client::IRenderer *renderer;
client::IAudioDevice *audio;
virtual ~ClientThread() {
renderer->Release();
}
2013-08-18 16:18:06 +09:00
virtual void Run() {
2013-08-26 01:27:44 +09:00
SPLog("Starting Client Thread");
2013-08-18 16:18:06 +09:00
runner->ClientThreadProc(renderer, audio);
2013-08-26 01:27:44 +09:00
SPLog("Client Thread Done");
2013-08-18 16:18:06 +09:00
}
};
void SDLAsyncRunner::RunClientLoop(client::IRenderer *renderer,
client::IAudioDevice *audio) {
client::AsyncRenderer *asyncRenderer = new client::AsyncRenderer(renderer,
2013-08-18 16:18:06 +09:00
DispatchQueue::GetThreadQueue());
modState = 0;
ClientThread *cliThread = new ClientThread();
cliThread->runner = this;
cliThread->renderer = asyncRenderer;
cliThread->audio = audio;
cliThread->Start();
2013-08-18 16:18:06 +09:00
2014-03-16 23:07:57 +09:00
bool editing = false;
bool absoluteMouseCoord = true;
2013-08-26 01:27:44 +09:00
SPLog("Main event loop started");
try{
while(cliThread->IsAlive()) {
2013-08-18 16:18:06 +09:00
modState = SDLRunner::GetModState();
2013-08-18 16:18:06 +09:00
if(currentView){
class SDLEventDispatch: public ConcurrentDispatch {
SDLAsyncRunner *runner;
SDL_Event ev;
public:
SDLEventDispatch(SDLAsyncRunner *runner,
const SDL_Event& ev):
runner(runner),
ev(ev){
2013-08-18 16:18:06 +09:00
}
virtual void Run() {
View *view = runner->currentView;
if(view){
runner->ProcessEvent(ev, view);
}
}
};
SDL_Event event;
2013-08-18 16:18:06 +09:00
if(SDL_WaitEvent(&event)){
{
SDLEventDispatch *disp = new SDLEventDispatch(this, event);
// FIXME: cliQueue may be deleted..
if(cliQueue){
disp->StartOn(cliQueue);
disp->Release();
}else{
delete disp;
}
}
2013-08-18 16:18:06 +09:00
while(SDL_PollEvent(&event)) {
SDLEventDispatch *disp = new SDLEventDispatch(this, event);
// FIXME: cliQueue may be deleted..
if(cliQueue){
disp->StartOn(cliQueue);
disp->Release();
}else{
delete disp;
}
2013-08-18 16:18:06 +09:00
}
}
}else{
SDL_Event event;
SDL_WaitEvent(&event);
if(event.type == SDL_QUIT){
break;
}
2013-08-18 16:18:06 +09:00
}
2014-03-16 23:07:57 +09:00
PulledState state;
{
AutoLocker guard(&stateMutex);
state = this->state;
}
bool ed = state.acceptsTextInput;
if(ed && !editing) {
SDL_StartTextInput();
}else if(!ed && editing){
SDL_StopTextInput();
}
editing = ed;
if(editing){
SDL_SetTextInputRect(&state.textInputRect);
}
bool ab = state.needsAbsoluteMouseCoord;
if(ab != absoluteMouseCoord) {
absoluteMouseCoord = ab;
SDL_SetRelativeMouseMode(absoluteMouseCoord?SDL_FALSE:SDL_TRUE);
}
DispatchQueue::GetThreadQueue()->ProcessQueue();
2013-08-18 16:18:06 +09:00
}
}catch(const std::exception& ex){
rendererErrorOccured = true;
SPLog("Renderer error:\n%s", ex.what());
cliThread->MarkForAutoDeletion();
SPLog("Main event loop terminated");
throw;
2013-08-18 16:18:06 +09:00
}
2013-08-26 01:27:44 +09:00
SPLog("Main event loop ended");
2013-08-18 16:18:06 +09:00
if(!clientError.empty()){
2013-08-26 01:27:44 +09:00
SPLog("Client reported an error: \n%s",
clientError.c_str());
2013-08-18 16:18:06 +09:00
SPRaise("Client error:\n%s", clientError.c_str());
}
}
void SDLAsyncRunner::ClientThreadProc(client::IRenderer *renderer, client::IAudioDevice *audio){
try{
2013-11-22 22:50:14 +09:00
Handle<View> view(CreateView(renderer, audio), false);
2013-08-18 16:18:06 +09:00
Uint32 ot = SDL_GetTicks();
bool running = true;
2013-11-22 22:50:14 +09:00
currentView = view;
2013-08-18 16:18:06 +09:00
cliQueue = DispatchQueue::GetThreadQueue();
bool lastShift = false;
bool lastCtrl = false;
2013-12-25 22:55:05 +09:00
bool lastGui = false;
bool lastAlt = false;
2013-08-18 16:18:06 +09:00
while(running && !rendererErrorOccured){
2013-08-18 16:18:06 +09:00
DispatchQueue::GetThreadQueue()->ProcessQueue();
Uint32 dt = SDL_GetTicks() - ot;
2013-11-22 22:50:14 +09:00
view->RunFrame((float)dt / 1000.f);
2013-08-18 16:18:06 +09:00
ot += dt;
if(view->WantsToBeClosed() || rendererErrorOccured){
2013-11-22 22:50:14 +09:00
view->Closing();
2013-08-18 16:18:06 +09:00
running = false;
break;
}
int modState = GetModState();
if(modState & (KMOD_CTRL | KMOD_LCTRL | KMOD_RCTRL)){
if(!lastCtrl){
2013-11-22 22:50:14 +09:00
view->KeyEvent("Control", true);
2013-08-18 16:18:06 +09:00
lastCtrl = true;
}
}else{
if(lastCtrl){
2013-11-22 22:50:14 +09:00
view->KeyEvent("Control", false);
2013-08-18 16:18:06 +09:00
lastCtrl = false;
}
}
if(modState & (KMOD_SHIFT | KMOD_LSHIFT | KMOD_RSHIFT)){
if(!lastShift){
2013-11-22 22:50:14 +09:00
view->KeyEvent("Shift", true);
2013-08-18 16:18:06 +09:00
lastShift = true;
}
}else{
if(lastShift){
2013-11-22 22:50:14 +09:00
view->KeyEvent("Shift", false);
2013-08-18 16:18:06 +09:00
lastShift = false;
}
}
2013-12-25 22:55:05 +09:00
if(modState & KMOD_GUI){
if(!lastGui){
view->KeyEvent("Meta", true);
lastGui = true;
}
}else{
if(lastGui){
view->KeyEvent("Meta", false);
lastGui = false;
}
}
if(modState & KMOD_ALT){
if(!lastAlt){
view->KeyEvent("Alt", true);
lastAlt = true;
}
}else{
if(lastAlt){
view->KeyEvent("Alt", false);
lastAlt = false;
}
}
2014-03-16 23:07:57 +09:00
PulledState state;
state.acceptsTextInput = view->AcceptsTextInput();
if(state.acceptsTextInput) {
AABB2 rt = view->GetTextInputRect();
SDL_Rect& srt = state.textInputRect;
srt.x = (int)rt.GetMinX();
srt.y = (int)rt.GetMinY();
srt.w = (int)rt.GetWidth();
srt.h = (int)rt.GetHeight();
}
state.needsAbsoluteMouseCoord = view->NeedsAbsoluteMouseCoordinate();
2013-12-25 22:55:05 +09:00
2014-03-16 23:07:57 +09:00
{
AutoLocker guard(&stateMutex);
this->state = state;
}
2013-12-09 17:36:05 +09:00
2013-08-18 16:18:06 +09:00
}
cliQueue = NULL;
2013-11-22 22:50:14 +09:00
currentView = NULL;
2013-08-18 16:18:06 +09:00
}catch(const std::exception& ex){
2013-08-26 01:27:44 +09:00
SPLog("Exiting Client Event Loop due to exception:\n%s",
ex.what());
2013-08-18 16:18:06 +09:00
cliQueue = NULL;
2013-11-22 22:50:14 +09:00
currentView = NULL;
2013-08-18 16:18:06 +09:00
clientError = ex.what();
}
}
}
}