Use shared_ptr for Scene::mCamera

Makes memory management a bit easier...
This commit is contained in:
Eryn Wells 2014-07-20 12:37:31 -07:00
parent c564791d1a
commit c65c6a3cfd
3 changed files with 16 additions and 8 deletions

View file

@ -10,11 +10,15 @@
#ifndef __CAMERA_H__ #ifndef __CAMERA_H__
#define __CAMERA_H__ #define __CAMERA_H__
#include <memory>
#include "basics.h" #include "basics.h"
struct Camera struct Camera
{ {
typedef std::shared_ptr<Camera> Ptr;
Camera(); Camera();
Camera(const Camera& other); Camera(const Camera& other);
virtual ~Camera(); virtual ~Camera();

View file

@ -31,10 +31,7 @@ Scene::Scene()
Scene::~Scene() Scene::~Scene()
{ {
if (mCamera) { mCamera.reset();
delete mCamera;
mCamera = NULL;
}
if (ambient != NULL) { if (ambient != NULL) {
delete ambient; delete ambient;
@ -81,7 +78,10 @@ Scene::get_height()
} }
Camera* /*
* Scene::GetCamera --
*/
Camera::Ptr
Scene::GetCamera() Scene::GetCamera()
const const
{ {
@ -89,10 +89,13 @@ Scene::GetCamera()
} }
/*
* Scene::SetCamera --
*/
void void
Scene::SetCamera(Camera* camera) Scene::SetCamera(Camera* camera)
{ {
mCamera = camera; mCamera.reset(camera);
} }

View file

@ -36,8 +36,9 @@ public:
int get_height() const; int get_height() const;
void set_height(int h) { height = h; } void set_height(int h) { height = h; }
Camera* GetCamera() const; Camera::Ptr GetCamera() const;
void SetCamera(Camera* camera); void SetCamera(Camera* camera);
void SetCamera(Camera::Ptr camera);
AmbientLight &get_ambient() const; AmbientLight &get_ambient() const;
const Color *get_pixels() const; const Color *get_pixels() const;
@ -55,7 +56,7 @@ private:
// Pixel dimensions of the image. // Pixel dimensions of the image.
int width, height; int width, height;
Camera *mCamera; Camera::Ptr mCamera;
/* /*
* Ray tracing parameters. max_depth indicates the maximum depth of the ray tree. min_weight indicates the minimum * Ray tracing parameters. max_depth indicates the maximum depth of the ray tree. min_weight indicates the minimum