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

📄 input.cpp

📁 一本关于OPenGL的很好的电子书
💻 CPP
📖 第 1 页 / 共 2 页
字号:

  // now make the rendering context the active one
  if(!wglMakeCurrent(g_hdc, g_hrc))
  {
    MessageBox(NULL,"Unable to activate OpenGL rendering context", "ERROR", MB_OK | MB_ICONEXCLAMATION);
    return FALSE;
  }

  // show the window in the forground, and set the keyboard focus to it
  ShowWindow(g_hwnd, SW_SHOW);
  SetForegroundWindow(g_hwnd);
  SetFocus(g_hwnd);

  // set up the perspective for the current screen size
  ResizeScene(width, height);

  // do one-time initialization
  if (!InitializeScene())
  {
    MessageBox(NULL, "Initialization failed", "Error", MB_OK | MB_ICONEXCLAMATION);
    return FALSE;
  }

  return TRUE;
} // end SetupWindow()


/*****************************************************************************
 KillWindow()

 Deletes the DC, RC, and Window, and restores the original display.
*****************************************************************************/
BOOL KillWindow()
{
  // restore the original display if we're in fullscreen mode
  if (g_isFullscreen)
  {
    ChangeDisplaySettings(NULL, 0);
    ShowCursor(TRUE);
  }

  // if we have an RC, release it
  if (g_hrc)
  {
    // release the RC
    if (!wglMakeCurrent(NULL,NULL))
    {
      MessageBox(NULL, "Unable to release rendering context", "Error", MB_OK | MB_ICONINFORMATION);
    }

    // delete the RC
    if (!wglDeleteContext(g_hrc))
    {
      MessageBox(NULL, "Unable to delete rendering context", "Error", MB_OK | MB_ICONINFORMATION);
    }

    g_hrc = NULL;
  }

  // release the DC if we have one
  if (g_hdc && !ReleaseDC(g_hwnd, g_hdc))
  {
    MessageBox(NULL, "Unable to release device context", "Error", MB_OK | MB_ICONINFORMATION);
    g_hdc = NULL;
  }

  // destroy the window if we have a valid handle
  if (g_hwnd && !DestroyWindow(g_hwnd))
  {
    MessageBox(NULL, "Unable to destroy window", "Error", MB_OK | MB_ICONINFORMATION);
    g_hwnd = NULL;
  }

  // unregister our class so we can create a new one if we need to
  if (!UnregisterClass(WND_CLASS_NAME, g_hInstance))
  {
    MessageBox(NULL, "Unable to unregister window class", "Error", MB_OK | MB_ICONINFORMATION);
    g_hInstance = NULL;
  }

  return TRUE;
} // end KillWindow()


/*****************************************************************************
 ResizeScene()

 Called once when the application starts and again every time the window is
 resized by the user.
*****************************************************************************/
GLvoid ResizeScene(GLsizei width, GLsizei height)
{
  // avoid divide by zero
  if (height==0)  
  {
    height=1;
  }

  g_screenWidth = width;
  g_screenHeight = height;

  // set the viewport to the new dimensions
  glViewport(0, 0, width, height);

  // select the projection matrix and clear it out
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  // set the perspective with the appropriate aspect ratio
  gluOrtho2D(0, g_screenWidth, 0, g_screenHeight);

  // select modelview matrix
  glMatrixMode(GL_MODELVIEW);
} // end ResizeScene()


/*****************************************************************************
 InitializeScene()

 Performs one-time application-specific setup. Returns FALSE on any failure.
*****************************************************************************/
BOOL InitializeScene()
{
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  LoadTexture("arrows.bmp", g_mouseTexture);
  g_input.Initialize(g_hwnd, g_hInstance, true, IS_USEKEYBOARD | IS_USEMOUSE);

  g_listBase = CreateBitmapFont("Times New Roman", 200);

  return TRUE;
} // end InitializeScene()


/*****************************************************************************
 DisplayScene()

 The work of the application is done here. This is called every frame, and
 handles the actual rendering of the scene.
*****************************************************************************/
BOOL DisplayScene()
{
  glLoadIdentity();
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // update and process input data
  g_input.Update();
  ProcessInput();

  // draw text
  glTranslatef(0.0, 0.0, -1.0);
	glColor3f(0.0f, 0.66f, 0.99f);
	glRasterPos2i(g_screenWidth/2 - 100, g_screenHeight/2 - 50);
	PrintString(g_listBase, g_lastKey);

  DrawMouseCursor();

  return TRUE;
} // end DisplayScene()


/*****************************************************************************
 Cleanup()

 Called at the end of successful program execution.
*****************************************************************************/
BOOL Cleanup()
{
  g_input.Shutdown();

	ClearFont(g_listBase);

  glDeleteTextures(1, &g_mouseTexture);
  return TRUE;
} // end Cleanup()


/****************************************************************************
 LoadTexture()

 Loads the texture from the specified file and stores it in iTexture.
*****************************************************************************/
void LoadTexture(char *filename, GLuint &texture)
{
  // get a texture object
  glGenTextures(1, &texture);
  glBindTexture(GL_TEXTURE_2D, texture);

  // load the bitmap
  BITMAPINFOHEADER bitmapInfoHeader;
  unsigned char *buffer = LoadBitmapFileWithAlpha(filename, &bitmapInfoHeader);

  // set up the texture
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D, 0, 4, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

  // we're done with the bitmap data
  free(buffer);
} // end LoadTexture()


/*****************************************************************************
 ProcessInput()

 Reads data from the input system and updates globals accordingly
*****************************************************************************/
void ProcessInput()
{
  static bool leftButtonDown = false;
  static bool rightButtonDown = false;

  // rotate through mouse cursors on mouseclicks or wheel movement
  if (g_input.ButtonDown(0))
    leftButtonDown = true;

  if (g_input.ButtonDown(1))
    rightButtonDown = true;

  if (g_input.GetMouseWheelMovement() < 0 || (leftButtonDown && g_input.ButtonUp(0)))
  {
    leftButtonDown = false;
    g_textureIndex--;
    if (g_textureIndex < 0)
      g_textureIndex = NUM_TEXTURES - 1;
  }

  if (g_input.GetMouseWheelMovement() > 0 || (rightButtonDown && g_input.ButtonUp(1)))
  {
    rightButtonDown = false;
    g_textureIndex++;
    if (g_textureIndex == NUM_TEXTURES)
      g_textureIndex = 0;
  }

  // update the mouse position
  int dx, dy;
  g_input.GetMouseMovement(dx, dy);

  // keep the cursor within the window
  g_mouseX += dx;
  if (g_mouseX >= g_screenWidth)
    g_mouseX = g_screenWidth - 1;
  if (g_mouseX < 0)
    g_mouseX = 0;
  g_mouseY -= dy;
  if (g_mouseY >= g_screenHeight)
    g_mouseY = g_screenHeight - 1;
  if (g_mouseY < 0)
    g_mouseY = 0;

  // check to see if one of the function keys was pressed
  if (g_input.KeyDown(DIK_F1))
    strcpy(g_lastKey, "F1");
  if (g_input.KeyDown(DIK_F2))
    strcpy(g_lastKey, "F2");
  if (g_input.KeyDown(DIK_F3))
    strcpy(g_lastKey, "F3");
  if (g_input.KeyDown(DIK_F4))
    strcpy(g_lastKey, "F4");
  if (g_input.KeyDown(DIK_F5))
    strcpy(g_lastKey, "F5");
  if (g_input.KeyDown(DIK_F6))
    strcpy(g_lastKey, "F6");
  if (g_input.KeyDown(DIK_F7))
    strcpy(g_lastKey, "F7");
  if (g_input.KeyDown(DIK_F8))
    strcpy(g_lastKey, "F8");
  if (g_input.KeyDown(DIK_F9))
    strcpy(g_lastKey, "F9");
  if (g_input.KeyDown(DIK_F10))
    strcpy(g_lastKey, "F10");
  if (g_input.KeyDown(DIK_F11))
    strcpy(g_lastKey, "F11");
  if (g_input.KeyDown(DIK_F12))
    strcpy(g_lastKey, "F12");

  // check for the exit key
  if (g_input.KeyDown(DIK_ESCAPE))
    PostQuitMessage(0);

} // end ProcessInput()


/*****************************************************************************
 DrawMouseCursor()

 Draw the mouse cursor as a textured quad.
*****************************************************************************/
void DrawMouseCursor()
{
  // determine the starting mouse texture u coordinate
  float texStart = (float)g_textureIndex/(float)NUM_TEXTURES;

  // make sure the transparent portions of the cursor aren't shown
  glEnable(GL_ALPHA_TEST);
  glAlphaFunc(GL_GREATER, 0);

  // draw the cursor
  glEnable(GL_TEXTURE_2D);
  glColor3f(1.0, 1.0, 1.0);
  glBindTexture(GL_TEXTURE_2D, g_mouseTexture);
  glBegin(GL_QUADS);
    glTexCoord2f(texStart, 0.0);
    glVertex2i(g_mouseX, g_mouseY - CURSOR_SIZE);
    glTexCoord2f(texStart + 1.0f/NUM_TEXTURES, 0.0);
    glVertex2i(g_mouseX + CURSOR_SIZE, g_mouseY - CURSOR_SIZE);
    glTexCoord2f(texStart + 1.0f/NUM_TEXTURES, 1.0);
    glVertex2i(g_mouseX + CURSOR_SIZE, g_mouseY);
    glTexCoord2f(texStart, 1.0);
    glVertex2i(g_mouseX, g_mouseY);
  glEnd();
  glDisable(GL_ALPHA_TEST);
  glDisable(GL_TEXTURE_2D);
} // end DrawMouseCursor()


/*****************************************************************************
 CreateBitmapFont()

 Use native wgl support to create display list based fonts
*****************************************************************************/
unsigned int CreateBitmapFont(char *fontName, int fontSize)
{
	HFONT hFont;         // windows font
	unsigned int base;

	base = glGenLists(96);      // create storage for 96 characters

	if (stricmp(fontName, "symbol") == 0)
	{
    hFont = CreateFont(fontSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, SYMBOL_CHARSET, 
                       OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
                       FF_DONTCARE | DEFAULT_PITCH, fontName);
	}
	else
	{
    hFont = CreateFont(fontSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, 
                       OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
                       FF_DONTCARE | DEFAULT_PITCH, fontName);
	}

	if (!hFont)
		return 0;

	SelectObject(g_hdc, hFont);
	wglUseFontBitmaps(g_hdc, 32, 96, base);

	return base;
} // end CreateBitmapFont


/*****************************************************************************
 ClearFont()

 Free the display lists for the font
*****************************************************************************/
void ClearFont(unsigned int base)
{
	if (base != 0)
		glDeleteLists(base, 96);
} // end ClearFont


/*****************************************************************************
 PrintString()

 Display the text in msg at the current raster position
*****************************************************************************/
void PrintString(unsigned int base, char *msg)
{
	if ((base == 0) || (msg == NULL))
		return;

	glPushAttrib(GL_LIST_BIT);
		glListBase(base - 32);
		glCallLists(strlen(msg), GL_UNSIGNED_BYTE, msg);
	glPopAttrib();
} // end PrintString()


⌨️ 快捷键说明

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