Angel 3.2
A 2D Game Prototyping Engine
World.h
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #pragma once
31 
32 #include "../Infrastructure/Renderable.h"
33 #include "../Infrastructure/RenderableIterator.h"
34 #include "../Infrastructure/GameManager.h"
35 #include "../Infrastructure/DebugDraw.h"
36 #include "../Messaging/Message.h"
37 
38 #include <Box2D/Box2D.h>
39 
40 //forward declarations
41 class Actor;
42 class PhysicsActor;
43 class Console;
44 
45 #define MAX_TIMESTEP 1.0f
46 #define MAX_CONTACT_POINTS 2048
47 
48 //singleton shortcut
49 #define theWorld World::GetInstance()
50 
52 
66 class World : public b2ContactListener, public MessageListener
67 {
68 public:
75  static World& GetInstance();
76 
101  bool Initialize(unsigned int windowWidth=1024, unsigned int windowHeight=768, String windowName="Angel Engine", bool antiAliasing=false, bool fullScreen=false, bool resizable=false);
102 
113  std::vector<Vec3ui> GetVideoModes();
114 
123  void AdjustWindow(int windowWidth, int windowHeight, const String& windowName);
124 
132  void MoveWindow(int xPosition, int yPosition);
133 
134  #if !ANGEL_MOBILE
135 
143  GLFWwindow* GetMainWindow();
144  #endif
145 
160  bool SetupPhysics(const Vector2& gravity = Vector2(0, -10), const Vector2& maxVertex = Vector2(100.0f, 100.0f), const Vector2& minVertex = Vector2(-100.0f, -100.0f));
161 
165  void Destroy();
166 
172  void ResetWorld();
173 
178  void StartGame();
179 
184  void StopGame();
185 
191  void ScriptExec(const String& code);
192 
205  void LoadLevel(const String& levelName);
206 
214  const float GetDT();
215 
223  const bool PauseSimulation();
224 
231  const bool ResumeSimulation();
232 
238  const bool PausePhysics();
239 
246  const bool ResumePhysics();
247 
253  void SetBackgroundColor(const Color& bgColor);
254 
262  void Add(Renderable *newElement, int layer = 0);
263 
270  void Add(Renderable *newElement, const String& layer);
271 
278  void Remove(Renderable *oldElement);
279 
286  void UpdateLayer(Renderable* element, int newLayer);
287 
294  void UpdateLayer(Renderable* element, const String& newLayerName);
295 
303  void NameLayer(const String& name, int number);
304 
312  const int GetLayerByName(const String& name);
313 
319  RenderLayers& GetLayers() { return _layers; }
320 
327  void RegisterConsole(Console* console);
328 
334  Console* GetConsole();
335 
341  b2World &GetPhysicsWorld();
342 
349  const bool IsPhysicsSetUp() { return _physicsSetUp; }
350 
355  void WakeAllPhysics();
356 
361  virtual void BeginContact(b2Contact* contact);
362 
367  virtual void EndContact(b2Contact* contact);
368 
378  void SetSideBlockers(bool turnOn, float restitution=-1.0f);
379 
385  float GetCurrentTimeSeconds() { return ( float(_currTime) ); }
386 
393  float GetTimeSinceSeconds( float lastTime ) {return GetCurrentTimeSeconds() - lastTime;}
394 
403  void DrawDebugLine( const Vector2& a, const Vector2& b, float time = 5.f, const Color& color = Color(1.f, 0.f, 0.f) );
404 
409  void PurgeDebugDrawing();
410 
416  const bool IsSimulationOn();
417 
423  void SetGameManager(GameManager* gameManager);
424 
430  GameManager* GetGameManager() { return _gameManager; }
431 
439  {
440  RenderableIterator iter;
441  return iter.begin();
442  }
443 
450  {
451  RenderableIterator iter;
452  return iter.end();
453  }
454 
459  void UnloadAll();
460 
468  virtual void ReceiveMessage(Message *m);
469 
476  const bool IsHighResScreen() { return _highResScreen; }
477 
485  void SetHighResolutionScreen(bool highRes) { _highResScreen = highRes; }
486 
492  const bool IsAntiAliased() { return _antiAliased; }
493 
498  void TickAndRender();
499 
504  void Tick();
505 
510  void Render();
511 
516  void SetDT(float dt)
517  {
518  #if ANGEL_MOBILE
519  _systemEstimatedDT = dt;
520  #endif
521  }
522 
523 protected:
524  World();
525  static World* s_World;
526  float CalculateNewDT();
527  void UpdateRenderables(float frame_dt);
528  void CleanupRenderables();
529  void DrawRenderables();
530  void Simulate(bool simRunning);
531  void ProcessDeferredAdds();
532  void ProcessDeferredLayerChanges();
533  void ProcessDeferredRemoves();
534  void RunPhysics(float frame_dt);
535  void UpdateDebugItems(float frame_dt);
536  void DrawDebugItems();
537 
538 private:
539  struct RenderableLayerPair
540  {
541  Renderable* _renderable;
542  int _layer;
543  };
544  bool _running;
545  #if ANGEL_MOBILE
546  float _startTime;
547  float _systemEstimatedDT;
548  #else
549  GLFWwindow* _mainWindow;
550  #endif
551  bool _highResScreen;
552  bool _antiAliased;
553  float _prevTime;
554  float _currTime;
555  float _dt;
556  float _physicsRemainderDT;
557 
558  bool _simulateOn;
559  bool _initialized;
560  bool _started;
561 
562 
563  RenderLayers _layers;
564 
565  bool _processingDeferredAdds;
566  std::vector<RenderableLayerPair> _deferredAdds;
567  std::vector<RenderableLayerPair> _deferredLayerChanges;
568  std::vector<Renderable*> _deferredRemoves;
569  std::map<String, int> _layerNames;
570 
571  bool _elementsLocked;
572 
573  b2World *_physicsWorld;
574  bool _physicsSetUp;
575  bool _physicsRunning;
576 
577  void SendCollisionNotifications(b2Contact* cp, bool beginning);
578  std::map< PhysicsActor*, ActorSet > _currentTouches;
579 
580  bool _blockersOn;
581  float _blockerRestitution;
582  PhysicsActor* _blockers[4];
583 
584  std::vector< DebugDrawBase* > _debugDrawItems;
585 
586  GameManager* _gameManager;
587 
588  Console* _console;
589 };