Code:
//background image
LPDIRECT3DSURFACE9 back;
//sprite handler
LPD3DXSPRITE sprite_handler;
//ball sprite
LPDIRECT3DTEXTURE9 ball_image;
SPRITE ball;
//platform
LPDIRECT3DTEXTURE9 platform;
//misc
long start = GetTickCount();
HRESULT result;
int angle = 45;
//initializes the game
int Game_Init(HWND hwnd)
{
//set random number seed
srand((int)time(NULL));
//initialize mouse
if (!Init_Mouse(hwnd))
{
MessageBox(hwnd, "Error initializing the mouse", "Error", MB_OK);
return 0;
}
//initialize keyboard
if (!Init_Keyboard(hwnd))
{
MessageBox(hwnd, "Error initializing the keyboard", "Error", MB_OK);
return 0;
}
//create sprite handler object
result = D3DXCreateSprite(d3ddev, &sprite_handler);
if (result != D3D_OK)
return 0;
//load the background image
back = LoadSurface("background.bmp", NULL);
if (back == NULL)
return 0;
//load the ball sprite
ball_image = LoadTexture("ball.bmp", D3DCOLOR_XRGB(255,0,255));
if (ball_image == NULL)
return 0;
//set the ball's properties
ball.x = 50;
ball.y = 175;
ball.width = 12;
ball.height = 12;
ball.movex = 50 * cos((angle*3.14159265)/180);
ball.movey = 50 * sin((angle*3.14159265)/180);
ball.movey *= -0.1;
ball.movex *= 0.1;
//load the paddle sprite
platform = LoadTexture("platform.bmp", D3DCOLOR_XRGB(255,0,255));
if (platform == NULL)
return 0;
//return okay
return 1;
}
//the main game loop
void Game_Run(HWND hwnd)
{
//ball position vector
D3DXVECTOR3 position(0,0,0);
//make sure the Direct3D device is valid
if (d3ddev == NULL)
return;
//update mouse and keyboard
Poll_Mouse();
Poll_Keyboard();
//after short delay, ready for next frame?
//this keeps the game running at a steady frame rate
if (GetTickCount() - start >= 30)
{
//reset timing
start = GetTickCount();
//move the ball sprite
ball.x += ball.movex;
ball.y += ball.movey;
if (ball.y > 350)
{
ball.movey = 0;
ball.movex = 0;
}
else
{
ball.movey += 0.098;
}
}
//start rendering
if (d3ddev->BeginScene())
{
//erase the entire background
d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);
//start sprite handler
sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);
//draw the platform
position.x = 50;
position.y = 200;
sprite_handler->Draw(
platform,
NULL,
NULL,
&position,
D3DCOLOR_XRGB(255,255,255));
//draw the ball
position.x = (double)ball.x;
position.y = (double)ball.y;
sprite_handler->Draw(
ball_image,
NULL,
NULL,
&position,
D3DCOLOR_XRGB(255,255,255));
//stop drawing
sprite_handler->End();
//stop rendering
d3ddev->EndScene();
}
//display the back buffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);
//check for mouse button (to exit program)
if (Mouse_Button(0))
PostMessage(hwnd, WM_DESTROY, 0, 0);
//check for escape key (to exit program)
if (Key_Down(DIK_ESCAPE))
PostMessage(hwnd, WM_DESTROY, 0, 0);
}
That is all the code that is relevant.