diff --git a/SConstruct b/SConstruct old mode 100644 new mode 100755 index e52496c..265003d --- a/SConstruct +++ b/SConstruct @@ -23,6 +23,8 @@ if 'SFML_PATH' in os.environ: SFML_PATH = os.environ['SFML_PATH'] CPPFLAGS = ['-I%s/include' % SFML_PATH] LIBPATH = ['%s/lib' % SFML_PATH] +for dirent in os.listdir('src'): + CPPFLAGS.append('-I%s/src/%s' % (os.getcwd(), dirent)) if platform == 'windows': # Windows-specific environment settings diff --git a/src/client/Client.cc b/src/client/Client.cc old mode 100644 new mode 100755 index 6143e91..7f1be7d --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -1,45 +1,32 @@ -#include -#include +#include "Client.h" -int main(int argc, char *argv[]) +Client::Client(bool fullscreen) { - bool fullscreen = false; - for (;;) - { - int c = getopt_long(argc, argv, "f", NULL, NULL); - if (c == -1) - break; - switch (c) - { - case 'f': - fullscreen = true; - break; - } - } sf::VideoMode mode = fullscreen ? sf::VideoMode::getDesktopMode() : sf::VideoMode(800, 600, 32); long style = fullscreen ? sf::Style::Fullscreen : sf::Style::Resize | sf::Style::Close; - sf::Window window(mode, "Treacherous Terrain", style); + m_window = new sf::Window(mode, "Treacherous Terrain", style); +} - while (window.isOpen()) +void Client::run() +{ + while (m_window->isOpen()) { sf::Event event; - while (window.pollEvent(event)) + while (m_window->pollEvent(event)) { if (event.type == sf::Event::Closed) - window.close(); + m_window->close(); if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape) ) - window.close(); + m_window->close(); } - window.display(); + m_window->display(); } - - return 0; } diff --git a/src/client/Client.h b/src/client/Client.h new file mode 100755 index 0000000..70826a5 --- /dev/null +++ b/src/client/Client.h @@ -0,0 +1,17 @@ + +#ifndef CLIENT_H +#define CLIENT_H + +#include +#include "refptr.h" + +class Client +{ + public: + Client(bool fullscreen); + void run(); + protected: + refptr m_window; +}; + +#endif diff --git a/src/client/main.cc b/src/client/main.cc new file mode 100755 index 0000000..e565249 --- /dev/null +++ b/src/client/main.cc @@ -0,0 +1,26 @@ + +#include +#include "Client.h" + +int main(int argc, char *argv[]) +{ + bool fullscreen = false; + for (;;) + { + int c = getopt_long(argc, argv, "f", NULL, NULL); + if (c == -1) + break; + switch (c) + { + case 'f': + fullscreen = true; + break; + } + } + + Client client(fullscreen); + + client.run(); + + return 0; +} diff --git a/src/common/refptr.h b/src/common/refptr.h new file mode 100644 index 0000000..7335af5 --- /dev/null +++ b/src/common/refptr.h @@ -0,0 +1,99 @@ + +#ifndef REFPTR_H +#define REFPTR_H REFPTR_H + +/* Author: Josh Holtrop + * Purpose: Provide a reference-counting pointer-like first order + * C++ object that will free the object it is pointing to when + * all references to it have been destroyed. + * This implementation does not solve the circular reference problem. + * I was not concerned with that when developing this class. + */ +#include /* NULL */ + +template +class refptr +{ + public: + refptr(); + refptr(T * ptr); + refptr(const refptr & orig); + refptr & operator=(const refptr & orig); + refptr & operator=(T * ptr); + ~refptr(); + T & operator*() const { return *m_ptr; } + T * operator->() const { return m_ptr; } + bool isNull() const { return m_ptr == NULL; } + + private: + void cloneFrom(const refptr & orig); + void destroy(); + + T * m_ptr; + int * m_refCount; +}; + +template refptr::refptr() +{ + m_ptr = NULL; + m_refCount = NULL; +} + +template refptr::refptr(T * ptr) +{ + m_ptr = ptr; + m_refCount = new int; + *m_refCount = 1; +} + +template refptr::refptr(const refptr & orig) +{ + cloneFrom(orig); +} + +template refptr & refptr::operator=(const refptr & orig) +{ + destroy(); + cloneFrom(orig); + return *this; +} + +template refptr & refptr::operator=(T * ptr) +{ + destroy(); + m_ptr = ptr; + m_refCount = new int; + *m_refCount = 1; + return *this; +} + +template void refptr::cloneFrom(const refptr & orig) +{ + this->m_ptr = orig.m_ptr; + this->m_refCount = orig.m_refCount; + if (m_refCount != NULL) + (*m_refCount)++; +} + +template refptr::~refptr() +{ + destroy(); +} + +template void refptr::destroy() +{ + if (m_refCount != NULL) + { + if (*m_refCount <= 1) + { + delete m_ptr; + delete m_refCount; + } + else + { + (*m_refCount)--; + } + } +} + +#endif