From 21564bd54a73fe5bc7752bf3b07f8d63b0d554f3 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Mon, 30 Dec 2019 15:13:06 +0100 Subject: [PATCH] ~ refactored everything --- main/main.cpp | 188 +-------------------------- {src => res}/P10.bmp | Bin {src => res}/P6.bmp | Bin {src => res}/P8.bmp | Bin {src => res}/P9.bmp | Bin {src => res}/font1.bmp | Bin {src => res}/test.bmp | Bin {src => res}/test1.bmp | Bin {src => res}/test2.bmp | Bin src/{Imagedisplay.cpp => Bitmap.cpp} | 38 +++--- src/{Imagedisplay.hpp => Bitmap.hpp} | 18 +-- src/demo.cpp | 6 - src/demo.h | 16 --- test/test_demo.cpp | 1 - 14 files changed, 25 insertions(+), 242 deletions(-) rename {src => res}/P10.bmp (100%) rename {src => res}/P6.bmp (100%) rename {src => res}/P8.bmp (100%) rename {src => res}/P9.bmp (100%) rename {src => res}/font1.bmp (100%) rename {src => res}/test.bmp (100%) rename {src => res}/test1.bmp (100%) rename {src => res}/test2.bmp (100%) rename src/{Imagedisplay.cpp => Bitmap.cpp} (51%) rename src/{Imagedisplay.hpp => Bitmap.hpp} (56%) delete mode 100644 src/demo.cpp delete mode 100644 src/demo.h diff --git a/main/main.cpp b/main/main.cpp index 1f17a93..fe93dbf 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,187 +1 @@ -//#include -//#include "internal.hpp" -//#include "vkvm.hpp" -// -//int main() { -// vkvm::initialize(0); -// -// vkvm::registerEvent(vkvm::EventType::Redraw, [](){ -// vkvm::log(vkvm::INFO, "redraw event callback"); -// }); -// vkvm::registerEvent(vkvm::EventType::Timer, [](){ -// vkvm::log(vkvm::INFO, "timer event callback"); -// }); -// -// vkvm::buttonPressed(vkvm::KeyCode::A); -// vkvm::buttonPressed(vkvm::KeyCode::D); -// vkvm::buttonPressed(vkvm::KeyCode::Z); -// vkvm::buttonPressed(vkvm::KeyCode::Backspcce); -// -// vkvm::KeyCode code = vkvm::getLastPressedKey(); -// while(code != 0){ -// vkvm::log(vkvm::INFO, "keyCode: ", code); -// code = vkvm::getLastPressedKey(); -// } -// -// vkvm::setMode(vkvm::GraphicMode::RGB); -// -// vkvm::setPixel(0,0, vkvm::getBackgroundColor()); -// vkvm::setPixel(5,8, vkvm::getForegroundColor()); -// -// vkvm::Color color = vkvm::getPixel(0,0); -// vkvm::log(vkvm::INFO,"color = (", (int)color.getRed(), ", ", (int)color.getGreen(), ", ", (int)color.getBlue(), ")"); -// color = vkvm::getPixel(5,8); -// vkvm::log(vkvm::INFO,"color = (", (int)color.getRed(), ", ", (int)color.getGreen(), ", ", (int)color.getBlue(), ")"); -// -// vkvm::setMode(vkvm::GraphicMode::TwoColors); -// -// color = vkvm::getPixel(0,0); -// vkvm::log(vkvm::INFO,"color = (", (int)color.getRed(), ", ", (int)color.getGreen(), ", ", (int)color.getBlue(), ")"); -// color = vkvm::getPixel(5,8); -// vkvm::log(vkvm::INFO,"color = (", (int)color.getRed(), ", ", (int)color.getGreen(), ", ", (int)color.getBlue(), ")"); -// -// -// std::string input; -// while(input != "exit"){ -// std::cout << "type r for redraw and t for timer (exit to exit)" << std::endl; -// std::cout << "> "; -// std::getline(std::cin, input); -// -// if(input == "r"){ -// vkvm::callEvent(vkvm::Redraw); -// } -// if(input == "t"){ -// vkvm::callEvent(vkvm::Timer); -// } -// } -// return 0; -//} - -#include -#include "../src/demo.h" -#include "internal.hpp" -#include "vkvm.hpp" -#include "../src/Imagedisplay.hpp" -#include - -void outPutPixel(int windowHeight, int windowWidth); - -int main(){ - - vkvm::initialize(0); - - vkvm::setDefaultValues(); - - vkvm::registerEvent(vkvm::Timer, [](){ - std::cout <<"picture" << std::endl; - vkvm::Color backgroundColor = vkvm::getBackgroundColor(); - - for(int i = 0; i<1000; i++) { - Imagedisplay imagedisplay("../src/P6.bmp"); - for (int w = 0; w < vkvm::getWidth(); w++) { - for (int h = 0; h < vkvm::getHeight(); h++) { - vkvm::setMode(vkvm::GraphicMode(2)); - unsigned int a ; - a = imagedisplay.getPixel(w, h); - vkvm::setPixel(w,h,vkvm::Color(a)); - } - } - sleep(3); - vkvm::setDefaultValues(); - for (int w = 0; w < vkvm::getWidth(); w++) { - for (int h = 0; h < vkvm::getHeight(); h++) { - vkvm::setMode(vkvm::GraphicMode(4)); - unsigned int a ; - a = imagedisplay.getPixel(w, h); - vkvm::setPixel(w,h,vkvm::Color(a)); - } - } - sleep(3); - vkvm::setDefaultValues(); - - Imagedisplay imagedisplay2("../src/P8.bmp"); - - for (int w = 0; w < vkvm::getWidth(); w++) { - for (int h = 0; h < vkvm::getHeight(); h++) { - vkvm::setMode(vkvm::GraphicMode(3)); - unsigned int a ; - a = imagedisplay2.getPixel(w, h); - vkvm::setPixel(w,h,vkvm::Color(a)); - } - } - sleep(3); - vkvm::setDefaultValues(); - - for (int w = 0; w < vkvm::getWidth(); w++) { - for (int h = 0; h < vkvm::getHeight(); h++) { - vkvm::setMode(vkvm::GraphicMode(4)); - unsigned int a ; - a = imagedisplay2.getPixel(w, h); - vkvm::setPixel(w,h,vkvm::Color(a)); - } - } - sleep(3); - vkvm::setDefaultValues(); - - Imagedisplay imagedisplay3("../src/P9.bmp"); - for (int w = 0; w < vkvm::getWidth(); w++) { - for (int h = 0; h < vkvm::getHeight(); h++){ - vkvm::setMode(vkvm::GraphicMode(3)); - unsigned int a ; - a = imagedisplay3.getPixel(w, h); - vkvm::setPixel(w,h,vkvm::Color(a)); - } - } - sleep(3); - vkvm::setDefaultValues(); - for (int w = 0; w < vkvm::getWidth(); w++) { - for (int h = 0; h < vkvm::getHeight(); h++) { - vkvm::setMode(vkvm::GraphicMode(4)); - unsigned int a ; - a = imagedisplay3.getPixel(w, h); - vkvm::setPixel(w,h,vkvm::Color(a)); - } - } - sleep(3); - vkvm::setDefaultValues(); - Imagedisplay imagedisplay4("../src/P10.bmp"); - for (int w = 0; w < vkvm::getWidth(); w++) { - for (int h = 0; h < vkvm::getHeight(); h++){ - vkvm::setMode(vkvm::GraphicMode(3)); - unsigned int a ; - a = imagedisplay4.getPixel(w, h); - vkvm::setPixel(w,h,vkvm::Color(a)); - } - } - sleep(3); - vkvm::setDefaultValues(); - for (int w = 0; w < vkvm::getWidth(); w++) { - for (int h = 0; h < vkvm::getHeight(); h++){ - vkvm::setMode(vkvm::GraphicMode(4)); - unsigned int a ; - a = imagedisplay4.getPixel(w, h); - vkvm::setPixel(w,h,vkvm::Color(a)); - } - } - sleep(3); - outPutPixel(vkvm::getHeight() ,vkvm::getWidth()); - } - - }); - vkvm::callEvent(vkvm::Timer); - -} - -void outPutPixel(int windowHeight, int windowWidth) { - for(int y = 0; y < windowHeight; y++) { - for(int x = 0; x < windowWidth; x++) { - if(vkvm::getPixel(x, y).getRed() !=0) { - std::cout << "*" ; - - } else { - std::cout << " "; - } - } - std::cout << "\n"; - } -} \ No newline at end of file +#include "internal.hpp" #include "vkvm.hpp" #include "../src/Bitmap.hpp" #include //time to sleep for in seconds constexpr int sleepTime = 10; void displayImage(const std::string &file, vkvm::GraphicMode graphicMode) { vkvm::setMode(graphicMode); Bitmap image(file); vkvm::setWidth(image.getWidth()); vkvm::setHeight(image.getHeight()); for (int x = 0; x < vkvm::getWidth(); x++) { for (int y = 0; y < vkvm::getHeight(); y++) { unsigned int hex = image.getPixel(x, y); vkvm::Color color = vkvm::Color(hex); vkvm::setPixel(x, y, color); } } vkvm::callEvent(vkvm::EventType::Redraw); } void displayImage(const std::string &file) { displayImage(file, vkvm::GraphicMode::RGB); sleep(sleepTime); vkvm::setMode(vkvm::GraphicMode::Gray_256); vkvm::callEvent(vkvm::EventType::Redraw); sleep(sleepTime); vkvm::setMode(vkvm::GraphicMode::TwoColors); vkvm::callEvent(vkvm::EventType::Redraw); sleep(sleepTime); } int main() { bool running = true; vkvm::initialize(0); //vkvm::setLogLevel(vkvm::LogLevel::DEBUG); while(running) { displayImage("../res/P6.bmp"); sleep(sleepTime); vkvm::setBackgroundColor(vkvm::red); vkvm::setForegroundColor(vkvm::blue); vkvm::callEvent(vkvm::EventType::Redraw); vkvm::setBackgroundColor(vkvm::black); vkvm::setForegroundColor(vkvm::white); sleep(sleepTime); displayImage("../res/P8.bmp"); displayImage("../res/P9.bmp"); displayImage("../res/P10.bmp"); vkvm::setText("Hello World"); sleep(sleepTime); vkvm::setFont(vkvm::FontType::FuturisticBlack); vkvm::callEvent(vkvm::EventType::RenderText); sleep(sleepTime); vkvm::setText("Hello World, lorem ipsum dolor sit amet"); sleep(sleepTime); vkvm::setFont(vkvm::FontType::ProFontIIX); vkvm::callEvent(vkvm::EventType::RenderText); sleep(sleepTime); } } \ No newline at end of file diff --git a/src/P10.bmp b/res/P10.bmp similarity index 100% rename from src/P10.bmp rename to res/P10.bmp diff --git a/src/P6.bmp b/res/P6.bmp similarity index 100% rename from src/P6.bmp rename to res/P6.bmp diff --git a/src/P8.bmp b/res/P8.bmp similarity index 100% rename from src/P8.bmp rename to res/P8.bmp diff --git a/src/P9.bmp b/res/P9.bmp similarity index 100% rename from src/P9.bmp rename to res/P9.bmp diff --git a/src/font1.bmp b/res/font1.bmp similarity index 100% rename from src/font1.bmp rename to res/font1.bmp diff --git a/src/test.bmp b/res/test.bmp similarity index 100% rename from src/test.bmp rename to res/test.bmp diff --git a/src/test1.bmp b/res/test1.bmp similarity index 100% rename from src/test1.bmp rename to res/test1.bmp diff --git a/src/test2.bmp b/res/test2.bmp similarity index 100% rename from src/test2.bmp rename to res/test2.bmp diff --git a/src/Imagedisplay.cpp b/src/Bitmap.cpp similarity index 51% rename from src/Imagedisplay.cpp rename to src/Bitmap.cpp index dd3c4e4..ca78fa0 100644 --- a/src/Imagedisplay.cpp +++ b/src/Bitmap.cpp @@ -1,26 +1,23 @@ -// -// Created by yukun on 27.11.19. -// -#include "Imagedisplay.hpp" +#include "Bitmap.hpp" #include #include -Imagedisplay::Imagedisplay() { +Bitmap::Bitmap() { offset = 0; width = 0; height = 0; - bpp = 0; + bitsPerPixel = 0; } -Imagedisplay::Imagedisplay(const std::string &file) { +Bitmap::Bitmap(const std::string &file) { offset = 0; width = 0; height = 0; - bpp = 0; + bitsPerPixel = 0; load(file); } -void Imagedisplay::load(const std::string &file) { +void Bitmap::load(const std::string &file) { std::ifstream stream; stream.open(file); if(stream.is_open()){ @@ -29,35 +26,34 @@ void Imagedisplay::load(const std::string &file) { for(int i = 0; i < str.size();i++){ data[i] = str[i]; } - offset = (int)*(unsigned int*)(&data[10]); - width = (int)*(unsigned int*)(&data[18]); - height = (int)*(unsigned int*)(&data[22]); - bpp = (int)*(unsigned short*)(&data[28]); + offset = static_cast(*reinterpret_cast(&data[10])); + width = static_cast(*reinterpret_cast(&data[18])); + height = static_cast(*reinterpret_cast(&data[22])); + bitsPerPixel = static_cast(*reinterpret_cast(&data[28])); } } -int Imagedisplay::getWidth() { +int Bitmap::getWidth() { return width; } -int Imagedisplay::getHeight() { +int Bitmap::getHeight() { return height; } -char *Imagedisplay::getData() { +char *Bitmap::getData() { return &data[offset]; } -int Imagedisplay::getBitsPerPixel() { - return bpp; +int Bitmap::getBitsPerPixel() { + return bitsPerPixel; } -unsigned int Imagedisplay::getPixel(int x, int y) { +unsigned int Bitmap::getPixel(int x, int y) { unsigned int pixel = 0; char *ptr = getData() + ((getHeight() - 1 - y) * getWidth() + x) * (getBitsPerPixel() / 8); for(int i = 0; i < getBitsPerPixel() / 8;i++){ - *((char*)&pixel+i) = ptr[i]; - // std::cout<< "hi: im" << (unsigned int) ptr[i]<< std::endl; + *((char*)&pixel+i) = ptr[i];//NOLINT } if(pixel != 0){ return pixel; diff --git a/src/Imagedisplay.hpp b/src/Bitmap.hpp similarity index 56% rename from src/Imagedisplay.hpp rename to src/Bitmap.hpp index 8a7664e..83a22ca 100644 --- a/src/Imagedisplay.hpp +++ b/src/Bitmap.hpp @@ -1,17 +1,13 @@ -// -// Created by yukun on 27.11.19. -// - -#ifndef DEMO_IMAGEDISPLAY_HPP -#define DEMO_IMAGEDISPLAY_HPP +#ifndef DEMO_BITMAP_HPP +#define DEMO_BITMAP_HPP #include #include -class Imagedisplay { +class Bitmap { public: - Imagedisplay(); - explicit Imagedisplay(const std::string &file); + Bitmap(); + explicit Bitmap(const std::string &file); void load(const std::string &file); int getWidth(); int getHeight(); @@ -22,7 +18,7 @@ private: int width; int height; int offset; - int bpp; + int bitsPerPixel; std::vector data; }; -#endif //DEMO_IMAGEDISPLAY_HPP +#endif //DEMO_BITMAP_HPP diff --git a/src/demo.cpp b/src/demo.cpp deleted file mode 100644 index 8773d58..0000000 --- a/src/demo.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "demo.h" - -int test() { - const int returnValue = 42; - return returnValue; -} diff --git a/src/demo.h b/src/demo.h deleted file mode 100644 index 9e417e5..0000000 --- a/src/demo.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef DEMO_H -#define DEMO_H - -#include -#include -#include -#include -#include "vector" - -class demo { -public: - -}; - - -#endif //SHARED_MEMORY_DEMO_H diff --git a/test/test_demo.cpp b/test/test_demo.cpp index 729de9c..5db7f1d 100644 --- a/test/test_demo.cpp +++ b/test/test_demo.cpp @@ -1,4 +1,3 @@ -#include "../src/demo.h" #include TEST_CASE("Demo test") {