From da3aa51f1b932c9a1ddccc27fd252e17b696dc1f Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 7 Sep 2013 16:48:50 -0700 Subject: [PATCH] Code for objects and spheres --- src/object.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/object.h | 17 +++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) diff --git a/src/object.c b/src/object.c index 5b61a32..41da527 100644 --- a/src/object.c +++ b/src/object.c @@ -6,10 +6,104 @@ */ +#include +#include +#include "basics.h" #include "object.h" -struct _Object -{ - int type; +struct _Object { + ObjectType type; + Vector3 location; + void *shape; }; + +typedef struct _Sphere { + float radius; +} Sphere; + + +/* + * object_init --- + * + * Create a new object of the given type. + */ +Object * +object_init(ObjectType type) +{ + Object *obj = malloc(sizeof(Object)); + if (obj == NULL) { + return NULL; + } + + switch (type) { + case ObjectTypeSphere: + obj->shape = malloc(sizeof(Sphere)); + break; + default: + assert(0); + } + + return obj; +} + + +/* + * object_destroy -- + * + * Destroy the given object. + */ +void +object_destroy(Object *obj) +{ + assert(obj != NULL); + assert(obj->shape != NULL); + + free(obj->shape); + free(obj); +} + + +/* + * object_get_location -- + * object_set_location -- + * + * Get and set the location of the object. + */ +Vector3 +object_get_location(Object *obj) +{ + assert(obj != NULL); + return obj->location; +} + +void +object_set_location(Object *obj, Vector3 location) +{ + assert(obj != NULL); + obj->location = location; +} + +/* + * Sphere functions + */ + +/* + * object_sphere_get_radius -- + * object_sphere_set_radius -- + * + * Get and set the radius of a Sphere object. + */ +float +object_sphere_get_radius(Object *obj) +{ + assert(obj != NULL && obj->type == ObjectTypeSphere); + return ((Sphere *)obj->shape)->radius; +} + +void +object_sphere_set_radius(Object *obj, float r) +{ + assert(obj != NULL && obj->type == ObjectTypeSphere); + ((Sphere *)obj->shape)->radius = r; +} diff --git a/src/object.h b/src/object.h index f25c4fc..e459bc9 100644 --- a/src/object.h +++ b/src/object.h @@ -9,8 +9,25 @@ #ifndef __OBJECT_H #define __OBJECT_H +#include "basics.h" + + +typedef enum { + ObjectTypeSphere = 1, +} ObjectType; typedef struct _Object Object; +Object *object_init(ObjectType type); +void object_destroy(Object *obj); + +Vector3 object_get_location(Object *obj); +void object_set_location(Object *obj, Vector3 location); + +// Sphere methods +float object_sphere_get_radius(Object *obj); +void object_sphere_set_radius(Object *obj, float r); + + #endif