알파값 블랜딩 효과 어떻게 넣는지 좀 알려주시면 감사 하겠습니다. ㅠㅠ

2D, 3D, 다각형, 픽셀 등 게임의 그래픽 프로그래밍에 관한 포럼입니다.

Moderator: 류광

Locked
비회원

알파값 블랜딩 효과 어떻게 넣는지 좀 알려주시면 감사 하겠습니다. ㅠㅠ

Post by 비회원 »

#include <windows.h>
#include <d3dx9.h>


#define D3DFVF_UIVERTEX_RHW (D3DFVF_XYZRHW|D3DFVF_TEX1)

#define PLAYER_BULLET_MAX 20

typedef struct _bullet
{
int x;
int y;
bool fire;
}BULLET;

char bg[25][80];
int playerX;
int playerY;

int enemyX;
int enemyY;

BULLET playerBullet[PLAYER_BULLET_MAX];

bool fire;
int bulletX;
int bulletY;

void ClearScreen();
void KeyControl(void);

void BulletDraw();

void PlayerDraw();



void EnemyDraw();

void EnemyMove(void);
void ClashEenmyAndBullet(void);

LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;

LPDIRECT3DVERTEXBUFFER9 g_pVBBG = NULL;
LPDIRECT3DTEXTURE9 g_pTextureBG = NULL;

LPDIRECT3DVERTEXBUFFER9 g_pVBPlayer = NULL;
LPDIRECT3DTEXTURE9 g_pTexturePlayer = NULL;

LPDIRECT3DVERTEXBUFFER9 g_pVBBullet = NULL;
LPDIRECT3DTEXTURE9 g_pTextureBullet = NULL;

LPDIRECT3DVERTEXBUFFER9 g_pVBEnemy = NULL;
LPDIRECT3DTEXTURE9 g_pTextureEnemy = NULL;



void Init3D(HWND hwnd);
void GameMain();
void ClearMemory();



typedef struct _CUSTOMVERTEX
{
D3DXVECTOR3 position;
FLOAT tu, tv;
}CUSTOMVERTEX;


#define WINDOW_CLASS_NAME "WINCLASS1"

HWND main_window_handle = NULL;


LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
switch(msg)
{
case WM_DESTROY:
ClearMemory();
PostQuitMessage(0);
return 0;
}


return DefWindowProc(hwnd, msg, wparam, lparam);

}


INT WINAPI WinMain( HINSTANCE hinstance, HINSTANCE, LPSTR, INT)
{

WNDCLASS winclass;
HWND hwnd;
MSG msg;


winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;

winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;




if (!RegisterClass(&winclass))
return(0);


if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME,
"정한철WINDOWS",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0,
640,480,
NULL,
NULL,
hinstance,
NULL)))
return(0);
HDC hdc;
hdc = GetDC(hwnd);


Init3D( hwnd );

while(1)
{
if (PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
{

if (msg.message == WM_QUIT)
break;



TranslateMessage(&msg);


DispatchMessage(&msg);
}
else
{
GameMain();
}


}


return 0;

}

void Init3D(HWND hwnd)
{


playerX=-200;
playerY=0;

enemyX=320;
enemyY=0;


for(int i=0;i<PLAYER_BULLET_MAX;i++)
{
playerBullet.x=0;
playerBullet.y=0;
playerBullet.fire=false;

}
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);


D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;


g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice);

// g_pd3dDevice->SetRenderstate(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
// g_pd3dDevice->SetRenderstate(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
/*
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

g_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
*/
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);


g_pd3dDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_TEX1, D3DPOOL_DEFAULT, &g_pVBBG, NULL);


CUSTOMVERTEX* pVertices;


g_pVBBG->Lock( 0, 0, (void**)&pVertices, 0 );
pVertices[0].position = D3DXVECTOR3( -320.0f, 240.0f, 0.0f );
pVertices[0].tu = 0;
pVertices[0].tv = 0;

pVertices[1].position = D3DXVECTOR3( -320.0f, -240.0f, 0.0f );
pVertices[1].tu = 0;
pVertices[1].tv = 1;

pVertices[2].position = D3DXVECTOR3( 320.0f, 240.0f, 0.0f );
pVertices[2].tu = 1;
pVertices[2].tv = 0;

pVertices[3].position = D3DXVECTOR3( 320.0f, -240.0f, 0.0f );
pVertices[3].tu = 1;
pVertices[3].tv = 1;
g_pVBBG->Unlock();

D3DXCreateTextureFromFile(g_pd3dDevice, "img\\액션가면.bmp", &g_pTextureBG); // 배경 ㄱ림


g_pd3dDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_TEX1, D3DPOOL_DEFAULT, &g_pVBPlayer, NULL);

g_pVBPlayer->Lock( 0, 0, (void**)&pVertices, 0 );
pVertices[0].position = D3DXVECTOR3( -64.0f, 64.0f, 0.0f );
pVertices[0].tu = 0;
pVertices[0].tv = 0;

pVertices[1].position = D3DXVECTOR3( -64.0f, -64.0f, 0.0f );
pVertices[1].tu = 0;
pVertices[1].tv = 1;

pVertices[2].position = D3DXVECTOR3( 64.0f, 64.0f, 0.0f );
pVertices[2].tu = 1;
pVertices[2].tv = 0;

pVertices[3].position = D3DXVECTOR3( 64.0f, -64.0f, 0.0f );
pVertices[3].tu = 1;
pVertices[3].tv = 1;
g_pVBPlayer->Unlock();

D3DXCreateTextureFromFile(g_pd3dDevice, "img\\player22.jpg", &g_pTexturePlayer);


g_pd3dDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_TEX1, D3DPOOL_DEFAULT, &g_pVBBullet, NULL);

g_pVBBullet->Lock( 0, 0, (void**)&pVertices, 0 );
pVertices[0].position = D3DXVECTOR3( -16.0f, 16.0f, 0.0f );
pVertices[0].tu = 0;
pVertices[0].tv = 0;

pVertices[1].position = D3DXVECTOR3( -16.0f, -16.0f, 0.0f );
pVertices[1].tu = 0;
pVertices[1].tv = 1;

pVertices[2].position = D3DXVECTOR3( 16.0f, 16.0f, 0.0f );
pVertices[2].tu = 1;
pVertices[2].tv = 0;

pVertices[3].position = D3DXVECTOR3( 16.0f, -16.0f, 0.0f );
pVertices[3].tu = 1;
pVertices[3].tv = 1;
g_pVBBullet->Unlock();

D3DXCreateTextureFromFile(g_pd3dDevice, "img\\초코비.bmp", &g_pTextureBullet);


g_pd3dDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_TEX1, D3DPOOL_DEFAULT, &g_pVBEnemy, NULL);

g_pVBEnemy->Lock( 0, 0, (void**)&pVertices, 0 );
pVertices[0].position = D3DXVECTOR3( -64.0f, 32.0f, 0.0f );
pVertices[0].tu = 0;
pVertices[0].tv = 0;

pVertices[1].position = D3DXVECTOR3( -64.0f, -32.0f, 0.0f );
pVertices[1].tu = 0;
pVertices[1].tv = 1;

pVertices[2].position = D3DXVECTOR3( 64.0f, 32.0f, 0.0f );
pVertices[2].tu = 1;
pVertices[2].tv = 0;

pVertices[3].position = D3DXVECTOR3( 64.0f, -32.0f, 0.0f );
pVertices[3].tu = 1;
pVertices[3].tv = 1;
g_pVBEnemy->Unlock();

D3DXCreateTextureFromFile(g_pd3dDevice, "img\\enemy.jpg", &g_pTextureEnemy);
}

void GameMain()
{
ClearScreen();

KeyControl();
ClashEenmyAndBullet();
EnemyMove();






g_pd3dDevice->BeginScene();


D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640.0f/480.0f, 1.0f, 1000.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);

D3DXVECTOR3 vEyePt(0.0f, 0.0f, -580.0f);
D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f);
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);


D3DXMATRIXA16 matBG;
D3DXMatrixIdentity(&matBG);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matBG);


g_pd3dDevice->SetTexture(0, g_pTextureBG);
g_pd3dDevice->SetStreamSource(0, g_pVBBG, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
PlayerDraw();
EnemyDraw();
BulletDraw();
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
g_pd3dDevice->EndScene();

g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}

void ClearMemory()
{

if(g_pTextureBG !=NULL)
g_pTextureBG->Release();

if(g_pVBBG !=NULL)
g_pVBBG->Release();

if(g_pTexturePlayer !=NULL)
g_pTexturePlayer->Release();

if(g_pVBPlayer !=NULL)
g_pVBPlayer->Release();

if(g_pTextureBullet !=NULL)
g_pTextureBullet->Release();

if(g_pVBBullet !=NULL)
g_pVBBullet->Release();

if(g_pTextureEnemy !=NULL)
g_pTextureEnemy->Release();

if(g_pVBEnemy !=NULL)
g_pVBEnemy->Release();

if(g_pd3dDevice !=NULL)
g_pd3dDevice->Release();

if(g_pD3D !=NULL)
g_pD3D->Release();
}
void ClearScreen()
{

g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);

}

void KeyControl(void)
{
if(GetAsyncKeyState(VK_LEFT))
{
playerX-=2;
if(playerX<-320)
playerX=-320;

}
else if(GetAsyncKeyState(VK_RIGHT))
{
playerX+=2;
if(playerX>320)
playerX=320;

}
if(GetAsyncKeyState(VK_UP))
{
playerY+=2;
if(playerY>240)
playerY=240;

}
else if(GetAsyncKeyState(VK_DOWN))
{
playerY-=2;
if(playerY<-240)
playerY=-240;

}
if(GetAsyncKeyState(VK_SPACE))
{

for(int i=0;i<PLAYER_BULLET_MAX;i++)
{
if(playerBullet.fire==false)
{
playerBullet.fire=true;
playerBullet.x=playerX+5;
playerBullet.y=playerY;
break;
}
}

}
}
void BulletDraw()

{
for(int i=0;i<PLAYER_BULLET_MAX;i++)
{
if(playerBullet.fire==true)
{

D3DXMATRIXA16 A;
D3DXMatrixIdentity(&A);
D3DXMatrixTranslation(&A, playerBullet.x, playerBullet.y, 0.0f);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &A);



g_pd3dDevice->SetTexture(0, g_pTextureBullet);
g_pd3dDevice->SetStreamSource(0, g_pVBBullet, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);



playerBullet[i].x+=5;
if(playerBullet[i].x>320)

{
playerBullet[i].fire=false;
}
}
}
}

void PlayerDraw()

{
D3DXMATRIXA16 A;
D3DXMatrixIdentity(&A);
D3DXMatrixTranslation(&A, playerX, playerY, 0.0f);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &A);
g_pd3dDevice->SetTexture(0, g_pTexturePlayer);
g_pd3dDevice->SetStreamSource(0, g_pVBPlayer, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);


}

void EnemyDraw()

{

D3DXMATRIXA16 A;
D3DXMatrixIdentity(&A);
D3DXMatrixTranslation(&A, enemyX, enemyY, 0.0f);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &A);



g_pd3dDevice->SetTexture(0, g_pTextureEnemy);
g_pd3dDevice->SetStreamSource(0, g_pVBEnemy, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

}
void EnemyMove(void)
{
enemyX-=2;
if(enemyX<-320)

{
enemyX=320;
enemyY=(rand()%480)-240;

}
}
void ClashEenmyAndBullet(void)
{
for(int i=0;i < PLAYER_BULLET_MAX;i++)
{
if(playerBullet[i].fire==true)
{
if((playerBullet[i].y>=(enemyY-32))&&(playerBullet[i].y<=(enemyY+32)))

{
if((playerBullet[i].x>=(enemyX-64))&&(playerBullet[i].x<=(enemyX+64)))

{
enemyX=320;
enemyY=(rand()%480)-240;
playerBullet[i].fire=false;

}
}
}
}
}



이것이 전체소 소스 입니다.....

일단 배경 화면 에 플레이어 하나의 랜덤으로 나오는 적 입니다...

플레이어 그림을 불러왔는데 플레리어 주위 색은 검은색 이구요...
이 검은색을 지워 플레이어 캐릭터가 자연스럽게 나오게 할려고 하는데...
몇일간 노력해봤늗네 안대네요...ㅠㅠ
어느 부분에 적용해야 될지... 고수님들 부탁 드립니다. ㅠㅠ
Locked