Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Scene.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21#ifndef GF_SCENE_H
22#define GF_SCENE_H
23
24#include "Action.h"
25#include "EntityContainer.h"
26#include "Event.h"
27#include "GraphicsApi.h"
28#include "ModelContainer.h"
29#include "RenderWindow.h"
30#include "RenderStates.h"
31#include "Time.h"
32#include "ViewContainer.h"
33#include "Views.h"
34#include "Window.h"
35
36namespace gf {
37#ifndef DOXYGEN_SHOULD_SKIP_THIS
38inline namespace v1 {
39#endif
40
66 class GF_GRAPHICS_API Scene {
67 public:
77 Scene(Vector2i initialSize);
78
82 virtual ~Scene();
83
101 void processEvent(Event& event);
102
115 void handleActions(Window& window);
116
128 void update(Time time);
129
145 void render(RenderTarget& target, const RenderStates &states = RenderStates());
146
155
163 m_clear = color;
164 }
165
173 return m_clear;
174 }
175
192 void setActive(bool active = true);
193
201 bool isActive() const;
202
210 void pause();
211
217 void resume();
218
226 bool isPaused() const;
227
235 void hide();
236
242 void show();
243
251 bool isHidden() const;
252
268 m_views.addView(view);
269 }
270
276 void addAction(Action& action) {
277 m_actions.addAction(action);
278 }
279
285 void addModel(Model& model) {
286 m_models.addModel(model);
287 }
288
294 void addWorldEntity(Entity& entity) {
295 m_worldEntities.addEntity(entity);
296 }
297
304 return m_worldEntities.removeEntity(entity);
305 }
306
312 void addHudEntity(Entity& entity) {
313 m_hudEntities.addEntity(entity);
314 }
315
322 return m_hudEntities.removeEntity(entity);
323 }
324
340
347
352 protected:
356 void renderWorldEntities(RenderTarget& target, const RenderStates &states);
357
361 void renderHudEntities(RenderTarget& target, const RenderStates &states);
362
367 return m_worldView;
368 }
369
374 return m_hudView;
375 }
376
381 return m_worldEntities;
382 }
383
388 return m_hudEntities;
389 }
390
396 virtual bool doEarlyProcessEvent(Event& event);
397
401 virtual void doProcessEvent(Event& event);
402
406 virtual void doHandleActions(Window& window);
407
411 virtual void doUpdate(Time time);
412
416 virtual void doRender(RenderTarget& target, const RenderStates &states);
417
421 virtual void doPause();
422
426 virtual void doResume();
427
431 virtual void doHide();
432
436 virtual void doShow();
437
443 virtual void onActivityChange(bool active);
444
445 private:
446 friend class SceneManager;
447
448 bool m_active;
449
450 enum class Status {
451 Paused,
452 Resumed,
453 };
454
455 Status m_status;
456
457 enum class Visibility {
458 Shown,
459 Hidden,
460 };
461
462 Visibility m_visibility;
463
464 ActionContainer m_actions;
465 Action m_closeWindowAction;
466
467 ModelContainer m_models;
468
469 ExtendView m_worldView;
470 ScreenView m_hudView;
471 ViewContainer m_views;
472
473 EntityContainer m_worldEntities;
474 EntityContainer m_hudEntities;
475
476 Color4f m_clear;
477 };
478
479#ifndef DOXYGEN_SHOULD_SKIP_THIS
480}
481#endif
482}
483
484
485#endif // GF_SCENE_H
An action that can be triggered by different controls.
Definition: Action.h:53
Adaptative view.
Definition: View.h:390
A collection of entities.
Definition: EntityContainer.h:60
A game entity.
Definition: Entity.h:53
A game object that can be updated.
Definition: Model.h:46
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
A scene in the game.
Definition: Scene.h:66
virtual void doHandleActions(Window &window)
Customization point for handleActions()
void hide()
Hide the scene.
void processEvent(Event &event)
Process an event.
void addAction(Action &action)
Add an action to the scene.
Definition: Scene.h:276
virtual void doHide()
Customization point for hide()
void render(RenderTarget &target, const RenderStates &states=RenderStates())
Render the scene.
virtual void doProcessEvent(Event &event)
Customization point for processEvent()
View & getWorldView()
Get the world view.
Definition: Scene.h:366
EntityContainer & getWorldEntities()
Get the world entities.
Definition: Scene.h:380
virtual void doUpdate(Time time)
Customization point for update()
void setWorldViewSize(Vector2f size)
Set the size of the world view.
void setFramebufferSize(gf::Vector2i size)
Update the framebuffer size.
virtual ~Scene()
Destructor.
void setClearColor(gf::Color4f color)
Set the new clear color.
Definition: Scene.h:162
void addWorldEntity(Entity &entity)
Add a world entity to the scene.
Definition: Scene.h:294
void setActive(bool active=true)
Change the active state of the scene.
Entity * removeHudEntity(Entity *entity)
Remove a HUD entity from the scene.
Definition: Scene.h:321
bool isHidden() const
Check if the scene is hidden.
Entity * removeWorldEntity(Entity *entity)
Remove a world entity from the scene.
Definition: Scene.h:303
void show()
Show the scene.
void addModel(Model &model)
Add a model to the scene.
Definition: Scene.h:285
void addHudEntity(Entity &entity)
Add a HUD entity to the scene.
Definition: Scene.h:312
void resume()
Resume the scene.
void pause()
Pause the scene.
virtual void doResume()
Customization point for resume()
virtual void onActivityChange(bool active)
Callback when the scene becomes active or inactive.
virtual void doRender(RenderTarget &target, const RenderStates &states)
Customization point for render()
bool isActive() const
Check if the scene is active.
void addView(AdaptativeView &view)
Add a view to the scene.
Definition: Scene.h:267
gf::Color4f getClearColor() const
Get the current clear color.
Definition: Scene.h:172
void handleActions(Window &window)
Handle actions.
EntityContainer & getHudEntities()
Get the HUD entities.
Definition: Scene.h:387
virtual void doShow()
Customization point for show()
View & getHudView()
Get the HUD view.
Definition: Scene.h:373
virtual void doPause()
Customization point for pause()
void setWorldViewCenter(Vector2f center)
Set the center of the world view.
bool isPaused() const
Check if the scene is paused.
void renderWorldEntities(RenderTarget &target, const RenderStates &states)
Render the world entities.
void renderHudEntities(RenderTarget &target, const RenderStates &states)
Render the HUD entities.
Scene(Vector2i initialSize)
Constructor.
void update(Time time)
Update the scene.
virtual bool doEarlyProcessEvent(Event &event)
Customization point for processEvent()
A scene manager.
Definition: SceneManager.h:61
Represents a time value.
Definition: Time.h:65
2D camera that defines what region is shown on framebuffer
Definition: View.h:94
An OS window.
Definition: Window.h:97
Color4< float > Color4f
A float color vector with 4 components.
Definition: Vector.h:1287
@ Hidden
The window is hidden (data in event.window)
@ Shown
The window is shown (data in event.window)
The namespace for gf classes.
Defines a system event and its parameters.
Definition: Event.h:224
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A 4D vector.
Definition: Vector.h:852