diff options
33 files changed, 1239 insertions, 270 deletions
diff --git a/engine/flatactor.cpp b/engine/flatactor.cpp index 7ab0909..e41d5cf 100644 --- a/engine/flatactor.cpp +++ b/engine/flatactor.cpp @@ -1,44 +1,30 @@ #include "flatactor.h" -#include "flattask.h" #include "flatland.h" -FlatActor::FlatActor(FlatCollector *parent, bool evolving) +FlatActor::FlatActor(FlatCollector *parent, FlatBound *bounds) - : FlatCollector(parent), task(0) + : FlatCollector(parent), bounds(bounds) { - setEvolving(evolving); } FlatActor::~FlatActor() { - if (task) - delete task; + } -void FlatActor::evolve_task(void *data) +void FlatActor::setBounds(FlatBound * bounds) { - evolve(*(float*)data); + this->bounds = bounds; } -void FlatActor::evolve(float) {} - -void FlatActor::setEvolving(bool flag) +FlatBound * FlatActor::getBounds() const { - if (isEvolving() != flag) - { - if (flag) - task = new FlatTask<FlatActor>(this, &FlatActor::evolve_task, (void*)&flatland_dt); - else { - - delete task; - task = 0; - } - } + return bounds; } -bool FlatActor::isEvolving() const +void FlatActor::evolve(float dt) { - return task != 0; + } diff --git a/engine/flatevolvable.cpp b/engine/flatevolvable.cpp new file mode 100644 index 0000000..20dd6dd --- /dev/null +++ b/engine/flatevolvable.cpp @@ -0,0 +1,41 @@ +#include "flatevolvable.h" +#include "flattask.h" +#include "flatland.h" + +FlatEvolvable::FlatEvolvable(bool evolving) + + : task(0) +{ + setEvolving(evolving); +} + +FlatEvolvable::~FlatEvolvable() +{ + if (task) + delete task; +} + +void FlatEvolvable::evolve_task(void *data) +{ + evolve(*(float*)data); +} + +void FlatEvolvable::setEvolving(bool flag) +{ + if (isEvolving() != flag) + { + if (flag) + task = new FlatTask<FlatEvolvable>(this, &FlatEvolvable::evolve_task, (void*)&flatland_dt); + else { + + delete task; + task = 0; + } + } +} + +bool FlatEvolvable::isEvolving() const +{ + return task != 0; +} + diff --git a/engine/flatland.cpp b/engine/flatland.cpp index 669daa5..d8c17f5 100644 --- a/engine/flatland.cpp +++ b/engine/flatland.cpp @@ -9,6 +9,7 @@ using namespace std; +#include "flattask.h" #include "flatwindow.h" float flatland_dt; @@ -90,6 +91,10 @@ int init_flatland(const FlatWindow& w, gameloop loop, const flat_status& s, floa delay = clock(); + /* Execute tasks */ + task_s::executeAll(); + + /* Execute loop function */ loop_function(flatland_dt); SDL_Delay((Uint32) (1000.0f / fps)); diff --git a/engine/flatlayer.cpp b/engine/flatlayer.cpp index e69de29..06185e2 100644 --- a/engine/flatlayer.cpp +++ b/engine/flatlayer.cpp @@ -0,0 +1,12 @@ +#include "flatlayer.h" + +FlatLayer::FlatLayer(FlatLayer *parent) +{ + +} + +FlatLayer::~FlatLayer() +{ + +} + diff --git a/engine/flatobject.cpp b/engine/flatobject.cpp index 464d984..bfbff0b 100644 --- a/engine/flatobject.cpp +++ b/engine/flatobject.cpp @@ -4,16 +4,18 @@ using namespace std; +list<FlatObject*> FlatObject::allObjects; + FlatObject::FlatObject() { /* Collect this object */ - allObjects.push_back(this); + FlatObject::allObjects.push_back(this); } FlatObject::~FlatObject() { /* Eliminate this object reference */ - allObjects.remove(this); + FlatObject::allObjects.remove(this); } void FlatObject::setID(const char *id) @@ -29,7 +31,7 @@ const char* FlatObject::getID() const bool FlatObject::isAllocated(FlatObject *obj) { - for (FlatObject * o : allObjects) + for (FlatObject * o : FlatObject::allObjects) { if (o == obj) return true; @@ -40,7 +42,7 @@ bool FlatObject::isAllocated(FlatObject *obj) vector<FlatObject*>& FlatObject::getByID(const char *id, vector<FlatObject*>& l) { - for (FlatObject * obj : allObjects) + for (FlatObject * obj : FlatObject::allObjects) { if (!strcmp(id, obj->getID())) l.push_back(obj); diff --git a/engine/flatsignal.cpp b/engine/flatsignal.cpp new file mode 100644 index 0000000..1503c24 --- /dev/null +++ b/engine/flatsignal.cpp @@ -0,0 +1,17 @@ +#include "flatsignal.h" + +using namespace std; + +vector<FlatSignal> FlatSignal::sig_stack; + +FlatSignal::FlatSignal(int type, void *data) + : type(type), data(data) +{ +} + +FlatSignal::emit(const FlatSignal &signal) +{ + FlatSignal::sig_stack.push_back(signal); +} + + diff --git a/engine/flatspriter.cpp b/engine/flatspriter.cpp deleted file mode 100644 index 4e9f7b7..0000000 --- a/engine/flatspriter.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "flatspriter.h" - -#include "flatsprite.h" -#include "flatwindow.h" -#include "flatland.h" - -FlatSpriter::FlatSpriter(const char *filename, Uint32 format, FlatSpriter *parent) - - : FlatActor(parent) -{ - SDL_Surface * base; - - if (parent == 0) - { - // TODO, default parent - } else - base = parent->getSprite()->getSurface(); - - sprite = new FlatSprite(filename, format, base); - - if (sprite == 0) - { - // TODO, error: failed to load - } -} - -FlatSpriter::FlatSpriter(SDL_Surface *surface, FlatSpriter *parent) - - : FlatActor(parent) -{ - SDL_Surface * base; - - if (parent == 0) - { - // TODO, default parent - } else - base = parent->getSprite()->getSurface(); - - if (surface == 0) - { - // TODO, default surface - } - - sprite = new FlatSprite(surface, base); -} - -FlatSpriter::FlatSpriter(FlatSprite * sprite, FlatSpriter *parent) - - : FlatActor(parent) -{ - if (parent == 0) - { - // TODO, screen surface - } - - if (sprite == 0) - { - // TODO, default sprite - } - - this->sprite = new FlatSprite(*sprite); -} - -FlatSpriter::~FlatSpriter() -{ - delete sprite; -} - -FlatSprite * FlatSpriter::getSprite() -{ - return sprite; -} - -void FlatSpriter::setSprite(FlatSprite *sprite) -{ - -} - -FlatActor * FlatSpriter::getParent() -{ - -} - -void FlatSpriter::setParent(FlatActor*) -{ - -} - diff --git a/engine/flatsprite.cpp b/engine/flatsurface.cpp index 36fb6f8..1970b71 100644 --- a/engine/flatsprite.cpp +++ b/engine/flatsurface.cpp @@ -1,16 +1,16 @@ -#include "flatsprite.h" +#include "flatsurface.h" #include <iostream> using namespace std; -FlatSprite::FlatSprite(const char *filename, Uint32 format, SDL_Surface *parent) +FlatSurface::FlatSurface(const char *filename, Uint32 format, SDL_Surface *parent) : FlatObject(), parent(parent), hide(false) { setID(filename); - cout << "FlatSprite: loading " << filename << endl; + cout << "FlatSurface: loading " << filename << endl; surface = loadOptimizedSurface(filename, format); if (!surface) @@ -33,7 +33,7 @@ FlatSprite::FlatSprite(const char *filename, Uint32 format, SDL_Surface *parent) viewport->h = surface->h; } -FlatSprite::FlatSprite(SDL_Surface *surface, SDL_Surface *parent) +FlatSurface::FlatSurface(SDL_Surface *surface, SDL_Surface *parent) : FlatObject(), parent(parent), hide(false) { @@ -54,7 +54,7 @@ FlatSprite::FlatSprite(SDL_Surface *surface, SDL_Surface *parent) viewport->h = surface->h; } -FlatSprite::FlatSprite(const FlatSprite &sprite) +FlatSurface::FlatSurface(const FlatSurface &sprite) : FlatObject(sprite), parent(sprite.parent), hide(sprite.hide) @@ -66,7 +66,7 @@ FlatSprite::FlatSprite(const FlatSprite &sprite) surface = copySurface(sprite.surface); } -FlatSprite::~FlatSprite() +FlatSurface::~FlatSurface() { SDL_FreeSurface(surface); @@ -74,7 +74,7 @@ FlatSprite::~FlatSprite() delete viewport; } -void FlatSprite::setOffset(int x, int y, int w, int h) +void FlatSurface::setOffset(int x, int y, int w, int h) { offset->x = x; offset->y = y; @@ -86,7 +86,7 @@ void FlatSprite::setOffset(int x, int y, int w, int h) offset->h = h; } -void FlatSprite::setViewport(int x, int y, int w, int h) +void FlatSurface::setViewport(int x, int y, int w, int h) { viewport->x = x; viewport->y = y; @@ -94,59 +94,59 @@ void FlatSprite::setViewport(int x, int y, int w, int h) viewport->h = h; } -void FlatSprite::setViewport(const SDL_Rect &rect) +void FlatSurface::setViewport(const SDL_Rect &rect) { *viewport = rect; } -void FlatSprite::setOffset(const SDL_Rect &offset) +void FlatSurface::setOffset(const SDL_Rect &offset) { *this->offset = offset; } -const SDL_Rect * FlatSprite::getOffset() const +const SDL_Rect * FlatSurface::getOffset() const { return offset; } -const SDL_Rect * FlatSprite::getViewport() const +const SDL_Rect * FlatSurface::getViewport() const { return viewport; } -void FlatSprite::setParent(SDL_Surface *parent) +void FlatSurface::setParent(SDL_Surface *parent) { this->parent = parent; } -SDL_Surface * FlatSprite::getParent() +SDL_Surface * FlatSurface::getParent() { return parent; } -SDL_Surface * FlatSprite::getSurface() +SDL_Surface * FlatSurface::getSurface() { return surface; } -void FlatSprite::setHidden(bool flag) +void FlatSurface::setHidden(bool flag) { hide = flag; } -bool FlatSprite::isHidden() const +bool FlatSurface::isHidden() const { return hide; } -void FlatSprite::blit() +void FlatSurface::blit() { if (!hide) SDL_BlitSurface(surface, viewport, parent, offset); } -SDL_Surface * FlatSprite::loadOptimizedSurface(const char *filename, Uint32 format) +SDL_Surface * FlatSurface::loadOptimizedSurface(const char *filename, Uint32 format) { SDL_Surface * optimized = 0; @@ -161,7 +161,7 @@ SDL_Surface * FlatSprite::loadOptimizedSurface(const char *filename, Uint32 form return optimized; } -SDL_Surface * FlatSprite::copySurface(SDL_Surface *src) +SDL_Surface * FlatSurface::copySurface(SDL_Surface *src) { return SDL_ConvertSurface(src, src->format, src->flags); } diff --git a/engine/flattask.cpp b/engine/flattask.cpp new file mode 100644 index 0000000..b71f9d7 --- /dev/null +++ b/engine/flattask.cpp @@ -0,0 +1,23 @@ +#include "flattask.h" + +/* Static variable definition */ +std::list<task_s*> task_s::tasks; + +task_s::task_s() +{ + /* Push into the public callback list */ + tasks.push_back(this); +} + +task_s::~task_s() +{ + /* Remove from the public callback list */ + tasks.remove(this); +} + +void task_s::executeAll() +{ + for (task_s * task : task_s::tasks) + task->exec(); +} + diff --git a/engine/flatwindow.cpp b/engine/flatwindow.cpp index 2a39b0a..d1d444e 100644 --- a/engine/flatwindow.cpp +++ b/engine/flatwindow.cpp @@ -155,6 +155,20 @@ void FlatWindow::setWindowStatus(window_status status) this->status = status; } +void FlatWindow::serial_cb(const SDL_Event &event) +{ + // TODO Default escape exits + + switch (event.type) + { + case SDL_KEYDOWN: + + if (event.key.keysym.sym == SDLK_ESCAPE) + close(); // TODO not enough + break; + } +} + Uint32 FlatWindow::winstatus_to_flags(window_status s) { Uint32 flags = 0; diff --git a/engine/focusable.cpp b/engine/focusable.cpp new file mode 100644 index 0000000..4a651e6 --- /dev/null +++ b/engine/focusable.cpp @@ -0,0 +1,53 @@ +#include "focusable.h" +#include "flattask.h" +#include "SDL2/SDL.h" + + +SDL_EventCollector::SDL_EventCollector() +{ + checker = new FlatTask<SDL_EventCollector>(this, &SDL_EventCollector::collect, 0); +} + +SDL_EventCollector::~SDL_EventCollector() +{ + delete checker; +} + +void SDL_EventCollector::collect(void*) +{ + SDL_Event event; + + while ( SDL_PollEvent(&event) ) + stack.push_back(event); +} + + + +SDL_EventCollector * Focusable::collector = new SDL_EventCollector(); + +Focusable::Focusable(bool focused) : focused(focused) +{ + event_trigger = new FlatTask<Focusable>(this, &Focusable::serial_precall, 0); +} + +Focusable::~Focusable() +{ + delete event_trigger; +} + +void Focusable::setFocused(bool flag) +{ + focused = flag; +} + +bool Focusable::isFocused() const +{ + return focused; +} + +void Focusable::serial_precall(void*) +{ + for (auto event : Focusable::collector->stack) + serial_cb(event); +} + diff --git a/engine/include/flatactor.h b/engine/include/flatactor.h index 2c8b92d..8e2d3d3 100644 --- a/engine/include/flatactor.h +++ b/engine/include/flatactor.h @@ -2,35 +2,28 @@ #define __FLATACTOR_H__ #include "flatcollector.h" +#include "flatevolvable.h" -struct task_s; +class FlatBound; -class FlatActor : public FlatCollector +class FlatActor : public FlatCollector, public FlatEvolvable { - task_s * task; - // TODO, event binding // TODO, serial binding + + /* Bounds */ + FlatBound * bounds; public: - FlatActor(FlatCollector *parent = 0, bool evolving = false); - - virtual ~FlatActor(); - - /* Evolution fields */ + FlatActor(FlatCollector *parent = 0, FlatBound *bounds = 0); - void evolve_task(void*); + ~FlatActor(); - virtual void evolve(float dt); - - void setEvolving(bool flag); - - bool isEvolving() const; - - /* Surface fields */ + virtual void evolve(float dt) override; - virtual SDL_Surface * sdl_surface() = 0; + void setBounds(FlatBound * bounds); + FlatBound * getBounds() const; }; #endif diff --git a/engine/include/flatbound.h b/engine/include/flatbound.h new file mode 100644 index 0000000..a80cd6e --- /dev/null +++ b/engine/include/flatbound.h @@ -0,0 +1,38 @@ +#ifndef __FLATBOUND_H__ +#define __FLATBOUND_H__ + +#include "flatobject.h" +#include "svector.h" + +typedef SVector<int, 2> pixel; + +class FlatBound : virtual public FlatObject +{ + pixel location; + +public: + + FlatBound() {} + virtual ~FlatBound() {} + + virtual void scale(double ratio) = 0; + + virtual pixel extremum(const FlatBound*) const = 0; + + bool isColliding(const FlatBound *other) const + { + return (other.extremum(this) - location) < (this.extremum(other) - location); + } + + const pixel& getLocation() const + { + return location; + } + + void setLocation(const pixel &loc) + { + this->location = loc; + } +}; + +#endif diff --git a/engine/include/flatevent.h b/engine/include/flatevent.h deleted file mode 100644 index e69de29..0000000 --- a/engine/include/flatevent.h +++ /dev/null diff --git a/engine/include/flatevolvable.h b/engine/include/flatevolvable.h new file mode 100644 index 0000000..526599a --- /dev/null +++ b/engine/include/flatevolvable.h @@ -0,0 +1,29 @@ +#ifndef __FLATEVOLVABLE_H__ +#define __FLATEVOLVABLE_H__ + +#include "flatobject.h" + +struct task_s; + +class FlatEvolvable : virtual public FlatObject +{ + task_s * task; + +public: + + FlatEvolvable(bool evolving = false); + + virtual ~FlatEvolvable(); + + /* Evolution fields */ + + void evolve_task(void*); + + virtual void evolve(float dt) = 0; + + void setEvolving(bool flag); + + bool isEvolving() const; +}; + +#endif diff --git a/engine/include/flatlistener.h b/engine/include/flatlistener.h deleted file mode 100644 index e69de29..0000000 --- a/engine/include/flatlistener.h +++ /dev/null diff --git a/engine/include/flatmultispriter.h b/engine/include/flatmultispriter.h deleted file mode 100644 index e69de29..0000000 --- a/engine/include/flatmultispriter.h +++ /dev/null diff --git a/engine/include/flatsignal.h b/engine/include/flatsignal.h new file mode 100644 index 0000000..6ec837a --- /dev/null +++ b/engine/include/flatsignal.h @@ -0,0 +1,60 @@ +#ifndef __FLAT_SIGNAL_H__ +#define __FLAT_SIGNAL_H__ + +#include <vector> +#include <map> +#include "flatobject.h" + +class task_s; + +struct FlatSignal : virtual public FlatObject +{ + const int type; + void * data; + + FlatSignal(int type, void *data); + + static emit(const FlatSignal&); + static std::vector<FlatSignal> sig_stack; +}; + +template <class T> +class FlatListener : virtual public FlatObject +{ + typedef void (T::*callback_t)(void*); + + task_s * listener; + + void checkSignal(void*) + { + for (auto signal : FlatSignal::sig_stack) + { + callback_t cb; + + if (cb = checkmap.find(signal.type)) + cb(signal.data); + } + } + + std::map<int, callback_t> checkmap; + +public: + + FlatListener() + { + + } + + ~FlatListener() + { + + } + + connect(int type, callback_t cb) + { + checkmap.insert(std::make_pair(type, cb)); + } +}; + + +#endif diff --git a/engine/include/flatsprite.h b/engine/include/flatsprite.h deleted file mode 100644 index 3fef3b3..0000000 --- a/engine/include/flatsprite.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef __FLATSPRITE_H__ -#define __FLATSPRITE_H__ - -#include "flatobject.h" -#include <SDL2/SDL.h> - -class FlatSprite : public FlatObject -{ - SDL_Surface * surface; - SDL_Surface * parent; - - SDL_Rect * offset; - SDL_Rect * viewport; - - bool hide; - -public: - - FlatSprite(const char *filename, Uint32 format = SDL_PIXELFORMAT_RGBA32, SDL_Surface *parent = 0); - FlatSprite(SDL_Surface *surface, SDL_Surface *parent = 0); - - FlatSprite(const FlatSprite&); - - ~FlatSprite(); - - void setOffset(int, int, int w = -1, int h = -1); - - void setOffset(const SDL_Rect&); - - void setViewport(int x = 0, int y = 0, int w = -1, int h = -1); - - void setViewport(const SDL_Rect&); - - const SDL_Rect * getOffset() const; - - const SDL_Rect * getViewport() const; - - void setParent(SDL_Surface *parent); - - SDL_Surface * getParent(); - - SDL_Surface * getSurface(); - - void setHidden(bool); - - bool isHidden() const; - - void blit(); - - static SDL_Surface * loadOptimizedSurface(const char* filename, Uint32 format); - - static SDL_Surface * copySurface(SDL_Surface*); -}; - -#endif diff --git a/engine/include/flatspriter.h b/engine/include/flatspriter.h deleted file mode 100644 index 0043272..0000000 --- a/engine/include/flatspriter.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __FLATSPRITER_H__ -#define __FLATSPRITER_H__ - -#include "flatactor.h" - -class FlatSprite; - -class FlatSpriter : public FlatActor -{ - FlatSprite * sprite; - -public: - - FlatSpriter(const char *filename, Uint32 format, FlatSpriter *parent = 0); - FlatSpriter(SDL_Surface *surface, FlatSpriter *parent = 0); - FlatSpriter(FlatSprite * sprite, FlatSpriter *parent = 0); - - ~FlatSpriter(); - - /* Sprite modifiers */ - - FlatSprite * getSprite(); - - void setSprite(FlatSprite*); - - FlatActor * getParent(); - - void setParent(FlatActor*); -}; - -#endif diff --git a/engine/include/flatsurface.h b/engine/include/flatsurface.h index e69de29..bc04385 100644 --- a/engine/include/flatsurface.h +++ b/engine/include/flatsurface.h @@ -0,0 +1,55 @@ +#ifndef __FLATSURFACE_H__ +#define __FLATSURFACE_H__ + +#include "flatobject.h" +#include <SDL2/SDL.h> + +class FlatSurface : public FlatObject +{ + SDL_Surface * surface; + SDL_Surface * parent; + + SDL_Rect * offset; + SDL_Rect * viewport; + + bool hide; + +public: + + FlatSurface(const char *filename, Uint32 format = SDL_PIXELFORMAT_RGBA32, SDL_Surface *parent = 0); + FlatSurface(SDL_Surface *surface, SDL_Surface *parent = 0); + + FlatSurface(const FlatSurface&); + + ~FlatSurface(); + + void setOffset(int, int, int w = -1, int h = -1); + + void setOffset(const SDL_Rect&); + + void setViewport(int x = 0, int y = 0, int w = -1, int h = -1); + + void setViewport(const SDL_Rect&); + + const SDL_Rect * getOffset() const; + + const SDL_Rect * getViewport() const; + + void setParent(SDL_Surface *parent); + + SDL_Surface * getParent(); + + SDL_Surface * getSurface(); + + void setHidden(bool); + + bool isHidden() const; + + void blit(); + + static SDL_Surface * loadOptimizedSurface(const char* filename, Uint32 format); + + static SDL_Surface * copySurface(SDL_Surface*); +}; + +#endif diff --git a/engine/include/flattask.h b/engine/include/flattask.h index bb8c175..b470737 100644 --- a/engine/include/flattask.h +++ b/engine/include/flattask.h @@ -11,29 +11,16 @@ class task_s public: - task_s() - { - /* Push into the public callback list */ - tasks.push_back(this); - } + task_s(); - virtual ~task_s() - { - /* Remove from the public callback list */ - tasks.remove(this); - } + virtual ~task_s(); virtual void exec() = 0; /* Execute all tasks */ - static void executeAll() - { - for (task_s * task : tasks) - task->exec(); - } + static void executeAll(); }; - template<class T> class FlatTask : public task_s { diff --git a/engine/include/flattrigger.h b/engine/include/flattrigger.h deleted file mode 100644 index e69de29..0000000 --- a/engine/include/flattrigger.h +++ /dev/null diff --git a/engine/include/flatwindow.h b/engine/include/flatwindow.h index f6645f6..8c3d5bf 100644 --- a/engine/include/flatwindow.h +++ b/engine/include/flatwindow.h @@ -45,13 +45,14 @@ class FlatWindow : public FlatObject, public Focusable FlatLayer * main_layer; - void serial_cb(SDL_Event*) override; + void serial_cb(const SDL_Event&) override; -protected: +// TODO window calls +//protected: - virtual void keyEvent(SDL_KeyEvent*); + //virtual void resizeEvent(); - virtual void quitEvent(); + //virtual void quitEvent(); public: diff --git a/engine/include/focusable.h b/engine/include/focusable.h index 6438ab8..b70357b 100644 --- a/engine/include/focusable.h +++ b/engine/include/focusable.h @@ -1,29 +1,43 @@ #ifndef __FOCUSABLE_H__ #define __FOCUSABLE_H__ +#include <vector> + union SDL_Event; +class task_s; + +struct SDL_EventCollector +{ + task_s * checker; + std::vector<SDL_Event> stack; + + SDL_EventCollector(); + ~SDL_EventCollector(); + + void collect(void*); +}; class Focusable { bool focused; + task_s * event_trigger; + + static SDL_EventCollector * collector; + public: - Focusable(bool focused = true) + Focusable(bool focused = true); + + virtual ~Focusable(); - : focused(focused) {} + virtual void serial_cb(const SDL_Event&) = 0; - virtual void serial_cb(SDL_Event *) = 0; + void setFocused(bool flag); - void setFocused(bool flag) - { - focused = flag; - } + bool isFocused() const; - bool isFocused() const - { - return focused; - } + void serial_precall(void*); }; #endif diff --git a/engine/include/symbasic/.test.cpp.swp b/engine/include/symbasic/.test.cpp.swp Binary files differnew file mode 100644 index 0000000..b30055c --- /dev/null +++ b/engine/include/symbasic/.test.cpp.swp diff --git a/engine/include/symbasic/dvector.h b/engine/include/symbasic/dvector.h new file mode 100644 index 0000000..99dadee --- /dev/null +++ b/engine/include/symbasic/dvector.h @@ -0,0 +1,80 @@ +#ifndef __DVECTOR_H__ +#define __DVECTOR_H__ + +#include <vector> +#include <initializer_list> + +#include "vector.h" + +template<class T> +class DVector : public Vector<T> +{ + +public : + + DVector(size_t dim = 0); + DVector(const std::vector<T>&); + DVector(const std::initializer_list<T> &init); + + virtual const size_t size() const override; + + virtual T& operator[](size_t i) override; + virtual const T& operator[](size_t i) const override; + + DVector& operator<<(const T&); + + /* Plus / minus */ + DVector<T>& operator+=(const Vector<T>&); + DVector<T>& operator-=(const Vector<T>&); + + /* Scalar multiplication / division */ + DVector<T>& operator*=(double k); + DVector<T>& operator/=(double k); + + /* Reverse operator */ + DVector<T>& operator~(); + + /* Unit vector */ + DVector<T>& unit(); + +private: + + std::vector<T> components; +}; + +template<class T> +const DVector<T> operator+(DVector<T> , const Vector<T>& ); + +template<class T> +const DVector<T> operator-(DVector<T> , const Vector<T>& ); + +template<class T> +const DVector<T> operator*(DVector<T> , double); + +template<class T> +const DVector<T> operator/(DVector<T> , double); + +/*template<class T> +const DVector<T> operator+(const Vector<T>&, DVector<T>); + +template<class T> +const DVector<T> operator-(const Vector<T>&, DVector<T>);*/ + +template<class T> +const DVector<T> operator*(double, DVector<T>); + +template<class T> +const DVector<T> operator/(double, DVector<T>); + + +template<class T> +const DVector<T> operator-(DVector<T>); + +inline const DVector<double> operator^(const DVector<double>& v, const Vector<double>&); + +/* + * Include definitions file + */ +#include "dvector.tpp" + +#endif diff --git a/engine/include/symbasic/dvector.tpp b/engine/include/symbasic/dvector.tpp new file mode 100644 index 0000000..3aa7923 --- /dev/null +++ b/engine/include/symbasic/dvector.tpp @@ -0,0 +1,147 @@ +#ifndef __DVECTOR_TPP__ +#define __DVECTOR_TPP__ + +template<class T> +DVector<T>::DVector(size_t dim) + : components(dim) +{ + +} + +template<class T> +DVector<T>::DVector(const std::vector<T>& init) + : components(init) +{ + +} + +template<class T> +DVector<T>::DVector(const std::initializer_list<T> &init) + : components(init.begin(), init.end()) +{ + +} + +template<class T> +const size_t DVector<T>::size() const +{ + return components.size(); +} + +template<class T> +T& DVector<T>::operator[](size_t i) +{ + return components[i]; +} + +template<class T> +const T& DVector<T>::operator[](size_t i) const +{ + return components[i]; +} + +template<class T> +DVector<T>& DVector<T>::operator<<(const T &value) +{ + components.push_back(value); +} + +template<class T> +DVector<T>& DVector<T>::operator+=(const Vector<T> &w) +{ + return static_cast<DVector<T>&>(this->add(w)); +} + +template<class T> +DVector<T>& DVector<T>::operator-=(const Vector<T> &w) +{ + return static_cast<DVector<T>&>(this->sub(w)); +} + +template<class T> +DVector<T>& DVector<T>::operator*=(double k) +{ + return static_cast<DVector<T>&>(this->mult(k)); +} + +template<class T> +DVector<T>& DVector<T>::operator/=(double k) +{ + return static_cast<DVector<T>&>(this->divide(k)); +} + +template<class T> +DVector<T>& DVector<T>::operator~() +{ + return static_cast<DVector<T>&>(this->reverse()); +} + +template<class T> +DVector<T>& DVector<T>::unit() +{ + return static_cast<DVector<T>&>(this->_unit()); +} + +template<class T> +const DVector<T> operator-(DVector<T> v) +{ + return ~v; +} + +template<class T> +const DVector<T> operator+(DVector<T> v, const Vector<T>& w) +{ + return v += w; +} + +template<class T> +const DVector<T> operator-(DVector<T> v, const Vector<T>& w) +{ + return v -= w; +} + +template<class T> +const DVector<T> operator*(DVector<T> v, double k) +{ + return v *= k; +} + +template<class T> +const DVector<T> operator/(DVector<T> v, double k) +{ + return v /= k; +} + +template<class T> +const DVector<T> operator*(double k, DVector<T> v) +{ + return v *= k; +} + +template<class T> +const DVector<T> operator/(double k, DVector<T> v) +{ + return v /= k; +} + +inline const DVector<double> operator^(const DVector<double>& v, const Vector<double>& w) +{ + DVector<double> u; + + size_t n = (v.size() <= w.size()) ? v.size() : w.size(); + + if (n > 2) + { + u << v[1] * w[2] - v[2] * w[1]; + u << v[2] * w[0] - v[0] * w[2]; + u << v[0] * w[1] - v[1] * w[0]; + + } else if (n == 2) + u << v[0] * w[1] - v[1] * w[0]; + else if (n == 1) + u << v[0] * w[0]; + + return u; +} + +#endif diff --git a/engine/include/symbasic/svector.h b/engine/include/symbasic/svector.h new file mode 100644 index 0000000..c53ae0f --- /dev/null +++ b/engine/include/symbasic/svector.h @@ -0,0 +1,78 @@ +#ifndef __STATIC_VECTOR_H__ +#define __STATIC_VECTOR_H__ + +#include <array> +#include "vector.h" + +template<class T, std::size_t N> +class SVector : public Vector<T> +{ + +public: + + /* Constructors */ + + SVector(const T &value = T()); + SVector(const std::initializer_list<T> &init); + SVector(const SVector<T, N>&); + + SVector& operator=(const SVector<T, N>&); + + virtual const size_t size() const override; + + virtual T& operator[](size_t i) override; + virtual const T& operator[](size_t i) const override; + + /* Plus / minus */ + SVector<T, N>& operator+=(const Vector<T>&); + SVector<T, N>& operator-=(const Vector<T>&); + + /* Scalar multiplication / division */ + SVector<T, N>& operator*=(double k); + SVector<T, N>& operator/=(double k); + + /* Reverse operator */ + SVector<T, N>& operator~(); + + /* Unit vector */ + SVector<T, N>& unit(); + +private: + + std::array<T, N> components; +}; + +template<class T, std::size_t N> +const SVector<T, N> operator+(SVector<T, N> , const Vector<T>& ); + +template<class T, std::size_t N> +const SVector<T, N> operator-(SVector<T, N> , const Vector<T>& ); + +template<class T, std::size_t N> +const SVector<T, N> operator*(SVector<T, N> , double); + +template<class T, std::size_t N> +const SVector<T, N> operator/(SVector<T, N> , double); + +template<class T, std::size_t N> +const SVector<T, N> operator*(double, SVector<T, N>); + +template<class T, std::size_t N> +const SVector<T, N> operator/(double, SVector<T, N>); + +template<class T, std::size_t N> +const SVector<T, N> operator-(SVector<T, N>); + +/* + * Prototipe for cross product + */ +inline const SVector<double, 3> operator^(const SVector<double, 3>& v, const SVector<double, 3>& w); +inline double operator^(const SVector<double, 2>& v, const SVector<double, 2>& w); +inline double operator^(const SVector<double, 1>& v, const SVector<double, 1>& w); + +/* + * Include definitions file + */ +#include "svector.tpp" + +#endif // __SVECTOR_H__ diff --git a/engine/include/symbasic/svector.tpp b/engine/include/symbasic/svector.tpp new file mode 100644 index 0000000..e054049 --- /dev/null +++ b/engine/include/symbasic/svector.tpp @@ -0,0 +1,162 @@ +#ifndef __SVECTOR_TPP__ +#define __SVECTOR_TPP__ + +template<class T, std::size_t N> +SVector<T, N>::SVector(const T &value) +{ + components.fill(value); +} + +template<class T, std::size_t N> +SVector<T, N>::SVector(const std::initializer_list<T> &init) +{ + size_t n = (init.size() < N) ? init.size() : N; + + /* copy init list without include <algorithm> */ + size_t index = 0; + + /* copy the initializer list until its size */ + for (auto c : init) + { + if (index >= n) + break; + + components[index++] = c; + } + + /* fill the remaining components with zero */ + while(index < N) + components[index++] = 0; +} + +template<class T, std::size_t N> +SVector<T, N>::SVector(const SVector<T, N> &cpy) +{ + this->components = cpy.components; +} + +template<class T, std::size_t N> +SVector<T, N>& SVector<T, N>::operator=(const SVector<T, N> &v) +{ + this->components = v.components; +} + +template<class T, std::size_t N> +const size_t SVector<T, N>::size() const +{ + return N; +} + +/* Access operators */ +template<class T, std::size_t N> +T& SVector<T, N>::operator[](size_t i) +{ + return components[i]; +} + +template<class T, std::size_t N> +const T& SVector<T, N>::operator[](size_t i) const +{ + return components[i]; +} + +template<class T, std::size_t N> +SVector<T, N>& SVector<T, N>::operator+=(const Vector<T> &w) +{ + return static_cast<SVector<T, N>&>(this->add(w)); +} + +template<class T, std::size_t N> +SVector<T, N>& SVector<T, N>::operator-=(const Vector<T> &w) +{ + return static_cast<SVector<T, N>&>(this->sub(w)); +} + +template<class T, std::size_t N> +SVector<T, N>& SVector<T, N>::operator*=(double k) +{ + return static_cast<SVector<T, N>&>(this->mult(k)); +} + +template<class T, std::size_t N> +SVector<T, N>& SVector<T, N>::operator/=(double k) +{ + return static_cast<SVector<T, N>&>(this->divide(k)); +} + +template<class T, std::size_t N> +SVector<T, N>& SVector<T, N>::operator~() +{ + return static_cast<SVector<T, N>&>(this->reverse()); +} + +template<class T, std::size_t N> +SVector<T, N>& SVector<T, N>::unit() +{ + return static_cast<SVector<T, N>&>(this->_unit()); +} + +template<class T, std::size_t N> +const SVector<T, N> operator+(SVector<T, N> v, const Vector<T>& w) +{ + return v += w; +} + +template<class T, std::size_t N> +const SVector<T, N> operator-(SVector<T, N> v, const Vector<T>& w) +{ + return v -= w; +} + +template<class T, std::size_t N> +const SVector<T, N> operator*(SVector<T, N> v, double k) +{ + return v *= k; +} + +template<class T, std::size_t N> +const SVector<T, N> operator/(SVector<T, N> v, double k) +{ + return v /= k; +} + +template<class T, std::size_t N> +const SVector<T, N> operator*(double k, SVector<T, N> v) +{ + return v *= k; +} + +template<class T, std::size_t N> +const SVector<T, N> operator/(double k, SVector<T, N> v) +{ + return v /= k; +} + +template<class T, std::size_t N> +const SVector<T, N> operator-(SVector<T, N> v) +{ + return ~v; +} + +inline const SVector<double, 3> operator^(const SVector<double, 3>& v, const SVector<double, 3>& w) +{ + SVector<double, 3> u = {0, 0, 0}; + + u[0] = v[1] * w[2] - v[2] * w[1]; + u[1] = v[2] * w[0] - v[0] * w[2]; + u[2] = v[0] * w[1] - v[1] * w[0]; + + return u; +} + +inline double operator^(const SVector<double, 2>& v, const SVector<double, 2>& w) +{ + return v[0] * w[1] - v[1] * w[0]; +} + +inline double operator^(const SVector<double, 1>& v, const SVector<double, 1>& w) +{ + return v[0] * w[0]; +} + +#endif diff --git a/engine/include/symbasic/vector.h b/engine/include/symbasic/vector.h new file mode 100644 index 0000000..f8574dc --- /dev/null +++ b/engine/include/symbasic/vector.h @@ -0,0 +1,133 @@ +#ifndef __VECTOR_H__ +#define __VECTOR_H__ + +#ifdef _USE_VECTOR_OSTREAM +#include <iosfwd> +#endif + +#include <cstddef> + +template <class T> +class Vector { + +public: + + typedef std::size_t size_t; + + class iterator + { + size_t i; + Vector<T> *v; + + public: + + iterator(Vector<T> *v, size_t i = 0) + : v(v), i(i) + { + } + + iterator& operator++() + { + ++i; + } + + iterator& operator--() + { + --i; + } + + bool operator!=(const iterator& it) + { + return this->i != it.i; + } + + bool operator==(const iterator& it) + { + return !(*this != it); + } + + T& operator*() + { + return (*v)[i]; + } + }; + + typedef const iterator const_iterator; + + /* For auto implementation */ + /* it allows to loop through components + * + * for (auto comp : v) + * { + * // loop content + * } + */ + + iterator begin(); + iterator end(); + + const_iterator begin() const; + const_iterator end() const; + + //Virtual accesseurs + + virtual const size_t size() const = 0; + + /* Accède à la i-ème valeur */ + virtual T& operator[](size_t i) = 0; + virtual const T& operator[](size_t i) const = 0; + + /* Return the module */ + double module() const; + double sq_module() const; // squared module + + //Operateurs + + /* Operateurs de comparaison */ + bool operator==(const Vector<T>&) const; + bool operator!=(const Vector<T>&) const; + + bool operator>=(const Vector<T>&) const; + bool operator<=(const Vector<T>&) const; + + bool operator>=(double) const; + bool operator<=(double) const; + + bool operator>(const Vector<T>&) const; + bool operator<(const Vector<T>&) const; + + bool operator>(double) const; + bool operator<(double) const; + + /* Dot product */ + double operator*(const Vector<T>&) const; + + /* For printing a Vector on a ostream */ +#ifdef _USE_VECTOR_OSTREAM + void affiche(std::ostream&) const; +#endif + +protected: // not stable operations + + /* Plus / minus */ + Vector<T>& add(const Vector<T>&); + Vector<T>& sub(const Vector<T>&); + + /* Scalar multiplication / division */ + Vector<T>& mult(double k); + Vector<T>& divide(double k); + + Vector<T>& reverse(); + Vector<T>& _unit() const; +}; + +#ifdef _USE_VECTOR_OSTREAM +// forward declaration of ostream + +template<class T> +std::ostream& operator<<(std::ostream&, const Vector<T>&); +#endif + +#include "vector.tpp" + +#endif diff --git a/engine/include/symbasic/vector.tpp b/engine/include/symbasic/vector.tpp new file mode 100644 index 0000000..d622940 --- /dev/null +++ b/engine/include/symbasic/vector.tpp @@ -0,0 +1,213 @@ +#ifndef __VECTOR_TPP__ +#define __VECTOR_TPP__ + +/* for auto loop */ + +template<class T> +typename Vector<T>::iterator Vector<T>::begin() +{ + return iterator(this); +} + +template<class T> +typename Vector<T>::iterator Vector<T>::end() +{ + return iterator(this, this->size() - 1); +} + +template<class T> +typename Vector<T>::const_iterator Vector<T>::begin() const +{ + return iterator(this); +} + +template<class T> +typename Vector<T>::const_iterator Vector<T>::end() const +{ + return iterator(this, this->size() - 1); +} + +template<class T> +bool Vector<T>::operator==(const Vector<T>& v) const { + + if(this->size() != v.size()) + return false; + + //Retourne déjà faux si les deux vecteurs n'ont pas la même dimension + for(size_t i(0); i < v.size(); ++i){ + + if ((*this)[i] != v[i]) + return false; + //Lors que le programme rencontre deux coordonnées différenentes respectivement pour la même dimension des deux vecteurs + //la fonction retourne false + } + return true; +} + +template<class T> +bool Vector<T>::operator>=(const Vector<T>& v) const +{ + return sq_module() >= v.sq_module(); +} + +template<class T> +bool Vector<T>::operator<=(const Vector<T>& v) const +{ + return sq_module() <= v.sq_module(); +} + +template<class T> +bool Vector<T>::operator>=(double m) const +{ + return sq_module() >= m * m; +} + +template<class T> +bool Vector<T>::operator<=(double m) const +{ + return sq_module() <= m * m; +} + +template<class T> +bool Vector<T>::operator>(const Vector<T>& v) const +{ + return sq_module() > v.sq_module(); +} + +template<class T> +bool Vector<T>::operator<(const Vector<T>& v) const +{ + return sq_module() < v.sq_module(); +} + +template<class T> +bool Vector<T>::operator>(double m) const +{ + return sq_module() > m * m; +} + +template<class T> +bool Vector<T>::operator<(double m) const +{ + return sq_module() < m * m; +} + +template<class T> +bool Vector<T>::operator!=(const Vector<T>& w) const +{ + return !(*this == w); +} + +//Operateurs somme et produit + +template<class T> +Vector<T>& Vector<T>::add(const Vector<T>& w) +{ + size_t n = (this->size() <= w.size()) ? this->size() : w.size(); + + for (size_t i= 0; i < n; ++i) + (*this)[i] += w[i]; + + return *this; +} + + +template<class T> +Vector<T>& Vector<T>::sub(const Vector<T>& w) +{ + size_t n = (this->size() <= w.size()) ? this->size() : w.size(); + + for (size_t i = 0; i < n; ++i) + (*this)[i] -= w[i]; + + return *this; +} + +template<class T> +Vector<T>& Vector<T>::mult(double k) +{ + for (size_t i = 0; i < this->size(); ++i) + (*this)[i] *= k; + + return *this; +} + +template<class T> +Vector<T>& Vector<T>::divide(double k) +{ + for (size_t i = 0; i < this->size(); ++i) + (*this)[i] /= k; + + return *this; +} + +template<class T> +Vector<T>& Vector<T>::_unit() const +{ + return (*this) /= this->module(); +} + +/* Dot product */ +template<class T> +double Vector<T>::operator*(const Vector<T>& w) const +{ + double x = 0; + size_t n = (this->size() <= w.size()) ? this->size() : w.size(); + + for (size_t i = 0; i < n; ++i) + x += (*this)[i] * w[i]; + + return x; +} + +template<class T> +Vector<T>& Vector<T>::reverse() +{ + return (*this)*=-1.0; +} + +template<class T> +double Vector<T>::sq_module() const +{ + return (*this) * (*this); +} + +template<class T> +double Vector<T>::module() const +{ + return sqrt(this->sq_module()); +} + +/* Implementation of ostream operator overloading */ + +/* Le prototype je le mets dans un header séparé parce que + * il ne fait pas partie de l'implementation d'un vecteur + * Une classe l'utilisant n'as pas besoin de l'imprimer + */ + +#ifdef _USE_VECTOR_OSTREAM +#include <ostream> + +template<class T> +void Vector<T>::affiche(std::ostream& os) const +{ + os << "("; + + for (size_t i(0); i < (size() - 1); ++i) + { + os << (*this)[i] << ", "; + } + + os << (*this)[size()-1] << ")"; +} + +// Flux de sortie permettant d'afficher les coordonnées du vecteur +template<class T> +std::ostream& operator<<(std::ostream& os, const Vector<T>& v) +{ + v.affiche(os); + return os; +} +#endif + +#endif //__VECTOR_TPP__ Binary files differ |