Gestionnaire de ressources
Bien le bonjour,
Je travaille sur un moteur de rendu en OpenGL et j'arrive maintenant sur la gestion des ressources. J'ai vu deux façon de faire lors de la création d'une ressource. La première, classique, consiste à retourner un pointeur sur la ressource créée
Code:
1 2 3 4 5 6 7 8 9 10 11
|
class Renderer
{
public:
VertexBuffer* CreateVertexBuffer(...);
IndexBuffer* CreateIndexBuffer(...);
private:
std::vector<VertexBuffer*> m_vVertexBuffers;
std::vector<IndexBuffer*> m_vIndexBuffers;
}; |
La deuxième consiste à retourner un handle sur la ressource créée. L'utilisateur doit ensuite interroger le gestionnaire de ressources avec cet handle pour obtenir un pointeur sur la ressource.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class Renderer
{
public:
int CreateVertexBuffer(...);
int CreateIndexBuffer(...);
VertexBuffer* GetVertexBufferByIndex(int Index);
IndexBuffer* GetIndexBufferByIndex(int Index);
private:
std::vector<VertexBuffer*> m_vVertexBuffers;
std::vector<IndexBuffer*> m_vIndexBuffers;
}; |
Dans le deuxième cas je cite la documentation du moteur dans lequel je vu l'implémentation
"The renderer provides the ability for other classes to create and reference the various Direct3D 11 objects. The primary objects that other non-rendering system classes will be interested in are the memory resource classes, which represent buffers and textures. The renderer allows creation of these objects, but retains ownership of them and returns only an ID to the caller. That ID is then used by the caller any time they want to reference a particular resource.
This pattern is also followed for non-resource classes as well, such as shader objects, samplers, input layouts, and so on. Instead of making the other classes work with direct references to D3D11 objects, they are instead referenced by IDs. This allows the renderer to keep control over how and when the D3D11 objects are used, and minimizes the direct knowledge needed by the application to use the engine."
Quel pattern utilisez-vous le plus et pourquoi ?
Kromagg