diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..9060489 --- /dev/null +++ b/.vscode/launch.json @@ -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 + } + ] + } + + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0abcd66 --- /dev/null +++ b/.vscode/settings.json @@ -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" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..05054c5 --- /dev/null +++ b/.vscode/tasks.json @@ -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" +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f2ec2a..efcd09d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ set(PROJECT_N_T "mp_test") project(${PROJECT_N} VERSION 1.0) -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED True) include_directories(${PROJECT_SOURCE_DIR}/include) diff --git a/img/ocean.jpg b/img/ocean.jpg new file mode 100755 index 0000000..703d774 Binary files /dev/null and b/img/ocean.jpg differ diff --git a/include/imageService.h b/include/imageService.h index b82de82..23d5d2c 100644 --- a/include/imageService.h +++ b/include/imageService.h @@ -1,18 +1,19 @@ #include #include +#include class ImageService { private: - sf::Texture texture; + std::shared_ptr texture = std::make_shared(); public: ImageService(const std::string& name){ - texture.loadFromFile(name); + texture->loadFromFile(name); } - sf::Texture& GetTexture(){ + std::shared_ptr GetTexture(){ return texture; } float GetScale(int width, int height){ - auto imgSize = texture.getSize(); + auto imgSize = texture->getSize(); return std::min(static_cast(width) / imgSize.x, static_cast(height) / imgSize.y); } }; diff --git a/include/mediaService.h b/include/mediaService.h index 04bb367..4a4b3fa 100644 --- a/include/mediaService.h +++ b/include/mediaService.h @@ -2,24 +2,26 @@ #include #include #include +#include class MediaService { private: ImageService *imageService = nullptr; MediaType type; - sf::Texture *texture = nullptr; - sf::Sprite *sprite = nullptr; + std::shared_ptr texture; + std::shared_ptr sprite; + std::shared_ptr 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 GetSprite() { - return *sprite; + return sprite; } -}; \ No newline at end of file + void SetWindow(std::shared_ptr window); + void Play(); + +}; diff --git a/include/thread_queue.h b/include/thread_queue.h new file mode 100644 index 0000000..205b7a1 --- /dev/null +++ b/include/thread_queue.h @@ -0,0 +1,38 @@ +#include +#include +#include + +template +class ThreadQueue { +public: + ThreadQueue() = default; + ~ThreadQueue() = default; + + void push(const T& value) { + std::lock_guard lock(mutex_); + queue_.push(value); + condition_.notify_one(); + } + + T pop() { + std::unique_lock lock(mutex_); + condition_.wait(lock, [this] { return !queue_.empty(); }); + T value = queue_.front(); + queue_.pop(); + return value; + } + + bool empty() const { + std::lock_guard lock(mutex_); + return queue_.empty(); + } + + size_t size() const { + std::lock_guard lock(mutex_); + return queue_.size(); + } +private: + std::queue queue_; + mutable std::mutex mutex_; + std::condition_variable condition_; +}; diff --git a/include/videoService.h b/include/videoService.h new file mode 100644 index 0000000..35d56f1 --- /dev/null +++ b/include/videoService.h @@ -0,0 +1,11 @@ +#ifndef VIDEOSERVICE_H +#define VIDEOSERVICE_H +#include +#include + +class VideoService { +private: + std::shared_ptr texture; +}; + +#endif diff --git a/main.cc b/main.cc index ffc80a9..740a3e7 100644 --- a/main.cc +++ b/main.cc @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,23 +7,31 @@ #include "UtilTool.h" #include "mediaService.h" +#define DEBUG + constexpr int CLIENT_WIDTH = 800; constexpr int CLIENT_HEIGHT = 600; 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){ spdlog::error("Usage: mp filename "); return 0; } +#endif + spdlog::info("filename: {}", argv[1]); MediaService mediaService(argv[1], CLIENT_WIDTH, CLIENT_HEIGHT); - sf::RenderWindow window(sf::VideoMode(CLIENT_WIDTH, CLIENT_HEIGHT), "mp"); + std::shared_ptr window = std::make_shared(sf::VideoMode(CLIENT_WIDTH, CLIENT_HEIGHT), "mp"); + mediaService.SetWindow(window); bool running = true; while(running){ sf::Event event; - while(window.pollEvent(event)){ + while(window->pollEvent(event)){ if(event.type == sf::Event::Closed){ running = false; } @@ -31,11 +40,8 @@ int main(int argc, char** argv){ running = false; } } - } - window.clear(); - window.draw(mediaService.GetSprite()); - window.display(); + mediaService.Play(); } return 0; } diff --git a/src/mediaService.cc b/src/mediaService.cc index aed5af7..753851f 100644 --- a/src/mediaService.cc +++ b/src/mediaService.cc @@ -1,7 +1,9 @@ #include +#include +#include MediaService::MediaService(const std::string& filename, int width, int height){ - auto type = UtilTool::CheckFileType(filename); + type = UtilTool::CheckFileType(filename); client_width = width; client_height = height; float scale = .0f; @@ -9,14 +11,34 @@ MediaService::MediaService(const std::string& filename, int width, int height){ { case MediaType::IMAGE: imageService = new ImageService(filename); - texture = &imageService->GetTexture(); - sprite = new sf::Sprite(); + texture = imageService->GetTexture(); + sprite = std::make_shared(); sprite->setTexture(*texture); scale = imageService->GetScale(client_width, client_height); sprite->setScale(scale, scale); break; + case MediaType::VIDEO: + break; + + default: + break; + } +} + +void MediaService::SetWindow(std::shared_ptr window){ + this->window = window; +} + +void MediaService::Play(){ + switch (type) + { + case MediaType::IMAGE: + window->clear(); + window->draw(*sprite); + window->display(); + break; default: break; } -} \ No newline at end of file +} diff --git a/src/videoService.cc b/src/videoService.cc new file mode 100644 index 0000000..648ebae --- /dev/null +++ b/src/videoService.cc @@ -0,0 +1 @@ +#include "videoService.h"