Short intro:
I am developing a template based library for easily creating GUI.
Currently, basics functionalities (window drawing and very simple event management) works correctly, but code is not optimized yet.
Templates should allow me to only have to implement 2 classes for switching from a library to another:
_ screen<T1,T2>
_ bitmap<T1,T2>
Here, no problem.
I have used SDL to implement those classes for tests.
Everything work perfectly if I use only 24bits images (loaded from files).
But, when I use a 8bit one (xpaint automatically choose the best format for images, not as mspaint, this is how I discovered this problem), nothing is drawn!
If I mix 8bits and 24bits images, the same…
I don’t understand from where this problem come. I have made a try to convert the 8bit image in the “bitmap::loadFile(std::string)” method with SDL_ConvertSurface, but it didn’t change anything (and so I have removed the try).
The code is really simple, as my library call SDL for everything that need to draw. I often only have to interface my API with SDL one, which is done by bitmap class (in fact, by it’s descendant)
In the sdl implementation, the first template argument is SDL_Surface, the second one is sdl_bitmap (the class which encapsulate SDL_Surface).
Class bitmap is here to give an abstract base model and a few methods to the class user will have to implement. (in my test code, I have named it sdl_bitmap.)
Here is the code:
template <typename className,typename object>
bool __bitmap<className,object>::draw(std::weak_ptr<className> &target,int16_t const sx, int16_t const sy,int16_t const w, int16_t const h, int16_t const dx, int16_t const dy)
{
std::weak_ptr<className> weakSource (m_surface);
return blitSurface(weakSource, target,dx,dy,!w?getW():w,!h?getH():h,sx,sy);
}
template <typename className,typename object>
bool __bitmap<className,object>::copy(std::weak_ptr<className> &source,int16_t sx, int16_t sy,int16_t w, int16_t h, int16_t dx, int16_t dy)
{
std::weak_ptr<className> weakDest(m_surface);
return blitSurface(source, weakDest,sx,sy,!w?getW():w,!h?getH():h,dx,dy);
}
For the sprite:
The first goal of the image class is to encapsulate the class bitmap, providing higher level methods.
Another thing is the “image::clone(image&)” method. It don’t really clone, it just make “this->m_texture” having same characteristics than the given object. There is no copy of image content.
template<typename className,typename object>
bool __sprite<className,object>::draw(std::weak_ptr<className> &target)
{
//first verify if image is set
if(!m_sprite.isValid())
return false;
//clear the sprite BEFORE changing the screen
if(!clear())
return false;
m_target=target.lock();
if(!m_target.use_count())
return false;
m_weakTarget=m_target;
m_shadowX=m_spriteX;
m_shadowY=m_spriteY;
//if the shadow does not exist or is not of the same size as the sprite, create one of the same size
if(!m_shadow.isValid())
m_shadow.clone(m_sprite);
if(m_shadow.getH()!=m_sprite.getH() || m_shadow.getW()!=m_sprite.getW())
m_shadow.clone(m_sprite);
if(!m_shadow.copy(m_weakTarget,m_shadowX,m_shadowY))
return false;
return (m_isDraw=m_sprite.draw(m_weakTarget,m_spriteX,m_spriteY));
}
PS:
If this portion of code is not enough, the full code is here: http://svn.gna.org/svn/cremone/trunk
PPS:
I know that many names were stupidly chosen, and I have corrected most of it (with many other improvements) in my working directory. But since my repo on gna.org don’t work for a reason I don’t know, I can’t commit. Please forgive me for crap code…