diff options
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | Museum.cpp | 5 | ||||
-rw-r--r-- | Museum.h | 3 | ||||
-rw-r--r-- | Rectangle.h | 9 | ||||
-rw-r--r-- | View.cpp | 67 | ||||
-rw-r--r-- | View.h | 25 | ||||
-rw-r--r-- | ViewController.cpp | 12 | ||||
-rw-r--r-- | ViewController.h | 15 | ||||
-rw-r--r-- | docs/class-diag.puml | 10 | ||||
-rw-r--r-- | main.cpp | 10 |
10 files changed, 155 insertions, 4 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index aaf2d07..e24e73f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,9 @@ add_executable(main BlueTileBehavior.cpp RedTileBehavior.cpp GrayTileBehavior.cpp + ViewController.cpp + View.cpp + Museum.cpp ) target_link_libraries(main diff --git a/Museum.cpp b/Museum.cpp new file mode 100644 index 0000000..7c2a722 --- /dev/null +++ b/Museum.cpp @@ -0,0 +1,5 @@ +#include "Museum.h" + +void Museum::update() { +} + @@ -7,5 +7,8 @@ class Museum { public: People people; Canvas canvas; + +public: + void update(); }; diff --git a/Rectangle.h b/Rectangle.h new file mode 100644 index 0000000..a3d0a0d --- /dev/null +++ b/Rectangle.h @@ -0,0 +1,9 @@ +#pragma once + +typedef struct { + unsigned int x; + unsigned int y; + unsigned int width; + unsigned int height; +} Rectangle; + diff --git a/View.cpp b/View.cpp new file mode 100644 index 0000000..5671c1f --- /dev/null +++ b/View.cpp @@ -0,0 +1,67 @@ +#include <SDL2/SDL.h> +#include <thread> + +#include "View.h" + +View::View() { + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); + this->window = SDL_CreateWindow( + "dpa", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + this->width, + this->height, + SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL + ); + this->renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED); + this->worker = new std::thread(&View::work, this); +} + +View::~View() { + if (this->renderer != nullptr) + SDL_DestroyRenderer(this->renderer); + if (this->window != nullptr) + SDL_DestroyWindow(this->window); + if (this->worker != nullptr) { + this->open = false; + this->worker->join(); + } + SDL_Quit(); +} + +void View::set_size(unsigned int width, unsigned int height) { + if (this->width == width && this->height == height) return; + + SDL_SetWindowSize(this->window, width, height); + this->width = width; + this->height = height; +} + +void View::work() { + while (this->open) { + SDL_Event e; + while (SDL_PollEvent(&e)) + if (e.type == SDL_QUIT) this->open = false; + } +} + +void View::draw_begin() { + SDL_RenderClear(this->renderer); +} + +void View::draw_end() { + SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, 255); + SDL_RenderPresent(this->renderer); +} + +void View::draw_rect(Rectangle r, Color c) { + SDL_SetRenderDrawColor(this->renderer, c.red, c.green, c.blue, 255); + SDL_Rect rect = { + .x = static_cast<int>(r.x), + .y = static_cast<int>(r.y), + .w = static_cast<int>(r.width), + .h = static_cast<int>(r.height), + }; + SDL_RenderFillRect(this->renderer, &rect); +} + @@ -1,6 +1,31 @@ #pragma once +#include "Color.h" +#include "Rectangle.h" +#include <SDL2/SDL.h> +#include <thread> + class View { +public: + View(); + virtual ~View(); + +public: + virtual void set_size(unsigned int width, unsigned int height); + virtual void draw_begin(); + virtual void draw_rect(Rectangle r, Color c); + virtual void draw_end(); + +public: + bool open = true; + +private: + unsigned int width = 0, height = 0; + SDL_Window * window = nullptr; + SDL_Renderer * renderer = nullptr; +private: + std::thread * worker = nullptr; + void work(); }; diff --git a/ViewController.cpp b/ViewController.cpp new file mode 100644 index 0000000..256be90 --- /dev/null +++ b/ViewController.cpp @@ -0,0 +1,12 @@ +#include "ViewController.h" +#include "View.h" + +ViewController::ViewController(View & v, Museum & m) : view(v), museum(m) {}; + +void ViewController::update() { + this->view.set_size(100, 100); + this->view.draw_begin(); + this->view.draw_rect({ 10, 10, 50, 50 }, { 255, 0, 255 }); + this->view.draw_end(); +} + diff --git a/ViewController.h b/ViewController.h index 3f59c93..418082f 100644 --- a/ViewController.h +++ b/ViewController.h @@ -1,2 +1,17 @@ #pragma once +class View; +class Museum; + +class ViewController { +public: + ViewController(View & v, Museum & m); + +public: + void update(); + +private: + View & view; + Museum & museum; +}; + diff --git a/docs/class-diag.puml b/docs/class-diag.puml index a59f531..0da5b0f 100644 --- a/docs/class-diag.puml +++ b/docs/class-diag.puml @@ -191,12 +191,18 @@ rectangle Group_Model as "Model" <<group>> { } rectangle "Visualization" <<group>> { + struct Rectangle { + x : unsigned int + y : unsigned int + width : unsigned int + height : unsigned int + } + package SDL2 { } class View { - } class ViewController { - + + update() } ViewController --> View @@ -6,6 +6,7 @@ #include "Deserializer.h" #include "Parser.h" #include "View.h" +#include "ViewController.h" using namespace std; @@ -46,10 +47,15 @@ int main(int argc, char** argv) { Museum m {}; load_museum(m, argc, argv); + printf("%s", m.canvas.to_string(true).c_str()); + View v {}; - load_view(v, m); + ViewController vc {v, m}; - printf("%s", m.canvas.to_string(true).c_str()); + while (v.open) { + m.update(); + vc.update(); + } return EXIT_SUCCESS; } |