~ refactored everything

This commit is contained in:
Johannes Theiner 2019-12-30 15:13:06 +01:00
parent 9c7763f58c
commit 21564bd54a
14 changed files with 25 additions and 242 deletions

View File

@ -1,187 +1 @@
//#include <iostream> #include "internal.hpp" #include "vkvm.hpp" #include "../src/Bitmap.hpp" #include <unistd.h> //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); } }
//#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 <iostream>
#include "../src/demo.h"
#include "internal.hpp"
#include "vkvm.hpp"
#include "../src/Imagedisplay.hpp"
#include <unistd.h>
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";
}
}

View File

Before

Width:  |  Height:  |  Size: 780 KiB

After

Width:  |  Height:  |  Size: 780 KiB

View File

Before

Width:  |  Height:  |  Size: 1.0 MiB

After

Width:  |  Height:  |  Size: 1.0 MiB

View File

Before

Width:  |  Height:  |  Size: 844 KiB

After

Width:  |  Height:  |  Size: 844 KiB

View File

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View File

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

View File

Before

Width:  |  Height:  |  Size: 659 KiB

After

Width:  |  Height:  |  Size: 659 KiB

View File

Before

Width:  |  Height:  |  Size: 659 KiB

After

Width:  |  Height:  |  Size: 659 KiB

View File

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.6 MiB

View File

@ -1,26 +1,23 @@
// #include "Bitmap.hpp"
// Created by yukun on 27.11.19.
//
#include "Imagedisplay.hpp"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
Imagedisplay::Imagedisplay() { Bitmap::Bitmap() {
offset = 0; offset = 0;
width = 0; width = 0;
height = 0; height = 0;
bpp = 0; bitsPerPixel = 0;
} }
Imagedisplay::Imagedisplay(const std::string &file) { Bitmap::Bitmap(const std::string &file) {
offset = 0; offset = 0;
width = 0; width = 0;
height = 0; height = 0;
bpp = 0; bitsPerPixel = 0;
load(file); load(file);
} }
void Imagedisplay::load(const std::string &file) { void Bitmap::load(const std::string &file) {
std::ifstream stream; std::ifstream stream;
stream.open(file); stream.open(file);
if(stream.is_open()){ if(stream.is_open()){
@ -29,35 +26,34 @@ void Imagedisplay::load(const std::string &file) {
for(int i = 0; i < str.size();i++){ for(int i = 0; i < str.size();i++){
data[i] = str[i]; data[i] = str[i];
} }
offset = (int)*(unsigned int*)(&data[10]); offset = static_cast<int>(*reinterpret_cast<unsigned int *>(&data[10]));
width = (int)*(unsigned int*)(&data[18]); width = static_cast<int>(*reinterpret_cast<unsigned int *>(&data[18]));
height = (int)*(unsigned int*)(&data[22]); height = static_cast<int>(*reinterpret_cast<unsigned int *>(&data[22]));
bpp = (int)*(unsigned short*)(&data[28]); bitsPerPixel = static_cast<int>(*reinterpret_cast<uint16_t *>(&data[28]));
} }
} }
int Imagedisplay::getWidth() { int Bitmap::getWidth() {
return width; return width;
} }
int Imagedisplay::getHeight() { int Bitmap::getHeight() {
return height; return height;
} }
char *Imagedisplay::getData() { char *Bitmap::getData() {
return &data[offset]; return &data[offset];
} }
int Imagedisplay::getBitsPerPixel() { int Bitmap::getBitsPerPixel() {
return bpp; return bitsPerPixel;
} }
unsigned int Imagedisplay::getPixel(int x, int y) { unsigned int Bitmap::getPixel(int x, int y) {
unsigned int pixel = 0; unsigned int pixel = 0;
char *ptr = getData() + ((getHeight() - 1 - y) * getWidth() + x) * (getBitsPerPixel() / 8); char *ptr = getData() + ((getHeight() - 1 - y) * getWidth() + x) * (getBitsPerPixel() / 8);
for(int i = 0; i < getBitsPerPixel() / 8;i++){ for(int i = 0; i < getBitsPerPixel() / 8;i++){
*((char*)&pixel+i) = ptr[i]; *((char*)&pixel+i) = ptr[i];//NOLINT
// std::cout<< "hi: im" << (unsigned int) ptr[i]<< std::endl;
} }
if(pixel != 0){ if(pixel != 0){
return pixel; return pixel;

View File

@ -1,17 +1,13 @@
// #ifndef DEMO_BITMAP_HPP
// Created by yukun on 27.11.19. #define DEMO_BITMAP_HPP
//
#ifndef DEMO_IMAGEDISPLAY_HPP
#define DEMO_IMAGEDISPLAY_HPP
#include <string> #include <string>
#include <vector> #include <vector>
class Imagedisplay { class Bitmap {
public: public:
Imagedisplay(); Bitmap();
explicit Imagedisplay(const std::string &file); explicit Bitmap(const std::string &file);
void load(const std::string &file); void load(const std::string &file);
int getWidth(); int getWidth();
int getHeight(); int getHeight();
@ -22,7 +18,7 @@ private:
int width; int width;
int height; int height;
int offset; int offset;
int bpp; int bitsPerPixel;
std::vector<char> data; std::vector<char> data;
}; };
#endif //DEMO_IMAGEDISPLAY_HPP #endif //DEMO_BITMAP_HPP

View File

@ -1,6 +0,0 @@
#include "demo.h"
int test() {
const int returnValue = 42;
return returnValue;
}

View File

@ -1,16 +0,0 @@
#ifndef DEMO_H
#define DEMO_H
#include <string>
#include <Color.hpp>
#include <vkvm.hpp>
#include <iostream>
#include "vector"
class demo {
public:
};
#endif //SHARED_MEMORY_DEMO_H

View File

@ -1,4 +1,3 @@
#include "../src/demo.h"
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
TEST_CASE("Demo test") { TEST_CASE("Demo test") {