Skip to content

Instantly share code, notes, and snippets.

@miguelmartin75
Last active January 2, 2016 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmartin75/8351147 to your computer and use it in GitHub Desktop.
Save miguelmartin75/8351147 to your computer and use it in GitHub Desktop.
A design for my library rojo (the rendering aspect of it).
struct ogl_backend
{
typedef unsigned texture_handle_type;
void load(texture_handle_type& texture, const Image& image)
{
std::cout << "loading, " << image.id << '\n';
}
void destroy(texture_handle_type& texture)
{
std::cout << "destroying texture\n";
}
};
template <class GraphicsBackend>
struct texture_gpu_resource
{
typedef GraphicsBackend graphics_backend;
typedef typename GraphicsBackend::texture_handle_type texture_handle;
texture_gpu_resource(graphics_backend& backend)
: _backend{backend}
{
}
~texture_gpu_resource()
{
_backend.destroy(_handle);
}
void load(const Image& image)
{
_backend.load(_handle, image);
}
const texture_handle& handle() const
{
return _handle;
}
private:
graphics_backend& _backend;
texture_handle _handle;
};
template <typename GraphicBackend>
class graphics_device
{
typedef graphics_device<GraphicBackend> this_type;
public:
typedef texture_gpu_resource<GraphicBackend> texture;
template <typename... Args>
texture createTexture(Args&&... args)
{
return texture{_backend, args...};
}
template <typename Resource, typename... Args>
Resource create(Args&&... args)
{
return Resource{_backend, args...};
}
private:
GraphicBackend _backend;
};
class ogl_graphics_device : public graphics_device<ogl_backend>
{
public:
enum class feature
{
texturing
};
void enableFeature(feature f)
{
std::cout << "enabling feature... " << (int)f << '\n';
}
};
// or...
// typedef graphics_device<ogl_backend> ogl_graphics_device
int main()
{
ogl_graphics_device device;
device.enableFeature(ogl_graphics_device::feature::texturing);
auto texture = device.create<decltype(device)::texture>();
texture.load({"hello"});
return 0;
}
/*
Backend
- defines the backend of the graphics interface
Interface
- the interface is defined by the individual objects
that use the backend, e.g. texture, etc.
Device
- the device is the individual device that connects
the backend to the interface
ogl_graphics_device device;
device.enableFeature(ogl_graphics_device::feature::texturing);
// etc.
auto texture = device.create<ogl_graphics_device::texture>();
*/
/*
Expected output:
enabling feature... 0
loading, hello
destroying texture
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment