I started writing code.

xiaoxiao2021-03-06  39

First, a CGAME class is built first, and the basic part of the game control is packaged. The structure is as follows: #pragma overce

Class cgame {public: cgame (void); ~ cgame (void); // Start game int start (void); private: // Game message loop int gameloop (void); // Game running status BOOL BISRUNNING;}; then Put the message inside WinMain in Gameloop's position in WinMain originally messaging Game :: start () instead // // Start Game Int cgame :: Start (void) {// Read game settings

// Initialize DirectDraw

// bisrunning = TRUE;

Return gameloop ();

// Game message loop int CGAME :: gameloop (void) {msg msg;

While (Bisrunning) {IF (PeekMessage (& MSG, NULL, 0, 0, PM_Remove) {if (msg.Message == wm_quit) {// mydebug-> Outdebug ("Game Terminated ... / n"); / / break; bisrunning = false;} translateMessage (& MSG); DISPATCHMESSAGE (& MSG);}} Return Msg.wParam;} The message loop section also needs to be modified, waiting for the message when the game window is in the non-active state, to block the thread, reduce CPU resource is occupied. Inside the callback function wndproc, join the code in the active status: Case WM_ACTIVATE: // Application is active if (loword (wparam) == wa_inactive) gegame.setActive (false); else if (Hiword (wparam)) {Gegame.setActive (false);} else {setcursor (null); game.setActive (true);} There are two setActive (false), the principle is from GPGame's Holy Sword 2 Copy (now It seems that the game developed by the individual / small team is inseparable from the "sword" word). However, it should be noted that when recovery from non-active state, you need to re-acquire input, drawing equipment, etc. The approach of this CGAME class has come out. The source code is as follows:

//Game.cpp

#include "stdafx.h"

#include "./game.h"

CGAME :: Cgame (void): Bisrunning (false), BACTIVE (FALSE) {}

CGAME :: ~ cgame (void) {}

// // Start the game INT cgame :: start (void) {// read game settings

// Initialize DirectDraw

// Start game bisrunning = true; return gameloop ();

// Game message loop int CGAME :: gameloop (void) {msg msg;

while (bIsRunning) {if (PeekMessage (& msg, NULL, 0, 0, PM_REMOVE)) {if (msg.message == WM_QUIT) {bIsRunning = false; // exit the game break;} TranslateMessage (& msg); DispatchMessage (& msg } Else if (! "{Waitmessage (); // Waiting message} else {mainloop (); // Game main loop}} Return msg.wparam;} // Setting window is active void cgame :: setAntive Bool Active = Active;}

// Game main loop, responsible for updating status, drawing page, responding, etc. Void cgame :: mainloop (void) {Return;}

//Game.h

#pragma overce

CGAME {public: cgame (void); ~ cgame (void); // // Start game int start (void); private: // Game message loop int gameloop (void); // Game running status BOOL BISRUNNING; public : // Whether the window is active; // Set the window to active (Bool Active); // Game main loop, responsible for updating status, drawing, response, etc. Void mainloop (void);}; next time DirectX Initialized, 3:00 in the middle of the night, play sweep thousands of army, then sleep. . .

转载请注明原文地址:https://www.9cbs.com/read-59329.html

New Post(0)