//---------------------------include files--------------------------------------------//
#include <windows.h>
#include <time.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include "tetris.h"
#include "CPlaySound.h"
#include "CTexture.h"
#include "CText2D.h"
#include "CChineseText2D.h"
#include "CRandomChar.h"
#include "Resource\Resource.h"
#include "htmlhelp.h"
//加载htmlhelp库
#pragma comment(lib, "htmlhelp.lib")
//-----------------------------global variable--------------------------------------------//
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
int g_cw=(X+7)*SCALE, g_ch=Y*SCALE;
static DWORD TimeStart=0, TimeCurrent=0;
bool bGameActive =true; //control status of render
int current_scene=0; //current scene
int x = 5, y = 19; //the original position of every block
struct Status //description of data in game
{
int score;
int level;
bool bMusic;
};
Status data={0,1,true};
//---------------------------sounds variable--------------------------------------------//
enum eSound_ID
{
SCENE0_SND_ID,
BG0_SND_ID, BG1_SND_ID, BG2_SND_ID, BG3_SND_ID,
Scene3_SND_ID
};
const int nMAX_SOUND = Scene3_SND_ID+1; //number of sounds
CPlaySound sound[nMAX_SOUND];
int nIndexMusic=BG0_SND_ID; //current sound
char szFileSound[nMAX_SOUND][256]=
{
"Data/Sound/Scene0.s3m",
//background music of scene1
"Data/Sound/BG0.xm", "Data/Sound/BG0.xm",
"Data/Sound/BG1.xm", "Data/Sound/BG1.xm",
"Data/Sound/Scene3.mod"
};
//----------------------------texture variable------------------------------------------//
//texture ID
enum eTex_ID
{
Loading_TEX_ID,
SCENE0BG_TEX_ID,
MENU_FONT_ID, MENU1_ID, MENU2_ID,
SCENE1LBG_TEX_ID, SCENE1RBG_TEX_ID,
BLOCK_TEX_ID,
SCENE3BG_TEX_ID, SCENE3BGMASK_TEX_ID, SCENE3_CREDIT_ID
};
const int nMAX_TEXTURE = SCENE3_CREDIT_ID+1; //num of texture
CTexture texture[nMAX_TEXTURE];
UINT g_Texture[nMAX_TEXTURE] = {0}; //array for texture
char szFileTexture[nMAX_TEXTURE][256]=
{
//texture of loading
"Data/Texture/Loading.jpg",
//texture of scene0
"Data/Texture/Scene0BG.jpg",
//texture of menu
"Data/Texture/Font.jpg", "Data/Texture/Menu1.jpg",
"Data/Texture/Menu2.jpg",
//texture of scene1
"Data/Texture/Scene0LBG.jpg", "Data/Texture/Scene0BG.jpg",
"Data/Texture/Block.jpg",
//texture of scene3 , credit
"Data/Texture/Scene3BG.jpg", "Data/Texture/Scene3BGMask.jpg", "Data/Texture/Credit.jpg"
};
//----------------------------menu variable------------------------------------------//
CText2D text;
enum eText_ID
{
//菜单的字体 9个
Menu_Text_ID,
Levels_Text_ID, Easy_Text_ID, Normal_Text_ID, Harder_Text_ID, Hard_Text_ID,
Music_Text_ID, MusicOn_Text_ID, MusicOff_Text_ID,
//玩家数据 3个
Level_Text_ID, Scores_Text_ID, Help_Text_ID,
//帮助 9个
KeyTable_Text_ID,
Up_Text_ID, Down_Text_ID, Left_Text_ID, Right_Text_ID,Space_Text_ID,
F1_Text_ID, H_Text_ID, Esc_Text_ID,
//game over 3个
GameOver_Text_ID, Continue_Text_ID, Credit_Text_ID, Quit_Text_ID
};
const int nNumofChinaText=Quit_Text_ID+1;
CChineseText2D ChinaText[nNumofChinaText];
char szChinaText[nNumofChinaText][256]={
"菜单", "难度", "简单", "一般", "较难", "难", "音乐", "开", "关",
"难度:", "分数:", "H - 帮助/暂停",
"按键表:","[↑]或[W] - 旋转","[↓]或[S] - 向下","[←]或[A] - 向左", "[→]或[D] - 向右", "[空格] - 快速下落",
"[F1] - 联机帮助", "[H] - 帮助", "[ESC] - 不玩了",
"游戏结束", "[回车] - 重玩", "[C] - 介绍", "[ESC] - 不玩了"
};
const int nMax_Menu = 2;
int nOption=0; //current index of option
//------------------------------Scene3 variable--------------------------------//
CRandomChar RamChar; //render random char
//-------------------------------statement funtion------------------------------------//
void DrawLoading(float fFrame);//loading or initilize
bool Scene0(); //memu
bool Scene1(); //game
bool Scene2(); //game over
bool Scene3(); //credit
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
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
gluOrtho2D(0, X, 0, 10);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
/*
================================================================================
InitGL() : initilize all
================================================================================
*/
int InitGL()
{
int i;
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_TEXTURE_2D);
float fStep=100.0f/float (nMAX_TEXTURE+nNumofChinaText+nMAX_SOUND); //init step of loading
float fPosition=0.0f; //position of loading
//load texture of loading
texture[Loading_TEX_ID].LoadTexture(szFileTexture[Loading_TEX_ID],1);
g_Texture[Loading_TEX_ID]=texture[Loading_TEX_ID].Get_TextureID();
//load texture of bg
texture[SCENE0BG_TEX_ID].LoadTexture(szFileTexture[SCENE0BG_TEX_ID],1);
g_Texture[SCENE0BG_TEX_ID]=texture[SCENE0BG_TEX_ID].Get_TextureID();
//init all font
for(i=0;i<nNumofChinaText;i++)
{
ChinaText[i].Init(hDC,szChinaText[i],"宋体");
fPosition+=1;
DrawLoading(fPosition*fStep);
}
fPosition=nNumofChinaText; DrawLoading(fPosition*fStep);
//load sounds
for(i=0;i<nMAX_SOUND;i++)
{
sound[i].Init(hWnd,szFileSound[i],1);
fPosition+=1;
DrawLoading(fPosition*fStep);
}
fPosition=nMAX_SOUND+nNumofChinaText; DrawLoading(fPosition*fStep);
//load texture else
for(i=MENU_FONT_ID;i<nMAX_TEXTURE;i++)
{
texture[i].LoadTexture(szFileTexture[i],1);
g_Texture[i]=texture[i].Get_TextureID();
fPosition+=1;
DrawLoading(fPosition*fStep);
}
fPosition=nMAX_TEXTURE+nMAX_SOUND+nNumofChinaText; DrawLoading(fPosition*fStep);
//初始化纹理字体
text.Init(g_Texture[MENU_FONT_ID]);
//seed of random
srand( (unsigned)time( NULL ) );
//generate the first random char
block = rand()%7;
TimeStart=GetTickCount();
return TRUE; // Initialization Went OK
}
GLvoid KillGLWindow() // Properly Kill The Window
{
if (fullscreen) // Are We In Fullscreen Mode?
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}
if (hRC) // Do We Have A Rendering Context?
{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL;