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.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. SDL_GetWindowSize(state_->window, &w, &h);
  29. onResize(w, h);
  30. }
  31. Window::~Window() {
  32. SDL_DestroyWindow(state_->window);
  33. }
  34. void Window::makeCurrent() {
  35. SDL_GL_MakeCurrent(state_->window, state_->glctx);
  36. glCheck();
  37. }
  38. void Window::clear(Color color) {
  39. glClearColor(color.r, color.g, color.b, color.a);
  40. glClear(GL_COLOR_BUFFER_BIT);
  41. glCheck();
  42. }
  43. void Window::flip() {
  44. SDL_GL_SwapWindow(state_->window);
  45. glCheck();
  46. }
  47. void Window::onResize(int w, int h) {
  48. w_ = w;
  49. h_ = h;
  50. int dw, dh;
  51. SDL_GL_GetDrawableSize(state_->window, &dw, &dh);
  52. glViewport(0, 0, dw, dh);
  53. glCheck();
  54. }
  55. SDL_Window *Window::sdlWindow() {
  56. return state_->window;
  57. }
  58. }