From d84e36d50b68272e0e2b45bde041fc3bb8582119 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 15 Oct 2019 16:12:56 +0200 Subject: [PATCH] +Bitmap class +Bitmap file loading +string output using font --- CMakeLists.txt | 2 +- src/Bitmap.cpp | 54 +++++++++++++++++++++++++++++++++++++++ src/Bitmap.h | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 52 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 src/Bitmap.cpp create mode 100644 src/Bitmap.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bfe6883..7a5296a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ include_directories(src) set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library/build") include_directories(${LIB_PATH}/include) -add_executable(TextRenderer ${SOURCES} ${HEADERS}) +add_executable(TextRenderer ${SOURCES} ${HEADERS} src/Bitmap.cpp src/Bitmap.h) target_link_libraries(TextRenderer ${LIB_PATH}/lib/liblibrary.a) diff --git a/src/Bitmap.cpp b/src/Bitmap.cpp new file mode 100644 index 0000000..9972e23 --- /dev/null +++ b/src/Bitmap.cpp @@ -0,0 +1,54 @@ +#include "Bitmap.h" +#include +#include + +Bitmap::Bitmap() { + offset = 0; + width = 0; + height = 0; + bpp = 0; + data = nullptr; +} + +Bitmap::~Bitmap() { + if(data){ + delete data; + data = nullptr; + } +} + +void Bitmap::load(const std::string &file) { + std::ifstream stream(file); + if(stream.is_open()){ + std::string str((std::istreambuf_iterator(stream)),std::istreambuf_iterator()); + data = new char[str.size()]; + for(int i = 0; i < str.size();i++){ + data[i] = str[i]; + } + + offset = *(unsigned int*)(data + 10); + width = *(unsigned int*)(data + 18); + height = *(unsigned int*)(data + 22); + bpp = *(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; +} + +char *Bitmap::getPixel(int x, int y) { + return getData() + ((getHeight() - y) * getWidth() + x) * getBitsPerPixel() / 8; +} diff --git a/src/Bitmap.h b/src/Bitmap.h new file mode 100644 index 0000000..48cc47c --- /dev/null +++ b/src/Bitmap.h @@ -0,0 +1,68 @@ +#ifndef TEXTRENDERER_BITMAP_H +#define TEXTRENDERER_BITMAP_H + +#include + +/* + * @author: Julian Hinxlage + * @since: v0.0.0 + * @brief: Used to load a Bitmap from a file. + */ +class Bitmap { +public: + Bitmap(); + + ~Bitmap(); + + /* + * @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 + */ + char *getPixel(int x, int y); + +private: + int width; + int height; + int offset; + int bpp; + char *data; +}; + + +#endif //TEXTRENDERER_BITMAP_H diff --git a/src/main.cpp b/src/main.cpp index 227cd45..a498b2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,57 @@ #include #include "add.h" +#include "Bitmap.h" +/* + * @author: Julian Hinxlage + * @since: v0.0.0 + * @brief: An image is loaded and used as a font. + * A string is converted and displayed in the console. + * Currently only to test. + */ int main() { - std::cout << "Hello, World!" << std::endl; - std::cout << add(6,7) << std::endl; + Bitmap bitmap; + bitmap.load("../res/font.bmp"); + + std::string str = "Hello, World!"; + + //values used to calculate the coordinates of the characters + int xOffset = 1; + int yOffset = 2; + int xSize = 6; + int ySize = 7; + int xCount = 18; + int xStart = 0; + int yStart = 2; + + //print vertical + //i is the current row + for (int i = 0; i < ySize; i++) { + //loop through the characters in the string + for (char c : str) { + //index of character(x and y) + int index = (c - ' '); + int xIndex = index % xCount; + int yIndex = index / xCount; + + //character index to pixel index conversion + int x = xIndex * (xSize + xOffset) + xStart; + int y = yIndex * (ySize + yOffset) + yStart; + + //print current row of the current character + for (int j = x; j < x + xSize; j++) { + auto *pixel = (unsigned int *) bitmap.getPixel(j, i + y); + if (*pixel == 0) { + std::cout << " "; + } else { + std::cout << "1"; + } + } + std::cout << " "; + } + std::cout << std::endl; + } + return 0; } \ No newline at end of file