2
0
Эх сурвалжийг харах

Merge branch 'master' into rendering-2

Daniele Bartolini 12 жил өмнө
parent
commit
43c39e05d2

+ 1 - 0
CMakeLists.txt

@@ -26,6 +26,7 @@ set (INCLUDES
 	${CMAKE_SOURCE_DIR}/src/core/threads
 	${CMAKE_SOURCE_DIR}/src/core/settings
 	${CMAKE_SOURCE_DIR}/src/os
+	${CMAKE_SOURCE_DIR}/src/os/posix
 	${CMAKE_SOURCE_DIR}/src/os/linux
 	${CMAKE_SOURCE_DIR}/src/input
 	${CMAKE_SOURCE_DIR}/src/renderers

+ 6 - 3
android/jni/Android.mk

@@ -36,8 +36,6 @@ LOCAL_SRC_FILES :=\
 	core/streams/FileStream.cpp\
 	core/streams/MemoryStream.cpp\
 	core/streams/Stream.cpp\
-	core/threads/Thread.cpp\
-	core/threads/Mutex.cpp\
 \
 	input/EventDispatcher.cpp\
 	input/InputManager.cpp\
@@ -47,6 +45,11 @@ LOCAL_SRC_FILES :=\
 	os/android/AndroidInput.cpp\
 	os/android/AndroidDevice.cpp\
 	os/android/File.cpp\
+	os/posix/Thread.cpp\
+	os/posix/Mutex.cpp\
+	os/posix/Cond.cpp\
+	os/posix/TCPSocket.cpp\
+	os/posix/UDPSocket.cpp\
 \
 	Filesystem.cpp\
 \
@@ -102,4 +105,4 @@ LOCAL_C_INCLUDES	:=\
 LOCAL_CPPFLAGS	:= -g -fexceptions
 LOCAL_LDLIBS	:= -llog -landroid -lGLESv1_CM
 include $(BUILD_SHARED_LIBRARY)
-#(call import-module, android/native_app_glue)
+#(call import-module, android/native_app_glue)

+ 1 - 11
src/CMakeLists.txt

@@ -168,15 +168,6 @@ set (COMPRESSORS_HEADERS
 	core/compressors/ZipCompressor.h
 )
 
-set (THREADS_HEADERS
-	core/threads/Thread.h
-	core/threads/Mutex.h
-	core/threads/Cond.h
-)
-
-set (THREADS_SRC
-)
-
 set (SETTINGS_SRC
 	core/settings/IntSetting.cpp
 	core/settings/FloatSetting.cpp
@@ -224,6 +215,7 @@ set (RENDERERS_HEADERS
 
 set (OS_HEADERS
 	os/OS.h
+	os/NetAddress.h
 )
 
 set (OS_SRC
@@ -247,7 +239,6 @@ set (CROWN_SOURCES
 	${STREAMS_SRC}
 	${MEM_SRC}
 	${COMPRESSORS_SRC}
-	${THREADS_SRC}
 	${SETTINGS_SRC}
 
 	${INPUT_SRC}
@@ -269,7 +260,6 @@ set (CROWN_HEADERS
 	${STREAMS_HEADERS}
 	${MEM_HEADERS}
 	${COMPRESSORS_HEADERS}
-	${THREADS_HEADERS}
 	${SETTINGS_HEADERS}
 
 	${INPUT_HEADERS}

+ 1 - 1
src/Filesystem.h

@@ -143,7 +143,7 @@ public:
 
 	/// Closes a previously opened file @stream
 	void				close(FileStream* stream);
-	
+
 private:
 
 	// Builds the OS-dependent path from base_path and relative_path

+ 6 - 6
src/ResourceManager.h

@@ -161,14 +161,14 @@ private:
 
 	// Background loading thread
 	bool 					m_background_thread_should_run;
-	Thread					m_thread;
+	os::Thread				m_thread;
 
-	mutable Mutex			m_loading_mutex;
-	Cond 					m_loading_requests;
-	Cond 					m_all_loaded;
+	mutable os::Mutex		m_loading_mutex;
+	os::Cond 				m_loading_requests;
+	os::Cond 				m_all_loaded;
 
-	Mutex					m_loaded_mutex;
-	mutable Mutex			m_resources_mutex;
+	os::Mutex				m_loaded_mutex;
+	mutable os::Mutex		m_resources_mutex;
 
 	friend class			Device;
 };

+ 29 - 58
src/core/streams/FileStream.cpp

@@ -33,26 +33,16 @@ namespace crown
 
 //-----------------------------------------------------------------------------
 FileStream::FileStream(StreamOpenMode mode, const char* filename) :
-	Stream(mode), m_file(NULL),
+	Stream(mode),
 	m_last_was_read(true)
 {
-	//Takes ownership
-	FileOpenMode file_open_mode = (FileOpenMode)0;
-
-	if (math::test_bitmask(mode, SOM_READ))
-		file_open_mode = (FileOpenMode)(file_open_mode | FOM_READ);
-	if (math::test_bitmask(mode, SOM_WRITE))
-		file_open_mode = (FileOpenMode)(file_open_mode | FOM_WRITE);
-
-	m_file = File::open(filename, file_open_mode);
+	m_file.open(filename, mode);
 }
 
 //-----------------------------------------------------------------------------
 FileStream::~FileStream()
 {
-	delete m_file;
-
-	m_file = 0;
+	//m_file.close();
 }
 
 //-----------------------------------------------------------------------------
@@ -60,7 +50,7 @@ void FileStream::seek(size_t position)
 {
 	check_valid();
 
-	m_file->seek(position, SEEK_SET);
+	m_file.seek(position);
 }
 
 //-----------------------------------------------------------------------------
@@ -68,7 +58,7 @@ void FileStream::seek_to_end()
 {
 	check_valid();
 
-	m_file->seek(0, SEEK_END);
+	m_file.seek_to_end();
 }
 
 //-----------------------------------------------------------------------------
@@ -76,7 +66,7 @@ void FileStream::skip(size_t bytes)
 {
 	check_valid();
 
-	m_file->seek(bytes, SEEK_CUR);
+	m_file.skip(bytes);
 }
 
 //-----------------------------------------------------------------------------
@@ -87,15 +77,12 @@ uint8_t FileStream::read_byte()
 	if (!m_last_was_read)
 	{
 		m_last_was_read = true;
-		m_file->seek(0, SEEK_CUR);
+		m_file.seek(0);
 	}
 
 	uint8_t buffer;
-	
-	if (m_file->read(&buffer, 1, 1) != 1)
-	{
-		Log::e("Could not read from file");
-	}
+
+	assert(m_file.read(&buffer, 1));
 
 	return buffer;
 }
@@ -108,13 +95,10 @@ void FileStream::read(void* buffer, size_t size)
 	if (!m_last_was_read)
 	{
 		m_last_was_read = true;
-		m_file->seek(0, SEEK_CUR);
+		m_file.seek(0);
 	}
 
-	if (m_file->read(buffer, size, 1) != 1)
-	{
-		Log::e("Could not read from file.");
-	}
+	assert(m_file.read(buffer, size));
 }
 
 //-----------------------------------------------------------------------------
@@ -131,22 +115,22 @@ bool FileStream::copy_to(Stream* stream, size_t size)
 
 	char* buff = new char[chunksize];
 
-	size_t totReadBytes = 0;
+	size_t tot_read_bytes = 0;
 
-	while (totReadBytes < size)
+	while (tot_read_bytes < size)
 	{
-		int32_t readBytes;
-		int32_t expectedReadBytes = math::min(size - totReadBytes, chunksize);
+		size_t read_bytes;
+		size_t expected_read_bytes = math::min(size - tot_read_bytes, chunksize);
 
-		readBytes = m_file->read(buff, 1, expectedReadBytes);
+		read_bytes = m_file.read(buff, expected_read_bytes);
 
-		if (readBytes < expectedReadBytes)
+		if (read_bytes < expected_read_bytes)
 		{
-			if (m_file->eof())
+			if (m_file.eof())
 			{
-				if (readBytes != 0)
+				if (read_bytes != 0)
 				{
-					stream->write(buff, readBytes);
+					stream->write(buff, read_bytes);
 				}
 			}
 
@@ -155,8 +139,8 @@ bool FileStream::copy_to(Stream* stream, size_t size)
 			return false;
 		}
 
-		stream->write(buff, readBytes);
-		totReadBytes += readBytes;
+		stream->write(buff, read_bytes);
+		tot_read_bytes += read_bytes;
 	}
 
 	delete [] buff;
@@ -172,14 +156,7 @@ bool FileStream::end_of_stream() const
 //-----------------------------------------------------------------------------
 bool FileStream::is_valid() const
 {
-	{
-		if (!m_file)
-		{
-			return false;
-		}
-
-		return m_file->is_valid();
-	}
+	return m_file.is_open();
 }
 
 //-----------------------------------------------------------------------------
@@ -190,13 +167,10 @@ void FileStream::write_byte(uint8_t val)
 	if (m_last_was_read)
 	{
 		m_last_was_read = false;
-		m_file->seek(0, SEEK_CUR);
+		m_file.seek(0);
 	}
 
-	if (m_file->write(&val, 1, 1) != 1)
-	{
-		Log::e("Could not write to file.");
-	}
+	assert(m_file.write(&val, 1) == 1);
 }
 
 //-----------------------------------------------------------------------------
@@ -207,13 +181,10 @@ void FileStream::write(const void* buffer, size_t size)
 	if (m_last_was_read)
 	{
 		m_last_was_read = false;
-		m_file->seek(0, SEEK_CUR);
+		m_file.seek(0);
 	}
 
-	if (m_file->write(buffer, size, 1) != 1)
-	{
-		Log::e("Could not write to file.");
-	}
+	assert(m_file.write(buffer, size) == size);
 }
 
 //-----------------------------------------------------------------------------
@@ -229,7 +200,7 @@ size_t FileStream::position() const
 {
 	check_valid();
 
-	return m_file->tell();
+	return m_file.position();
 }
 
 //-----------------------------------------------------------------------------
@@ -237,7 +208,7 @@ size_t FileStream::size() const
 {
 	check_valid();
 	
-	return m_file->size();
+	return m_file.size();
 }
 
 //-----------------------------------------------------------------------------

+ 4 - 3
src/core/streams/FileStream.h

@@ -25,9 +25,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include <cassert>
+
 #include "Stream.h"
 #include "File.h"
-#include <cassert>
 
 namespace crown
 {
@@ -93,14 +94,14 @@ public:
 
 protected:
 
-	File*			m_file;
+	File			m_file;
 	bool			m_last_was_read;
 
 protected:
 
 	inline void		check_valid() const
 	{
-		assert(m_file != NULL);
+		assert(m_file.is_open());
 	}
 };
 

+ 39 - 57
src/network/BitMessage.cpp

@@ -1,15 +1,42 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
 #include <cassert>
 #include <cstring>
 
+#include "NetAddress.h"
 #include "BitMessage.h"
 
 namespace crown
 {
 namespace network
 {
-  
+
+//---------------------------------------------------------------------------------------------
 BitMessage::BitMessage(Allocator& allocator) :
-	m_allocator(&allocator),
+	m_allocator(allocator),
 	m_header(NULL),
 	m_data(NULL),
 	m_write(NULL), 
@@ -25,20 +52,19 @@ BitMessage::BitMessage(Allocator& allocator) :
 }
 
 //---------------------------------------------------------------------------------------------
-
 BitMessage::~BitMessage()
 {
 	if (m_header)
 	{
-		m_allocator->deallocate((void*)m_header);
+		m_allocator.deallocate((void*)m_header);
 	}
 	if (m_data)
 	{
-		  m_allocator->deallocate((void*)m_data);
+		  m_allocator.deallocate((void*)m_data);
 	}
 }
-//---------------------------------------------------------------------------------------------
 
+//---------------------------------------------------------------------------------------------
 uint8_t* BitMessage::get_byte_space(int32_t len)
 {
 	uint8_t *ptr;
@@ -62,7 +88,6 @@ uint8_t* BitMessage::get_byte_space(int32_t len)
 }
 
 //---------------------------------------------------------------------------------------------
-
 bool BitMessage::check_overflow(int32_t num_bits)
 {
 	assert(num_bits >= 0);
@@ -78,15 +103,15 @@ bool BitMessage::check_overflow(int32_t num_bits)
 		m_overflowed = true;
 		return true;
 	}
-	return false;  
+	return false;
 }
 
 //---------------------------------------------------------------------------------------------
 void BitMessage::init(int32_t len)
 {
-	m_header = (uint8_t*)m_allocator->allocate(12);
+	m_header = (uint8_t*)m_allocator.allocate(12);
 	
-	m_data = (uint8_t*)m_allocator->allocate(len);
+	m_data = (uint8_t*)m_allocator.allocate(len);
 	
 	m_write = m_data;
 	m_read = m_data;
@@ -169,7 +194,6 @@ size_t BitMessage::get_size() const
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::set_size(size_t size)
 {
 	if (size > m_max_size)
@@ -183,14 +207,12 @@ void BitMessage::set_size(size_t size)
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_write_bit() const
 {
 	return m_write_bit;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::set_write_bit(int32_t bit)
 {
 	m_write_bit = bit & 7;
@@ -201,21 +223,18 @@ void BitMessage::set_write_bit(int32_t bit)
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_num_bits_written() const
 {
 	return ((m_cur_size << 3) - ((8 - m_write_bit) & 7));  
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_remaining_write_bits() const
 {
 	return (m_max_size << 3) - get_num_bits_written(); 
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::save_write_state(int32_t& s,int32_t& b) const
 {
 	s = m_cur_size;
@@ -223,7 +242,6 @@ void BitMessage::save_write_state(int32_t& s,int32_t& b) const
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::restore_write_state(int32_t s,int32_t b)
 {
 	m_cur_size = s;
@@ -236,49 +254,42 @@ void BitMessage::restore_write_state(int32_t s,int32_t b)
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_read_count() const
 {
 	return m_read_count;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::set_read_count(int32_t bytes)
 {
 	m_read_count = bytes;
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_read_bit() const
 {
 	return m_read_bit;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::set_read_bit(int32_t bit)
 {
 	m_read_bit = bit & 7;
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_num_bits_read() const
 {
 	return ((m_read_count << 3) - ((8 - m_read_bit) & 7));  
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_remaining_read_bits() const
 {
 	return (m_cur_size << 3) - get_num_bits_read();
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::save_read_state(int32_t& c, int32_t& b) const
 {
 	c = m_read_count;
@@ -286,7 +297,6 @@ void BitMessage::save_read_state(int32_t& c, int32_t& b) const
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::restore_read_state(int32_t c, int32_t b)
 {
 	m_read_count = c;
@@ -294,7 +304,6 @@ void BitMessage::restore_read_state(int32_t c, int32_t b)
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::begin_writing()
 {
 	m_cur_size = 0;
@@ -303,21 +312,18 @@ void BitMessage::begin_writing()
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_remaining_space() const
 {
 	return m_max_size - m_cur_size;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_byte_align()
 {
 	m_write_bit = 0;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_bits(int32_t value, int32_t num_bits)
 {
 	int32_t		put;
@@ -399,49 +405,42 @@ void BitMessage::write_bits(int32_t value, int32_t num_bits)
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_int8(int32_t c)
 {
 	write_bits(c, -8);
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_uint8(int32_t c)
 {
 	write_bits(c, 8);  
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_int16(int32_t c)
 {
 	write_bits(c, -16);  
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_uint16(int32_t c)
 {
 	write_bits(c, 16);
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_int32(int32_t c)
 {
 	write_bits(c, 32);
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_real(real f)
 {
 	write_bits(*reinterpret_cast<int32_t *>(&f), 32);  
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_vec3(const Vec3& v)
 {
 	write_real(v.x);
@@ -450,7 +449,6 @@ void BitMessage::write_vec3(const Vec3& v)
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_string(const char* s, int32_t max_len, bool make_7_bit)
 {
 	if (!s) 
@@ -501,25 +499,22 @@ void BitMessage::write_string(const char* s, int32_t max_len, bool make_7_bit)
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_data(const void* data, int32_t length)
 {
 	memcpy(get_byte_space(length), data, length);
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::write_netaddr(const os::NetAddress addr)
 {
 	uint8_t* ptr;
 	
 	ptr = get_byte_space(4);
-	memcpy(ptr, addr.address, 4);
-	write_uint16(addr.port);
+	memcpy(ptr, addr.m_address, 4);
+	write_uint16(addr.port());
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::begin_reading() const
 {
 	m_read_count = 0;
@@ -527,21 +522,18 @@ void BitMessage::begin_reading() const
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::get_remaing_data() const
 {
 	m_cur_size - m_read_count;
 }
 
 //---------------------------------------------------------------------------------------------
-
 void BitMessage::read_byte_align() const
 {
 	m_read_bit = 0;
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_bits(int32_t num_bits) const
 {
 	int32_t		value;
@@ -616,14 +608,12 @@ int32_t BitMessage::read_bits(int32_t num_bits) const
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_int8() const
 {
 	return (int32_t)read_bits(-8);
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_uint8() const
 {
   	return (int32_t)read_bits(8);
@@ -631,28 +621,24 @@ int32_t BitMessage::read_uint8() const
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_int16() const
 {
 	return (int32_t)read_bits(-16);  
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_uint16() const
 {
 	return (int32_t)read_bits(16);  
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_int32() const
 {
 	return (int32_t)read_bits(32);
 }
 
 //---------------------------------------------------------------------------------------------
-
 real BitMessage::read_real() const
 {
 	float value;
@@ -661,7 +647,6 @@ real BitMessage::read_real() const
 }
 
 //---------------------------------------------------------------------------------------------
-
 Vec3 BitMessage::read_vec3() const
 {
 	Vec3 v;
@@ -674,7 +659,6 @@ Vec3 BitMessage::read_vec3() const
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_string(char* buffer, int32_t buffer_size) const
 {
 	int	l = 0;
@@ -710,7 +694,6 @@ int32_t BitMessage::read_string(char* buffer, int32_t buffer_size) const
 }
 
 //---------------------------------------------------------------------------------------------
-
 int32_t BitMessage::read_data(void* data, int32_t length) const
 {
 	int count;
@@ -742,13 +725,12 @@ int32_t BitMessage::read_data(void* data, int32_t length) const
 //---------------------------------------------------------------------------------------------
 void BitMessage::read_netaddr(os::NetAddress* addr) const
 {
-
 	for (int i = 0; i < 4; i++) 
 	{
-		addr->address[i] = read_uint8();
+		addr->m_address[i] = read_uint8();
 	}
 	
-	addr->port = read_uint16();  
+	addr->m_port = read_uint16();  
 }
 
 //---------------------------------------------------------------------------------------------

+ 31 - 10
src/network/BitMessage.h

@@ -1,7 +1,31 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
 #pragma once
 
 #include "Types.h"
-#include "OS.h"
 #include "Allocator.h"
 #include "Vec3.h"
 
@@ -9,15 +33,12 @@ namespace crown
 {
 namespace network
 {
-	/**
-	* bit-packet reliable message.
-	* Usage: After every instantition, must be initialized with @init(len)
-	*	TODO: rework as POD is needed; this feature provides compatibility with queue
-	*/
 
+/// Bit-packet reliable message.
+/// Usage: After every instantition, must be initialized with @init(len)
+/// TODO: rework as POD is needed; this feature provides compatibility with queue
 class BitMessage
 {
-
 public:
 						BitMessage(Allocator& allocator);
 						~BitMessage();
@@ -91,7 +112,7 @@ private:
 	
 private:
 	
-	Allocator*			m_allocator;								// memory allocator
+	Allocator&			m_allocator;								// memory allocator
 	
 	uint8_t*			m_header;
 	uint8_t*			m_data;
@@ -106,9 +127,9 @@ private:
 	mutable int32_t		m_read_bit;									// number of bits read from the last read byte
 	
 	bool 				m_overflowed;								// overflow flag
-	bool				m_init;										// is init flag
-	
+	bool				m_init;										// is init flag	
 };
+
 } // namespace network
 } // namespace crown
 

+ 158 - 0
src/os/NetAddress.h

@@ -0,0 +1,158 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "OS.h"
+
+namespace crown
+{
+namespace os
+{
+
+/// OS level network address.
+class NetAddress
+{
+public:
+
+	/// Initializes the address to 127.0.0.1 and port to 1000.
+					NetAddress();
+
+	/// Initializes the address from IP address (as single elemets)
+	/// and the port number.
+					NetAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port);
+
+	
+	/// Returns the IP address as packed 32-bit integer.
+	uint32_t		address() const;
+
+	/// Returns the port number bind to the address.
+	uint16_t		port() const;
+
+	/// Sets both the IP address (packed 32-bit integer) and the port number.
+	void			set(uint32_t address, uint16_t port);
+
+	/// Sets both the IP address (as single elements) and the port number.
+	void			set(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port);
+	
+	bool			operator==(const NetAddress& addr) const;
+	NetAddress&		operator=(const NetAddress& addr);
+	
+	void			print() const;
+
+public:
+
+	uint8_t 		m_address[4];
+	uint16_t 		m_port;
+};
+
+//-----------------------------------------------------------------------------
+NetAddress::NetAddress() :
+	m_port(1000)
+{
+	m_address[0] = 127;
+	m_address[1] = 0;
+	m_address[2] = 0;
+	m_address[3] = 1;
+}
+
+//-----------------------------------------------------------------------------
+NetAddress::NetAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port) :
+	m_port(port)
+{
+	m_address[0] = a;
+	m_address[1] = b;
+	m_address[2] = c;
+	m_address[3] = d;
+}
+
+//-----------------------------------------------------------------------------
+uint32_t NetAddress::address() const
+{
+	uint32_t addr = (m_address[0] << 24) | (m_address[1] << 16) | (m_address[2] << 8) | (m_address[3]);
+
+	return addr;
+}
+
+//-----------------------------------------------------------------------------
+uint16_t NetAddress::port() const
+{
+	return m_port;
+}
+
+//-----------------------------------------------------------------------------
+void NetAddress::set(uint32_t address, uint16_t port)
+{
+	m_address[0] = address >> 24;
+	m_address[1] = address >> 16;
+	m_address[2] = address >> 8;
+	m_address[3] = address;
+
+	m_port = port;
+}
+
+//-----------------------------------------------------------------------------
+void NetAddress::set(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port)
+{
+	m_address[0] = a;
+	m_address[1] = b;
+	m_address[2] = c;
+	m_address[3] = d;
+	
+	m_port = port;
+}
+
+//-----------------------------------------------------------------------------
+bool NetAddress::operator==(const NetAddress& addr) const
+{
+	return m_address[0] == addr.m_address[0] &&
+		   m_address[1] == addr.m_address[1] &&
+		   m_address[2] == addr.m_address[2] &&
+		   m_address[3] == addr.m_address[3] &&
+		   m_port == addr.m_port;
+}
+
+//-----------------------------------------------------------------------------
+NetAddress& NetAddress::operator=(const NetAddress& addr)
+{
+	m_address[0] = addr.m_address[0];
+	m_address[1] = addr.m_address[1];
+	m_address[2] = addr.m_address[2];
+	m_address[3] = addr.m_address[3];
+	
+	m_port = addr.m_port;
+	
+	return *this;
+}
+
+//-----------------------------------------------------------------------------
+void NetAddress::print() const
+{
+	os::printf("NetAddress: %i.%i.%i.%i:%i\n", m_address[0], m_address[1], m_address[2], m_address[3], m_port);
+}
+
+} // namespace os
+} // namespace crown

+ 0 - 185
src/os/OS.h

@@ -30,10 +30,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include <cstdarg>
 #include "Types.h"
 
-#ifdef LINUX
-	#include <pthread.h>
-#endif
-
 namespace crown
 {
 
@@ -50,23 +46,6 @@ const size_t	MAX_EVENTS = 512;
 
 const size_t	MAX_THREADS = 16;
 const size_t	MAX_MUTEXES = 16;
-
-struct OSThread
-{
-	pthread_t	thread;
-	const char*	name;
-};
-
-struct OSMutex
-{
-	pthread_mutex_t mutex;
-};
-
-struct OSCond
-{
-	pthread_cond_t cond;
-};
-
 #endif
 
 #ifdef WINDOWS
@@ -191,170 +170,6 @@ void*			open_library(const char* path);
 void			close_library(void* library);
 void*			lookup_symbol(void* library, const char* name);
 
-//-----------------------------------------------------------------------------
-// Threads
-//-----------------------------------------------------------------------------
-typedef			void* (*ThreadFunction)(void*);
-
-void			thread_create(ThreadFunction f, void* params, OSThread* thread, const char* name);
-void			thread_join(OSThread* thread);
-void			thread_detach(OSThread* thread);
-
-void			mutex_create(OSMutex* mutex);
-void			mutex_destroy(OSMutex* mutex);
-void			mutex_lock(OSMutex* mutex);
-void			mutex_unlock(OSMutex* mutex);
-void			cond_create(OSCond* cond);
-void			cond_destroy(OSCond* cond);
-void			cond_signal(OSCond* cond);
-void			cond_wait(OSCond* cond, OSMutex* mutex);
-
-//-----------------------------------------------------------------------------
-// Networking
-//-----------------------------------------------------------------------------
-struct NetAddress
-{
-	uint8_t 	address[4];
-	uint16_t 	port;
-
-	NetAddress()
-	{
-		
-	}
-	
-	NetAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t p)
-	{
-		address[0] = a;
-		address[1] = b;
-		address[2] = c;
-		address[3] = d;
-		
-		port = p;
-	}
-	
-	uint32_t get_address()
-	{
-		uint32_t addr = (address[0] << 24) | (address[1] << 16) | (address[2] << 8) | (address[3]);
-		
-		return addr;
-	}
-	
-	uint16_t get_port() const
-	{
-		return port;
-	}
-	
-	void set(uint32_t a, uint16_t p)
-	{
-		address[0] = a >> 24;
-		address[1] = a >> 16;
-		address[2] = a >> 8;
-		address[3] = a;
-
-		port = p;
-	}
-	
-	void set(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t p)
-	{
-		address[0] = a;
-		address[1] = b;
-		address[2] = c;
-		address[3] = d;
-		
-		port = p;
-	}
-	
-	bool operator==(NetAddress& addr)
-	{
-		return address[0] == addr.address[0] &&
-			   address[1] == addr.address[1] &&
-			   address[2] == addr.address[2] &&
-			   address[3] == addr.address[3] &&
-			   port == addr.port;
-	}
-	
-	NetAddress& operator=(const NetAddress& addr)
-	{
-		address[0] = addr.address[0];
-		address[1] = addr.address[1];
-		address[2] = addr.address[2];
-		address[3] = addr.address[3];
-		
-		port = addr.port;
-		
-		return *this;
-	}
-	
-	void print()
-	{
-		printf("NetAddress: %i.%i.%i.%i:%i\n", address[0], address[1], address[2], address[3], port);
-	}
-};
-
-//-----------------------------------------------------------------------------
-class UDPSocket
-{
-public:
-
-				// Constructor
-				UDPSocket();
-				// Destructor
-				~UDPSocket();
-				// Open connection
-	bool 		open(uint16_t port);
-				 // Send data through socket
-	bool 		send(NetAddress &receiver, const void* data, size_t size );
-				// Receive data through socket
-	int32_t 	receive(NetAddress &sender, void* data, size_t size);
-				// Close connection
-	void 		close();
-				// Is connection open?
-	bool 		is_open();
-
-private:
-				// Socket descriptor
-	int32_t 	m_socket;
-};
-
-//-----------------------------------------------------------------------------
-class TCPSocket
-{
-public:
-
-				// Constructor
-				TCPSocket();
-				// Destructor
-				~TCPSocket();
-				// Open connection (server side)
-	bool 		open(uint16_t port);
-				// Connect (client side)
-	bool		connect(NetAddress& destination);
-				// Close connection
-	int32_t		close();
-				// Send data through socket
-	bool 		send(const void* data, size_t size);
-				// Receive data through socket
-	int32_t		receive(void* data, size_t size);
-				// Is connection open?
-	bool 		is_open();
-				// Getter method for socket descriptor
-	int32_t 	get_socket_id();
-				// Getter method for active socket descriptor
-	int32_t 	get_active_socket_id();
-				// Setter method for socket descriptor
-
-private:
-
-	void 		set_socket_id(int32_t socket);
-				// Setter method for ative socket descriptor
-	void 		set_active_socket_id(int32_t socket);
-	
-				// Generated by ::socket
-	int32_t 	m_socket;
-			  	// Generated by ::accept
-	int32_t 	m_active_socket;
-}; 
-
 } // namespace os
 } // namespace crown
 

+ 2 - 5
src/os/android/AndroidDevice.cpp

@@ -1,7 +1,6 @@
 #include <jni.h>
 #include "Device.h"
 
-
 namespace crown
 {
 
@@ -16,15 +15,13 @@ extern "C"
 //------------------------------------------------------------------------------------
 JNIEXPORT void JNICALL Java_crown_android_CrownLib_init(JNIEnv* env, jobject obj)
 {
-	Device* device = GetDevice();
-	device->Init(0, NULL);
+	device()->init(0, NULL);
 }
 
 //------------------------------------------------------------------------------------
 JNIEXPORT void JNICALL Java_crown_android_CrownLib_shutdown(JNIEnv* env, jobject obj)
 {
-	Device* device = GetDevice();
-	device->Shutdown();
+	device()->shutdown();
 }
 
 } // namespace crown

+ 0 - 67
src/os/android/AndroidOS.cpp

@@ -266,73 +266,6 @@ void swap_buffers()
 	// not necessary
 }
 
-//-----------------------------------------------------------------------------
-void thread_create(ThreadFunction f, void* params, OSThread& thread, const char* name)
-{
-	OSThread tid;
-	tid.name = name;
-
-	// Make thread joinable
-	pthread_attr_t attr;
-	pthread_attr_init(&attr);
-	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
-
-	// Create thread
-	int rc = pthread_create(&tid.thread, &attr, f, (void*)params);
-
-	if (rc != 0)
-	{
-		os::printf("OS: ERROR: Unable to create the thread '%s' Error code: %d\n", name, rc);
-		exit(-1);
-	}
-
-	// Free memory
-	pthread_attr_destroy(&attr);
-
-	thread = tid;
-}
-
-//-----------------------------------------------------------------------------
-void thread_join(OSThread thread)
-{
-	pthread_join(thread.thread, NULL);
-}
-
-//-----------------------------------------------------------------------------
-void thread_detach(OSThread thread)
-{
-	pthread_detach(thread.thread);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_create(OSMutex& mutex)
-{
-	OSMutex mut;
-
-	pthread_mutex_init(&mut.mutex, NULL);
-
-	mutex = mut;
-}
-
-//-----------------------------------------------------------------------------
-void mutex_destroy(OSMutex mutex)
-{
-	pthread_mutex_destroy(&mutex.mutex);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_lock(OSMutex mutex)
-{
-	pthread_mutex_lock(&mutex.mutex);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_unlock(OSMutex mutex)
-{
-	pthread_mutex_unlock(&mutex.mutex);
-}
-
-
 //-----------------------------------------------------------------------------
 JNIEXPORT void JNICALL Java_crown_android_CrownLib_initAssetManager(JNIEnv* env, jobject obj, jobject assetManager)
 {

+ 0 - 87
src/os/android/AndroidTimer.cpp

@@ -1,87 +0,0 @@
-/*
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "Types.h"
-#include "Timer.h"
-#include <sys/time.h>
-
-namespace crown
-{
-
-Timer::Timer()
-{
-	Reset();
-}
-
-Timer::~Timer()
-{
-}
-
-void Timer::Reset()
-{
-	clock_gettime(CLOCK_MONOTONIC, &mCreationTime);
-}
-
-uint64_t Timer::GetMilliseconds() const
-{
-	timespec tmp;
-	clock_gettime(CLOCK_MONOTONIC, &tmp);
-	return (tmp.tv_sec - mCreationTime.tv_sec) * 1000 + (tmp.tv_nsec - mCreationTime.tv_nsec) / 1000000;
-}
-
-uint64_t Timer::GetMicroseconds() const
-{
-	timespec tmp;
-	clock_gettime(CLOCK_MONOTONIC, &tmp);
-	return (tmp.tv_sec - mCreationTime.tv_sec) * 1000000 + (tmp.tv_nsec - mCreationTime.tv_nsec) / 1000;
-}
-
-
-void Timer::StartMilliseconds()
-{
-	clock_gettime(CLOCK_MONOTONIC, &mStartTime);
-}
-
-uint64_t Timer::StopMilliseconds() const
-{
-	timespec tmp;
-	clock_gettime(CLOCK_MONOTONIC, &tmp);
-	return (tmp.tv_sec - mStartTime.tv_sec) * 1000 + (tmp.tv_nsec - mStartTime.tv_nsec) / 1000000;
-}
-
-void Timer::StartMicroseconds()
-{
-	clock_gettime(CLOCK_MONOTONIC, &mStartTime);
-}
-
-uint64_t Timer::StopMicroseconds() const
-{
-	timespec tmp;
-	clock_gettime(CLOCK_MONOTONIC, &tmp);
-	return (tmp.tv_sec - mStartTime.tv_sec) * 1000000 + (tmp.tv_nsec - mStartTime.tv_nsec) / 1000;
-}
-
-} // namespace crown
-

+ 0 - 73
src/os/android/AndroidTimer.h

@@ -1,73 +0,0 @@
-/*
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include <sys/time.h>
-#include "Types.h"
-
-namespace crown
-{
-
-class Timer
-{
-
-public:
-
-	//! Constructor
-	Timer();
-
-	//! Destructor
-	~Timer();
-
-	//! Returns the time (in milliseconds) elapsed since the instantiation of this class
-	uint64_t GetMilliseconds() const;
-
-	//! Returns the time (in microseconds) elapsed since the instantiation of this class
-	uint64_t GetMicroseconds() const;
-
-	//! Records the current time
-	void StartMilliseconds();
-
-	//! Returns the time (in milliseconds) elapsed since the last call to StartMilliseconds()
-	uint64_t StopMilliseconds() const;
-
-	//! Records the current time
-	void StartMicroseconds();
-
-	//! Returns the time (in microseconds) elapsed since the last call to StartMicroseconds()
-	uint64_t StopMicroseconds() const;
-
-private:
-
-	// Records the initial reference time
-	void Reset();
-
-	timespec mCreationTime;		// Time at instantiation
-	timespec mStartTime;		// Time at Start* call
-};
-
-} // namespace crown
-

+ 61 - 28
src/os/android/File.cpp

@@ -23,105 +23,138 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
+#include <cassert>
+#include <stdio.h>
+
+#include "OS.h"
 #include "File.h"
-#include "Log.h"
-#include "MathUtils.h"
 #include "AndroidOS.h"
-#include <cassert>
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
 File::File() :
-	m_asset(NULL), m_mode(FOM_READ)
+	m_asset(NULL),
+	m_mode(SOM_READ)
 {
 }
 
 //-----------------------------------------------------------------------------
 File::~File()
+{
+	close();
+}
+
+//-----------------------------------------------------------------------------
+void File::close()
 {
 	if (m_asset != NULL)
 	{
 		AAsset_close(m_asset);
+		m_asset = NULL;
 	}
 }
 
 //-----------------------------------------------------------------------------
-bool File::is_valid()
+bool File::is_open() const
 {
 	return m_asset != NULL;
 }
 
 //-----------------------------------------------------------------------------
-FileOpenMode File::mode()
+StreamOpenMode File::mode()
 {
 	return m_mode;
 }
 
 //-----------------------------------------------------------------------------
-File* File::open(const char* path, FileOpenMode mode)
+size_t File::size() const
+{
+	assert(m_asset != NULL);
+	
+	return AAsset_getLength(m_asset);
+}
+
+//-----------------------------------------------------------------------------
+bool File::open(const char* path, StreamOpenMode mode)
 {
-	File* f = new File();
+	assert(!is_open());
 
-	f->m_asset = AAssetManager_open(os::get_android_asset_manager(), path, AASSET_MODE_RANDOM);
+	// Android assets are always read-only
+	(void) mode;
+	m_mode = SOM_READ;
 
-	if (f->m_asset == NULL)
+	m_asset = AAssetManager_open(os::get_android_asset_manager(), path, AASSET_MODE_RANDOM);
+
+	if (m_asset == NULL)
 	{
-		Log::E("File::Open: Could not open file %s", path);
-		return NULL;
+		os::printf("Could not open asset %s", path);
+
+		return false;
 	}
 
-	f->m_mode = FOM_READ;
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+size_t File::read(void* data, size_t size)
+{
+	assert(m_asset != NULL);
+	assert(data != NULL);
 
-	return f;
+	return (size_t)AAsset_read(m_asset, data, size);
 }
 
 //-----------------------------------------------------------------------------
-size_t File::read(void* ptr, size_t size, size_t nmemb)
+size_t File::write(const void* data, size_t size)
 {
 	assert(m_asset != NULL);
-	assert(ptr != NULL);
+	assert(data != NULL);
 
-	return (size_t)AAsset_read(m_asset, ptr, size * nmemb);
+	os::printf("Android asset directory is read-only!");
+
+	return 0;
 }
 
 //-----------------------------------------------------------------------------
-size_t File::write(const void* ptr, size_t size, size_t nmemb)
+void File::seek(size_t position)
 {
-	Log::W("Cannot write to Android asset directory!!!");
+	assert(m_asset != NULL);
+
+	assert(AAsset_seek(m_asset, (off_t)position, SEEK_SET) != (off_t) -1);
 }
 
 //-----------------------------------------------------------------------------
-int File::seek(int32_t offset, int whence)
+void File::seek_to_end()
 {
 	assert(m_asset != NULL);
 
-	return AAsset_seek(m_asset, (off_t)offset, whence);
+	assert(AAsset_seek(m_asset, 0, SEEK_END) != (off_t) -1);
 }
 
 //-----------------------------------------------------------------------------
-int32_t File::tell()
+void File::skip(size_t bytes)
 {
 	assert(m_asset != NULL);
-	
-	return (int32_t)(AAsset_getLength(m_asset) - AAsset_getRemainingLength(m_asset));
+
+	assert(AAsset_seek(m_asset, (off_t) bytes, SEEK_CUR) != (off_t) -1);
 }
 
 //-----------------------------------------------------------------------------
-int File::eof()
+size_t File::position() const
 {
 	assert(m_asset != NULL);
 	
-	return (int)(AAsset_getRemainingLength(m_asset) == 0);
+	return (size_t) (AAsset_getLength(m_asset) - AAsset_getRemainingLength(m_asset));
 }
 
 //-----------------------------------------------------------------------------
-size_t File::size()
+bool File::eof() const
 {
 	assert(m_asset != NULL);
 	
-	return AAsset_getLength(m_asset);
+	return AAsset_getRemainingLength(m_asset) == 0;
 }
 
 } // namespace crown

+ 41 - 29
src/os/android/File.h

@@ -25,54 +25,66 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include "Types.h"
 #include <sys/types.h>
 #include <android/asset_manager.h>
 
-namespace crown
-{
+#include "Types.h"
+#include "Stream.h"
 
-/**
-	Enumerates file opening modes.
-*/
-enum FileOpenMode
+namespace crown
 {
-	FOM_READ	= 1,
-	FOM_WRITE	= 2
-};
 
-/**
-	Standard C file wrapper.
-*/
+/// Android assets wrapper
 class File
 {
-
 public:
 
-						~File();
+							File();
+							~File();
 
-	bool				is_valid();
+	/// Opens the file located at @path with the given @mode.
+	bool					open(const char* path, StreamOpenMode mode);
 
-	FileOpenMode		mode();
+	/// Closes the file.
+	void					close();
 
-	size_t				read(void* ptr, size_t size, size_t nmemb);
-	size_t				write(const void* ptr, size_t size, size_t nmemb);
-	int					seek(int32_t offset, int whence);
-	int32_t				tell();
+	bool					is_open() const;
 
-	int					eof();
+	/// Return the size of the file in bytes.
+	size_t					size() const;
 
-	size_t				size();
+	/// Returs the mode used to open the file.
+	StreamOpenMode			mode();
 
-	static File*		open(const char* path, FileOpenMode mode);
+	/// Reads @size bytes from the file and stores it into @data.
+	/// Returns the number of bytes read.
+	size_t					read(void* data, size_t size);
 
-private:
+	/// Writes @size bytes of data stored in @data and returns the
+	/// number of bytes written.
+	size_t					write(const void* data, size_t size);
+
+	/// Moves the file pointer to the given @position.
+	void					seek(size_t position);
+
+	/// Moves the file pointer to the end of the file.
+	void					seek_to_end();
+
+	/// Moves the file pointer @bytes bytes ahead the current
+	/// file pointer position.
+	void					skip(size_t bytes);
 
-	AAsset*				m_asset;
-	FileOpenMode		m_mode;
+	/// Returns the position of the file pointer from the
+	/// start of the file in bytes.
+	size_t					position() const;
 
-						File();
+	/// Returns whether the file pointer is at the end of the file.
+	bool					eof() const;
+
+private:
+
+	AAsset*					m_asset;
+	StreamOpenMode			m_mode;
 };
 
 } // namespace crown
-

+ 12 - 4
src/os/linux/CMakeLists.txt

@@ -6,13 +6,21 @@ set (LINUX_SRC
 	GLXRenderWindow.cpp
 	LinuxOS.cpp
 	Input.cpp
-	LinuxTCPSocket.cpp
-	LinuxUDPSocket.cpp	
-	File.cpp
+	../posix/TCPSocket.cpp
+	../posix/UDPSocket.cpp	
+	../posix/File.cpp
+	../posix/Thread.cpp
+	../posix/Mutex.cpp
+	../posix/Cond.cpp
 )
 
 set (LINUX_HEADERS
-	File.h
+	../posix/TCPSocket.h
+	../posix/UDPSocket.h
+	../posix/File.h
+	../posix/Thread.h
+	../posix/Mutex.h
+	../posix/Cond.h
 )
 
 link_libraries(X11 Xrandr pthread)

+ 0 - 83
src/os/linux/LinuxOS.cpp

@@ -301,88 +301,5 @@ void* lookup_symbol(void* library, const char* name)
 	return symbol;
 }
 
-//-----------------------------------------------------------------------------
-void thread_create(ThreadFunction f, void* params, OSThread* thread, const char* name)
-{
-	thread->name = name;
-
-	// Make thread joinable
-	pthread_attr_t attr;
-	pthread_attr_init(&attr);
-	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
-
-	// Create thread
-	int rc = pthread_create(&(thread->thread), &attr, f, (void*)params);
-
-	if (rc != 0)
-	{
-		os::printf("OS: ERROR: Unable to create the thread '%s' Error code: %d\n", name, rc);
-		exit(-1);
-	}
-
-	// Free memory
-	pthread_attr_destroy(&attr);
-}
-
-//-----------------------------------------------------------------------------
-void thread_join(OSThread* thread)
-{
-	pthread_join(thread->thread, NULL);
-}
-
-//-----------------------------------------------------------------------------
-void thread_detach(OSThread* thread)
-{
-	pthread_detach(thread->thread);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_create(OSMutex* mutex)
-{
-	pthread_mutex_init(&mutex->mutex, NULL);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_destroy(OSMutex* mutex)
-{
-	pthread_mutex_destroy(&mutex->mutex);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_lock(OSMutex* mutex)
-{
-	pthread_mutex_lock(&mutex->mutex);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_unlock(OSMutex* mutex)
-{
-	pthread_mutex_unlock(&mutex->mutex);
-}
-
-//-----------------------------------------------------------------------------
-void cond_create(OSCond* cond)
-{
-	pthread_cond_init(&cond->cond, NULL);
-}
-
-//-----------------------------------------------------------------------------
-void cond_destroy(OSCond* cond)
-{
-	pthread_cond_destroy(&cond->cond);
-}
-
-//-----------------------------------------------------------------------------
-void cond_signal(OSCond* cond)
-{
-	pthread_cond_signal(&cond->cond);
-}
-
-//-----------------------------------------------------------------------------
-void cond_wait(OSCond* cond, OSMutex* mutex)
-{
-	pthread_cond_wait(&cond->cond, &mutex->mutex);
-}
-
 } // namespace os
 } // namespace crown

+ 0 - 182
src/os/linux/LinuxTCPSocket.cpp

@@ -1,182 +0,0 @@
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <cassert>
-
-#include "Types.h"
-#include "OS.h"
-
-namespace crown
-{
-namespace os
-{
-
-TCPSocket::TCPSocket()
-{
-	set_socket_id(0);
-	set_active_socket_id(0);
-}
-
-TCPSocket::~TCPSocket()
-{
-	close();
-}
-
-bool TCPSocket::open(uint16_t port)
-{
-	int32_t sd = socket(AF_INET, SOCK_STREAM, 0);
-
-	if (sd <= 0)
-	{
-		os::printf("failed to open socket\n");
-		set_socket_id(0);
-		return false;
-	}
-
-	set_socket_id(sd);
-
-	// Bind socket
-	sockaddr_in address;
-	address.sin_family = AF_INET;
-	address.sin_addr.s_addr = htonl(INADDR_ANY);
-	address.sin_port = htons(port);
-
-	if (bind(sd, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
-	{
-		os::printf("failed to bind socket\n");
-		close();
-		return false;
-	}
-
-	listen(sd, 5);
-	os::printf("listening on port %d\n", port);
-
-	sockaddr_in client;
-	uint32_t client_length = sizeof(client);
-
-	int32_t asd = accept(sd, (sockaddr*)&client, &client_length);
-	if (asd < 0)
-	{
-		os::printf("failed to accept connection\n");
-	}
-
-	set_active_socket_id(asd);
-
-	return true;  
-}
-
-bool TCPSocket::connect(NetAddress& destination)
-{
-	int32_t sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
-
-	if (sd <= 0)
-	{
-		os::printf("failed to open socket\n");
-		set_socket_id(0);
-		return false;
-	}
-
-	set_socket_id(sd);
-
-	sockaddr_in address;
-	address.sin_family = AF_INET;
-	address.sin_addr.s_addr =  htonl(destination.get_address());
-	address.sin_port = htons(destination.get_port());
-
-	if (::connect(sd, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
-	{
-		os::printf("failed to connect socket\n");
-		close();
-		return false;
-	}  
-}
-
-int32_t	TCPSocket::close()
-{
-	int32_t asd = get_active_socket_id();
-	if (asd != 0)
-	{
-		::close(asd);
-		set_active_socket_id(0);  
-	}
-	int32_t sd = get_socket_id();
-	if (sd != 0)
-	{
-		::close(sd);
-		set_socket_id(0);
-	}
-}
-
-bool TCPSocket::send(const void* data, size_t size)
-{
-	assert(data);
-	assert(size > 0);
-
-	int32_t sd = get_active_socket_id();
-	if (sd <= 0)
-	{
-		set_socket_id(0);
-		set_active_socket_id(0);
-		return false;
-	}
-
-	int32_t sent_bytes = ::send(sd, (const char*)data, size, 0);
-	if (sent_bytes <= 0)
-	{
-		os::printf("Unable to send data");
-		return false;
-	}
-
-	return true;  
-}
-
-int32_t TCPSocket::receive(void* data, size_t size)
-{
-	assert(data);
-	assert(size > 0);
-
-	int32_t sd = get_active_socket_id();
-
-	if ( sd <= 0 )
-	{
-		return false;
-	}
-
-	int32_t received_bytes = ::recv(sd, (char*)data, size, 0);
-	if ( received_bytes <= 0 )
-	{
-		return 0;
-	}
-
-	return received_bytes;
-}
-
-bool TCPSocket::is_open()
-{
-	return m_active_socket != 0 || m_socket != 0;
-}
-
-int32_t	TCPSocket::get_socket_id()
-{
-	return m_socket;
-}
-
-int32_t	TCPSocket::get_active_socket_id()
-{
-	return m_active_socket;
-}
-
-void TCPSocket::set_socket_id(int32_t socket)
-{
-	m_socket = socket;
-}
-
-void TCPSocket::set_active_socket_id(int32_t socket)
-{
-	m_active_socket = socket;
-}
-		
-}
-}

+ 0 - 127
src/os/linux/LinuxUDPSocket.cpp

@@ -1,127 +0,0 @@
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <cassert>
-
-#include "Types.h"
-#include "OS.h"
-
-namespace crown
-{ 
-namespace os
-{
-
-UDPSocket::UDPSocket()
-{
-	m_socket = 0;  
-}
-UDPSocket::~UDPSocket()
-{
-	close();
-}
-
-bool UDPSocket::open(uint16_t port)
-{
-  		assert(!is_open());
-
-		m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
-		if (m_socket <= 0)
-		{
-			os::printf("Failed to create socket.\n");;
-			m_socket = 0;
-			return false;
-		}
-
-		// bind to port
-		sockaddr_in address;
-		address.sin_family = AF_INET;
-		address.sin_addr.s_addr = INADDR_ANY;
-		address.sin_port = htons(port);
-
-		if ( bind( m_socket, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
-		{
-			os::printf("Failed to bind socket\n");
-			close();
-			return false;
-		}
-
-		int32_t non_blocking = 1;
-		if (fcntl( m_socket, F_SETFL, O_NONBLOCK, non_blocking ) == -1)
-		{
-			os::printf("Failed to set non-blocking socket\n");
-			close();
-			return false;
-		}
-		
-		return true;
-}
-
-bool UDPSocket::send(NetAddress &receiver, const void* data, size_t size)
-{
-	assert(data);
-	assert(size > 0);
-
-	if (m_socket == 0)
-	{
-		return false;
-	}
-	
-	assert(receiver.address);
-	assert(receiver.port);
-
-	sockaddr_in address;
-	address.sin_family = AF_INET;
-	address.sin_addr.s_addr = htonl(receiver.get_address());
-	address.sin_port = htons(receiver.get_port());
-
-	int32_t sent_bytes = sendto(m_socket, (const char*)data, size, 0, (sockaddr*)&address, sizeof(sockaddr_in));
-
-	return sent_bytes == size;
-}
-
-int32_t UDPSocket::receive(NetAddress& sender, void* data, size_t size)
-{
-	assert(data);
-	assert(size > 0);
-
-	if (m_socket == 0)
-	{
-		return false;
-	}
-
-	sockaddr_in from;
-	socklen_t from_length = sizeof(from);
-
-	int32_t received_bytes = recvfrom(m_socket, (char*)data, size, 0, (sockaddr*)&from, &from_length);
-
-	if (received_bytes <= 0)
-	{
-			return 0;
-	}
-
-	uint32_t address = ntohl(from.sin_addr.s_addr);
-	uint16_t port = ntohs(from.sin_port);
-	
-	sender.set(address, port);
-	
-	return received_bytes;	
-}
-
-void UDPSocket::close()
-{
-	if ( m_socket != 0 )
-	{
-		::close(m_socket);
-		m_socket = 0; 
-	}
-}
-
-bool UDPSocket::is_open()
-{
-	return m_socket != 0;
-}
-
-} // namespace os
-} // namespace crown

+ 12 - 31
src/core/threads/Mutex.h → src/os/posix/Cond.cpp

@@ -23,57 +23,38 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#pragma once
-
-#include "Types.h"
-#include "OS.h"
+#include "Cond.h"
 
 namespace crown
 {
-
-class Mutex
+namespace os
 {
-public:
-
-					Mutex();
-					~Mutex();
-
-	void			lock();
-	void			unlock();
-
-private:
-
-	os::OSMutex		m_mutex;
-
-private:
-
-	friend class	Cond;
-};
 
 //-----------------------------------------------------------------------------
-inline Mutex::Mutex()
+Cond::Cond()
 {
-	memset(&m_mutex, 0, sizeof(os::OSMutex));
+	memset(&m_cond, 0, sizeof(pthread_cond_t));
 
-	os::mutex_create(&m_mutex);
+	pthread_cond_init(&m_cond, NULL);
 }
 
 //-----------------------------------------------------------------------------
-inline Mutex::~Mutex()
+Cond::~Cond()
 {
-	os::mutex_destroy(&m_mutex);
+	pthread_cond_destroy(&m_cond);
 }
 
 //-----------------------------------------------------------------------------
-inline void Mutex::lock()
+void Cond::signal()
 {
-	os::mutex_lock(&m_mutex);
+	pthread_cond_signal(&m_cond);
 }
 
 //-----------------------------------------------------------------------------
-inline void Mutex::unlock()
+void Cond::wait(Mutex& mutex)
 {
-	os::mutex_unlock(&m_mutex);
+	pthread_cond_wait(&m_cond, &(mutex.m_mutex));
 }
 
+} // namespace os
 } // namespace crown

+ 55 - 0
src/os/posix/Cond.h

@@ -0,0 +1,55 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include <pthread.h>
+
+#include "Types.h"
+#include "Mutex.h"
+#include "OS.h"
+
+namespace crown
+{
+namespace os
+{
+
+class Cond
+{
+public:
+
+					Cond();
+					~Cond();
+
+	void			signal();
+	void			wait(Mutex& mutex);
+
+private:
+
+	pthread_cond_t	m_cond;
+};
+
+} // namespace os
+} // namespace crown

+ 63 - 44
src/os/linux/File.cpp → src/os/posix/File.cpp

@@ -23,124 +23,143 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "File.h"
-#include "Log.h"
-#include "MathUtils.h"
 #include <cassert>
+#include <stdio.h>
+
+#include "OS.h"
+#include "File.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
 File::File() :
-	m_file_handle(NULL), m_mode(FOM_READ)
+	m_file_handle(NULL),
+	m_mode(SOM_READ)
 {
 }
 
 //-----------------------------------------------------------------------------
 File::~File()
+{
+	close();
+}
+
+//-----------------------------------------------------------------------------
+void File::close()
 {
 	if (m_file_handle != NULL)
 	{
 		fclose(m_file_handle);
+		m_file_handle = NULL;
 	}
 }
 
 //-----------------------------------------------------------------------------
-bool File::is_valid()
+bool File::is_open() const
 {
-	return m_file_handle != 0;
+	return m_file_handle != NULL;
 }
 
 //-----------------------------------------------------------------------------
-FileOpenMode File::mode()
+StreamOpenMode File::mode()
 {
 	return m_mode;
 }
 
 //-----------------------------------------------------------------------------
-FILE* File::get_handle()
+size_t File::size() const
 {
-	return m_file_handle;
+	long pos = ftell(m_file_handle);
+
+	fseek(m_file_handle, 0, SEEK_END);
+
+	long size = ftell(m_file_handle);
+
+	fseek(m_file_handle, pos, SEEK_SET);
+
+	return (size_t) size;
 }
 
 //-----------------------------------------------------------------------------
-File* File::open(const char* path, FileOpenMode mode)
+bool File::open(const char* path, StreamOpenMode mode)
 {
-	File* f = new File();
-	f->m_file_handle = fopen(path, 
+	assert(!is_open());
 
-	/*
-		TestFlag(mode, FOM_READ) ?
-			(TestFlag(mode, FOM_WRITE) ?
-				(TestFlag(mode, FOM_CREATENEW) ? "wb+" : "rb+") : "rb") : (TestFlag(mode, FOM_WRITE) ? "wb" : "rb"));
-	*/
+	const char* c_mode = mode == SOM_READ ? "rb" : SOM_WRITE ? "wb" : "x";
 
-	math::test_bitmask(mode, FOM_READ) ?
-		(math::test_bitmask(mode, FOM_WRITE) ? "rb+" : "rb") : (math::test_bitmask(mode, FOM_WRITE) ? "wb" : "rb")); 
+	assert(c_mode[0] != 'x');
 
-	if (f->m_file_handle == NULL)
+	m_file_handle = fopen(path, c_mode);
+
+	if (m_file_handle == NULL)
 	{
-		Log::e("File::Open: Could not open file %s", path);
-		return NULL;
+		os::printf("Could not open file %s", path);
+
+		return false;
 	}
 
-	f->m_mode = mode;
+	m_mode = mode;
 
-	return f;
+	return true;
 }
 
 //-----------------------------------------------------------------------------
-size_t File::read(void* ptr, size_t size, size_t nmemb)
+size_t File::read(void* data, size_t size)
 {
 	assert(m_file_handle != NULL);
-	assert(ptr != NULL);
+	assert(data != NULL);
 
-	return fread(ptr, size, nmemb, m_file_handle);
+	return fread(data, 1, size, m_file_handle);
 }
 
 //-----------------------------------------------------------------------------
-size_t File::write(const void* ptr, size_t size, size_t nmemb)
+size_t File::write(const void* data, size_t size)
 {
 	assert(m_file_handle != NULL);
-	assert(ptr != NULL);
+	assert(data != NULL);
 
-	return fwrite(ptr, size, nmemb, m_file_handle);
+	return fwrite(data, 1, size, m_file_handle);
 }
 
 //-----------------------------------------------------------------------------
-int File::seek(int32_t offset, int whence)
+void File::seek(size_t position)
 {
 	assert(m_file_handle != NULL);
 
-	return fseek(m_file_handle, offset, whence);
+	assert(fseek(m_file_handle, (long) position, SEEK_SET) == 0);
 }
 
 //-----------------------------------------------------------------------------
-int32_t File::tell()
+void File::seek_to_end()
 {
 	assert(m_file_handle != NULL);
-	
-	return ftell(m_file_handle);
+
+	assert(fseek(m_file_handle, 0, SEEK_END) == 0);
 }
 
 //-----------------------------------------------------------------------------
-int File::eof()
+void File::skip(size_t bytes)
 {
 	assert(m_file_handle != NULL);
-	
-	return feof(m_file_handle);
+
+	assert(fseek(m_file_handle, bytes, SEEK_CUR) == 0);
 }
 
 //-----------------------------------------------------------------------------
-size_t File::size()
+size_t File::position() const
 {
-	size_t pos = ftell(m_file_handle);
-	fseek(m_file_handle, 0, SEEK_END);
-	size_t size = ftell(m_file_handle);
-	fseek(m_file_handle, pos, SEEK_SET);
+	assert(m_file_handle != NULL);
+
+	return (size_t) ftell(m_file_handle);
+}
+
+//-----------------------------------------------------------------------------
+bool File::eof() const
+{
+	assert(m_file_handle != NULL);
 
-	return size;
+	return feof(m_file_handle) != 0;
 }
 
 } // namespace crown

+ 90 - 0
src/os/posix/File.h

@@ -0,0 +1,90 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include <cstdio>
+
+#include "Types.h"
+#include "Stream.h"
+
+namespace crown
+{
+
+/// Standard C file wrapper
+class File
+{
+public:
+
+							File();
+							~File();
+
+	/// Opens the file located at @path with the given @mode.
+	bool					open(const char* path, StreamOpenMode mode);
+
+	/// Closes the file.
+	void					close();
+
+	bool					is_open() const;
+
+	/// Return the size of the file in bytes.
+	size_t					size() const;
+
+	/// Returs the mode used to open the file.
+	StreamOpenMode			mode();
+
+	/// Reads @size bytes from the file and stores it into @data.
+	/// Returns the number of bytes read.
+	size_t					read(void* data, size_t size);
+
+	/// Writes @size bytes of data stored in @data and returns the
+	/// number of bytes written.
+	size_t					write(const void* data, size_t size);
+
+	/// Moves the file pointer to the given @position.
+	void					seek(size_t position);
+
+	/// Moves the file pointer to the end of the file.
+	void					seek_to_end();
+
+	/// Moves the file pointer @bytes bytes ahead the current
+	/// file pointer position.
+	void					skip(size_t bytes);
+
+	/// Returns the position of the file pointer from the
+	/// start of the file in bytes.
+	size_t					position() const;
+
+	/// Returns whether the file pointer is at the end of the file.
+	bool					eof() const;
+
+private:
+
+	FILE*					m_file_handle;
+	StreamOpenMode			m_mode;
+};
+
+} // namespace crown
+

+ 12 - 27
src/core/threads/Cond.h → src/os/posix/Mutex.cpp

@@ -23,54 +23,39 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#pragma once
-
-#include "Types.h"
 #include "Mutex.h"
-#include "OS.h"
 
 namespace crown
 {
-
-class Cond
+namespace os
 {
-public:
-
-					Cond();
-					~Cond();
-
-	void			signal();
-	void			wait(Mutex& mutex);
-
-private:
-
-	os::OSCond		m_cond;
-};
 
 //-----------------------------------------------------------------------------
-inline Cond::Cond()
+Mutex::Mutex()
 {
-	memset(&m_cond, 0, sizeof(os::OSCond));
+	memset(&m_mutex, 0, sizeof(pthread_mutex_t));
 
-	os::cond_create(&m_cond);
+	pthread_mutex_init(&m_mutex, NULL);
 }
 
 //-----------------------------------------------------------------------------
-inline Cond::~Cond()
+Mutex::~Mutex()
 {
-	os::cond_destroy(&m_cond);
+	pthread_mutex_destroy(&m_mutex);
 }
 
 //-----------------------------------------------------------------------------
-inline void Cond::signal()
+void Mutex::lock()
 {
-	os::cond_signal(&m_cond);
+	pthread_mutex_lock(&m_mutex);
 }
 
 //-----------------------------------------------------------------------------
-inline void Cond::wait(Mutex& mutex)
+void Mutex::unlock()
 {
-	os::cond_wait(&m_cond, &mutex.m_mutex);
+	pthread_mutex_unlock(&m_mutex);
 }
 
+} // namespace os
 } // namespace crown
+

+ 56 - 0
src/os/posix/Mutex.h

@@ -0,0 +1,56 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include <pthread.h>
+
+#include "Types.h"
+#include "OS.h"
+
+namespace crown
+{
+namespace os
+{
+
+class Mutex
+{
+public:
+
+						Mutex();
+						~Mutex();
+
+	void				lock();
+	void				unlock();
+
+private:
+
+	pthread_mutex_t		m_mutex;
+
+	friend class		Cond;
+};
+
+} // namespace os
+} // namespace crown

+ 216 - 0
src/os/posix/TCPSocket.cpp

@@ -0,0 +1,216 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <cassert>
+
+#include "Types.h"
+#include "OS.h"
+#include "TCPSocket.h"
+#include "NetAddress.h"
+
+namespace crown
+{
+namespace os
+{
+
+//-----------------------------------------------------------------------------
+TCPSocket::TCPSocket() :
+	m_socket(0),
+	m_active_socket(0)
+
+{
+}
+
+//-----------------------------------------------------------------------------
+TCPSocket::~TCPSocket()
+{
+	close();
+}
+
+//-----------------------------------------------------------------------------
+bool TCPSocket::open(uint16_t port)
+{
+	int sd = socket(AF_INET, SOCK_STREAM, 0);
+
+	if (sd <= 0)
+	{
+		os::printf("failed to open socket\n");
+		m_socket = 0;
+
+		return false;
+	}
+
+	m_socket = sd;
+
+	// Bind socket
+	sockaddr_in address;
+	address.sin_family = AF_INET;
+	address.sin_addr.s_addr = htonl(INADDR_ANY);
+	address.sin_port = htons(port);
+
+	if (bind(sd, (const sockaddr*) &address, sizeof(sockaddr_in)) < 0)
+	{
+		os::printf("Failed to bind socket\n");
+		close();
+
+		return false;
+	}
+
+	listen(sd, 5);
+	os::printf("Listening on port %d\n", port);
+
+	sockaddr_in client;
+	size_t client_length = sizeof(client);
+
+	int asd = accept(sd, (sockaddr*)&client, (socklen_t*)&client_length);
+
+	if (asd < 0)
+	{
+		os::printf("failed to accept connection\n");
+
+		return false;
+	}
+
+	m_active_socket = asd;
+
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+bool TCPSocket::connect(const NetAddress& destination)
+{
+	int sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+
+	if (sd <= 0)
+	{
+		os::printf("Failed to open socket\n");
+		m_socket = 0;
+
+		return false;
+	}
+
+	m_socket = sd;
+
+	sockaddr_in address;
+	address.sin_family = AF_INET;
+	address.sin_addr.s_addr =  htonl(destination.address());
+	address.sin_port = htons(destination.port());
+
+	if (::connect(sd, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
+	{
+		os::printf("Failed to connect socket\n");
+		close();
+
+		return false;
+	}
+
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+void TCPSocket::close()
+{
+	if (m_active_socket != 0)
+	{
+		::close(m_active_socket);
+		m_active_socket = 0;  
+	}
+
+	if (m_socket != 0)
+	{
+		::close(m_socket);
+		m_socket = 0;
+	}
+}
+
+//-----------------------------------------------------------------------------
+bool TCPSocket::send(const void* data, size_t size)
+{
+	assert(data != NULL);
+	assert(size > 0);
+
+	if (m_active_socket <= 0)
+	{
+		m_socket = 0;
+		m_active_socket = 0;
+
+		return false;
+	}
+
+	ssize_t sent_bytes = ::send(m_active_socket, (const char*) data, size, 0);
+	if (sent_bytes <= 0)
+	{
+		os::printf("Unable to send data");
+		return false;
+	}
+
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+size_t TCPSocket::receive(void* data, size_t size)
+{
+	assert(data);
+	assert(size > 0);
+
+	if (m_active_socket <= 0)
+	{
+		return false;
+	}
+
+	ssize_t received_bytes = ::recv(m_active_socket, (char*) data, size, 0);
+	if (received_bytes <= 0)
+	{
+		return 0;
+	}
+
+	return received_bytes;
+}
+
+//-----------------------------------------------------------------------------
+bool TCPSocket::is_open()
+{
+	return m_active_socket != 0 || m_socket != 0;
+}
+
+//-----------------------------------------------------------------------------
+int	TCPSocket::socket_id()
+{
+	return m_socket;
+}
+
+//-----------------------------------------------------------------------------
+int	TCPSocket::active_socket_id()
+{
+	return m_active_socket;
+}
+		
+} // namespace os
+} // namespace crown

+ 79 - 0
src/os/posix/TCPSocket.h

@@ -0,0 +1,79 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Types.h"
+
+namespace crown
+{
+namespace os
+{
+
+class NetAddress;
+
+/// OS level TCP socket.
+class TCPSocket
+{
+public:
+
+				TCPSocket();
+				~TCPSocket();
+
+	// Open connection (server side)
+	bool 		open(uint16_t port);
+
+	// Connect (client side)
+	bool		connect(const NetAddress& destination);
+
+	// Close connection
+	void		close();
+
+	// Send data through socket
+	bool 		send(const void* data, size_t size);
+
+	// Receive data through socket
+	size_t		receive(void* data, size_t size);
+
+	// Is connection open?
+	bool 		is_open();
+
+	// Getter method for socket descriptor
+	int 		socket_id();
+
+	// Getter method for active socket descriptor
+	int 		active_socket_id();
+
+private:
+	
+	// Generated by ::socket
+	int 		m_socket;
+
+	// Generated by ::accept
+	int 		m_active_socket;
+};
+
+} // namespace os
+} // namespace crown

+ 27 - 25
src/core/threads/Thread.h → src/os/posix/Thread.cpp

@@ -23,52 +23,54 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#pragma once
-
-#include "Types.h"
-#include "OS.h"
+#include "Thread.h"
+#include <stdlib.h>
 
 namespace crown
 {
-
-class Thread
+namespace os
 {
-public:
 
-					Thread(os::ThreadFunction f, void* args, const char* name);
-					~Thread();
-
-	void			join();
-	void			detach();
+//-----------------------------------------------------------------------------
+Thread::Thread(os::ThreadFunction f, void* params, const char* name) :
+	m_name(name)
+{
+	memset(&m_thread, 0, sizeof(pthread_t));
 
-private:
+	// Make thread joinable
+	pthread_attr_t attr;
+	pthread_attr_init(&attr);
+	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
-	os::OSThread	m_thread;
-};
+	// Create thread
+	int rc = pthread_create(&m_thread, &attr, f, (void*)params);
 
-//-----------------------------------------------------------------------------
-inline Thread::Thread(os::ThreadFunction f, void* args, const char* name)
-{
-	memset(&m_thread, 0, sizeof(os::OSThread));
+	if (rc != 0)
+	{
+		os::printf("Unable to create the thread '%s' Error code: %d\n", name, rc);
+		exit(-1);
+	}
 
-	os::thread_create(f, args, &m_thread, name);
+	// Free attr memory
+	pthread_attr_destroy(&attr);
 }
 
 //-----------------------------------------------------------------------------
-inline Thread::~Thread()
+Thread::~Thread()
 {
 }
 
 //-----------------------------------------------------------------------------
-inline void Thread::join()
+void Thread::join()
 {
-	os::thread_join(&m_thread);
+	pthread_join(m_thread, NULL);
 }
 
 //-----------------------------------------------------------------------------
-inline void Thread::detach()
+void Thread::detach()
 {
-	os::thread_detach(&m_thread);
+	pthread_detach(m_thread);
 }
 
+} // namespace os
 } // namespace crown

+ 57 - 0
src/os/posix/Thread.h

@@ -0,0 +1,57 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include <pthread.h>
+
+#include "Types.h"
+#include "OS.h"
+
+namespace crown
+{
+namespace os
+{
+
+typedef void* (*ThreadFunction)(void*);
+
+class Thread
+{
+public:
+
+					Thread(os::ThreadFunction f, void* params, const char* name);
+					~Thread();
+
+	void			join();
+	void			detach();
+
+private:
+
+	pthread_t		m_thread;
+	const char*		m_name;
+};
+
+} // namespace os
+} // namespace crown

+ 161 - 0
src/os/posix/UDPSocket.cpp

@@ -0,0 +1,161 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <cassert>
+
+#include "Types.h"
+#include "OS.h"
+#include "UDPSocket.h"
+#include "NetAddress.h"
+
+namespace crown
+{ 
+namespace os
+{
+
+//-----------------------------------------------------------------------------
+UDPSocket::UDPSocket() :
+	m_socket(0)
+{
+}
+
+//-----------------------------------------------------------------------------
+UDPSocket::~UDPSocket()
+{
+	close();
+}
+
+//-----------------------------------------------------------------------------
+bool UDPSocket::open(uint16_t port)
+{
+	assert(!is_open());
+
+	m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+
+	if (m_socket <= 0)
+	{
+		os::printf("Failed to create socket.\n");
+		m_socket = 0;
+
+		return false;
+	}
+
+	// Bind to port
+	sockaddr_in address;
+	address.sin_family = AF_INET;
+	address.sin_addr.s_addr = INADDR_ANY;
+	address.sin_port = htons(port);
+
+	if (bind(m_socket, (const sockaddr*) &address, sizeof(sockaddr_in)) < 0)
+	{
+		os::printf("Failed to bind socket\n");
+		close();
+
+		return false;
+	}
+
+	int non_blocking = 1;
+
+	if (fcntl(m_socket, F_SETFL, O_NONBLOCK, non_blocking) == -1)
+	{
+		os::printf("Failed to set non-blocking socket\n");
+		close();
+
+		return false;
+	}
+	
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+bool UDPSocket::send(const NetAddress &receiver, const void* data, size_t size)
+{
+	assert(is_open());
+	assert(data != NULL);
+	assert(size > 0);
+
+	sockaddr_in address;
+	address.sin_family = AF_INET;
+	address.sin_addr.s_addr = htonl(receiver.address());
+	address.sin_port = htons(receiver.port());
+
+	ssize_t sent_bytes = sendto(m_socket, (const char*) data, size, 0, (sockaddr*) &address, sizeof(sockaddr_in));
+
+	if (sent_bytes < 0)
+	{
+		return false;
+	}
+
+	return (size_t) sent_bytes == size;
+}
+
+//-----------------------------------------------------------------------------
+size_t UDPSocket::receive(NetAddress& sender, void* data, size_t size)
+{
+	assert(is_open());
+	assert(data);
+	assert(size > 0);
+
+	sockaddr_in from;
+	socklen_t from_length = sizeof(from);
+
+	ssize_t received_bytes = recvfrom(m_socket, (char*)data, size, 0, (sockaddr*)&from, &from_length);
+
+	if (received_bytes <= 0)
+	{
+		return 0;
+	}
+
+	uint32_t address = ntohl(from.sin_addr.s_addr);
+	uint16_t port = ntohs(from.sin_port);
+
+	sender.set(address, port);
+
+	return (size_t) received_bytes;
+}
+
+//-----------------------------------------------------------------------------
+void UDPSocket::close()
+{
+	if (m_socket != 0)
+	{
+		::close(m_socket);
+		m_socket = 0;
+	}
+}
+
+//-----------------------------------------------------------------------------
+bool UDPSocket::is_open()
+{
+	return m_socket != 0;
+}
+
+} // namespace os
+} // namespace crown

+ 21 - 33
src/os/linux/File.h → src/os/posix/UDPSocket.h

@@ -25,55 +25,43 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include <cstdio>
 #include "Types.h"
 
 namespace crown
 {
-
-/**
-	Enumerates file opening modes.
-*/
-enum FileOpenMode
+namespace os
 {
-	FOM_READ	= 1,
-	FOM_WRITE	= 2
-};
 
-/**
-	Standard C file wrapper.
-*/
-class File
-{
+class NetAddress;
 
+/// OS level UDP socket.
+class UDPSocket
+{
 public:
 
-						~File();
+				UDPSocket();
+				~UDPSocket();
 
-	bool				is_valid();
+	// Open connection
+	bool 		open(uint16_t port);
 
-	FileOpenMode		mode();
+	// Send data through socket
+	bool 		send(const NetAddress& receiver, const void* data, size_t size);
 
-	FILE*				get_handle();
+	// Receive data through socket
+	size_t	 	receive(NetAddress& sender, void* data, size_t size);
 
-	size_t				read(void* ptr, size_t size, size_t nmemb);
-	size_t				write(const void* ptr, size_t size, size_t nmemb);
-	int					seek(int32_t offset, int whence);
-	int32_t				tell();
+	// Close connection
+	void 		close();
 
-	int					eof();
-
-	size_t				size();
-
-	static File*		open(const char* path, FileOpenMode mode);
+	// Is connection open?
+	bool 		is_open();
 
 private:
-
-	FILE*				m_file_handle;
-	FileOpenMode		m_mode;
-
-						File();
+	
+	// Socket descriptor
+	int 	m_socket;
 };
 
+} // namespace os
 } // namespace crown
-

+ 0 - 72
src/os/win/WinInputManager.cpp

@@ -1,72 +0,0 @@
-/*
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "WinInputManager.h"
-#include "WinMouse.h"
-#include "WinKeyboard.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-WinInputManager::WinInputManager() :
-	mIsMouseAvailable(false),
-	mIsKeyboardAvailable(false),
-	mWindowHandle(0)
-{
-}
-
-//-----------------------------------------------------------------------------
-WinInputManager::~WinInputManager()
-{
-	if (mMouse)
-	{
-		delete mMouse;
-	}
-
-	if (mKeyboard)
-	{
-		delete mKeyboard;
-	}
-}
-
-//-----------------------------------------------------------------------------
-void WinInputManager::Init(const EventSource& source)
-{
-	mWindowHandle = (HWND)source.WindowHandle;
-
-	if (!mMouse)
-	{
-		mMouse = new WinMouse(this);
-	}
-
-	if (!mKeyboard)
-	{
-		mKeyboard = new WinKeyboard(this);
-	}
-}
-
-} // namespace crown
-

+ 0 - 92
src/os/win/WinInputManager.h

@@ -1,92 +0,0 @@
-/*
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "InputManager.h"
-#include <windows.h>
-
-namespace crown
-{
-
-class WinInputManager : public InputManager
-{
-
-	friend class WinMouse;
-	friend class WinKeyboard;
-
-public:
-
-	/** @copydoc InputManager::InputManager() */
-	WinInputManager();
-
-	/** @copydoc InputManager::~InputManager() */
-	virtual ~WinInputManager();
-
-	/** @copydoc InputManager::Init() */
-	virtual void Init(const EventSource& source);
-
-	/** @copydoc InputManager::IsMouseAvailable() */
-	virtual bool IsMouseAvailable()
-	{
-		return mIsMouseAvailable;
-	}
-
-	/** @copydoc InputManager::IsKeyboardAvailable() */
-	virtual bool IsKeyboardAvailable()
-	{
-		return mIsKeyboardAvailable;
-	}
-
-	/** @copydoc InputManager::IsTouchAvailable() */
-	virtual bool IsTouchAvailable()
-	{
-		return false;
-	}
-
-	/**
-		Returns the Window Handle "attached" to this manager.
-	@return
-		The Window Handle
-	*/
-	inline unsigned int64_t GetWindowHandle()
-	{
-		return (unsigned int64_t)mWindowHandle;
-	}
-
-private:
-
-	void SetMouseAvailable(bool available) { mIsMouseAvailable = available; }
-	void SetKeyboardAvailable(bool available) { mIsKeyboardAvailable = available; }
-
-	bool mIsMouseAvailable		: 1;
-	bool mIsKeyboardAvailable	: 1;
-
-	// Win related
-	HWND mWindowHandle;
-};
-
-} // namespace crown
-

+ 2 - 2
tools/CMakeLists.txt

@@ -10,7 +10,7 @@ link_directories(${CROWN_BINARY_DIR} ${GTKMM_LIBRARY_DIRS})
 include_directories(${INCLUDES} ${GTKMM_INCLUDE_DIRS})
 
 add_subdirectory(compilers)
-add_subdirectory(editors/world-editor)
-add_subdirectory(editors/resource-browser)
+#add_subdirectory(editors/world-editor)
+#add_subdirectory(editors/resource-browser)
 add_subdirectory(pycrown)