Hi, I'm new in the singleton world, I used to think that i was using singletons, but really i was using static variables instead
example: I have Core.h:
#pragma once
#define WIN32_LEAN_AND_MEAN
const bool FULL_SCREEN = false;
const bool VSYNC_ENABLED = true;
const float SCREEN_DEPTH = 1000.0f;
const float SCREEN_NEAR = 0.1f;
#include "Renderer.h"
#include "inputclass.h"
#include "Camera.h"
#include "ObjectsManager.h"
//
#include "MeshRenderer.h"
#include "ColorMaterial.h"
#include "ColorShader.h"
//
#include "Game.h"
class Core
{
private:
//Singleton
static Core* m_Core;
Core();
Core(const Core&);
~Core();
bool Frame(bool first_frame);
bool InitializeWindows(int&, int&);
void ShutdownWindows();
LPCWSTR m_applicationName;
HINSTANCE m_hinstance;
HWND m_hwnd;
//Singletons
Renderer* m_renderer;
ObjectsManager* m_Objmanager;
InputClass* m_Input;
Camera* m_Camera;
Game* m_game;
//test
//D3DClass* d3d;
//ID3D11Device* d3d_Device;
//ID3D11DeviceContext* d3d_DeviceContext;
ColorShader* color_shader;
public:
//Singleton
static Core* GetInstance()
{
m_Core = new Core;
return m_Core;
}
//Core();
bool Initialize();
void Shutdown();
void Run();
//get window handler
LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM);
//singletons
inline HWND* GetHwnd() { return &m_hwnd; }
inline Renderer* GetRenderer() { return m_renderer; }
inline ObjectsManager* GetObjManager() { return m_Objmanager; }
inline InputClass* GetInput() { return m_Input; }
inline Camera* GetCamera() { return m_Camera; }
inline Game* GetGame() { return m_game; }
inline ColorShader* GetColorShader() { return color_shader; }
};
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
in Core.cpp I have this line:
Core* Core::m_Core = 0;
I have the previous line in Renderer.cpp , Inputclass.cpp , .. and so on, in all my singletons classes.
So, in the cpp (where almost everything happens) I have initialize every singleton, Here is and example with renderer singleton inside Core::Initialize():
m_renderer = Renderer::GetInstance();
if (!m_renderer)return false;
result = m_renderer->Initialize(screenWidth, screenHeight, m_hwnd, SCREEN_DEPTH, SCREEN_NEAR);
if (!result)
{
return false;
}
in main.cpp:
#include "Core.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
{
bool result;
//Core Singleton
Core* m_Core = Core::GetInstance();
// Initialize and run the system object.
result = Core::GetInstance()->Initialize();
if (!result) return -1;
//.....
}
Questions:
1 - Should I call m_renderer every time i want my renderer singleton (same with others) or should I call Renderer::GetInstance() ??
2 - How can I initialize a singleton that depends of another singleton? (for example first I have to initialize d3d, then initialize
renderer)
3 - in main.cpp should I define a "m_Core" or use directly GetInstance()? what is the difference?
4 - Do I need to pass a singleton to a function? for example (inside core.cpp):
bool Core::Frame()
{
bool result;
//...
//some code
//...
result = m_renderer->Frame(m_Camera->GetViewMatrix() ,
m_Camera->GetProjectionMatrix() ,
m_Objmanager);
//...
//some code
//...
return true;
}
As you can see, i'm passing camera and objmanager singletons, can I call this singleton inside m_renderer->Frame? (previously i was using this and had too many problems, so i started to pass everything i need)
sorry about my english
Thanks!