Some style cleanups by Fredrik Ekdahl. Thanks!

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22013 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini 2007-08-19 20:14:48 +00:00
parent 0e5fe45f13
commit be98a6028d
12 changed files with 426 additions and 355 deletions

View File

@ -18,20 +18,20 @@ void
FPS::drawChar(GLfloat x, GLfloat y, GLint number)
{
static bool numbers[13][7] = {
{ true, true, true, true, true, true, false }, /* 0 */
{ false, true, true, false, false, false, false }, /* 1 */
{ true, true, false, true, true, false, true }, /* 2 */
{ true, true, true, true, false, false, true }, /* 3 */
{ false, true, true, false, false, true, true }, /* 4 */
{ true, false, true, true, false, true, true }, /* 5 */
{ true, false, true, true, true, true, true }, /* 6 */
{ true, true, true, false, false, false, false }, /* 7 */
{ true, true, true, true, true, true, true }, /* 8 */
{ true, true, true, false, false, true, true }, /* 9 */
{true, true, true, true, true, true, false}, /* 0 */
{false, true, true, false, false, false, false},/* 1 */
{true, true, false, true, true, false, true}, /* 2 */
{true, true, true, true, false, false, true}, /* 3 */
{false, true, true, false, false, true, true}, /* 4 */
{true, false, true, true, false, true, true}, /* 5 */
{true, false, true, true, true, true, true}, /* 6 */
{true, true, true, false, false, false, false}, /* 7 */
{true, true, true, true, true, true, true}, /* 8 */
{true, true, true, false, false, true, true}, /* 9 */
{ true, false, false, false, true, true, true }, /* F */
{ true, true, false, false, true, true, true }, /* P */
{ true, false, true, true, false, true, true }, /* S */
{true, false, false, false, true, true, true}, /* F */
{true, true, false, false, true, true, true}, /* P */
{true, false, true, true, false, true, true}, /* S */
};
static GLfloat gap = 0.03;

View File

@ -5,16 +5,14 @@
#include <GL/gl.h>
class FPS
{
public:
FPS();
~FPS();
class FPS {
public:
FPS();
~FPS();
static void drawCounter(GLfloat frameRate);
static void drawCounter( GLfloat frameRate );
private:
static void drawChar( GLfloat x, GLfloat y, GLint number );
private:
static void drawChar(GLfloat x, GLfloat y, GLint number);
};

View File

@ -3,22 +3,17 @@
This file may be used under the terms of the Be Sample Code License.
*/
#include <stdlib.h>
#include <GL/gl.h>
#include <InterfaceKit.h>
#include "GLObject.h"
#include <GL/gl.h>
#include <stdlib.h>
#include <InterfaceKit.h>
#include "glob.h"
struct material {
float ambient[3],diffuse[3],specular[3];
float ambient[3], diffuse[3], specular[3];
};
float *colors[] =
{
NULL,white,yellow,blue,red,green
};
float *colors[] = {NULL, white, yellow, blue, red, green};
material materials[] = {
// Null
@ -61,25 +56,34 @@ material materials[] = {
#define USE_QUAD_STRIPS 1
extern long setEvent(sem_id event);
extern long setEvent(sem_id event);
GLObject::GLObject(ObjectView *ov)
: rotX(0),
rotY(0),
lastRotX(0),
lastRotY(0),
spinX(2),
spinY(2),
x(0),
y(0),
z(-2.0),
color(4),
solidity(0),
changed(false),
objView(ov)
{
rotX = rotY = lastRotX = lastRotY = 0;
spinX = spinY = 2;
x = y = 0;
z = -2.0;
color = 4;
solidity = 0;
changed = false;
objView = ov;
};
GLObject::~GLObject()
{
};
void GLObject::MenuInvoked(BPoint point)
void
GLObject::MenuInvoked(BPoint point)
{
BPopUpMenu *m = new BPopUpMenu("Object",false,false);
BMenuItem *i;
@ -126,7 +130,9 @@ void GLObject::MenuInvoked(BPoint point)
setEvent(objView->drawEvent);
};
bool GLObject::SpinIt()
bool
GLObject::SpinIt()
{
rotX += spinX;
rotY += spinY;
@ -138,7 +144,9 @@ bool GLObject::SpinIt()
return c;
};
void GLObject::Draw(bool forID, float IDcolor[])
void
GLObject::Draw(bool forID, float IDcolor[])
{
glPushMatrix();
glTranslatef(x, y, z);
@ -156,6 +164,7 @@ void GLObject::Draw(bool forID, float IDcolor[])
changed = false;
};
TriangleObject::TriangleObject(ObjectView *ov, char *filename)
: GLObject(ov),
points(100,100),
@ -247,6 +256,7 @@ TriangleObject::TriangleObject(ObjectView *ov, char *filename)
fclose(f);
};
TriangleObject::~TriangleObject()
{
for (int i=0;i<qs.num_items;i++) {
@ -254,7 +264,9 @@ TriangleObject::~TriangleObject()
};
};
void TriangleObject::DoDrawing(bool forID)
void
TriangleObject::DoDrawing(bool forID)
{
if (!forID) {
float c[3][4];

View File

@ -2,8 +2,11 @@
Copyright 1999, Be Incorporated. All Rights Reserved.
This file may be used under the terms of the Be Sample Code License.
*/
#ifndef GL_OBJECT_H
#define GL_OBJECT_H
#include "ObjectView.h"
#include "util.h"
struct point {
float x,y,z;
@ -20,35 +23,35 @@ struct quadStrip {
int *pts;
};
#include "util.h"
class GLObject {
public:
float rotX,rotY,spinX,spinY,lastRotX,lastRotY;
float x,y,z;
int color;
int solidity;
bool changed;
ObjectView * objView;
public:
GLObject(ObjectView *ov);
virtual ~GLObject();
GLObject(ObjectView *ov);
virtual ~GLObject();
virtual void Draw(bool forID, float IDcolor[]);
virtual bool SpinIt();
virtual void MenuInvoked(BPoint point);
virtual void DoDrawing(bool forID) {};
virtual void Draw(bool forID, float IDcolor[]);
virtual bool SpinIt();
virtual void MenuInvoked(BPoint point);
virtual void DoDrawing(bool forID) {};
float rotX,rotY,spinX,spinY,lastRotX,lastRotY;
float x,y,z;
int color;
int solidity;
bool changed;
ObjectView * objView;
};
class TriangleObject : public GLObject {
public:
BufferArray<point> points;
BufferArray<tri> triangles;
BufferArray<quadStrip> qs;
public:
TriangleObject(ObjectView *ov, char *filename);
virtual ~TriangleObject();
virtual void DoDrawing(bool forID);
TriangleObject(ObjectView *ov, char *filename);
virtual ~TriangleObject();
virtual void DoDrawing(bool forID);
BufferArray<point> points;
BufferArray<tri> triangles;
BufferArray<quadStrip> qs;
};
#endif // GL_OBJECT_H

View File

@ -3,16 +3,17 @@
This file may be used under the terms of the Be Sample Code License.
*/
#include "ObjectView.h"
#include <stdio.h>
#include <stdlib.h>
#include <InterfaceKit.h>
#include <FindDirectory.h>
#include "ObjectView.h"
#include "ResScroll.h"
#include "GLObject.h"
#include "FPS.h"
#include "GLObject.h"
#include "ResScroll.h"
#define teapotData "teapot.data"
char teapotPath[PATH_MAX];
@ -35,22 +36,26 @@ float red[3] = {1.0,0.0,0.0};
float *bgColor = black;
struct light {
float *ambient;
float *diffuse;
float *specular;
};
light lights[] = {
{NULL,NULL,NULL},
{dimWhite,white,white},
{dimWhite,yellow,yellow},
{dimWhite,red,red},
{dimWhite,blue,blue},
{dimWhite,green,green}
{NULL, NULL, NULL},
{dimWhite, white, white},
{dimWhite, yellow, yellow},
{dimWhite, red, red},
{dimWhite, blue, blue},
{dimWhite, green, green}
};
long signalEvent(sem_id event)
long
signalEvent(sem_id event)
{
long c;
get_sem_count(event,&c);
@ -60,7 +65,9 @@ long signalEvent(sem_id event)
return 0;
};
long setEvent(sem_id event)
long
setEvent(sem_id event)
{
long c;
get_sem_count(event,&c);
@ -71,7 +78,8 @@ long setEvent(sem_id event)
};
long waitEvent(sem_id event)
long
waitEvent(sem_id event)
{
acquire_sem(event);
@ -83,7 +91,9 @@ long waitEvent(sem_id event)
return 0;
};
long simonThread(ObjectView *ov)
long
simonThread(ObjectView *ov)
{
int noPause=0;
while (acquire_sem_etc(ov->quittingSem,1,B_TIMEOUT,0) == B_NO_ERROR) {
@ -101,6 +111,7 @@ long simonThread(ObjectView *ov)
return 0;
};
ObjectView::ObjectView(BRect r, char *name, ulong resizingMode, ulong options)
: BGLView(r,name,resizingMode,0,options)
{
@ -130,13 +141,16 @@ ObjectView::ObjectView(BRect r, char *name, ulong resizingMode, ulong options)
objListLock.Unlock();
};
ObjectView::~ObjectView()
{
delete_sem(quittingSem);
delete_sem(drawEvent);
};
void ObjectView::AttachedToWindow()
void
ObjectView::AttachedToWindow()
{
float position[] = {0.0, 3.0, 3.0, 0.0};
float position1[] = {-3.0, -3.0, 3.0, 0.0};
@ -204,7 +218,9 @@ void ObjectView::AttachedToWindow()
setEvent(drawEvent);
};
void ObjectView::DetachedFromWindow()
void
ObjectView::DetachedFromWindow()
{
BGLView::DetachedFromWindow();
@ -224,7 +240,9 @@ void ObjectView::DetachedFromWindow()
while (locks--) Window()->Lock();
};
void ObjectView::Pulse()
void
ObjectView::Pulse()
{
Window()->Lock();
BRect p = Parent()->Bounds();
@ -237,7 +255,9 @@ void ObjectView::Pulse()
Window()->Unlock();
};
void ObjectView::MessageReceived(BMessage *msg)
void
ObjectView::MessageReceived(BMessage *msg)
{
BMenuItem *i;
bool *b;
@ -320,7 +340,9 @@ void ObjectView::MessageReceived(BMessage *msg)
};
};
int ObjectView::ObjectAtPoint(BPoint p)
int
ObjectView::ObjectAtPoint(BPoint p)
{
LockGL();
glShadeModel(GL_FLAT);
@ -347,7 +369,9 @@ int ObjectView::ObjectAtPoint(BPoint p)
return objNum;
};
void ObjectView::MouseDown(BPoint p)
void
ObjectView::MouseDown(BPoint p)
{
BPoint op=p,np=p;
BRect bounds = Bounds();
@ -441,7 +465,9 @@ void ObjectView::MouseDown(BPoint p)
setEvent(drawEvent);
};
void ObjectView::FrameResized(float w, float h)
void
ObjectView::FrameResized(float w, float h)
{
LockGL();
@ -481,7 +507,9 @@ void ObjectView::FrameResized(float w, float h)
setEvent(drawEvent);
}
bool ObjectView::RepositionView()
bool
ObjectView::RepositionView()
{
if (!(persp != lastPersp) &&
!(lastObjectDistance != objectDistance) &&
@ -516,7 +544,9 @@ bool ObjectView::RepositionView()
return true;
};
void ObjectView::EnforceState()
void
ObjectView::EnforceState()
{
glShadeModel(gouraud?GL_SMOOTH:GL_FLAT);
if (zbuf) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
@ -542,7 +572,9 @@ void ObjectView::EnforceState()
};
};
bool ObjectView::SpinIt()
bool
ObjectView::SpinIt()
{
bool changed = false;
@ -615,7 +647,9 @@ bool ObjectView::SpinIt()
return changed;
};
void ObjectView::DrawFrame(bool noPause)
void
ObjectView::DrawFrame(bool noPause)
{
LockGL();
glClear(GL_COLOR_BUFFER_BIT|(zbuf?GL_DEPTH_BUFFER_BIT:0));

View File

@ -3,8 +3,8 @@
This file may be used under the terms of the Be Sample Code License.
*/
#ifndef ObjectView_h
#define ObjectView_h
#ifndef OBJECT_VIEW_H
#define OBJECT_VIEW_H
#include <GL/glu.h>
#include <GLView.h>
@ -33,47 +33,49 @@ enum lights {
#define HISTSIZE 10
class ResScroll;
class ObjectView : public BGLView {
public:
sem_id drawEvent;
sem_id quittingSem;
thread_id drawThread;
ResScroll * resScroll;
public:
ObjectView(BRect r, char *name,
ulong resizingMode, ulong options);
~ObjectView();
BList objects;
BLocker objListLock;
virtual void MouseDown(BPoint p);
virtual void MessageReceived(BMessage *msg);
virtual void AttachedToWindow();
virtual void DetachedFromWindow();
virtual void FrameResized(float width, float height);
bool SpinIt();
int ObjectAtPoint(BPoint p);
virtual void DrawFrame(bool noPause);
virtual void Pulse();
void EnforceState();
bool RepositionView();
float yxRatio,lastYXRatio;
uint64 lastFrame;
float fpsHistory[HISTSIZE];
int32 histEntries,oldestEntry;
bool fps;
bool gouraud, lastGouraud;
bool zbuf,lastZbuf;
bool culling,lastCulling;
bool lighting,lastLighting;
bool filled,lastFilled;
bool persp,lastPersp;
bool lastTextured,textured;
bool lastFog,fog;
bool forceRedraw;
float objectDistance,lastObjectDistance;
sem_id drawEvent;
sem_id quittingSem;
thread_id drawThread;
ResScroll * resScroll;
BList objects;
BLocker objListLock;
float yxRatio,lastYXRatio;
uint64 lastFrame;
float fpsHistory[HISTSIZE];
int32 histEntries,oldestEntry;
bool fps;
bool gouraud, lastGouraud;
bool zbuf,lastZbuf;
bool culling,lastCulling;
bool lighting,lastLighting;
bool filled,lastFilled;
bool persp,lastPersp;
bool lastTextured,textured;
bool lastFog,fog;
bool forceRedraw;
float objectDistance,lastObjectDistance;
ObjectView(BRect r, char *name,
ulong resizingMode, ulong options);
~ObjectView();
virtual void MouseDown(BPoint p);
virtual void MessageReceived(BMessage *msg);
virtual void AttachedToWindow();
virtual void DetachedFromWindow();
virtual void FrameResized(float width, float height);
bool SpinIt();
int ObjectAtPoint(BPoint p);
virtual void DrawFrame(bool noPause);
virtual void Pulse();
void EnforceState();
bool RepositionView();
};
#endif
#endif // OBJECT_VIEW_H

View File

@ -3,21 +3,20 @@
This file may be used under the terms of the Be Sample Code License.
*/
#ifndef ResScroll_h
#define ResScroll_h
#ifndef RESSCROLL_H
#define RESSCROLL_H
#include <interface/ScrollBar.h>
class ObjectView;
class ResScroll : public BScrollBar {
public:
ObjectView * objectView;
ResScroll(BRect r, const char *name,
ObjectView *target, orientation posture);
virtual void ValueChanged(float value);
public:
ObjectView * objectView;
ResScroll(BRect r, const char *name,
ObjectView *target, orientation posture);
virtual void ValueChanged(float value);
};
#endif

View File

@ -5,7 +5,8 @@
#include "error.h"
void fatalerror(char *s)
void
fatalerror(char *s)
{
printf("FATAL ERROR: %s\n",s);
};

View File

@ -3,8 +3,8 @@
This file may be used under the terms of the Be Sample Code License.
*/
#ifndef error_h
#define error_h
#ifndef ERROR_H
#define ERROR_H
#include <stdio.h>
@ -28,4 +28,4 @@ extern void fatalerror(char *);
#endif //DEBUGGING
#endif
#endif // ERROR_H

View File

@ -3,7 +3,7 @@
This file may be used under the terms of the Be Sample Code License.
*/
#include "GL/gl.h"
#include <GL/gl.h>
void auxSolidTeapot(GLdouble scale);
void auxWireTeapot(GLdouble scale);

View File

@ -3,42 +3,48 @@
This file may be used under the terms of the Be Sample Code License.
*/
#include <InterfaceKit.h>
#include <stdio.h>
#include <InterfaceKit.h>
#include "ObjectView.h"
class QuitWindow : public BDirectWindow
{
public:
QuitWindow(BRect r, char *name, window_type wt, ulong something);
virtual bool QuitRequested();
virtual void DirectConnected( direct_buffer_info *info );
ObjectView *bgl;
class QuitWindow : public BDirectWindow {
public:
QuitWindow(BRect r, char *name, window_type wt, ulong something);
virtual bool QuitRequested();
virtual void DirectConnected( direct_buffer_info *info );
ObjectView *bgl;
};
QuitWindow::QuitWindow(BRect r, char *name, window_type wt, ulong something)
: BDirectWindow(r,name,wt,something)
{
}
bool QuitWindow::QuitRequested()
bool
QuitWindow::QuitRequested()
{
printf( "closing \n" );
printf("closing \n");
bgl->EnableDirectMode( false );
be_app->PostMessage(B_QUIT_REQUESTED);
return true;
};
void QuitWindow::DirectConnected( direct_buffer_info *info )
void
QuitWindow::DirectConnected(direct_buffer_info *info)
{
if( bgl )
if (bgl)
bgl->DirectConnected( info );
bgl->EnableDirectMode( true );
}
int main(int argc, char **argv)
int
main(int argc, char **argv)
{
GLenum type = BGL_RGB | BGL_DEPTH | BGL_DOUBLE;
//GLenum type = BGL_RGB | BGL_DEPTH | BGL_SINGLE;

View File

@ -3,15 +3,14 @@
This file may be used under the terms of the Be Sample Code License.
*/
#ifndef list_h
#define list_h
#ifndef UTIL_H
#define UTIL_H
#include <memory.h>
#include "error.h"
template <class contents>
struct LispNode
{
struct LispNode {
contents *car;
LispNode *cdr;
@ -31,89 +30,100 @@ struct LispNode
template <class contents>
struct LispList
{
LispNode<contents> *first;
struct LispList {
LispNode<contents> *first;
/* -------- List creation --------------- */
/* Create an empty list */
inline LispList()
{ first = 0; }
/* -------- List creation --------------- */
/* Create an empty list */
inline LispList()
{
first = 0;
}
/* Create a list pointing to the specified node */
inline LispList(LispNode<contents> *_first)
{ first = _first; }
/* Create a list pointing to the specified node */
inline LispList(LispNode<contents> *_first)
{
first = _first;
}
/* ?? */
inline LispList(LispList &init)
{ first = init.first; }
/* ?? */
inline LispList(LispList &init)
{
first = init.first;
}
/* ---------- List queries ------------- */
inline int is_empty()
{ return first == 0; }
/* ---------- List queries ------------- */
inline int is_empty()
{
return first == 0;
}
/* Determines if a thing is on the list */
inline int is_present(contents *element)
{
for (LispNode<contents> *node = first; node; node = node->cdr)
if (node->car == element)
return 1;
return 0;
}
inline int is_present(contents *element)
{
for (LispNode<contents> *node = first; node; node = node->cdr)
if (node->car == element)
return 1;
return 0;
}
/* Returns the length of the list */
inline int count()
{
int n = 0;
for (LispNode<contents> *node = first; node; node = node->cdr)
n++;
return n;
}
inline int count()
{
int n = 0;
for (LispNode<contents> *node = first; node; node = node->cdr)
n++;
return n;
}
/* ----------- Adding "nodes" to the list ------------ */
/* Add a specified node to the head of the list. */
inline void add_head(LispNode<contents> *new_element)
{
new_element->cdr = first;
first = new_element;
}
/* ----------- Adding "nodes" to the list ------------ */
/* Add a specified node to the head of the list. */
inline void add_head(LispNode<contents> *new_element)
{
new_element->cdr = first;
first = new_element;
}
/* Add a specified node anywhere on the list */
inline void add(LispNode<contents> *new_element)
{
add_head (new_element);
}
/* Add a specified node anywhere on the list */
inline void add(LispNode<contents> *new_element)
{
add_head (new_element);
}
inline void add_tail(LispNode<contents> *new_element)
{
LispNode<contents>** pred = &first;
while( *pred )
while(*pred)
pred = &(*pred)->cdr;
*pred = new_element;
new_element->cdr = 0;
}
/* ----------- Adding "contents" to the list ------------ */
/* ----- (Which in my opinion is far more useful) ------ */
/* Create new node pointing to thing, & add to head of list. */
inline void add_head_new(contents *new_element)
{
first = new LispNode<contents>(new_element, first);
}
inline void add_head(contents *new_element)
{ add_head_new(new_element); }
/* ----------- Adding "contents" to the list ------------ */
/* ----- (Which in my opinion is far more useful) ------ */
/* Create new node pointing to thing, & add to head of list. */
inline void add_head_new(contents *new_element)
{
first = new LispNode<contents>(new_element, first);
}
inline void add_head(contents *new_element)
{
add_head_new(new_element);
}
/* Create new node pointing to thing, & add to end of list. */
inline void add_tail_new(contents *new_element)
{
LispNode< contents > **pred = &first;
while (*pred)
pred = &(*pred)->cdr;
*pred = new LispNode< contents >(new_element);
}
inline void add_tail(contents *new_element)
{ add_tail_new(new_element); }
/* Create new node pointing to thing, & add to end of list. */
inline void add_tail_new(contents *new_element)
{
LispNode< contents > **pred = &first;
while (*pred)
pred = &(*pred)->cdr;
*pred = new LispNode< contents >(new_element);
}
inline void add_tail(contents *new_element)
{
add_tail_new(new_element);
}
/* Create new node pointing to thing, & add anywhere on list */
inline void add_new(contents *new_element)
@ -226,156 +236,162 @@ struct LispList
}
#endif
/* Take the first thing off the list */
inline contents *get_head()
{
contents *c = 0;
if(first)
{
c = first->car;
delete rem_head();
}
return c;
}
/* Take something off the list */
inline contents *get()
{ return(get_head()); }
/* Take the first thing off the list */
inline contents *get_head()
{
contents *c = 0;
if(first) {
c = first->car;
delete rem_head();
}
return c;
}
/* Take something off the list */
inline contents *get()
{
return(get_head());
}
/* XXX inline contents *get_tail() { } */
/* XXX inline contents *get_tail() { } */
/* -------- Stack simulation (by j.h.) ------------ */
/* Put a thing onto the head of the list */
inline void push( contents *c )
{ put_head( c ); }
/* -------- Stack simulation (by j.h.) ------------ */
/* Put a thing onto the head of the list */
inline void push(contents *c)
{
put_head(c);
}
/* Remove a thing from the head of the list */
inline contents *pop()
{ return(get_head()); }
/* Remove a thing from the head of the list */
inline contents *pop()
{
return(get_head());
}
/* Pop everything off the stack. Empty the stack/list. */
inline void pop_all()
{ rem_del_all(); }
/* Pop everything off the stack. Empty the stack/list. */
inline void pop_all()
{
rem_del_all();
}
/* ----------- list/list manipulations ------------ */
/* Add all elements present on another list to this list also. */
inline void add_new(LispList other)
{
for (LispNode<contents> *n = other.first; n; n = n->cdr)
add_new(n->car);
}
inline void add_new_once(LispList other)
{
for (LispNode<contents> *n = other.first; n; n = n->cdr)
add_new_once(n->car);
}
/* Add all elements present on another list to this list also. */
inline void add_new(LispList other)
{
for (LispNode<contents> *n = other.first; n; n = n->cdr)
add_new(n->car);
}
inline void add_new_once(LispList other)
{
for (LispNode<contents> *n = other.first; n; n = n->cdr)
add_new_once(n->car);
}
/* Remove and delete all nodes whose contents are also present
in a different list (set disjunction). */
inline void rem_del(LispList other)
{
for (LispNode<contents> *n = other.first; n; n = n->cdr)
rem_del(n->car);
}
/* Remove and delete all nodes whose contents are also present
in a different list (set disjunction). */
inline void rem_del(LispList other)
{
for (LispNode<contents> *n = other.first; n; n = n->cdr)
rem_del(n->car);
}
};
template <class thetype>
struct DoubleLinkedNode
{
thetype *next,*prev;
DoubleLinkedNode() { next = prev = NULL; };
void insert_after(thetype *n)
{
if (next != NULL)
next->prev = n;
n->next = next;
n->prev = (thetype*)this;
next = n;
};
void insert_before(thetype *n)
{
prev->next = n;
n->next = (thetype*)this;
n->prev = prev;
prev = n;
};
void remove()
{
assert(prev != NULL);
prev->next = next;
if (next != NULL)
next->prev = prev;
};
thetype *next,*prev;
DoubleLinkedNode() { next = prev = NULL; };
void insert_after(thetype *n)
{
if (next != NULL)
next->prev = n;
n->next = next;
n->prev = (thetype*)this;
next = n;
};
void insert_before(thetype *n)
{
prev->next = n;
n->next = (thetype*)this;
n->prev = prev;
prev = n;
};
void remove()
{
assert(prev != NULL);
prev->next = next;
if (next != NULL)
next->prev = prev;
};
};
template <class thetype>
struct DoubleLinkedList : public DoubleLinkedNode<thetype>
{
DoubleLinkedList() : DoubleLinkedNode<thetype>() {};
void insert(thetype *n)
{
insert_after(n);
};
void add(thetype *n)
{
insert_after(n);
};
DoubleLinkedList() : DoubleLinkedNode<thetype>() {};
void insert(thetype *n)
{
insert_after(n);
};
void add(thetype *n)
{
insert_after(n);
};
};
template <class T>
struct BufferArray
{
T * items;
int num_items;
int num_slots;
int slot_inc;
void resize(int i)
{
items = (T*)realloc(items,sizeof(T)*i);
num_slots = i;
};
T & operator [](int index)
{
assert(index < num_items);
return items[index];
};
struct BufferArray {
T * items;
int num_items;
int num_slots;
int slot_inc;
T & get(int index)
{
assert(index < num_items);
return items[index];
};
void add(T &item)
{
if (num_items == num_slots)
resize(num_slots+slot_inc);
memcpy(items+num_items,&item,sizeof(item));
num_items++;
};
BufferArray(int start_slots, int _slot_inc)
{
num_slots = start_slots;
slot_inc = _slot_inc;
assert(slot_inc > 0);
num_items = 0;
items = (T*)malloc(sizeof(T)*num_slots);
};
void resize(int i)
{
items = (T*)realloc(items,sizeof(T)*i);
num_slots = i;
};
~BufferArray()
{
free(items);
};
T & operator [](int index)
{
assert(index < num_items);
return items[index];
};
T & get(int index)
{
assert(index < num_items);
return items[index];
};
void add(T &item)
{
if (num_items == num_slots)
resize(num_slots+slot_inc);
memcpy(items+num_items,&item,sizeof(item));
num_items++;
};
BufferArray(int start_slots, int _slot_inc)
{
num_slots = start_slots;
slot_inc = _slot_inc;
assert(slot_inc > 0);
num_items = 0;
items = (T*)malloc(sizeof(T)*num_slots);
};
~BufferArray()
{
free(items);
};
};
#endif
#endif // UTIL_H