Browse Source

scene: use a global g_scene object instead of Scene::GetDefault().

legacy
Sam Hocevar sam 11 years ago
parent
commit
ad8144f0e8
18 changed files with 68 additions and 68 deletions
  1. +4
    -4
      src/debug/lines.cpp
  2. +3
    -3
      src/easymesh/easymesh.cpp
  3. +2
    -2
      src/emitter.cpp
  4. +1
    -3
      src/font.cpp
  5. +0
    -3
      src/gpu/rendercontext.cpp
  6. +3
    -1
      src/gpu/renderer.cpp
  7. +2
    -2
      src/gradient.cpp
  8. +5
    -6
      src/layer.cpp
  9. +1
    -1
      src/light.cpp
  10. +12
    -14
      src/scene.cpp
  11. +7
    -4
      src/scene.h
  12. +1
    -2
      src/sprite.cpp
  13. +2
    -2
      src/ticker.cpp
  14. +4
    -0
      src/video.cpp
  15. +2
    -2
      test/btphystest.cpp
  16. +13
    -13
      test/meshviewer.cpp
  17. +2
    -2
      tutorial/05_easymesh.cpp
  18. +4
    -4
      tutorial/06_sprite.cpp

+ 4
- 4
src/debug/lines.cpp View File

@@ -19,7 +19,7 @@ namespace lol


void Debug::DrawLine(vec3 a, vec3 b, vec4 color) void Debug::DrawLine(vec3 a, vec3 b, vec4 color)
{ {
Scene::GetDefault()->AddLine(a, b, color);
g_scene->AddLine(a, b, color);
} }


void Debug::DrawBox(vec3 a, vec3 b, vec4 color) void Debug::DrawBox(vec3 a, vec3 b, vec4 color)
@@ -36,9 +36,9 @@ void Debug::DrawBox(vec3 a, vec3 b, vec4 color)
{ {
int j = ((i & 1) << 1) | ((i >> 1) ^ 1); int j = ((i & 1) << 1) | ((i >> 1) ^ 1);


Scene::GetDefault()->AddLine(v[i], v[i + 4], color);
Scene::GetDefault()->AddLine(v[i], v[j], color);
Scene::GetDefault()->AddLine(v[i + 4], v[j + 4], color);
g_scene->AddLine(v[i], v[i + 4], color);
g_scene->AddLine(v[i], v[j], color);
g_scene->AddLine(v[i + 4], v[j + 4], color);
} }
} }




+ 3
- 3
src/easymesh/easymesh.cpp View File

@@ -133,15 +133,15 @@ void DefaultShaderData::SetupDefaultData(bool with_UV)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void DefaultShaderData::SetupShaderDatas(mat4 const &model) void DefaultShaderData::SetupShaderDatas(mat4 const &model)
{ {
mat4 proj = Scene::GetDefault()->GetCamera()->GetProjection();
mat4 view = Scene::GetDefault()->GetCamera()->GetView();
mat4 proj = g_scene->GetCamera()->GetProjection();
mat4 view = g_scene->GetCamera()->GetView();
mat4 modelview = view * model; mat4 modelview = view * model;
mat3 normalmat = transpose(inverse(mat3(modelview))); mat3 normalmat = transpose(inverse(mat3(modelview)));


/* FIXME: this should be hidden in the shader */ /* FIXME: this should be hidden in the shader */
/* FIXME: the 4th component of the position can be used for other things */ /* FIXME: the 4th component of the position can be used for other things */
/* FIXME: GetUniform("blabla") is costly */ /* FIXME: GetUniform("blabla") is costly */
Array<Light *> const lights = Scene::GetDefault()->GetLights();
Array<Light *> const lights = g_scene->GetLights();
Array<vec4> light_data; Array<vec4> light_data;
for (int i = 0; i < lights.Count(); ++i) for (int i = 0; i < lights.Count(); ++i)
light_data << lights[i]->GetPosition() << lights[i]->GetColor(); light_data << lights[i]->GetPosition() << lights[i]->GetColor();


+ 2
- 2
src/emitter.cpp View File

@@ -73,8 +73,8 @@ void Emitter::TickDraw(float seconds)
Entity::TickDraw(seconds); Entity::TickDraw(seconds);


for (int i = 0; i < data->nparticles; i++) for (int i = 0; i < data->nparticles; i++)
Scene::GetDefault()->AddTile(data->tileset, data->particles[i],
data->positions[i], 0, vec2(1.0f));
g_scene->AddTile(data->tileset, data->particles[i],
data->positions[i], 0, vec2(1.0f));
} }


void Emitter::AddParticle(int id, vec3 pos, vec3 vel) void Emitter::AddParticle(int id, vec3 pos, vec3 vel)


+ 1
- 3
src/font.cpp View File

@@ -69,14 +69,12 @@ char const *Font::GetName()


void Font::Print(vec3 pos, char const *str, vec2 scale) void Font::Print(vec3 pos, char const *str, vec2 scale)
{ {
Scene *scene = Scene::GetDefault();

while (*str) while (*str)
{ {
uint32_t ch = (uint8_t)*str++; uint32_t ch = (uint8_t)*str++;


if (ch != ' ') if (ch != ' ')
scene->AddTile(data->tileset, ch & 255, pos, 0, scale);
g_scene->AddTile(data->tileset, ch & 255, pos, 0, scale);


pos.x += data->size.x * scale.x; pos.x += data->size.x * scale.x;
} }


+ 0
- 3
src/gpu/rendercontext.cpp View File

@@ -52,8 +52,6 @@ class RenderContextData
friend class RenderContext; friend class RenderContext;


private: private:
Scene *m_scene;

TrackedState<ibox2> m_viewport; TrackedState<ibox2> m_viewport;
TrackedState<vec4> m_clear_color; TrackedState<vec4> m_clear_color;
TrackedState<float> m_clear_depth; TrackedState<float> m_clear_depth;
@@ -71,7 +69,6 @@ private:
RenderContext::RenderContext() RenderContext::RenderContext()
: m_data(new RenderContextData()) : m_data(new RenderContextData())
{ {
m_data->m_scene = Scene::GetDefault();
} }


RenderContext::~RenderContext() RenderContext::~RenderContext()


+ 3
- 1
src/gpu/renderer.cpp View File

@@ -44,11 +44,13 @@ namespace lol
/* /*
* The global g_renderer object, initialised by Video::Init * The global g_renderer object, initialised by Video::Init
*/ */
Renderer *g_renderer;

Renderer *g_renderer = nullptr;


/* /*
* Private RendererData class * Private RendererData class
*/ */

class RendererData class RendererData
{ {
friend class Renderer; friend class Renderer;


+ 2
- 2
src/gradient.cpp View File

@@ -94,9 +94,9 @@ void Gradient::TickDraw(float seconds)
data->shader->Bind(); data->shader->Bind();


uni_mat = data->shader->GetUniformLocation("proj_matrix"); uni_mat = data->shader->GetUniformLocation("proj_matrix");
data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetCamera()->GetProjection());
data->shader->SetUniform(uni_mat, g_scene->GetCamera()->GetProjection());
uni_mat = data->shader->GetUniformLocation("view_matrix"); uni_mat = data->shader->GetUniformLocation("view_matrix");
data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetCamera()->GetView());
data->shader->SetUniform(uni_mat, g_scene->GetCamera()->GetView());
uni_mat = data->shader->GetUniformLocation("model_matrix"); uni_mat = data->shader->GetUniformLocation("model_matrix");
data->shader->SetUniform(uni_mat, model_matrix); data->shader->SetUniform(uni_mat, model_matrix);




+ 5
- 6
src/layer.cpp View File

@@ -51,15 +51,14 @@ void Layer::Render(int /* x */, int /* y */, int /* z */)
if (error && !(error = 0)) if (error && !(error = 0))
Log::Error("FIXME: Layer::Render no longer works\n"); Log::Error("FIXME: Layer::Render no longer works\n");
#if 0 #if 0
Scene *scene = Scene::GetDefault();
for (int j = 0; j < height; j++) for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++) for (int i = 0; i < width; i++)
if (data[j * width + i]) if (data[j * width + i])
scene->AddTile(data[j * width + i],
vec3(x + i * 32,
y + j * 32 - altitude,
altitude + z),
orientation);
g_scene->AddTile(data[j * width + i],
vec3(x + i * 32,
y + j * 32 - altitude,
altitude + z),
orientation);
#endif #endif
} }




+ 1
- 1
src/light.cpp View File

@@ -64,7 +64,7 @@ void Light::TickDraw(float seconds)
{ {
WorldEntity::TickDraw(seconds); WorldEntity::TickDraw(seconds);


Scene::GetDefault()->AddLight(this);
g_scene->AddLight(this);
} }


} /* namespace lol */ } /* namespace lol */


+ 12
- 14
src/scene.cpp View File

@@ -28,6 +28,16 @@ LOLFX_RESOURCE_DECLARE(line);
namespace lol namespace lol
{ {


/*
* The global g_scene object, initialised by Video::Init
*/

Scene *g_scene = nullptr;

/*
* A quick and dirty Tile structure for 2D blits
*/

struct Tile struct Tile
{ {
TileSet *tileset; TileSet *tileset;
@@ -71,23 +81,18 @@ private:


Camera *m_default_cam; Camera *m_default_cam;
Array<Camera *> m_camera_stack; Array<Camera *> m_camera_stack;

static Scene *scene;
}; };


Scene *SceneData::scene = nullptr;

/* /*
* Public Scene class * Public Scene class
*/ */


Scene::Scene()
Scene::Scene(ivec2 size)
: data(new SceneData()) : data(new SceneData())
{ {
/* Create a default orthographic camera, in case the user doesn’t. */ /* Create a default orthographic camera, in case the user doesn’t. */
data->m_default_cam = new Camera(); data->m_default_cam = new Camera();
mat4 proj = mat4::ortho(0, Video::GetSize().x, 0, Video::GetSize().y,
-1000.f, 1000.f);
mat4 proj = mat4::ortho(0, size.x, 0, size.y, -1000.f, 1000.f);
data->m_default_cam->SetProjection(proj); data->m_default_cam->SetProjection(proj);
PushCamera(data->m_default_cam); PushCamera(data->m_default_cam);


@@ -114,13 +119,6 @@ Scene::~Scene()
delete data; delete data;
} }


Scene *Scene::GetDefault()
{
if (!SceneData::scene)
SceneData::scene = new Scene();
return SceneData::scene;
}

void Scene::PushCamera(Camera *cam) void Scene::PushCamera(Camera *cam)
{ {
Ticker::Ref(cam); Ticker::Ref(cam);


+ 7
- 4
src/scene.h View File

@@ -29,12 +29,13 @@ class SceneData;


class Scene class Scene
{ {
public:
Scene();
~Scene();
friend class Video;


static Scene *GetDefault();
private:
Scene(ivec2 size);
~Scene();


public:
Camera *GetCamera(); Camera *GetCamera();
void PushCamera(Camera *cam); void PushCamera(Camera *cam);
void PopCamera(Camera *cam); void PopCamera(Camera *cam);
@@ -54,6 +55,8 @@ private:
SceneData *data; SceneData *data;
}; };


extern Scene *g_scene;

} /* namespace lol */ } /* namespace lol */


#endif // __LOL_SCENE_H__ #endif // __LOL_SCENE_H__


+ 1
- 2
src/sprite.cpp View File

@@ -50,8 +50,7 @@ void Sprite::TickDraw(float seconds)
{ {
Entity::TickDraw(seconds); Entity::TickDraw(seconds);


Scene::GetDefault()->AddTile(data->tileset, data->id, m_position,
0, vec2(1.0f));
g_scene->AddTile(data->tileset, data->id, m_position, 0, vec2(1.0f));
} }


Sprite::~Sprite() Sprite::~Sprite()


+ 2
- 2
src/ticker.cpp View File

@@ -386,7 +386,7 @@ void TickerData::DrawThreadTick()
switch (i) switch (i)
{ {
case Entity::DRAWGROUP_BEGIN: case Entity::DRAWGROUP_BEGIN:
Scene::GetDefault()->Reset();
g_scene->Reset();
g_renderer->Clear(ClearMask::All); g_renderer->Clear(ClearMask::All);
break; break;
default: default:
@@ -412,7 +412,7 @@ void TickerData::DrawThreadTick()
} }


/* Do this render step */ /* Do this render step */
Scene::GetDefault()->Render();
g_scene->Render();
} }


Profiler::Stop(Profiler::STAT_TICK_DRAW); Profiler::Stop(Profiler::STAT_TICK_DRAW);


+ 4
- 0
src/video.cpp View File

@@ -51,6 +51,7 @@ DebugRenderMode VideoData::render_mode = DebugRenderMode::Default;
void Video::Setup(ivec2 size) void Video::Setup(ivec2 size)
{ {
g_renderer = new Renderer(size); g_renderer = new Renderer(size);
g_scene = new Scene(size);


/* Initialise reasonable scene default properties */ /* Initialise reasonable scene default properties */
SetDebugRenderMode(DebugRenderMode::Default); SetDebugRenderMode(DebugRenderMode::Default);
@@ -114,6 +115,9 @@ DebugRenderMode Video::GetDebugRenderMode()


void Video::Destroy() void Video::Destroy()
{ {
delete g_scene;
g_scene = nullptr;

delete g_renderer; delete g_renderer;
g_renderer = nullptr; g_renderer = nullptr;
} }


+ 2
- 2
test/btphystest.cpp View File

@@ -56,7 +56,7 @@ BtPhysTest::BtPhysTest(bool editor)
vec3(0.f, 0.f, 0.f), vec3(0.f, 0.f, 0.f),
vec3(0, 1, 0)); vec3(0, 1, 0));
m_camera->SetProjection(mat4::perspective(45.f, 1280.f, 960.f, .1f, 1000.f)); m_camera->SetProjection(mat4::perspective(45.f, 1280.f, 960.f, .1f, 1000.f));
Scene::GetDefault()->PushCamera(m_camera);
g_scene->PushCamera(m_camera);


m_ready = false; m_ready = false;


@@ -421,7 +421,7 @@ void BtPhysTest::TickDraw(float seconds)


BtPhysTest::~BtPhysTest() BtPhysTest::~BtPhysTest()
{ {
Scene::GetDefault()->PopCamera(m_camera);
g_scene->PopCamera(m_camera);
Ticker::Unref(m_light1); Ticker::Unref(m_light1);
Ticker::Unref(m_light2); Ticker::Unref(m_light2);




+ 13
- 13
test/meshviewer.cpp View File

@@ -61,9 +61,9 @@ public:
void SetFov(float new_fov=60.0f, vec2 video_size = vec2(Video::GetSize())) void SetFov(float new_fov=60.0f, vec2 video_size = vec2(Video::GetSize()))
{ {
if (new_fov > MIN_FOV) if (new_fov > MIN_FOV)
Scene::GetDefault()->GetCamera()->SetProjection(mat4::perspective(new_fov, video_size.x, video_size.y, .1f, 1000.f));
g_scene->GetCamera()->SetProjection(mat4::perspective(new_fov, video_size.x, video_size.y, .1f, 1000.f));
else else
Scene::GetDefault()->GetCamera()->SetProjection(mat4::ortho(video_size.x, video_size.y, .1f, 1000.f));
g_scene->GetCamera()->SetProjection(mat4::ortho(video_size.x, video_size.y, .1f, 1000.f));
} }


MeshViewer(char const *file_name = "data/mesh-buffer.txt") MeshViewer(char const *file_name = "data/mesh-buffer.txt")
@@ -107,7 +107,7 @@ public:
m_camera->SetView(vec3(0.f, 0.f, 10.f), m_camera->SetView(vec3(0.f, 0.f, 10.f),
vec3(0.f, 0.f, 0.f), vec3(0.f, 0.f, 0.f),
vec3(0.f, 1.f, 0.f)); vec3(0.f, 1.f, 0.f));
Scene::GetDefault()->PushCamera(m_camera);
g_scene->PushCamera(m_camera);


//Lights setup //Lights setup
m_lights << new Light(); m_lights << new Light();
@@ -144,7 +144,7 @@ public:


~MeshViewer() ~MeshViewer()
{ {
Scene::GetDefault()->PopCamera(m_camera);
g_scene->PopCamera(m_camera);
for (int i = 0; i < m_lights.Count(); ++i) for (int i = 0; i < m_lights.Count(); ++i)
Ticker::Unref(m_lights[i]); Ticker::Unref(m_lights[i]);
} }
@@ -178,8 +178,8 @@ public:
if (m_meshes[mesh_id].m2) if (m_meshes[mesh_id].m2)
break; break;


mat4 world_cam = Scene::GetDefault()->GetCamera()->GetView();
mat4 cam_screen = Scene::GetDefault()->GetCamera()->GetProjection();
mat4 world_cam = g_scene->GetCamera()->GetView();
mat4 cam_screen = g_scene->GetCamera()->GetProjection();


if (m_meshes.Count() && mesh_id >= 0) if (m_meshes.Count() && mesh_id >= 0)
{ {
@@ -250,13 +250,13 @@ public:
SetFov(m_fov_damp); SetFov(m_fov_damp);


//Move modification //Move modification
vec3 campos = Scene::GetDefault()->GetCamera()->GetPosition();
vec3 campos = g_scene->GetCamera()->GetPosition();
if (m_fov_damp < MIN_FOV) if (m_fov_damp < MIN_FOV)
Scene::GetDefault()->GetCamera()->SetView(vec3(campos.xy, 10.f), quat(1.f));
g_scene->GetCamera()->SetView(vec3(campos.xy, 10.f), quat(1.f));
else if (fov_ratio > .0f) else if (fov_ratio > .0f)
Scene::GetDefault()->GetCamera()->SetView(vec3(campos.xy, campos.z * fov_ratio * 1.1f), quat(1.f));
g_scene->GetCamera()->SetView(vec3(campos.xy, campos.z * fov_ratio * 1.1f), quat(1.f));
#else #else
Camera* cur_cam = Scene::GetDefault()->GetCamera();
Camera* cur_cam = g_scene->GetCamera();
vec3 min_max_diff = (cam_min_max[1] - cam_min_max[0]); vec3 min_max_diff = (cam_min_max[1] - cam_min_max[0]);
float screen_size = max(max(lol::abs(min_max_diff.x), lol::abs(min_max_diff.y)), float screen_size = max(max(lol::abs(min_max_diff.x), lol::abs(min_max_diff.y)),
max( lol::abs(min_max_diff.x), lol::abs(min_max_diff.y))); max( lol::abs(min_max_diff.x), lol::abs(min_max_diff.y)));
@@ -442,7 +442,7 @@ public:


g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));


mat4 default_proj = Scene::GetDefault()->GetCamera()->GetProjection();
mat4 default_proj = g_scene->GetCamera()->GetProjection();
int max_drawn = m_meshes.Count() - m_mesh_shown; int max_drawn = m_meshes.Count() - m_mesh_shown;
for (int i = max_drawn; i < m_meshes.Count(); i++) for (int i = max_drawn; i < m_meshes.Count(); i++)
m_meshes[i].m4 = vec3(.0f); m_meshes[i].m4 = vec3(.0f);
@@ -461,7 +461,7 @@ public:
} }
m_meshes[i].m4 = damp(m_meshes[i].m4, new_mesh_offset, .35f, seconds); m_meshes[i].m4 = damp(m_meshes[i].m4, new_mesh_offset, .35f, seconds);


Scene::GetDefault()->GetCamera()->SetProjection(
g_scene->GetCamera()->SetProjection(
mat4::translate(m_meshes[i].m4) * mat4::translate(m_meshes[i].m4) *
mat4::translate(vec3(m_mesh_screen_offset_damp, .0f)) * mat4::translate(vec3(m_mesh_screen_offset_damp, .0f)) *
mat4::scale(vec3(vec2(m_meshes[i].m3), 1.0f)) * mat4::scale(vec3(vec2(m_meshes[i].m3), 1.0f)) *
@@ -475,7 +475,7 @@ public:
g_renderer->Clear(ClearMask::Depth); g_renderer->Clear(ClearMask::Depth);
} }
} }
Scene::GetDefault()->GetCamera()->SetProjection(default_proj);
g_scene->GetCamera()->SetProjection(default_proj);
} }


private: private:


+ 2
- 2
tutorial/05_easymesh.cpp View File

@@ -51,7 +51,7 @@ public:
m_camera->SetView(mat4::lookat(vec3(-15.f, 5.f, 0.f), m_camera->SetView(mat4::lookat(vec3(-15.f, 5.f, 0.f),
vec3(0.f, -1.f, 0.f), vec3(0.f, -1.f, 0.f),
vec3(0.f, 1.f, 0.f))); vec3(0.f, 1.f, 0.f)));
Scene::GetDefault()->PushCamera(m_camera);
g_scene->PushCamera(m_camera);


/* Add a white directional light */ /* Add a white directional light */
m_light1 = new Light(); m_light1 = new Light();
@@ -70,7 +70,7 @@ public:


~EasyMeshTutorial() ~EasyMeshTutorial()
{ {
Scene::GetDefault()->PopCamera(m_camera);
g_scene->PopCamera(m_camera);
Ticker::Unref(m_light1); Ticker::Unref(m_light1);
Ticker::Unref(m_light2); Ticker::Unref(m_light2);
} }


+ 4
- 4
tutorial/06_sprite.cpp View File

@@ -26,7 +26,7 @@ public:
m_camera = new Camera(); m_camera = new Camera();
m_camera->SetView(mat4(1.f)); m_camera->SetView(mat4(1.f));
m_camera->SetProjection(mat4::ortho(0.f, 640.f, 0.f, 480.f, -100.f, 100.f)); m_camera->SetProjection(mat4::ortho(0.f, 640.f, 0.f, 480.f, -100.f, 100.f));
Scene::GetDefault()->PushCamera(m_camera);
g_scene->PushCamera(m_camera);
Ticker::Ref(m_camera); Ticker::Ref(m_camera);


m_tileset = Tiler::Register("06_sprite.png"); m_tileset = Tiler::Register("06_sprite.png");
@@ -46,7 +46,7 @@ public:
{ {
Tiler::Deregister(m_tileset); Tiler::Deregister(m_tileset);


Scene::GetDefault()->PopCamera(m_camera);
g_scene->PopCamera(m_camera);
Ticker::Unref(m_camera); Ticker::Unref(m_camera);
} }


@@ -77,8 +77,8 @@ public:
{ {
int frame = (int)(m_sprites[i].m2 * FRAME_COUNT); int frame = (int)(m_sprites[i].m2 * FRAME_COUNT);
// m_sprites[i].m1.z = frame; // m_sprites[i].m1.z = frame;
Scene::GetDefault()->AddTile(m_tileset, frame,
(ivec3)m_sprites[i].m1, 0, vec2(2.f));
g_scene->AddTile(m_tileset, frame,
(ivec3)m_sprites[i].m1, 0, vec2(2.f));
} }
} }




Loading…
Cancel
Save