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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. state_->window = SDL_CreateWindow(name,
  13. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h,
  14. SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE |
  15. SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL);
  16. sdlCheck(state_->window != NULL);
  17. state_->glctx = SDL_GL_CreateContext(state_->window);
  18. glCheck();
  19. makeCurrent();
  20. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  21. glEnable(GL_BLEND);
  22. glCheck();
  23. SDL_GetWindowSize(state_->window, &w, &h);
  24. onResize(w, h);
  25. }
  26. Window::~Window() {
  27. SDL_DestroyWindow(state_->window);
  28. }
  29. void Window::makeCurrent() {
  30. SDL_GL_MakeCurrent(state_->window, state_->glctx);
  31. glCheck();
  32. }
  33. void Window::clear() {
  34. glClear(GL_COLOR_BUFFER_BIT);
  35. glCheck();
  36. }
  37. void Window::flip() {
  38. SDL_GL_SwapWindow(state_->window);
  39. glCheck();
  40. }
  41. void Window::onResize(int w, int h) {
  42. w_ = w;
  43. h_ = h;
  44. glViewport(0, 0, w, h);
  45. glCheck();
  46. }
  47. }