Btk
core.hpp
1 #if !defined(_BOXIMPL_CORE_HPP_)
2 #define _BOXIMPL_CORE_HPP_
3 #include <SDL2/SDL.h>
4 #include <condition_variable>
5 #include <unordered_map>
6 #include <exception>
7 #include <atomic>
8 #include <mutex>
9 #include <queue>
10 #include <list>
11 
12 #include "thread.hpp"
13 #include "../module.hpp"
14 
15 namespace Btk{
16  struct WindowImpl;
17  struct System{
18  struct ExitHandler{
19  //Function pointer
20  typedef void (*FnPtr)(void*);
21  FnPtr fn;
22  void *data;
23  void operator ()() const{
24  fn(data);
25  }
26  };
27  struct EventHandler{
28  typedef void (*FnPtr)(const SDL_Event &ev,void *data);
29  FnPtr fn;
30  void *data;
31  void operator()(SDL_Event &ev) const{
32  fn(ev,data);
33  }
34  };
35  System();
36  ~System();
37  static System *instance;//instance of system
38  static bool is_running;
39  void run();
40  //Window Register
41  void register_window(WindowImpl *impl);
42  void unregister_window(WindowImpl *impl);
43  //Handle event
44  inline void on_dropev(const SDL_Event &event);//Handle SDL_DropEvent
45  inline void on_windowev(const SDL_Event &event);//Handle SDL_WINDOWEVENT
46  inline void on_keyboardev(const SDL_Event &event);//Handle SDL_KeyboardEvent
47  inline void on_mousewheel(const SDL_Event &event);//Handle SDL_MouseWheel
48  inline void on_mousemotion(const SDL_Event &event);//Handle SDL_MouseMotion
49  inline void on_mousebutton(const SDL_Event &event);//Handle SDL_MouseButton
50  inline void on_textinput(const SDL_Event &event);
51  //defercall in eventloop
52  void defer_call(void(* fn)(void*),void *data = nullptr);
53  //Get window from WindowID
54  WindowImpl *get_window(Uint32 winid);//< It is thread unsafe
55  WindowImpl *get_window_s(Uint32 winid);//< It is thread safe
56 
57  std::unordered_map<Uint32,WindowImpl*> wins_map;//Windows map
58  std::unordered_map<Uint32,EventHandler> evcbs_map;//Event callbacks map
59  std::recursive_mutex map_mtx;
60  Uint32 defer_call_ev_id;//defer call Event ID
61  Uint32 dispatch_ev_id;//dispatch our event Event ID
62  //called after a exception was throwed
63  //return false to abort program
64  bool (*handle_exception)(std::exception *) = nullptr;
65  //called atexit
66  std::list<ExitHandler> atexit_handlers;
67  //register handlers
68  void atexit(void (*fn)(void *),void *data);
69  void atexit(void (*fn)());
70 
71  void regiser_eventcb(Uint32 evid,EventHandler::FnPtr ptr,void *data);
72 
73  std::list<Module> modules_list;
74  //std::list<RendererCreateFn> render_list;
75  //Init Global
76  static int Init();
77  static void Quit();
78  };
79  BTKAPI int run();
80  BTKAPI void Init();
81  //Exit the app
82  BTKAPI void Exit(int code);
83  inline System &Instance(){
84  return *(System::instance);
85  }
86 };
87 
88 
89 #endif // _BOXIMPL_CORE_HPP_
Definition: core.hpp:17
This header include many useful containers.
Definition: async.hpp:7
BTKAPI void Exit(int code=EXIT_SUCCESS)
End the event loop.
Definition: core.cpp:402
Definition: core.hpp:18
BTKAPI int run()
Enter the EventLoop.
Definition: core.cpp:58
Definition: window.hpp:39
Definition: core.hpp:27