aboutsummaryrefslogtreecommitdiff
path: root/src/Bitmap.cpp
blob: 733eefeb91bf14b0a1560aabb8f361fdae16a032 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "Bitmap.h"

Bitmap::Bitmap(int width_, int height_) {
  std::chrono::high_resolution_clock::time_point time1 = std::chrono::high_resolution_clock::now();

	width = width_;
	height = height_;
	pixels = new unsigned char[width * height * 4];
  image = nullptr;

	std::chrono::high_resolution_clock::time_point time2 = std::chrono::high_resolution_clock::now();
	loadingTime = std::chrono::duration_cast<std::chrono::duration<double>>(time2 - time1);
}

Bitmap::Bitmap(std::string fileName) {
  std::chrono::high_resolution_clock::time_point time1 = std::chrono::high_resolution_clock::now();

  image = new sf::Image();
  isLoaded = image->loadFromFile(fileName);
  if (isLoaded) {
    width = image->getSize().x;
    height = image->getSize().y;
    pixels = (unsigned char*)image->getPixelsPtr();
  }
  else {
    width = 0;
    height = 0;
    pixels = nullptr;
  }

	std::chrono::high_resolution_clock::time_point time2 = std::chrono::high_resolution_clock::now();
	loadingTime = std::chrono::duration_cast<std::chrono::duration<double>>(time2 - time1);
}

Bitmap::~Bitmap() {
  if (image == nullptr)
    delete[] pixels;
  else
    delete image;
}

void Bitmap::fill(int r, int g, int b) {
	for (int i = 0; i < width * height * 4; i+=4)
	{
		pixels[i + 0] = r;
		pixels[i + 1] = g;
		pixels[i + 2] = b;
		pixels[i + 3] = 255;
	}
}

void Bitmap::setPixel(int x, int y, int r, int g, int b) {
	int hlp = ((y * width) + x) * 4;
	if (!(hlp < 0 || hlp >(width * height * 4) - 3)) {
		pixels[hlp + 0] = r;
		pixels[hlp + 1] = g;
		pixels[hlp + 2] = b;
		pixels[hlp + 3] = 255;
	}
	else {
		std::cout << "Position of pixel:\t" << hlp << "\tX: " << x << "\tY: "<< y << "\n";
	}
}