From 9e7016e23471b7ba67598b5558c7fc7c528f1f2a Mon Sep 17 00:00:00 2001 From: Mateja Date: Fri, 19 Feb 2021 17:28:33 +0100 Subject: Moved texture loading to `Bitmap` class. --- src/Bitmap.cpp | 31 ++++++++++++++++++++++++++++++- src/Bitmap.h | 9 +++++++++ src/ObjModel.h | 2 -- src/main.cpp | 31 +++++-------------------------- 4 files changed, 44 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/Bitmap.cpp b/src/Bitmap.cpp index 363145f..733eefe 100644 --- a/src/Bitmap.cpp +++ b/src/Bitmap.cpp @@ -1,13 +1,42 @@ #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>(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>(time2 - time1); } Bitmap::~Bitmap() { - delete[] pixels; + if (image == nullptr) + delete[] pixels; + else + delete image; } void Bitmap::fill(int r, int g, int b) { diff --git a/src/Bitmap.h b/src/Bitmap.h index c4064ad..60a72bd 100644 --- a/src/Bitmap.h +++ b/src/Bitmap.h @@ -1,10 +1,19 @@ #include +#include +#include + +#include + class Bitmap { public: int width, height; unsigned char *pixels; + sf::Image* image; + bool isLoaded; + std::chrono::duration loadingTime; Bitmap(int width_, int height_); + Bitmap(std::string); ~Bitmap(); void fill(int r, int g, int b); void setPixel(int x, int y, int r, int g, int b); diff --git a/src/ObjModel.h b/src/ObjModel.h index 63d49ef..02bf16d 100644 --- a/src/ObjModel.h +++ b/src/ObjModel.h @@ -1,11 +1,9 @@ #include"Vertice.h" #include"Bitmap.h" #include -#include #include #include #include -#include struct Triangle { int p[3][3]; diff --git a/src/main.cpp b/src/main.cpp index ca4ff1b..1234964 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,4 @@ #include -#include #include"Render.h" @@ -28,30 +27,12 @@ int main() Render render(width, height); - Bitmap* modelTexture; - - bool textureLoaded = false; - - std::chrono::high_resolution_clock::time_point time1 = std::chrono::high_resolution_clock::now(); - sf::Image image; - textureLoaded = image.loadFromFile("model/modelTexture.png"); - if (textureLoaded) { - modelTexture = new Bitmap(image.getSize().x, image.getSize().y); - const sf::Uint8* pp = image.getPixelsPtr(); - for (int i = 0; i < image.getSize().x * image.getSize().y * 4; i += 4) { - modelTexture->pixels[i + 0] = (unsigned char)pp[i + 0]; - modelTexture->pixels[i + 1] = (unsigned char)pp[i + 1]; - modelTexture->pixels[i + 2] = (unsigned char)pp[i + 2]; - modelTexture->pixels[i + 3] = (unsigned char)pp[i + 3]; - } - } - std::chrono::high_resolution_clock::time_point time2 = std::chrono::high_resolution_clock::now(); - std::chrono::duration modelTextureLoadingTime = std::chrono::duration_cast>(time2 - time1); + Bitmap modelTexture("model/modelTexture.png"); ObjModel model("model/model.obj"); - if (textureLoaded) { - std::cout << "Texture loading time: " << modelTextureLoadingTime.count() << "\n"; + if (modelTexture.isLoaded) { + std::cout << "Texture loading time: " << modelTexture.loadingTime.count() << "\n"; } else { std::cerr << "Couldn't load and/or find texture!\n"; @@ -66,7 +47,7 @@ int main() std::cerr << "Couldn't load and/or find model!\n"; } - if (textureLoaded && model.isLoaded) { + if (modelTexture.isLoaded && model.isLoaded) { while (window.isOpen()) { while (window.pollEvent(event)) @@ -90,7 +71,7 @@ int main() render.fill(255, 255, 255); render.resetZBuf(); - render.renderModel(projectedModel, *modelTexture); + render.renderModel(projectedModel, modelTexture); texture.update((sf::Uint8*)render.pixels); window.draw(sprite); @@ -98,7 +79,5 @@ int main() } } - delete modelTexture; - return 0; } -- cgit v1.2.3