home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows Game Programming for Dummies (2nd Edition)
/
WinGamProgFD.iso
/
mac
/
Source
/
GPCHAP12
/
STARFERR.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
2002-04-29
|
38KB
|
1,452 lines
// STARFERR.CPP - Game Engine Demo - STAR FERRET!
// try and add stuff!
// remember to compile you need GPDUMB1.CPP and
// DirectDraw library DDRAW.LIB
// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#define INITGUID
#include <windows.h> // include important windows stuff
#include <windowsx.h>
#include <mmsystem.h>
#include <objbase.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <ddraw.h> // directX includes
#include "gpdumb1.h"
// DEFINES ////////////////////////////////////////////////
// defines for windows
#define WINDOW_CLASS_NAME "WINXCLASS" // class name
#define WINDOW_WIDTH 64 // size of window
#define WINDOW_HEIGHT 48
// star ferret constants
// vertical extents of players motion
#define MIN_STARFERRET_Y 300
#define MAX_STARFERRET_Y (480-32)
// weapons defines
#define MAX_PLASMA 8 // max number of plasma pulses
#define PLASMA_STATE_OFF 0 // this plasma is dead or off
#define PLASMA_STATE_ON 1 // this one is alive and in flight
// starfield defines
#define MAX_STARS 128
// asteroid field defines
#define MAX_ROCKS 48
#define ROCK_STATE_OFF 0 // this rock is dead or off
#define ROCK_STATE_ON 1 // this one is alive and in flight
#define ROCK_LARGE 0 // sizes of rock
#define ROCK_MEDIUM 1
#define ROCK_SMALL 2
// explosion defines
#define MAX_BURSTS 8
#define BURST_STATE_OFF 0 // this burst is dead or off
#define BURST_STATE_ON 1 // this one is alive
// defines for player states
#define PLAYER_STATE_DEAD 0
#define PLAYER_STATE_DYING 1
#define PLAYER_STATE_ALIVE 2
// PROTOTYPES /////////////////////////////////////////////
// game console
int Game_Init(void *parms=NULL);
int Game_Shutdown(void *parms=NULL);
int Game_Main(void *parms=NULL);
// helper functions for game logic
void Draw_Info(void);
void Start_Burst(int x, int y, int width, int height, int xv,int yv);
void Draw_Bursts(void);
void Move_Bursts(void);
void Delete_Bursts(void);
void Init_Bursts(void);
void Draw_Rocks(void);
void Start_Rock(int x, int y, int size,int xv, int yv);
void Move_Rocks(void);
void Delete_Rocks(void);
void Init_Rocks(void);
void Fire_Plasma(int x,int y, int vel);
void Draw_Plasma(void);
void Move_Plasma(void);
int Collision_Test(int x1, int y1, int w1, int h1,
int x2, int y2, int w2, int h2);
void Delete_Plasma(void);
void Init_Plasma(void);
void Move_Stars(void);
void Draw_Stars(void);
void Init_Stars(void);
// TYPES //////////////////////////////////////////////////
// used to contain a single star
typedef struct STAR_TYP
{
UCHAR color;
int x,y;
int velocity;
} STAR, *STAR_PTR;
// GLOBALS ////////////////////////////////////////////////
HWND main_window_handle = NULL; // save the window handle
HINSTANCE main_instance = NULL; // save the instance
char buffer[80]; // used to print text
BOB starferret; // the player
int moving_up = 0; // used to track if player is moving up for
// various parallax effects
BOB plasma[MAX_PLASMA]; // plasma pulses
BOB rocks[MAX_ROCKS]; // the asteroids
BOB bursts[MAX_BURSTS]; // the explosion bursts
int rock_sizes[3] = {96,32,12}; // width X height sizes for scaler
STAR stars[MAX_STARS]; // the star field
// player state variables
int player_state = PLAYER_STATE_ALIVE;
int player_score = 0; // the score
int player_ships = 5; // ships left
int player_damage = 0; // damage of player
// PROTOTYPES //////////////////////////////////////////////
// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
return(0);
} break;
case WM_PAINT:
{
// start painting
hdc = BeginPaint(hwnd,&ps);
// end painting
EndPaint(hwnd,&ps);
return(0);
} break;
case WM_DESTROY:
{
// kill the application
PostQuitMessage(0);
return(0);
} break;
default:break;
} // end switch
// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
// this is the winmain function
WNDCLASS winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
HDC hdc; // generic dc
PAINTSTRUCT ps; // generic paintstruct
// first fill in the window class stucture
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(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
// register the window class
if (!RegisterClass(&winclass))
return(0);
// create the window, note the use of WS_POPUP
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
"WinX Game Console", // title
WS_POPUP | WS_VISIBLE,
0,0, // x,y
WINDOW_WIDTH, // width
WINDOW_HEIGHT, // height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance
NULL))) // creation parms
return(0);
// save the window handle and instance in a global
main_window_handle = hwnd;
main_instance = hinstance;
// perform all game console specific initialization
Game_Init();
// enter main event loop
while(1)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// test if this is a quit
if (msg.message == WM_QUIT)
break;
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
} // end if
// main game processing goes here
Game_Main();
} // end while
// shutdown game and release all resources
Game_Shutdown();
// return to Windows like this
return(msg.wParam);
} // end WinMain
///////////////////////////////////////////////////////////
void Init_Stars(void)
{
// this function initializes all the stars in such a way
// that their intensity is proportional to their
// velocity
for (int index=0; index<MAX_STARS; index++)
{
// random postion
stars[index].x = rand()%screen_width;
stars[index].y = rand()%screen_height;
// select star plane
int plane = rand()%4; // (1..4)
// based on plane select velocity and color
stars[index].velocity = -(1 + plane*2);
stars[index].color = 25 - (plane*3);
} // end for index
} // end Init_Stars
///////////////////////////////////////////////////////////
void Move_Stars(void)
{
// this function moves all the stars
for (int index=0; index<MAX_STARS; index++)
{
// translate upward
if (!moving_up)
stars[index].y-=stars[index].velocity;
else
stars[index].y-=(stars[index].velocity+((moving_up*stars[index].velocity)));
// test for collision with top of screen
if (stars[index].y >= screen_height)
stars[index].y-=screen_height;
} // end for index
} // end Move_Stars
///////////////////////////////////////////////////////////
void Draw_Stars(void)
{
// this function draws all the stars
// lock back surface
DD_Lock_Back_Surface();
// draw all the stars
for (int index=0; index<MAX_STARS; index++)
{
// draw stars
Draw_Pixel(stars[index].x,stars[index].y, stars[index].color,back_buffer, back_lpitch);
} // end for index
// unlock the secondary surface
DD_Unlock_Back_Surface();
} // end Draw_Stars
///////////////////////////////////////////////////////////
void Init_Plasma(void)
{
// this function initializes and loads all the plasma
// weapon pulses
// load the plasma imagery
Load_Bitmap_File(&bitmap8bit, "PLASMA8.BMP");
#if 1
// now create and load each plasma pulse
for (int pulse=0; pulse<MAX_PLASMA; pulse++)
{
// create the bob to hold pulse
Create_BOB(&plasma[pulse],0,0,16,24,6,
BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_FRAME,
DDSCAPS_SYSTEMMEMORY);
// load animation frames
for (int frame=0; frame<6; frame++)
Load_Frame_BOB(&plasma[pulse],&bitmap8bit,frame,frame,0,BITMAP_EXTRACT_MODE_CELL);
// set animation rate
Set_Anim_Speed_BOB(&plasma[pulse],1);
// set state to off
plasma[pulse].state = PLASMA_STATE_OFF;
} // end for pulse
#endif
#if 0
// create the first bob
Create_BOB(&plasma[0],0,0,16,24,6,
BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_FRAME,
DDSCAPS_SYSTEMMEMORY);
// load animation frames
for (int frame=0; frame<6; frame++)
Load_Frame_BOB(&plasma[0],&bitmap8bit,frame,frame,0,BITMAP_EXTRACT_MODE_CELL);
// set animation rate
Set_Anim_Speed_BOB(&plasma[0],1);
// set state to off
plasma[0].state = PLASMA_STATE_OFF;
for (int pulse=1; pulse<MAX_PLASMA; pulse++)
{
memcpy(&plasma[pulse], &plasma[0], sizeof(BOB));
} // end for pulse
#endif
// unload data infile
Unload_Bitmap_File(&bitmap8bit);
} // end Init_Plasma
///////////////////////////////////////////////////////////
void Delete_Plasma(void)
{
// this function simply deletes all memory and surfaces
// related to the plasma pulses
for (int index=0; index<MAX_PLASMA; index++)
Destroy_BOB(&plasma[index]);
} // end Delete_Plasma
///////////////////////////////////////////////////////////
int Collision_Test(int x1, int y1, int w1, int h1,
int x2, int y2, int w2, int h2)
{
// this function tests if the two rects overlap
// get the radi of each rect
int width1 = (w1>>1) - (w1>>3);
int height1 = (h1>>1) - (h1>>3);
int width2 = (w2>>1) - (w2>>3);
int height2 = (h2>>1) - (h2>>3);
// compute center of each rect
int cx1 = x1 + width1;
int cy1 = y1 + height1;
int cx2 = x2 + width2;
int cy2 = y2 + height2;
// compute deltas
int dx = abs(cx2 - cx1);
int dy = abs(cy2 - cy1);
// test if rects overlap
if (dx < (width1+width2) && dy < (height1+height2))
return(1);
else
// else no collision
return(0);
} // end Collision_Test
///////////////////////////////////////////////////////////
void Move_Plasma(void)
{
// this function moves all the plasma pulses and checks for
// collision with the rocks
for (int index=0; index<MAX_PLASMA; index++)
{
// test if plasma pulse is in flight
if (plasma[index].state == PLASMA_STATE_ON)
{
// move the pulse upward
Move_BOB(&plasma[index]);
// test for boundaries
if (plasma[index].y < 0 )
{
// kill the pulse
plasma[index].state = PLASMA_STATE_OFF;
// move to next pulse
continue;
} // end if
// test for collision with rocks
for (int rock=0; rock<MAX_ROCKS; rock++)
{
if (rocks[rock].state==ROCK_STATE_ON)
{
// test for collision
if (Collision_Test(plasma[index].x, plasma[index].y,
plasma[index].width, plasma[index].height,
rocks[rock].x, rocks[rock].y,
rocks[rock].varsI[1], rocks[rock].varsI[1]))
{
// kill pulse
plasma[index].state = PLASMA_STATE_OFF;
// start explosion
Start_Burst(plasma[index].x, plasma[index].y,
42,36,
rocks[rock].xv*.5, rocks[rock].yv*.5);
// update score
player_score+=rocks[rock].varsI[2];
// test strength of rock, cause damage
rocks[rock].varsI[2]-=50;
// split test
if (rocks[rock].varsI[2] > 0 && rocks[rock].varsI[2] < 50)
{
// test the size of rock
switch(rocks[rock].varsI[0])
{
case ROCK_LARGE:
{
// split into two medium
Start_Rock(rocks[rock].x+rand()%16,rocks[rock].y+rand()%16,
ROCK_MEDIUM,
rocks[rock].xv-2+rand()%4,rocks[rock].yv-2+rand()%4);
Start_Rock(rocks[rock].x+rand()%16,rocks[rock].y+rand()%16,
ROCK_MEDIUM,
rocks[rock].xv-2+rand()%4,rocks[rock].yv-2+rand()%4);
// throw in a small?
if ((rand()%3)==1)
Start_Rock(rocks[rock].x+rand()%16,rocks[rock].y+rand()%16,
ROCK_SMALL,
rocks[rock].xv-2+rand()%4,rocks[rock].yv-2+rand()%4);
// kill the original
rocks[rock].state = ROCK_STATE_OFF;
} break;
case ROCK_MEDIUM:
{
// split into 1 - 3 small
int num_rocks = 1+rand()%3;
for (; num_rocks >=1; num_rocks--)
{
Start_Rock(rocks[rock].x+rand()%8,rocks[rock].y+rand()%8,
ROCK_SMALL,
rocks[rock].xv-2+rand()%4,rocks[rock].yv-2+rand()%4);
} // end for num_rocks
// kill the original
rocks[rock].state = ROCK_STATE_OFF;
} break;
case ROCK_SMALL:
{
// just kill it
rocks[rock].state = ROCK_STATE_OFF;
} break;
default:break;
} // end switch
} // end if split
else
if (rocks[rock].varsI[2] <= 0)
{
// kill rock
rocks[rock].state = ROCK_STATE_OFF;
} // end else
// break out of loop
break;
} // end if collision
} // end if rock alive
} // end for rock
} // end if
} // end for index
} // end Move_Plasma
///////////////////////////////////////////////////////////
void Draw_Plasma(void)
{
// this function draws all the plasma pulses
for (int index=0; index<MAX_PLASMA; index++)
{
// test if plasma pulse is in flight
if (plasma[index].state == PLASMA_STATE_ON)
{
// draw the pulse
Draw_BOB(&plasma[index],lpddsback);
// animate the pulse
Animate_BOB(&plasma[index]);
} // end if
} // end for index
} // end Draw_Plasma
///////////////////////////////////////////////////////////
void Fire_Plasma(int x,int y, int vel)
{
// this function fires a plasma pulse at the given starting
// position and velocity, of course, one must be free for
// this to work
// scan for a pulse that is available
for (int index=0; index<MAX_PLASMA; index++)
{
// is this one available
// test if plasma pulse is in flight
if (plasma[index].state == PLASMA_STATE_OFF)
{
// start this one up
plasma[index].x = x;
plasma[index].y = y;
plasma[index].yv = -vel;
plasma[index].curr_frame = 0;
plasma[index].state = PLASMA_STATE_ON;
// later
return;
} // end if
} // end for
} // end Fire_Plasma
///////////////////////////////////////////////////////////
void Init_Rocks(void)
{
// this function initializes and loads all the rocks
// load the rocks imagery
Load_Bitmap_File(&bitmap8bit, "ROCKS8.BMP");
// create the first bob
Create_BOB(&rocks[0],0,0,96,96,20,
BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_FRAME,
DDSCAPS_SYSTEMMEMORY);
// load animation frames
for (int frame=0; frame < 20; frame++)
Load_Frame_BOB(&rocks[0],&bitmap8bit,frame,frame%6,frame/6,BITMAP_EXTRACT_MODE_CELL);
// set animation rate
Set_Anim_Speed_BOB(&rocks[0],1+rand()%5);
Set_Vel_BOB(&rocks[0], -4+rand()%8, 4+rand()%4);
Set_Pos_BOB(&rocks[0], rand()%screen_width, rand()%screen_height);
// set size of rock
rocks[0].varsI[0] = rand()%3;
rocks[0].varsI[1] = rock_sizes[rocks[0].varsI[0]];
// set state to off
rocks[0].state = ROCK_STATE_OFF;
for (int rock=1; rock<MAX_ROCKS; rock++)
{
memcpy(&rocks[rock], &rocks[0], sizeof(BOB));
// set animation rate
Set_Anim_Speed_BOB(&rocks[rock],1+rand()%5);
// set velocity
Set_Vel_BOB(&rocks[rock], -4+rand()%8, 4+rand()%4);
// set position
Set_Pos_BOB(&rocks[rock], rand()%screen_width, rand()%screen_height);
// set size of rock
rocks[rock].varsI[0] = rand()%3;
rocks[rock].varsI[1] = rock_sizes[rocks[rock].varsI[0]];
} // end for rock
// unload data infile
Unload_Bitmap_File(&bitmap8bit);
} // end Init_Rocks
///////////////////////////////////////////////////////////
void Delete_Rocks(void)
{
// this function simply deletes all memory and surfaces
// related to the rocks pulses
for (int index=0; index<MAX_ROCKS; index++)
Destroy_BOB(&rocks[index]);
} // end Delete_Rocks
///////////////////////////////////////////////////////////
void Move_Rocks(void)
{
// this function moves all the rocks pulses and checks for
// collision with the rocks
for (int index=0; index<MAX_ROCKS; index++)
{
// test if rocks pulse is in flight
if (rocks[index].state == ROCK_STATE_ON)
{
// move the rock
Move_BOB(&rocks[index]);
// add player motion in
if (moving_up)
rocks[index].y+=(rocks[index].yv*.5);
// test if rock is off screen
if (rocks[index].y > screen_height)
{
// kill rock and put back on available list
rocks[index].state = ROCK_STATE_OFF;
} // end if
} // end if
} // end for index
// now test if it's time to add a new rock to the list
if ((rand()%6)==3)
{
// scan for a rock to initialize
for (index=0; index<MAX_ROCKS; index++)
{
// is this rock available?
if (rocks[index].state == ROCK_STATE_OFF)
{
// set animation rate
Set_Anim_Speed_BOB(&rocks[index],1+rand()%5);
// set velocity
Set_Vel_BOB(&rocks[index], -4+rand()%8, 4+rand()%12);
// set position
Set_Pos_BOB(&rocks[index], rand()%screen_width, -128);
// set size of rock
rocks[index].varsI[0] = rand()%3;
rocks[index].varsI[1] = rock_sizes[rocks[index].varsI[0]];
// set strength of rock
switch(rocks[index].varsI[0])
{
case ROCK_LARGE:
{
// set hardness of rock
rocks[index].varsI[2] = 100+rand()%100;
} break;
case ROCK_MEDIUM:
{
// set hardness of rock
rocks[index].varsI[2] = 40 + rand()%30;
} break;
case ROCK_SMALL:
{
// set hardness of rock
rocks[index].varsI[2] = 10;
} break;
default:break;
} // end switch
// turn rock on
rocks[index].state = ROCK_STATE_ON;
// later
return;
} // end if
} // end for index
} // end if
} // end Move_Rocks
///////////////////////////////////////////////////////////
void Start_Rock(int x, int y, int size,int xv, int yv)
{
// this functions starts a rock up with the sent parms, considering
// there is one left
// scan for a rock to initialize
for (int index=0; index<MAX_ROCKS; index++)
{
// is this rock available?
if (rocks[index].state == ROCK_STATE_OFF)
{
// set animation rate
Set_Anim_Speed_BOB(&rocks[index],1+rand()%5);
// set velocity
Set_Vel_BOB(&rocks[index], xv,yv);
// set position
Set_Pos_BOB(&rocks[index], x,y);
// set size of rock
rocks[index].varsI[0] = size;
rocks[index].varsI[1] = rock_sizes[rocks[index].varsI[0]];
// set strength of rock
switch(rocks[index].varsI[0])
{
case ROCK_LARGE:
{
// set hardness of rock
rocks[index].varsI[2] = 100+rand()%100;
} break;
case ROCK_MEDIUM:
{
// set hardness of rock
rocks[index].varsI[2] = 40 + rand()%30;
} break;
case ROCK_SMALL:
{
// set hardness of rock
rocks[index].varsI[2] = 10;
} break;
default:break;
} // end switch
// turn rock on
rocks[index].state = ROCK_STATE_ON;
// later
return;
} // end if
} // end for index
} // end Start_Rock
///////////////////////////////////////////////////////////
void Draw_Rocks(void)
{
// this function draws all the rocks
for (int index=0; index<MAX_ROCKS; index++)
{
// test if rocks pulse is in flight
if (rocks[index].state == ROCK_STATE_ON)
{
if (rocks[index].varsI[0]!=ROCK_LARGE)
{
// draw the rock scaled
Draw_Scaled_BOB(&rocks[index],
rocks[index].varsI[1],rocks[index].varsI[1],
lpddsback);
}
else // draw normal
Draw_BOB(&rocks[index],lpddsback);
// animate the pulse
Animate_BOB(&rocks[index]);
} // end if
} // end for index
} // end Draw_Rocks
///////////////////////////////////////////////////////////
void Init_Bursts(void)
{
// this function initializes and loads all the bursts
// load the bursts imagery
Load_Bitmap_File(&bitmap8bit, "EXPL8.BMP");
// create the first bob
Create_BOB(&bursts[0],0,0,42,36,14,
BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_FRAME,
DDSCAPS_SYSTEMMEMORY);
// load animation frames
for (int frame=0; frame < 14; frame++)
Load_Frame_BOB(&bursts[0],&bitmap8bit,frame,frame%6,frame/6,BITMAP_EXTRACT_MODE_CELL);
// set animation rate
Set_Anim_Speed_BOB(&bursts[0],1);
// set size of burst
bursts[0].varsI[0] = bursts[0].width;
bursts[0].varsI[1] = bursts[0].height;
// set state to off
bursts[0].state = BURST_STATE_OFF;
for (int burst=1; burst<MAX_BURSTS; burst++)
{
memcpy(&bursts[burst], &bursts[0], sizeof(BOB));
} // end for burst
// unload data infile
Unload_Bitmap_File(&bitmap8bit);
} // end Init_Bursts
///////////////////////////////////////////////////////////
void Delete_Bursts(void)
{
// this function simply deletes all memory and surfaces
// related to the bursts pulses
for (int index=0; index<MAX_BURSTS; index++)
Destroy_BOB(&bursts[index]);
} // end Delete_Bursts
///////////////////////////////////////////////////////////
void Move_Bursts(void)
{
// this function moves all the bursts
for (int index=0; index<MAX_BURSTS; index++)
{
// test if bursts pulse is in moving
if (bursts[index].state == BURST_STATE_ON)
{
// move the burst
Move_BOB(&bursts[index]);
// add player motion in
if (moving_up)
bursts[index].y+=(bursts[index].yv*.5);
// test if burst is off screen or done with animation
if ((bursts[index].y > screen_height) || bursts[index].curr_frame >= bursts[index].num_frames-1)
{
// kill burst and put back on available list
bursts[index].state = BURST_STATE_OFF;
} // end if
} // end if
} // end for index
} // end Move_Bursts
///////////////////////////////////////////////////////////
void Draw_Bursts(void)
{
// this function draws all the bursts
for (int index=0; index<MAX_BURSTS; index++)
{
// test if bursts pulse is in flight
if (bursts[index].state == BURST_STATE_ON)
{
if (bursts[index].varsI[0]!=bursts[index].width ||
bursts[index].varsI[1]!=bursts[index].height)
{
// draw the burst scaled
Draw_Scaled_BOB(&bursts[index],
bursts[index].varsI[0],bursts[index].varsI[1],
lpddsback);
}
else // draw normal
Draw_BOB(&bursts[index],lpddsback);
// animate the explosion
Animate_BOB(&bursts[index]);
} // end if
} // end for index
} // end Draw_Bursts
///////////////////////////////////////////////////////////
void Start_Burst(int x, int y, int width, int height, int xv,int yv)
{
// this function starts a burst up
// now test if it's time to add a new burst to the list
// scan for a burst to initialize
for (int index=0; index<MAX_BURSTS; index++)
{
// is this burst available?
if (bursts[index].state == BURST_STATE_OFF)
{
// set animation rate
Set_Anim_Speed_BOB(&bursts[index],1);
bursts[index].curr_frame = 0;
// set velocity
Set_Vel_BOB(&bursts[index], xv,yv);
// set position
Set_Pos_BOB(&bursts[index], x,y);
// set size of burst
bursts[index].varsI[0] = width;
bursts[index].varsI[1] = height;
// turn burst on
bursts[index].state = BURST_STATE_ON;
// later
return;
} // end if
} // end for index
} // end Start_Burst
///////////////////////////////////////////////////////////
void Draw_Info(void)
{
// this function draws all the information at the top of the screen
char score[16]; // hold score
// build up scrore string
sprintf(score,"0000000%d",player_score);
// build up final string
sprintf(buffer,"SCORE %s",&score[strlen(score)-8]);
Draw_Text_GDI(buffer,10,10,RGB(0,255,0),lpddsback);
// draw damage
sprintf(buffer,"DAMAGE %d%%",player_damage);
Draw_Text_GDI(buffer,320-8*strlen(buffer),10,RGB(0,255,0),lpddsback);
// draw ships
sprintf(buffer,"SHIPS %d",player_ships);
Draw_Text_GDI(buffer,500,10,RGB(0,255,0),lpddsback);
} // end Draw_Info
///////////////////////////////////////////////////////////
void Do_Intro(void)
{
// the worlds simples intro
int shade; // used as looping var
for (shade=0; shade < 256; shade++)
{
// draw text in shades
Draw_Text_GDI("DUMB GAMES INC.",320-4*strlen("DUMB GAMES INC."),200,
RGB(shade,shade,shade),lpddsprimary);
Sleep(2);
// is user trying to bail
if (KEY_DOWN(VK_ESCAPE))
return;
} // end for shade
Sleep(2000);
for (shade=255; shade >=0; shade--)
{
// draw text in shades
Draw_Text_GDI("DUMB GAMES INC.",320-4*strlen("DUMB GAMES INC."),200,
RGB(0,shade,0),lpddsprimary);
Sleep(2);
// is user trying to bail
if (KEY_DOWN(VK_ESCAPE))
return;
} // end for shade
Sleep(1000);
for (shade=0; shade < 256; shade++)
{
// draw text in shades
Draw_Text_GDI("PRESENTS...",320-4*strlen("PRESENTS..."),200,
RGB(shade,shade,shade),lpddsprimary);
Sleep(2);
// is user trying to bail
if (KEY_DOWN(VK_ESCAPE))
return;
} // end for shade
Sleep(2000);
for (shade=255; shade >=0; shade--)
{
// draw text in shades
Draw_Text_GDI("PRESENTS...",320-4*strlen("PRESENTS..."),200,
RGB(0,shade,0),lpddsprimary);
Sleep(2);
// is user trying to bail
if (KEY_DOWN(VK_ESCAPE))
return;
} // end for shade
Sleep(2000);
for (shade=0; shade < 256; shade++)
{
// draw text in shades
Draw_Text_GDI("S T A R F E R R E T !",320-4*strlen("S T A R F E R R E T !"),200,
RGB(shade,shade,shade),lpddsprimary);
Sleep(2);
// is user trying to bail
if (KEY_DOWN(VK_ESCAPE))
return;
} // end for shade
Sleep(3000);
for (shade=255; shade >=0; shade--)
{
// draw text in shades
Draw_Text_GDI("S T A R F E R R E T !",320-4*strlen("S T A R F E R R E T !"),200,
RGB(0,shade,0),lpddsprimary);
Sleep(2);
// is user trying to bail
if (KEY_DOWN(VK_ESCAPE))
return;
} // end for shade
} // end Do_Intro
// WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
int Game_Init(void *parms)
{
// this function is where you do all the initialization
// for your game
int index; // looping var
char filename[80]; // used to build up files names
// set screen stuff
screen_width = 640;
screen_height = 480;
screen_bpp = 8;
// initialize directdraw
DD_Init(screen_width, screen_height, screen_bpp);
// load the starferret ship
Load_Bitmap_File(&bitmap8bit, "STAR8.BMP");
// set the palette to background image palette
Set_Palette(bitmap8bit.palette);
// now create the starferret
Create_BOB(&starferret,0,0,56,38,3,
BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_FRAME,
DDSCAPS_SYSTEMMEMORY);
// load star ferret frames
for (index=0; index<3; index++)
Load_Frame_BOB(&starferret,&bitmap8bit,index,index,0,BITMAP_EXTRACT_MODE_CELL);
// set position
Set_Pos_BOB(&starferret,320,350);
// set animation speed
Set_Anim_Speed_BOB(&starferret,1);
// unload data infile
Unload_Bitmap_File(&bitmap8bit);
// initialize the plasma pulses
Init_Plasma();
// initialize all the stars
Init_Stars();
// init rocks
Init_Rocks();
// init all the explosions
Init_Bursts();
#if 0
// load the background
Load_Bitmap_File(&bitmap8bit, "MUSH.BMP");
// set the palette to background image palette
Set_Palette(bitmap8bit.palette);
// load in the four frames of the mushroom
for (index=0; index<4; index++)
{
// create mushroom bitmaps
Create_Bitmap(&mushrooms[index],0,0,32,32);
Load_Image_Bitmap(&mushrooms[index],&bitmap8bit,index,0,BITMAP_EXTRACT_MODE_CELL);
} // end for index
// now create the bug blaster bob
Create_BOB(&blaster,0,0,32,32,3,
BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_ANIM | BOB_ATTR_ANIM_ONE_SHOT,
DDSCAPS_SYSTEMMEMORY);
// load in the four frames of the mushroom
for (index=0; index<3; index++)
Load_Frame_BOB(&blaster,&bitmap8bit,index,index,1,BITMAP_EXTRACT_MODE_CELL);
// unload the bitmap file
Unload_Bitmap_File(&bitmap8bit);
// set the animation sequences for bug blaster
Load_Animation_BOB(&blaster,0,5,blaster_anim);
// set up stating state of bug blaster
Set_Pos_BOB(&blaster,320, 200);
Set_Anim_Speed_BOB(&blaster,1);
#endif
// set clipping rectangle to screen extents so objects dont
// mess up at edges
RECT screen_rect = {0,0,screen_width,screen_height};
lpddclipper = DD_Attach_Clipper(lpddsback,1,&screen_rect);
// seed random number generate
srand(Start_Clock());
// hide the mouse
ShowCursor(FALSE);
// do the introdcution
Do_Intro();
// return success
return(1);
} // end Game_Init
///////////////////////////////////////////////////////////
int Game_Shutdown(void *parms)
{
// this function is where you shutdown your game and
// release all resources that you allocated
// delete all the explosions
Delete_Bursts();
// delete the player
Destroy_BOB(&starferret);
// the delete all the rocks
Delete_Rocks();
// delete all the plasma pulses
Delete_Plasma();
// shutdonw directdraw
DD_Shutdown();
// return success
return(1);
} // end Game_Shutdown
///////////////////////////////////////////////////////////
int Game_Main(void *parms)
{
// this is the workhorse of your game it will be called
// continuously in real-time this is like main() in C
// all the calls for you game go here!
static int ready_counter = 0, // used to draw a little "get ready"
ready_state = 0;
int index; // looping var
int dx,dy; // general deltas used in collision detection
// check of user is trying to exit
if (KEY_DOWN(VK_ESCAPE) && ready_state)
PostMessage(main_window_handle, WM_DESTROY,0,0);
// start the timing clock
Start_Clock();
// reset upward motion flag
moving_up = 0;
// clear the drawing surface
DD_Fill_Surface(lpddsback, 0);
// only process player if alive
if (player_state == PLAYER_STATE_ALIVE)
{
// test if player is moving
if (KEY_DOWN(VK_RIGHT))
{
// move player to right
starferret.x+=8;
} // end if
else
if (KEY_DOWN(VK_LEFT))
{
// move player to left
starferret.x-=8;
} // end if
// vertical/speed motion
if (KEY_DOWN(VK_UP))
{
// move player up
starferret.y-=4;
// set upward motion flag
moving_up = 1;
} // end if
else
if (KEY_DOWN(VK_DOWN))
{
// move player to left
starferret.y+=2;
} // end if
// test if player is firing
if (KEY_DOWN(VK_CONTROL) || KEY_DOWN(VK_SPACE))
Fire_Plasma(starferret.x+28-8,starferret.y-8,16);
// apply friction, or downward motion
starferret.y++;
// do bounds check
// first vertical axis
if (starferret.y < MIN_STARFERRET_Y)
starferret.y = MIN_STARFERRET_Y;
else
if (starferret.y > MAX_STARFERRET_Y)
starferret.y = MAX_STARFERRET_Y;
// now horizontal axis
if (starferret.x < 0)
starferret.x = 0;
else
if (starferret.x > (screen_width - starferret.width))
starferret.x = (screen_width - starferret.width);
// animate player
Animate_BOB(&starferret);
} // end if player alive
else
if (player_state = PLAYER_STATE_DYING)
{
// player is dying
} // end if
else
if (player_state = PLAYER_STATE_DEAD)
{
// player is dead
} // end if
// move the plasma
Move_Plasma();
// move the asteroids
Move_Rocks();
// move the stars
Move_Stars();
// move the explosions
Move_Bursts();
// draw the tocks
Draw_Rocks();
// draw the plasma
Draw_Plasma();
// draw the player if alive
if (player_state == PLAYER_STATE_ALIVE)
{
Draw_BOB(&starferret,lpddsback);
} // end if
// draw the stars
Draw_Stars();
// draw explosions last
Draw_Bursts();
// draw the score and ships left
Draw_Info();
// draw get ready?
if (!ready_state)
{
// draw text
Draw_Text_GDI("GET READY!",320-8*strlen("GET READY!")/2, 200,RGB(0,0,255),lpddsback);
// increment counter
if (++ready_counter > 60)
{
// set state to ready
ready_state = 1;
ready_counter = 0;
} // end if
} // end if
// flip the surfaces
DD_Flip();
// sync to 30ish fps
Wait_Clock(30);
// return success
return(1);
} // end Game_Main
//////////////////////////////////////////////////////////