aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateja <mail@matejamaric.com>2021-02-19 17:28:33 +0100
committerMateja <mail@matejamaric.com>2021-02-19 17:28:33 +0100
commit9e7016e23471b7ba67598b5558c7fc7c528f1f2a (patch)
tree693b9a7fe468e8df1f431b3b5c33971f1a996764
parent3b89cf20f583a82a3cd5bdbe04c07630838af5c3 (diff)
downloaderender-9e7016e23471b7ba67598b5558c7fc7c528f1f2a.tar.gz
erender-9e7016e23471b7ba67598b5558c7fc7c528f1f2a.zip
Moved texture loading to `Bitmap` class.
-rw-r--r--src/Bitmap.cpp31
-rw-r--r--src/Bitmap.h9
-rw-r--r--src/ObjModel.h2
-rw-r--r--src/main.cpp31
4 files changed, 44 insertions, 29 deletions
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<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() {
- 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<iostream>
+#include<string>
+#include<chrono>
+
+#include<SFML/Graphics.hpp>
+
class Bitmap
{
public:
int width, height;
unsigned char *pixels;
+ sf::Image* image;
+ bool isLoaded;
+ std::chrono::duration<double> 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<vector>
-#include<string>
#include<sstream>
#include<fstream>
#include<exception>
-#include<chrono>
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<SFML/Window.hpp>
-#include<SFML/Graphics.hpp>
#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<double> modelTextureLoadingTime = std::chrono::duration_cast<std::chrono::duration<double>>(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;
}