2016-01-27 00:40:34 chip
Page 1540
📢 PUBLIC
Sprite.cpp:
/*************************************************************************/
/** Sprite.cpp: Draw a sprite, SwapChains method. **/
/** (C)2013 nlited systems, Chip Doran **/
/*************************************************************************/
#include <Windows.h>
#include <d2d1.h>
#include <d2d1_1.h>
#include <dwrite.h>
#include <wincodec.h>
#include <math.h>
#include "Util.h"
#include "MyD2D.h"
#include "Sprite.h"
using namespace D2D1;
static int SpriteDrawBox(const struct Sprite_s *pBox, ID2D1DeviceContext *pDC);
static int SpriteDrawBullet(const struct Sprite_s *pBullet,ID2D1DeviceContext *pDC);
static int SpriteDrawBitmap(const struct Sprite_s *pSprite,ID2D1DeviceContext *pDC);
/*************************************************************************/
/** Public interface **/
/*************************************************************************/
int SpriteDraw(HD2D hD2D, const struct Sprite_s *pSprite) {
int Err= ERR_OK;
ID2D1DeviceContext *pDC= MyD2DGetDC(hD2D);
Err= SpriteDrawImg(pDC,pSprite);
return(Err);
}
int SpriteDrawImg(ID2D1DeviceContext *pDC, const struct Sprite_s *pSprite) {
if(!pDC)
return(Warn(ERR_NOINIT,__FUNCTION__": No Renderer!"));
pDC->SetTransform(Matrix3x2F::Identity());
pDC->SetTransform(Matrix3x2F::Rotation(pSprite->Rotation,Point2F(pSprite->Pos.x,pSprite->Pos.y)));
switch(pSprite->SpriteID) {
case SPRITE_BOX: return(SpriteDrawBox(pSprite,pDC));
case SPRITE_BULLET: return(SpriteDrawBullet(pSprite,pDC));
case SPRITE_BITMAP: return(SpriteDrawBitmap(pSprite,pDC));
}
return(ERR_NOTFOUND);
}
static int SpriteDrawBox(const struct Sprite_s *pBox, ID2D1DeviceContext *pDC) {
int Wid2= pBox->Size/2;
ID2D1SolidColorBrush *pbrBox;
D2D_RECT_F rBox(RectF(pBox->Pos.x-Wid2,pBox->Pos.y-Wid2,pBox->Pos.x+Wid2,pBox->Pos.y+Wid2));
pDC->CreateSolidColorBrush(pBox->Color,&pbrBox);
pDC->FillRectangle(rBox,pbrBox);
pbrBox->Release();
return(ERR_OK);
}
static int SpriteDrawBullet(const struct Sprite_s *pBullet, ID2D1DeviceContext *pDC) {
int Wid2= pBullet->Size/2;
ID2D1SolidColorBrush *pbrFill;
D2D1_ELLIPSE rDot= Ellipse(pBullet->Pos,(FLOAT)pBullet->Size,(FLOAT)pBullet->Size);
pDC->CreateSolidColorBrush(pBullet->Color,&pbrFill);
pDC->FillEllipse(rDot,pbrFill);
pbrFill->Release();
return(ERR_OK);
}
static int SpriteDrawBitmap(const struct Sprite_s *pSprite, ID2D1DeviceContext *pDC) {
FLOAT Scale;
D2D1_SIZE_F SrcDPI;
D2D_RECT_F rSrc,rDst;
struct Bitmap_s *pBitmap= pSprite->pBitmap;
if(pBitmap && pBitmap->pBitmap) {
SrcDPI= pBitmap->SizeDPI;
rSrc= RectF(0,0,SrcDPI.width,SrcDPI.height);
Scale= (FLOAT)pSprite->Size/100.0f;
if(pBitmap->FrameCt>0) {
SrcDPI.width/= pBitmap->FrameCt;
rSrc.right/= pBitmap->FrameCt;
if(pSprite->FrameID>0) {
rSrc.left+= SrcDPI.width*pSprite->FrameID;
rSrc.right+= SrcDPI.width*pSprite->FrameID;
}
}
rDst= RectF(pSprite->Pos.x-(SrcDPI.width*Scale)/2,pSprite->Pos.y-(SrcDPI.height*Scale)/2,pSprite->Pos.x+(SrcDPI.width*Scale)/2,pSprite->Pos.y+(SrcDPI.height*Scale)/2);
pDC->DrawBitmap(pBitmap->pBitmap,&rDst,pSprite->Color.a,D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,&rSrc);
}
return(ERR_OK);
}
WebV7 (C)2018 nlited | Rendered by tikope in 40.824ms | 18.191.171.10