Euphoria
sdlwindow.cc
Go to the documentation of this file.
1 #include "window/sdlwindow.h"
2 
3 #include "log/log.h"
4 
5 #include "SDL.h"
6 
7 namespace eu::window
8 {
10  (
11  const std::string& title,
12  int width,
13  int height,
14  bool resize
15  )
16  : window(SDL_CreateWindow
17  (
18  title.c_str(),
19  SDL_WINDOWPOS_UNDEFINED,
20  SDL_WINDOWPOS_UNDEFINED,
21  width,
22  height,
23  SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | (resize ? SDL_WINDOW_RESIZABLE : 0)
24  ))
25  {
26 
27  if(window == nullptr)
28  {
29  LOG_ERROR("Failed to create window {0}", SDL_GetError());
30  }
31  }
32 
34  {
35  SDL_DestroyWindow(window);
36  }
37 
38  void
40  {
41  switch(mb)
42  {
44  SDL_SetRelativeMouseMode(SDL_FALSE);
45  SDL_SetWindowGrab(window, SDL_FALSE);
46  break;
48  SDL_SetWindowGrab(window, SDL_FALSE);
49  SDL_SetRelativeMouseMode(SDL_TRUE);
50  break;
52  SDL_SetRelativeMouseMode(SDL_FALSE);
53  SDL_SetWindowGrab(window, SDL_TRUE);
54  break;
55  default:
56  DIE("inavlid mouse behaviour");
57  }
58  }
59 
60  void
61  enable_char_event(bool enable)
62  {
63  if(enable)
64  {
65  SDL_StartTextInput();
66  }
67  else
68  {
69  SDL_StopTextInput();
70  }
71  }
72 
73  [[nodiscard]] vec2i
75  {
76  vec2i ret {0, 0};
77  int width = 0;
78  int height = 0;
79  SDL_GetWindowSize(window, &width, &height);
80  SDL_GetMouseState(&ret.x, &ret.y);
81  ret.y = height - (ret.y + 1);
82  return ret;
83  }
84 }
#define DIE(message)
Definition: assert.h:67
#define LOG_ERROR(...)
Definition: log.h:9
void enable_char_event(bool enable)
Definition: sdlwindow.cc:61
Definition: vec2.h:72
SdlWindow(const std::string &title, int width, int height, bool resize=false)
Definition: sdlwindow.cc:10
vec2i get_mouse_position() const
Definition: sdlwindow.cc:74
SDL_Window * window
Definition: sdlwindow.h:39
void set_mouse_behaviour(core::MouseBehaviour) const
Definition: sdlwindow.cc:39