A 2D tile-based sandbox game.
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.

Window.cc 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "Window.h"
  2. #include <SDL.h>
  3. #include "gl.h"
  4. #include "util.h"
  5. namespace Cygnet {
  6. struct WindowState {
  7. SDL_Window *window;
  8. SDL_GLContext glctx;
  9. };
  10. Window::Window(const char *name, int w, int h):
  11. state_(std::make_unique<WindowState>()), w_(w), h_(h) {
  12. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
  13. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  14. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
  15. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  16. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  17. //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
  18. //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
  19. SDL_GL_SetSwapInterval(1);
  20. state_->window = SDL_CreateWindow(name,
  21. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h,
  22. SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE |
  23. SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL);
  24. sdlCheck(state_->window != NULL);
  25. state_->glctx = SDL_GL_CreateContext(state_->window);
  26. glCheck();
  27. makeCurrent();
  28. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  29. glEnable(GL_BLEND);
  30. glCheck();
  31. SDL_GetWindowSize(state_->window, &w, &h);
  32. onResize(w, h);
  33. }
  34. Window::~Window() {
  35. SDL_DestroyWindow(state_->window);
  36. }
  37. void Window::makeCurrent() {
  38. SDL_GL_MakeCurrent(state_->window, state_->glctx);
  39. glCheck();
  40. }
  41. void Window::clear(Color color) {
  42. glClearColor(color.r, color.g, color.b, color.a);
  43. glClear(GL_COLOR_BUFFER_BIT);
  44. glCheck();
  45. }
  46. void Window::flip() {
  47. SDL_GL_SwapWindow(state_->window);
  48. glCheck();
  49. }
  50. void Window::onResize(int w, int h) {
  51. w_ = w;
  52. h_ = h;
  53. int dw, dh;
  54. SDL_GL_GetDrawableSize(state_->window, &dw, &dh);
  55. glViewport(0, 0, dw, dh);
  56. glCheck();
  57. }
  58. SDL_Window *Window::sdlWindow() {
  59. return state_->window;
  60. }
  61. }