📄 invasion.cpp
字号:
{
if(pTexture->pbyData)
delete pTexture->pbyData;
return true;
}
ijlFree(&jcProps);
return false;
} // end ASLoadJpegRGB()
void ASLoadTextures(char (*byFilename)[256], int iTextures, AS_TEXTURE *pTexture)
{ // begin ASLoadTextures()
AS_TEXTURE *pTextureT;
for(int i = 0; i < iTextures; i++)
{
pTextureT = &pTexture[i];
strcpy(pTextureT->byFilename, byFilename[i]);
ASLoadJpegRGB(pTextureT, pTextureT->byFilename);
}
} // begin ASLoadTextures()
void ASDestroyTextures(int iTextures, AS_TEXTURE *Texture)
{ // begin ASDestroyTextures()
for(int i = 0; i < iTextures; i++)
{
if(Texture[i].pbyData)
Texture[i].pbyData;
}
} // end ASDestroyTextures()
void ASGenOpenGLTextures(int iTextures, AS_TEXTURE *Texture)
{ // begin ASGenOpenGLTextures()
AS_TEXTURE *pTextureT;
if(!Texture[0].pbyData)
return;
for(int i = 0; i < iTextures; i++)
{
pTextureT = &Texture[i];
glGenTextures(1, &pTextureT->iOpenGLID);
glBindTexture(GL_TEXTURE_2D, pTextureT->iOpenGLID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, pTextureT->byColorComponents, pTextureT->iWidth,
pTextureT->iHeight, pTextureT->iFormat, GL_UNSIGNED_BYTE,
pTextureT->pbyData);
}
} // end ASGenOpenGLTextures()
void ASDestroyOpenGLTextures(int iTextures, AS_TEXTURE *Texture)
{ // begin ASDestroyOpenGLTextures()
if(!Texture[0].pbyData)
return;
for(int i = 0; i < iTextures; i++)
glDeleteTextures(1, &Texture[i].iOpenGLID);
} // end ASDestroyOpenGLTextures()
void StartLevel(LEVEL *pLevelT, BOOL bPlayer, BOOL bInitalize)
{
ACTOR *pActor;
short x, y, i;
pLevel = pLevelT;
// Set The Aliens
for(y = 0; y < MAX_ALIEN_ROW; y++)
for(x = 0; x < MAX_ALIEN_COLUMN; x++)
{
pActor = &pAlien[y*MAX_ALIEN_COLUMN+x];
if(!pLevel->byAliens[y][x])
{
pActor->bActive = FALSE;
continue;
}
InitActor(pActor);
pActor->fPos[X] = (float) 5+(x+pLevel->byXStart)*10;
pActor->fPos[Y] = SPACE_HEIGHT-(5+(y+pLevel->byYStart)*10);
pActor->fPosVelocity[X] = pLevel->fXStartVelocity;
pActor->fPosVelocity[Y] = pLevel->fYStartVelocity;
pActor->iType = pLevel->byAliens[y][x];
pActor->byLives = 0;
if(pActor->iType == ALIEN_3)
pActor->byLives = 4; // This Alien Could Be 5 Times Hit
else
if(pActor->iType == BIG_BOSS)
pActor->byLives = 100;
pActor->fPos2[Y] = SPACE_HEIGHT;
}
if(!bPlayer)
return;
InitActor(&pPlayer);
pPlayer.iType = PLAYER;
bGameOver = FALSE;
if(!bInitalize)
return;
// Init Objects
for(i = 0; i < MAX_OBJECTS; i++)
pObject[i].bActive = FALSE;
SetWalls();
// We Begin A New Game
byPlayerWeapon = SINGLE_LASER_WEAPON;
pPlayer.byLives = 3;
pPlayer.fPower = pPlayer.fMaxPower = MAX_PLAYER_POWER;
iScore = 0;
byWormholeAniStep = 0;
fWormholeHitPoints = 100.0f;
for(i = 0; i < MAX_SHOTS; i++)
pShot[i].bActive = FALSE;
bGameWon = FALSE;
}
void StartNewGame(void)
{
bGameIntro = FALSE;
bPlayerEnteringHighScore = FALSE;
byLevel = byStartLevel;
StartLevel(pLevels[byLevel], TRUE, TRUE);
lLastPlayerShotTime = g_lNow;
}
HRESULT LoadHighScores(char *pbyFilename)
{
FILE *pFile = NULL;
if(!pbyFilename)
return 1;
pFile = fopen(pbyFilename,"r");
if(!pFile)
return 1;
fread(Highscore, sizeof(HIGHSCORE), MAX_HIGHSCORES, pFile);
fclose(pFile);
return 0;
}
HRESULT SaveHighScores(char *pbyFilename)
{
FILE *pFile = NULL;
if(!pbyFilename)
return 0;
pFile = fopen(pbyFilename,"w");
if(!pFile)
return 0;
fwrite(Highscore, sizeof(HIGHSCORE), MAX_HIGHSCORES, pFile);
fclose(pFile);
return 0;
}
void InitGameIntro(void)
{
fWormholeHitPoints = 100.0f;
fPlanetZScale = 2.0f;
pPlanet->SetAnimationT(0);
pPlanet->SetAnimationStepT(0);
fPlanetZScaleVelocity = 0.0f;
bGameIntro = TRUE;
bPlayerEnteringHighScore = FALSE;
pPlayer.byLives = 0;
bGameWon = FALSE;
}
void GameOver(void)
{
short i;
if(!bGameOver)
{
DSUtil_PlaySound(pInvasionSound, 0);
bGameOver = TRUE;
for(i = 0; i < MAX_ALIENS; i++)
pAlien[i].fPosVelocity[Y] -= 1.0f;
pPlanet->SetAnimationT(1);
pPlanet->SetAnimationStepT(0);
if(LEVELS == EASTER_EGG_LEVELS)
StartLevel(&InvasionEgg, FALSE, FALSE);
else
StartLevel(&Invasion, FALSE, FALSE);
pPlayer.fPower = pPlayer.fMaxPower;
// Enable Highscore
bPlayerEnteringHighScore = TRUE;
byPlayersHScorePlace = -1;
}
}
//////////////////////////////////////////////////////////////////////
void InitSounds()
{
// Create Sounds
if(!bSoundPossible)
return;
pExplosion1Sound = DSUtil_CreateSound(TEXT("EXPLOSION1"), 1);
pExplosion2Sound = DSUtil_CreateSound(TEXT("EXPLOSION2"), 1);
pWormholeSound = DSUtil_CreateSound(TEXT("WORMHOLE"), 1);
pInvasionSound = DSUtil_CreateSound(TEXT("INVASION"), 1);
pPlayerShotSound = DSUtil_CreateSound(TEXT("PLAYER_SHOT"), 1);
pAlienShotSound = DSUtil_CreateSound(TEXT("ALIEN_SHOT"), 1);
pAlien5ShotSound = DSUtil_CreateSound(TEXT("ALIEN_5_SHOT"), 1);
pBigBossShotSound = DSUtil_CreateSound(TEXT("BIG_BOSS_SHOT"), 1);
pWormholeShotSound = DSUtil_CreateSound(TEXT("WORMHOLE_SHOT"), 1);
pAlien5DeadSound = DSUtil_CreateSound(TEXT("ALIEN_5_DEAD"), 1);
pBigBossDeadSound = DSUtil_CreateSound(TEXT("BIG_BOSS_DEAD"), 1);
pAlienAttackSound = DSUtil_CreateSound(TEXT("ALIEN_ATTACK"), 1);
pObjectLiveSound = DSUtil_CreateSound(TEXT("O_LIVE"), 1);
pObjectWallSound = DSUtil_CreateSound(TEXT("O_WALL"), 1);
pObjectSingleLaserSound = DSUtil_CreateSound(TEXT("O_SINGLE_LASER"), 1);
pObjectDoubleLaserSound = DSUtil_CreateSound(TEXT("O_DOUBLE_LASER"), 1);
pObjectPowerIncreaseSound = DSUtil_CreateSound(TEXT("O_POWER_INCREASE"), 1);
}
void DestroySounds(void)
{
// Destroy Sounds
if(!bSoundPossible)
return;
DSUtil_DestroySound(pExplosion1Sound);
pExplosion1Sound = NULL;
DSUtil_DestroySound(pExplosion2Sound);
pExplosion2Sound = NULL;
DSUtil_DestroySound(pWormholeSound);
pWormholeSound = NULL;
DSUtil_DestroySound(pInvasionSound);
pInvasionSound = NULL;
DSUtil_DestroySound(pPlayerShotSound);
pPlayerShotSound = NULL;
DSUtil_DestroySound(pAlienShotSound);
pAlienShotSound = NULL;
DSUtil_DestroySound(pAlien5ShotSound);
pAlien5ShotSound = NULL;
DSUtil_DestroySound(pBigBossShotSound);
pBigBossShotSound = NULL;
DSUtil_DestroySound(pWormholeShotSound);
pWormholeShotSound = NULL;
DSUtil_DestroySound(pAlien5DeadSound);
pAlien5DeadSound = NULL;
DSUtil_DestroySound(pBigBossDeadSound);
pBigBossDeadSound = NULL;
DSUtil_DestroySound(pAlienAttackSound);
pAlienAttackSound = NULL;
DSUtil_DestroySound(pObjectLiveSound);
pObjectLiveSound = NULL;
DSUtil_DestroySound(pObjectWallSound);
pObjectWallSound = NULL;
DSUtil_DestroySound(pObjectSingleLaserSound);
pObjectSingleLaserSound = NULL;
DSUtil_DestroySound(pObjectDoubleLaserSound);
pObjectDoubleLaserSound = NULL;
DSUtil_DestroySound(pObjectPowerIncreaseSound);
pObjectPowerIncreaseSound = NULL;
}
void NormalizeFace(FLOAT3 *ResultV, FLOAT3 V1, FLOAT3 V2, FLOAT3 V3)
{
FLOAT3 V1_V2, V1_V3;
float fLength;
SubtVer(V1_V2, V1, V2);
SubtVer(V1_V3, V1, V3);
CrossProductVer(*ResultV, V1_V2, V1_V3);
fLength = (float) sqrt((*ResultV)[X]*(*ResultV)[X]+(*ResultV)[Y]*(*ResultV)[Y]+(*ResultV)[Z]*(*ResultV)[Z]);
(*ResultV)[X] /= fLength;
(*ResultV)[Y] /= fLength;
(*ResultV)[Z] /= fLength;
}
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
char byFilename[TEXTURES][256] = {"Font.jpg",
"space_lt.jpg",
"space_rt.jpg",
"space_rb.jpg",
"space_lb.jpg",
"Alien.jpg",
"Ship.jpg",
"Worm1.jpg",
"Worm2.jpg",
"Worm3.jpg",
"Worm4.jpg",
"Worm5.jpg",
"Explosion1.jpg",
"Title1.jpg",
"Title2.jpg",
"Title3.jpg"};
ASLoadTextures(byFilename, TEXTURES, GameTexture);
ASGenOpenGLTextures(TEXTURES, GameTexture);
return TRUE;
}
void DestroyGLTextures(void)
{ // begin DestroyGLTextures()
ASDestroyOpenGLTextures(TEXTURES, GameTexture);
ASDestroyTextures(TEXTURES, GameTexture);
} // end DestroyGLTextures()
GLvoid BuildFont(GLvoid) // Build Our Font Display List
{
float cx; // Holds Our X Character Coord
float cy; // Holds Our Y Character Coord
base=glGenLists(256); // Creating 256 Display Lists
glBindTexture(GL_TEXTURE_2D, GameTexture[0].iOpenGLID); // Select Our Font Texture
for (loop=0; loop<256; loop++) // Loop Through All 256 Lists
{
cx=float(loop%16)/16.0f; // X Position Of Current Character
cy=float(loop/16)/16.0f; // Y Position Of Current Character
glNewList(base+loop,GL_COMPILE); // Start Building A List
glBegin(GL_QUADS); // Use A Quad For Each Character
glTexCoord2f(cx,1-cy-0.0625f); // Texture Coord (Bottom Left)
glVertex2i(0,0); // Vertex Coord (Bottom Left)
glTexCoord2f(cx+0.0625f,1-cy-0.0625f); // Texture Coord (Bottom Right)
glVertex2i(16,0); // Vertex Coord (Bottom Right)
glTexCoord2f(cx+0.0625f,1-cy); // Texture Coord (Top Right)
glVertex2i(16,16); // Vertex Coord (Top Right)
glTexCoord2f(cx,1-cy); // Texture Coord (Top Left)
glVertex2i(0,16); // Vertex Coord (Top Left)
glEnd(); // Done Building Our Quad (Character)
glTranslated(10,0,0); // Move To The Right Of The Character
glEndList(); // Done Building The Display List
} // Loop Until All 256 Are Built
}
GLvoid KillFont(GLvoid) // Delete The Font From Memory
{
glDeleteLists(base,256); // Delete All 256 Display Lists
}
GLvoid glPrint(GLint x, GLint y, char *string, int set) // Where The Printing Happens
{
if (set>1)
set=1;
glBindTexture(GL_TEXTURE_2D, GameTexture[0].iOpenGLID); // Select Our Font Texture
glDisable(GL_DEPTH_TEST); // Disables Depth Testing
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glOrtho(0,640,0,480,-100,100); // Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glPushMatrix(); // Store The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glTranslated(x,y,0); // Position The Text (0,0 - Bottom Left)
glListBase(base-32+(128*set)); // Choose The Font Set (0 or 1)
glCallLists(strlen(string),GL_BYTE,string); // Write The Text To The Screen
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPopMatrix(); // Restore The Old Projection Matrix
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glPopMatrix(); // Restore The Old Projection Matrix
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
height=1; // Making Height Equal One
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,1000.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
GLvoid CreateLists(GLvoid)
{
background_list = glGenLists(NUM_LISTS);
// Draw Background As Textured Quad
glNewList(background_list, GL_COMPILE);
// Upper Left
glBindTexture(GL_TEXTURE_2D, GameTexture[1].iOpenGLID);
glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-WORLD_WIDTH, WORLD_HEIGHT * 1.4f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-WORLD_WIDTH, 0.0f , 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.0f , 0.0f , 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.0f , WORLD_HEIGHT * 1.4f, 0.0f);
glEnd();
// Upper Right
glBindTexture(GL_TEXTURE_2D, GameTexture[2].iOpenGLID);
glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f , WORLD_HEIGHT * 1.4f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.0f , 0.0f , 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( WORLD_WIDTH, 0.0f , 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( WORLD_WIDTH, WORLD_HEIGHT * 1.4f, 0.0f);
glEnd();
// Lower Right
glBindTexture(GL_TEXTURE_2D, GameTexture[3].iOpenGLID);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -