~ fix code to reduce warnings

This commit is contained in:
Johannes Theiner 2019-12-17 12:06:44 +01:00
parent 2286c59325
commit a8914cb357
8 changed files with 45 additions and 69 deletions

0
.ci/clang-tidy.sh Normal file → Executable file
View File

View File

@ -1,10 +1,10 @@
#include "Bitmap.h" #include "../src/Bitmap.h"
#include "Font.h" #include "../src/Font.h"
#include "TextRenderer.h" #include "../src/TextRenderer.h"
#include "internal.hpp" #include <internal.hpp>
#include <iostream> #include <iostream>
bool isQuit(std::string command); bool isQuit(const std::string& command);
/** /**
* @author: Julian Hinxlage * @author: Julian Hinxlage
@ -37,14 +37,17 @@ int main() {
std::string command; std::string command;
std::cout << "TextRender: "; std::cout << "TextRender: ";
std::getline(std::cin, command); std::getline(std::cin, command);
while (!isQuit(command)) { while (command != "quit") {
if (command == "clear") { if (command == "clear") {
textRenderer.clear(); textRenderer.clear();
} if (command == "redblue") {
vkvm::setBackgroundColor(vkvm::red);
vkvm::setForegroundColor(vkvm::blue);
} else {
vkvm::setText(command);
currentText = vkvm::getText();
textRenderer.update(currentText);
} }
vkvm::setText(command);
currentText = vkvm::getText();
textRenderer.update(currentText);
std::cout << "TextRender: "; std::cout << "TextRender: ";
std::getline(std::cin, command); std::getline(std::cin, command);
@ -54,29 +57,4 @@ int main() {
std::cout << "TextRender finished." << std::endl; std::cout << "TextRender finished." << std::endl;
return 0; return 0;
} }
// std::string str;
// std::cout << "string to draw: ";
// std::getline(std::cin, str);
//
// for (int i = 0; i < font.height(); i++) {
// for (char c : str) {
// for (int j = 0; j < font.width(); j++) {
// if (font.getPixel(c,j,i)) {
// std::cout << "█";
// } else {
// std::cout << " ";
// }
// }
// std::cout << " ";
// }
// std::cout << std::endl;
// }
//
// return 0;
bool isQuit(std::string command) {
return command.compare("quit") == 0;
}

View File

@ -26,11 +26,10 @@ void Bitmap::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 = *reinterpret_cast<int*>(&data[10]);
offset = (int)*(unsigned int*)(&data[10]); width = *reinterpret_cast<int*>(&data[18]);
width = (int)*(unsigned int*)(&data[18]); height = *reinterpret_cast<int*>(&data[22]);
height = (int)*(unsigned int*)(&data[22]); bpp = *reinterpret_cast<uint16_t *>(&data[28]);
bpp = (int)*(unsigned short*)(&data[28]);
} }
} }
@ -54,7 +53,8 @@ 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]; auto value = reinterpret_cast<char*>(&pixel+i);
*value = ptr[i];
} }
if(pixel != 0){ if(pixel != 0){
return pixel; return pixel;

View File

@ -3,7 +3,7 @@
// //
#include "Font.h" #include "Font.h"
#include <cpptoml.h> #include "../lib/toml/cpptoml.h"
Font::Font() { Font::Font() {
xOffset = 0; xOffset = 0;
@ -34,10 +34,10 @@ void Font::load(const std::string &file, const std::string &configFile) {
xStart = config->get_as<int>("xStart").value_or(0); xStart = config->get_as<int>("xStart").value_or(0);
yStart = config->get_as<int>("yStart").value_or(0); yStart = config->get_as<int>("yStart").value_or(0);
fillValue = config->get_as<unsigned int>("fillValue").value_or(0); fillValue = config->get_as<unsigned int>("fillValue").value_or(0);
firstChar = (char)config->get_as<int>("firstChar").value_or(0); firstChar = config->get_as<char>("firstChar").value_or(0);
pixelSize = config->get_as<int>("pixelSize").value_or(0); pixelSize = config->get_as<int>("pixelSize").value_or(0);
gap = config->get_as<int>("gap").value_or(-1); gap = config->get_as<int>("gap").value_or(-1);
invertedColors = config->get_as<int>("invertedColors").value_or(0); invertedColors = config->get_as<bool>("invertedColors").value_or(0);
} }
int Font::width() { int Font::width() {
@ -71,10 +71,6 @@ bool Font::getPixel(char character, int x, int y) {
int yPos = yIndex * (ySize + yOffset) + yStart; int yPos = yIndex * (ySize + yOffset) + yStart;
bool value = bitmap.getPixel((xPos + x) * pixelSize, (yPos + y) * pixelSize) == fillValue; bool value = bitmap.getPixel((xPos + x) * pixelSize, (yPos + y) * pixelSize) == fillValue;
if(invertedColors){ return invertedColors != value;
return !value;
}else{
return value;
}
} }

View File

@ -13,7 +13,7 @@
* @brief: this class provides pixel access based on characters * @brief: this class provides pixel access based on characters
*/ */
class Font { class Font {
public: private:
Bitmap bitmap; Bitmap bitmap;
//space between characters //space between characters
@ -40,6 +40,8 @@ public:
bool invertedColors; bool invertedColors;
public:
Font(); Font();
explicit Font(const std::string &file, const std::string &configFile); explicit Font(const std::string &file, const std::string &configFile);
void load(const std::string &file, const std::string &configFile); void load(const std::string &file, const std::string &configFile);

View File

@ -3,18 +3,20 @@
// //
#include "TextRenderer.h" #include "TextRenderer.h"
#include <utility>
TextRenderer::TextRenderer() {}
TextRenderer::TextRenderer() = default;
void TextRenderer::update(std::string newText) { void TextRenderer::update(std::string newText) {
int startX = 0; int startX = 0;
int startY = 0; int startY = 0;
int i; int i;
std::string fontResourcePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".bmp"; std::string fontResourcePath = "../res/font" + std::to_string(vkvm::getFont()) + ".bmp";
std::string fontConfigureFilePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".toml"; std::string fontConfigureFilePath = "../res/font" + std::to_string(vkvm::getFont()) + ".toml";
font = Font(fontResourcePath, fontConfigureFilePath); font = Font(fontResourcePath, fontConfigureFilePath);
int fontNumbersInOneLine = vkvm::getWidth() / (vkvm::getFont().getWidth() + left_margin); int fontNumbersInOneLine = vkvm::getWidth() / (font.width() + left_margin);
std::vector<std::vector<bool>> characterBitmap; std::vector<std::vector<bool>> characterBitmap;
for(i = 0; i < newText.size(); i++) { for(i = 0; i < newText.size(); i++) {
@ -31,7 +33,8 @@ void TextRenderer::update(std::string newText) {
} }
void TextRenderer::clear() { void TextRenderer::clear() {
int x, y; int x;
int y;
for(y = 0; y < vkvm::getHeight(); y++) { for(y = 0; y < vkvm::getHeight(); y++) {
for(x = 0; x < vkvm::getWidth(); x++) { for(x = 0; x < vkvm::getWidth(); x++) {
vkvm::setPixel(x, y, vkvm::getBackgroundColor()); vkvm::setPixel(x, y, vkvm::getBackgroundColor());
@ -40,7 +43,7 @@ void TextRenderer::clear() {
} }
void TextRenderer::setOldText(std::string text) { void TextRenderer::setOldText(std::string text) {
oldText = text; oldText = std::move(text);
} }
std::vector<std::vector<bool>> TextRenderer::getCharacter(unsigned char character) { std::vector<std::vector<bool>> TextRenderer::getCharacter(unsigned char character) {
@ -62,7 +65,8 @@ std::vector<std::vector<bool>> TextRenderer::getCharacter(unsigned char characte
} }
void TextRenderer::translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY) { void TextRenderer::translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY) {
int x, y; int x;
int y;
int currentX = startX; int currentX = startX;
int currentY = startY; int currentY = startY;

View File

@ -5,15 +5,11 @@
#ifndef TEXTRENDERER_TEXTRENDERER_H #ifndef TEXTRENDERER_TEXTRENDERER_H
#define TEXTRENDERER_TEXTRENDERER_H #define TEXTRENDERER_TEXTRENDERER_H
#define BOLD 0b001
#define ITALICS 0b010
#define UNDERLINE 0b100
#include <string>
#include <Color.hpp>
#include <vkvm.hpp>
#include <iostream>
#include "Font.h" #include "Font.h"
#include <Color.hpp>
#include <iostream>
#include <string>
#include <vkvm.hpp>
/** /**
* @author: Yajie Qi, Shaohua Tong * @author: Yajie Qi, Shaohua Tong

View File

@ -1,6 +1,6 @@
#include "../src/Bitmap.h"
#include "../src/Font.h"
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
#include "Bitmap.h"
#include "Font.h"
TEST_CASE("default values") { TEST_CASE("default values") {
Bitmap bitmap = Bitmap(); Bitmap bitmap = Bitmap();
@ -20,7 +20,7 @@ TEST_CASE("Font") {
Font font("../res/font1.bmp", "../res/font1.toml"); Font font("../res/font1.bmp", "../res/font1.toml");
REQUIRE(font.width() == 5); REQUIRE(font.width() == 5);
REQUIRE(font.height() == 7); REQUIRE(font.height() == 7);
REQUIRE(!font.getPixel('a', 1,1)); REQUIRE_FALSE(font.getPixel('a', 1,1));
REQUIRE(font.getPixel('a', 1,2)); REQUIRE(font.getPixel('a', 1,2));
} }