使用只能指针代替裸指针
This commit is contained in:
parent
7c049db4ac
commit
1237b164bf
33
.vscode/launch.json
vendored
Normal file
33
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
{
|
||||||
|
// 使用 IntelliSense 了解相关属性。
|
||||||
|
// 悬停以查看现有属性的描述。
|
||||||
|
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "(gdb) Launch",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/build/mp",
|
||||||
|
"args": ["${workspaceFolder}/img/ocean.jpg"],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"cwd": "${fileDirname}",
|
||||||
|
"environment": [],
|
||||||
|
"externalConsole": false,
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"setupCommands": [
|
||||||
|
{
|
||||||
|
"description": "Enable pretty-printing for gdb",
|
||||||
|
"text": "-enable-pretty-printing",
|
||||||
|
"ignoreFailures": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "Set Disassembly Flavor to Intel",
|
||||||
|
"text": "-gdb-set disassembly-flavor intel",
|
||||||
|
"ignoreFailures": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
79
.vscode/settings.json
vendored
Normal file
79
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"*.cpp": "cpp",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"csignal": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cstring": "cpp",
|
||||||
|
"ctime": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"any": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"bit": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"bitset": "cpp",
|
||||||
|
"charconv": "cpp",
|
||||||
|
"chrono": "cpp",
|
||||||
|
"codecvt": "cpp",
|
||||||
|
"compare": "cpp",
|
||||||
|
"concepts": "cpp",
|
||||||
|
"condition_variable": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"forward_list": "cpp",
|
||||||
|
"list": "cpp",
|
||||||
|
"map": "cpp",
|
||||||
|
"set": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"unordered_set": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"ratio": "cpp",
|
||||||
|
"regex": "cpp",
|
||||||
|
"source_location": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"fstream": "cpp",
|
||||||
|
"future": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iomanip": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"mutex": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"numbers": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"semaphore": "cpp",
|
||||||
|
"span": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"stop_token": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"thread": "cpp",
|
||||||
|
"cinttypes": "cpp",
|
||||||
|
"typeinfo": "cpp",
|
||||||
|
"variant": "cpp",
|
||||||
|
"format": "cpp"
|
||||||
|
}
|
||||||
|
}
|
28
.vscode/tasks.json
vendored
Normal file
28
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"type": "cppbuild",
|
||||||
|
"label": "C/C++: g++ build active file",
|
||||||
|
"command": "/usr/bin/g++",
|
||||||
|
"args": [
|
||||||
|
"-fdiagnostics-color=always",
|
||||||
|
"-g",
|
||||||
|
"${file}",
|
||||||
|
"-o",
|
||||||
|
"${fileDirname}/${fileBasenameNoExtension}"
|
||||||
|
],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${fileDirname}"
|
||||||
|
},
|
||||||
|
"problemMatcher": [
|
||||||
|
"$gcc"
|
||||||
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
},
|
||||||
|
"detail": "Task generated by Debugger."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": "2.0.0"
|
||||||
|
}
|
@ -5,7 +5,7 @@ set(PROJECT_N_T "mp_test")
|
|||||||
|
|
||||||
project(${PROJECT_N} VERSION 1.0)
|
project(${PROJECT_N} VERSION 1.0)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||||
|
|
||||||
include_directories(${PROJECT_SOURCE_DIR}/include)
|
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||||
|
BIN
img/ocean.jpg
Executable file
BIN
img/ocean.jpg
Executable file
Binary file not shown.
After Width: | Height: | Size: 2.0 MiB |
@ -1,18 +1,19 @@
|
|||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
class ImageService {
|
class ImageService {
|
||||||
private:
|
private:
|
||||||
sf::Texture texture;
|
std::shared_ptr<sf::Texture> texture = std::make_shared<sf::Texture>();
|
||||||
public:
|
public:
|
||||||
ImageService(const std::string& name){
|
ImageService(const std::string& name){
|
||||||
texture.loadFromFile(name);
|
texture->loadFromFile(name);
|
||||||
}
|
}
|
||||||
sf::Texture& GetTexture(){
|
std::shared_ptr<sf::Texture> GetTexture(){
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
float GetScale(int width, int height){
|
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);
|
return std::min(static_cast<float>(width) / imgSize.x, static_cast<float>(height) / imgSize.y);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -2,24 +2,26 @@
|
|||||||
#include <UtilTool.h>
|
#include <UtilTool.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
class MediaService
|
class MediaService
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
ImageService *imageService = nullptr;
|
ImageService *imageService = nullptr;
|
||||||
MediaType type;
|
MediaType type;
|
||||||
sf::Texture *texture = nullptr;
|
std::shared_ptr<sf::Texture> texture;
|
||||||
sf::Sprite *sprite = nullptr;
|
std::shared_ptr<sf::Sprite> sprite;
|
||||||
|
std::shared_ptr<sf::RenderWindow> window;
|
||||||
int client_width = 0;
|
int client_width = 0;
|
||||||
int client_height = 0;
|
int client_height = 0;
|
||||||
public:
|
public:
|
||||||
MediaService(const std::string &filename, int width, int height);
|
MediaService(const std::string &filename, int width, int height);
|
||||||
~MediaService(){
|
~MediaService() = default;
|
||||||
delete texture;
|
std::shared_ptr<sf::Sprite> GetSprite()
|
||||||
delete sprite;
|
|
||||||
}
|
|
||||||
sf::Sprite GetSprite()
|
|
||||||
{
|
{
|
||||||
return *sprite;
|
return sprite;
|
||||||
}
|
}
|
||||||
|
void SetWindow(std::shared_ptr<sf::RenderWindow> window);
|
||||||
|
void Play();
|
||||||
|
|
||||||
};
|
};
|
38
include/thread_queue.h
Normal file
38
include/thread_queue.h
Normal 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
11
include/videoService.h
Normal 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
|
20
main.cc
20
main.cc
@ -1,3 +1,4 @@
|
|||||||
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <SFML/System.hpp>
|
#include <SFML/System.hpp>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
@ -6,23 +7,31 @@
|
|||||||
#include "UtilTool.h"
|
#include "UtilTool.h"
|
||||||
#include "mediaService.h"
|
#include "mediaService.h"
|
||||||
|
|
||||||
|
#define DEBUG
|
||||||
|
|
||||||
constexpr int CLIENT_WIDTH = 800;
|
constexpr int CLIENT_WIDTH = 800;
|
||||||
constexpr int CLIENT_HEIGHT = 600;
|
constexpr int CLIENT_HEIGHT = 600;
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
// spdlog::info("Current WorkDir Is: {}",argv[0]);
|
spdlog::info("Current WorkDir Is: {}",argv[0]);
|
||||||
|
#ifdef DEBUG
|
||||||
|
argv[1] = R"(../img/ocean.jpg)";
|
||||||
|
#else
|
||||||
if(argc != 2){
|
if(argc != 2){
|
||||||
spdlog::error("Usage: mp filename ");
|
spdlog::error("Usage: mp filename ");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
spdlog::info("filename: {}", argv[1]);
|
||||||
MediaService mediaService(argv[1], CLIENT_WIDTH, CLIENT_HEIGHT);
|
MediaService mediaService(argv[1], CLIENT_WIDTH, CLIENT_HEIGHT);
|
||||||
sf::RenderWindow window(sf::VideoMode(CLIENT_WIDTH, CLIENT_HEIGHT), "mp");
|
std::shared_ptr<sf::RenderWindow> window = std::make_shared<sf::RenderWindow>(sf::VideoMode(CLIENT_WIDTH, CLIENT_HEIGHT), "mp");
|
||||||
|
mediaService.SetWindow(window);
|
||||||
bool running = true;
|
bool running = true;
|
||||||
|
|
||||||
while(running){
|
while(running){
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
while(window.pollEvent(event)){
|
while(window->pollEvent(event)){
|
||||||
if(event.type == sf::Event::Closed){
|
if(event.type == sf::Event::Closed){
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
@ -31,11 +40,8 @@ int main(int argc, char** argv){
|
|||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
window.clear();
|
mediaService.Play();
|
||||||
window.draw(mediaService.GetSprite());
|
|
||||||
window.display();
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
#include <mediaService.h>
|
#include <mediaService.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <videoService.h>
|
||||||
|
|
||||||
MediaService::MediaService(const std::string& filename, int width, int height){
|
MediaService::MediaService(const std::string& filename, int width, int height){
|
||||||
auto type = UtilTool::CheckFileType(filename);
|
type = UtilTool::CheckFileType(filename);
|
||||||
client_width = width;
|
client_width = width;
|
||||||
client_height = height;
|
client_height = height;
|
||||||
float scale = .0f;
|
float scale = .0f;
|
||||||
@ -9,12 +11,32 @@ MediaService::MediaService(const std::string& filename, int width, int height){
|
|||||||
{
|
{
|
||||||
case MediaType::IMAGE:
|
case MediaType::IMAGE:
|
||||||
imageService = new ImageService(filename);
|
imageService = new ImageService(filename);
|
||||||
texture = &imageService->GetTexture();
|
texture = imageService->GetTexture();
|
||||||
sprite = new sf::Sprite();
|
sprite = std::make_shared<sf::Sprite>();
|
||||||
sprite->setTexture(*texture);
|
sprite->setTexture(*texture);
|
||||||
scale = imageService->GetScale(client_width, client_height);
|
scale = imageService->GetScale(client_width, client_height);
|
||||||
sprite->setScale(scale, scale);
|
sprite->setScale(scale, scale);
|
||||||
break;
|
break;
|
||||||
|
case MediaType::VIDEO:
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MediaService::SetWindow(std::shared_ptr<sf::RenderWindow> window){
|
||||||
|
this->window = window;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MediaService::Play(){
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case MediaType::IMAGE:
|
||||||
|
window->clear();
|
||||||
|
window->draw(*sprite);
|
||||||
|
window->display();
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
1
src/videoService.cc
Normal file
1
src/videoService.cc
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "videoService.h"
|
Loading…
Reference in New Issue
Block a user