~ converted to new keycodes
This commit is contained in:
parent
6bbf08be56
commit
1b6471ae94
@ -20,13 +20,11 @@ file(GLOB_RECURSE TESTS test/*.cpp)
|
||||
include_directories(src)
|
||||
include_directories(test)
|
||||
|
||||
#toml
|
||||
include_directories(lib/toml)
|
||||
|
||||
set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library")
|
||||
|
||||
include_directories(${LIB_PATH}/include)
|
||||
add_executable(Terminal ${SOURCES} ${HEADERS} main/main.cpp src/Terminal.h src/Terminal.cpp src/Bitmap.cpp src/Bitmap.h src/Font.cpp src/Font.h)
|
||||
add_executable(Terminal ${SOURCES} ${HEADERS} main/main.cpp)
|
||||
|
||||
target_link_libraries(Terminal ${LIB_PATH}/lib/liblibrary.a)
|
||||
|
||||
|
@ -14,12 +14,12 @@ int main() {
|
||||
vkvm::KeyCode keycode = vkvm::getLastPressedKey();
|
||||
std::cout << keycode << std::endl;
|
||||
|
||||
if(((vkvm::KeyCode::ShiftLeft & 0xfff) == keycode) ||((vkvm::KeyCode::ShiftRight & 0xfff) == keycode)){
|
||||
if(((vkvm::KeyCode::ShiftLeft) == keycode) ||((vkvm::KeyCode::ShiftRight) == keycode)){
|
||||
terminal.shiftpressed();
|
||||
}
|
||||
|
||||
if(((vkvm::KeyCode::Zero & 0xfff) <= keycode) && (keycode <= (vkvm::Nine & 0xfff))){
|
||||
char ch = keycode - (vkvm::KeyCode::Zero & 0xfff) + '0';
|
||||
if(((vkvm::KeyCode::Zero) <= keycode) && (keycode <= (vkvm::Nine))){
|
||||
char ch = keycode - (vkvm::KeyCode::Zero) + '0';
|
||||
std::cout << ch << std::endl;
|
||||
terminal.setString(ch);
|
||||
vkvm::setText(terminal.getString());
|
||||
@ -27,14 +27,14 @@ int main() {
|
||||
vkvm::callEvent(vkvm::EventType::RenderText);
|
||||
}
|
||||
|
||||
if(((vkvm::KeyCode::A & 0xfff) <= keycode) && (keycode <= (vkvm::KeyCode::Z & 0xfff))){
|
||||
if(((vkvm::KeyCode::A) <= keycode) && (keycode <= (vkvm::KeyCode::Z))){
|
||||
int status = terminal.getstatus();
|
||||
char ch;
|
||||
if(status == 0){
|
||||
ch = keycode - (vkvm::KeyCode::A & 0xfff) + 'a';
|
||||
ch = keycode - (vkvm::KeyCode::A) + 'a';
|
||||
}
|
||||
else{
|
||||
ch = keycode - (vkvm::KeyCode::A & 0xfff) + 'A';
|
||||
ch = keycode - (vkvm::KeyCode::A) + 'A';
|
||||
}
|
||||
std::cout << ch << std::endl;
|
||||
terminal.setString(ch);
|
||||
@ -43,8 +43,8 @@ int main() {
|
||||
vkvm::callEvent(vkvm::EventType::RenderText);
|
||||
}
|
||||
|
||||
if((vkvm::KeyCode::Space & 0xfff) == keycode){
|
||||
char ch = keycode - (vkvm::KeyCode::Space & 0xfff) + ' ';
|
||||
if((vkvm::KeyCode::Space) == keycode){
|
||||
char ch = keycode - (vkvm::KeyCode::Space) + ' ';
|
||||
std::cout << ch << std::endl;
|
||||
terminal.setString(ch);
|
||||
vkvm::setText(terminal.getString());
|
||||
@ -52,8 +52,8 @@ int main() {
|
||||
vkvm::callEvent(vkvm::EventType::RenderText);
|
||||
}
|
||||
|
||||
if((vkvm::KeyCode::Backspace & 0xfff) == keycode){
|
||||
char ch = keycode - (vkvm::KeyCode::Backspace & 0xfff) + ' ';
|
||||
if((vkvm::KeyCode::Backspace) == keycode){
|
||||
char ch = keycode - (vkvm::KeyCode::Backspace) + ' ';
|
||||
std::cout << ch << std::endl;
|
||||
terminal.subString();
|
||||
vkvm::setText(terminal.getString());
|
||||
@ -61,8 +61,8 @@ int main() {
|
||||
vkvm::callEvent(vkvm::EventType::RenderText);
|
||||
}
|
||||
|
||||
if((vkvm::KeyCode::Tab & 0xfff) == keycode){
|
||||
char ch = keycode - (vkvm::KeyCode::Tab & 0xfff) + ' ';
|
||||
if((vkvm::KeyCode::Tab) == keycode){
|
||||
char ch = keycode - (vkvm::KeyCode::Tab) + ' ';
|
||||
for(int i = 0; i < 3; i++) {
|
||||
std::cout << ch << std::endl;
|
||||
terminal.setString(ch);
|
||||
@ -72,7 +72,7 @@ int main() {
|
||||
vkvm::callEvent(vkvm::EventType::RenderText);
|
||||
}
|
||||
|
||||
if((vkvm::KeyCode::Enter & 0xfff) == keycode){
|
||||
if((vkvm::KeyCode::Enter) == keycode){
|
||||
char ch = '\n';
|
||||
std::cout << ch << std::endl;
|
||||
terminal.setString(ch);
|
||||
@ -86,7 +86,7 @@ int main() {
|
||||
|
||||
vkvm::registerEvent(vkvm::KeyUp, [&terminal]() {
|
||||
vkvm::KeyCode keycode = vkvm::getLastPressedKey();
|
||||
if(((vkvm::KeyCode::ShiftLeft & 0xfff) == keycode) ||((vkvm::KeyCode::ShiftRight & 0xfff) == keycode)){
|
||||
if(((vkvm::KeyCode::ShiftLeft) == keycode) ||((vkvm::KeyCode::ShiftRight) == keycode)){
|
||||
terminal.shiftup();
|
||||
}
|
||||
// char ch = keycode - (vkvm::KeyCode::A & 0xfff) + 'A';
|
||||
|
@ -1,63 +0,0 @@
|
||||
#include "Bitmap.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
Bitmap::Bitmap() {
|
||||
offset = 0;
|
||||
width = 0;
|
||||
height = 0;
|
||||
bpp = 0;
|
||||
}
|
||||
|
||||
Bitmap::Bitmap(const std::string &file) {
|
||||
offset = 0;
|
||||
width = 0;
|
||||
height = 0;
|
||||
bpp = 0;
|
||||
load(file);
|
||||
}
|
||||
|
||||
void Bitmap::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 Bitmap::getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
int Bitmap::getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
char *Bitmap::getData() {
|
||||
return &data[offset];
|
||||
}
|
||||
|
||||
int Bitmap::getBitsPerPixel() {
|
||||
return bpp;
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
if(pixel != 0){
|
||||
return pixel;
|
||||
}
|
||||
return pixel;
|
||||
}
|
67
src/Bitmap.h
67
src/Bitmap.h
@ -1,67 +0,0 @@
|
||||
#ifndef TERMINAL_BITMAP_H
|
||||
#define TERMINAL_BITMAP_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
* @since: v0.0.0
|
||||
* @brief: Used to load a Bitmap from a file.
|
||||
*/
|
||||
class Bitmap {
|
||||
public:
|
||||
Bitmap();
|
||||
explicit Bitmap(const std::string &file);
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
* @since: v0.0.0
|
||||
* @brief: Used to load a Bitmap from a file.
|
||||
*/
|
||||
void load(const std::string &file);
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
* @since: v0.0.0
|
||||
* @return: the width of the image.
|
||||
*/
|
||||
int getWidth();
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
* @since: v0.0.0
|
||||
* @return: the height of the image
|
||||
*/
|
||||
int getHeight();
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
* @since: v0.0.0
|
||||
* @return: the pixel data as an byte array
|
||||
*/
|
||||
char *getData();
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
* @since: v0.0.0
|
||||
* @return: the number of bits uses per pixel
|
||||
*/
|
||||
int getBitsPerPixel();
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
* @since: v0.0.0
|
||||
* @return: the pixel value by coordinates, (0,0) is on the top left
|
||||
*/
|
||||
unsigned int getPixel(int x, int y);
|
||||
|
||||
private:
|
||||
int width;
|
||||
int height;
|
||||
int offset;
|
||||
int bpp;
|
||||
std::vector<char> data;
|
||||
};
|
||||
|
||||
#endif //TERMINAL_BITMAP_H
|
79
src/Font.cpp
79
src/Font.cpp
@ -1,79 +0,0 @@
|
||||
#include "Font.h"
|
||||
#include <cpptoml.h>
|
||||
|
||||
|
||||
Font::Font() {
|
||||
xOffset = 0;
|
||||
yOffset = 0;
|
||||
xSize = 0;
|
||||
ySize = 0;
|
||||
xCount = 0;
|
||||
yCount = 0;
|
||||
xStart = 0;
|
||||
yStart = 0;
|
||||
fillValue = 0;
|
||||
firstChar = ' ';
|
||||
}
|
||||
|
||||
Font::Font(const std::string &file, const std::string &configFile) : Font() {
|
||||
load(file, configFile);
|
||||
}
|
||||
|
||||
void Font::load(const std::string &file, const std::string &configFile) {
|
||||
bitmap.load(file);
|
||||
auto config = cpptoml::parse_file(configFile);
|
||||
xOffset = config->get_as<int>("xOffset").value_or(0);
|
||||
yOffset = config->get_as<int>("yOffset").value_or(0);
|
||||
xSize = config->get_as<int>("xSize").value_or(0);
|
||||
ySize = config->get_as<int>("ySize").value_or(0);
|
||||
xCount = config->get_as<int>("xCount").value_or(0);
|
||||
yCount = config->get_as<int>("yOffset").value_or(0);
|
||||
xStart = config->get_as<int>("xStart").value_or(0);
|
||||
yStart = config->get_as<int>("yStart").value_or(0);
|
||||
fillValue = config->get_as<unsigned int>("fillValue").value_or(0);
|
||||
firstChar = (char)config->get_as<int>("firstChar").value_or(0);
|
||||
pixelSize = config->get_as<int>("pixelSize").value_or(0);
|
||||
gap = config->get_as<int>("gap").value_or(-1);
|
||||
invertedColors = config->get_as<int>("invertedColors").value_or(0);
|
||||
}
|
||||
|
||||
int Font::width() {
|
||||
return xSize;
|
||||
}
|
||||
|
||||
int Font::height() {
|
||||
return ySize;
|
||||
}
|
||||
|
||||
bool Font::getPixel(char character, int x, int y) {
|
||||
//index of character(x and y)
|
||||
int index = (character - firstChar);
|
||||
|
||||
if(gap != -1){
|
||||
if (index >= gap){
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
int xIndex = index % xCount;
|
||||
int yIndex = index / xCount;
|
||||
|
||||
if(index < 0){
|
||||
yIndex--;
|
||||
xIndex += xCount;
|
||||
}
|
||||
|
||||
//character index to pixel index conversion
|
||||
int xPos = xIndex * (xSize + xOffset) + xStart;
|
||||
int yPos = yIndex * (ySize + yOffset) + yStart;
|
||||
|
||||
bool value = bitmap.getPixel((xPos + x) * pixelSize, (yPos + y) * pixelSize) == fillValue;
|
||||
if(invertedColors){
|
||||
return !value;
|
||||
}else{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
51
src/Font.h
51
src/Font.h
@ -1,51 +0,0 @@
|
||||
//
|
||||
// Created by yukun on 05.12.19.
|
||||
//
|
||||
|
||||
#ifndef TERMINAL_FONT_H
|
||||
#define TERMINAL_FONT_H
|
||||
#include "Bitmap.h"
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
* @since: v0.0.0
|
||||
* @brief: this class provides pixel access based on characters
|
||||
*/
|
||||
class Font {
|
||||
public:
|
||||
Bitmap bitmap;
|
||||
|
||||
//space between characters
|
||||
int xOffset;
|
||||
int yOffset;
|
||||
|
||||
//size of a character
|
||||
int xSize;
|
||||
int ySize;
|
||||
|
||||
//count of characters per row
|
||||
int xCount;
|
||||
//count of rows
|
||||
int yCount;
|
||||
|
||||
//pixel offset of first character
|
||||
int xStart;
|
||||
int yStart;
|
||||
|
||||
unsigned int fillValue;
|
||||
char firstChar;
|
||||
int pixelSize;
|
||||
int gap;
|
||||
|
||||
bool invertedColors;
|
||||
|
||||
Font();
|
||||
explicit Font(const std::string &file, const std::string &configFile);
|
||||
void load(const std::string &file, const std::string &configFile);
|
||||
|
||||
int width();
|
||||
int height();
|
||||
bool getPixel(char character, int x, int y);
|
||||
};
|
||||
|
||||
#endif //TERMINAL_FONT_H
|
Loading…
Reference in New Issue
Block a user