Code:
//main.cpp
//The seagulls have AI. They try to avoid JJ.
//JJ shoots water, which have a fixed lifetime.
//When you hit the seagulls, they explode and turn into
//falling corpses.
//system includes
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include <stdio.h>
//system defines
#define WIN32_LEAN_AND_MEAN
//custom includes
#include "defines.h" //global definitions
#include "bmp.h" //bmp file reader
#include "timer.h" //game timer
#include "csprite.h" //for clipped sprite class
#include "objects.h" //for object class
#include "objman.h" //for object manager
#include "view.h" //for viewpoint class
#include "random.h" //for random number generator
//defines
#define MAX_OBJECTS 32 //max number of objects in game
//globals
BOOL ActiveApp; //is this application active?
LPDIRECTDRAW lpDirectDrawObject=NULL; //direct draw object
LPDIRECTDRAWSURFACE lpPrimary=NULL; //primary surface
LPDIRECTDRAWPALETTE lpPrimaryPalette; //its palette
LPDIRECTDRAWSURFACE lpSecondary=NULL; //back buffer
LPDIRECTDRAWPALETTE lpSecondaryPalette; //its palette
LPDIRECTDRAWSURFACE lpBackground=NULL; //background image
CTimer Timer; //game timer
CBmpFileReader background; //background image
CBmpSpriteFileReader g_cSpriteImages; //sprite images
CBmpSpriteFileReader g_cFrgndImages; //foreground images
CObjectManager ObjectManager(MAX_OBJECTS); //object manager
CClippedSprite *g_pSprite[NUM_SPRITES]; //sprites
CViewPoint Viewpoint; //player viewpoint
CRandom Random; //random number generator
//helper functions
LPDIRECTDRAWPALETTE CreatePalette(LPDIRECTDRAWSURFACE surface);
BOOL InitDirectDraw(HWND hwnd);
HWND CreateDefaultWindow(char* name,HINSTANCE hInstance);
BOOL LoadJJSprite(){
BOOL result=TRUE;
result=result&&g_pSprite[JJ_OBJECT]->
load(&g_cSpriteImages,0,1,223);
result=result&&g_pSprite[JJ_OBJECT]->
load(&g_cSpriteImages,1,66,223);
result=result&&g_pSprite[JJ_OBJECT]->
load(&g_cSpriteImages,2,137,223);
result=result&&g_pSprite[JJ_OBJECT]->
load(&g_cSpriteImages,3,210,223);
return result;
} //LoadJJSprite
BOOL LoadSEAGULLSprite(){
BOOL result=TRUE;
result=result&&g_pSprite[SEAGULL_OBJECT]->
load(&g_cSpriteImages,0,105,106); //frame 0
result=result&&g_pSprite[SEAGULL_OBJECT]->
load(&g_cSpriteImages,1,105,106); //frame 1
result=result&&g_pSprite[SEAGULL_OBJECT]->
load(&g_cSpriteImages,2,105,106); //frame 2
result=result&&g_pSprite[SEAGULL_OBJECT]->
load(&g_cSpriteImages,3,105,106); //frame 3
return result;
} //LoadCrowSprite
BOOL LoadFrgndSprites(){ //load foreground sprites
BOOL result=TRUE;
result=result&&g_pSprite[FARM_OBJECT]->
load(&g_cFrgndImages,0,0,0); //load farm sprite
result=result&&g_pSprite[FIELD_OBJECT]->
load(&g_cFrgndImages,0,640,0); //load field sprite
return result;
} //LoadFrgndSprites
BOOL LoadDEADSEAGULLSprite(){ //load dead crow
return g_pSprite[DEADSEAGULL_OBJECT]->
load(&g_cSpriteImages,0,199,110);
} //LoadDeadCrowSprite
BOOL LoadExplodingSEAGULLSprite(){ //load exploding crow
BOOL result=TRUE;
result=result&&g_pSprite[EXPLODINGSEAGULL_OBJECT]->
load(&g_cSpriteImages,0,132,42);
result=result&&g_pSprite[EXPLODINGSEAGULL_OBJECT]->
load(&g_cSpriteImages,1,199,42);
result=result&&g_pSprite[EXPLODINGSEAGULL_OBJECT]->
load(&g_cSpriteImages,2,132,110);
result=result&&g_pSprite[EXPLODINGSEAGULL_OBJECT]->
load(&g_cSpriteImages,3,453,162);
result=result&&g_pSprite[EXPLODINGSEAGULL_OBJECT]->
load(&g_cSpriteImages,4,199,110);
return result;
} //LoadExplodingCrowSprite
BOOL LoadWATERSprite(){ //load bullet
return g_pSprite[WATER_OBJECT]->
load(&g_cSpriteImages,0,291,265);
} //LoadBulletSprite
BOOL LoadImages(){ //load graphics from files to surfaces
//get the background image
if(!background.load("London3.bmp"))return FALSE; //read from file
background.draw(lpBackground); //draw to background surface
//set palettes in all surfaces
if(!background.setpalette(lpPrimaryPalette))return FALSE;
if(!background.setpalette(lpSecondaryPalette))return FALSE;
//load the sprites...
if(!g_cSpriteImages.load("sprites.bmp"))return FALSE;
//...the plane
//**********TAKEN OUT FOR NOW CLIPPING **********
// 0g_pSprite[JJ_OBJECT]=new CClippedSprite(6,1,223);
// if(!LoadJJSprite())return FALSE; //load plane images
//...the crow
//g_pSprite[SEAGULL_OBJECT]=new CClippedSprite(4,58,37);
//if(!LoadSEAGULLSprite())return FALSE; //load crow images..the dead crow
//g_pSprite[DEADSEAGULL_OBJECT]=new CClippedSprite(1,62,53);
//LoadDeadSEAGULLSprite(); //load dead crow images
//...the exploding crow
//g_pSprite[EXPLODINGSEAGULL_OBJECT]=new CClippedSprite(6,62,53);
//LoadExplodingSEAGULLSprite(); //load exploding crow images
//...the bullet
//g_pSprite[WATER_OBJECT]=new CClippedSprite(1,5,3);
//LoadWaterSprite(); //load bullet images
//...the foreground sprites
//if(!g_cFrgndImages.load("farm.bmp"))return FALSE;
//g_pSprite[FARM_OBJECT]=new CClippedSprite(1,640,162);
//g_pSprite[FIELD_OBJECT]=new CClippedSprite(1,640,162);
//if(!LoadFrgndSprites())return FALSE; //load foreground
//return TRUE;
}
//LoadImages()
void CreateObjects(){
int i;
ObjectManager.create(FARM_OBJECT,0,SCREEN_HEIGHT-1,0,0);
ObjectManager.create(FIELD_OBJECT,SCREEN_WIDTH,
SCREEN_HEIGHT-1,0,0);
for(i=0; i<8; i++)
ObjectManager.create(SEAGULL_OBJECT,
Random.number(0,WORLD_WIDTH-1),
Random.number(100,400),-1,0);
ObjectManager.set_current(
ObjectManager.create(JJ_OBJECT,320,271,-1,0));
for(i=0; i<8; i++)
ObjectManager.create(SEAGULL_OBJECT,
Random.number(0,WORLD_WIDTH-1),
Random.number(100,400),-1,0);
} //CreateObjects
BOOL RestoreSurfaces(){ //restore all surfaces
BOOL result=TRUE;
if(SUCCEEDED(lpPrimary->Restore())) //if primary restored
result=result&&background.draw(lpPrimary)&& //redraw image
background.setpalette(lpPrimaryPalette); //set palette
else return FALSE;
if(SUCCEEDED(lpSecondary->Restore())) //if secondary restored
result=result&&background.draw(lpSecondary)&& //redraw image
background.setpalette(lpSecondaryPalette); //set palette
else return FALSE;
if(SUCCEEDED(lpBackground->Restore())) //if background restored
result=result&&background.draw(lpBackground); //redraw image
else return FALSE;
if(g_pSprite[JJ_OBJECT]->Restore()) //if plane restored
result=result&&LoadJJSprite(); //redraw image
else return FALSE;
if(g_pSprite[SEAGULL_OBJECT]->Restore()) //if crow restored
result=result&&LoadSEAGULLSprite(); //redraw image
else return FALSE;
if(g_pSprite[DEADSEAGULL_OBJECT]->Restore()) //if restored
result=result&&LoadDEADSEAGULLSprite(); //redraw image
else return FALSE;
if(g_pSprite[EXPLODINGSEAGULL_OBJECT]->Restore()) //if restored
result=result&&LoadExplodingSEAGULLSprite(); //redraw image
else return FALSE;
if(g_pSprite[WATER_OBJECT]->Restore()) //if restored
result=result&&LoadWATERSprite(); //redraw image
else return FALSE;
if(g_pSprite[FARM_OBJECT]->Restore()&& //if farm and ...
g_pSprite[FIELD_OBJECT]->Restore()) //... field restored
result=result&&LoadFrgndSprites(); //redraw image
else return FALSE;
return result;
} //RestoreSurfaces
BOOL PageFlip(){ //return TRUE if page flip succeeds
if(lpPrimary->Flip(NULL,DDFLIP_WAIT)==DDERR_SURFACELOST)
return RestoreSurfaces();
return TRUE;
} //PageFlip
BOOL ComposeFrame(){ //compose a frame of animation
Viewpoint.draw_background(lpBackground,lpSecondary,
ObjectManager.speed()); //draw scrolling background
ObjectManager.animate(lpSecondary); //draw objects
return TRUE;
} //ComposeFrame
BOOL ProcessFrame(){ //process a frame of animation
ComposeFrame(); //compose a frame in secondary surface
return PageFlip(); //flip video memory surfaces
} //ProcessFrame
BOOL keyboard_handler(WPARAM keystroke){ //keyboard handler
BOOL result=FALSE; //return TRUE if game is to end
switch(keystroke){
case VK_ESCAPE: result=TRUE; break; //exit game
case VK_UP: ObjectManager.accelerate(0,-1); break;
case VK_DOWN: ObjectManager.accelerate(0,1); break;
case VK_LEFT: ObjectManager.accelerate(-1,0); break;
case VK_RIGHT: ObjectManager.accelerate(1,0); break;
case VK_SPACE: ObjectManager.fire_gun(); break;
default: break;
}
return result;
} //keyboard_handler
//message handler (window procedure)
long CALLBACK WindowProc(HWND hwnd,UINT message,
WPARAM wParam,LPARAM lParam){
switch(message){
case WM_ACTIVATEAPP: ActiveApp=wParam; break;
case WM_CREATE: break;
case WM_KEYDOWN: //keyboard hit
if(keyboard_handler(wParam))DestroyWindow(hwnd);
break;
case WM_DESTROY: //end of game
if(lpDirectDrawObject!=NULL){ //if DD object exists
if(lpSecondary!=NULL) //if secondary surface exists
lpSecondary->Release(); //release secondary surface
if(lpPrimary!=NULL) //if primary surface exists
lpPrimary->Release(); //release primary surface
if(lpBackground!=NULL) //if background exists
lpBackground->Release(); //release background
for(int i=0; i<NUM_SPRITES; i++){ //for each sprite
if(g_pSprite[i]) //if sprite exists
g_pSprite[i]->Release(); //release sprite
delete g_pSprite[i]; //delete sprite
}
lpDirectDrawObject->Release(); //release DD object
}
ShowCursor(TRUE); //show the mouse cursor
PostQuitMessage(0); //and exit
break;
default: //default window procedure
return DefWindowProc(hwnd,message,wParam,lParam);
} //switch(message)
return 0L;
} //WindowProc
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow){
MSG msg; //current message
HWND hwnd; //handle to fullscreen window
hwnd=CreateDefaultWindow("directX demo 6",hInstance);
if(!hwnd)return FALSE;
//set up window
ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd);
SetFocus(hwnd); //allow input from keyboard
ShowCursor(FALSE); //hide the cursor
//init graphics
for(int i=0; i<NUM_SPRITES; i++) //null out sprites
g_pSprite[i]=NULL;
BOOL OK=InitDirectDraw(hwnd);//initialize DirectDraw
if(OK)OK=LoadImages(); //load images from disk
if(!OK){ //bail out if initialization failed
DestroyWindow(hwnd); return FALSE;
}
//start game timer
Timer.start();
//create objects
CreateObjects();
//message loop
while(TRUE)
if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)){
if(!GetMessage(&msg,NULL,0,0))return msg.wParam;
TranslateMessage(&msg); DispatchMessage(&msg);
}
else if(ActiveApp)ProcessFrame(); else WaitMessage();
} //WinMain
Thanks alot!