回 帖 发 新 帖 刷新版面

主题:水波特效

水波特效,watcom c实现的(仅能在98下运行),其实算法是别人的(www.imagic3d.com),我只是写了个dos下的图形库罢了(还没写完),要的人请[email]woshihanjin@msn.com[/email]

[img]http://www.wodutom.com/coolwindows/water.JPG[/img]

回复列表 (共13个回复)

沙发

贴一下源代码吧!
/***************water.cpp****************/

#include <i86.h>
#include <conio.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>

#include "x\xKey.hpp"
#include "x\xTimer.hpp"
#include "x\xSvga.hpp"
#include "x\xGIO.hpp"

int _w=800,_h=600,_bpp=8;
short*  buf1;
short*  buf2;
cBitmap bmpa,bmpb;

void Ripple()
{
        for(int i=_w;i<_w*_h-_w;i++)
        {
                buf2[i]=(buf1[i-1]+buf1[i+1]+buf1[i-_w]+buf1[i+_w]>>1)-buf2[i];
                buf2[i]-=buf2[i]>>6;
        }
        short *p=buf1;
        buf1=buf2;
        buf2=p;
}

void Render()
{
        int xoff,yoff;
        int k=_w;
        char *p=(char *) bmpa.GetBuffer(0,1);
        for(int i=1;i<_h-1;i++)
                for(int j=0;j<_w;j++,p+=bmpa.Bpp(),k++)
                {
                        xoff=buf1[k-1]-buf1[k+1];
                        yoff=buf1[k-_w]-buf1[k+_w];
                        if((i+yoff)<0) continue;
                        if((i+yoff)>_h) continue;
                        if((j+xoff)<0) continue;
                        if((j+xoff)>_w) continue;
                        bmpa.Set(p,bmpb.GetBuffer(j+xoff,i+yoff));
                }
}

void DropStone(int x,int y,int s,int w)
{
        if(x+s>_w||y+s>_h||x-x<0||y-s<0) return;
        for(int posx=x-s;posx<x+s;posx++)
                for(int posy=y-s;posy<y+s;posy++)
                        if((posx-x)*(posx-x)+(posy-y)*(posy-y)<s*s)
                                buf1[_w*posy+posx]=-w;
}

void main()
{

        cFPS    fps;
        cBitmap bmpbg;
        cSVGA* svga=cSVGA::Instance();
        cGIO    gio;

        gio.Load(bmpbg,"1.pcx",_bpp);
        svga->SetMode(_w,_h,_bpp,cSVGA::_c_linear);
        svga->SetPal(bmpbg.GetPalette());
        svga->SetFrameBuffer(bmpb);
        bmpa.Create(svga->GetDirectBuffer(),_w,_h,_bpp);

        cBitmap_Effect::xCopy(bmpb,bmpbg);

        buf1=new short[_w*_h];
        buf2=new short[_w*_h];
        for(int i=0;i<_w*_h;buf1[i++]=buf2[i]=0) ;

        long c=0x80;

        fps.Start();
        while(cKey::Asc()!=27)
        {
                Ripple();
                Render();
                if(c++&0x80) DropStone(rand()%_w,rand()%_h,rand()%32,rand()%128);
                fps.Refresh();
        };
        fps.Stop();

        delete[] buf1;
        delete[] buf2;

        svga->CloseMode();
        delete svga;
        cout<<fps<<endl;
}

板凳

#ifndef ___xKey
#define ___xKey

class cKey
{
        private:
                static  unsigned char*  const  p_first;
                static  unsigned char*  const  p_last;
                static  unsigned short* const  p_function;   
        public:
                static  void    Clear(){  *p_first=*p_last; }
                static  bool    isEmpty(){  return *p_first==*p_last; }
                static  bool    isFull(){  return *p_first==30?*p_first==(*p_last)-30:*p_first==(*p_last)+2;    }
                static  char    KeysInBuffer(){  return (*p_last)+(*p_last<*p_first?32:0)-(*p_first)>>1;        }
                static  void    SetFunction(unsigned short s){ *p_function=((*p_function)&0xff00)+s;      }
                static  void    ReplaceFunction(unsigned short s){      (*p_function)|=s;      }
                static  unsigned short  Function(){      return *p_function;      }
                static  unsigned char   FunctionState(){        return (*p_function)&0xff;       }
                static  unsigned char   FunctionAction(){        return (*p_function)>>8;       }
                static  unsigned short  Key(){      return *(unsigned short *)(*p_last!=30?0x00400+(*p_last)-2:0x0043c);      }
                static  unsigned char   Scan(){        return Key()>>8;       }
                static  unsigned char   Asc(){        return Key()&0xff;       }
                static  void    Pause(){        while(isEmpty());       }
                static  void    PAUSE()
                {
                        Clear();
                        while(isEmpty());
                }
};
static  unsigned char*  const  cKey::p_first=(unsigned char*)(0x0041A);
static  unsigned char*  const  cKey::p_last=(unsigned char*)(0x0041C);
static  unsigned short* const  cKey::p_function=(unsigned short*)(0x00417);

typedef unsigned short KEY;
typedef unsigned char hKEY;

const KEY _cKey_rShift=0x0001;
const KEY _cKey_lShift=0x0002;
const KEY _cKey_Ctrl=0x0004;
const KEY _cKey_Alt=0x0008;
const KEY _cKey_sScrollLock=0x0010;
const KEY _cKey_sNumLock=0x0020;
const KEY _cKey_sCapsLock=0x0040;
const KEY _cKey_sInsert=0x0080;
const KEY _cKey_pCtrl=0x0100;
const KEY _cKey_pAlt=0x0200;
const KEY _cKey_pScrollLock=0x1000;
const KEY _cKey_pNumLock=0x2000;
const KEY _cKey_pCapsLock=0x4000;
const KEY _cKey_pInsert=0x8000;

const hKEY _cKey_rShift_Act=0;
const hKEY _cKey_rShift_Sta=0x01;
const hKEY _cKey_lShift_Act=0;
const hKEY _cKey_lShift_Sta=0x02;
const hKEY _cKey_rCtrl_Act=0;
const hKEY _cKey_rCtrl_Sta=0x04;
const hKEY _cKey_lCtrl_Act=0x01;
const hKEY _cKey_lCtrl_Sta=0x04;
const hKEY _cKey_rAlt_Act=0;
const hKEY _cKey_rAlt_Sta=0x08;
const hKEY _cKey_lAlt_Act=0x02;
const hKEY _cKey_lAlt_Sta=0x08;
const hKEY _cKey_ScrollLock_Act=0x10;
const hKEY _cKey_NumLock_Act=0x20;
const hKEY _cKey_CapsLock_Act=0x40;
const hKEY _cKey_Insert_Act=0x80;
const hKEY _cKey_ScrollLock_Sta=0x10;
const hKEY _cKey_NumLock_Sta=0x20;
const hKEY _cKey_CapsLock_Sta=0x40;
const hKEY _cKey_Insert_Sta=0x80;

const KEY _cKey_Up=0x48E0;
const KEY _cKey_Left=0x4BE0;
const KEY _cKey_Right=0x4DE0;
const KEY _cKey_Down=0x50E0;
const KEY _cKey_Home=0x47E0;
const KEY _cKey_End=0x4FE0;
const KEY _cKey_Pgup=0x49E0;
const KEY _cKey_Pgdn=0x51E0;
const KEY _cKey_Insert=0x52E0;
const KEY _cKey_Delete=0x53E0;
const KEY _cKey_F1=0x3B00;
const KEY _cKey_F2=0x3C00;
const KEY _cKey_F3=0x3D00;
const KEY _cKey_F4=0x3E00;
const KEY _cKey_F5=0x3F00;
const KEY _cKey_F6=0x4000;
const KEY _cKey_F7=0x4100;
const KEY _cKey_F8=0x4200;
const KEY _cKey_F9=0x4300;
const KEY _cKey_F10=0x4400;
const KEY _cKey_F11=0x8500;
const KEY _cKey_F12=0x8600;

const KEY _cKey_Esc=0x011b;
const KEY _cKey_Enter=0x1C0D;
const KEY _cKey_Tab=0x0F09;

const KEY _cKey_1=0x0231;
const KEY _cKey_2=0x0332;
const KEY _cKey_3=0x0433;
const KEY _cKey_4=0x0534;
const KEY _cKey_5=0x0635;
const KEY _cKey_6=0x0736;
const KEY _cKey_7=0x0837;
const KEY _cKey_8=0x0938;
const KEY _cKey_9=0x0A39;
const KEY _cKey_0=0x0B30;
const KEY _cKey_Dot=0x342E;
const KEY _cKey_Div=0x352F;
const KEY _cKey_Mul=0x092A;
const KEY _cKey_Sub=0x0C2D;
const KEY _cKey_Add=0x0D2B;

const KEY _cSKey_Up=0x4800;
const KEY _cSKey_Left=0x4B00;
const KEY _cSKey_Right=0x4D00;
const KEY _cSKey_Down=0x5000;
const KEY _cSKey_Home=0x4700;
const KEY _cSKey_End=0x4F00;
const KEY _cSKey_Pgup=0x4900;
const KEY _cSKey_Pgdn=0x5100;
const KEY _cSKey_Insert=0x5200;
const KEY _cSKey_Delete=0x5300;

const KEY _cSKey_1=0x4F31;
const KEY _cSKey_2=0x5032;
const KEY _cSKey_3=0x5133;
const KEY _cSKey_4=0x4B34;
const KEY _cSKey_5=0x4C35;
const KEY _cSKey_6=0x4D36;
const KEY _cSKey_7=0x4737;
const KEY _cSKey_8=0x4838;
const KEY _cSKey_9=0x4939;
const KEY _cSKey_0=0x5230;
const KEY _cSKey_Dot=0x532E;
const KEY _cSKey_Div=0xE02F;
const KEY _cSKey_Mul=0x372A;
const KEY _cSKey_Sub=0x4A2D;
const KEY _cSKey_Add=0x4E2B;
const KEY _cSKey_Enter=0xE00D;

#endif

3 楼

#ifndef ___xTimer
#define ___xTimer

class cTimer
{
        private:
                static const unsigned long      *p_clock;
                __int64         m_tick;
                unsigned long   timer;
        public:
                ~cTimer()
                {
                        if(frequency!=65536) SetTimer(65536);
                }
                void    InitTick()
                {
                        unsigned long ml;
                        unsigned long mh;
                        _asm
                        {
                                RDTSC;
                                mov ml,eax;
                                mov mh,edx;
                        }
                        m_tick=(__int64)mh<<32|ml;
                }
                __int64 ReadTick()
                {
                        __int64 tmp=m_tick;
                        InitTick();
                        return m_tick-tmp;
                 }
                void            InitTimer(){    timer =*(unsigned long*)(0x0046C);       }
                unsigned long   ReadTimer(){    return *(unsigned long*)(0x0046C)-timer;       }
                };    
static const unsigned long* cTimer::p_clock=(unsigned long*)0x004c;

class cFPS
{
        private:
                unsigned long   c;
                float           fps;
                cTimer          timer;
        public:
                void Start()
                {
                        c=0;
                        fps=0.0;
                        timer.InitTimer();
                }
                void Refresh(bool refVideo=false)
                {
                        c++;
                        if(refVideo)    //only use in virtual pc
                        {
                                outpd(0x3d4,0x0d);
                                outpd(0x3d5,1);
                                outpd(0x3d4,0x0d);
                                outpd(0x3d5,0);
                        }
                }
                void Stop(){    fps=c*cTimer::GetFrequency()/timer.ReadTimer();   }
                float FPS(){    return fps;        }
                friend ostream& operator<<(ostream& os,const cFPS& fps){        return os<<fps.fps;   }
};
#endif

4 楼

#ifndef ___xSvga
#define ___xSvga

#ifndef ___xBase
#include "xBase.hpp"
#endif

#ifndef ___xGBase
#include "xGBase.hpp"
#endif

#ifndef ___xRM
#include "xRM.hpp"
#endif

#ifndef ___xSurface
#include "xSurface.hpp"
#endif

struct sSVGA_Card_Info
{
        unsigned char   VESASignature[4];
        unsigned short  VESAVersion;   
        unsigned long   OEMStringPtr;     
        unsigned long   Capabilities;  
        unsigned long   VideoModePtr;  
        unsigned short  VRAMSize;                 //*64k
    unsigned short  OemSoftwareRev;
    unsigned long     OemVendorNamePtr;          // Pointer to Vendor Name String
      unsigned long     OemProductNamePtr;         // Pointer to Product Name String
      unsigned long     OemProductRevPtr;          // Pointer to Product Revision String
      unsigned short  AccelVbeVersion;           // VBE/AF Version
      unsigned long    AccelVideoModePtr;         // Pointer to Acclelerated Mode List
        unsigned char   Reserved20[216];           // Reserved for VBE implementation
        unsigned char   NaVsyakiySloochay[256];    // 2e
};

struct sSVGA_Mode_Info
{
        unsigned short  ModeAttributes;
        unsigned char   WinAAttributes;
        unsigned char   WinBAttributes;
        unsigned short  WinGranularity;
        unsigned short  WinSize;
        unsigned short  WinASegment;
        unsigned short  WinBSegment;
        unsigned long      WinFuncPtr;
        unsigned short  bytesPerScanLine;
        unsigned short  XResolution;
        unsigned short  YResolution;
        unsigned char   XCharSize;
        unsigned char   YCharSize;
        unsigned char   NumberOfPlanes;
        unsigned char   BitsPerPixel;
        unsigned char   NumberOfBanks;
        unsigned char   MemoryModel;
        unsigned char   BankSize;
        unsigned char   NumImagePages;
        unsigned char   Reserved12;
        unsigned char   RedMaskSize;
        unsigned char   RedMaskPos;
        unsigned char   GreenMaskSize;
        unsigned char   GreenMaskPos;
        unsigned char   BlueMaskSize;
        unsigned char   BlueMaskPos;
        unsigned char   ReservedMaskSize;
        unsigned char   ReservedMaskPos;
        unsigned char   DirectScreenMode;
        unsigned long   PhysBasePtr;
        unsigned long   OffScreenMemOffset;
        unsigned short  OffScreenMemSize;    //*1k
        unsigned short  LinBytesPerScanLine;
        unsigned char   BnkNumberOfImagePages;
    unsigned char   LinNumberOfImagePages;
    unsigned char   LinRedMaskSize;
    unsigned char   LinRedFieldPosition;
    unsigned char   LinGreenMaskSize;
    unsigned char   LinGreenFieldPosition;
    unsigned char   LinBlueMaskSize;
    unsigned char   LinBlueFieldPosition;
    unsigned char   LinRsvdMaskSize;
    unsigned char   LinRsvdFieldPosition;
    unsigned long   MaxPixelClock;
        unsigned char   ModeResever[190];
};

class cSVGA
{
        private:
                static cSVGA*   __instance;
                sSVGA_Card_Info SCI;
                sSVGA_Mode_Info SMI;
                bool            SetSign;
                unsigned short  DAC_Bit;
                cPALETTE        SysPal;
                unsigned int    bufLength;
                unsigned int    bytePerPixel;
                unsigned char*  pLBA;
                unsigned char*  pBuffer;

                cSVGA():SetSign(false),pLBA(NULL),pBuffer(NULL),bufLength(0){}
                cSVGA(cSVGA const&){}
    public:
                static const unsigned long _c_linear;
                static const unsigned long _c_hacc;
                static const unsigned long _c_noclear;
                static cSVGA* Instance()
                {
                        if(__instance==NULL)
                                __instance=new cSVGA;
                        return __instance;
                }
                ~cSVGA()
                {
                        if(SetSign) CloseMode();
                }

                int     SetMode(unsigned int x=800,unsigned int y=600,unsigned int c=8,unsigned long cap=0);
                void    CloseMode();

                unsigned short DAC_PALETTE(){     return DAC_Bit;       }

                const sSVGA_Card_Info& GetSCI(void){ return SCI; }
                const sSVGA_Mode_Info& GetSMI(void){ return SMI; }

                bool GetPage(unsigned int &Page);
                bool SetPage(unsigned int Page);

                bool SetFrameBuffer(cBitmap& Buffer_map);
                unsigned  int   GetBufferLength(void){  return bufLength;       }
                unsigned  char* GetFrameBuffer(void){   return pBuffer; };
                unsigned  char* GetDirectBuffer(void){   return pLBA;    };

                void    Wait(void);
                bool    Refresh(void);
                bool    Refresh(const cRECT& rect);
                bool    Refresh(unsigned int left,unsigned int top,unsigned int width,unsigned int height){     return Refresh(cRECT(left,top,width,height));       };

                bool SetColor(unsigned char index,unsigned char r,unsigned char g,unsigned char b);
                bool SetColor(unsigned char index,sCOLOR c);
                bool GetColor(unsigned char index,sCOLOR& c);

                bool SetPal(const cPALETTE* pal);
                bool GetPal(cPALETTE* pal);
                bool GetSysPal(cPALETTE* pal);
                bool RestorePal(void){  return SetPal(&SysPal);  };

                bool ChangeColor(unsigned char index,sCOLOR A,sCOLOR B,unsigned int times);
                bool ChangeColor(unsigned char index,sCOLOR T,unsigned int times);

                bool ChangePal(const cPALETTE* A,const cPALETTE* B,unsigned int times,unsigned int Method=0);
                bool ChangePal(const cPALETTE* T,unsigned int times,unsigned int Method=0);
};

static cSVGA* cSVGA::__instance=NULL;
static const unsigned long cSVGA::_c_hacc=_cBit[13];
static const unsigned long cSVGA::_c_linear=_cBit[14];
static const unsigned long cSVGA::_c_noclear=_cBit[15];

int cSVGA::SetMode(unsigned int x,unsigned int y,unsigned int c,unsigned long cap)
{
        struct s_RMI RMI;
        union REGS r;
        const char far* cinfo;
        const char far* minfo;
    unsigned short  sel,seg,ic;
    unsigned long phys;

        if(SetSign==true) return 1;

        if(!cRealMode::Malloc(32,seg,sel)) return 2;
    memset(&RMI,0,sizeof(RMI));
        RMI.EAX=0x4f00;
        RMI.ES=seg;
        RMI.EDI=0;
        if(!cRealMode::Interrupt(0x0010,RMI)) return 3;
    if((unsigned short) RMI.EAX!=0x004f) return 4;
        cinfo=(const char far*)(MK_FP(sel,0));
        _fmemcpy(&SCI,cinfo,sizeof(SCI));
        if(!cRealMode::Free(sel)) return 5;

        if(SCI.VESAVersion<0x0200) return 6;
        if(SCI.Capabilities&_cBit[0]) DAC_Bit=8;

        for(ic=0x0100;ic<0x0200;ic++)
    {
                if(!cRealMode::Malloc(32,seg,sel)) return 2;
                memset(&RMI,0,sizeof(RMI));
                RMI.EAX=0x4f01;
                RMI.ECX=ic;
                RMI.ES=seg;
                RMI.EDI=0;
                if(!cRealMode::Interrupt(0x0010,RMI)) return 3;
                minfo=(const char far*)(MK_FP(sel,0));
                _fmemcpy(&SMI,minfo,sizeof(SMI));
                if(!cRealMode::Free(sel)) return 5;
                memset(&RMI,0,sizeof(RMI));

                if(SMI.BitsPerPixel==c&&SMI.XResolution==x&&SMI.YResolution==y)
                {
                        r.w.ax = 0x4f02;
                        r.w.bx = ic|cap;
              int386 (0x10, &r, &r);
              if (r.w.ax != 0x004f) return 9;

                        SetSign=true;
                        r.w.ax=0x4f08;
                        r.w.bx=0x0001;
                        int386(0x10,&r,&r);
                        DAC_Bit=r.h.bh==0?6:8;

                        if(cap&_c_linear)
                        {
                                bufLength=(unsigned int)SMI.YResolution*(unsigned int)SMI.bytesPerScanLine;
                                pLBA=(unsigned char *) cRealMode::PhysicalToLinear(SMI.PhysBasePtr,bufLength);
                               if(pLBA==NULL) return 11;
                        }

                        bytePerPixel=c>>3;
                        GetPal(&SysPal);
                        return 0;
                }
        }        
    return 10;
}
void cSVGA::CloseMode()
{
        union REGS r;
        if(pLBA!=NULL)
        {
                r.w.ax=0x0801;
                r.w.bx=(unsigned int)pLBA>>16;
                r.w.cx=(unsigned int)pLBA;
                int386(0x31,&r,&r);
        }
        SetPal(&SysPal);
        r.x.eax=0x03;
        int386(0x10,&r,&r);
    SetSign=false;
}
/*inline bool cSVGA::GetMode(unsigned int &Mode)
{
        union REGS r;
        r.x.eax=0x4f03;
        int386(0x10,&r,&r);
        if(r.x.eax!=0x004f) return false;
        Mode=r.x.ebx;
    return true;
}*/

inline bool cSVGA::GetPage(unsigned int &Page)
{
        union REGS r;
        r.w.ax=0x4f05;
        r.w.bx=0x0100;
        int386(0x10,&r,&r);
        if(r.w.ax!=0x004f) return false;
        Page=r.w.dx;
    return true;
}
inline bool cSVGA::SetPage(unsigned int Page)
{    
    union REGS r;
        r.w.ax=0x4f05;
        r.w.bx=0x0000;
        r.w.dx=Page;
        int386(0x10,&r,&r);
        if(r.w.ax!=0x004f) return false;
    return true;
}

inline bool cSVGA::SetFrameBuffer(cBitmap& Buffer_map)
{
        if(bufLength==0) bufLength=((unsigned int)SMI.YResolution*(unsigned int)SMI.bytesPerScanLine|0xffff)+1;
        pBuffer=(unsigned char*)Buffer_map.Create((unsigned int)SMI.XResolution,(unsigned int)SMI.YResolution,(unsigned int)SMI.BitsPerPixel,bufLength);
        return pBuffer!=NULL;
}

inline void cSVGA::Wait(void)
{
        while(inpd(0x3da)&0x08);
        while(!(inpd(0x3da)&0x08));
}

inline bool cSVGA::Refresh(void)
{
        if(pBuffer==NULL) return false;
        if(pLBA!=NULL) memcpy(pLBA,pBuffer,bufLength);
        else
        {
                unsigned int i=0, mp=(bufLength>>16);
                unsigned char *ps=pBuffer,*pd=(unsigned char *)(0xA0000);
                for(i=0;i<mp;i++,ps+=0x10000)
                {
                        SetPage(i);
                        memcpy(pd,ps,0x10000);
                }
        }
        return true;
}
inline bool cSVGA::Refresh(const cRECT& rect)
{
        if(pBuffer==NULL) return false;        
        if(pLBA!=NULL)
        {
                unsigned int offset=rect.top*SMI.bytesPerScanLine+rect.left*bytePerPixel;
                unsigned int l=rect.width*bytePerPixel;
                unsigned char *ps=pBuffer+offset,*pd=pLBA+offset;
                for(unsigned int i=0;i<rect.height;i++,ps+=SMI.bytesPerScanLine,pd+=SMI.bytesPerScanLine)  memcpy(pd,ps,l);
                return true;
        }
        else return false;
}

inline bool cSVGA::SetColor(unsigned char index,unsigned char r,unsigned char g,unsigned char b)
{
        if(SMI.BitsPerPixel!=8) return false;
        unsigned char rm=8-DAC_Bit;
        outpd(0x3c8,index);
        outpd(0x3c9,r>>rm);
        outpd(0x3c9,g>>rm);
        outpd(0x3c9,b>>rm);
    return true;
}
inline bool cSVGA::SetColor(unsigned char index,sCOLOR c)
{
        return SetColor(index,c.R,c.G,c.B);
}
inline bool cSVGA::GetColor(unsigned char index,sCOLOR& c)
{
        if(SMI.BitsPerPixel!=8) return false;
        unsigned int lm=8-DAC_Bit;
        outpd(0x3c7,index);
        c.R=inpd(0x3c9)<<lm;
        c.G=inpd(0x3c9)<<lm;
        c.B=inpd(0x3c9)<<lm;
    return true;
}

inline bool cSVGA::ChangeColor(unsigned char index,sCOLOR A,sCOLOR B,unsigned int times)
{
        if(SMI.BitsPerPixel!=8) return false;
        float d[3]={
                ((float)B.R-(float)A.R)/(float)times,
                ((float)B.G-(float)A.G)/(float)times,
                ((float)B.B-(float)A.B)/(float)times };
        for(unsigned int i=0;i<=times;i++)
                SetColor(index,(unsigned char)(A.R+i*d[0]),(unsigned char)(A.G+i*d[1]),(unsigned char)(A.B+i*d[2]));
    return true;
}
inline bool cSVGA::ChangeColor(unsigned char index,sCOLOR T,unsigned int times)
{
        if(SMI.BitsPerPixel!=8) return false;
        sCOLOR F;
    GetColor(index,F);
    ChangeColor(index,F,T,times);
    return true;
}
bool cSVGA::SetPal(const cPALETTE* pal)
{
        if(SMI.BitsPerPixel!=8) return false;
        if(pal==NULL) return false;
        for(unsigned int i=0;i<256;i++) SetColor(i,(*pal)[i]);
    return true;
}
bool cSVGA::GetPal(cPALETTE* pal)
{
        if(SMI.BitsPerPixel!=8) return false;
        if(pal==NULL) return false;
        for(unsigned int i=0;i<256;i++) GetColor(i,(*pal)[i]);
    return true;
}
bool cSVGA::GetSysPal(cPALETTE* pal)
{
        if(SMI.BitsPerPixel!=8) return false;
        if(pal==NULL) return false;
        *pal=SysPal;
    return true;
}

bool cSVGA::ChangePal(const cPALETTE* A,const cPALETTE* B,unsigned int times,unsigned int Method)
{
        if(SMI.BitsPerPixel!=8) return false;
        if(A==NULL||B==NULL) return false;
        unsigned int i;
     if(!Method)
    {
                float d[3][256];
                for(i=0;i<256;i++)
                {       d[0][i]=((float)(*B)[i].R-(float)(*A)[i].R)/(float)times;
                        d[1][i]=((float)(*B)[i].G-(float)(*A)[i].G)/(float)times;
                        d[2][i]=((float)(*B)[i].B-(float)(*A)[i].B)/(float)times;
        }
                for(unsigned int j=0;j<=times;j++)
                        for(i=0;i<256;i++)
                                SetColor(i,(unsigned char)((*A)[i].R+j*d[0][i]),(unsigned char)((*A)[i].G+j*d[1][i]),(unsigned char)((*A)[i].B+j*d[2][i]));
    }
    else
        for(i=0;i<256;i++) ChangeColor(i,(*A)[i],(*B)[i],times);
    return true;
}
inline bool cSVGA::ChangePal(const cPALETTE* T,unsigned int times,unsigned int Method)
{
        if(SMI.BitsPerPixel!=8) return false;
        if(T==NULL) return false;
        cPALETTE F;
        GetPal(&F);
        ChangePal(&F,T,times,Method);
    return true;
}

#endif

5 楼

#ifndef ___xGIO
#define ___xGIO

#ifndef ___xString
#include "xString.hpp"
#endif

#ifndef ___xSurface
#include "xSurface.hpp"
#endif

struct sFile_PCX
{
        unsigned char manufacturer;
        unsigned char verison;
        unsigned char encoding;
        unsigned char bitsperpixel;
        short xmin,ymin;
        short xmax,ymax;
        short hers,vers;
        unsigned char palette[48];
        unsigned char reserved;
        unsigned char colorplane;
        short width;
        short palettetype;
        unsigned char filer[58];
};

class cGIO
{
        protected:
                bool    loadpcx(cBitmap& bmp,FILE* fp,unsigned int bpp);
                bool    loadbmp(cBitmap& bmp,FILE* fp,unsigned int bpp);
                bool    loadgif(cBitmap& bmp,FILE* fp,unsigned int bpp);
                bool    loadjpg(cBitmap& bmp,FILE* fp,unsigned int bpp);
                bool    loadtga(cBitmap& bmp,FILE* fp,unsigned int bpp);
                bool    savepcx(cBitmap& bmp,FILE* fp,unsigned int bpp);
                bool    savebmp(cBitmap& bmp,FILE* fp,unsigned int bpp);
                bool    savegif(cBitmap& bmp,FILE* fp,unsigned int bpp);
                bool    savejpg(cBitmap& bmp,FILE* fp,unsigned int bpp);
        public:
                bool    Load(cBitmap& bmp, const cString& filename,unsigned int bpp);
                bool    Save(cBitmap& bmp, const cString& filename,unsigned int bpp);
};

bool cGIO::loadpcx(cBitmap& bmp,FILE* fp,unsigned int bpp)
{
        sFile_PCX       pcx;
        cCOLOR          color;
        uCOLOR          wcolor;
        char            simpal[768];
        cPALETTE        pal;
        int x,y,count,total,height;
        unsigned char data;

        fseek(fp, 0,SEEK_SET);
        fread(&pcx, 1, sizeof(pcx), fp);
        fseek(fp,-768l,SEEK_END);
        fread(&simpal,768l,1,fp);
        fseek(fp,128L,SEEK_SET);

        height=pcx.ymax-pcx.ymin+1;
        bmp.Create(pcx.width,height,bpp);
        pal.FromSimple(simpal,8);

        char *p=(char *)bmp.GetBuffer(0,0);

        for(y=0;y<height;y++)
        {
                total=0;
                while(total<pcx.width)
                {
                        count=1;
                        data=fgetc(fp);
                        if(0xc0==(0xc0&data))
                        {
                                count=0x3f&data;
                                data=fgetc(fp);
                        }
                        for(x=0;x<count;x++,p+=bmp.Bpp())
                                if(bpp!=8) bmp.Set(p,&color.FromIndex(data,&pal).ToColor(bpp,&pal));
                                else  bmp.Set(p,&data);
                        total+=count;
                }
        }
        bmp.SetPalette(&pal);
        return true;
}

bool    cGIO::Load(cBitmap& bmp,const cString& filename,unsigned int bpp)
{
        bool ret=false;
        FILE* fp;
        if((fp=fopen(filename.c_str(),"rb"))==NULL) return false;
        cString type=filename.subString(filename.Length()-3,3);
        type.LowCase();
        if(type=="pcx") ret=loadpcx(bmp,fp,bpp);
        fclose(fp);
        return ret;
}

#endif

6 楼

#ifndef ___xBase
#define ___xBase

const unsigned long _cBit[]={
    0x1,0x2,0x4,0x8,
    0x10,0x20,0x40,0x80,
    0x100,0x200,0x400,0x800,
    0x1000,0x2000,0x4000,0x8000,
    0x10000,0x20000,0x40000,0x80000,
    0x100000,0x200000,0x400000,0x800000,
    0x1000000,0x2000000,0x4000000,0x8000000,
    0x10000000,0x20000000,0x40000000,0x80000000
};

template <class T> class cBase
{
        public:
                static void Swap(T& a, T& b){   T c=a,a=b,b=c;  }
};

#endif

7 楼

#ifndef ___xGBase
#define ___xGBase

#ifndef ___xBase
#include "xBase.hpp"
#endif

const unsigned long COLOR_MASK[5][8]={
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0xffffffff,0xf7def7de,0xe79ce79c,0xc718c718,0x86108610,0x04000400,0,0},
{0xffffffff,0xfefefefe,0xfcfcfcfc,0xf8f8f8f8,0xf0f0f0f0,0xe0e0e0e0,0xc0c0c0c0,0x80808080},
{0xffffffff,0xfefefefe,0xfcfcfcfc,0xf8f8f8f8,0xf0f0f0f0,0xe0e0e0e0,0xc0c0c0c0,0x80808080}
};

struct sTrueColor32
{
        unsigned char   B;
        unsigned char   G;
        unsigned char   R;
        unsigned char   A;
        
};
struct sTrueColor24
{
        unsigned char   B;
        unsigned char   G;
        unsigned char   R;
};
struct sHighColor555
{
        unsigned int    B:5;
        unsigned int    G:5;
        unsigned int    R:5;
        unsigned int    A:1;
};
struct sHighColor565
{
        unsigned int    B:5;
        unsigned int    G:6;
        unsigned int    R:5;
};

union uCOLOR
{
        sTrueColor32    T32;
        sTrueColor24    T24;
        sHighColor555   H555;
        sHighColor565   H565;
        unsigned long   C;
};

typedef sTrueColor32 sCOLOR;

class cPALETTE
{
        private:
                sCOLOR *PAL;
        public:
                cPALETTE()
                {
                        PAL=new sCOLOR[256];
                        for(int i=0;i<256;PAL[i++].R=PAL[i].G=PAL[i].B=PAL[i].A=0);
                }
                cPALETTE(cPALETTE const& src)
                {
                        PAL=new sCOLOR[256];
                        for(int i=0;i<256;PAL[i++]=src.PAL[i]);
                }
                ~cPALETTE()
                {
                        delete[] PAL;
                }
                cPALETTE const& operator=(cPALETTE const& src)
                {
                        if(this==&src) return *this;
                        for(int i=0;i<256;PAL[i++]=src.PAL[i]);
                        return *this;
                }
                sCOLOR& operator[](unsigned int idx)
                {
                        return PAL[idx];
                }
                const sCOLOR& operator[](unsigned int idx) const
                {
                        return PAL[idx];
                }
                void FromSimple(const char* src,unsigned int bpp=8)
                {
                        for(int i=0;i<256;i++)
                        {
                                PAL[i].R=src[i*3]<<(8-bpp);
                                PAL[i].G=src[i*3+1]<<(8-bpp);
                                PAL[i].B=src[i*3+2]<<(8-bpp);
                        }
                }
};

class cFColor
{
        public:
                float R;
                float G;
                float B;
                cFColor(float r=0,float g=0,float b=0):R(r),G(g),B(b){}
                void    Level(int l)
                {
                        float l1=1.0/(float)l;
                        R*=l1;
                        G*=l1;
                        B*=l1;
                }
};

class cCOLOR
{
        public:
                union
                {
                        struct
                        {
                                unsigned char B;
                                unsigned char G;
                                unsigned char R;
                                unsigned char A;
                        };
                        unsigned long C;
                };

                cCOLOR(unsigned char r,unsigned char g,unsigned char b,unsigned char a=255):R(r),G(g),B(b),A(a){}
                cCOLOR(unsigned long c=0):C(c){}
                uCOLOR  ToH555() const
                {
                        uCOLOR ret;
                        ret.H555.R=R>>3;
                        ret.H555.G=G>>3;
                        ret.H555.B=B>>3;
                        ret.H555.A=A>>7;
                        return ret;
                }

                uCOLOR  ToH565() const
                {
                        uCOLOR ret;
                        ret.H565.R=R>>3;
                        ret.H565.G=G>>2;
                        ret.H565.B=B>>3;
                        return ret;
                }
                uCOLOR  ToT32()  const
                {
                        uCOLOR ret;
                        ret.C=C;
                        return ret;
                }
                uCOLOR  ToT24()  const
                {
                        uCOLOR ret;
                        ret.C=C;
                        return ret;
                }
                uCOLOR  ToIndex(const cPALETTE* pal,unsigned int max_diff=40,unsigned int addtion=5)  const
                {
                        unsigned int D,DMin=0x300,i;
                        int d[3];
                        uCOLOR ret;
                        ret.C=0;
                        if(pal==NULL) return ret;
                        for(i=0;i<256;i++)
                        {
                                if((d[0]=(*pal)[i].R>R?(*pal)[i].R-R:R-(*pal)[i].R)>max_diff) continue;
                                if((d[1]=(*pal)[i].G>R?(*pal)[i].G-G:G-(*pal)[i].G)>max_diff) continue;
                                if((d[2]=(*pal)[i].B>R?(*pal)[i].B-B:B-(*pal)[i].B)>max_diff) continue;
                                if((D=d[0]+d[1]+d[2]) <DMin)
                                {
                                        DMin=D;
                                        ret.C=i;
                                }
                                if(DMin==0) break;
                        }
                        return DMin<0x300?ret:ToIndex(pal,max_diff+addtion,addtion);
                };
                uCOLOR ToColor(unsigned int l,const cPALETTE* pal=NULL) const
                {
                        if(l==8) return ToIndex(pal);
                        else if(l==15) return ToH555();
                        else if(l==16) return ToH565();
                        else if(l==32) return ToT32();
                        else return ToT24();
                }
                const cCOLOR& FromH555(uCOLOR c)
                {
                        R=c.H555.R<<3;
                        G=c.H555.G<<3;
                        B=c.H555.B<<3;
                        A=c.H555.A<<7;
                        return *this;
                }
                const cCOLOR& FromH565(uCOLOR c)
                {
                        R=c.H565.R<<3;
                        G=c.H565.G<<2;
                        B=c.H565.B<<3;
                        return *this;
                }
                const cCOLOR& FromT24(uCOLOR c)
                {
                        C=c.C;
                        return *this;
                }
                const cCOLOR& FromT32(uCOLOR c)
                {
                        C=c.C;
                        return *this;
                }
                const cCOLOR& FromIndex(unsigned char i,const cPALETTE* pal)
                {
                        if(pal==NULL) C=0;
                        else
                        {
                                R=(*pal)[i].R;
                                G=(*pal)[i].G;
                                B=(*pal)[i].B;
                        }
                        return *this;
                }
                const cCOLOR& FromColor(uCOLOR c,unsigned char l,const cPALETTE* pal=NULL)
                {
                        if(l==15) return FromH555(c);
                        else if(l==16) return FromH565(c);
                        else if(l==24) return FromT24(c);
                        else if(l==32) return FromT32(c);
                        else return FromIndex(c.C,pal);
                }
                const cCOLOR& Invert()
                {
                        C=~C;
                        return *this;
                }
                const cCOLOR& Gray()
                {
                        R=G=B=R*5+G*9+B*2>>4;
                        return *this;
                }
                const cCOLOR& operator +=(const cCOLOR& src)
                {
                        C=(C&COLOR_MASK[4][1])+(src.C&COLOR_MASK[4][1])>>1;
                        return *this;
                }
                cFColor operator -(const cCOLOR& src)
                {
                        return cFColor(R-src.R,G-src.G,B-src.B);
                }

};

#endif

8 楼

#ifndef ___xRM
#define ___xRM

struct s_RMI    //real mode interrupt structure
{
    unsigned long    EDI,ESI,EBP,ReservedByRMI,EBX,EDX,ECX,EAX;
    unsigned short  flags,ES,DS,FS,GS,IP,CS,SP,SS;
};

class cRealMode
{
    public:
        static bool Malloc(unsigned short Size, unsigned short& Segment,  unsigned short& Selector)
        {
               union REGS regs;
                memset(&regs, 0, sizeof(regs));
                regs.w.ax = 0x0100;               
                regs.w.bx = Size;        //size*16
                int386 (0x31, &regs, &regs);
                if (regs.x.cflag) return false;
                Selector = regs.w.dx;
               Segment = regs.w.ax;
               return true;
        }
        static bool Free(unsigned short Selector)
        {   
            union REGS regs;
                memset(&regs, 0, sizeof(regs));
                regs.x.eax = 0x0101;
                regs.x.edx = Selector;
                int386 (0x31, &regs, &regs);
                if (regs.x.cflag) return false;
            return true;
        }
        static bool Interrupt(unsigned short IntNO, s_RMI& RealRegs)
        {
            union REGS regs;
              struct SREGS sregs;
                memset(&regs, 0, sizeof(regs));
                memset(&sregs, 0, sizeof(sregs));
                regs.w.ax = 0x0300;
                regs.w.bx = IntNO&0xff;
               regs.w.cx = 0;
                        sregs.es = FP_SEG(&RealRegs);
                        regs.x.edi = FP_OFF(&RealRegs);
                int386x(0x31, &regs, &regs, &sregs);
                if (regs.x.cflag) return false;
                return true;
        }
        static unsigned long PhysicalToLinear(unsigned long physAddr, unsigned long length)
        {
            union REGS regs;
            regs.w.ax = 0x800;
                regs.w.bx = physAddr >> 16;
            regs.w.cx = physAddr;
                regs.w.si = length >> 16;
                regs.w.di = length;
                int386(0x31, &regs, &regs);
                if (regs.w.cflag) return 0;
                return ((unsigned long)regs.w.bx << 16) + regs.w.cx;
        }
        static unsigned long GetSelectorBase(unsigned short Selector)
        {   
            union REGS regs;
            regs.w.ax = 6;
                regs.w.bx = Selector;
                int386(0x31, &regs, &regs);
                return ((unsigned long)regs.w.cx << 16) + regs.w.dx;
        }        
        static bool SetSelectorLength(unsigned short Selector, unsigned long length)
        {   
            union REGS regs;
                regs.w.ax = 8;
                regs.w.bx = Selector;
                regs.w.cx = length >> 16;
                regs.w.dx = length;
                int386(0x31, &regs, &regs);
                if (regs.w.cflag) return false;
                return true;
        }
        /*static void* PhysicalAddr(unsigned long base, unsigned long length)
        {
                struct SREGS sregs;
                unsigned long  linAddr;
                unsigned long  DSBaseAddr;
                segread(&sregs);
                DSBaseAddr = DPMIGetSelectorBase(sregs.ds);
                if((linAddr = DPMIPhysicalToLinear(base, limit))==FALSE)
                  return NULL;
                 if(DPMISetSelectorLimit(sregs.ds, 0xFFFFFFFFUL)==FALSE)
            return NULL;
            return (void*)(linAddr - DSBaseAddr);
        }*/
};

#endif

9 楼

#ifndef ___xString
#define ___xString

class cString
{
        protected:
                char*   m_data;
                unsigned int length,size;
    public:
                cString(unsigned int len=8);
                cString(const char* p,unsigned int l=0);
                cString(const cString& Src);
                ~cString();

        const unsigned int Length(void) const;
        const unsigned int FreeSize(void) const;
        const unsigned int SizeByte(void) const;

                const cString& UpCase(void);
                const cString& LowCase(void);

                const char* c_str(void) const;
                static const cString ToString(long data,int entry=10);
        long ToLong(unsigned int start);

                const cString& operator =(const cString& Src);
                const cString& operator +=(const cString& Src);
                const cString& operator +=(char c);
                const cString& operator -=(unsigned int l);
                cString operator+(const cString& stra);
                cString operator+(const char& c);
                cString operator-(unsigned int l);

                bool operator ==(const cString& Str) const;
                bool operator !=(const cString& Str) const;
                bool operator <(const cString& Str) const;
                bool operator <=(const cString& Str) const;
                bool operator >(const cString& Str) const;
                bool operator >=(const cString& Str) const;

                char& operator[](int index) const;
                char& operator[](int index);

        void Resize(unsigned int newsize);
        void Clear(void);
                cString& Compact(void);

                cString& Reverse(void);
                cString subString(unsigned int start,int len) const;
                void    subString(unsigned int start,int len,cString& substr) const;
                unsigned int Find(unsigned int start,const cString& substr) const;
                cString& Insert(unsigned int start,const cString& substr);
                unsigned int Remove(unsigned int start,const cString& substr);
                cString& Remove(unsigned int start,unsigned int len);
                cString& RemoveAll(const cString& substr);
                cString& Replace(const cString& strold,const cString& strnew);
                cString& Trim(cString& substr,int method=3);

                friend ostream& operator <<(ostream& outputStream,const cString& str);
};


inline cString::cString(unsigned int len)
{
    m_data=new char[len+1];
    length=0;
    size=len;
}
inline cString::cString(const char * p,unsigned int l)
{
    if(l==0) for(length=0;*(p+(length++))!='\0';); else length=l+1;
    length--;
    m_data=new char[length+1];
    size=length;
        memcpy(m_data,p,length);
}
inline cString::cString(const cString& Src)
{
    m_data=new char[Src.length+1];
    length=Src.length;
    size=Src.length;
        memcpy(m_data,Src.m_data,length);
}
inline cString::~cString()
{
    size=length=0;
    delete[] m_data;
        m_data=NULL;
}
inline const unsigned int cString::Length(void) const
{
    return length;
}
inline const unsigned int cString::FreeSize(void) const
{
    return size-length;
}
inline void cString::Clear(void)
{
    length=0;
}

const cString& cString::UpCase(void)
{
        for(unsigned int i=0;i<length;i++)
        if(m_data[i]>='a'&&m_data[i]<='z') m_data[i]-=0x20;
    return *this;
}
const cString& cString::LowCase(void)
{
    for(unsigned int i=0;i<length;i++)
        if(m_data[i]>='A'&&m_data[i]<='Z') m_data[i]+=0x20;
    return *this;
}
inline const char * cString::c_str(void) const
{
    m_data[length]='\0';
    return m_data;
}
inline const cString cString::ToString(long data,int entry)
{
    char p[48];
        return cString(ltoa(data,p,entry));
}
inline long cString::ToLong(unsigned int start)
{
    return atol(&m_data[start]);
}
inline const cString& cString:: operator =(const cString& Src)
{
        if(&Src==this) return *this;
    if(size<Src.length)
    {
        delete[] m_data;
                m_data=new char[Src.length+1];
        size=Src.length;
    }
    length=Src.length;
        memcpy(m_data,Src.m_data,length);
    return *this;
}
inline const cString& cString:: operator +=(const cString& Src)
{
        char* ptemp=&m_data[length];
    if(size<length+Src.length)
    {
        size=length+Src.length;
                ptemp=new char[size+1];
                memcpy(m_data,ptemp,length);
        delete[] m_data;
        m_data=ptemp;
        ptemp=&ptemp[length];
    }
        memcpy(Src.m_data,ptemp,Src.length);
    length+=Src.length;
    return *this;
}
const cString& cString:: operator +=(char c)
{
        char* ptemp=&m_data[length];
    if(size<length+1)
    {
        size=length+1;
                ptemp=new char[size+1];
                memcpy(m_data,ptemp,length);
        delete[] m_data;
        m_data=ptemp;
    }
    m_data[length++]=c;
    return *this;
}
inline const cString& cString:: operator -=(unsigned int l)
{
        length=l>length?0:length-l;
    return *this;
}
inline bool cString:: operator ==(const cString& Str) const
{
    if(length!=Str.length) return false;
        return (bool)!memcmp(m_data,Str.m_data,length);
}
inline bool cString::operator !=(const cString& Str) const
{
        return (bool)!(*this==Str);
}
inline bool cString:: operator <(const cString& Str) const
{
    unsigned int l=length<Str.length?length:Str.length;
        return (bool)(memcmp(m_data,Str.m_data,l)<0);
}
inline bool cString:: operator <=(const cString& Str) const
{
    unsigned int l=length<Str.length?length:Str.length;
        return (bool)(memcmp(m_data,Str.m_data,l)<=0);
}
inline bool cString::operator >(const cString& Str) const
{
        return (bool)!(*this<=Str);
}
inline bool cString::operator >=(const cString& Str) const
{
        return (bool)!(*this<Str);
}
inline char& cString::operator[](int index) const
{
    return length==0?m_data[0]:m_data[index%length];
}

inline char& cString::operator[](int index)
{
    return length==0?m_data[0]:m_data[index%length];
}

inline cString cString:: operator+(const cString& stra)
{
        cString retStr(length+stra.length);
    retStr=*this;
    retStr+=stra;
    return retStr;
}
inline cString cString:: operator+(const char& c)
{
        cString retStr(length+1);
    retStr=*this;
    retStr+=c;
    return retStr;
}
inline cString cString:: operator-(unsigned int l)
{
        return cString((const char*)m_data,length-l);
}
inline void cString::Resize(unsigned int newsize)
{
    if(newsize==size) return;
    length=newsize>length?length:newsize;
    size=newsize;
        char* ptemp=new char[size+1];
        memcpy(m_data,ptemp,length);
    delete[] m_data;
    m_data=ptemp;
}
inline cString& cString:: Compact(void)
{
    if(size==length) return *this;
    Resize(length);
    return *this;
}
cString& cString:: Reverse(void)
{
        char temp;
    for(unsigned int i=0,l=length-1;i<l;i++,l--)
        temp=m_data[i],m_data[i]=m_data[l],m_data[l]=temp;
    return *this;
}
inline cString cString:: subString(unsigned int start,int len) const
{
    if(start+len>length) len=length-start;
        if(start>length) return cString();
        cString retStr(len);
        char * ptemp=&m_data[start];
        memcpy(retStr.m_data,ptemp,len);
        retStr.length=len;
    return retStr;
}
void cString:: subString(unsigned int start,int len,cString& substr) const
{
    if(start+len>length) len=length-start;
    if(start>length) return;
        char * ptemp=&m_data[start];
    if(substr.size<len) substr.Resize(len);
        memcpy(ptemp,substr.m_data,len);
    substr.length=len;
}
unsigned int cString::Find(unsigned int start,const cString& substr) const
{
        if(start+substr.length>length) return length;
        if(substr.length==0) return start;
        int i,j,l=length-substr.length;
    for(i=start;i<=l;i++)
                if(m_data[i]==substr.m_data[0])
                        if ((bool)! memcmp(&m_data[i],substr.m_data,substr.length)) return i;
    return length;
}
inline cString& cString:: Insert(unsigned int start,const cString& substr)
{
        if(size<length+substr.length) Resize(length+substr.length);
    if(start>length) start=length;
    unsigned int l=length-start;
        memmove(&m_data[start+substr.length],&m_data[start],l);
        memcpy(substr.m_data,&m_data[start],substr.length);
        length+=substr.length;
    return *this;
}
inline cString& cString:: Remove(unsigned int start,unsigned int len)
{
    if(start>=length) return *this;
    int l=length-start-len;
        if(l>0) memmove(&m_data[start],&m_data[start+len],l);
    length=l<0?start:length-len;
    return *this;
}
inline unsigned int cString::Remove(unsigned int start,const cString& substr)
{
        unsigned int s=Find(start,substr);
        Remove(s,substr.length);
    return s;
}
inline cString& cString:: RemoveAll(const cString& substr)
{
    if(length==0) return *this;
        while(Remove(0,substr)!=length);
    return *this;
}
cString& cString::Trim(cString& substr,int method)
{
    if(length==0) return *this;
        if(method==0) return RemoveAll(substr);
        if(method&1) while(Find(0,substr)==0&&length!=0) Remove(0,substr);
    if(method&2)
    {
        Reverse();
                substr.Reverse();
                Trim(substr,1);
                substr.Reverse();
        Reverse();
    }
    return *this;
}
inline cString& cString:: Replace(const cString& strold,const cString& strnew)
{
    unsigned int i=0;
        while((i=Find(i,strold))!=length)
    {
                Remove(i,strold);
                Insert(i,strnew);
    }
    return *this;
}


inline ostream& operator <<( ostream& outputStream,const cString& str)
{
    return outputStream<<str.c_str();
}

#endif

10 楼

#ifndef ___xSurface
#define ___xSurface

#ifndef ___xGBase
#include "xGBase.hpp"
#endif

#ifndef ___xlStruct
#include "xlStruct.hpp"
#endif

class cBitmap
{
        private:
                bool    ref_map;
                cBitmap(const cBitmap&):pcolorpms(32),pcolorfns(16){};
                const cBitmap& operator =(const cBitmap& src){ return *this;    };
                bool addpara(void* p,cParameterQueue& queue)
                {
                        long addr=(long)p;
                        return queue.Poke(&addr);
                }
        protected:
                unsigned int width,height,bppi,Bppi,bytesLine;
                cRECT           sel_rect;
                char*           p_map;
                cPALETTE*       palette;
                cParameterQueue pcolorpms;
                cParameterQueue pcolorfns;
        public:
                cBitmap():p_map(NULL),palette(NULL),width(0),height(0),bppi(0),Bppi(0),bytesLine(0),ref_map(false),sel_rect(0,0,0,0),pcolorpms(32),pcolorfns(16){}
                virtual ~cBitmap()
                {
                        if(!ref_map) delete[]   p_map;
                        p_map=NULL;
                        delete palette;
                        palette=NULL;
                        sel_rect=cRECT(0,0,0,0);
                }
                unsigned int Width(){   return width;   }
                unsigned int Height(){   return height;   }
                unsigned int bpp(){   return bppi;   }
                unsigned int Bpp(){   return Bppi;   }
                unsigned int BytesPerLine(){    return bytesLine;       }
                const cPALETTE* GetPalette(){   return  palette;        }
                void SetPalette(const   cPALETTE* src)
                {
                        if(src==NULL) return;
                        if(palette==NULL) palette=new cPALETTE(*src);
                        else *palette=*src;
                }
                void    Clip(const cRECT& rect){      sel_rect=rect;  }
                const cRECT thisRect(){   return cRECT(0,0,width,height);    }
                void*   GetBuffer(unsigned int x=0,unsigned int y=0){     return p_map+y*bytesLine+(x*bppi>>3); }
                void     Set(void *pdest,void *pdata)
                {
                        if(Bppi==4) *(long *)pdest=*(long *)pdata;
                        else if(Bppi==2) *(short *)pdest=*(short *)pdata;
                        else if(Bppi==1) *(char *)pdest=*(char *)pdata;
                        else *(sTrueColor24 *)pdest=*(sTrueColor24 *)pdata;
                }
                void    Get(void *pdest,void *pdata)
                {
                        if(Bppi==4) *(long *)pdata=*(long *)pdest;
                        else if(Bppi==2) *(short *)pdata=*(short *)pdest;
                        else if(Bppi==1) *(char *)pdata=*(char *)pdest;
                        else *(sTrueColor24 *)pdata=*(sTrueColor24 *)pdest;
                }
                void*   Create(unsigned int w,unsigned int h,unsigned int b,unsigned int l=0)
                {
                        delete[] p_map;
                        delete palette;
                        palette=b==8?new cPALETTE:NULL;
                        width=w, height=h, bppi=b, Bppi=b>>3, bytesLine=width*b>>3, ref_map=false;
                        sel_rect=cRECT(0,0,w,h);
                        return p_map=new char[l==0?(w*h*b>>3):l];
                }
                void*   Create(void* p,unsigned int w,unsigned int h,unsigned int b)
                {
                        if(p_map!=p) delete[] p_map;
                        delete palette;
                        palette=b==8?new cPALETTE:NULL;
                        width=w, height=h, bppi=b, Bppi=b>>3, bytesLine=width*b>>3, ref_map=true;
                        sel_rect=cRECT(0,0,w,h);
                        return p_map=(char *)p;
                }
                void*   CloneMemory(const cBitmap& src)
                {
                        Create(src.width,src.height,src.bppi);
                        memcpy(p_map,src.p_map,bytesLine*height);
                        return p_map;
                }
                void mapSwap(cBitmap& src)
                {
                        bool tb;
                        char* tp;
                        tb=ref_map, ref_map=src.ref_map, src.ref_map=tb;
                }       
                void Swap(cBitmap& src)
                {
                        unsigned int t;
                        bool tb;
                        char* tp;
                        cPALETTE* tpl;
                        cRECT tr;
                        t=width, width=src.width, src.width=t;
                        t=height, height=src.height, src.height=t;
                        t=bppi, bppi=src.bppi, src.bppi=bppi;
                        t=Bppi, Bppi=src.Bppi, src.Bppi=Bppi;
                        t=bytesLine, bytesLine=src.bytesLine, src.bytesLine=t;
                        tb=ref_map, ref_map=src.ref_map, src.ref_map=tb;
                        tp=p_map, p_map=src.p_map,src.p_map=tp;
                        tpl=palette, palette=src.palette,src.palette=tpl;
                        tr=sel_rect,sel_rect=src.sel_rect,src.sel_rect=tr;
                }       
                bool    AddFunc(uCOLOR& (*func)(uCOLOR&,cParameterQueue&)){        return addpara(func,pcolorfns); }
                bool    AddPara(void *para){       return pcolorpms.Poke(para);    }
                bool    AddPointPara(void *para){       return addpara(para,pcolorpms);    }
                void    Refresh()
                {
                        char *plt=p_map+sel_rect.top*bytesLine+sel_rect.left*Bppi;
                        char *p=plt;
                        uCOLOR& (*cfunc[16])(uCOLOR&,cParameterQueue&);
                        for(unsigned int l=pcolorfns.ReCall(); l<pcolorfns.Length();pcolorfns.Peek(&cfunc[l++]));
                        for(unsigned int i=0;i<sel_rect.height;i++,p=(plt+=bytesLine))
                        for(unsigned int j=0;j<sel_rect.width;j++,p+=Bppi)
                        for(unsigned int k=pcolorpms.ReCall();k<pcolorfns.Length();k++) cfunc[k](*(uCOLOR *)p,pcolorpms);
                }
                friend class cLayer_Idx;
                friend class cBitmap_Effect;
                friend class cLight;
                friend class cDraw2D;
                friend class cGIO;
};

class cPixel_XY_Pipe
{};
class cPixal_Color_Pipe
{};

class cBitmap_Effect
{
        public:
                 static  void    xCopy(cBitmap& dest,cBitmap& src)
                {
                        unsigned int scale_x=src.sel_rect.width/dest.width,scale_y=src.sel_rect.height/dest.height;
                        unsigned int id,jd,is,js;
                        char *p=dest.p_map;
                        if(dest.bppi!=src.bppi)
                        {
                                uCOLOR c;
                                cCOLOR cr;
                                for(is=src.sel_rect.top,id=0;id<dest.height;id++,is+=scale_y) for(js=src.sel_rect.left,jd=0;jd<dest.width;jd++,js+=scale_x,p+=dest.Bppi)
                                {
                                        src.Get(src.GetBuffer(js,is),&c);
                                        cr.FromColor(c,src.bppi,src.palette);
                                        c=cr.ToColor(dest.bppi,dest.palette);
                                        dest.Set(p,&c);
                                }
                        }
                        else
                        {
                                dest.SetPalette(src.GetPalette());
                                for(is=src.sel_rect.top,id=0;id<dest.height;id++,is+=scale_y) for(js=src.sel_rect.left,jd=0;jd<dest.width;jd++,js+=scale_x,p+=dest.Bppi)
                                        dest.Set(p,src.GetBuffer(js,is));
                        }
                }
                static  void    Rotate(cBitmap& dest,cBitmap& src)
                {
                        dest.Create(src.sel_rect.height,src.sel_rect.width,src.bppi);
                        char *p=dest.p_map;
                        for(unsigned int i=0;i<dest.height;i++)
                        for(unsigned int j=0;j<dest.width;j++,p+=dest.Bppi)
                                dest.Set(p,src.GetBuffer(src.sel_rect.left+i,src.sel_rect.top+j));
                }
                static  void    Rotate(cBitmap& bmp)
                {
                        cBitmap temp;
                        temp.Create(bmp.height,bmp.width,bmp.bppi);
                        char *p=bmp.p_map;
                        for(unsigned int i=0;i<bmp.height;i++)
                        for(unsigned int j=0;j<bmp.width;j++,p+=bmp.Bppi)
                                temp.Set(temp.GetBuffer(i,j),p);
                        bmp.mapSwap(temp);
                }
                static  void    Flip(cBitmap& dest,cBitmap& src)
                {
                        dest.Create(src.sel_rect.width,src.sel_rect.height,src.bppi);
                        dest.SetPalette(src.palette);
                        for(unsigned int i=0;i<src.sel_rect.height;i++)
                                memcpy(dest.GetBuffer(0,dest.height-i-1),src.GetBuffer(src.sel_rect.left,src.sel_rect.top+i),src.sel_rect.width*src.Bppi);
                }
                static  void    Flip(cBitmap& bmp)
                {
                        cBitmap temp;
                        temp.Create(bmp.width,bmp.height,bmp.bppi);
                        for(unsigned int i=0;i<bmp.height;i++)
                                memcpy(temp.GetBuffer(0,temp.height-i-1),bmp.GetBuffer(0,i),bmp.bytesLine);
                        bmp.mapSwap(temp);
                }
                static  void    Mirror(cBitmap& dest)
                {
                        cBitmap temp;
                        uCOLOR c;
                        temp.CloneMemory(dest);
                        for(unsigned int i=0;i<dest.sel_rect.height;i++)
                        for(unsigned int j=0;j<dest.sel_rect.width;j++)
                        {
                                dest.Get(dest.GetBuffer(dest.sel_rect.left+j,dest.sel_rect.top+i),&c);
                                temp.Set(temp.GetBuffer(dest.sel_rect.left+dest.sel_rect.width-j-1,dest.sel_rect.top+i),&c);
                        }
                        dest.Swap(temp);
                }
                static  void    Invert(cBitmap& dest,cBitmap& src)
                {
                        uCOLOR c;
                        dest.Create(src.sel_rect.width,src.sel_rect.height,src.bppi);                        
                        if(src.bppi!=8)
                        {
                                char *p=dest.p_map;
                                for(unsigned int i=0;i<dest.sel_rect.height;i++)
                                for(unsigned int j=0;j<dest.sel_rect.width;j++,p+=dest.Bppi)
                                {
                                        src.Get(src.GetBuffer(src.sel_rect.left+j,src.sel_rect.top+i),&c);
                                        c.C=~c.C;
                                        dest.Set(p,&c);
                                }
                        }
                        else{}
                }
                static  void    Invert(cBitmap& bmp)
                {
                        __int64 *p=(__int64 *)bmp.p_map,*pe=(__int64 *)bmp.GetBuffer(bmp.width-1,bmp.height-1);
                        for(;p<pe;p++) *p=~*p;
                }
                static  void    Gray(cBitmap& bmp)
                {
                        cCOLOR cr;
                        uCOLOR c;
                        char *p=bmp.p_map;
                        for(unsigned int i=0;i<bmp.height;i++)
                        for(unsigned int j=0;j<bmp.width;j++,p+=bmp.Bppi)
                        {
                                bmp.Get(p,&c);
                                cr.FromColor(c,bmp.bppi,bmp.palette);
                                c=cr.Gray().ToColor(bmp.bppi,bmp.palette);
                                bmp.Set(p,&c);
                        }
                }
                static  void    ColorFilter(cBitmap& bmp,cCOLOR filter)
                {
                        uCOLOR cr=filter.ToColor(bmp.bppi,bmp.palette);
                        long c=cr.C;
                        if(bmp.bppi<24) c<<=16,c|=cr.C;
                        long *p=(long *)bmp.p_map,*pe=(long *)bmp.GetBuffer(bmp.width-1,bmp.height-1);
                        for(;p<pe;p++) *p&=c;
                }
                static  void    HalfBlend(cBitmap& bmp,cCOLOR filter)
                {
                        cCOLOR cr;
                        uCOLOR c;
                        char *p=bmp.p_map, *pe=(char *)bmp.GetBuffer(bmp.width-1,bmp.height-1);
                        for(;p<pe;p+=bmp.Bppi)
                        {
                                cr.FromColor(*(uCOLOR *)p,bmp.bppi,bmp.palette);
                                cr+=filter;
                                c=cr.ToColor(bmp.bppi,bmp.palette);
                                bmp.Set(p,&c);
                        }
                }
                static  void    HalfBlendFast(cBitmap& bmp,cCOLOR filter)
                {
                        uCOLOR cr=filter.ToColor(bmp.bppi,bmp.palette);
                        long c=cr.C;
                        if(bmp.bppi<24) c<<=16,c|=cr.C;
                        long *p=(long *)bmp.p_map,*pe=(long *)bmp.GetBuffer(bmp.width-1,bmp.height-1);
                        for(;p<pe;p++) *p=((c&COLOR_MASK[bmp.Bppi][1])+(*p&COLOR_MASK[bmp.Bppi][1])>>1);
                }
                static  void    HalfBlendFast(cBitmap& bmp,cBitmap& bmpx)
                {
                        long *p=(long *)bmp.p_map,*pe=(long *)bmp.GetBuffer(bmp.width-1,bmp.height-1);
                        long *q=(long *)bmpx.p_map;
                        for(;p<pe;p++,q++) *p=((*q&COLOR_MASK[bmp.Bppi][1])+(*p&COLOR_MASK[bmp.Bppi][1])>>1);
                }
                static  void    Soften(cBitmap& bmp)
                {
                        cCOLOR cr[5];
                        uCOLOR c;
                        char *p=(char *)bmp.GetBuffer(1,1);
                        unsigned long la=bmp.bytesLine-bmp.Bppi,ls=bmp.bytesLine+bmp.Bppi;
                        unsigned long mask=COLOR_MASK[4][2];
                        for(unsigned int i=1;i<bmp.height-1;i++,p+=bmp.Bppi+bmp.Bppi)
                        for(unsigned int j=1;j<bmp.width-1;j++,p+=bmp.Bppi)
                        {
                                cr[0].FromColor(*(uCOLOR *)(p-ls),bmp.bppi,bmp.palette);
                                cr[1].FromColor(*(uCOLOR *)(p-la),bmp.bppi,bmp.palette);
                                cr[2].FromColor(*(uCOLOR *)(p+la),bmp.bppi,bmp.palette);
                                cr[3].FromColor(*(uCOLOR *)(p+ls),bmp.bppi,bmp.palette);
                                cr[4].C=(cr[0].C&mask)+(cr[1].C&mask)+(cr[2].C&mask)+(cr[3].C&mask)>>2;
                                c=cr[4].ToColor(bmp.bppi,bmp.palette);
                                bmp.Set(p,&c);
                        }
                }
                static  void    SoftenFast(cBitmap& bmp)
                {
                        uCOLOR c;
                        char *p=(char *)bmp.GetBuffer(1,1);
                        unsigned long la=bmp.bytesLine-bmp.Bppi,ls=bmp.bytesLine+bmp.Bppi;
                        unsigned long mask= COLOR_MASK[bmp.Bppi][2];
                        for(unsigned int i=1;i<bmp.height-1;i++,p+=bmp.Bppi+bmp.Bppi)
                        for(unsigned int j=1;j<bmp.width-1;j++,p+=bmp.Bppi)
                        {
                                c.C=(((uCOLOR *)(p-ls))->C&mask)+(((uCOLOR *)(p-la))->C&mask)+(((uCOLOR *)(p+la))->C&mask)+(((uCOLOR *)(p+ls))->C&mask)>>2;
                                bmp.Set(p,&c);
                        }
                }
                static  void    AlphaBlend(cBitmap& bmp,cCOLOR filter,int alpha)
                {
                        cCOLOR cr;
                        uCOLOR c;
                        int ab=255-alpha;
                        int R=filter.R*alpha,G=filter.G*alpha,B=filter.B*alpha;
                        char *p=bmp.p_map,*pe=(char *)bmp.GetBuffer(bmp.width-1,bmp.height-1);
                        for(;p<pe;p+=bmp.Bppi)
                        {
                                cr.FromColor(*(uCOLOR *)p,bmp.bppi,bmp.palette);
                                cr.R=ab*cr.R+R>>8;
                                cr.G=ab*cr.G+G>>8;
                                cr.B=ab*cr.B+B>>8;
                                c=cr.ToColor(bmp.bppi,bmp.palette);
                                bmp.Set(p,&c);
                        }
                };
                static  void    AlphaBlend(cBitmap& bmpBG,cBitmap& bmpFT,cBitmap& alpha)
                {
                        cCOLOR crf,crb;
                        uCOLOR cf,cb;
                        char *p=bmpBG.p_map;
                        char *q=bmpFT.p_map;
                        char *r=alpha.p_map;
                        char  a;
                        for(unsigned int i=0;i<bmpBG.height;i++)
                        for(unsigned int j=0;j<bmpBG.width;j++,p+=bmpBG.Bppi,q+=bmpFT.Bppi,r++)
                        {
                                a=255-*r;
                                crf.FromColor(*(uCOLOR *)q,bmpFT.bppi,bmpFT.palette);
                                crb.FromColor(*(uCOLOR *)p,bmpBG.bppi,bmpBG.palette);
                                crb.R=a*crb.R+crf.R*(*r)>>8;
                                crb.G=a*crb.G+crf.G*(*r)>>8;
                                crb.B=a*crb.B+crf.B*(*r)>>8;
                                cb=crb.ToColor(bmpBG.bppi,bmpBG.palette);
                                bmpBG.Set(p,&cb);
                        }
                }
                static  void    Blur(cBitmap& dest);
                static  void    Sharp(cBitmap& dest);
                static  void    AA(cBitmap& dest);
};

#endif

我来回复

您尚未登录,请登录后再回复。点此登录或注册