openarras/include/render.h
2023-05-30 21:03:04 +08:00

99 lines
2.0 KiB
C++

#ifndef HEADER_ARRAS_RENDER_H
#define HEADER_ARRAS_RENDER_H
extern "C" {
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
}
#include "2d.h"
#include <fmt/core.h>
#include <memory>
#include <stdexcept>
#include <utility>
namespace arras {
struct Color {
uint8_t r, g, b, a;
Color();
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
};
std::shared_ptr<SDL_Rect> makeRect(int x, int y, int w, int h);
std::shared_ptr<SDL_FRect> makeFRect(const arras::Vec2& pos, const arras::Vec2& size);
class RenderSurface {
public:
RenderSurface(RenderSurface&& rs);
RenderSurface(const RenderSurface& rs);
explicit RenderSurface(SDL_RWops* src);
RenderSurface(int width, int height);
~RenderSurface();
SDL_Surface* nativeHandle() const noexcept;
int width() const noexcept;
int height() const noexcept;
private:
SDL_Surface* s;
};
class RenderWindow;
class RenderTexture {
RenderTexture(const RenderTexture&) = delete;
RenderTexture& operator=(const RenderTexture&) = delete;
public:
RenderTexture(RenderTexture&& rt);
RenderTexture(const RenderWindow& rd, SDL_RWops* src);
RenderTexture(const RenderWindow& rd, const RenderSurface& rs);
~RenderTexture();
SDL_Texture* nativeHandle() const noexcept;
int width() const noexcept;
int height() const noexcept;
std::pair<int, int> calcRenderSize(int w, int h) const noexcept;
private:
SDL_Texture* t;
int w, h;
};
class RenderWindow {
RenderWindow(const RenderWindow&) = delete;
RenderWindow& operator=(const RenderWindow&) = delete;
public:
RenderWindow(RenderWindow&& rd);
RenderWindow(int w, int h);
~RenderWindow();
SDL_Renderer* nativeHandle() const noexcept;
void renderTextureAt(const RenderTexture& t, const Vec2& pos) const;
void renderTextureAt(const RenderTexture& t, const Vec2& pos, const Vec2& direction) const;
void present() const noexcept;
void clear(const Color& c = {}) const;
int width() const noexcept;
int height() const noexcept;
private:
int w, h;
SDL_Window* window;
SDL_Renderer* renderer;
};
} // namespace arras
#endif