วันศุกร์ที่ 26 พฤศจิกายน พ.ศ. 2553

เอาภาพ sprite มาแสดง

การบ้าน
1. นำภาพ sprite มาแสดงในหน้าจอ 5 ภาพ
2. มี 1 ภาพที่ขยับได้



รูปทั้งหมดที่ใช้ในงาน


อธิบาย Code
CDXSprite* pic = 0;
CDXSprite* pic2 = 0;

CDXSprite* char1 = 0;
CDXSprite* char2 = 0;
CDXSprite* char3 = 0;
CDXSprite* char4 = 0;

int a  = 0;
int x=0,y=0;
เริ่มจากประกาศตัวแปร ที่ใช้ทั้งหมดก่อน CDXSprite ใช้สำหรับโหลดภาพเข้ามา ส่วน ตัวแปร a นั้นใช้เก็บค่าของเฟรมของ sprite ที่จะเรียกใช้ x และ y เป็นค่าตำแหน่ง x และ y ของภาพที่จะขยับ

pic = new CDXSprite( );
pic->Create( "bg.png", 1 );

pic2 = new CDXSprite( );
pic2->Create( "fireheart-sprites7vehicles.png", 32, 32, 1 );

char1 = new CDXSprite( );
char1->Create( "dbzsprites.png",66.66,94, 1 );

char2 = new CDXSprite( );
char2->Create( "flighmare-sprites.png",83.33,82.5, 1 );

char3 = new CDXSprite( );
char3->Create( "sprites_2.gif",93.5,118.25, 1 );

char4 = new CDXSprite( );
char4->Create( "Xigbar_face_sprites_by_ShiningamiMaxwell.png",78,97.5, 1 );
โหลดภาพทั้งหมดเข้ามาที่ตัวแปร
pic2->Create( "fireheart-sprites7vehicles.png", 32, 32, 1 );

ตัวเลข 32 ทั้ง 2 ตัวคือ ค่าความกว้างและความสูงของแต่ละเฟรมใน sprite

void cdx_DeInit( void )
{
// TODO: Destroy your CDX objects here
SAFEDELETE( pic );
SAFEDELETE( pic2 );
SAFEDELETE( char1 );
SAFEDELETE( char2 );
SAFEDELETE( char3 );
SAFEDELETE( char4 );


SAFEDELETE( Fps );
SAFEDELETE( Input );
SAFEDELETE( Screen );
}



เคลียตัวแปรออกจาก ram

a++;
if(a >= 20)
a= 0;

if( Input->GetKeyState(SDLK_UP) )
y--;
if( Input->GetKeyState(SDLK_DOWN) )
y++;
if( Input->GetKeyState(SDLK_LEFT) )
x--;
if( Input->GetKeyState(SDLK_RIGHT) )
x++;




ตัวแปร a จะเพิ่มขึ้นเรื่อยๆ แต่ถ้าเกิน 20 แล้วจะกลับไปเริ่มที่ 0 ใหม่ โดยที่เมื่อกดปุ่มลูกศรแล้ว ค่าจะเปลี่ยนไปตามที่กด
กดขึ้น y จะลดที่ละ 1
กดลง y จะเพิ่มที่ละ 1
กดซ้าย x จะลดที่ละ 1
กดขวา x จะเพิ่มที่ละ 1
และค่า x และ y นี้จะไปค่าตำแหน่งของ pic2 ในหน้าจอด้วย ซึ่งต่อจาก code ของปุ่มกดจะเป็น code ของการโหลดภาพ

pic2->SetPos(x,y);
pic2->SetFrame (a);
pic2->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

char1->SetPos(150,94);
char1->SetFrame (a);
char1->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

char2->SetPos(200,94);
char2->SetFrame (a);
char2->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

char3->SetPos(250,94);
char3->SetFrame (a);
char3->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

char4->SetPos(250,150);
char4->SetFrame (a);
char4->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );


จะเห็นได้ว่าค่า SetPos ของ pic2 จะไม่เหมือนอันอื่น แต่จะเป็นตัวแปร x ,y แทนทั้งนี้เพื่อให้สามารถควบคุมตำแหน่งของภาพได้นั้นเอง

ScreenShot

Code ทั้งหมด
#include 

// ------------------------------------------------------------------
// CDX Objects
// ------------------------------------------------------------------
CDXScreen   *Screen     = 0;     // The screen object, every program must have one
CDXInput *Input  = 0;
FPSmanager *Fps  = 0;

CDXSprite* pic = 0;
CDXSprite* pic2 = 0;

CDXSprite* char1 = 0;
CDXSprite* char2 = 0;
CDXSprite* char3 = 0;
CDXSprite* char4 = 0;


int a  = 0;
int x=0,y=0;

// ------------------------------------------------------------------
// cdx_Init - handles initialization of the CDX objects
// ------------------------------------------------------------------
BOOL cdx_Init()
{
Screen = new CDXScreen();
Screen->Create( );
Screen->CreateWindowed( 480, 272, 32, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_INIT_TIMER );

Input = new CDXInput( );
Input->Create( );

Fps = new FPSmanager( );
SDL_initFramerate( Fps );
SDL_setFramerate( Fps, 30 );

// TODO: Initialize your own CDX objects here
pic = new CDXSprite( );
pic->Create( "bg.png", 1 );

pic2 = new CDXSprite( );
pic2->Create( "fireheart-sprites7vehicles.png", 32, 32, 1 );

char1 = new CDXSprite( );
char1->Create( "dbzsprites.png",66.66,94, 1 );

char2 = new CDXSprite( );
char2->Create( "flighmare-sprites.png",83.33,82.5, 1 );

char3 = new CDXSprite( );
char3->Create( "sprites_2.gif",93.5,118.25, 1 );

char4 = new CDXSprite( );
char4->Create( "Xigbar_face_sprites_by_ShiningamiMaxwell.png",78,97.5, 1 );





return TRUE;
}

// ------------------------------------------------------------------
// cdx_DeInit - handles cleanup of CDX objects
// ------------------------------------------------------------------
void cdx_DeInit( void )
{
// TODO: Destroy your CDX objects here
SAFEDELETE( pic );
SAFEDELETE( pic2 );
SAFEDELETE( char1 );
SAFEDELETE( char2 );
SAFEDELETE( char3 );
SAFEDELETE( char4 );


SAFEDELETE( Fps );
SAFEDELETE( Input );
SAFEDELETE( Screen );
}

// ------------------------------------------------------------------
// cdx_DoFrame - performs drawing of the current frame
// ------------------------------------------------------------------
void cdx_DoFrame()
{ 
Input->Update( );

Screen->GetBackEx()->Fill(0);

// TODO: Add code to draw your objects during each frame
pic->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

a++;
if(a >= 20)
a= 0;

if( Input->GetKeyState(SDLK_UP) )
y--;
if( Input->GetKeyState(SDLK_DOWN) )
y++;
if( Input->GetKeyState(SDLK_LEFT) )
x--;
if( Input->GetKeyState(SDLK_RIGHT) )
x++;

pic2->SetPos(x,y);
pic2->SetFrame (a);
pic2->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

char1->SetPos(150,94);
char1->SetFrame (a);
char1->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

char2->SetPos(200,94);
char2->SetFrame (a);
char2->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

char3->SetPos(250,94);
char3->SetFrame (a);
char3->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

char4->SetPos(250,150);
char4->SetFrame (a);
char4->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );





Screen->Flip( 0, 0, 1 );
SDL_framerateDelay( Fps );
}

int main( int argc, char* args[] )
{
#ifdef _PSP

#ifndef NDEBUG
pspDebugScreenInit( );
#endif

SetupCallbacks( );
#endif 

cdx_Init();

while(1)
{
#ifdef WIN32
if( Input->GetKeyState(SDLK_ESCAPE) )
break;
#endif

cdx_DoFrame();
}

cdx_DeInit();

return 0;
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น