posted by REDFORCE 2017. 4. 19. 23:09

#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 Class의 header 파일을 살펴봤던 것에 이어서, 


.cpp 내용을 살펴보도록 하겠습니다.


1. 생성자 / 소멸자 


간단하게 생성자부터 확인하도록 하겠습니다.


#include "stdafx.h"
#include "image.h"
//=============================================================================
// default constructor
//=============================================================================
Image::Image()
{
initialized = false; // set true when successfully initialized
spriteData.width = 2;
spriteData.height = 2;
spriteData.x = 0.0;
spriteData.y = 0.0;
spriteData.scale = 1.0;
spriteData.angle = 0.0;
spriteData.rect.left = 0; // used to select one frame from multi-frame image
spriteData.rect.top = 0;
spriteData.rect.right = spriteData.width;
spriteData.rect.bottom = spriteData.height;
spriteData.texture = NULL; // the sprite texture (picture)
spriteData.flipHorizontal = false;
spriteData.flipVertical = false;
cols = 1;
textureManager = NULL;
startFrame = 0;
endFrame = 0;
currentFrame = 0;
frameDelay = 1.0; // default to 1 second per frame of animation
animTimer = 0.0;
visible = true; // the image is visible
loop = true; // loop frames
animComplete = false;
graphics = NULL; // link to graphics system
colorFilter = graphicsNS::WHITE; // WHITE for no change
layer = NULL;
}
view raw image.cpp hosted with ❤ by GitHub


생성자 내엔 역시나 주요 변수들에 대한 초기화가 이루어집니다.


포인터 형태인 변수는 null 값으로 초기화가 됐습니다만,

최근에는 모던 C++을 공부하면서 nullptr을 사용하는 것이 더 낫다 보여집니다.



소멸자 아무 내용도 없어서 여기에 담진 않았습니다.



2. initialize


두 번째 내용은 의미있는 초기화를 담당하고 있는 initialize 함수 입니다.


try - catch 구문으로 감싸져있으며, 어떤 exception이 발생할 시엔 return false 값을 내뱉고 종료합니다.

//=============================================================================
// Initialize the Image.
// Post: returns true if successful, false if failed
// pointer to Graphics
// width of Image in pixels (0 = use full texture width)
// height of Image in pixels (0 = use full texture height)
// number of columns in texture (1 to n) (0 same as 1)
// pointer to TextureManager
//=============================================================================
bool Image::initialize(Graphics *g, int width, int height, int ncols,
TextureManager *textureM)
{
try{
graphics = g; // the graphics object
textureManager = textureM; // pointer to texture object
spriteData.texture = textureManager->getTexture();
if(width == 0)
width = textureManager->getWidth(); // use full width of texture
spriteData.width = width;
if(height == 0)
height = textureManager->getHeight(); // use full height of texture
spriteData.height = height;
cols = ncols;
if (cols == 0)
cols = 1; // if 0 cols use 1
// configure spriteData.rect to draw currentFrame
spriteData.rect.left = (currentFrame % cols) * spriteData.width;
// right edge + 1
spriteData.rect.right = spriteData.rect.left + spriteData.width;
spriteData.rect.top = (currentFrame / cols) * spriteData.height;
// bottom edge + 1
spriteData.rect.bottom = spriteData.rect.top + spriteData.height;
this->layer = LAYERMANAGER->getLayer(enLayerList::LAYER_DEFAULT);
}
catch(...) {return false;}
initialized = true; // successfully initialized
return true;
}
//=============================================================================
// Initialize the Image.
// Post: returns true if successful, false if failed
// pointer to Graphics
// width of Image in pixels (0 = use full texture width)
// height of Image in pixels (0 = use full texture height)
// number of columns in texture (1 to n) (0 same as 1)
// pointer to TextureManager
// pointer to this->layer
//=============================================================================
bool Image::initialize(Graphics * g, int width, int height, int ncols, TextureManager * textureM, Layer * pLayer)
{
try {
graphics = g; // the graphics object
textureManager = textureM; // pointer to texture object
spriteData.texture = textureManager->getTexture();
if (width == 0)
width = textureManager->getWidth(); // use full width of texture
spriteData.width = width;
if (height == 0)
height = textureManager->getHeight(); // use full height of texture
spriteData.height = height;
cols = ncols;
if (cols == 0)
cols = 1; // if 0 cols use 1
// configure spriteData.rect to draw currentFrame
spriteData.rect.left = (currentFrame % cols) * spriteData.width;
// right edge + 1
spriteData.rect.right = spriteData.rect.left + spriteData.width;
spriteData.rect.top = (currentFrame / cols) * spriteData.height;
// bottom edge + 1
spriteData.rect.bottom = spriteData.rect.top + spriteData.height;
this->layer = pLayer;
}
catch (...) { return false; }
initialized = true; // successfully initialized
return true;
}
view raw image.cpp hosted with ❤ by GitHub


try 안에서는 매게변수로 받아온 


 (1) Graphics*

 (2) width / height

 (3) cols

 (4) textureManager*


값을 그대로 대입합니다.


아마 이글에서 textureManager (?) 하고 의문이 들수 있습니다.


지금까지 언급했던 내용중에는 textureManager라는 항목이 없었거든요.

많이 궁금하시겠지만 일단은 그냥 그런가보다 하고 넘어가시기 바랍니다.


여기서 꼭 숙지할 점은 받아온 각 파라미터의 값들이 spriteData 안에 대입이 되어진다는 것을 확인하시기 바랍니다.


그리고 Layer 라고 하는 클래스도 갑자기 등장했습니다만, 

추후 Layer 파트에 대한 메뉴얼이 있으므로 그곳에서 설명드리도록 하겠습니다.



3. Draw Functions


세 번째 Draw 함수들 입니다.


Graphics에서 보신것과 같이 기본적인 Image를 Draw 하는 함수와

Image의 Rect가 어느정도 크기인지를 볼 수 있게 도와주는 drawRect 함수입니다.


(drawNine 함수는 현재 제대로 작동하지 않아서 언급하지 않습니다. 추후 버그수정 후 다시 올리도록 하겠습니다)


//=============================================================================
// Draw the image using color as filter
// The color parameter is optional, WHITE is assigned as default in image.h
// The textureN parameter is optional, 0 is default.
// Pre : spriteBegin() is called
// Post: spriteEnd() is called
//=============================================================================
void Image::draw(COLOR_ARGB color, UINT textureN)
{
/* LayerManager Logic Rendering Function ===== Need Fixing */
//if (this->layer->getLayerState() < LAYERMANAGER->getCurrentLayerState())
// return;
if (this->layer->getSwitch() == FALSE)
return;
if (!visible || graphics == NULL)
return;
// set texture to draw
spriteData.texture = textureManager->getTexture(textureN);
if(color == graphicsNS::FILTER) // if draw with filter
graphics->drawSprite(spriteData, colorFilter); // use colorFilter
else
graphics->drawSprite(spriteData, color); // use color as filter
}
//=============================================================================
// Draw this image using the specified SpriteData.
// The current SpriteData.rect is used to select the texture.
// Pre : spriteBegin() is called
// Post: spriteEnd() is called
//=============================================================================
void Image::draw(SpriteData sd, COLOR_ARGB color, UINT textureN)
{
/* LayerManager Logic Rendering Function ===== Need Fixing */
//if (this->layer->getLayerState() < LAYERMANAGER->getCurrentLayerState())
// return;
if (this->layer->getSwitch() == FALSE)
return;
if (!visible || graphics == NULL)
return;
sd.rect = spriteData.rect; // use this Images rect to select texture
sd.texture = textureManager->getTexture(textureN); // set texture to draw
if(color == graphicsNS::FILTER) // if draw with filter
graphics->drawSprite(sd, colorFilter); // use colorFilter
else
graphics->drawSprite(sd, color); // use color as filter
}
void Image::drawNine(SpriteData sd, COLOR_ARGB color, UINT textureN)
{
if (this->layer->getSwitch() == FALSE)
return;
if (!visible || graphics == NULL)
return;
sd.rect = spriteData.rect; // use this Images rect to select texture
sd.texture = textureManager->getTexture(textureN); // set texture to draw
if (color == graphicsNS::FILTER) // if draw with filter
graphics->drawSprite(sd, colorFilter); // use colorFilter
else
graphics->drawSprite(sd, color); // use color as filter
}
//=============================================================================
// Draw Sprite x,y,width,height : RECT
// For Testing Function
// This is Not!! SpriteData.rect
//=============================================================================
void Image::drawRect(COLOR_ARGB color)
{
// LEFT TOP to RIGHT TOP
graphics->drawLine(spriteData.x, spriteData.y,
spriteData.x + spriteData.width, spriteData.y, 1.0f, color);
// LEFT TOP to LEFT BOTTOM
graphics->drawLine(spriteData.x, spriteData.y,
spriteData.x, spriteData.y + spriteData.height, 1.0f, color);
// RIGHT TOP to RIGHT BOTTOM
graphics->drawLine(spriteData.x + spriteData.width, spriteData.y,
spriteData.x + spriteData.width, spriteData.y + spriteData.height, 1.0f, color);
// LEFT BOTTOM to RIGHT BOTTOM
graphics->drawLine(spriteData.x, spriteData.y + spriteData.height,
spriteData.x + spriteData.width, spriteData.y + spriteData.height, 1.0f, color);
}
view raw image.cpp hosted with ❤ by GitHub


draw 함수들에 대해서는 의미적으로 이미 바로 알 수 있기 때문에


어렵게 설명할 내용은 없습니다.


현재 Image 클래스에서 갖고있는 spriteData 구조체에 들어가있는 Texture와 각종 값들을 토대로 Texture 이미지를 그대로 graphics 클래스에게 부탁하여 drawSprite 함수를 호출합니다.



나머지 함수부터는 다음 글로 이어서 적도록 하겠습니다.

'MyProject > SephyEngine' 카테고리의 다른 글

June_Engine #3_Layer Class  (0) 2017.04.21
June_Engine #3_Image Class .cpp (2)  (0) 2017.04.20
June_Engine #3_Image (Class) & SpriteData (Struct)  (0) 2017.04.19
June_Engine #2_Graphics.cpp (3)  (0) 2017.04.18
June_Engine #2_Graphics.cpp (2)  (0) 2017.04.17