⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lesson1.cpp

📁 以前下载的不知道跟网站上面的一样不
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	
	if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))	// Did Windows Find A Matching Pixel Format?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}
	
	if(!SetPixelFormat(hDC,PixelFormat,&pfd))		// Are We Able To Set The Pixel Format?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}
	
	if (!(hRC=wglCreateContext(hDC)))				// Are We Able To Get A Rendering Context?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}
	
	if(!wglMakeCurrent(hDC,hRC))					// Try To Activate The Rendering Context
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}
	
	ShowWindow(hWnd,SW_SHOW);						// Show The Window
	SetForegroundWindow(hWnd);						// Slightly Higher Priority
	SetFocus(hWnd);									// Sets Keyboard Focus To The Window
	ReSizeGLScene(width, height);					// Set Up Our Perspective GL Screen
	
	if (!InitGL())									// Initialize Our Newly Created GL Window
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}
	
	return TRUE;									// Success
}

LRESULT CALLBACK WndProc(HWND	hWnd,			// Handle For This Window
						 UINT	uMsg,			// Message For This Window
						 WPARAM	wParam,			// Additional Message Information
						 LPARAM	lParam)			// Additional Message Information
{
	switch (uMsg)									// Check For Windows Messages
	{
	case WM_ACTIVATE:							// Watch For Window Activate Message
		{
			if (!HIWORD(wParam))					// Check Minimization State
			{
				active=TRUE;						// Program Is Active
			}
			else
			{
				active=FALSE;						// Program Is No Longer Active
			}
			
			return 0;								// Return To The Message Loop
		}
		
	case WM_SYSCOMMAND:							// Intercept System Commands
		{
			switch (wParam)							// Check System Calls
			{
			case SC_SCREENSAVE:					// Screensaver Trying To Start?
			case SC_MONITORPOWER:				// Monitor Trying To Enter Powersave?
				return 0;							// Prevent From Happening
			}
			break;									// Exit
		}
		
	case WM_CLOSE:								// Did We Receive A Close Message?
		{
			PostQuitMessage(0);						// Send A Quit Message
			return 0;								// Jump Back
		}
		
	case WM_KEYDOWN:							// Is A Key Being Held Down?
		{
			keys[wParam] = TRUE;					// If So, Mark It As TRUE
			return 0;								// Jump Back
		}
		
	case WM_KEYUP:								// Has A Key Been Released?
		{
			keys[wParam] = FALSE;					// If So, Mark It As FALSE
			return 0;								// Jump Back
		}
		
	case WM_SIZE:								// Resize The OpenGL Window
		{
			ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
			return 0;								// Jump Back
		}
	}
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}



int WINAPI WinMain(	HINSTANCE	hInstance,			// Instance
				   HINSTANCE	hPrevInstance,		// Previous Instance
				   LPSTR		lpCmdLine,			// Command Line Parameters
				   int			nCmdShow)			// Window Show State
{
	
	srand(GetTickCount());
	MSG		msg;									
	BOOL	bQuit=FALSE;						
	
	if (!CreateGLWindow("俄罗斯方块OPENGL版V1.0",500,500,16,FALSE))
	{
		return 0;		
	}
	
	while(!bQuit)						
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))	
		{
			if (msg.message==WM_QUIT)				
			{
				bQuit=TRUE;							
			}
			else									
			{
				TranslateMessage(&msg);				
				DispatchMessage(&msg);				
			}
		}
		else										
		{
			if (active)								
			{
				if (keys[VK_ESCAPE])				
				{
					bQuit=TRUE;						
				}
				else if (keys['P'])
				{
					if ( gameplayer.GameState==GS_PAUSE)
					{		
						gameplayer.GameState=GS_PLAY;
					}
					else
					{
						gameplayer.GameState=GS_PAUSE;
					}
				}
				else if (keys['N'])
				{
					NewGame();
					gameplayer.GameState=GS_PLAY;
				}
				else								// Not Time To Quit, Update Screen
				{
					gameloop();
				}
			}
		}
	}
	KillGLWindow();									// Kill The Window
	return (msg.wParam);							// Exit The Program
}

void drawnext()
{
	int i,j;
    int n;
	
    n = nextmodel.textureid;
	
    for(i=0;i<4;i++)
	{
        for(j=0;j<4;j++)
		{
            if(nextmodel.model[i][j]!= 0)
			{
                drawcell(290+20*i,450-20*j,n);
            }
        }
    }
}

void gameloop()
{
	if (gameplayer.GameState==GS_NONE)
	{
		drawmap();
	}
	
	if(gameplayer.GameState == GS_PLAY)
	{
		keyproc();
        DWORD dwTime = GetTickCount();
		if ((dwTime - gameplayer.dwTimeLamp)>(500-gameplayer.Level*20))
		{
            gameplayer.dwTimeLamp = dwTime;
			currentmodel.CheckDrop();
        }
		drawmap();
	}
	
	if(gameplayer.GameState == GS_PAUSE)
	{
		drawmap();
	}
	
}

void drawmap()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	char buffer[256];  //256个字符的buffer
	
    //画主框架
	glDisable(GL_TEXTURE_2D);     //关闭纹理,画线         
	glColor3f(1.0f,1.0f,1.0f);
	glBegin(GL_LINE_STRIP);
	glVertex2f(29,49);
	glVertex2f(271,49);
	glVertex2f(271,451);
	glVertex2f(29,451);
	glVertex2f(29,49);
	glEnd();
	
	glColor3f(0.3f,0.3f,0.3f);
	glBegin(GL_LINES);
	for(int i=1;i<12;i++)
	{
		glVertex2i(30+i*20,450);
		glVertex2i(30+i*20,50);
	}
	for(i=1;i<20;i++)
	{
		glVertex2i(30,50+20*i);
        glVertex2i(270,50+20*i);
	}
	glEnd();
	
	
	glBegin(GL_LINES);
	for(i=1;i<4;i++)
	{
		glVertex2i(290,450-20*i);
		glVertex2i(370,450-20*i);
	}
	for(i=1;i<4;i++)
	{
		glVertex2i(290+20*i,450);
        glVertex2i(290+20*i,370);
	}
	glEnd();
	
	
	glColor3f(1.0f,1.0f,1.0f);
	glBegin(GL_LINE_STRIP);    //画nextmodel框架
	glVertex2f(289,369);
	glVertex2f(371,369);
	glVertex2f(371,451);
	glVertex2f(289,451);
	glVertex2f(289,369);
	glEnd();
	
	glEnable(GL_TEXTURE_2D);    //开启纹理
	
	if (gameplayer.GameState!=GS_NONE)
	{
		
		for(int j= 0;j<20;j++)      //画map上有model的地方
		{
			for(int i= 0;i<12;i++)
			{
				if(gameplayer.Map[i][j]!=0)
				{
					int x,y,n;
					n =gameplayer.Map[i][j];
					x = i*20;
					y = j*20;
					drawcell(30+x,450-y,n);
				}
			}
		}
		
		
		for(j= 0;j<4;j++)          //画运动中的model
		{
			for(int i= 0;i<4;i++)
			{
				if (currentmodel.model[i][j]!= 0)
				{
					int x,y;
					int n = currentmodel.textureid;
					x = (currentmodel.x_pos +i)*20;
					y = (currentmodel.y_pos +j)*20;
					drawcell(30+x,450-y,n);
				}
			}
		}
		drawnext();               //画mextmodel
	}
	
    glColor3f(0.0f,1.0f,1.0f);                
	fNeHe.Print(80,470,"starwolf's first opengl game!");
	
	wsprintf(buffer,"%d",gameplayer.dwScore);
	fNeHe.Print(290,320,"score:");
	fNeHe.Print(360,320,buffer);
	
	wsprintf(buffer,"%d",gameplayer.Level);
	fNeHe.Print(290,290,"level:");
	fNeHe.Print(360,290,buffer);
	
	wsprintf(buffer,"%d",gameplayer.Speed);
	fNeHe.Print(290,260,"speed:");
	fNeHe.Print(360,260,buffer);

	glColor3f(1.0f,0.0f,0.0f);
	fNeHe.Print(290,200,"P:Pause game");
	fNeHe.Print(290,170,"N:New game");

	glColor3f(0.0f,1.0f,1.0f);
	fNeHe.Print(70,20,"press ESCAPE Key to quit the game:)");

	if (gameplayer.GameState==GS_NONE)
	{
		glColor4f(1.0f,1.0f,1.0f,0.4f);
		glEnable(GL_BLEND);
		glBindTexture(GL_TEXTURE_2D,modeltexture[4]);
	    glBegin(GL_QUADS);
	    glTexCoord2f(0.0,0.0);glVertex2i(50,50);
	    glTexCoord2f(1.0,0.0);glVertex2i(450,50);
	    glTexCoord2f(1.0,1.0);glVertex2i(450,410);
	    glTexCoord2f(0.0,1.0);glVertex2i(50,410);
	    glEnd();
		glDisable(GL_BLEND);
		glColor3f(0.0f,1.0f,1.0f);
	}
	
    glFlush();
	SwapBuffers(hDC);
}

void keyproc()
{
	if (GetAsyncKeyState(VK_UP)>0x80000000)
    {
        if (!gameplayer.KeyState.KeyUpDown) 
        {
            currentmodel.ShiftSheet();
			gameplayer.KeyState.KeyUpDown=TRUE;
		}
    }
    else 
	{
		gameplayer.KeyState.KeyUpDown=FALSE;
    }
	
	
	
    if (GetAsyncKeyState(VK_LEFT)>0x80000000)
    {
        if (!gameplayer.KeyState.KeyLeftDown)
        {
            if (currentmodel.x_pos >0 )
            {
                currentmodel.x_pos--;
				
                if (!currentmodel.CheckCurrSheet())
					currentmodel.x_pos++;
				//Player.x++;
            }
            gameplayer.KeyState.KeyLeftDown=TRUE;  
            gameplayer.KeyState.dwTimeLeftDown=GetTickCount();
        }
        else if((GetTickCount()-gameplayer.KeyState.dwTimeLeftDown )>150)
		{
			gameplayer.KeyState.KeyLeftDown=FALSE;
        }
    }
    else
	{
        gameplayer.KeyState.KeyLeftDown=FALSE;
    }
	
	
    if (GetAsyncKeyState(VK_RIGHT)>0x80000000)
    {
        if (!gameplayer.KeyState.KeyRightDown)
		{
            if (currentmodel.x_pos <11)
            {
                currentmodel.x_pos++;
                if (!currentmodel.CheckCurrSheet())
                    currentmodel.x_pos--;
				
            }
			gameplayer.KeyState.KeyRightDown=TRUE;
			gameplayer.KeyState.dwTimeRightDown=GetTickCount();
        }
        else if((GetTickCount()-gameplayer.KeyState.dwTimeRightDown)>150)
		{
            gameplayer.KeyState.KeyRightDown=FALSE;
        }
    }
    else
	{
        gameplayer.KeyState.KeyRightDown=FALSE;
    }
	
	
    if (GetAsyncKeyState(VK_DOWN)>0x80000000)
    {
        if (!gameplayer.KeyState.KeyDownDown)
		{
			currentmodel.y_pos++;
            if (!currentmodel.CheckCurrSheet())
			{
                currentmodel.y_pos--;
			}
			gameplayer.KeyState.KeyDownDown=TRUE;
			gameplayer.KeyState.dwTimeDDown=GetTickCount();
			
        }
        else if((GetTickCount()-gameplayer.KeyState.dwTimeDDown )>30)
		{
			gameplayer.KeyState.KeyDownDown=FALSE;
			
        }
    }
    else
	{
		gameplayer.KeyState.KeyDownDown=FALSE;
		
    }
	
}

void drawcell(int x,int y,int textureid)
/*  绘制俄罗斯方块的最小单元       textureid为1到4
|------>X          x,y为左上角坐标
| 
|
|
|y
坐标系如上定义,textureid为纹理id   */
{
	glBindTexture(GL_TEXTURE_2D,modeltexture[textureid-1]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0,1.0);glVertex2i(x,y);
	glTexCoord2f(1.0,1.0);glVertex2i(x+20,y);
	glTexCoord2f(1.0,0.0);glVertex2i(x+20,y-20);
	glTexCoord2f(0.0,0.0);glVertex2i(x,y-20);
	glEnd();
}

void NewGame(void)
{
	currentmodel.shapeid=rand()%7;
	currentmodel.textureid=rand()%4+1;
	currentmodel.x_pos=4;
	currentmodel.y_pos=0;
	CopyMemory(currentmodel.model,SheetsAddrTab[currentmodel.shapeid],16);

	nextmodel.shapeid=rand()%7;
	nextmodel.textureid=rand()%4+1;
	CopyMemory(nextmodel.model,SheetsAddrTab[nextmodel.shapeid],16);

	ZeroMemory(gameplayer.Map,12*20);
	gameplayer.Level=1;
	gameplayer.Speed=10;
	gameplayer.dwScore=0;
	gameplayer.dwTimeLamp=GetTickCount();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -