// File sprite.h
гл
JJL
4. Работа с основными графическими устройствам
#ifndef _SPRITE_ #define __SPRITE_
^define MAX_STAGES 20
#define TRANSP_COLOR OxFF
class Sprite {
public:
int x, y; // location of upper-left corner
int width, height; // size of single image
int stageCount; // number of stages
int curStage; // current stage
char * underlmage; // place to store image
// under the sprite char * image [MAX_STAGES];
Sprite (int, int, char *,…); -Sprite ()
{
free (underlmage);
}
void set (int ax, int ay ) {
x = ax; y = ay;
}
void draw (); void storellnder (); void restoreUnder ();
};
inline int min (int x, int y )
{
return x < y ? x : y;
}
inline int max {int x, int y )
return x > y ? x : y;
>
extern char far * videoAddr;
extern int screenWidth;
extern int screenHeight;
extern int orgX;
extern int orgY;
#endif
II File sprite.cpp include <aiioc.h> include <dos h>
include "spnte.h"
Sprite :: Sprite (int w, int h, char * im1, … )
Компьютерная графика. Полигональные модели
{
char ** imPtr = &im1;
x =0; y =0; width = w; height = h; curStage = 0;
underlmage = (char *) malloc ( width * height);
for ( stageCount=0; *imPtr != NULL; imPtr ++, stageCount ++ ) image [stageCount] = * imPtr;
}
void Sprite :: draw () {
int x1 = max ( 0, x - orgX );
int x2 = min ( screenWidth, x - orgX + width );
if ( x2 < x1 )
return;
int y1 = max ( 0, y - orgY );
int y2 = min ( screenHeight, y - orgY + height);
if(y2<y1 )
return;
char far * videoPtr = videoAddr + y1 * screenWidth + x1; char- * dataPtr = image [curStage]+(y1-y)*width + (x1-x); int step = screenWidth - (x2 - x1);
for (register int y = y1; y < y2; y++, videoPtr += step ) {
for (int x = x1; x < x2; x++, dataPtr++, videoPtr++) if (* dataPtr != TRANSP_COLOR ) * videoPtr = * dataPtr;
dataPtr +^ width -(x2-x1);
}
}
void Sprite :: storeUnder () {
int x1 = max ( 0, x - orgX );
int x2 = min ( screenWidth, x - orgX + width );