This commit is contained in:
yukun 2019-11-28 15:20:15 +01:00
parent 852695860d
commit 8ef788ab64
9 changed files with 226 additions and 48 deletions

View File

@ -21,7 +21,7 @@ file(GLOB_RECURSE TESTS test/*.cpp)
set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library") set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library")
include_directories(${LIB_PATH}/include) include_directories(${LIB_PATH}/include)
add_executable(Demo ${SOURCES} ${HEADERS} main/main.cpp) add_executable(Demo ${SOURCES} ${HEADERS} main/main.cpp src/Imagedisplay.hpp src/Imagedisplay.cpp)
target_link_libraries(Demo ${LIB_PATH}/lib/liblibrary.a) target_link_libraries(Demo ${LIB_PATH}/lib/liblibrary.a)

View File

@ -1,58 +1,133 @@
//#include <iostream>
//#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 <iostream>
#include "../src/demo.h"
#include "internal.hpp" #include "internal.hpp"
#include "vkvm.hpp" #include "vkvm.hpp"
#include "../src/Imagedisplay.hpp"
#include <unistd.h>
int main(){
int main() {
vkvm::initialize(0); vkvm::initialize(0);
vkvm::registerEvent(vkvm::EventType::Redraw, [](){ vkvm::setDefaultValues();
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::registerEvent(vkvm::Timer, [](){
vkvm::buttonPressed(vkvm::KeyCode::D); std::cout <<"picture" << std::endl;
vkvm::buttonPressed(vkvm::KeyCode::Z); vkvm::Color backgroundColor = vkvm::getBackgroundColor();
vkvm::buttonPressed(vkvm::KeyCode::Backspcce); for(int i = 0; i<1000; i++) {
Imagedisplay imagedisplay("src/P1.bmp");
imagedisplay.load("src/P1.bmp");
for (int w; w < vkvm::getWidth(); w++) {
for (int h; h < vkvm::getHeight(); h++) {
unsigned int a ;
a = imagedisplay.getPixel(w, h);
vkvm::setPixel(w,h,vkvm::Color(a));
vkvm::KeyCode code = vkvm::getLastPressedKey(); }
while(code != 0){ }
vkvm::log(vkvm::INFO, "keyCode: ", code); sleep(5);
code = vkvm::getLastPressedKey(); vkvm::setDefaultValues();
Imagedisplay imagedisplay2("src/P2.bmp");
imagedisplay2.load("src/P2.bmp");
for (int w; w < vkvm::getWidth(); w++) {
for (int h; h < vkvm::getHeight(); h++) {
unsigned int a ;
a = imagedisplay2.getPixel(w, h);
vkvm::setPixel(w,h,vkvm::Color(a));
}
}
sleep(5);
vkvm::setDefaultValues();
Imagedisplay imagedisplay3("src/P3.bmp");
imagedisplay3.load("src/P3.bmp");
for (int w; w < vkvm::getWidth(); w++) {
for (int h; h < vkvm::getHeight(); h++){
unsigned int a ;
a = imagedisplay3.getPixel(w, h);
vkvm::setPixel(w,h,vkvm::Color(a));
} }
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"){ sleep(5);
vkvm::setDefaultValues();
Imagedisplay imagedisplay4("src/P4.bmp");
imagedisplay4.load("src/P4.bmp");
for (int w; w < vkvm::getWidth(); w++) {
for (int h; h < vkvm::getHeight(); h++){
unsigned int a ;
a = imagedisplay4.getPixel(w, h);
vkvm::setPixel(w,h,vkvm::Color(a));
}
}
sleep(5);
}
});
vkvm::callEvent(vkvm::Timer); vkvm::callEvent(vkvm::Timer);
}
}
return 0;
} }

66
src/Imagedisplay.cpp Normal file
View File

@ -0,0 +1,66 @@
//
// Created by yukun on 27.11.19.
//
#include "Imagedisplay.hpp"
#include <fstream>
#include <iostream>
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<char>(stream)),std::istreambuf_iterator<char>());
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];
}
if(pixel != 0){
return pixel;
}
return pixel;
}

28
src/Imagedisplay.hpp Normal file
View File

@ -0,0 +1,28 @@
//
// Created by yukun on 27.11.19.
//
#ifndef DEMO_IMAGEDISPLAY_HPP
#define DEMO_IMAGEDISPLAY_HPP
#include <string>
#include <vector>
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<char> data;
};
#endif //DEMO_IMAGEDISPLAY_HPP

BIN
src/P1.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

BIN
src/P2.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

BIN
src/P3.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
src/P4.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@ -1,8 +1,17 @@
#ifndef SHARED_MEMORY_DEMO_H #ifndef DEMO_H
#define SHARED_MEMORY_DEMO_H #define DEMO_H
#include <string>
#include <Color.hpp>
#include <vkvm.hpp>
#include <iostream>
#include "Font.h"
#include "vector"
int test(); class demo {
public:
};
#endif //SHARED_MEMORY_DEMO_H #endif //SHARED_MEMORY_DEMO_H