(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 "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<SDL_Renderer, decltype(RendererDestroy)> renderer_;
};

View File

@ -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<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)
: 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)),

View File

@ -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);
}

View File

@ -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)
{
}