Code:
//main source code file. contains windows api code
#include <d3d9.h>
#include <d3dx9.h>
#include <ctime>
#include <cstdio>
#include "dxgraphics.h"
#include "game.h"
//window event callback function
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
//release the DIrect3D Device
if(d3ddev != NULL)
d3ddev->Release();
//release the direct3d object'
if(d3d != NULL)
d3d->Release();
//call the "font end" shutdown function
gameEnd(hWnd);
//tell windows to kill this program
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//create the window class structure
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
//fill the struct with info
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = GAMETITLE;
wc.hIconSm = NULL;
//set up the window with the class info
return RegisterClassEx(&wc);
}
//entry point for a windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HWND hWnd;
//register the class
MyRegisterClass(hInstance);
//set up the screen in windowed or fullscreen mode?
DWORD style;
if(FULLSCREEN)
style = WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP;
else
style = WS_OVERLAPPEDWINDOW;
//create a new window
hWnd = CreateWindow(
GAMETITLE, //window class
GAMETITLE, //title bar
style, //windows style
CW_USEDEFAULT, // x position of the window
CW_USEDEFAULT, //y positin of the window
1280, //width
720, //height of the window
NULL, //parent window
NULL, //menu
hInstance, //application instance
NULL ); // window parameters
//was there an error creating the window?
if(!hWnd)
return false;
//display the window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if(!Init_Direct3D(hWnd, SCREEN_WIDTH, SCREEN_HEIGHT, FULLSCREEN))
return 0;
//initialize the game
if(!gameInit(hWnd))
{
MessageBoxA(hWnd, "Error initializing game", "Error", MB_OK);
return 0;
}
//main message loop
int done = 0;
while(!done)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//look for quit message
if(msg.message == WM_QUIT)
done = 1;
//decode and pass messages on to WndProc
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
//process game loop (else prevents from running after window is closed)
gameRun(hWnd);
}
return msg.wParam;
}
/GAME.h
Code:
#ifndef GAME_H
#define GAME_H
#include <d3d9.h>
#include <d3dx9.h>
#include <d3dx9math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "dxgraphics.h"
#include "sprite.h"
#include "player.h"
//application title
#define GAMETITLE "Zombie Game"
//screen set up
#define FULLSCREEN 0 //0 = windowed, 1 = fullscreen
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 720
//function prototypes
int gameInit(HWND);
void gameRun(HWND);
void gameEnd(HWND);
#endif
//GAME.CPP
Code:
#include "game.h"
#include "sprite.h"
//Sprite Texture Objects (Holds full sprite sheet bmp)
LPDIRECT3DTEXTURE9 main_character = NULL; //Sprite sheet for main character
SPRITE mainCharacter;
LPDIRECT3DSURFACE9 back;
LPD3DXSPRITE sprite_handler;
HRESULT result;
//timing variable
long start = GetTickCount();
//initialize the game
int gameInit(HWND hwnd)
{
//set random number seed
srand(time(NULL));
//create sprite handler object
result = D3DXCreateSprite(d3ddev, &sprite_handler);
if(result != D3D_OK)
return 0;
//load the main character sprite sheet
main_character = LoadTexture("mainCharacter.bmp", D3DCOLOR_XRGB(255, 0, 255));
if(main_character == NULL)
return 0;
back = LoadSurface("background.bmp", NULL);
//initializes the sprites properties
mainCharacter.x = 100;
mainCharacter.y = 150;
mainCharacter.width = 64;
mainCharacter.height = 64;
mainCharacter.curframe = 0;
mainCharacter.lastframe = 1;
mainCharacter.animdelay = 8;
mainCharacter.animcount = 0;
mainCharacter.movex = 5;
mainCharacter.movey = 0;
return 1;
}
//the main game loop
void gameRun(HWND hwnd)
{
//make sure the d3d device is valid
if(d3ddev == NULL)
return;
//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 sprite
mainCharacter.x += mainCharacter.movex;
mainCharacter.y += mainCharacter.movey;
//warp the sprite on the edges of the screen
if(mainCharacter.x > SCREEN_WIDTH - mainCharacter.width)
mainCharacter.x = 0;
if(mainCharacter.x < 0)
mainCharacter.x = SCREEN_WIDTH - mainCharacter.width;
//has animation delay reached threshold?
if(++mainCharacter.animcount > mainCharacter.animdelay)
{
//reset counter
mainCharacter.animcount = 0;
//animate the sprite
if(++mainCharacter.curframe > mainCharacter.lastframe)
mainCharacter.curframe = 0;
}
}
//start rendering
if(d3ddev->BeginScene())
{
//erase the entire backgoround
d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);
//start sprite handler
sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);
//create vector to update sprite position
D3DXVECTOR3 position((float)mainCharacter.x, (float)mainCharacter.y, 0);
//configure the rect for the source file
RECT srcRect;
int columns = 2;
srcRect.left = (mainCharacter.curframe % columns) * mainCharacter.width;
srcRect.top = (mainCharacter.curframe / columns) * mainCharacter.height;
srcRect.right = srcRect.left + mainCharacter.width;
srcRect.bottom = srcRect.top + mainCharacter.height;
//draw the sprite
sprite_handler->Draw(
main_character,
&srcRect,
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);
}
void gameEnd(HWND hwnd)
{
if(back != NULL)
back->Release();
if(sprite_handler != NULL)
sprite_handler->Release();
if(main_character != NULL)
main_character->Release();
}
//SPRITE.H
Code:
#ifndef SPRITE_H
#define SPRITE_H
//sprite structure
typedef struct
{
int x, y;
int width, height;
int movex, movey;
int curframe, lastframe;
int animdelay, animcount;
} SPRITE;
//function prototypes
int Collision(SPRITE sprite1, SPRITE sprite2); //collision detection function
#endif
//SPRITE.CPP
Code:
#include "sprite.h"
#include <Windows.h>
#include <d3d9.h>
//collision function. tests for collision
int Collision(SPRITE sprite1, SPRITE sprite2)
{
RECT rect1;
rect1.left = sprite1.x + 1;
rect1.top = sprite1.y + 1;
rect1.right = sprite1.x + sprite1.width - 1;
rect1.bottom = sprite1.y + sprite1.height - 1;
RECT rect2;
rect2.left = sprite2.x + 1;
rect2.top = sprite2.y + 1;
rect2.right = sprite2.x + sprite2.width - 1;
rect2.bottom = sprite2.y + sprite2.height -1;
RECT dest;
return IntersectRect(&dest, &rect1, &rect2);
}