Conceptually it's Perfect. Unfortunately, it seems that Practice would be slow and inefficient. The thought of millions of heap manipulations is rotten.
I suppose we could manage our own
cache of available color objects, so as to minimize heap de/allocations, but that could lead to inefficient use of space, and there would still be the
overhead of management.
Moreover, it's isn't possible to have the best of both worlds:
Code: Select all
IColor color = driver->SColor(r,g,b,a); //stack alloc, faster but uses stack
IColor *color2 = driver->SColor(r,g,b,a); //heap alloc
There is no such thing as return-type overloading, and the stack doesn't allow for runtime polymorphism.
I investigated another trick, but came to the conclusion that it wouldn't work here (convince me otherwise, please):
http://en.wikipedia.org/wiki/Curiously_ ... te_Pattern
I still believe the best marriage of Practice and Abstraction is the use of the video driver.
IVideoDriver* driver = device->getVideoDriver();
u32 color = driver->ARGB(...);
u32 color = driver->RGB(...);
u32 color = driver->BGRA(...);
driver->setR(&color, ....);
etc
Then the only overhead is the use of virtual methods that create u32 stack values; in practice, the programmer will generally never need to worry about what's going on, and in theory, the programmer can still use different devices by converting properly, as one expects:
u32 color2 = driverD3D->RGB(driverGL->getR(color), driverGL->getG(color), driverGL->getB(color));
or
u32 color2 = driverD3D->ARGB(color, EDT_OPENGL);
or
driverD3D->ARGB(&color, EDT_OPENGL);
Let me add that I think this need to convert colors is nonexistant; however, the need to create device-dependent colors as quickly and as transparently as possible is very necessary.
Moreover, neither D3D (from what I know) nor OpenGL support pure colors in various formats, so I think it unnecessary to consider them for now: 32 bits, 8 bits per component (4 bytes per pixel), ARGB is all that matters.