Code for objects and spheres
This commit is contained in:
parent
b4cc32a196
commit
da3aa51f1b
2 changed files with 114 additions and 3 deletions
100
src/object.c
100
src/object.c
|
@ -6,10 +6,104 @@
|
|||
*/
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
|
|
17
src/object.h
17
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue