From a8914cb35793879d328bf31799b8adb1e43d0bc3 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 17 Dec 2019 12:06:44 +0100 Subject: [PATCH] ~ fix code to reduce warnings --- .ci/clang-tidy.sh | 0 main/main.cpp | 50 +++++++++++++------------------------------- src/Bitmap.cpp | 12 +++++------ src/Font.cpp | 12 ++++------- src/Font.h | 4 +++- src/TextRenderer.cpp | 18 +++++++++------- src/TextRenderer.h | 12 ++++------- test/test_bitmap.cpp | 6 +++--- 8 files changed, 45 insertions(+), 69 deletions(-) mode change 100644 => 100755 .ci/clang-tidy.sh diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh old mode 100644 new mode 100755 diff --git a/main/main.cpp b/main/main.cpp index 7d25eeb..1be5873 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,10 +1,10 @@ -#include "Bitmap.h" -#include "Font.h" -#include "TextRenderer.h" -#include "internal.hpp" +#include "../src/Bitmap.h" +#include "../src/Font.h" +#include "../src/TextRenderer.h" +#include #include -bool isQuit(std::string command); +bool isQuit(const std::string& command); /** * @author: Julian Hinxlage @@ -37,14 +37,17 @@ int main() { std::string command; std::cout << "TextRender: "; std::getline(std::cin, command); - while (!isQuit(command)) { + while (command != "quit") { if (command == "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::getline(std::cin, command); @@ -54,29 +57,4 @@ int main() { std::cout << "TextRender finished." << std::endl; 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; -} +} \ No newline at end of file diff --git a/src/Bitmap.cpp b/src/Bitmap.cpp index a264e50..aae17ce 100644 --- a/src/Bitmap.cpp +++ b/src/Bitmap.cpp @@ -26,11 +26,10 @@ void Bitmap::load(const std::string &file) { 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]); + offset = *reinterpret_cast(&data[10]); + width = *reinterpret_cast(&data[18]); + height = *reinterpret_cast(&data[22]); + bpp = *reinterpret_cast(&data[28]); } } @@ -54,7 +53,8 @@ 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]; + auto value = reinterpret_cast(&pixel+i); + *value = ptr[i]; } if(pixel != 0){ return pixel; diff --git a/src/Font.cpp b/src/Font.cpp index 0a601b7..a219ebd 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -3,7 +3,7 @@ // #include "Font.h" -#include +#include "../lib/toml/cpptoml.h" Font::Font() { xOffset = 0; @@ -34,10 +34,10 @@ void Font::load(const std::string &file, const std::string &configFile) { xStart = config->get_as("xStart").value_or(0); yStart = config->get_as("yStart").value_or(0); fillValue = config->get_as("fillValue").value_or(0); - firstChar = (char)config->get_as("firstChar").value_or(0); + firstChar = config->get_as("firstChar").value_or(0); pixelSize = config->get_as("pixelSize").value_or(0); gap = config->get_as("gap").value_or(-1); - invertedColors = config->get_as("invertedColors").value_or(0); + invertedColors = config->get_as("invertedColors").value_or(0); } int Font::width() { @@ -71,10 +71,6 @@ bool Font::getPixel(char character, int x, int y) { int yPos = yIndex * (ySize + yOffset) + yStart; bool value = bitmap.getPixel((xPos + x) * pixelSize, (yPos + y) * pixelSize) == fillValue; - if(invertedColors){ - return !value; - }else{ - return value; - } + return invertedColors != value; } diff --git a/src/Font.h b/src/Font.h index e4c45d5..a33cdfc 100644 --- a/src/Font.h +++ b/src/Font.h @@ -13,7 +13,7 @@ * @brief: this class provides pixel access based on characters */ class Font { -public: +private: Bitmap bitmap; //space between characters @@ -40,6 +40,8 @@ public: bool invertedColors; +public: + Font(); explicit Font(const std::string &file, const std::string &configFile); void load(const std::string &file, const std::string &configFile); diff --git a/src/TextRenderer.cpp b/src/TextRenderer.cpp index 28e6296..a9abbda 100644 --- a/src/TextRenderer.cpp +++ b/src/TextRenderer.cpp @@ -3,18 +3,20 @@ // #include "TextRenderer.h" +#include -TextRenderer::TextRenderer() {} + +TextRenderer::TextRenderer() = default; void TextRenderer::update(std::string newText) { int startX = 0; int startY = 0; int i; - std::string fontResourcePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".bmp"; - std::string fontConfigureFilePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".toml"; + std::string fontResourcePath = "../res/font" + std::to_string(vkvm::getFont()) + ".bmp"; + std::string fontConfigureFilePath = "../res/font" + std::to_string(vkvm::getFont()) + ".toml"; font = Font(fontResourcePath, fontConfigureFilePath); - int fontNumbersInOneLine = vkvm::getWidth() / (vkvm::getFont().getWidth() + left_margin); + int fontNumbersInOneLine = vkvm::getWidth() / (font.width() + left_margin); std::vector> characterBitmap; for(i = 0; i < newText.size(); i++) { @@ -31,7 +33,8 @@ void TextRenderer::update(std::string newText) { } void TextRenderer::clear() { - int x, y; + int x; + int y; for(y = 0; y < vkvm::getHeight(); y++) { for(x = 0; x < vkvm::getWidth(); x++) { vkvm::setPixel(x, y, vkvm::getBackgroundColor()); @@ -40,7 +43,7 @@ void TextRenderer::clear() { } void TextRenderer::setOldText(std::string text) { - oldText = text; + oldText = std::move(text); } std::vector> TextRenderer::getCharacter(unsigned char character) { @@ -62,7 +65,8 @@ std::vector> TextRenderer::getCharacter(unsigned char characte } void TextRenderer::translateToSharedMemory(std::vector> characterBitmap, int startX, int startY) { - int x, y; + int x; + int y; int currentX = startX; int currentY = startY; diff --git a/src/TextRenderer.h b/src/TextRenderer.h index b362c76..bf73735 100644 --- a/src/TextRenderer.h +++ b/src/TextRenderer.h @@ -5,15 +5,11 @@ #ifndef TEXTRENDERER_TEXTRENDERER_H #define TEXTRENDERER_TEXTRENDERER_H -#define BOLD 0b001 -#define ITALICS 0b010 -#define UNDERLINE 0b100 - -#include -#include -#include -#include #include "Font.h" +#include +#include +#include +#include /** * @author: Yajie Qi, Shaohua Tong diff --git a/test/test_bitmap.cpp b/test/test_bitmap.cpp index 1746a06..5847d49 100644 --- a/test/test_bitmap.cpp +++ b/test/test_bitmap.cpp @@ -1,6 +1,6 @@ +#include "../src/Bitmap.h" +#include "../src/Font.h" #include -#include "Bitmap.h" -#include "Font.h" TEST_CASE("default values") { Bitmap bitmap = Bitmap(); @@ -20,7 +20,7 @@ TEST_CASE("Font") { Font font("../res/font1.bmp", "../res/font1.toml"); REQUIRE(font.width() == 5); REQUIRE(font.height() == 7); - REQUIRE(!font.getPixel('a', 1,1)); + REQUIRE_FALSE(font.getPixel('a', 1,1)); REQUIRE(font.getPixel('a', 1,2)); }