1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-25 21:38:59 +02:00
- moved PutPixel templates into header to allow inlining
This commit is contained in:
Ivan Savenko 2012-05-15 08:47:11 +00:00
parent 90ff99d4eb
commit 8de71c2b39
4 changed files with 191 additions and 213 deletions

View File

@ -618,7 +618,7 @@ void CTerrainRect::showPath(const SDL_Rect * extRect, SDL_Surface * to)
CDefEssential * arrows = graphics->heroMoveArrows;
int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
if (x<0 || y<0 || x>pos.w || y>pos.h)
if (x< -32 || y< -32 || x>pos.w || y>pos.h)
continue;
int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w),
hvy = (y+arrows->ourImages[pn].bitmap->h)-(pos.y+pos.h);
@ -1456,7 +1456,6 @@ void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
const CGHeroInstance *h = curHero(); //selected hero
const CGTownInstance *t = curTown(); //selected town
switch(k)
{
case SDLK_g:
@ -1569,34 +1568,24 @@ void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
//numpad arrow
if(CGuiHandler::isArrowKey(SDLKey(k)))
{
switch(k)
{
case SDLK_UP:
Dir = UP;
break;
case SDLK_LEFT:
Dir = LEFT;
break;
case SDLK_RIGHT:
Dir = RIGHT;
break;
case SDLK_DOWN:
Dir = DOWN;
break;
}
k = CGuiHandler::arrowToNum(SDLKey(k));
}
if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
break;
k -= SDLK_KP0 + 1;
if(k < 0 || k > 8 || key.state != SDL_PRESSED)
if(k < 0 || k > 8)
return;
if(!h)
int3 dir = directions[k];
if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
{
Dir = (dir.x<0 ? LEFT : 0) |
(dir.x>0 ? RIGHT : 0) |
(dir.y<0 ? UP : 0) |
(dir.y>0 ? DOWN : 0) ;
break;
}
if(!h || key.state != SDL_PRESSED)
break;
if(k == 4)
@ -1605,8 +1594,6 @@ void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
return;
}
int3 dir = directions[k];
CGPath &path = LOCPLINT->paths[h];
terrain.currentPath = &path;
if(!LOCPLINT->cb->getPath2(h->getPosition(false) + dir, path))
@ -1615,10 +1602,11 @@ void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
return;
}
if (path.nodes.size() > 2)
updateMoveHero(h);
else
if(!path.nodes[0].turns)
{
LOCPLINT->moveHero(h, path);
}
}
return;

View File

@ -23,176 +23,6 @@ SDL_Color Colors::createColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a /*= 0*/)
return temp;
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
{
PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
{
PutColor(ptr, Color.r, Color.g, Color.b);
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
{
switch (A)
{
case 255:
ptr += bpp * incrementPtr;
return;
case 0:
PutColor(ptr, R, G, B);
return;
case 128: // optimized
PutColor(ptr, ((Uint16)R + (Uint16)ptr[2]) >> 1,
((Uint16)G + (Uint16)ptr[1]) >> 1,
((Uint16)B + (Uint16)ptr[0]) >> 1);
return;
default:
PutColor(ptr, R, G, B, A);
return;
}
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
{
PutColor(ptr, ((((Uint32)ptr[2]-(Uint32)R)*(Uint32)A) >> 8 ) + (Uint32)R,
((((Uint32)ptr[1]-(Uint32)G)*(Uint32)A) >> 8 ) + (Uint32)G,
((((Uint32)ptr[0]-(Uint32)B)*(Uint32)A) >> 8 ) + (Uint32)B);
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
{
if(incrementPtr == 0)
{
ptr[0] = B;
ptr[1] = G;
ptr[2] = R;
}
else if(incrementPtr == 1)
{
*ptr++ = B;
*ptr++ = G;
*ptr++ = R;
if(bpp == 4)
*ptr++ = 0;
}
else if(incrementPtr == -1)
{
if(bpp == 4)
*(--ptr) = 0;
*(--ptr) = R;
*(--ptr) = G;
*(--ptr) = B;
}
else
{
assert(0);
}
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
{
Uint32 pixel = ((Uint32)Color.b << 0 ) + ((Uint32)Color.g << 8) + ((Uint32)Color.r << 16);
for (size_t i=0; i<count; i++)
{
memcpy(ptr, &pixel, bpp);
if(incrementPtr == -1)
ptr -= bpp;
if(incrementPtr == 1)
ptr += bpp;
}
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
{
if(incrementPtr == -1)
ptr -= 2;
Uint16 * const px = (Uint16*)ptr;
*px = (B>>3) + ((G>>2) << 5) + ((R>>3) << 11); //drop least significant bits of 24 bpp encoded color
if(incrementPtr == 1)
ptr += 2; //bpp
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
{
switch (A)
{
case 255:
ptr += 2 * incrementPtr;
return;
case 0:
PutColor(ptr, R, G, B);
return;
default:
PutColor(ptr, R, G, B, A);
return;
}
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
{
const int rbit = 5, gbit = 6, bbit = 5; //bits per color
const int rmask = 0xF800, gmask = 0x7E0, bmask = 0x1F;
const int rshift = 11, gshift = 5, bshift = 0;
const Uint8 r5 = (*((Uint16 *)ptr) & rmask) >> rshift,
b5 = (*((Uint16 *)ptr) & bmask) >> bshift,
g5 = (*((Uint16 *)ptr) & gmask) >> gshift;
const Uint32 r8 = (r5 << (8 - rbit)) | (r5 >> (2*rbit - 8)),
g8 = (g5 << (8 - gbit)) | (g5 >> (2*gbit - 8)),
b8 = (b5 << (8 - bbit)) | (b5 >> (2*bbit - 8));
PutColor(ptr,
(((r8-R)*A) >> 8) + R,
(((g8-G)*A) >> 8) + G,
(((b8-B)*A) >> 8) + B);
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
{
PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
{
PutColor(ptr, Color.r, Color.g, Color.b);
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
{
//drop least significant bits of 24 bpp encoded color
Uint16 pixel = (Color.b>>3) + ((Color.g>>2) << 5) + ((Color.r>>3) << 11);
for (size_t i=0; i<count; i++)
{
memcpy(ptr, &pixel, 2);
if(incrementPtr == -1)
ptr -= 2;
if(incrementPtr == 1)
ptr += 2;
}
}
SDL_Surface * CSDL_Ext::newSurface(int w, int h, SDL_Surface * mod) //creates new surface, with flags/format same as in surface given
{
return SDL_CreateRGBSurface(mod->flags,w,h,mod->format->BitsPerPixel,mod->format->Rmask,mod->format->Gmask,mod->format->Bmask,mod->format->Amask);
@ -1317,14 +1147,3 @@ std::string CSDL_Ext::trimToFit(std::string text, int widthLimit, EFonts font)
}
SDL_Surface * CSDL_Ext::std32bppSurface = NULL;
//instantiation of templates used in CAnimation and CCreatureAnimation, required for correct linking
template struct ColorPutter<2,-1>;
template struct ColorPutter<3,-1>;
template struct ColorPutter<4,-1>;
template struct ColorPutter<2, 0>;
template struct ColorPutter<3, 0>;
template struct ColorPutter<4, 0>;
template struct ColorPutter<2, 1>;
template struct ColorPutter<3, 1>;
template struct ColorPutter<4, 1>;

View File

@ -21,7 +21,7 @@
#ifdef _MSC_VER
#define STRONG_INLINE __forceinline
#elif __GNUC__
#define STRONG_INLINE __attribute__((always_inline))
#define STRONG_INLINE inline __attribute__((always_inline))
#else
#define STRONG_INLINE inline
#endif
@ -189,3 +189,174 @@ namespace CSDL_Ext
std::string trimToFit(std::string text, int widthLimit, EFonts font);
};
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
{
PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
{
PutColor(ptr, Color.r, Color.g, Color.b);
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
{
switch (A)
{
case 255:
ptr += bpp * incrementPtr;
return;
case 0:
PutColor(ptr, R, G, B);
return;
case 128: // optimized
PutColor(ptr, ((Uint16)R + (Uint16)ptr[2]) >> 1,
((Uint16)G + (Uint16)ptr[1]) >> 1,
((Uint16)B + (Uint16)ptr[0]) >> 1);
return;
default:
PutColor(ptr, R, G, B, A);
return;
}
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
{
PutColor(ptr, ((((Uint32)ptr[2]-(Uint32)R)*(Uint32)A) >> 8 ) + (Uint32)R,
((((Uint32)ptr[1]-(Uint32)G)*(Uint32)A) >> 8 ) + (Uint32)G,
((((Uint32)ptr[0]-(Uint32)B)*(Uint32)A) >> 8 ) + (Uint32)B);
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
{
if(incrementPtr == 0)
{
ptr[0] = B;
ptr[1] = G;
ptr[2] = R;
}
else if(incrementPtr == 1)
{
*ptr++ = B;
*ptr++ = G;
*ptr++ = R;
if(bpp == 4)
*ptr++ = 0;
}
else if(incrementPtr == -1)
{
if(bpp == 4)
*(--ptr) = 0;
*(--ptr) = R;
*(--ptr) = G;
*(--ptr) = B;
}
else
{
assert(0);
}
}
template<int bpp, int incrementPtr>
STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
{
Uint32 pixel = ((Uint32)Color.b << 0 ) + ((Uint32)Color.g << 8) + ((Uint32)Color.r << 16);
for (size_t i=0; i<count; i++)
{
memcpy(ptr, &pixel, bpp);
if(incrementPtr == -1)
ptr -= bpp;
if(incrementPtr == 1)
ptr += bpp;
}
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
{
if(incrementPtr == -1)
ptr -= 2;
Uint16 * const px = (Uint16*)ptr;
*px = (B>>3) + ((G>>2) << 5) + ((R>>3) << 11); //drop least significant bits of 24 bpp encoded color
if(incrementPtr == 1)
ptr += 2; //bpp
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
{
switch (A)
{
case 255:
ptr += 2 * incrementPtr;
return;
case 0:
PutColor(ptr, R, G, B);
return;
default:
PutColor(ptr, R, G, B, A);
return;
}
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
{
const int rbit = 5, gbit = 6, bbit = 5; //bits per color
const int rmask = 0xF800, gmask = 0x7E0, bmask = 0x1F;
const int rshift = 11, gshift = 5, bshift = 0;
const Uint8 r5 = (*((Uint16 *)ptr) & rmask) >> rshift,
b5 = (*((Uint16 *)ptr) & bmask) >> bshift,
g5 = (*((Uint16 *)ptr) & gmask) >> gshift;
const Uint32 r8 = (r5 << (8 - rbit)) | (r5 >> (2*rbit - 8)),
g8 = (g5 << (8 - gbit)) | (g5 >> (2*gbit - 8)),
b8 = (b5 << (8 - bbit)) | (b5 >> (2*bbit - 8));
PutColor(ptr,
(((r8-R)*A) >> 8) + R,
(((g8-G)*A) >> 8) + G,
(((b8-B)*A) >> 8) + B);
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
{
PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
{
PutColor(ptr, Color.r, Color.g, Color.b);
}
template <int incrementPtr>
STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
{
//drop least significant bits of 24 bpp encoded color
Uint16 pixel = (Color.b>>3) + ((Color.g>>2) << 5) + ((Color.r>>3) << 11);
for (size_t i=0; i<count; i++)
{
memcpy(ptr, &pixel, 2);
if(incrementPtr == -1)
ptr -= 2;
if(incrementPtr == 1)
ptr += 2;
}
}

View File

@ -5089,7 +5089,7 @@ bool CGameHandler::castSpell(const CGHeroInstance *h, int spellID, const int3 &p
iw.text.addTxt(MetaString::GENERAL_TXT, 336); //%s tried to summon a boat, but failed.
iw.text.addReplacement(h->name);
sendAndApply(&iw);
return true; //TODO? or should it be false? request was correct and realized, but spell failed...
break;
}
//try to find unoccupied boat to summon
@ -5151,7 +5151,7 @@ bool CGameHandler::castSpell(const CGHeroInstance *h, int spellID, const int3 &p
iw.text.addTxt(MetaString::GENERAL_TXT, 337); //%s tried to scuttle the boat, but failed
iw.text.addReplacement(h->name);
sendAndApply(&iw);
return true; //TODO? or should it be false? request was correct and realized, but spell failed...
break;
}
if(!gs->map->isInTheMap(pos))
COMPLAIN_RET("Invalid dst tile for scuttle!");