使用只能指针代替裸指针

This commit is contained in:
Jie
2024-09-30 13:34:05 +08:00
parent 7c049db4ac
commit 1237b164bf
12 changed files with 246 additions and 25 deletions

View File

@@ -1,18 +1,19 @@
#include <SFML/Graphics.hpp>
#include <algorithm>
#include <memory>
class ImageService {
private:
sf::Texture texture;
std::shared_ptr<sf::Texture> texture = std::make_shared<sf::Texture>();
public:
ImageService(const std::string& name){
texture.loadFromFile(name);
texture->loadFromFile(name);
}
sf::Texture& GetTexture(){
std::shared_ptr<sf::Texture> GetTexture(){
return texture;
}
float GetScale(int width, int height){
auto imgSize = texture.getSize();
auto imgSize = texture->getSize();
return std::min(static_cast<float>(width) / imgSize.x, static_cast<float>(height) / imgSize.y);
}
};

View File

@@ -2,24 +2,26 @@
#include <UtilTool.h>
#include <string>
#include <SFML/Graphics.hpp>
#include <memory>
class MediaService
{
private:
ImageService *imageService = nullptr;
MediaType type;
sf::Texture *texture = nullptr;
sf::Sprite *sprite = nullptr;
std::shared_ptr<sf::Texture> texture;
std::shared_ptr<sf::Sprite> sprite;
std::shared_ptr<sf::RenderWindow> window;
int client_width = 0;
int client_height = 0;
public:
MediaService(const std::string &filename, int width, int height);
~MediaService(){
delete texture;
delete sprite;
}
sf::Sprite GetSprite()
~MediaService() = default;
std::shared_ptr<sf::Sprite> GetSprite()
{
return *sprite;
return sprite;
}
};
void SetWindow(std::shared_ptr<sf::RenderWindow> window);
void Play();
};

38
include/thread_queue.h Normal file
View File

@@ -0,0 +1,38 @@
#include <queue>
#include <mutex>
#include <condition_variable>
template <typename T>
class ThreadQueue {
public:
ThreadQueue() = default;
~ThreadQueue() = default;
void push(const T& value) {
std::lock_guard<std::mutex> lock(mutex_);
queue_.push(value);
condition_.notify_one();
}
T pop() {
std::unique_lock<std::mutex> lock(mutex_);
condition_.wait(lock, [this] { return !queue_.empty(); });
T value = queue_.front();
queue_.pop();
return value;
}
bool empty() const {
std::lock_guard<std::mutex> lock(mutex_);
return queue_.empty();
}
size_t size() const {
std::lock_guard<std::mutex> lock(mutex_);
return queue_.size();
}
private:
std::queue<T> queue_;
mutable std::mutex mutex_;
std::condition_variable condition_;
};

11
include/videoService.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef VIDEOSERVICE_H
#define VIDEOSERVICE_H
#include <SFML/Graphics.hpp>
#include <memory>
class VideoService {
private:
std::shared_ptr<sf::Texture> texture;
};
#endif