diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..4f881d8 --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +OBJDIR := obj +SRCDIR := sources +INCLUDEDIR := include +BINDIR := bin + +DEBUG := yes + +ifdef COMSPEC +# windows + CXX := C:/g++ + CXXFLAGS := -Wall -I$(INCLUDEDIR) + LIBS := + RM := rm + PROGNAME := cg.exe +else +# better than windows + CXX := g++ + CXXFLAGS := -DUNIX -Wall `sdl-config --cflags` -I$(INCLUDEDIR) + LIBS := `sdl-config --libs` -lGL -lGLU + RM := rm -f + PROGNAME := cg +endif + +ifeq "$(DEBUG)" "yes" + CXXFLAGS := $(CXXFLAGS) -DDEBUG +endif + +all: $(OBJDIR)/main.o $(OBJDIR)/array.o + $(CXX) $(CXXFLAGS) $(LIBS) $(OBJDIR)/array.o $(OBJDIR)/main.o -o $(BINDIR)/$(PROGNAME) + +$(OBJDIR)/main.o: $(SRCDIR)/main.cpp + $(CXX) $(CXXFLAGS) -c $(SRCDIR)/main.cpp -o $(OBJDIR)/main.o + +$(OBJDIR)/array.o: $(SRCDIR)/array.cpp $(INCLUDEDIR)/array.h + $(CXX) $(CXXFLAGS) -c $(SRCDIR)/array.cpp -o $(OBJDIR)/array.o + +run: + $(BINDIR)/$(PROGNAME) +clean: + $(RM) $(OBJDIR)/* + $(RM) $(BINDIR)/* diff --git a/include/array.h b/include/array.h new file mode 100755 index 0000000..20f7b85 --- /dev/null +++ b/include/array.h @@ -0,0 +1,24 @@ +class Array { + private: + int _size; + int _current; + int _stacksize; + void** _data; + public: + Array(); + Array(Array* oa); + void setPos(int np); + int pos(); + int length(); + void reset(); + void* current(); + void* next(); + void push(void* elem); + void* pop(); + void* get(int p); + void set(int p, void* ne); + void clear(); + void operator+=(void *ne); + void*& operator[](int p); + ~Array(); +}; diff --git a/sources/array.cpp b/sources/array.cpp index f8c6156..d3d0f6c 100755 --- a/sources/array.cpp +++ b/sources/array.cpp @@ -1,124 +1,118 @@ #include -class Array { -private: - int _size; - int _current; - int _stacksize; - void** _data; -public: - Array() { - _size = 0; - _current = 0; - _stacksize = 0; +#include "array.h" + +Array::Array() { + _size = 0; + _current = 0; + _stacksize = 0; +} + +Array::Array(Array* oa) { + _size = 0; + _current = 0; + _stacksize = 0; + void* telem; + int tc = oa->pos(); + oa->reset(); + + while ( (telem = oa->next()) ) { + push(telem); } - Array(Array* oa) { - _size = 0; - _current = 0; - _stacksize = 0; - void* telem; - int tc = oa->pos(); - oa->reset(); + oa->setPos(tc); - while ( (telem = oa->next()) ) { - push(telem); +} + +void Array::setPos(int np) { + _current = np; +} + +int Array::pos() { + return _current; +} + +int Array::length() { + return _size; +} + +void Array::reset() { + _current = 0; +} + +void* Array::current() { + if ( _current < _size ) { + return _data[_current]; + } else { + return 0; + } +} + +void* Array::next() { + if ( _current < _size ) { + _current++; + return _data[_current-1]; + } else { + return 0; + } +} + +void Array::push(void* elem) { + if ( _size >= _stacksize ) { + _stacksize = _stacksize + 3; + void** tdat = new void*[_stacksize]; + + for ( int i = 0; i < _size; i++ ) { + tdat[i] = _data[i]; } - oa->setPos(tc); - - } - - void setPos(int np) { - _current = np; - } - - int pos() { - return _current; - } - - int length() { - return _size; - } - - void reset() { - _current = 0; - } - - void* current() { - if ( _current < _size ) { - return _data[_current]; - } else { - return 0; + if ( _size > 0 ) { + delete[] _data; } + _data = tdat; } - void* next() { - if ( _current < _size ) { - _current++; - return _data[_current-1]; - } else { - return 0; - } + _data[_size] = elem; + _size++; +} + +void* Array::pop() { + _size--; + if ( _current == _size ) { + _current = _size-1; } + return _data[_size]; +} - void push(void* elem) { - if ( _size >= _stacksize ) { - _stacksize = _stacksize + 3; - void** tdat = new void*[_stacksize]; +void* Array::get(int p) { + assert(p>=0); + assert(p<_size); + return _data[p]; +} - for ( int i = 0; i < _size; i++ ) { - tdat[i] = _data[i]; - } +void Array::set(int p, void* ne) { + assert(p>=0); + assert(p<_size); + _data[p] = ne; +} - if ( _size > 0 ) { - delete[] _data; - } - _data = tdat; - } +void Array::clear() { + _size = 0; + _current = 0; + _stacksize = 0; + delete[] _data; +} - _data[_size] = elem; - _size++; - } +void Array::operator+=(void* ne) { + push(ne); +} - void* pop() { - _size--; - if ( _current == _size ) { - _current = _size-1; - } - return _data[_size]; - } +void*& Array::operator[](int p) { + assert(p>=0); + assert(p<_size); + return _data[p]; +} - void* get(int p) { - assert(p>=0); - assert(p<_size); - return _data[p]; - } - - void set(int p, void* ne) { - assert(p>=0); - assert(p<_size); - _data[p] = ne; - } - - void clear() { - _size = 0; - _current = 0; - _stacksize = 0; - delete[] _data; - } - - void operator+=(void* ne) { - push(ne); - } - - void*& operator[](int p) { - assert(p>=0); - assert(p<_size); - return _data[p]; - } - - ~Array() { - delete[] _data; - } -}; +Array::~Array() { + delete[] _data; +} diff --git a/sources/main.cpp b/sources/main.cpp index 1f11c94..6faacd4 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -10,11 +10,11 @@ // #INCLUDE // ////////////// -#ifdef WIN32 +#ifndef UNIX #include // Only required under Windows :-D #endif -#include // This file import the SDL inteface +//#include // This file import the SDL inteface #include // This file import the SDL inteface #include // This file import the OpenGL interface #include // This file offers some OpenGL-related utilities @@ -30,7 +30,7 @@ #include -#ifdef WIN32 +#ifndef UNIX ///////////// // #PRAGMA // ///////////// @@ -43,7 +43,7 @@ #pragma comment(lib, "sdlmain.lib") #endif -#include "array.cpp" +#include "array.h" using namespace std; @@ -60,8 +60,6 @@ #define SURFACE_COUNT 6 #define PI 3.14159265f -#define DEBUG - int debugct = 0; int firstprint = 1;