Program Listing for File SDL2.hpp

Return to documentation for file (gui/Demeter/Renderer/SDL2.hpp)

#pragma once

#include <memory>

#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_surface.h>

#include "SDL_timer.h"

struct SDL2 {
private:
  bool _isSDLInit = false;
  bool _isIMGInit = false;
  SDL_Window *_window;
  SDL_GLContext _context = nullptr;
  SDL_Event _event;
  size_t _width;
  size_t _height;

  void SetAttribute(SDL_GLattr attr, int value) const  // NOLINT
  {
    SDL_GL_SetAttribute(attr, value);
  }

public:
  SDL2() = default;
  ~SDL2();

  bool Init(size_t width = 800, size_t height = 600);

  void SetWindowSize(size_t width, size_t height)
  {
    _width = width;
    _height = height;
    glViewport(0, 0, (GLsizei)_width, (GLsizei)_height);
  }

  [[nodiscard]] size_t GetWidth() const
  {
    return _width;
  }

  [[nodiscard]] size_t GetHeight() const
  {
    return _height;
  }

  std::string GetError() const  // NOLINT
  {
    return SDL_GetError();
  }

  std::string GetIMGError() const  // NOLINT
  {
    return IMG_GetError();
  }

  void SwapWindow() const
  {
    SDL_GL_SwapWindow(_window);
  }

  void Clear(GLclampf r, GLclampf g, GLclampf b, GLclampf a) const  // NOLINT
  {
    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  }

  [[nodiscard]] Uint64 GetTicks64() const  // NOLINT
  {
    return SDL_GetTicks64();
  }

  [[nodiscard]] bool PollEvent()
  {
    return SDL_PollEvent(&_event);
  }

  [[nodiscard]] const SDL_Event &GetEvent() const
  {
    return _event;
  }

  [[nodiscard]] std::unique_ptr<SDL_Surface>
  IMGLoad(const std::string &s) const  // NOLINT
  {
    return std::unique_ptr<SDL_Surface>(IMG_Load(s.c_str()));
  }
};