/*
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Copyright (c) 1998-2010, Vladimir A. Smirnov (kkatarn@rambler.ru)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Vladimir A. Smirnov nor the
 *    names of his contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Vladimir A. Smirnov ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Vladimir A. Smirnov BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * (set tab to 4 spaces and read more)
 *
 *  Purpose:    Another one framework library that - or at least should be
 *              - platform-independent. Currently it can be compiled both
 *              under NT (and under MustDie - if you prefer it) and Linux.
 *
 *				As of 24 oct 2008, it compiles under FreeBSD 7, but some
 *				stuff (sound output, etc.) must be fixed. GL stuff works.
 *
 *				As of 25 oct 2008, it compiles under 64-bit Linux. Got
 *				rid of ASM stuff (finally...), though the 32-bit elf output
 *				from nasm worked nicely in 64-bit environment (surprise!).
 *				Strange: solution of 2 linsystems in parallel takes much
 *				more then half a time on a 2-core Brisbane; the loads of
 *				both cores are at maximum (21 vs 32 s.; order 2000; 3GHz).
 *
 *				As of 01 aug 2010, it compiles under 64-bit FreeBSD, but
 *				some stuff (mp3lib, zlib) are broken.
 *
 *  Version:    5.9x - working alpha.
 *
 *  Language:   ANSI C. We can write object-oriented code in C, arn't we ?
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *  Classes of routines:
 *
 *      0. Internal: NT-like object management (although I'm not using
 *  handle tables; handles are pointers) with some king of RTTI (needs
 *  both CHECKED_BUILD and MALLOC_DEBUG to be defined at compile time).
 *  See source/TAL/objbase.h for details.
 *
 *      1. Support for abstract data types: list (code borrowed from Linux
 *  kernel), stack (realloc()-able array), fifo, binary and N-way trees.
 *
 *      2. Wrappers for kernel primitives: thread, mutex and event (later
 *  called 'conditional variable'). LibV itself internally maintains
 *  thread-safe global storage (see code of sched.c:libvLockGlobal()) and
 *  per-thread local storage.
 *  TODO: local IPC (named mutex, named event)
 *
 *      3. Support for I/O in UN*X style: both file and device I/O can be
 *  performed via fileXXXX() calls, common for both platforms.
 *  TODO: UDP and TCP I/O
 *
 *      4. Some kind of extendable VFS (read-only). Currently there are
 *  'drivers' for a several concatenated file systems (Id's Quake *.pak,
 *  DF and DF_II *.gob, DOOM *.wad), for resources in NT PE executable,
 *  and ZIP archives (*.zip / *.pk3 / *.pk4).
 *
 *      5. Much, much more (vectors and matrices, geometry, multimedia
 *  file formats, decompression, GLUT-unlike OpenGL utility toolkit ...).
 *
 *  Project started:    Autumn, 1998
 *  Last update:        Autumn, 2010
 *
 *  Copyright:          Copyrighted under BSD licence.
 *
 *  Bugs:               The project is still in alpha state. And:
 *
 *  Under Win32, some C compilers _are_ broken; use MSVC 6.0 (_no_ SP's)
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#ifndef V_LIB_H
#define V_LIB_H

/*
 * Configuration information
 */
#include "v_config.h"

/*
 * Main set of system headers: common for both platforms.
 */

/*
 * Some linux distributions come with strange libc headers:
 * for example, both stdio.h and unistd.h can be #include-d only
 * _after_ pthread.h if #defined __USE_UNIX98.
 *
 * FIXME: what's wrong ?
 */
#if defined(__LIBV__) && defined(__linux)

#include <pthread.h>

#endif /* __LIBV__ && __linux */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>

/*
 * Include private OpenGL header.
 */
#include	"GL/gl.h"

/*
 * 'math.h' from VisialC 5.0 is ANSI- and C++ - incompliant
 */
#ifdef      _MSC_VER
#pragma     warning(disable:4001)   /* C++ - style comment */
#pragma     warning(disable:4146)   /* unary minus applied to unsigned */
#pragma     warning(disable:4514)   /* unreferenced inline func removed */
#endif      /* _MSC_VER */

#include    <math.h>

#ifdef      _MSC_VER
#pragma     warning(default:4001)
#endif      /* _MSC_VER */

#ifndef     _MAX_PATH
#ifndef     MAX_PATH
#define     _MAX_PATH       256
#else       /* MAX_PATH */
#define     _MAX_PATH       MAX_PATH
#endif      /* MAX_PATH */
#endif      /* _MAX_PATH */

#ifndef     _MAX_DIR
#ifndef     MAX_DIR
#define     _MAX_DIR        256
#else       /* MAX_DIR */
#define     _MAX_DIR        MAX_DIR
#endif      /* MAX_DIR */
#endif      /* _MAX_DIR */

#ifndef     _MAX_FNAME
#ifndef     MAX_FNAME
#define     _MAX_FNAME      256
#else       /* MAX_FNAME */
#define     _MAX_FNAME      MAX_FNAME
#endif      /* MAX_FNAME */
#endif      /* _MAX_FNAME */

#ifndef     _MAX_EXT
#ifndef     MAX_EXT
#define     _MAX_EXT        256
#else       /* MAX_EXT */
#define     _MAX_EXT        MAX_EXT
#endif      /* MAX_EXT */
#endif      /* _MAX_EXT */

#ifndef     RIGHT_SLASHED
#ifndef     _MAX_DRIVE
#ifndef     MAX_DRIVE
#define     _MAX_DRIVE      4
#else       /* MAX_DRIVE */
#define     _MAX_DRIVE      MAX_DRIVE
#endif      /* MAX_DRIVE */
#endif      /* _MAX_DRIVE */
#endif      /* RIGHT_SLASHED */

#ifdef      CHECKED_BUILD
#include    <assert.h>
#define     V_ASSERT(exp)       assert(exp)
#else       /* CHECKED_BUILD */
#define     V_ASSERT(exp)
#endif      /* CHECKED_BUILD */

/*
 * Base LibV types
 */
#include    "v_types.h"

#ifdef  __cplusplus
extern "C" {
#endif  /* __cplusplus */
	/*
	 *  Memory management functions which are used in LibV routines.
	 *  Some routines return memAlloc()'ed buffers which have to be
	 *  released by memFree() call.
	 *  Implemented in TAL/objbase.c.
	 */
	void_p  V_GLOBAL    memAlloc(size_t size);
	void_p  V_GLOBAL    memAllocZ(size_t size); /* returns zeroed block */
	void_p  V_GLOBAL    memRealloc(void *ptr,size_t newsize);
	void    V_GLOBAL    memFree(void *ptr);
	/*
	 *  Extra debugging stuff.
	 *
	 *  Returns statistics about memory usage. Parameters:
	 *      - bytes_ret - pointer to store amount of allocated memory;
	 *      - blocks_ret - pointer to store number of allocated blocks;
	 *      - handles_ret - pointer to store number of created objects.
	 *  Note: bytes_ret is always 0 if compiled without MALLOC_DEBUG
	 */
	void        V_GLOBAL    memStats(u32_t *bytes_ret,u32_t *blocks_ret,
		u32_t *handles_ret);
	/* Prints memory info (using LibV console output) */
	void        V_GLOBAL    memPrintStats(void);
#ifdef  MALLOC_DEBUG
	/*
	 *  Sets new value for thread-specifig memory tag. Very useful
	 *  for hunting memory leaks.
	 */
	void    V_GLOBAL    memSetOpaque(unsigned opaque);
	/*
	 *  Retuns 'true' if 'ptr' refers to memory blocks allocated
	 *  by memAlloc().
	 */
	vboolean    V_GLOBAL    memIsPointer(void *ptr);
	/* For each allocated object, prints detailed info */
	void        V_GLOBAL    memPrintObjStats(void);
	/* Prints detailed list about all allocated blocks */
	void        V_GLOBAL    memPrintMemStats(void);
	/* Returns true if sentinel on every block is intact */
	vboolean	V_GLOBAL	memHeapGood(void);
#endif  /* MALLOC_DEBUG */
#ifdef  __cplusplus
}
#endif  /* __cplusplus */

/*
 * Macros for allocation of variables.
 */
#define	ALLOC_VAR(ptr_ret,base_type) \
	(ptr_ret) = (base_type*)memAlloc(sizeof(base_type))

#define	ALLOCZ_VAR(ptr_ret,base_type) \
	(ptr_ret) = (base_type*)memAllocZ(sizeof(base_type))

/*
 *  Other LibV headers.
 *  TODO: rename them.
 */
#include    "v_debug.h"

#include    "v_x86.h"
#include    "v_stack.h"
#include    "v_list.h"
#include    "v_tree.h"
#include    "v_fifo.h"
#include    "v_hash.h"
#include    "v_deque.h"
#include    "v_lltree.h"
#include    "v_ucode.h"

#include    "v_sched.h"
#include    "v_time.h"
#include    "v_comm.h"
#include    "v_wavin.h"
#include    "v_wavout.h"

#include    "v_log.h"
#include    "v_vector.h"
#include    "v_matrix.h"
#include    "v_math.h"
#include    "v_rkembd.h"
#include    "v_fft.h"
#include    "v_mrsmpl.h"
#include    "v_gpsp.h"
#include    "v_plane.h"
#include    "v_space.h"
#include    "v_parse.h"
#include    "v_nsl.h"
#include    "v_pgrid.h"
#include    "v_pprof.h"
#include    "v_ioctl.h"
#include    "v_fs.h"
#include    "v_image.h"
#include    "v_str.h"
#include    "v_wavlib.h"
#include    "v_reg.h"
#include    "v_rgen.h"
#include    "v_objlib.h"
#include    "v_camctl.h"

#include    "v_vgl.h"
#include    "v_dxf.h"
#include    "v_wmf.h"
#include    "v_tlist.h"
#include    "v_fbdraw.h"
/* 09 nov 2011 */
#include    "v_glvu.h"

#endif      /* V_LIB_H */
