Parcourir la source

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

legacy
Sam Hocevar sam il y a 11 ans
Parent
révision
ad8144f0e8
18 fichiers modifiés avec 68 ajouts et 68 suppressions
  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 Voir le fichier

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

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)
@@ -36,9 +36,9 @@ void Debug::DrawBox(vec3 a, vec3 b, vec4 color)
{
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 Voir le fichier

@@ -133,15 +133,15 @@ void DefaultShaderData::SetupDefaultData(bool with_UV)
//-----------------------------------------------------------------------------
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;
mat3 normalmat = transpose(inverse(mat3(modelview)));

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


+ 2
- 2
src/emitter.cpp Voir le fichier

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

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)


+ 1
- 3
src/font.cpp Voir le fichier

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

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

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

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;
}


+ 0
- 3
src/gpu/rendercontext.cpp Voir le fichier

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

private:
Scene *m_scene;

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

RenderContext::~RenderContext()


+ 3
- 1
src/gpu/renderer.cpp Voir le fichier

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

Renderer *g_renderer = nullptr;

/*
* Private RendererData class
*/

class RendererData
{
friend class Renderer;


+ 2
- 2
src/gradient.cpp Voir le fichier

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

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");
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");
data->shader->SetUniform(uni_mat, model_matrix);



+ 5
- 6
src/layer.cpp Voir le fichier

@@ -51,15 +51,14 @@ void Layer::Render(int /* x */, int /* y */, int /* z */)
if (error && !(error = 0))
Log::Error("FIXME: Layer::Render no longer works\n");
#if 0
Scene *scene = Scene::GetDefault();
for (int j = 0; j < height; j++)
for (int i = 0; i < 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
}



+ 1
- 1
src/light.cpp Voir le fichier

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

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

} /* namespace lol */


+ 12
- 14
src/scene.cpp Voir le fichier

@@ -28,6 +28,16 @@ LOLFX_RESOURCE_DECLARE(line);
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
{
TileSet *tileset;
@@ -71,23 +81,18 @@ private:

Camera *m_default_cam;
Array<Camera *> m_camera_stack;

static Scene *scene;
};

Scene *SceneData::scene = nullptr;

/*
* Public Scene class
*/

Scene::Scene()
Scene::Scene(ivec2 size)
: data(new SceneData())
{
/* Create a default orthographic camera, in case the user doesn’t. */
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);
PushCamera(data->m_default_cam);

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

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

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


+ 7
- 4
src/scene.h Voir le fichier

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

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

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

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

extern Scene *g_scene;

} /* namespace lol */

#endif // __LOL_SCENE_H__


+ 1
- 2
src/sprite.cpp Voir le fichier

@@ -50,8 +50,7 @@ void Sprite::TickDraw(float 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()


+ 2
- 2
src/ticker.cpp Voir le fichier

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

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

Profiler::Stop(Profiler::STAT_TICK_DRAW);


+ 4
- 0
src/video.cpp Voir le fichier

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

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

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

delete g_renderer;
g_renderer = nullptr;
}


+ 2
- 2
test/btphystest.cpp Voir le fichier

@@ -56,7 +56,7 @@ BtPhysTest::BtPhysTest(bool editor)
vec3(0.f, 0.f, 0.f),
vec3(0, 1, 0));
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;

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

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



+ 13
- 13
test/meshviewer.cpp Voir le fichier

@@ -61,9 +61,9 @@ public:
void SetFov(float new_fov=60.0f, vec2 video_size = vec2(Video::GetSize()))
{
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
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")
@@ -107,7 +107,7 @@ public:
m_camera->SetView(vec3(0.f, 0.f, 10.f),
vec3(0.f, 0.f, 0.f),
vec3(0.f, 1.f, 0.f));
Scene::GetDefault()->PushCamera(m_camera);
g_scene->PushCamera(m_camera);

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

~MeshViewer()
{
Scene::GetDefault()->PopCamera(m_camera);
g_scene->PopCamera(m_camera);
for (int i = 0; i < m_lights.Count(); ++i)
Ticker::Unref(m_lights[i]);
}
@@ -178,8 +178,8 @@ public:
if (m_meshes[mesh_id].m2)
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)
{
@@ -250,13 +250,13 @@ public:
SetFov(m_fov_damp);

//Move modification
vec3 campos = Scene::GetDefault()->GetCamera()->GetPosition();
vec3 campos = g_scene->GetCamera()->GetPosition();
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)
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
Camera* cur_cam = Scene::GetDefault()->GetCamera();
Camera* cur_cam = g_scene->GetCamera();
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)),
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));

mat4 default_proj = Scene::GetDefault()->GetCamera()->GetProjection();
mat4 default_proj = g_scene->GetCamera()->GetProjection();
int max_drawn = m_meshes.Count() - m_mesh_shown;
for (int i = max_drawn; i < m_meshes.Count(); i++)
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);

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

private:


+ 2
- 2
tutorial/05_easymesh.cpp Voir le fichier

@@ -51,7 +51,7 @@ public:
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)));
Scene::GetDefault()->PushCamera(m_camera);
g_scene->PushCamera(m_camera);

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

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


+ 4
- 4
tutorial/06_sprite.cpp Voir le fichier

@@ -26,7 +26,7 @@ public:
m_camera = new Camera();
m_camera->SetView(mat4(1.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);

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

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

@@ -77,8 +77,8 @@ public:
{
int frame = (int)(m_sprites[i].m2 * FRAME_COUNT);
// 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));
}
}



Chargement…
Annuler
Enregistrer