+ Font class
This commit is contained in:
parent
e09ee7b347
commit
7da7d96b17
|
@ -1,7 +1,8 @@
|
|||
#include <iostream>
|
||||
|
||||
//#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 << " ";
|
||||
}
|
||||
|
|
|
@ -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<char>(stream)),std::istreambuf_iterator<char>());
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define TEXTRENDERER_BITMAP_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* @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<char> data;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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
|
Loading…
Reference in New Issue