From c945a0a626c08f685bcf640eb7b4cf4fc596b382 Mon Sep 17 00:00:00 2001 From: VisualGMQ <2142587070@qq.com> Date: Sun, 25 Jun 2023 11:20:06 +0800 Subject: [PATCH] (fix): use std::unique_ptr to manage SDL_Window & SDL_Renderer --- include/renderer.hpp | 10 +++++----- include/window.hpp | 11 +++++------ src/context.cpp | 10 +++++----- src/renderer.cpp | 26 ++++++++------------------ src/window.cpp | 15 +++------------ 5 files changed, 26 insertions(+), 46 deletions(-) diff --git a/include/renderer.hpp b/include/renderer.hpp index 89aa565..3b669ce 100644 --- a/include/renderer.hpp +++ b/include/renderer.hpp @@ -2,15 +2,15 @@ #include "pch.hpp" #include "window.hpp" +inline auto RendererDestroy = [](SDL_Renderer* renderer) { + SDL_DestroyRenderer(renderer); +}; + class Renderer final { public: friend struct Context; Renderer(const Window&); - ~Renderer(); - Renderer(const Renderer&) = delete; - Renderer(Renderer&&); - Renderer& operator=(const Renderer&) = delete; void SetColor(const SDL_Color&); void Clear(); @@ -21,5 +21,5 @@ public: void DrawTexture(SDL_Texture*, const SDL_Rect&, int x, int y); private: - SDL_Renderer* renderer_; + std::unique_ptr renderer_; }; \ No newline at end of file diff --git a/include/window.hpp b/include/window.hpp index 014637e..f998b4b 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -2,17 +2,16 @@ #include "pch.hpp" +inline auto WindowDestroy = [](SDL_Window* window) { + SDL_DestroyWindow(window); +}; + class Window final { public: friend class Renderer; Window(const std::string& title, int w, int h); - Window(const Window&) = delete; - Window(Window&&); - Window& operator=(const Window&) = delete; - - ~Window(); private: - SDL_Window* window_; + std::unique_ptr window_; }; \ No newline at end of file diff --git a/src/context.cpp b/src/context.cpp index 806d774..2d195e7 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -86,11 +86,11 @@ SDL_Texture* loadTexture(SDL_Renderer* renderer, const std::string& bmpFilename, } Context::Context(Window&& window, Renderer&& renderer, Map&& map, int mineCount) - : numberImage(loadTexture(renderer.renderer_, "resources/font.bmp", KeyColor), TextureDestroy), - mineImage(loadTexture(renderer.renderer_,"resources/mine.bmp", KeyColor), TextureDestroy), - flagImage(loadTexture(renderer.renderer_,"resources/flag.bmp", KeyColor), TextureDestroy), - gameoverImage(loadTexture(renderer.renderer_,"resources/gameover.bmp", KeyColor), TextureDestroy), - winImage(loadTexture(renderer.renderer_,"resources/win.bmp", KeyColor), TextureDestroy), + : numberImage(loadTexture(renderer.renderer_.get(), "resources/font.bmp", KeyColor), TextureDestroy), + mineImage(loadTexture(renderer.renderer_.get(),"resources/mine.bmp", KeyColor), TextureDestroy), + flagImage(loadTexture(renderer.renderer_.get(),"resources/flag.bmp", KeyColor), TextureDestroy), + gameoverImage(loadTexture(renderer.renderer_.get(),"resources/gameover.bmp", KeyColor), TextureDestroy), + winImage(loadTexture(renderer.renderer_.get(),"resources/win.bmp", KeyColor), TextureDestroy), window(std::move(window)), renderer(std::move(renderer)), map(std::move(map)), diff --git a/src/renderer.cpp b/src/renderer.cpp index d3462c4..1d13717 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,43 +1,33 @@ #include "renderer.hpp" -Renderer::Renderer(const Window& window) { - renderer_ = SDL_CreateRenderer(window.window_, -1, 0); +Renderer::Renderer(const Window& window): renderer_(SDL_CreateRenderer(window.window_.get(), -1, 0), RendererDestroy) { } void Renderer::SetColor(const SDL_Color& c) { - SDL_SetRenderDrawColor(renderer_, c.r, c.g, c.b, c.a); -} - -Renderer::Renderer(Renderer&& oth) { - renderer_ = oth.renderer_; - oth.renderer_ = nullptr; + SDL_SetRenderDrawColor(renderer_.get(), c.r, c.g, c.b, c.a); } void Renderer::Clear() { - SDL_RenderClear(renderer_); + SDL_RenderClear(renderer_.get()); } void Renderer::Present() { - SDL_RenderPresent(renderer_); -} - -Renderer::~Renderer() { - SDL_DestroyRenderer(renderer_); + SDL_RenderPresent(renderer_.get()); } void Renderer::DrawRect(const SDL_Rect& rect) { - SDL_RenderDrawRect(renderer_, &rect); + SDL_RenderDrawRect(renderer_.get(), &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) { - 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) { SDL_Rect dst = {x, y, rect.w, rect.h}; - SDL_RenderCopy(renderer_, texture, &rect, &dst); + SDL_RenderCopy(renderer_.get(), texture, &rect, &dst); } \ No newline at end of file diff --git a/src/window.cpp b/src/window.cpp index 56866b8..a573771 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,15 +1,6 @@ #include "window.hpp" -Window::Window(const std::string& title, int w, int h) { - window_ = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_SHOWN); -} - -Window::Window(Window&& oth) { - window_ = oth.window_; - oth.window_ = nullptr; -} - -Window::~Window() { - SDL_DestroyWindow(window_); +Window::Window(const std::string &title, int w, int h) : window_(SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_SHOWN), WindowDestroy) +{ } \ No newline at end of file