Remove Event Handling in main file

This commit is contained in:
Kirigaya Kazuto 2017-04-04 09:49:18 +08:00
parent ccb47aad08
commit 499c25ddd3
4 changed files with 3 additions and 407 deletions

View File

@ -969,365 +969,6 @@ namespace MiniEngine
return Mix_HaltChannel(id);
}
int Event::gettype()
{
return e.type;
}
int MouseButtonEvent::getx()
{
return e.button.x;
}
int MouseButtonEvent::gety()
{
return e.button.y;
}
int MouseButtonEvent::getbutton()
{
return e.button.button;
}
Event EventEngine::poll(bool mustNew) /// mustNew: false=SDL_PollEvent(&e) returns 0 ; true=SDL_PollEvent(&e) returns 1
{
Event e;
while (1)
{
int ret = SDL_PollEvent(&e.e);
if ((mustNew&&ret) || !mustNew) break;
}
return e;
}
Event EventEngine::wait()
{
Event e;
SDL_WaitEvent(&e.e);
return e;
}
Event EventEngine::waitfor(int ms)
{
Event e;
SDL_WaitEventTimeout(&e.e, ms);
return e;
}
namespace EventHandle
{
const int _internal_event_num=46;
std::vector<std::map<int,DispatcherType>> disvec(_internal_event_num);
int TransIDToEvent(int ID)
{
switch(ID)
{
case 0:
return SDL_QUIT;
case 1:
return SDL_APP_TERMINATING;
case 2:
return SDL_APP_LOWMEMORY;
case 3:
return SDL_APP_WILLENTERBACKGROUND;
case 4:
return SDL_APP_DIDENTERBACKGROUND;
case 5:
return SDL_APP_WILLENTERFOREGROUND;
case 6:
return SDL_APP_DIDENTERFOREGROUND;
case 7:
return SDL_WINDOWEVENT;
case 8:
return SDL_SYSWMEVENT;
case 9:
return SDL_KEYDOWN;
case 10:
return SDL_KEYUP;
case 11:
return SDL_TEXTEDITING;
case 12:
return SDL_TEXTINPUT;
case 13:
return SDL_KEYMAPCHANGED;
case 14:
return SDL_MOUSEMOTION;
case 15:
return SDL_MOUSEBUTTONDOWN;
case 16:
return SDL_MOUSEBUTTONUP;
case 17:
return SDL_MOUSEWHEEL;
case 18:
return SDL_JOYAXISMOTION;
case 19:
return SDL_JOYBALLMOTION;
case 20:
return SDL_JOYHATMOTION;
case 21:
return SDL_JOYBUTTONDOWN;
case 22:
return SDL_JOYBUTTONUP;
case 23:
return SDL_JOYDEVICEADDED;
case 24:
return SDL_JOYDEVICEREMOVED;
case 25:
return SDL_CONTROLLERAXISMOTION;
case 26:
return SDL_CONTROLLERBUTTONDOWN;
case 27:
return SDL_CONTROLLERBUTTONUP;
case 28:
return SDL_CONTROLLERDEVICEADDED;
case 29:
return SDL_CONTROLLERDEVICEREMOVED;
case 30:
return SDL_CONTROLLERDEVICEREMAPPED;
case 31:
return SDL_FINGERDOWN;
case 32:
return SDL_FINGERUP;
case 33:
return SDL_FINGERMOTION;
case 34:
return SDL_DOLLARGESTURE;
case 35:
return SDL_DOLLARRECORD;
case 36:
return SDL_MULTIGESTURE;
case 37:
return SDL_CLIPBOARDUPDATE;
#ifdef __C4DROID__ /// C4droid does not support SDL_DROP...
case 38:
return SDL_DROPFILE;
case 39:
return SDL_DROPTEXT;
case 40:
return SDL_DROPBEGIN;
case 41:
return SDL_DROPCOMPLETE;
#endif
case 42:
return SDL_AUDIODEVICEADDED;
case 43:
return SDL_AUDIODEVICEREMOVED;
case 44:
return SDL_RENDER_TARGETS_RESET;
case 45:
return SDL_RENDER_DEVICE_RESET;
default:
return 0;/// Nothing.
}
}
int TransEventToID(int EventType)
{
switch(EventType)
{
case SDL_QUIT:
return 0;
case SDL_APP_TERMINATING:
return 1;
case SDL_APP_LOWMEMORY:
return 2;
case SDL_APP_WILLENTERBACKGROUND:
return 3;
case SDL_APP_DIDENTERBACKGROUND:
return 4;
case SDL_APP_WILLENTERFOREGROUND:
return 5;
case SDL_APP_DIDENTERFOREGROUND:
return 6;
case SDL_WINDOWEVENT:
return 7;
case SDL_SYSWMEVENT:
return 8;
case SDL_KEYDOWN:
return 9;
case SDL_KEYUP:
return 10;
case SDL_TEXTEDITING:
return 11;
case SDL_TEXTINPUT:
return 12;
case SDL_KEYMAPCHANGED:
return 13;
case SDL_MOUSEMOTION:
return 14;
case SDL_MOUSEBUTTONDOWN:
return 15;
case SDL_MOUSEBUTTONUP:
return 16;
case SDL_MOUSEWHEEL:
return 17;
case SDL_JOYAXISMOTION:
return 18;
case SDL_JOYBALLMOTION:
return 19;
case SDL_JOYHATMOTION:
return 20;
case SDL_JOYBUTTONDOWN:
return 21;
case SDL_JOYBUTTONUP:
return 22;
case SDL_JOYDEVICEADDED:
return 23;
case SDL_JOYDEVICEREMOVED:
return 24;
case SDL_CONTROLLERAXISMOTION:
return 25;
case SDL_CONTROLLERBUTTONDOWN:
return 26;
case SDL_CONTROLLERBUTTONUP:
return 27;
case SDL_CONTROLLERDEVICEADDED:
return 28;
case SDL_CONTROLLERDEVICEREMOVED:
return 29;
case SDL_CONTROLLERDEVICEREMAPPED:
return 30;
case SDL_FINGERDOWN:
return 31;
case SDL_FINGERUP:
return 32;
case SDL_FINGERMOTION:
return 33;
case SDL_DOLLARGESTURE:
return 34;
case SDL_DOLLARRECORD:
return 35;
case SDL_MULTIGESTURE:
return 36;
case SDL_CLIPBOARDUPDATE:
return 37;
#ifdef __C4DROID__ /// C4droid does not support SDL_DROP...
case SDL_DROPFILE:
return 38;
case SDL_DROPTEXT:
return 39;
case SDL_DROPBEGIN:
return 40;
case SDL_DROPCOMPLETE:
return 41;
#endif
case SDL_AUDIODEVICEADDED:
return 42;
case SDL_AUDIODEVICEREMOVED:
return 43;
case SDL_RENDER_TARGETS_RESET:
return 44;
case SDL_RENDER_DEVICE_RESET:
return 45;
default:
return -1;/// Error
}
}
std::mutex mdisvec_counter;
int disvec_counter = 0;
std::map<int, UpdaterType> upvec;
std::mutex mupvec_counter;
int upvec_counter = 0;
int RegistDispatcher(int EventType,DispatcherType func)
{
mdisvec_counter.lock();
int id = disvec_counter++;
mdisvec_counter.unlock();
disvec.at(TransEventToID(EventType)).insert(make_pair(id, func));
return id;
}
int UnregistDispatcher(int callbackid)
{
for (auto it = disvec.begin(); it != disvec.end(); it++)
{
for(auto i=it->begin(); i!=it->end(); i++)
{
if (callbackid == i->first)
{
it->erase(i);
return 0;
}
}
}
return -1;
}
int RegistUpdater(UpdaterType func)
{
mupvec_counter.lock();
int id = upvec_counter++;
mupvec_counter.unlock();
upvec.insert(make_pair(id, func));
return id;
}
int UnregistUpdater(int callbackid)
{
for (auto it = upvec.begin(); it != upvec.end(); it++)
{
if (callbackid == it->first)
{
upvec.erase(it);
return 0;
}
}
return -1;
}
void Dispatcher(SDL_Event e, int & running, int & update)
{
for (auto& func : disvec.at(TransEventToID(e.type)))
{
int r = 1;
int u = 0;
func.second(e, r, u);
running &= r;
update |= u;
}
}
void Updater(Renderer & rnd)
{
for (auto& func : upvec)
{
func.second(rnd);
}
}
void Loop(Renderer & rnd)
{
SDL_Event e;
int running = 1;
int update = 1;
while (running)
{
while (!update&&SDL_WaitEvent(&e))
{
Dispatcher(e, running, update);
}
rnd.clear();
Updater(rnd);
rnd.update();
update = 0;
}
}
}/// End of namespace EventHandle
AudioPlayer::_Audio* AudioPlayer::_sysAudio = nullptr;
int AudioPlayer::_sysAudioCounter = 0;

View File

@ -356,56 +356,8 @@ namespace MiniEngine
void pause(ChannelID id);
void resume(ChannelID id);
int stop(ChannelID id);
private:
};
class Event
{
public:
int gettype();
protected:
Event() = default;
SDL_Event e;
private:
friend class EventEngine;
};
class MouseButtonEvent : public Event
{
public:
int getx();
int gety();
int getbutton();
};
class EventEngine
{
public:
Event poll(bool mustNew = false);
Event wait();
Event waitfor(int ms);
};
namespace EventHandle
{
using DispatcherType = std::function<void(SDL_Event e, int& running, int& update)>;
int RegistDispatcher(int EventType,DispatcherType func);
int UnregistDispatcher(int callbackid);
using UpdaterType = std::function<void(Renderer& rnd)>;
int RegistUpdater(UpdaterType func);
int UnregistUpdater(int callbackid);
void Dispatcher(SDL_Event e, int& running, int& update);
void Updater(Renderer& rnd);
void Loop(Renderer& rnd);
}/// End of namespace MiniEngine::EventHandle
class StringEngine
{
public:

1
MiniEngine_Event.cpp Normal file
View File

@ -0,0 +1 @@
#include "MiniEngine_Event.h"

2
MiniEngine_Event.h Normal file
View File

@ -0,0 +1,2 @@
#pragma once
#include "MiniEngine.h"