I. Complete game framework In the previous chapters, the game framework did not render the game's capabilities. It only performed some logic input operations and did not feed the screen back to the console interface. In this chapter, we will expand the functions of the framework to enable continuous rendering of game images. The extended functions include game rendering and console re-painting. The latter means partial update. Only the specified area is updated, and the images in other areas remain unchanged, reduces the flashes caused by painting. The following is an extension of the game framework class:
// cgame class implementation
Using system; using system. threading; using system. runtime. interopservices; using cgraphics; namespace cengine {// <summary> /// general game class /// </Summary> public abstract class cgame: icgame {# region API function [dllimport ("user32.dll")] Private Static extern intptr findwindow (string lpclassname, string lpwindowname); # endregion # region field // omitted... /// <summary> /// drawing /// </Summary> private cdraw m_draw; // <s Ummary> // re-paint event Delegate /// </Summary> /// <typeparam name = "teventargs"> </typeparam> // <Param name = "E "> </param> Public Delegate void painteventhandler <teventargs> (teventargs E ); /// <summary> /// re-paint event /// </Summary> private event painteventhandler <cpainteventargs> m_paint; # endregion # region constructor // <summary> // constructor // </Summary> Public cgame () {// omitted... m_draw = new cdraw (); // omitted... // Add game re-painting event m_p Aint + = new painteventhandler <cpainteventargs> (onredraw );} # endregion # region game running function // <summary> // game initialization //</Summary> protected abstract void gameinit (); /// <summary> /// game input /// </Summary> private void gameinput () {// process the mouse event this. getmousedevice (). mouseeventshandler (); // handle keyboard events this. getkeyboarddevice (). keyboardeventshandler ();} // <summary> // re-paint the game, which only occurs when the explicit update is enabled. /// </Summary> /// <Param Name = "E"> </param> protected virtual void onredraw (cpainteventargs e) {// The default value is to use the background color to erase m_draw.setdrawsymbol (csymbol. default); m_draw.fillrect (E. getclientrect (), console. backgroundcolor );} /// <summary> /// game rendering /// </Summary> /// <Param name = "Draw"> </param> protected abstract void gamedraw (cdraw draw ); /// <summary> /// game logic /// </Summary> protected abstract void gameloop (); /// <summary> // game End /// </Summary> protected abstract void gameexit (); # endregion // omitted... /// <summary> /// obtain the console region // </Summary> /// <returns> </returns> protected crect getclientrect () {return New crect (console. windowleft, console. windowtop, (console. required wwidth> 1)-1, console. windowheight) ;}/// <summary> // obtain the Drawing Object /// </Summary> /// <returns> </returns> protected cdraw getdraw () {return this. m_draw ;}/// <Summary> /// response to the re-painting event /// </Summary> /// <Param name = "E"> </param> private void onpaint (cpainteventargs E) {painteventhandler <cpainteventargs> temp = m_paint; If (temp! = NULL) {temp (e) ;}/// <summary> // The update operation causes the entire workspace to be repainted /// </Summary> protected void Update () {cpainteventargs E = new cpainteventargs (getclientrect (), getdraw (); this. onpaint (E); }/// <summary> // update operation causes repainting of the specified region /// </Summary> protected void Update (crect rect) {cpainteventargs E = new cpainteventargs (rect, getdraw (); this. onpaint (E);} // omitted... # region game startup interface // <summary> // game running // </Summary> Public void run () {// game initialization this. gameinit (); int32 starttime = 0; while (! This. isgameover () {// start time starttime = environment. tickcount; // calculate FPS this. setfps (); // enter this for the game. gameinput (); // game logic this. gameloop (); // game rendering this. gamedraw (m_draw); // maintain a certain FPS while (environment. tickcount-starttime <this. m_updaterate) {This. delay () ;}// the game exits this. gameexit (); // release game resources this. close () ;}# endregion // omitted ...}}
The implementation of the redraw event parameter class is as follows:
/// Cpainteventargs class implementation
using system; using cgraphics; namespace cengine {// // redraw event parameters /// Public sealed class cpainteventargs: eventargs {private crect m_rect; private cdraw m_draw; public cpainteventargs (crect rect, cdraw draw) {This. m_rect = rect; this. m_draw = draw;} public crect getclientrect () {return this. m_rect;} public void setclientrect (crect rect) {This. m_rect = rect;} public cdraw getdraw () {return this. m_draw;} public void setdraw (cdraw draw) {This. m_draw = draw ;}}
Now, the game framework class has been basically completed, and the sound module is not implemented yet. In the following chapters, we will use this framework to implement several mini games to familiarize ourselves with the use of this framework and optimize it as necessary to better meet the needs.
Ii. Conclusion In these chapters, a micro-game framework is gradually "stacked". We can see that the current framework is still very simple, and it is very general in terms of design, coding, and efficiency, we also need your comments and suggestions from various aspects. Even so, our framework is not useless. It can still be used to paste a few small games. Please refer to the next decomposition.