From 7da7d96b173970b504233f7abe780236a0c721be Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 22 Oct 2019 15:34:51 +0200 Subject: [PATCH] + Font class --- main/main.cpp | 40 +++++++--------------------------------- src/Bitmap.cpp | 36 +++++++++++++++++++++--------------- src/Bitmap.h | 8 ++++---- src/Font.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/Font.h | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 52 deletions(-) create mode 100644 src/Font.cpp create mode 100644 src/Font.h diff --git a/main/main.cpp b/main/main.cpp index 3a0f898..cbf1f9d 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,7 +1,8 @@ #include -//#include "add.h" +#include "add.h" #include "Bitmap.h" +#include "Font.h" /** * @author: Julian Hinxlage @@ -11,50 +12,23 @@ * Currently only to test. */ int main() { - Bitmap bitmap; - bitmap.load("../res/font.bmp"); + Font font("../res/font.bmp"); std::string str; std::cout << "string to draw: "; std::cin >> str; - //values used to calculate the coordinates of the characters - int xOffset = 2; - int yOffset = 2; - int xSize = 5; - int ySize = 7; - int xCount = 18; - int xStart = 1; - 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 (int i = 0; i < font.height(); i++) { for (char c : str) { - //index of character(x and y) - int index = (c - ' '); - int xIndex = index % xCount; - int yIndex = index / xCount; + for (int j = 0; j < font.width(); j++) { - //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++) { - char *pixel_ptr = bitmap.getPixel(j, i + y); - unsigned int pixel = 0; - *((char*)&pixel+0) = pixel_ptr[0]; - *((char*)&pixel+1) = pixel_ptr[1]; - *((char*)&pixel+2) = pixel_ptr[2]; + unsigned int pixel = font.getPixel(c,j,i); if (pixel == 0) { std::cout << " "; } else { - std::cout << "1"; + std::cout << "1"; } - } std::cout << " "; } diff --git a/src/Bitmap.cpp b/src/Bitmap.cpp index 9972e23..a7fc8e8 100644 --- a/src/Bitmap.cpp +++ b/src/Bitmap.cpp @@ -7,29 +7,30 @@ Bitmap::Bitmap() { width = 0; height = 0; bpp = 0; - data = nullptr; } -Bitmap::~Bitmap() { - if(data){ - delete data; - data = nullptr; - } +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(file); + std::ifstream stream; + stream.open(file); if(stream.is_open()){ std::string str((std::istreambuf_iterator(stream)),std::istreambuf_iterator()); - data = new char[str.size()]; + data.resize(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); + offset = (int)*(unsigned int*)(&data[10]); + width = (int)*(unsigned int*)(&data[18]); + height = (int)*(unsigned int*)(&data[22]); + bpp = (int)*(unsigned short*)(&data[28]); } } @@ -42,13 +43,18 @@ int Bitmap::getHeight() { } char *Bitmap::getData() { - return data + offset; + return &data[offset]; } int Bitmap::getBitsPerPixel() { return bpp; } -char *Bitmap::getPixel(int x, int y) { - return getData() + ((getHeight() - y) * getWidth() + x) * getBitsPerPixel() / 8; +unsigned int Bitmap::getPixel(int x, int y) { + unsigned int pixel = 0; + char *ptr = getData() + ((getHeight() - y) * getWidth() + x) * getBitsPerPixel() / 8; + for(int i = 0; i < getBitsPerPixel() / 8;i++){ + *((char*)&pixel+i) = ptr[i]; + } + return pixel; } diff --git a/src/Bitmap.h b/src/Bitmap.h index 2619a22..7c82032 100644 --- a/src/Bitmap.h +++ b/src/Bitmap.h @@ -2,6 +2,7 @@ #define TEXTRENDERER_BITMAP_H #include +#include /** * @author: Julian Hinxlage @@ -11,8 +12,7 @@ class Bitmap { public: Bitmap(); - - ~Bitmap(); + explicit Bitmap(const std::string &file); /** * @author: Julian Hinxlage @@ -54,14 +54,14 @@ public: * @since: v0.0.0 * @return: the pixel value by coordinates, (0,0) is on the top left */ - char *getPixel(int x, int y); + unsigned int getPixel(int x, int y); private: int width; int height; int offset; int bpp; - char *data; + std::vector data; }; diff --git a/src/Font.cpp b/src/Font.cpp new file mode 100644 index 0000000..3c07b4a --- /dev/null +++ b/src/Font.cpp @@ -0,0 +1,46 @@ +// +// Copyright (c) 2019 Julian Hinxlage. All rights reserved. +// + +#include "Font.h" + +Font::Font() { + xOffset = 2; + yOffset = 2; + xSize = 5; + ySize = 7; + xCount = 18; + yCount = 4; + xStart = 1; + yStart = 2; +} + +Font::Font(const std::string &file) : Font() { + bitmap.load(file); +} + +void Font::load(const std::string &file) { + bitmap.load(file); +} + +int Font::width() { + return xSize; +} + +int Font::height() { + return ySize; +} + +unsigned int Font::getPixel(char character, int x, int y) { + //index of character(x and y) + int index = (character - ' '); + int xIndex = index % xCount; + int yIndex = index / xCount; + + //character index to pixel index conversion + int xPos = xIndex * (xSize + xOffset) + xStart; + int yPos = yIndex * (ySize + yOffset) + yStart; + + return bitmap.getPixel(xPos + x, yPos + y); +} + diff --git a/src/Font.h b/src/Font.h new file mode 100644 index 0000000..46de566 --- /dev/null +++ b/src/Font.h @@ -0,0 +1,41 @@ +// +// Copyright (c) 2019 Julian Hinxlage. All rights reserved. +// + +#ifndef TEXTRENDERER_FONT_H +#define TEXTRENDERER_FONT_H + +#include "Bitmap.h" + +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; + + Font(); + explicit Font(const std::string &file); + void load(const std::string &file); + + int width(); + int height(); + unsigned int getPixel(char character, int x, int y); +}; + + +#endif //TEXTRENDERER_FONT_H