Refactored Timer class and made Texture Atlas load missing texture.

master
aurailus 2018-12-07 13:55:59 -08:00
parent 7ac460ee28
commit 04edfb3601
7 changed files with 30 additions and 20 deletions

View File

@ -16,7 +16,4 @@ link_directories(Libraries/glew/lib)
add_executable(GlProject GlProject/Main.cpp GlProject/engine/graphics/Mesh.cpp GlProject/engine/graphics/Mesh.h GlProject/engine/Entity.cpp GlProject/engine/Entity.h GlProject/engine/graphics/Shader.cpp GlProject/engine/graphics/Shader.h GlProject/engine/Window.cpp GlProject/engine/Window.h GlProject/engine/Camera.cpp GlProject/engine/Camera.h GlProject/engine/graphics/Texture.cpp GlProject/engine/graphics/Texture.h GlProject/mesh/MeshGenerator.cpp GlProject/mesh/MeshGenerator.h GlProject/engine/Timer.cpp GlProject/engine/Timer.h GlProject/blocks/BlockAtlas.cpp GlProject/blocks/BlockAtlas.h GlProject/blocks/BlockDef.cpp GlProject/blocks/BlockDef.h GlProject/mesh/MeshPart.cpp GlProject/mesh/MeshPart.h GlProject/mesh/MeshMod.h GlProject/mesh/Vertex.cpp GlProject/mesh/Vertex.h GlProject/mesh/BlockModel.cpp GlProject/mesh/BlockModel.h GlProject/engine/TextureAtlas.cpp GlProject/engine/TextureAtlas.h)
target_link_libraries(GlProject
${OPENGL_gl_LIBRARY}
glfw
libGLEW.so)
target_link_libraries(GlProject ${OPENGL_gl_LIBRARY} glfw libGLEW.so)

View File

@ -77,6 +77,8 @@ void makeEntities(BlockModel* model) {
}
int main() {
Timer boot("Initialization");
//Window
window = new Window(1366, 768);
window->initialize();
@ -103,6 +105,8 @@ int main() {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
boot.elapsed();
//Game Loop
while (!window->getShouldClose()) {
Timer t("Game Loop");
@ -139,7 +143,7 @@ int main() {
//Finish Drawing
window->swapBuffers();
// t.elapsedInMs();
// t.elapsedMs();
}
return 0;

View File

@ -36,19 +36,19 @@ TextureAtlas::TextureAtlas(const char* directory) {
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
std::cout << "This GPU's max texture size is: " << maxTexSize / 4 << "px^2." << std::endl;
//Using cute_files to open the directory and recursively search for files.
//Using cute_files to open the directory and search for files.
//Filter the files by extension and add all of them to a vector as TextureRefs.
cf_dir_t dir;
cf_dir_open(&dir, (std::string(directory) + std::string("/game")).c_str());
std::list<TextureRef> textureRefs;
//
// //Load Missing Texture
// auto msg = TextureRef();
// strcpy(msg.path, "../Textures/_missing.png");
// strcpy(msg.name, "_missing.png");
// msg.texData = stbi_load(msg.path, &msg.width, &msg.height, &msg.bitDepth, 4);
// textureRefs.push_back(msg);
//Load Missing Texture
auto msg = TextureRef();
strcpy(msg.path, "../Textures/_missing.png");
strcpy(msg.name, "_missing.png");
msg.texData = stbi_load(msg.path, &msg.width, &msg.height, &msg.bitDepth, 4);
textureRefs.push_back(msg);
//Iterate though the files
while (dir.has_next) {
@ -57,7 +57,7 @@ TextureAtlas::TextureAtlas(const char* directory) {
if (!file.is_dir && strcmp(file.ext, ".png") == 0) {
// printf("Processing Texture: %s\n", file.name);
printf("Loading Texture: %s\n", file.name);
auto ref = TextureRef();
strcpy(ref.path, file.path);
@ -138,10 +138,11 @@ TextureAtlas::TextureAtlas(const char* directory) {
Texture* TextureAtlas::getTexture() {
return texture;
}
glm::vec4* TextureAtlas::getUVs(std::string* texture) {
if (textures.count(*texture) == 0) {
std::cout << "Texture '" << *texture << "' Not found in atlas! Terminating." << std::endl;
throw "Texture not found error";
throw std::exception();
}
return &textures.at(*texture);
}

View File

@ -9,7 +9,7 @@ Timer::Timer(const char* name) {
start = std::chrono::high_resolution_clock::now();
}
void Timer::elapsed() {
void Timer::elapsedNs() {
auto finish = std::chrono::high_resolution_clock::now();
long elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count();
@ -17,7 +17,7 @@ void Timer::elapsed() {
printf("%s took %ld ns.\n", this->name, elapsed);
}
void Timer::elapsedInMs() {
void Timer::elapsedMs() {
auto finish = std::chrono::high_resolution_clock::now();
double elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count() / (double)1000000;
@ -25,3 +25,10 @@ void Timer::elapsedInMs() {
printf("%s took %.2f ms.\n", this->name, elapsed);
}
void Timer::elapsed() {
auto finish = std::chrono::high_resolution_clock::now();
double elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count() / (double)1000000 / (double)1000;
printf("%s took %.2f secs.\n", this->name, elapsed);
}

View File

@ -12,8 +12,9 @@ class Timer {
public:
explicit Timer(const char* name);
void elapsed();
void elapsedInMs();
void elapsedNs();
void elapsedMs();
void elapsed(); //Seconds
private:
const char* name;
std::chrono::high_resolution_clock::time_point start;

View File

@ -48,7 +48,7 @@ void MeshGenerator::build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE], BlockM
vertices.shrink_to_fit();
indices.shrink_to_fit();
t.elapsedInMs();
t.elapsedMs();
}
void MeshGenerator::addFaces(int x, int y, int z, vector<float> &vertices, vector<unsigned int> &indices, vector<MeshPart*> meshParts) {

BIN
atlas.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

After

Width:  |  Height:  |  Size: 66 KiB