(fix): use std::unique_ptr to manage SDL_Window & SDL_Renderer

This commit is contained in:
VisualGMQ 2023-06-25 11:20:06 +08:00
parent 1d8771c80e
commit c945a0a626
5 changed files with 26 additions and 46 deletions

View File

@ -2,15 +2,15 @@
#include "pch.hpp" #include "pch.hpp"
#include "window.hpp" #include "window.hpp"
inline auto RendererDestroy = [](SDL_Renderer* renderer) {
SDL_DestroyRenderer(renderer);
};
class Renderer final { class Renderer final {
public: public:
friend struct Context; friend struct Context;
Renderer(const Window&); Renderer(const Window&);
~Renderer();
Renderer(const Renderer&) = delete;
Renderer(Renderer&&);
Renderer& operator=(const Renderer&) = delete;
void SetColor(const SDL_Color&); void SetColor(const SDL_Color&);
void Clear(); void Clear();
@ -21,5 +21,5 @@ public:
void DrawTexture(SDL_Texture*, const SDL_Rect&, int x, int y); void DrawTexture(SDL_Texture*, const SDL_Rect&, int x, int y);
private: private:
SDL_Renderer* renderer_; std::unique_ptr<SDL_Renderer, decltype(RendererDestroy)> renderer_;
}; };

View File

@ -2,17 +2,16 @@
#include "pch.hpp" #include "pch.hpp"
inline auto WindowDestroy = [](SDL_Window* window) {
SDL_DestroyWindow(window);
};
class Window final { class Window final {
public: public:
friend class Renderer; friend class Renderer;
Window(const std::string& title, int w, int h); Window(const std::string& title, int w, int h);
Window(const Window&) = delete;
Window(Window&&);
Window& operator=(const Window&) = delete;
~Window();
private: private:
SDL_Window* window_; std::unique_ptr<SDL_Window, decltype(WindowDestroy)> window_;
}; };

View File

@ -86,11 +86,11 @@ SDL_Texture* loadTexture(SDL_Renderer* renderer, const std::string& bmpFilename,
} }
Context::Context(Window&& window, Renderer&& renderer, Map&& map, int mineCount) Context::Context(Window&& window, Renderer&& renderer, Map&& map, int mineCount)
: numberImage(loadTexture(renderer.renderer_, "resources/font.bmp", KeyColor), TextureDestroy), : numberImage(loadTexture(renderer.renderer_.get(), "resources/font.bmp", KeyColor), TextureDestroy),
mineImage(loadTexture(renderer.renderer_,"resources/mine.bmp", KeyColor), TextureDestroy), mineImage(loadTexture(renderer.renderer_.get(),"resources/mine.bmp", KeyColor), TextureDestroy),
flagImage(loadTexture(renderer.renderer_,"resources/flag.bmp", KeyColor), TextureDestroy), flagImage(loadTexture(renderer.renderer_.get(),"resources/flag.bmp", KeyColor), TextureDestroy),
gameoverImage(loadTexture(renderer.renderer_,"resources/gameover.bmp", KeyColor), TextureDestroy), gameoverImage(loadTexture(renderer.renderer_.get(),"resources/gameover.bmp", KeyColor), TextureDestroy),
winImage(loadTexture(renderer.renderer_,"resources/win.bmp", KeyColor), TextureDestroy), winImage(loadTexture(renderer.renderer_.get(),"resources/win.bmp", KeyColor), TextureDestroy),
window(std::move(window)), window(std::move(window)),
renderer(std::move(renderer)), renderer(std::move(renderer)),
map(std::move(map)), map(std::move(map)),

View File

@ -1,43 +1,33 @@
#include "renderer.hpp" #include "renderer.hpp"
Renderer::Renderer(const Window& window) { Renderer::Renderer(const Window& window): renderer_(SDL_CreateRenderer(window.window_.get(), -1, 0), RendererDestroy) {
renderer_ = SDL_CreateRenderer(window.window_, -1, 0);
} }
void Renderer::SetColor(const SDL_Color& c) { void Renderer::SetColor(const SDL_Color& c) {
SDL_SetRenderDrawColor(renderer_, c.r, c.g, c.b, c.a); SDL_SetRenderDrawColor(renderer_.get(), c.r, c.g, c.b, c.a);
}
Renderer::Renderer(Renderer&& oth) {
renderer_ = oth.renderer_;
oth.renderer_ = nullptr;
} }
void Renderer::Clear() { void Renderer::Clear() {
SDL_RenderClear(renderer_); SDL_RenderClear(renderer_.get());
} }
void Renderer::Present() { void Renderer::Present() {
SDL_RenderPresent(renderer_); SDL_RenderPresent(renderer_.get());
}
Renderer::~Renderer() {
SDL_DestroyRenderer(renderer_);
} }
void Renderer::DrawRect(const SDL_Rect& rect) { void Renderer::DrawRect(const SDL_Rect& rect) {
SDL_RenderDrawRect(renderer_, &rect); SDL_RenderDrawRect(renderer_.get(), &rect);
} }
void Renderer::FillRect(const SDL_Rect& rect) { void Renderer::FillRect(const SDL_Rect& rect) {
SDL_RenderFillRect(renderer_, &rect); SDL_RenderFillRect(renderer_.get(), &rect);
} }
void Renderer::DrawLine(const SDL_Point& p1, const SDL_Point& p2) { void Renderer::DrawLine(const SDL_Point& p1, const SDL_Point& p2) {
SDL_RenderDrawLine(renderer_, p1.x, p1.y, p2.x, p2.y); SDL_RenderDrawLine(renderer_.get(), p1.x, p1.y, p2.x, p2.y);
} }
void Renderer::DrawTexture(SDL_Texture* texture, const SDL_Rect& rect, int x, int y) { void Renderer::DrawTexture(SDL_Texture* texture, const SDL_Rect& rect, int x, int y) {
SDL_Rect dst = {x, y, rect.w, rect.h}; SDL_Rect dst = {x, y, rect.w, rect.h};
SDL_RenderCopy(renderer_, texture, &rect, &dst); SDL_RenderCopy(renderer_.get(), texture, &rect, &dst);
} }

View File

@ -1,15 +1,6 @@
#include "window.hpp" #include "window.hpp"
Window::Window(const std::string& title, int w, int h) { Window::Window(const std::string &title, int w, int h) : window_(SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED,
window_ = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_SHOWN), WindowDestroy)
SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_SHOWN); {
}
Window::Window(Window&& oth) {
window_ = oth.window_;
oth.window_ = nullptr;
}
Window::~Window() {
SDL_DestroyWindow(window_);
} }