split client into main.cc and Client class

This commit is contained in:
Josh Holtrop 2012-08-14 21:46:41 -04:00
parent c216b9d09e
commit 34e909fca8
5 changed files with 155 additions and 24 deletions

2
SConstruct Normal file → Executable file
View File

@ -23,6 +23,8 @@ if 'SFML_PATH' in os.environ:
SFML_PATH = os.environ['SFML_PATH'] SFML_PATH = os.environ['SFML_PATH']
CPPFLAGS = ['-I%s/include' % SFML_PATH] CPPFLAGS = ['-I%s/include' % SFML_PATH]
LIBPATH = ['%s/lib' % 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': if platform == 'windows':
# Windows-specific environment settings # Windows-specific environment settings

35
src/client/Client.cc Normal file → Executable file
View File

@ -1,45 +1,32 @@
#include <getopt.h> #include "Client.h"
#include <SFML/Window.hpp>
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 mode = fullscreen
? sf::VideoMode::getDesktopMode() ? sf::VideoMode::getDesktopMode()
: sf::VideoMode(800, 600, 32); : sf::VideoMode(800, 600, 32);
long style = fullscreen long style = fullscreen
? sf::Style::Fullscreen ? sf::Style::Fullscreen
: sf::Style::Resize | sf::Style::Close; : 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; sf::Event event;
while (window.pollEvent(event)) while (m_window->pollEvent(event))
{ {
if (event.type == sf::Event::Closed) if (event.type == sf::Event::Closed)
window.close(); m_window->close();
if ( (event.type == sf::Event::KeyPressed) if ( (event.type == sf::Event::KeyPressed)
&& (event.key.code == sf::Keyboard::Escape) ) && (event.key.code == sf::Keyboard::Escape) )
window.close(); m_window->close();
} }
window.display(); m_window->display();
} }
return 0;
} }

17
src/client/Client.h Executable file
View File

@ -0,0 +1,17 @@
#ifndef CLIENT_H
#define CLIENT_H
#include <SFML/Window.hpp>
#include "refptr.h"
class Client
{
public:
Client(bool fullscreen);
void run();
protected:
refptr<sf::Window> m_window;
};
#endif

26
src/client/main.cc Executable file
View File

@ -0,0 +1,26 @@
#include <getopt.h>
#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;
}

99
src/common/refptr.h Normal file
View File

@ -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 <stdlib.h> /* NULL */
template <typename T>
class refptr
{
public:
refptr<T>();
refptr<T>(T * ptr);
refptr<T>(const refptr<T> & orig);
refptr<T> & operator=(const refptr<T> & orig);
refptr<T> & operator=(T * ptr);
~refptr<T>();
T & operator*() const { return *m_ptr; }
T * operator->() const { return m_ptr; }
bool isNull() const { return m_ptr == NULL; }
private:
void cloneFrom(const refptr<T> & orig);
void destroy();
T * m_ptr;
int * m_refCount;
};
template <typename T> refptr<T>::refptr()
{
m_ptr = NULL;
m_refCount = NULL;
}
template <typename T> refptr<T>::refptr(T * ptr)
{
m_ptr = ptr;
m_refCount = new int;
*m_refCount = 1;
}
template <typename T> refptr<T>::refptr(const refptr<T> & orig)
{
cloneFrom(orig);
}
template <typename T> refptr<T> & refptr<T>::operator=(const refptr<T> & orig)
{
destroy();
cloneFrom(orig);
return *this;
}
template <typename T> refptr<T> & refptr<T>::operator=(T * ptr)
{
destroy();
m_ptr = ptr;
m_refCount = new int;
*m_refCount = 1;
return *this;
}
template <typename T> void refptr<T>::cloneFrom(const refptr<T> & orig)
{
this->m_ptr = orig.m_ptr;
this->m_refCount = orig.m_refCount;
if (m_refCount != NULL)
(*m_refCount)++;
}
template <typename T> refptr<T>::~refptr()
{
destroy();
}
template <typename T> void refptr<T>::destroy()
{
if (m_refCount != NULL)
{
if (*m_refCount <= 1)
{
delete m_ptr;
delete m_refCount;
}
else
{
(*m_refCount)--;
}
}
}
#endif