/////////////////////////////////////////////////////////////////////////////// // $Id: Sprite.cxx,v 1.3 2000/01/10 23:30:17 bwmott Exp $ /////////////////////////////////////////////////////////////////////////////// // // Sprite.cxx - Sprite class // // // Bradford W. Mott // Copyright (C) 1994 // November 11,1994 // /////////////////////////////////////////////////////////////////////////////// // $Log: Sprite.cxx,v $ // Revision 1.3 2000/01/10 23:30:17 bwmott // Modified image operations to take the display depth into account // // Revision 1.2 1996/01/06 05:08:14 bwmott // Included string.h and fixed problem with for scope because of new // C++ standard // // Revision 1.1 1995/01/12 02:09:54 bmott // Initial revision // /////////////////////////////////////////////////////////////////////////////// #include #include "Sprite.hxx" #include "UIApplication.hxx" // Raw sprite header typedef struct { unsigned char widthMSB; unsigned char widthLSB; unsigned char heightMSB; unsigned char heightLSB; unsigned char numberOfColors; unsigned char transparentColor; unsigned char lengthOfName; } RawSprite; /////////////////////////////////////////////////////////////////////////////// // Constructor /////////////////////////////////////////////////////////////////////////////// Sprite::Sprite(unsigned char* spriteData) { int t; RawSprite rawSprite; // get the sprite's header memcpy(&rawSprite, spriteData, sizeof(rawSprite)); spriteData += sizeof(rawSprite); // Get the width and height of the sprite myWidth = (((int)rawSprite.widthMSB) << 8) + (int)rawSprite.widthLSB; myHeight = (((int)rawSprite.heightMSB) << 8) + (int)rawSprite.heightLSB; // Build a pixel mapping table unsigned long PixelMapping[256]; unsigned char pal[256][3]; memcpy(pal, spriteData, rawSprite.numberOfColors*3); spriteData += rawSprite.numberOfColors*3; for(t=0;tdisplay(), application->colormap(), &color); PixelMapping[t] = color.pixel; } // Allocate space for the sprite data myData = new unsigned char[myWidth * myHeight * sizeof(long)]; // Create my x image myImage = XCreateImage(application->display(), application->visual(), application->depth(), ZPixmap, 0, (char*)myData, myWidth, myHeight, 8, 0); for(int y = 0; y < myHeight; ++y) { for(int x = 0; x < myWidth; ++x) { unsigned char i; i = *spriteData; spriteData += 1; XPutPixel(myImage, x, y, PixelMapping[i]); } } // Get my name myName = new char[rawSprite.lengthOfName + 1]; memcpy(myName, spriteData, rawSprite.lengthOfName); spriteData += rawSprite.lengthOfName; myName[rawSprite.lengthOfName] = '\0'; } /////////////////////////////////////////////////////////////////////////////// // Destructor /////////////////////////////////////////////////////////////////////////////// Sprite::~Sprite() { // Free memory that holds my name delete[] myName; // Free image data delete[] myData; // Free the XImage structure myImage->data = 0; XDestroyImage(myImage); }