#1. Engine System
+ WinMain - EngineSystem
+ EngineCore
+ EngineError
+ MainNode
#2. FrameWork
+ Grahpics
+ Image - SpriteData (현재 글)
+ Layer
+ Input
+ Game
+ Manager
+ Game Interface
+ Utility
#3. Testing Module
+ 2D Image Test
+ 2D Animation Test
+ Game UI Test
-----------------------------------------------------------------------
이번 글에서는 Image 클래스와 SpriteData 구조체에 대해서 자세히 살펴보도록 하겠습니다.
Image Class 자체는 Graphics 클래스에 비하면
헤더 내용 자체는 양반입니다만 의미 자체는 역시나 Graphics 클래스 못지않게
인게임에서 렌더링을 편하게 할 수 있게 도와주는 클래스 이므로 매우 중요합니다.
그럼 먼저 성격 급하신 분들을 위해 대체 Image Class가 뭐하는 것인지부터
간략히 설명하도록 하겠습니다.
앞서 우리는 SpriteData 라는 구조체를 봤습니다.
SpriteData 구조체 만으로도 상당히 편하게 In game에서 사용할 이미지를 대충 뿌릴 수 있습니다만...
Image Class는 SpriteData 구조체보다 한 단계 위의 작업을 하기 위해 존재합니다.
결과만 먼저 한 줄로 이야기 해드리자면, 바로 애니메이션! 을 돌리기 위함 입니다.
1. 먼저 헤더 파일의 일부를 살펴보겠습니다.
#ifndef _IMAGE_H // Prevent multiple definitions if this | |
#define _IMAGE_H // file is included in more than one place | |
#define WIN32_LEAN_AND_MEAN | |
class Image; | |
#include "constants.h" | |
#include "textureManager.h" | |
#include "layer.h" | |
class Image | |
{ | |
// Image properties | |
protected: | |
Graphics* graphics; // pointer to graphics | |
TextureManager* textureManager; // pointer to texture manager | |
// spriteData contains the data required to draw the image by Graphics::drawSprite() | |
SpriteData spriteData; // SpriteData is defined in "graphics.h" | |
COLOR_ARGB colorFilter; // applied as a color filter (use WHITE for no change) | |
int cols; // number of cols (1 to n) in multi-frame sprite | |
int startFrame; // first frame of current animation | |
int endFrame; // end frame of current animation | |
int currentFrame; // current frame of animation | |
float frameDelay; // how long between frames of animation | |
float animTimer; // animation timer | |
HRESULT hr; // standard return type | |
bool loop; // true to loop frames | |
bool visible; // true when visible | |
bool initialized; // true when successfully initialized | |
bool animComplete; // true when loop is false and endFrame has finished displaying | |
Layer* layer; | |
} |
위와 같이 Protected 접근한정자 영역 안에
이미 SpriteData 구조체가 들어가 있음을 볼 수 있습니다.
즉 Image Class 자체에 텍스쳐 및 이미지 정보를 가지고 있는 SpriteData를 내포하고 있습니다.
위에서 아래로 차례대로 변수들의 목적에 대해서 설명해드리겠습니다.
(1) *graphics : 이미지가 사용 될 Graphics 클래스 포인터
(2) *textureManager : SpriteData에 입력 될 Texture 정보
(3) spriteData : SpriteData 구조체
(4) colorFilter : 투명화 시킬 컬러 필터 값 ( 아무것도 안주면 WHITE 컬러 = No change)
(5) cols : 텍스쳐에서 애니메이션화 시킬 프레임의 열 갯수
(6) startFrame : 시작 프레임
(7) endFrame : 끝 프레임
(8) currentFrame : 현재 출력중인 프레임
(9) frameDelay : 애니메이션 딜레이
(10) animTimer : 애니메이션 타이머
(11) hr : 초기화 시 리턴 값
(12) loop : 반복 애니메이션 여부
(13) visible : 출력 여부
(14) initialized : 초기화 성공 여부
(15) animComplete : 애니메이션 끝났는지 여부
(16) *layer : Layer 클래스 포인터
이렇게 총 16개의 변수가 있어서 상당히 뭐가 많아 보이긴 합니다만.
그저 Image 클래스를 자주 쓰다보면 저절로 익혀지는 내용들이라
지금 당장 숨가쁘게 이해하실 필요는 없습니다.
2. 다음, Image Class의 각 함수들 입니다.
코드를 보신다면 아마 헐... 뭐가 이렇게 함수가 많아 라고 하실 분들도 계실텐데
주석의 량 때문에 함수 구간이 길어지다보니 많아보일 뿐
사실 함수 갯수자체로는 각 변수별 필요한 내용들에 대한 Setter/Getter 밖에없습니다.
따라서 함수에 대한 설명은 Setter/Getter 함수들을 제외한
특별한 함수들에 대해서만 설명하겠습니다.
헤더에서 일단 이런게 있구나 살펴볼 함수들은
(1) initialize
(2) draw
(3) drawLine
(4) drawRect
(5) update
이상 5가지 입니다.
(함수에 대한 설명글은 .cpp 에서 잇도록 하겠습니다)
class Image | |
{ | |
public: | |
// Constructor | |
Image(); | |
// Destructor | |
virtual ~Image(); | |
//////////////////////////////////////// | |
// Get functions // | |
//////////////////////////////////////// | |
// Return visible parameter. | |
virtual bool getVisible() {return visible;} | |
// Return X position. | |
virtual float getX() {return spriteData.x;} | |
// Return Y position. | |
virtual float getY() {return spriteData.y;} | |
// Return scale factor. | |
virtual float getScale() {return spriteData.scale;} | |
// Return width. | |
virtual int getWidth() {return spriteData.width;} | |
// Return height. | |
virtual int getHeight() {return spriteData.height;} | |
// Return center X. | |
virtual float getCenterX() {return spriteData.x + spriteData.width/2*getScale();} | |
// Return center Y. | |
virtual float getCenterY() {return spriteData.y + spriteData.height/2*getScale();} | |
// Return rotation angle in degrees. | |
virtual float getDegrees() {return spriteData.angle*(180.0f/(float)PI);} | |
// Return rotation angle in radians. | |
virtual float getRadians() {return spriteData.angle;} | |
// Return delay between frames of animation. | |
virtual float getFrameDelay() {return frameDelay;} | |
// Return number of starting frame. | |
virtual int getStartFrame() {return startFrame;} | |
// Return number of ending frame. | |
virtual int getEndFrame() {return endFrame;} | |
// Return number of current frame. | |
virtual int getCurrentFrame() {return currentFrame;} | |
// Return reference to SpriteData structure. | |
const virtual SpriteData& getSpriteInfo() {return spriteData;} // for backward compatibility | |
const virtual SpriteData& getSpriteData() {return spriteData;} | |
// Return RECT structure of Image. | |
virtual RECT getSpriteDataRect() {return spriteData.rect;} | |
// Return state of animation complete. | |
virtual bool getAnimationComplete() {return animComplete;} | |
// Return colorFilter. | |
virtual COLOR_ARGB getColorFilter() {return colorFilter;} | |
//////////////////////////////////////// | |
// Set functions // | |
//////////////////////////////////////// | |
// Set X location. | |
virtual void setX(float newX) {spriteData.x = newX;} | |
// Set Y location. | |
virtual void setY(float newY) {spriteData.y = newY;} | |
// Set scale. | |
virtual void setScale(float s) {spriteData.scale = s;} | |
// Set width. | |
virtual void setWidth(int w) {spriteData.width = w;} | |
// Set height. | |
virtual void setHeight(int h) {spriteData.height = h;} | |
// Set rotation angle in degrees. | |
// 0 degrees is up. Angles progress clockwise. | |
virtual void setDegrees(float deg) {spriteData.angle = deg*((float)PI/180.0f);} | |
// Set rotation angle in radians. | |
// 0 radians is up. Angles progress clockwise. | |
virtual void setRadians(float rad) {spriteData.angle = rad;} | |
// Set visible. | |
virtual void setVisible(bool v) {visible = v;} | |
// Set delay between frames of animation. | |
virtual void setFrameDelay(float d) {frameDelay = d;} | |
// Set starting and ending frames of animation. | |
virtual void setFrames(int s, int e){startFrame = s; endFrame = e;} | |
// Set current frame of animation. | |
virtual void setCurrentFrame(int c); | |
// Set spriteData.rect to draw currentFrame | |
virtual void setRect(); | |
// Set spriteData.rect to r. | |
virtual void setSpriteDataRect(RECT r) {spriteData.rect = r;} | |
// Set animation loop. lp = true to loop. | |
virtual void setLoop(bool lp) {loop = lp;} | |
// Set animation complete Boolean. | |
virtual void setAnimationComplete(bool a) {animComplete = a;}; | |
// Set color filter. (use WHITE for no change) | |
virtual void setColorFilter(COLOR_ARGB color) {colorFilter = color;} | |
// Set TextureManager | |
virtual void setTextureManager(TextureManager *textureM) | |
{ textureManager = textureM; } | |
// Set Animation Timer | |
virtual void setAnimTimer(float t) {animTimer = t;}; | |
//////////////////////////////////////// | |
// Other functions // | |
//////////////////////////////////////// | |
// Initialize Image | |
// Pre: *g = pointer to Graphics object | |
// width = width of Image in pixels (0 = use full texture width) | |
// height = height of Image in pixels (0 = use full texture height) | |
// ncols = number of columns in texture (1 to n) (0 same as 1) | |
// *textureM = pointer to TextureManager object | |
virtual bool Image::initialize(Graphics *g, int width, int height, | |
int ncols, TextureManager *textureM); | |
// Initialize Image | |
// Pre: *g = pointer to Graphics object | |
// width = width of Image in pixels (0 = use full texture width) | |
// height = height of Image in pixels (0 = use full texture height) | |
// ncols = number of columns in texture (1 to n) (0 same as 1) | |
// *textureM = pointer to TextureManager object | |
// *pLayer = point to This->layer | |
virtual bool Image::initialize(Graphics *g, int width, int height, | |
int ncols, TextureManager *textureM, Layer *pLayer); | |
// Flip image horizontally (mirror) | |
virtual void flipHorizontal(bool flip) {spriteData.flipHorizontal = flip;} | |
// Flip image vertically | |
virtual void flipVertical(bool flip) {spriteData.flipVertical = flip;} | |
// Draw Image using color as filter. Default color is WHITE. | |
// textureN is number of texture in textureManager | |
virtual void draw(COLOR_ARGB color, UINT textureN); | |
// Draw Image using color as filter. Default color is WHITE. | |
virtual void draw(COLOR_ARGB color = graphicsNS::WHITE) { draw(color, 0); } | |
// Draw Image using default color filter. | |
// textureN is number of texture in textureManager | |
virtual void draw(UINT textureN) { draw(graphicsNS::WHITE, textureN); } | |
// Draw this image using the specified SpriteData. | |
// The current SpriteData.rect is used to select the texture. | |
// textureN is number of texture in textureManager | |
virtual void draw(SpriteData sd, COLOR_ARGB color = graphicsNS::WHITE, UINT textureN=0); | |
// Draw this image using the sepcified SpriteData. | |
// The Current SpriteData.rect called 9 times | |
virtual void drawNine(SpriteData sd, COLOR_ARGB color = graphicsNS::WHITE, UINT textureN = 0); | |
// For Testing Draw Rect Function | |
virtual void drawRect(COLOR_ARGB color = graphicsNS::RED ); | |
// Update the animation. frameTime is used to regulate the speed. | |
virtual void update(float frameTime); | |
Layer* getLayer() { return layer; } | |
void setLayer(Layer* layer) { this->layer = layer; } | |
}; |
'MyProject > SephyEngine' 카테고리의 다른 글
June_Engine #3_Image Class .cpp (2) (0) | 2017.04.20 |
---|---|
June_Engine #3_Image Class .cpp (1) (0) | 2017.04.19 |
June_Engine #2_Graphics.cpp (3) (0) | 2017.04.18 |
June_Engine #2_Graphics.cpp (2) (0) | 2017.04.17 |
June_Engine #2_Graphics.cpp (1) (0) | 2017.04.15 |