GameCode.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #pragma once
  2. //========================================================================
  3. // GameCode.h : Defines the entry point for the application.
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. #include "../Mainloop/Initialization.h"
  40. #include "../Debugging/Logger.h"
  41. #include "BaseGameLogic.h"
  42. #include <SDL3/SDL.h>
  43. class EventManager;
  44. //
  45. // class GameCodeApp - Chapter X, page Y
  46. //
  47. class GameCodeApp
  48. {
  49. friend class OpenGLRenderer46;
  50. friend class LogMgr;
  51. protected:
  52. SDL_Window *m_Window;
  53. SDL_Renderer *m_SdlRenderer;
  54. SDL_GLContext m_GlContext;
  55. bool m_bIsRunning; // true if everything is initialized and the game is in the main loop
  56. bool m_bQuitRequested; // true if the app should run the exit sequence
  57. bool m_bQuitting; // true if the app is running the exit sequence
  58. Point m_screenSize; // game screen size
  59. bool m_bIsEditorRunning; // true if the game editor is running
  60. public:
  61. const Point &GetScreenSize() { return m_screenSize; }
  62. protected:
  63. std::map<std::string,std::string> m_textResource;
  64. std::map<std::string,unsigned int> m_hotkeys;
  65. public:
  66. GameCodeApp();
  67. // Game Application Data
  68. // You must define these in an inherited
  69. // class - see TeapotWarsApp for an example
  70. // TODO(munnellg): uncomment and implement
  71. virtual std::string VGetGameTitle()=0;
  72. // virtual TCHAR *VGetGameAppDirectory()=0;
  73. // virtual SDL_Surface* VGetIcon()=0;
  74. SDL_Window* GetWindow() { return m_Window; }
  75. static int MsgProc(SDL_Event &e);
  76. // TODO(munnellg): uncomment and implement
  77. // int OnDisplayChange(int colorDepth, int width, int height);
  78. // int OnPowerBroadcast(int event);
  79. int OnSysCommand(SDL_Event const &e);
  80. // int OnClose();
  81. // TODO(munnellg): uncomment and implement
  82. // Game Application actions
  83. // int OnAltEnter();
  84. // int OnNcCreate(LPCREATESTRUCT cs);
  85. bool LoadStrings(std::string language);
  86. std::string GetString(std::string sID);
  87. virtual bool InitInstance(/*HINSTANCE hInstance, LPWSTR lpCmdLine, HWND hWnd = NULL, */int screenWidth = SCREEN_WIDTH, int screenHeight = SCREEN_HEIGHT);
  88. // FUTURE WORK - Seems like this class could be refactored, especailly the renderer !
  89. enum Renderer
  90. {
  91. Renderer_Unknown,
  92. Renderer_OpenGL46,
  93. };
  94. std::shared_ptr<IRenderer> m_Renderer;
  95. static Renderer GetRendererImpl();
  96. // OpenGL 4.6 Specific Stuff
  97. // static bool CALLBACK IsD3D11DeviceAcceptable( const CD3D11EnumAdapterInfo *AdapterInfo, UINT Output, const CD3D11EnumDeviceInfo *DeviceInfo, DXGI_FORMAT BackBufferFormat, bool bWindowed, void* pUserContext );
  98. // static HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
  99. // static HRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
  100. // static void CALLBACK OnD3D11ReleasingSwapChain( void* pUserContext );
  101. // static void CALLBACK OnD3D11DestroyDevice( void* pUserContext );
  102. static void OnOpenGLFrameRender(double fTime, float fElapsedTime);
  103. static void OnUpdateGame(double fTime, float fElapsedTime);
  104. // GameCode Specific Stuff
  105. BaseGameLogic *m_pGame;
  106. struct GameOptions m_Options;
  107. // You must define these functions to initialize your game.
  108. // Look for them in TeapotWars.h and TeapotWars.cpp
  109. virtual BaseGameLogic *VCreateGameAndView()=0;
  110. virtual bool VLoadGame(void);
  111. // HumanView* GetHumanView(); // Added post press - it was convenient to grab the HumanView attached to the game.
  112. // File and Resource System
  113. class ResCache *m_ResCache;
  114. // Event manager
  115. EventManager *m_pEventManager;
  116. public:
  117. // [gmunnelly] added for SDL/OpenGL compatibility. We must own the main loop
  118. void RunMainLoop();
  119. void Shutdown();
  120. void PumpSDLEvents();
  121. // Main loop processing
  122. void AbortGame() { m_bQuitting = true; }
  123. // TODO(munnellg): get proper exit code
  124. int GetExitCode() { return 0; }
  125. bool IsRunning() { return m_bIsRunning; }
  126. void SetQuitting(bool quitting) { m_bQuitting = quitting; }
  127. BaseGameLogic* GetGameLogic(void) const { return m_pGame; }
  128. };
  129. extern GameCodeApp *g_pApp;