Show kimagecardinaldirection.cpp syntax highlighted
/*
Copyright (C) 2003, 2004, 2005 by Luca Cappa
Written by Luca Cappa groton@users.sourceforge.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "cssysdef.h"
#include "csutil/sysfunc.h"
#include "csutil/cscolor.h"
#include "csutil/cmdhelp.h"
#include "csutil/cspmeter.h"
#include "csutil/csstring.h"
#include "csutil/scfstr.h"
#include "cstool/csview.h"
#include "cstool/initapp.h"
#include "cstool/collider.h"
#include "iutil/vfs.h"
#include "iutil/eventq.h"
#include "iutil/event.h"
#include "iutil/objreg.h"
#include "iutil/csinput.h"
#include "iutil/virtclk.h"
#include "iutil/plugin.h"
#include "iutil/string.h"
#include "iutil/document.h"
#include "iengine/sector.h"
#include "iengine/engine.h"
#include "iengine/camera.h"
#include "iengine/light.h"
#include "iengine/texture.h"
#include "iengine/mesh.h"
#include "iengine/movable.h"
#include "iengine/material.h"
#include "imesh/thing.h"
#include "imesh/object.h"
#include "imesh/sprite3d.h"
#include "imesh/ball.h"
#include "ivideo/graph3d.h"
#include "ivideo/graph2d.h"
#include "ivideo/natwin.h"
#include "ivideo/txtmgr.h"
#include "ivideo/texture.h"
#include "ivideo/material.h"
#include "ivideo/fontserv.h"
#include "igraphic/imageio.h"
#include "imap/loader.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/conout.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/collider.h"
#include "csgeom/quaterni.h"
#include "csgeom/transfrm.h"
#include "csgeom/math3d_d.h"
#include "csgeom/math3d.h"
#include "igeom/polymesh.h"
#include "igeom/objmodel.h"
#include "iaws/aws.h"
#include "iaws/awscnvs.h"
#include "csgfx/memimage.h"
#include "csgfx/rgbpixel.h"
#include "korientation.h"
#include "kblock.h"
#include "kmap.h"
#include "kblockid.h"
#include "kflags.h"
#include "kappstate.h"
#include "ikxmlwriter.h"
#include "ikxmlreader.h"
#include "ske.h"
//Include for this file.
#include "kimagecardinaldirection.h"
/**
Fill a csRefArray with an image rotated in the 4 cardinal directions.
*/
KImageCardinalDirection::KImageCardinalDirection (char* p_fileName)
{
csRef<iLoader> l_loader = CS_QUERY_REGISTRY (g_objReg, iLoader);
csRef<iImage> l_im = l_loader->LoadImage (p_fileName);
m_images.Push (l_im);
l_im = l_loader->LoadImage (p_fileName);
l_im = Rotate (l_im, KOrientation::EAST);
m_images.Push (l_im);
l_im = l_loader->LoadImage (p_fileName);
l_im = Rotate (l_im, KOrientation::SOUTH);
m_images.Push (l_im);
l_im = l_loader->LoadImage (p_fileName);
l_im = Rotate (l_im, KOrientation::WEST);
m_images.Push (l_im);
}
/**
* Rotate an image in the given direction. the p_image passed in the argument is supposed
* to be NORTH oriented.
*/
csPtr<iImage> KImageCardinalDirection::Rotate (iImage* p_image, const KOrientation& p_or)
{
unsigned char* l_dst = 0;
unsigned int l_height = p_image->GetHeight ();
unsigned int l_width = p_image->GetWidth ();
if (p_or == KOrientation::NORTH)
{
size_t lBytes = l_height * l_width * 4;
l_dst = new unsigned char [lBytes];
memcpy (l_dst, p_image->GetImageData (), lBytes);
}//if
else if (p_or == KOrientation::WEST)
{
size_t lBytes = l_height * l_width * 4;
l_dst = new unsigned char [l_width * l_height * 4];
unsigned char* l_src = (unsigned char*)p_image->GetImageData ();
unsigned char* l_tmpDst;
size_t x, y;
for (x = 0; x < l_width; x++)
{
l_tmpDst = l_dst + (l_height - 1) * (4 * l_width) + (4 * x);
for (y = 0; y < l_height; y++)
{
/*l_tmpDst[0] = l_src[0];
l_tmpDst[1] = l_src[1];
l_tmpDst[2] = l_src[2];
l_tmpDst[3] = l_src[3];*/
*(__int32*)l_tmpDst = *(__int32*)l_src;
l_tmpDst -= l_width * 4;
l_src += 4;
}//for
}//for
}//else
else if (p_or == KOrientation::SOUTH)
{
size_t lByte = l_width * l_height * 4;
l_dst = new unsigned char [lByte];
unsigned char* l_tmpDst = l_dst;
unsigned char* l_src = (unsigned char*)p_image->GetImageData ();
//
//flip vertically & horizzontally
l_tmpDst += (l_height - 1) * (4 * l_width);
unsigned char* l_tmpBuffer = new unsigned char [l_width * 4];
size_t i;
for (i = 0; i < l_height; i++)
{
memcpy (l_tmpBuffer, l_src, l_width * 4);
size_t j;
for (j = 0; j < l_width>>1; j++)
{
unsigned long* l_k = (unsigned long*)l_tmpBuffer;
unsigned long l_dword = l_k[j];
l_k[j] = l_k[l_width - 1 - j];
l_k[l_width - 1 - j] = l_dword;
}//for
memcpy (l_tmpDst, l_tmpBuffer, l_width * 4);
l_src += 4 * l_width;
l_tmpDst -= 4 * l_width;
}//for
delete[] l_tmpBuffer;
}//else
else if (p_or == KOrientation::EAST)
{
l_dst = new unsigned char [l_width * l_height * 4];
unsigned char* l_tmpDst = l_dst;
unsigned char* l_src = (unsigned char*)p_image->GetImageData ();
size_t x, y;
for (x = l_width; x > 0;)
{
x--;
l_tmpDst = l_dst + x * 4;
for (y = 0; y < l_height; y++)
{
/*l_tmpDst[0] = l_src[0];
l_tmpDst[1] = l_src[1];
l_tmpDst[2] = l_src[2];
l_tmpDst[3] = l_src[3];*/
*(__int32*)l_tmpDst = *(__int32*)l_src;
l_tmpDst += l_width * 4;
l_src += 4;
}//for
}//for
}//else
else
CS_ASSERT (false);
//Contruct the image.
csRef<csImageMemory> l_retImage;
l_retImage.AttachNew (new csImageMemory (l_width, l_height, l_dst,
true, CS_IMGFMT_TRUECOLOR));
return csPtr<iImage> (l_retImage);
}
iImage* KImageCardinalDirection::operator [] (KOrientation const& p_o)
{
if (p_o == KOrientation::NORTH)
return m_images[0];
else if (p_o == KOrientation::EAST)
return m_images[1];
else if (p_o == KOrientation::SOUTH)
return m_images[2];
else if (p_o == KOrientation::WEST)
return m_images[3];
else
return 0;
}
See more files for this project here