Advanced C++/Graphics Tutorial 15: ResourceManager, TextureCache!

preview_player
Показать описание

Now we get to implement texture caching! We have a lot more optimizations to learn so that our programs can run as fast as possible, but I want to do a challenge episode soon! So I may wait a bit before teaching you some of the optimizations.

Рекомендации по теме
Комментарии
Автор

For those of you following this tutorial on a Mac, remember to implement the static variable _textureCache in ResourceManager.cpp. Just add this to the top of ResourceManager.cpp:

TextureCache

And you'll be set :)

ll
Автор

Inside of the TextureCache::getTexture
you could just use the operator[] instead, that does the checking for you. So the whole function could be written as:

Texture& std::string& key) {
return m_TextureMap[key] =
}
It makes it a lot shorter.

About the vector of Sprite*, instead you could implement a move constructor:

Sprite::Sprite(Sprite&& sprite) noexcept {
this->m_Texture = std::move(sprite.m_Texture);
this->m_Size = std::move(sprite.m_Size);
this->m_vboID = std::move(sprite.m_vboID);
this->m_VertexData =
}

and then from MainGame, you keep them as regular stack-allocated sprites, but instead you add them to the vector like:

m_Sprites.reserve(2);

Sprite sprite1;
sprite1.init({ -1.f, -1.f, 1.0f, 1.0f },


Sprite sprite2;
sprite2.init({ 0.f, -1.f, 1.0f, 1.0f },



I personally prefer these styles of coding, the second point won't really save you either memory or performance, although it is cheaper just moving a pointer, instead of copying a pointer. Same goes with map<key, val>::operator[], it's an emplace but with added error-checking.

valizeth
Автор

(Note to others: Should proly add this apon exit):
while (!sprites.empty()) delete sprites.back(), sprites.pop_back();
(To prevent memory leaks. Also note that size() is unsigned int)

AlienScribble
Автор

Are there any advantages for using a static class over a namespace, or is it just personal preference? So far I've just been using namespaces where you have used static classes and haven't had any issues.

Anyway, awesome videos! I don't know what I would do without your tutorials. Keep up the good work!

liamwoodward
Автор

The code is crashing at SDL_GL_SwapWindow(_window); in MainGame.cpp it says "Frame Not In Module", when trying to draw from the _sprites vector

EltheMura
Автор

I know this is probably just me, but is anyone else writing out more or less the same code as his since Tut #1 of the 65 to this current one and UNDERSTAND the separate concepts of c++ and basically all of the normal stuff but has trouble using them all separated out into multiple files?
(I think I mainly have issues with the special gl stuff (hbu guys?) -/___ /-

JJCUBER
Автор

Hey Ben! I've just started to follow your tutorials and I must say, they are great! Thank you so much for making this tutorials and helping us to create games.

I've come this far but I encountered a problem in this tutorial in which I can only draw the last sprite in the vector. If I put three sprite in the vector, the last one gets drawn, but the other ones don't. Weird thing is that even the not-drawn ones get the black background, but without the png image. And another weird thing is that, I tried this with 2 sprites, and I used different png images for each one. Still, only the second one gets drawn, first one is still black. And in the console I can read that "Loaded new texture" twice. So it's loading the texture, but it's not getting drawn.

I am running on Mac OSX and because of that I had to use VAO's. This is probably the only thing different from your code but I couldn't find anything wrong with them. 

cemozen
Автор

17:50, I think you got the couts interchanged, because the first time it is going to go inside the if statement and should cout "loaded texture" not "Used cached texture" because we are loading it and for every subsequent sprite it is going to skip the if statement and should cout "Used cached texture" because it is using already loaded texture. And also at 17:43 you say "If you use the same texture we should only get loaded texture once and then we should get used cached texture multiple times", I think you got this mixed also. Correct me if I am wrong.

nsv.
Автор

Shouldn't you use emplace instead of insert? It's faster, because it constructs the object INSIDE the map, and also doesn't need you to make std::pair first.

spades
Автор

If anyone still here, i notice where ben uses std::string it works fine for him whereas i have to include <string> every time, did i miss something somewhere? Id check against his source but the link in description is down

deadsi
Автор

Hey, I keep getting an error when compiling the code, even when I copy/paste your files into mine. Says the following:
"Unhandled exception at 0x6964DB1A (nvoglv32.dll) in ComputerGraphics.exe: Access violation reading location

can't get past this, it's a forced break. What to do? I've read somewhere that its an issue possibly with attribute pointers or the nvidia driver, but nothing really to go on. Thanks in advance!

SeanMaley
Автор

I am getting the error "texturePath: No such file or directory" and
"Failed to load PNG file to buffer." even though the directory worked when I had the single sprite.


here is my directory for the texture ->

greenjd
Автор

I have a problem: The textures don't show up, just black. The log says they load, but they don't show

ichigo_nyanko
Автор

Errr my colours were still going at the same speed even with the extra thousand sprites >:D

BL-dmsz
Автор

for the texturecaching : why not simply index the map by a string ? something like _textureMap[name]. if the map finds the name, it'll spit it out, otherwise it will add the texture automatically to the tree. wouldnt have to do the checks, only the texture initialization.

calecacciatore
Автор

For some reason my glContext is equaling nullptr, so i'm getting our "SDL_GL Context could not be created" error. Any thoughts?

gavinpierce
Автор

For some reason my image when loaded, has an edge showing between the triangles. I downloaded your code and run it as is (except for some includes that my g++ required) and it still gives me that line between the triangles. Do you know what could be causing this and any possible fixes?

If it helps I am running OpenGL Mesa 3.0 and I am running the latest Ubuntu LTS

nRxUs
Автор

When I try to make the ResourceManager class, it says that ResourceManager is a reserved class name. I guess it's native?

Zephemus
Автор

Hey Ben:

For some reason, I get these errors in ResourceManager:

error C2146: syntax error : missing ';' before identifier '_textureCache'    13

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int    13



Here's my code:

#pragma once
#include "TextureCache.h"
#include <string>

using namespace std;

class ResourceManager
{
public:
    static GLTexture getTexture(string texturePath);
    //static GLuint boundTexture;
private:
    static TextureCache // Error is here
};

I've been able to follow along until now. Thanks

Zoku
Автор

I think I fixed it (at least the error went away). I had

#include "ResourceManager.h"
#include <iostream>

in TextureCache.h; I moved them to TextureCache.cpp and it ran.

Zoku