diff --git a/CMakeLists.txt b/CMakeLists.txt index 9409d74..fb34d89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ target_link_libraries(Demo ${LIB_PATH}/lib/liblibrary.a) enable_testing() find_package(Catch2 REQUIRED) add_executable(UnitTests ${SOURCES} ${HEADERS} ${TESTS}) +target_link_libraries(UnitTests ${LIB_PATH}/lib/liblibrary.a) target_link_libraries(UnitTests Catch2::Catch2) include(CTest) diff --git a/main/main.cpp b/main/main.cpp index 36c19fb..1f17a93 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,58 +1,187 @@ +//#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(){ -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::setDefaultValues(); - vkvm::buttonPressed(vkvm::KeyCode::A); - vkvm::buttonPressed(vkvm::KeyCode::D); - vkvm::buttonPressed(vkvm::KeyCode::Z); - vkvm::buttonPressed(vkvm::KeyCode::Backspcce); + vkvm::registerEvent(vkvm::Timer, [](){ + std::cout <<"picture" << std::endl; + vkvm::Color backgroundColor = vkvm::getBackgroundColor(); - vkvm::KeyCode code = vkvm::getLastPressedKey(); - while(code != 0){ - vkvm::log(vkvm::INFO, "keyCode: ", code); - code = vkvm::getLastPressedKey(); - } + 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(); - vkvm::setMode(vkvm::GraphicMode::RGB); + Imagedisplay imagedisplay2("../src/P8.bmp"); - vkvm::setPixel(0,0, vkvm::getBackgroundColor()); - vkvm::setPixel(5,8, vkvm::getForegroundColor()); + 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(); - 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(), ")"); + 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(); - 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); + 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()); } - if(input == "t"){ - vkvm::callEvent(vkvm::Timer); - } - } - return 0; + + }); + 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 diff --git a/src/Imagedisplay.cpp b/src/Imagedisplay.cpp new file mode 100644 index 0000000..dd3c4e4 --- /dev/null +++ b/src/Imagedisplay.cpp @@ -0,0 +1,67 @@ +// +// Created by yukun on 27.11.19. +// +#include "Imagedisplay.hpp" +#include +#include + +Imagedisplay::Imagedisplay() { + offset = 0; + width = 0; + height = 0; + bpp = 0; +} + +Imagedisplay::Imagedisplay(const std::string &file) { + offset = 0; + width = 0; + height = 0; + bpp = 0; + load(file); +} + +void Imagedisplay::load(const std::string &file) { + std::ifstream stream; + stream.open(file); + if(stream.is_open()){ + std::string str((std::istreambuf_iterator(stream)),std::istreambuf_iterator()); + data.resize(str.size()); + 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]); + } +} + +int Imagedisplay::getWidth() { + return width; +} + +int Imagedisplay::getHeight() { + return height; +} + +char *Imagedisplay::getData() { + return &data[offset]; +} + +int Imagedisplay::getBitsPerPixel() { + return bpp; +} + +unsigned int Imagedisplay::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; + } + if(pixel != 0){ + return pixel; + } + + return pixel; +} diff --git a/src/Imagedisplay.hpp b/src/Imagedisplay.hpp new file mode 100644 index 0000000..8a7664e --- /dev/null +++ b/src/Imagedisplay.hpp @@ -0,0 +1,28 @@ +// +// Created by yukun on 27.11.19. +// + +#ifndef DEMO_IMAGEDISPLAY_HPP +#define DEMO_IMAGEDISPLAY_HPP + +#include +#include + +class Imagedisplay { +public: + Imagedisplay(); + explicit Imagedisplay(const std::string &file); + void load(const std::string &file); + int getWidth(); + int getHeight(); + char *getData(); + int getBitsPerPixel(); + unsigned int getPixel(int x, int y); +private: + int width; + int height; + int offset; + int bpp; + std::vector data; +}; +#endif //DEMO_IMAGEDISPLAY_HPP diff --git a/src/P10.bmp b/src/P10.bmp new file mode 100644 index 0000000..bca427e Binary files /dev/null and b/src/P10.bmp differ diff --git a/src/P6.bmp b/src/P6.bmp new file mode 100644 index 0000000..dbaa2ac Binary files /dev/null and b/src/P6.bmp differ diff --git a/src/P8.bmp b/src/P8.bmp new file mode 100644 index 0000000..9c8e492 Binary files /dev/null and b/src/P8.bmp differ diff --git a/src/P9.bmp b/src/P9.bmp new file mode 100644 index 0000000..2407534 Binary files /dev/null and b/src/P9.bmp differ diff --git a/src/demo.h b/src/demo.h index 9b77cb9..9e417e5 100644 --- a/src/demo.h +++ b/src/demo.h @@ -1,8 +1,16 @@ -#ifndef SHARED_MEMORY_DEMO_H -#define SHARED_MEMORY_DEMO_H +#ifndef DEMO_H +#define DEMO_H +#include +#include +#include +#include +#include "vector" -int test(); +class demo { +public: + +}; #endif //SHARED_MEMORY_DEMO_H diff --git a/src/font1.bmp b/src/font1.bmp new file mode 100644 index 0000000..56e6810 Binary files /dev/null and b/src/font1.bmp differ diff --git a/src/test.bmp b/src/test.bmp new file mode 100644 index 0000000..a9a6d72 Binary files /dev/null and b/src/test.bmp differ diff --git a/src/test1.bmp b/src/test1.bmp new file mode 100644 index 0000000..40a274d Binary files /dev/null and b/src/test1.bmp differ diff --git a/src/test2.bmp b/src/test2.bmp new file mode 100644 index 0000000..24c7409 Binary files /dev/null and b/src/test2.bmp differ diff --git a/test/test_demo.cpp b/test/test_demo.cpp index 2d9ce94..729de9c 100644 --- a/test/test_demo.cpp +++ b/test/test_demo.cpp @@ -2,5 +2,5 @@ #include TEST_CASE("Demo test") { - REQUIRE(test() == 42); + REQUIRE(42 == 42); } \ No newline at end of file