/*
 * Copyright (c) 2008-2009 Bill Whitacre http://rampancy.g0dsoft.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
*/

#ifndef RAT_PHYSICS_COMMON
#define RAT_PHYSICS_COMMON

#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#include "memory.h"
#include "defines.h"

#include "qsort.h"
#define qsort rat_qsort

// link bin for linked lists (not used currently)

typedef struct rat_link_bin
{
	void *obj;
	struct rat_link_bin *next;
} rat_link_bin;

// iterators for stuff

struct rat_iterator;

typedef struct rat_iterator_class
{
	void (*destroy)(struct rat_iterator *iter);
	void (*next)(struct rat_iterator *iter);
	int (*finished)(struct rat_iterator *iter); // returns 1 if at the end of the list
	void *(*value)(struct rat_iterator *iter);
} rat_iterator_class;

typedef struct rat_iterator
{
	rat_iterator_class *clas;
} rat_iterator;

#ifdef __cplusplus
extern "C" {
#endif

// this is NOT to be abused.  small recyclable blocks only.
// you will murder your performance if you use it for anything
// else.
extern rat_block_allocator *rat_global_block_allocator;

// returns 1 if everything's good.  must be called BEFORE
// ANYTHING ELSE, PERIOD.  initializes Rat Physics.
int rat_physics_init();

// returns 1 if successful.  call at end of execution for
// good form, and to make sure everything is cleaned up
// properly.  it will also be called on atexit automatically.
int rat_physics_cleanup();

// returns 1 if Rat Physics is already initialized.
int rat_physics_is_init();

// iterator functions
void rat_iterator_next(rat_iterator *iter);
int rat_iterator_finished(rat_iterator *iter);
void *rat_iterator_value(rat_iterator *iter);
void rat_iterator_destroy(rat_iterator *iter);

// handy floating point op inlines
rat_real rat_clampf(rat_real x,rat_real max,rat_real min);
rat_real rat_maxf(rat_real x,rat_real max);
rat_real rat_minf(rat_real x,rat_real min);

#ifdef __cplusplus
}
#endif

#endif
