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

📄 dinput.cpp

📁 This tutorial will deal with getting input using Direct Input. All you will need to run and/or com
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
   Direct Input tutorial.
   Created by the Programming Ace.
   www.UltimateGameProgramming.com

   The purpose of this tutorial is to process input using Direct Input (which
   is apart of DirectX).  This method is much faster because you will have access
   to the hardware Directly plus a lot of the behind the scenes work is hidden so
   dealing with input is much easier.  What this tutorial will do is whenever the
   user presses a mouse button the object being rendered will change.  Also the
   user can press the 1, 2, 3, or 4 key to change the objects with the keyboard.
   To get access to the keyboard and mouse we must create two objects of type
   LPDIRECTINPUTDEVICE8.  To gain access to Direct Input we need an object of type
   LPDIRECTINPUT8.  Once we create the Direct Input object using the function
   DirectInput8Create() you can then create each device using the function
   CreateDevice().  After that the rest of the setup involves setting flags and options
   using the SetCooperativeLevel() function and to gain access to the device using the
   Acquire() function.  Thats it.  When your program is executing you will update
   that states of each device to test if a button was pressed or if the joystick or mouse
   has moved, etc.  When it is all done you call the Shutdown() function to delete Direct
   Input and all its devices.  All of the code for it is in the CDirectInput.cpp file.
   Once you look through it you will see that there is not too much to Direct Input as
   you first thought.  To make things more organized we have 3 classes.  1 class for the mouse,
   1 class for the keyboard, and 1 for Direct Input.  In Direct Input there is objects for the
   mouse and the keyboard.  So instead of dealing with the keyboard and mouse classes we will
   just deal with the input system class, which in turn will deal with the two devices.
   After going through this file go through the CDirectInput.cpp and header to get the scope
   on how to create and use Direct Input.
*/

// Tip: It would help to be familar with DirectX but it is not neccessary.


// LESSON: Direct Input tutorial.


// NOTE: You must know C++ in order to write OpenGL applications.
//       Before you compile the OpenGL source code you must first
//       link the libraries.  Go to the Project, then settings,
//       then click on the link tab and include the opengl32.lib,
//       glu32.lib and glaux.lib.


// SPECIAL NOTE: In this tutorial I use a custom made icon for the .exe
//               and the application.  If you want to use this icon make
//               sure the UGP.rc is included in the project and make sure
//               to #include"resource.h".  The tutorials that uses custom
//               icons will already have this but if you want to create your
//               own icon you must go to insert, then resource, then choose icon.
//               Next you draw your icon (when you save the icon will be used
//               for the .exe) and when you call LoadIcon in the WinMain send
//               in the HINSTANCE variable in the WinMain parameter, and then
//               send in the icons ID.  To find the ID double click on the .rc
//               file (of cource after you have included it to your project)
//               and the name will be right there.  By default the first is called
//               IDI_ICON1.  As you can see I didn't change my icons ID name.
//               The process is real quick and easy and it can make you .exe's
//               look a little nicer.  resource.h is created when you save the .rc.
//               If you didn't want to use the icon in the app but only on the .exe
//               then when you call LoadIcon send NULL, then IDI_APPLICATION.


#define WIN32_LEAN_MEAN    // Trims down the libraries.
#define WIN32_EXTRA_LEAN   // Trims even further.

// Defines for which object we will display.  We will use our new input system to cycle
// through the list.
#define CUBE   0
#define SPHERE 1
#define TORUS  2
#define TEAPOT 3

#include<windows.h>           // Windows header file.
#include<gl/gl.h>             // OpenGL header.
#include<gl/glu.h>            // OpenGL utility.
#include<gl/glaux.h>          // Yet another OpenGL header.
#include"CDirectInput.h"      // Input class.
#include"resource.h"          // Holds the resource ID for the app icon.


// Function prototypes...
bool InitializeGL();          // Initialize OpenGL.
void RenderScene();           // Renders each frame.
void GetInput();              // Check the input for each frame.
bool CheckTime(float amount); // Simply used to restrict the input based on time.


// The first is a global HDC that will be used in the RenderScene() function to swap buffers.
HDC g_HDC;

// Light properties for the diffuse light, specular light, and light position.
float diffuseLight[] = {0.5, 0.5, 0.5, 1.0};
float specularLight[] = {1.0, 1.0, 1.0, 1.0};
float LightPosition[] = {0.0, 0.0, 10.0, 1.0};

// These are the x and y rotations for our objects.
float xRotation = 0.0f;
float yRotation = 0.0f;

// This will allow us to cycle through different objects for display.
int ObjectChoice = 0;

// Here is our Direct Input system object used to check for user input.
CDirectInputSystem DirectInput;


// The pixel format is an extension to the Win32 API that is provided
// for support of OpenGL functionality.
void SetupPixelFormat(HDC hDC)
{
   int nPixelFormat;

   static PIXELFORMATDESCRIPTOR pfd = {
         sizeof(PIXELFORMATDESCRIPTOR),   // size of structure.
         1,                               // always 1.
         PFD_DRAW_TO_WINDOW |             // support window
         PFD_SUPPORT_OPENGL |             // support OpenGl
         PFD_DOUBLEBUFFER,                // support double buffering
         PFD_TYPE_RGBA,                   // support RGBA
         32,                              // 32 bit color mode
         0, 0, 0, 0, 0, 0,                // ignore color bits
         0,                               // no alpha buffer
         0,                               // ignore shift bit
         0,                               // no accumulation buffer
         0, 0, 0, 0,                      // ignore accumulation bits.
         16,                              // number of depth buffer bits.
         0,                               // number of stencil buffer bits.
         0,                               // 0 means no auxiliary buffer
         PFD_MAIN_PLANE,                  // The main drawing plane
         0,                               // this is reserved
         0, 0, 0 };                       // layer masks ignored.



   // this chooses the best pixel format and returns index.
   nPixelFormat = ChoosePixelFormat(hDC, &pfd);

   // This set pixel format to device context.
   SetPixelFormat(hDC, nPixelFormat, &pfd);

   // Remember that its not important to fully understand the pixel format,
   // just remember to include in all of your applications and you'll be
   // good to go.
}


// This is the Windows procedure (WndProc).  This handles messages that the
// windows operating sends to your application.
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   static HGLRC hRC;    // Rendering context.
   static HDC hDC;      // Device context.
   int width, height;   // The window width and height.


   switch(message)
      {
         case WM_CREATE:                     // Windows creation.
            hDC = GetDC(hwnd);               // This gets the device context for our window.
            g_HDC = hDC;                     // Assigns the global device context to this one.
            SetupPixelFormat(hDC);           // Call the pixel format function.

            hRC = wglCreateContext(hDC);     // Creates the rendering context.
            wglMakeCurrent(hDC, hRC);        // Makes the rendering context.
            return 0;
            break;

         case WM_CLOSE:                      // Close message.
            wglMakeCurrent(hDC, NULL);
            wglDeleteContext(hRC);           // Deletes the rendering context.

            PostQuitMessage(0);              // Says close the program.
            return 0;
            break;

         case WM_SIZE:                       // re-size message.
            height = HIWORD(lParam);         // This gets the height of the window.
            width = LOWORD(lParam);          // This gets the width of the window.

            if(height==0)                    // we don't want it to be possible for a
               {                             // height of 0.  If it is 0 me make it 1.
                  height = 1;
               }

            glViewport(0, 0, width, height); // resets the viewport to new dimensions.
            glMatrixMode(GL_PROJECTION);     // Sets the projection matrix.
            glLoadIdentity();                // Reset the modelview matrix.

            // calculate the aspect ratio of the window.
            gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);

            glMatrixMode(GL_MODELVIEW);      // Sets the projection matrix.
            glLoadIdentity();                // Reset the modelview matrix.

            return 0;
            break;

         default: // Always have a default just in case.
            break;
      }
      // What this does is pass all of the unhandled messages to DefWindowProc.
      return (DefWindowProc(hwnd, message, wParam, lParam));
}

// The WinMain().  Main program entry point.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
   MSG msg;                // A message variable.
   WNDCLASSEX windowClass; // Your Window class.
   HWND hwnd;              // The Window handle.

   bool isFinished;        // This will be used to check if the program is done or not.

   // This is the Window class.  Each attribute is defined for the creation of the
   // window.  I will try to go over more in future tutorials but if you want the
   // complete list of all the different values you can pass, check out the MSDN.
   windowClass.cbSize = sizeof(WNDCLASSEX);        // size of the WNDCLASSEX structure.
   windowClass.style = CS_HREDRAW | CS_VREDRAW;    // style of the window.
   windowClass.lpfnWndProc = WndProc;              // Address to the windows procedure.
   windowClass.cbClsExtra = 0;                     // Extra class information.
   windowClass.cbWndExtra = 0;                     // Extra window information.
   windowClass.hInstance = hInstance;              // Handle of application Instance.
   // Handle of application Icon.
   windowClass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
   windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);// mouse cursor
   windowClass.hbrBackground = NULL;               // background color.
   windowClass.lpszMenuName = NULL;                // name of the main menu.
   windowClass.lpszClassName = "UltimateGameProgrammingClass";// window class name.
   // icon when minimized.
   windowClass.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));

   // You must register you class with Windows.  What this does is if the class is
   // not registered, then the program will close right away.
   if(!RegisterClassEx(&windowClass))
      return 0;

   // Create window.

⌨️ 快捷键说明

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