about summary refs log tree commit diff
path: root/src/engine/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/client')
-rw-r--r--src/engine/client/client.cpp712
-rw-r--r--src/engine/client/gfx.cpp583
-rw-r--r--src/engine/client/snd.cpp520
-rw-r--r--src/engine/client/ui.cpp115
-rw-r--r--src/engine/client/ui.h33
5 files changed, 1963 insertions, 0 deletions
diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp
new file mode 100644
index 00000000..4aceaf0f
--- /dev/null
+++ b/src/engine/client/client.cpp
@@ -0,0 +1,712 @@
+#include <baselib/system.h>
+#include <baselib/keys.h>
+#include <baselib/mouse.h>
+#include <baselib/audio.h>
+#include <baselib/stream/file.h>
+
+#include <string.h>
+#include <stdarg.h>
+#include <math.h>
+#include <engine/interface.h>
+
+#include <engine/packet.h>
+#include <engine/snapshot.h>
+#include "ui.h"
+
+#include <engine/lzw.h>
+
+#include <engine/versions.h>
+
+using namespace baselib;
+
+// --- string handling (MOVE THESE!!) ---
+void snap_encode_string(const char *src, int *dst, int length, int max_length)
+{
+	const unsigned char *p = (const unsigned char *)src;
+	
+	// handle whole int
+	for(int i = 0; i < length/4; i++)
+	{
+		*dst = (p[0]<<24|p[1]<<16|p[2]<<8|p[3]);
+		p += 4;
+		dst++;
+	}
+	
+	// take care of the left overs
+	int left = length%4;
+	if(left)
+	{
+		unsigned last = 0;
+		switch(left)
+		{
+			case 3: last |= p[2]<<8;
+			case 2: last |= p[1]<<16;
+			case 1: last |= p[0]<<24;
+		}
+		*dst = last;
+	}
+}
+
+void snap_decode_string(const int *src, char *dst, int max_length)
+{
+	dbg_assert((max_length%4) == 0, "length must be power of 4");
+	for(int i = 0; i < max_length; i++)
+		dst[0] = 0;
+	
+	for(int i = 0; i < max_length/4; i++)
+	{
+		dst[0] = (*src>>24)&0xff;
+		dst[1] = (*src>>16)&0xff;
+		dst[2] = (*src>>8)&0xff;
+		dst[3] = (*src)&0xff;
+		src++;
+		dst+=4;
+	}
+	dst[-1] = 0; // make sure to zero terminate
+}
+
+// --- input wrappers ---
+static int keyboard_state[2][keys::last];
+static int keyboard_current = 0;
+static int keyboard_first = 1;
+
+void inp_mouse_relative(int *x, int *y) { mouse::position(x, y); }
+int inp_key_pressed(int key) { return keyboard_state[keyboard_current][key]; }
+int inp_key_was_pressed(int key) { return keyboard_state[keyboard_current^1][key]; }
+int inp_key_down(int key) { return inp_key_pressed(key)&&!inp_key_was_pressed(key); }
+int inp_mouse_button_pressed(int button) { return mouse::pressed(button); }
+
+void inp_update()
+{
+	if(keyboard_first)
+	{
+		// make sure to reset
+		keyboard_first = 0;
+		inp_update();
+	}
+	
+	keyboard_current = keyboard_current^1;
+	for(int i = 0; i < keys::last; i++)
+		keyboard_state[keyboard_current][i] = keys::pressed(i);
+}
+
+// --- input snapping ---
+static int input_data[MAX_INPUT_SIZE];
+static int input_data_size;
+static int input_is_changed = 1;
+void snap_input(void *data, int size)
+{
+	if(input_data_size != size || memcmp(input_data, data, size))
+		input_is_changed = 1;
+	mem_copy(input_data, data, size);
+	input_data_size = size;
+}
+
+// -- snapshot handling ---
+enum
+{
+	SNAP_INCOMMING=2,
+	NUM_SNAPSHOT_TYPES=3,
+};
+
+static snapshot *snapshots[NUM_SNAPSHOT_TYPES];
+static char snapshot_data[NUM_SNAPSHOT_TYPES][MAX_SNAPSHOT_SIZE];
+static int recived_snapshots;
+static int64 snapshot_start_time;
+static int64 local_start_time;
+
+float client_localtime()
+{
+	return (time_get()-local_start_time)/(float)(time_freq());
+}
+
+void *snap_get_item(int snapid, int index, snap_item *item)
+{
+	dbg_assert(snapid >= 0 && snapid < NUM_SNAPSHOT_TYPES, "invalid snapid");
+	snapshot::item *i = snapshots[snapid]->get_item(index);
+	item->type = i->type();
+	item->id = i->id();
+	return (void *)i->data;
+}
+
+int snap_num_items(int snapid)
+{
+	dbg_assert(snapid >= 0 && snapid < NUM_SNAPSHOT_TYPES, "invalid snapid");
+	return snapshots[snapid]->num_items;
+}
+
+static void snap_init()
+{
+	snapshots[SNAP_INCOMMING] = (snapshot*)snapshot_data[0];
+	snapshots[SNAP_CURRENT] = (snapshot*)snapshot_data[1];
+	snapshots[SNAP_PREV] = (snapshot*)snapshot_data[2];
+	mem_zero(snapshot_data, NUM_SNAPSHOT_TYPES*MAX_SNAPSHOT_SIZE);
+	recived_snapshots = 0;
+}
+
+float snap_intratick()
+{
+	return (time_get() - snapshot_start_time)/(float)(time_freq()/SERVER_TICK_SPEED);
+}
+
+void *snap_find_item(int snapid, int type, int id)
+{
+	// TODO: linear search. should be fixed.
+	for(int i = 0; i < snapshots[snapid]->num_items; i++)
+	{
+		snapshot::item *itm = snapshots[snapid]->get_item(i);
+		if(itm->type() == type && itm->id() == id)
+			return (void *)itm->data;
+	}
+	return 0x0;
+}
+
+
+int menu_loop();
+float frametime = 0.0001f;
+
+float client_frametime()
+{
+	return frametime;
+}
+
+void unpack(const char *src, const char *fmt, ...)
+{
+}
+
+/*int modc_onmsg(int msg)
+{
+	msg_get("iis")
+}*/
+
+/*
+	i = int (int i)
+	s = string (const char *str)
+	r = raw data (int size, void *data)
+*/
+
+/*
+class packet2
+{
+private:
+	// packet data
+	struct header
+	{
+		unsigned msg;
+		unsigned ack;
+		unsigned seq;
+	};
+	
+	unsigned char packet_data[MAX_PACKET_SIZE];
+	unsigned char *current;
+	
+	enum
+	{
+		MAX_PACKET_SIZE = 1024,
+	};
+	
+public:
+	packet2()
+	{
+		current = packet_data;
+		current += sizeof(header);
+	}
+
+	int pack(char *dst, const char *fmt, ...)
+	{
+		va_list arg_list;
+		va_start(arg_list, fmt);
+		while(*fmt)
+		{
+			if(*fmt == 's')
+			{
+				// pack string
+				const char *s = va_arg(arg_list, const char*);
+				*dst++ = 2;
+				while(*s)
+				{
+					*dst = *s;
+					dst++;
+					s++;
+				}
+				*dst = 0; // null terminate
+				dst++;
+				fmt++;
+			}
+			else if(*fmt == 'i')
+			{
+				// pack int
+				int i = va_arg(arg_list, int);
+				*dst++ = 1;
+				*dst++ = (i>>24)&0xff;
+				*dst++ = (i>>16)&0xff;
+				*dst++ = (i>>8)&0xff;
+				*dst++ = i&0xff;
+				fmt++;
+			}
+			else
+			{
+				dbg_break(); // error
+				break;
+			}
+		}
+		va_end(arg_list);	
+	}	
+};
+*/
+/*
+int msg_get(const char *fmt)
+{
+	
+}
+
+int client_msg_send(int msg, const char *fmt, ...)
+
+int server_msg_send(int msg, const char *fmt, ...)
+{
+
+}*/
+
+// --- client ---
+class client
+{
+public:
+	socket_udp4 socket;
+	connection conn;
+	int64 reconnect_timer;
+	
+	int snapshot_part;
+
+	int debug_font; // TODO: rfemove this line
+
+	// data to hold three snapshots
+	// previous, 
+	char name[MAX_NAME_LENGTH];
+
+	bool fullscreen;
+
+	enum
+	{
+		STATE_OFFLINE,
+		STATE_CONNECTING,
+		STATE_LOADING,
+		STATE_ONLINE,
+		STATE_BROKEN,
+		STATE_QUIT,
+	};
+	
+	int state;
+	int get_state() { return state; }
+	void set_state(int s)
+	{
+		dbg_msg("game", "state change. last=%d current=%d", state, s);
+		state = s;
+	}
+	
+	void set_name(const char *new_name)
+	{
+		mem_zero(name, MAX_NAME_LENGTH);
+		strncpy(name, new_name, MAX_NAME_LENGTH);
+		name[MAX_NAME_LENGTH-1] = 0;
+	}
+
+	void set_fullscreen(bool flag) { fullscreen = flag; }
+	
+	void send_packet(packet *p)
+	{
+		conn.send(p);
+	}
+	
+	void send_connect()
+	{
+		recived_snapshots = 0;
+		
+		/*
+		pack(NETMSG_CLIENT_CONNECT, "sssss",
+			TEEWARS_NETVERSION,
+			name,
+			"no clan",
+			"password",
+			"myskin");
+		*/
+		
+		packet p(NETMSG_CLIENT_CONNECT);
+		p.write_str(TEEWARS_NETVERSION); // payload
+		p.write_str(name);
+		p.write_str("no clan");
+		p.write_str("password");
+		p.write_str("myskin");
+		send_packet(&p);
+	}
+
+	void send_done()
+	{
+		packet p(NETMSG_CLIENT_DONE);
+		send_packet(&p);
+	}
+
+	void send_error(const char *error)
+	{
+		/*
+			pack(NETMSG_CLIENT_ERROR, "s", error);
+		*/
+		packet p(NETMSG_CLIENT_ERROR);
+		p.write_str(error);
+		send_packet(&p);
+		//send_packet(&p);
+		//send_packet(&p);
+	}	
+
+	void send_input()
+	{
+		/*
+			pack(NETMSG_CLIENT_ERROR, "s", error);
+		*/
+		packet p(NETMSG_CLIENT_INPUT);
+		p.write_int(input_data_size);
+		for(int i = 0; i < input_data_size/4; i++)
+			p.write_int(input_data[i]);
+		send_packet(&p);
+	}
+	
+	void disconnect()
+	{
+		send_error("disconnected");
+		set_state(STATE_OFFLINE);
+		map_unload();
+	}
+	
+	void connect(netaddr4 *server_address)
+	{
+		conn.init(&socket, server_address);
+		
+		// start by sending connect
+		send_connect();
+		set_state(STATE_CONNECTING);
+		reconnect_timer = time_get()+time_freq();
+	}
+	
+	bool load_data()
+	{
+		debug_font = gfx_load_texture_tga("data/debug_font.tga");
+		return true;
+	}
+	
+	void render()
+	{
+		gfx_clear(0.0f,0.0f,0.0f);
+		
+		// this should be moved around abit
+		if(get_state() == STATE_ONLINE)
+		{
+			modc_render();
+		}
+		else if (get_state() != STATE_CONNECTING && get_state() != STATE_LOADING)
+		{
+			netaddr4 server_address;
+			int status = modmenu_render(&server_address, name, MAX_NAME_LENGTH);
+
+			if (status == -1)
+				set_state(STATE_QUIT);
+			else if (status)
+				connect(&server_address);
+		}
+		else if (get_state() == STATE_CONNECTING)
+		{
+			static int64 start = time_get();
+			static int tee_texture;
+			static int connecting_texture;
+			static bool inited = false;
+			
+			if (!inited)
+			{
+				tee_texture = gfx_load_texture_tga("data/gui_tee.tga");
+				connecting_texture = gfx_load_texture_tga("data/gui/connecting.tga");
+					
+				inited = true;
+			}
+
+			gfx_mapscreen(0,0,400.0f,300.0f);
+
+			float t = (time_get() - start) / (double)time_freq();
+
+			float speed = 2*sin(t);
+
+			speed = 1.0f;
+
+			float x = 208 + sin(t*speed) * 32;
+			float w = sin(t*speed + 3.149) * 64;
+
+			ui_do_image(tee_texture, x, 95, w, 64);
+			ui_do_image(connecting_texture, 88, 150, 256, 64);
+		}
+	}
+	
+	void run(netaddr4 *server_address)
+	{
+		snapshot_part = 0;
+		
+		// init graphics and sound
+		if(!gfx_init(fullscreen))
+			return;
+
+		snd_init(); // sound is allowed to fail
+		
+		// load data
+		if(!load_data())
+			return;
+
+		// init snapshotting
+		snap_init();
+		
+		// init the mod
+		modc_init();
+
+		// init menu
+		modmenu_init();
+		
+		// open socket
+		if(!socket.open(0))
+		{
+			dbg_msg("network/client", "failed to open socket");
+			return;
+		}
+
+		// connect to the server if wanted
+		if (server_address)
+			connect(server_address);
+		
+		//int64 inputs_per_second = 50;
+		//int64 time_per_input = time_freq()/inputs_per_second;
+		int64 game_starttime = time_get();
+		int64 last_input = game_starttime;
+		
+		int64 reporttime = time_get();
+		int64 reportinterval = time_freq()*1;
+		int frames = 0;
+		
+		mouse::set_mode(mouse::mode_relative);
+		
+		while (1)
+		{	
+			frames++;
+			int64 frame_start_time = time_get();
+
+			// send input
+			if(get_state() == STATE_ONLINE)
+			{
+				if(input_is_changed || time_get() > last_input+time_freq())
+				{
+					send_input();
+					input_is_changed = 0;
+					last_input = time_get();
+				}
+			}
+			
+			// update input
+			inp_update();
+			
+			//
+			if(keys::pressed(keys::f1))
+				mouse::set_mode(mouse::mode_absolute);
+			if(keys::pressed(keys::f2))
+				mouse::set_mode(mouse::mode_relative);
+				
+			// pump the network
+			pump_network();
+			
+			// render
+			render();
+			
+			// swap the buffers
+			gfx_swap();
+			
+			// check conditions
+			if(get_state() == STATE_BROKEN || get_state() == STATE_QUIT)
+				break;
+
+			// be nice
+			//thread_sleep(1);
+			
+			if(reporttime < time_get())
+			{
+				unsigned sent, recved;
+				conn.counter_get(&sent, &recved);
+				dbg_msg("client/report", "fps=%.02f",
+					frames/(float)(reportinterval/time_freq()));
+				frames = 0;
+				reporttime += reportinterval;
+				conn.counter_reset();
+			}
+			
+			if (keys::pressed(keys::esc))
+				if (get_state() == STATE_CONNECTING || get_state() == STATE_ONLINE)
+					disconnect();
+
+			// update frametime
+			frametime = (time_get()-frame_start_time)/(float)time_freq();
+		}
+		
+		modc_shutdown();
+		disconnect();
+
+		modmenu_shutdown();
+		
+		gfx_shutdown();
+		snd_shutdown();
+	}
+	
+	void error(const char *msg)
+	{
+		dbg_msg("game", "error: %s", msg);
+		send_error(msg);
+		set_state(STATE_BROKEN);
+	}
+
+	void process_packet(packet *p)
+	{
+		if(p->msg() == NETMSG_SERVER_ACCEPT)
+		{
+			const char *map;
+			map = p->read_str();
+			
+			if(p->is_good())
+			{
+				dbg_msg("client/network", "connection accepted, map=%s", map);
+				set_state(STATE_LOADING);
+				
+				if(map_load(map))
+				{
+					modc_entergame();
+					send_done();
+					dbg_msg("client/network", "loading done");
+					// now we will wait for two snapshots
+					// to finish the connection
+				}
+				else
+				{
+					error("failure to load map");
+				}
+			}
+		}
+		else if(p->msg() == NETMSG_SERVER_SNAP)
+		{
+			//dbg_msg("client/network", "got snapshot");
+			int num_parts = p->read_int();
+			int part = p->read_int();
+			int part_size = p->read_int();
+			
+			if(p->is_good())
+			{
+				if(snapshot_part == part)
+				{
+					const char *d = p->read_raw(part_size);
+					mem_copy((char*)snapshots[SNAP_INCOMMING] + part*MAX_SNAPSHOT_PACKSIZE, d, part_size);
+					snapshot_part++;
+				
+					if(snapshot_part == num_parts)
+					{
+						snapshot *tmp = snapshots[SNAP_PREV];
+						snapshots[SNAP_PREV] = snapshots[SNAP_CURRENT];
+						snapshots[SNAP_CURRENT] = tmp;
+
+						// decompress snapshot
+						lzw_decompress(snapshots[SNAP_INCOMMING], snapshots[SNAP_CURRENT]);
+						
+						// apply snapshot, cycle pointers
+						recived_snapshots++;
+						snapshot_start_time = time_get();
+						
+						// we got two snapshots until we see us self as connected
+						if(recived_snapshots == 2)
+						{
+							local_start_time = time_get();
+							set_state(STATE_ONLINE);
+						}
+						
+						if(recived_snapshots > 2)
+							modc_newsnapshot();
+						
+						snapshot_part = 0;
+					}
+
+				}
+				else
+				{
+					dbg_msg("client", "snapshot reset!");
+					snapshot_part = 0;
+				}
+			}
+		}
+		else
+		{
+			dbg_msg("server/client", "unknown packet %x", p->msg());
+		}
+	}
+	
+	void pump_network()
+	{
+		while(1)
+		{
+			packet p;
+			netaddr4 from;
+			int bytes = socket.recv(&from, p.data(), p.max_size());
+			
+			if(bytes <= 0)
+				break;
+			
+			process_packet(&p);
+		}
+		
+		//
+		if(get_state() == STATE_CONNECTING && time_get() > reconnect_timer)
+		{
+			send_connect();
+			reconnect_timer = time_get() + time_freq();
+		}
+	}	
+};
+
+int main(int argc, char **argv)
+{
+	dbg_msg("client", "starting...");
+	netaddr4 server_address(127, 0, 0, 1, 8303);
+	const char *name = "nameless jerk";
+	bool connect_at_once = false;
+	bool fullscreen = true;
+
+	// init network, need to be done first so we can do lookups
+	net_init();
+
+	// parse arguments
+	for(int i = 1; i < argc; i++)
+	{
+		if(argv[i][0] == '-' && argv[i][1] == 'c' && argv[i][2] == 0 && argc - i > 1)
+		{
+			// -c SERVER
+			i++;
+			if(net_host_lookup(argv[i], 8303, &server_address) != 0)
+				dbg_msg("main", "could not find the address of %s, connecting to localhost", argv[i]);
+			else
+				connect_at_once = true;
+		}
+		else if(argv[i][0] == '-' && argv[i][1] == 'n' && argv[i][2] == 0 && argc - i > 1)
+		{
+			// -n NAME
+			i++;
+			name = argv[i];
+		}
+		else if(argv[i][0] == '-' && argv[i][1] == 'w' && argv[i][2] == 0)
+		{
+			// -w
+			fullscreen = false;
+		}
+	}
+	
+	// start the server
+	client c;
+	c.set_name(name);
+	c.set_fullscreen(fullscreen);
+	c.run(connect_at_once ? &server_address : 0x0);
+	return 0;
+}
diff --git a/src/engine/client/gfx.cpp b/src/engine/client/gfx.cpp
new file mode 100644
index 00000000..a824feac
--- /dev/null
+++ b/src/engine/client/gfx.cpp
@@ -0,0 +1,583 @@
+#include <baselib/opengl.h>
+#include <baselib/vmath.h>
+#include <baselib/stream/file.h>
+
+#include <engine/interface.h>
+
+using namespace baselib;
+
+static opengl::context context;
+
+struct custom_vertex
+{
+	vec3 pos;
+	vec2 tex;
+	vec4 color;
+};
+
+const int vertexBufferSize = 2048;
+//static custom_vertex vertices[4];
+static custom_vertex* g_pVertices = 0;
+static int g_iVertexStart = 0;
+static int g_iVertexEnd = 0;
+static vec4 g_QuadColor[4];
+static vec2 g_QuadTexture[4];
+
+static opengl::vertex_buffer vertex_buffer;
+//static int screen_width = 800;
+//static int screen_height = 600;
+static int screen_width = 1024;
+static int screen_height = 768;
+static float rotation = 0;
+static int quads_drawing = 0;
+
+
+struct texture_holder
+{
+	opengl::texture tex;
+	int flags;
+	int next;
+};
+
+static const int MAX_TEXTURES = 128;
+
+static texture_holder textures[MAX_TEXTURES];
+static int first_free_texture;
+
+static const char null_texture_data[] = {
+	0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff, 
+	0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff, 
+	0x00,0x00,0xff,0xff, 0x00,0x00,0xff,0xff, 0xff,0xff,0x00,0xff, 0xff,0xff,0x00,0xff, 
+	0x00,0x00,0xff,0xff, 0x00,0x00,0xff,0xff, 0xff,0xff,0x00,0xff, 0xff,0xff,0x00,0xff, 
+};
+
+static void draw_quad(bool _bflush = false)
+{
+	if (!_bflush && ((g_iVertexEnd + 4) < vertexBufferSize))
+	{
+		// Just add
+		g_iVertexEnd += 4;
+	}
+	else if (g_iVertexEnd)
+	{
+		if (!_bflush)
+			g_iVertexEnd += 4;
+		if(GLEW_VERSION_2_0)
+		{
+			// set the data
+			vertex_buffer.data(g_pVertices, vertexBufferSize * sizeof(custom_vertex), GL_DYNAMIC_DRAW);
+			opengl::stream_vertex(&vertex_buffer, 3, GL_FLOAT, sizeof(custom_vertex), 0);
+			opengl::stream_texcoord(&vertex_buffer, 0, 2, GL_FLOAT,
+					sizeof(custom_vertex),
+					sizeof(vec3));
+			opengl::stream_color(&vertex_buffer, 4, GL_FLOAT,
+					sizeof(custom_vertex),
+					sizeof(vec3)+sizeof(vec2));		
+			opengl::draw_arrays(GL_QUADS, 0, g_iVertexEnd);
+		}
+		else
+		{
+			glVertexPointer(3, GL_FLOAT,
+					sizeof(custom_vertex),
+					(char*)g_pVertices);
+			glTexCoordPointer(2, GL_FLOAT,
+					sizeof(custom_vertex),
+					(char*)g_pVertices + sizeof(vec3));
+			glColorPointer(4, GL_FLOAT,
+					sizeof(custom_vertex),
+					(char*)g_pVertices + sizeof(vec3) + sizeof(vec2));
+			glEnableClientState(GL_VERTEX_ARRAY);
+			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+			glEnableClientState(GL_COLOR_ARRAY);
+			glDrawArrays(GL_QUADS, 0, g_iVertexEnd);
+		}
+		// Reset pointer
+		g_iVertexEnd = 0;
+	}
+}
+	
+bool gfx_init(bool fullscreen)
+{
+	if(!context.create(screen_width, screen_height, 24, 8, 16, 0, fullscreen?opengl::context::FLAG_FULLSCREEN:0))
+	{
+		dbg_msg("game", "failed to create gl context");
+		return false;
+	}
+	// Init vertices
+	if (g_pVertices)
+		mem_free(g_pVertices);
+	g_pVertices = (custom_vertex*)mem_alloc(sizeof(custom_vertex) * vertexBufferSize, 1);
+	g_iVertexStart = 0;
+	g_iVertexEnd = 0;
+
+	context.set_title("---");
+
+	/*
+	dbg_msg("gfx", "OpenGL version %d.%d.%d", context.version_major(),
+											  context.version_minor(),
+											  context.version_rev());*/
+
+	gfx_mapscreen(0,0,screen_width, screen_height);
+	
+	// TODO: make wrappers for this
+	glEnable(GL_BLEND);
+	
+	// model
+	mat4 mat = mat4::identity;
+	opengl::matrix_modelview(&mat);
+	
+	// Set all z to -5.0f
+	for (int i = 0; i < vertexBufferSize; i++)
+		g_pVertices[i].pos.z = -5.0f;
+
+	if(GLEW_VERSION_2_0)
+	{
+		// set the streams
+		vertex_buffer.data(g_pVertices, sizeof(vertexBufferSize), GL_DYNAMIC_DRAW);
+		opengl::stream_vertex(&vertex_buffer, 3, GL_FLOAT, sizeof(custom_vertex), 0);
+		opengl::stream_texcoord(&vertex_buffer, 0, 2, GL_FLOAT,
+				sizeof(custom_vertex),
+				sizeof(vec3));
+		opengl::stream_color(&vertex_buffer, 4, GL_FLOAT,
+				sizeof(custom_vertex),
+				sizeof(vec3)+sizeof(vec2));		
+	}
+
+	// init textures
+	first_free_texture = 0;
+	for(int i = 0; i < MAX_TEXTURES; i++)
+		textures[i].next = i+1;
+	textures[MAX_TEXTURES-1].next = -1;
+	
+	// create null texture, will get id=0
+	gfx_load_texture_raw(4,4,null_texture_data);
+	
+	return true;
+}
+
+int gfx_unload_texture(int index)
+{
+	textures[index].tex.clear();
+	textures[index].next = first_free_texture;
+	first_free_texture = index;
+	return 0;
+}
+
+void gfx_blend_normal()
+{
+	// TODO: wrapper for this
+	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+}
+
+void gfx_blend_additive()
+{
+	// TODO: wrapper for this
+	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+}
+
+int gfx_load_texture_raw(int w, int h, const void *data)
+{
+	// grab texture
+	int tex = first_free_texture;
+	first_free_texture = textures[tex].next;
+	textures[tex].next = -1;
+	
+	// set data and return
+	// TODO: should be RGBA, not BGRA
+	dbg_msg("gfx", "%d = %dx%d", tex, w, h);
+	textures[tex].tex.data2d(w, h, GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE, data);
+	return tex;
+}
+
+// simple uncompressed RGBA loaders
+int gfx_load_texture_tga(const char *filename)
+{
+	image_info img;
+	
+	if(gfx_load_tga(&img, filename))
+	{
+		int id = gfx_load_texture_raw(img.width, img.height, img.data);
+		mem_free(img.data);
+		return id;
+	}
+	
+	return 0;
+}
+
+int gfx_load_tga(image_info *img, const char *filename)
+{
+	// open file for reading
+	file_stream file;
+	if(!file.open_r(filename))
+	{
+		dbg_msg("game/tga", "failed to open file. filename='%s'", filename);
+		return 0;
+	}
+	
+	// read header
+	unsigned char headers[18];
+	file.read(headers, sizeof(headers));
+	img->width = headers[12]+(headers[13]<<8);
+	img->height = headers[14]+(headers[15]<<8);
+
+	bool flipx = (headers[17] >> 4) & 1;
+	bool flipy = !((headers[17] >> 5) & 1);
+	
+	(void)flipx; // TODO: make use of this flag
+
+	if(headers[2] != 2) // needs to be uncompressed RGB
+	{
+		dbg_msg("game/tga", "tga not uncompressed rgb. filename='%s'", filename);
+		return 0;
+	}
+		
+	if(headers[16] != 32) // needs to be RGBA
+	{
+		dbg_msg("game/tga", "tga is 32bit. filename='%s'", filename);
+		return 0;
+	}
+	
+	// read data
+	int data_size = img->width*img->height*4;
+	img->data = mem_alloc(data_size, 1);
+
+	if (flipy)
+	{
+		for (int i = 0; i < img->height; i++)
+		{
+			file.read((char *)img->data + (img->height-i-1)*img->width*4, img->width*4);
+		}
+	}
+	else
+		file.read(img->data, data_size);
+	file.close();
+
+	return 1;
+}
+
+void gfx_shutdown()
+{
+	if (g_pVertices)
+		mem_free(g_pVertices);
+	context.destroy();
+}
+
+void gfx_swap()
+{
+	context.swap();
+}
+
+int gfx_screenwidth()
+{
+	return screen_width;
+}
+
+int gfx_screenheight()
+{
+	return screen_height;
+}
+
+void gfx_texture_set(int slot)
+{
+	dbg_assert(quads_drawing == 0, "called gfx_texture_set within quads_begin");
+	if(slot == -1)
+		opengl::texture_disable(0);
+	else
+		opengl::texture_2d(0, &textures[slot].tex);
+}
+
+void gfx_clear(float r, float g, float b)
+{
+	glClearColor(r,g,b,1.0f);
+	opengl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+}
+
+void gfx_mapscreen(float tl_x, float tl_y, float br_x, float br_y)
+{
+	mat4 mat;
+	mat.ortho(tl_x, br_x, br_y, tl_y, 1.0f, 10.f);
+	opengl::matrix_projection(&mat);
+}
+
+void gfx_setoffset(float x, float y)
+{
+	//const float scale = 0.75f;
+	const float scale = 1.0f;
+	mat4 mat = mat4::identity;
+	mat.m[0] = scale;
+	mat.m[5] = scale;
+	mat.m[10] = scale;
+	mat.m[12] = x*scale;
+	mat.m[13] = y*scale;
+	opengl::matrix_modelview(&mat);
+}
+
+void gfx_quads_begin()
+{
+	dbg_assert(quads_drawing == 0, "called quads_begin twice");
+	quads_drawing++;
+	
+	gfx_quads_setsubset(0,0,1,1);
+	gfx_quads_setrotation(0);
+	gfx_quads_setcolor(1,1,1,1);
+}
+
+void gfx_quads_end()
+{
+	dbg_assert(quads_drawing == 1, "called quads_end without quads_begin");
+	draw_quad(true);
+	quads_drawing--;
+}
+
+
+void gfx_quads_setrotation(float angle)
+{
+	dbg_assert(quads_drawing == 1, "called gfx_quads_setrotation without quads_begin");
+	rotation = angle;
+}
+
+void gfx_quads_setcolorvertex(int i, float r, float g, float b, float a)
+{
+	dbg_assert(quads_drawing == 1, "called gfx_quads_setcolorvertex without quads_begin");
+	g_QuadColor[i].r = r;
+	g_QuadColor[i].g = g;
+	g_QuadColor[i].b = b;
+	g_QuadColor[i].a = a;
+}
+
+void gfx_quads_setcolor(float r, float g, float b, float a)
+{
+	dbg_assert(quads_drawing == 1, "called gfx_quads_setcolor without quads_begin");
+	g_QuadColor[0] = vec4(r,g,b,a);
+	g_QuadColor[1] = vec4(r,g,b,a);
+	g_QuadColor[2] = vec4(r,g,b,a);
+	g_QuadColor[3] = vec4(r,g,b,a);
+	/*gfx_quads_setcolorvertex(0,r,g,b,a);
+	gfx_quads_setcolorvertex(1,r,g,b,a);
+	gfx_quads_setcolorvertex(2,r,g,b,a);
+	gfx_quads_setcolorvertex(3,r,g,b,a);*/
+}
+
+void gfx_quads_setsubset(float tl_u, float tl_v, float br_u, float br_v)
+{
+	dbg_assert(quads_drawing == 1, "called gfx_quads_setsubset without quads_begin");
+
+	g_QuadTexture[0].x = tl_u;
+	g_QuadTexture[0].y = tl_v;
+	//g_pVertices[g_iVertexEnd].tex.u = tl_u;
+	//g_pVertices[g_iVertexEnd].tex.v = tl_v;
+
+	g_QuadTexture[1].x = br_u;
+	g_QuadTexture[1].y = tl_v;
+	//g_pVertices[g_iVertexEnd + 2].tex.u = br_u;
+	//g_pVertices[g_iVertexEnd + 2].tex.v = tl_v;
+
+	g_QuadTexture[2].x = br_u;
+	g_QuadTexture[2].y = br_v;
+	//g_pVertices[g_iVertexEnd + 1].tex.u = tl_u;
+	//g_pVertices[g_iVertexEnd + 1].tex.v = br_v;
+
+	g_QuadTexture[3].x = tl_u;
+	g_QuadTexture[3].y = br_v;
+	//g_pVertices[g_iVertexEnd + 3].tex.u = br_u;
+	//g_pVertices[g_iVertexEnd + 3].tex.v = br_v;
+}
+
+static void rotate(vec3 &center, vec3 &point)
+{
+	vec3 p = point-center;
+	point.x = p.x * cosf(rotation) - p.y * sinf(rotation) + center.x;
+	point.y = p.x * sinf(rotation) + p.y * cosf(rotation) + center.y;
+}
+
+void gfx_quads_draw(float x, float y, float w, float h)
+{
+	gfx_quads_drawTL(x-w/2, y-h/2,w,h);
+}
+
+void gfx_quads_drawTL(float x, float y, float width, float height)
+{
+	dbg_assert(quads_drawing == 1, "called quads_draw without quads_begin");
+	
+	vec3 center;
+	center.x = x + width/2;
+	center.y = y + height/2;
+	center.z = 0;
+	
+	g_pVertices[g_iVertexEnd].pos.x = x;
+	g_pVertices[g_iVertexEnd].pos.y = y;
+	g_pVertices[g_iVertexEnd].tex.u = g_QuadTexture[0].x;
+	g_pVertices[g_iVertexEnd].tex.v = g_QuadTexture[0].y;
+	g_pVertices[g_iVertexEnd].color = g_QuadColor[0];
+	rotate(center, g_pVertices[g_iVertexEnd].pos);
+
+	g_pVertices[g_iVertexEnd + 1].pos.x = x+width;
+	g_pVertices[g_iVertexEnd + 1].pos.y = y;
+	g_pVertices[g_iVertexEnd + 1].tex.u = g_QuadTexture[1].x;
+	g_pVertices[g_iVertexEnd + 1].tex.v = g_QuadTexture[1].y;
+	g_pVertices[g_iVertexEnd + 1].color = g_QuadColor[1];
+	rotate(center, g_pVertices[g_iVertexEnd + 1].pos);
+
+	g_pVertices[g_iVertexEnd + 2].pos.x = x + width;
+	g_pVertices[g_iVertexEnd + 2].pos.y = y+height;
+	g_pVertices[g_iVertexEnd + 2].tex.u = g_QuadTexture[2].x;
+	g_pVertices[g_iVertexEnd + 2].tex.v = g_QuadTexture[2].y;
+	g_pVertices[g_iVertexEnd + 2].color = g_QuadColor[2];
+	rotate(center, g_pVertices[g_iVertexEnd + 2].pos);
+
+	g_pVertices[g_iVertexEnd + 3].pos.x = x;
+	g_pVertices[g_iVertexEnd + 3].pos.y = y+height;
+	g_pVertices[g_iVertexEnd + 3].tex.u = g_QuadTexture[3].x;
+	g_pVertices[g_iVertexEnd + 3].tex.v = g_QuadTexture[3].y;
+	g_pVertices[g_iVertexEnd + 3].color = g_QuadColor[3];
+	rotate(center, g_pVertices[g_iVertexEnd + 3].pos);
+	
+	draw_quad();
+}
+
+void gfx_quads_draw_freeform(
+	float x0, float y0,
+	float x1, float y1,
+	float x2, float y2,
+	float x3, float y3)
+{
+	dbg_assert(quads_drawing == 1, "called quads_draw_freeform without quads_begin");
+	
+	g_pVertices[g_iVertexEnd].pos.x = x0;
+	g_pVertices[g_iVertexEnd].pos.y = y0;
+	g_pVertices[g_iVertexEnd].tex.u = g_QuadTexture[0].x;
+	g_pVertices[g_iVertexEnd].tex.v = g_QuadTexture[0].y;
+	g_pVertices[g_iVertexEnd].color = g_QuadColor[0];
+
+	g_pVertices[g_iVertexEnd + 1].pos.x = x1;
+	g_pVertices[g_iVertexEnd + 1].pos.y = y1;
+	g_pVertices[g_iVertexEnd + 1].tex.u = g_QuadTexture[1].x;
+	g_pVertices[g_iVertexEnd + 1].tex.v = g_QuadTexture[1].y;
+	g_pVertices[g_iVertexEnd + 1].color = g_QuadColor[1];
+
+	g_pVertices[g_iVertexEnd + 2].pos.x = x3;
+	g_pVertices[g_iVertexEnd + 2].pos.y = y3;
+	g_pVertices[g_iVertexEnd + 2].tex.u = g_QuadTexture[2].x;
+	g_pVertices[g_iVertexEnd + 2].tex.v = g_QuadTexture[2].y;
+	g_pVertices[g_iVertexEnd + 2].color = g_QuadColor[2];
+
+	g_pVertices[g_iVertexEnd + 3].pos.x = x2;
+	g_pVertices[g_iVertexEnd + 3].pos.y = y2;
+	g_pVertices[g_iVertexEnd + 3].tex.u = g_QuadTexture[3].x;
+	g_pVertices[g_iVertexEnd + 3].tex.v = g_QuadTexture[3].y;
+	g_pVertices[g_iVertexEnd + 3].color = g_QuadColor[3];
+	
+	draw_quad();
+}
+
+void gfx_quads_text(float x, float y, float size, const char *text)
+{
+	gfx_quads_begin();
+	while(*text)
+	{
+		char c = *text;
+		text++;
+		
+		gfx_quads_setsubset(
+			(c%16)/16.0f,
+			(c/16)/16.0f,
+			(c%16)/16.0f+1.0f/16.0f,
+			(c/16)/16.0f+1.0f/16.0f);
+		
+		gfx_quads_drawTL(x,y,size,size);
+		x += size/2;
+	}
+	
+	gfx_quads_end();
+}
+
+struct pretty_font
+{
+	float m_CharStartTable[256];
+	float m_CharEndTable[256];
+	int font_texture;
+};
+
+pretty_font default_font = 
+{
+{
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+        0, 0.421875, 0.359375, 0.265625, 0.25, 0.1875, 0.25, 0.4375, 0.390625, 0.390625, 0.34375, 0.28125, 0.421875, 0.390625, 0.4375, 0.203125, 
+        0.265625, 0.28125, 0.28125, 0.265625, 0.25, 0.28125, 0.28125, 0.265625, 0.28125, 0.265625, 0.4375, 0.421875, 0.3125, 0.28125, 0.3125, 0.3125, 
+        0.25, 0.234375, 0.28125, 0.265625, 0.265625, 0.296875, 0.3125, 0.25, 0.25, 0.421875, 0.28125, 0.265625, 0.328125, 0.171875, 0.234375, 0.25, 
+        0.28125, 0.234375, 0.265625, 0.265625, 0.28125, 0.265625, 0.234375, 0.09375, 0.234375, 0.234375, 0.265625, 0.390625, 0.203125, 0.390625, 0.296875, 0.28125, 
+        0.375, 0.3125, 0.3125, 0.3125, 0.296875, 0.3125, 0.359375, 0.296875, 0.3125, 0.4375, 0.390625, 0.328125, 0.4375, 0.203125, 0.3125, 0.296875, 
+        0.3125, 0.296875, 0.359375, 0.3125, 0.328125, 0.3125, 0.296875, 0.203125, 0.296875, 0.296875, 0.328125, 0.375, 0.421875, 0.375, 0.28125, 0.3125, 
+        0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 
+        0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 
+        0, 0.421875, 0.3125, 0.265625, 0.25, 0.25, 0.421875, 0.265625, 0.375, 0.21875, 0.375, 0.328125, 0.3125, 0, 0.21875, 0.28125, 
+        0.359375, 0.28125, 0.34375, 0.34375, 0.421875, 0.3125, 0.265625, 0.421875, 0.421875, 0.34375, 0.375, 0.328125, 0.125, 0.125, 0.125, 0.296875, 
+        0.234375, 0.234375, 0.234375, 0.234375, 0.234375, 0.234375, 0.109375, 0.265625, 0.296875, 0.296875, 0.296875, 0.296875, 0.375, 0.421875, 0.359375, 0.390625, 
+        0.21875, 0.234375, 0.25, 0.25, 0.25, 0.25, 0.25, 0.296875, 0.21875, 0.265625, 0.265625, 0.265625, 0.265625, 0.234375, 0.28125, 0.3125, 
+        0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.1875, 0.3125, 0.3125, 0.3125, 0.3125, 0.3125, 0.375, 0.421875, 0.359375, 0.390625, 
+        0.3125, 0.3125, 0.296875, 0.296875, 0.296875, 0.296875, 0.296875, 0.28125, 0.28125, 0.3125, 0.3125, 0.3125, 0.3125, 0.296875, 0.3125, 0.296875, 
+},
+{
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+        0.2, 0.5625, 0.625, 0.71875, 0.734375, 0.796875, 0.765625, 0.546875, 0.59375, 0.59375, 0.65625, 0.703125, 0.546875, 0.59375, 0.5625, 0.6875, 
+        0.71875, 0.609375, 0.703125, 0.703125, 0.71875, 0.703125, 0.703125, 0.6875, 0.703125, 0.703125, 0.5625, 0.546875, 0.671875, 0.703125, 0.671875, 0.671875, 
+        0.734375, 0.75, 0.734375, 0.734375, 0.734375, 0.6875, 0.6875, 0.734375, 0.71875, 0.5625, 0.65625, 0.765625, 0.703125, 0.8125, 0.75, 0.734375, 
+        0.734375, 0.765625, 0.71875, 0.71875, 0.703125, 0.71875, 0.75, 0.890625, 0.75, 0.75, 0.71875, 0.59375, 0.6875, 0.59375, 0.6875, 0.703125, 
+        0.5625, 0.671875, 0.6875, 0.671875, 0.671875, 0.671875, 0.625, 0.671875, 0.671875, 0.5625, 0.546875, 0.703125, 0.5625, 0.78125, 0.671875, 0.671875, 
+        0.6875, 0.671875, 0.65625, 0.671875, 0.65625, 0.671875, 0.6875, 0.78125, 0.6875, 0.671875, 0.65625, 0.609375, 0.546875, 0.609375, 0.703125, 0.671875, 
+        0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 
+        0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 
+        0, 0.5625, 0.671875, 0.734375, 0.734375, 0.734375, 0.546875, 0.71875, 0.609375, 0.765625, 0.609375, 0.65625, 0.671875, 0, 0.765625, 0.703125, 
+        0.625, 0.703125, 0.640625, 0.640625, 0.609375, 0.671875, 0.703125, 0.546875, 0.5625, 0.578125, 0.609375, 0.65625, 0.859375, 0.859375, 0.859375, 0.671875, 
+        0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.84375, 0.734375, 0.6875, 0.6875, 0.6875, 0.6875, 0.5625, 0.609375, 0.640625, 0.59375, 
+        0.734375, 0.75, 0.734375, 0.734375, 0.734375, 0.734375, 0.734375, 0.6875, 0.765625, 0.71875, 0.71875, 0.71875, 0.71875, 0.75, 0.734375, 0.6875, 
+        0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.796875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.5625, 0.609375, 0.625, 0.59375, 
+        0.6875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.703125, 0.703125, 0.671875, 0.671875, 0.671875, 0.671875, 0.671875, 0.6875, 0.671875, 
+},
+0
+};
+
+pretty_font *current_font = &default_font;
+
+void gfx_pretty_text(float x, float y, float size, const char *text)
+{
+	const float spacing = 0.05f;
+	
+	gfx_quads_begin();
+	
+	while (*text)
+	{
+		const int c = *text;
+		const float width = current_font->m_CharEndTable[c] - current_font->m_CharStartTable[c];
+		
+		text++;
+
+		gfx_quads_setsubset(
+			(c%16)/16.0f + current_font->m_CharStartTable[c]/16.0f, // startx
+			(c/16)/16.0f, // starty
+			(c%16)/16.0f + current_font->m_CharEndTable[c]/16.0f + 0.001, // endx
+			(c/16)/16.0f+1.0f/16.0f); // endy
+
+		gfx_quads_drawTL(x, y, width * size, size);
+
+		x += (width + spacing) * size;
+	}
+
+	gfx_quads_end();
+}
+
+float gfx_pretty_text_width(float size, const char *text)
+{
+	const float spacing = 0.05f;
+	float width = 0.0f;
+
+	while (*text)
+	{
+		const int c = *text++;
+		width += size * (current_font->m_CharEndTable[c] - current_font->m_CharStartTable[c] + spacing);
+	}
+
+	return width;
+}
diff --git a/src/engine/client/snd.cpp b/src/engine/client/snd.cpp
new file mode 100644
index 00000000..dd6baa9a
--- /dev/null
+++ b/src/engine/client/snd.cpp
@@ -0,0 +1,520 @@
+#include <baselib/system.h>
+#include <baselib/audio.h>
+#include <baselib/stream/file.h>
+
+#include <engine/interface.h>
+
+using namespace baselib;
+
+static const int NUM_FRAMES_STOP = 512;
+static const float NUM_FRAMES_STOP_INV = 1.0f/(float)NUM_FRAMES_STOP;
+static const int NUM_FRAMES_LERP = 512;
+static const float NUM_FRAMES_LERP_INV = 1.0f/(float)NUM_FRAMES_LERP;
+
+static const float GLOBAL_VOLUME_SCALE = 0.75f;
+
+static const int64 GLOBAL_SOUND_DELAY = 1000;
+
+// --- sound ---
+class sound_data
+{
+public:
+	short *data;
+	int num_samples;
+	int rate;
+	int channels;
+	int sustain_start;
+	int sustain_end;
+	int64 last_played;
+};
+
+inline short clamp(int i)
+{
+	if(i > 0x7fff)
+		return 0x7fff;
+	if(i < -0x7fff)
+		return -0x7fff;
+	return i;
+}
+
+class mixer : public audio_stream
+{
+public:
+	class channel
+	{
+	public:
+		channel()
+		{ data = 0; lerp = -1; stop = -1; }
+		
+		sound_data *data;
+		int tick;
+		int loop;
+		float pan;
+		float vol;
+		float old_vol;
+		float new_vol;
+		int lerp;
+		int stop;
+	};
+	
+	enum
+	{
+		MAX_CHANNELS=8,
+	};
+
+	channel channels[MAX_CHANNELS];
+
+	virtual void fill(void *output, unsigned long frames)
+	{
+		//dbg_msg("snd", "mixing!");
+		
+		short *out = (short*)output;
+		bool clamp_flag = false;
+		
+		int active_channels = 0;
+		for(unsigned long i = 0; i < frames; i++)
+		{
+			int left = 0;
+			int right = 0;
+			
+			for(int c = 0; c < MAX_CHANNELS; c++)
+			{
+				if(channels[c].data)
+				{
+					if(channels[c].data->channels == 1)
+					{
+						left += (1.0f-(channels[c].pan+1.0f)*0.5f) * channels[c].vol * channels[c].data->data[channels[c].tick];
+						right += (channels[c].pan+1.0f)*0.5f * channels[c].vol * channels[c].data->data[channels[c].tick];
+						channels[c].tick++;
+					}
+					else
+					{
+						float pl = channels[c].pan<0.0f?-channels[c].pan:1.0f;
+						float pr = channels[c].pan>0.0f?1.0f-channels[c].pan:1.0f;
+						left += pl*channels[c].vol * channels[c].data->data[channels[c].tick];
+						right += pr*channels[c].vol * channels[c].data->data[channels[c].tick + 1];
+						channels[c].tick += 2;
+					}
+				
+					if(channels[c].loop)
+					{
+						if(channels[c].data->sustain_start >= 0 && channels[c].tick >= channels[c].data->sustain_end)
+							channels[c].tick = channels[c].data->sustain_start;
+						else if(channels[c].tick > channels[c].data->num_samples)
+							channels[c].tick = 0;
+					}
+					else if(channels[c].tick > channels[c].data->num_samples)
+						channels[c].data = 0;
+
+					if(channels[c].stop == 0)
+					{
+						channels[c].stop = -1;
+						channels[c].data = 0;
+					}
+					else if(channels[c].stop > 0)
+					{
+						channels[c].vol = channels[c].old_vol * (float)channels[c].stop * NUM_FRAMES_STOP_INV;
+						channels[c].stop--;
+					}
+					if(channels[c].lerp > 0)
+					{
+						channels[c].vol = (1.0f - (float)channels[c].lerp * NUM_FRAMES_LERP_INV) * channels[c].new_vol +
+							(float)channels[c].lerp * NUM_FRAMES_LERP_INV * channels[c].old_vol;
+						channels[c].lerp--;
+					}
+					active_channels++;
+				}
+			}
+
+			// TODO: remove these
+
+			*out = clamp(left); // left
+			if(*out != left) clamp_flag = true;
+			out++;
+			*out = clamp(right); // right
+			if(*out != right) clamp_flag = true;
+			out++;
+		}
+
+		if(clamp_flag)
+			dbg_msg("snd", "CLAMPED!");
+	}
+	
+	int play(sound_data *sound, unsigned loop, float vol, float pan)
+	{
+		if(time_get() - sound->last_played < GLOBAL_SOUND_DELAY)
+			return -1;
+
+		for(int c = 0; c < MAX_CHANNELS; c++)
+		{
+			if(channels[c].data == 0)
+			{
+				channels[c].data = sound;
+				channels[c].tick = 0;
+				channels[c].loop = loop;
+				channels[c].vol = vol * GLOBAL_VOLUME_SCALE;
+				channels[c].pan = pan;
+				sound->last_played = time_get();
+				return c;
+			}
+		}
+
+		return -1;
+	}
+
+	void stop(int id)
+	{
+		dbg_assert(id >= 0 && id < MAX_CHANNELS, "id out of bounds");
+		channels[id].old_vol = channels[id].vol;
+		channels[id].stop = NUM_FRAMES_STOP;
+	}
+
+	void set_vol(int id, float vol)
+	{
+		dbg_assert(id >= 0 && id < MAX_CHANNELS, "id out of bounds");
+		channels[id].new_vol = vol * GLOBAL_VOLUME_SCALE;
+		channels[id].old_vol = channels[id].vol;
+		channels[id].lerp = NUM_FRAMES_LERP;
+	}
+};
+
+static mixer mixer;
+//static sound_data test_sound;
+
+/*
+extern "C" 
+{
+#include "wavpack/wavpack.h"
+}*/
+
+/*
+static file_stream *read_func_filestream;
+static int32_t read_func(void *buff, int32_t bcount)
+{
+    return read_func_filestream->read(buff, bcount);
+}
+static uchar *format_samples(int bps, uchar *dst, int32_t *src, uint32_t samcnt)
+{
+    int32_t temp;
+
+    switch (bps) {
+
+        case 1:
+            while (samcnt--)
+                *dst++ = *src++ + 128;
+
+            break;
+
+        case 2:
+            while (samcnt--) {
+                *dst++ = (uchar)(temp = *src++);
+                *dst++ = (uchar)(temp >> 8);
+            }
+
+            break;
+
+        case 3:
+            while (samcnt--) {
+                *dst++ = (uchar)(temp = *src++);
+                *dst++ = (uchar)(temp >> 8);
+                *dst++ = (uchar)(temp >> 16);
+            }
+
+            break;
+
+        case 4:
+            while (samcnt--) {
+                *dst++ = (uchar)(temp = *src++);
+                *dst++ = (uchar)(temp >> 8);
+                *dst++ = (uchar)(temp >> 16);
+                *dst++ = (uchar)(temp >> 24);
+            }
+
+            break;
+    }
+
+    return dst;
+}*/
+
+/*
+struct sound_holder
+{
+	sound_data sound;
+	int next;
+};
+
+static const int MAX_SOUNDS = 256;
+static sound_holder sounds[MAX_SOUNDS];
+static int first_free_sound;
+
+bool snd_load_wv(const char *filename, sound_data *snd)
+{
+	// open file
+	file_stream file;
+	if(!file.open_r(filename))
+	{
+		dbg_msg("sound/wv", "failed to open file. filename='%s'", filename);
+		return false;
+	}
+	read_func_filestream = &file;
+	
+	// get info
+	WavpackContext *wpc;
+	char error[128];
+	wpc = WavpackOpenFileInput(read_func, error);
+	if(!wpc)
+	{
+		dbg_msg("sound/wv", "failed to open file. err=%s filename='%s'", error, filename);
+		return false;
+	}
+
+
+	snd->num_samples = WavpackGetNumSamples(wpc);
+    int bps = WavpackGetBytesPerSample(wpc);
+	int channels = WavpackGetReducedChannels(wpc);
+	snd->rate = WavpackGetSampleRate(wpc);
+	int bits = WavpackGetBitsPerSample(wpc);
+	
+	(void)bps;
+	(void)channels;
+	(void)bits;
+	
+	// decompress
+	int datasize = snd->num_samples*2;
+	snd->data = (short*)mem_alloc(datasize, 1);
+	int totalsamples = 0;
+	while(1)
+	{
+		int buffer[1024*4];
+		int samples_unpacked = WavpackUnpackSamples(wpc, buffer, 1024*4);
+		totalsamples += samples_unpacked;
+		
+		if(samples_unpacked)
+		{
+			// convert
+		}
+	}
+	
+	if(snd->num_samples != totalsamples)
+	{
+		dbg_msg("sound/wv", "wrong amount of samples. filename='%s'", filename);
+		mem_free(snd->data);
+		return false;;
+	}
+		
+	return false;
+}*/
+
+struct sound_holder
+{
+	sound_data sound;
+	int next;
+};
+
+static const int MAX_SOUNDS = 1024;
+static sound_holder sounds[MAX_SOUNDS];
+static int first_free_sound;
+
+bool snd_init()
+{
+	first_free_sound = 0;
+	for(int i = 0; i < MAX_SOUNDS; i++)
+		sounds[i].next = i+1;
+	sounds[MAX_SOUNDS-1].next = -1;
+	return mixer.create();
+}
+
+bool snd_shutdown()
+{
+	mixer.destroy();
+	return true;
+}
+
+static int snd_alloc_sound()
+{
+	if(first_free_sound < 0)
+		return -1;
+	int id = first_free_sound;
+	first_free_sound = sounds[id].next;
+	sounds[id].next = -1;
+	return id;
+}
+
+int snd_load_wav(const char *filename)
+{
+	sound_data snd;
+	
+	// open file for reading
+	file_stream file;
+	if(!file.open_r(filename))
+	{
+		dbg_msg("sound/wav", "failed to open file. filename='%s'", filename);
+		return -1;
+	}
+
+	int id = -1;
+	int state = 0;
+	while(1)
+	{
+		// read chunk header
+		unsigned char head[8];
+		if(file.read(head, sizeof(head)) != 8)
+		{
+			break;
+		}
+		
+		int chunk_size = head[4] | (head[5]<<8) | (head[6]<<16) | (head[7]<<24);
+		head[4] = 0;
+			
+		if(state == 0)
+		{
+			// read the riff and wave headers
+			if(head[0] != 'R' || head[1] != 'I' || head[2] != 'F' || head[3] != 'F')
+			{
+				dbg_msg("sound/wav", "not a RIFF file. filename='%s'", filename);
+				return -1;
+			}
+			
+			unsigned char type[4];
+			file.read(type, 4);
+
+			if(type[0] != 'W' || type[1] != 'A' || type[2] != 'V' || type[3] != 'E')
+			{
+				dbg_msg("sound/wav", "RIFF file is not a WAVE. filename='%s'", filename);
+				return -1;
+			}
+			
+			state++;
+		}
+		else if(state == 1)
+		{
+			// read the format chunk
+			if(head[0] == 'f' && head[1] == 'm' && head[2] == 't' && head[3] == ' ')
+			{
+				unsigned char fmt[16];
+				if(file.read(fmt, sizeof(fmt)) !=  sizeof(fmt))
+				{
+					dbg_msg("sound/wav", "failed to read format. filename='%s'", filename);
+					return -1;
+				}
+				
+				// decode format
+				int compression_code = fmt[0] | (fmt[1]<<8);
+				snd.channels = fmt[2] | (fmt[3]<<8);
+				snd.rate = fmt[4] | (fmt[5]<<8) | (fmt[6]<<16) | (fmt[7]<<24);
+
+				if(compression_code != 1)
+				{
+					dbg_msg("sound/wav", "file is not uncompressed. filename='%s'", filename);
+					return -1;
+				}
+				
+				if(snd.channels > 2)
+				{
+					dbg_msg("sound/wav", "file is not mono or stereo. filename='%s'", filename);
+					return -1;
+				}
+
+				if(snd.rate != 44100)
+				{
+					dbg_msg("sound/wav", "file is %d Hz, not 44100 Hz. filename='%s'", snd.rate, filename);
+					return -1;
+				}
+				
+				int bps = fmt[14] | (fmt[15]<<8);
+				if(bps != 16)
+				{
+					dbg_msg("sound/wav", "bps is %d, not 16, filname='%s'", bps, filename);
+					return -1;
+				}
+				
+				// skip extra bytes (not used for uncompressed)
+				//int extra_bytes = fmt[14] | (fmt[15]<<8);
+				//dbg_msg("sound/wav", "%d", extra_bytes);
+				//file.skip(extra_bytes);
+				
+				// next state
+				state++;
+			}
+			else
+				file.skip(chunk_size);
+		}
+		else if(state == 2)
+		{
+			// read the data
+			if(head[0] == 'd' && head[1] == 'a' && head[2] == 't' && head[3] == 'a')
+			{
+				snd.data = (short*)mem_alloc(chunk_size, 1);
+				file.read(snd.data, chunk_size);
+				snd.num_samples = chunk_size/(2);
+				snd.sustain_start = -1;
+				snd.sustain_end = -1;
+				snd.last_played = 0;
+				id = snd_alloc_sound();
+				sounds[id].sound = snd;
+				state++;
+			}
+			else
+				file.skip(chunk_size);
+		}
+		else if(state == 3)
+		{
+			if(head[0] == 's' && head[1] == 'm' && head[2] == 'p' && head[3] == 'l')
+			{
+				int smpl[9];
+				int loop[6];
+
+				file.read(smpl, sizeof(smpl));
+
+				if(smpl[7] > 0)
+				{
+					file.read(loop, sizeof(loop));
+					sounds[id].sound.sustain_start = loop[2] * sounds[id].sound.channels;
+					sounds[id].sound.sustain_end = loop[3] * sounds[id].sound.channels;
+				}
+
+				if(smpl[7] > 1)
+					file.skip((smpl[7]-1) * sizeof(loop));
+
+				file.skip(smpl[8]);
+				state++;
+			}
+			else
+				file.skip(chunk_size);
+		}
+		else
+			file.skip(chunk_size);
+	}
+
+	if(id >= 0)
+		dbg_msg("sound/wav", "loaded %s", filename);
+	else
+		dbg_msg("sound/wav", "failed to load %s", filename);
+
+	return id;
+}
+
+int snd_play(int id, int loop, float vol, float pan)
+{
+	if(id < 0)
+	{
+		dbg_msg("snd", "bad sound id");
+		return -1;
+	}
+
+	dbg_assert(sounds[id].sound.data != 0, "null sound");
+	dbg_assert(sounds[id].next == -1, "sound isn't allocated");
+	return mixer.play(&sounds[id].sound, loop, vol, pan);
+}
+
+void snd_stop(int id)
+{
+	if(id >= 0)
+		mixer.stop(id);
+}
+
+void snd_set_vol(int id, float vol)
+{
+	if(id >= 0)
+		mixer.set_vol(id, vol);
+}
diff --git a/src/engine/client/ui.cpp b/src/engine/client/ui.cpp
new file mode 100644
index 00000000..3353feca
--- /dev/null
+++ b/src/engine/client/ui.cpp
@@ -0,0 +1,115 @@
+#include <engine/interface.h>
+#include "ui.h"
+
+/********************************************************
+ UI                                                      
+*********************************************************/
+//static unsigned mouse_buttons_last = 0;
+
+struct pretty_font
+{
+    char m_CharStartTable[256];
+	char m_CharEndTable[256];
+	int font_texture;
+};
+
+extern pretty_font *current_font;
+void gfx_pretty_text(float x, float y, float size, const char *text);
+
+
+static void *hot_item = 0;
+static void *active_item = 0;
+static void *becomming_hot_item = 0;
+static float mouse_x, mouse_y; // in gui space
+static float mouse_wx, mouse_wy; // in world space
+static unsigned mouse_buttons = 0;
+
+float ui_mouse_x() { return mouse_x; }
+float ui_mouse_y() { return mouse_y; }
+float ui_mouse_world_x() { return mouse_wx; }
+float ui_mouse_world_y() { return mouse_wy; }
+int ui_mouse_button(int index) { return (mouse_buttons>>index)&1; }
+
+void ui_set_hot_item(void *id) { becomming_hot_item = id; }
+void ui_set_active_item(void *id) { active_item = id; }
+void *ui_hot_item() { return hot_item; }
+void *ui_active_item() { return active_item; }
+
+int ui_update(float mx, float my, float mwx, float mwy, int buttons)
+{
+    //mouse_buttons_last = mouse_buttons;
+    mouse_x = mx;
+    mouse_y = my;
+    mouse_wx = mwx;
+    mouse_wy = mwy;
+    mouse_buttons = buttons;
+    hot_item = becomming_hot_item;
+    becomming_hot_item = 0;
+    return 0;
+}
+
+/*
+static int ui_mouse_button_released(int index)
+{
+    return ((mouse_buttons_last>>index)&1) && !();
+}*/
+
+int ui_mouse_inside(float x, float y, float w, float h)
+{
+    if(mouse_x >= x && mouse_x <= x+w && mouse_y >= y && mouse_y <= y+h)
+        return 1;
+    return 0;
+}
+
+void ui_do_image(int texture, float x, float y, float w, float h)
+{
+    gfx_blend_normal();
+    gfx_texture_set(texture);
+    gfx_quads_begin();
+    gfx_quads_setcolor(1,1,1,1);
+    gfx_quads_setsubset(
+        0.0f, // startx
+        0.0f, // starty
+        1.0f, // endx
+        1.0f); // endy                                
+    gfx_quads_drawTL(x,y,w,h);
+    gfx_quads_end();
+}
+
+void ui_do_label(float x, float y, char *text)
+{
+    gfx_blend_normal();
+    gfx_texture_set(current_font->font_texture);
+    gfx_pretty_text(x, y, 18.f, text);
+}
+
+int ui_do_button(void *id, const char *text, int checked, float x, float y, float w, float h, draw_button_callback draw_func, void *extra)
+{
+    // logic
+    int r = 0;
+    int inside = ui_mouse_inside(x,y,w,h);
+
+	if(inside)
+	{
+		ui_set_hot_item(id);
+
+		if(ui_mouse_button(0))
+			ui_set_active_item(id);
+	}
+
+	if(ui_active_item() == id && ui_hot_item() == id && !ui_mouse_button(0))
+	{
+		ui_set_active_item(0);
+		r = 1;
+	}
+
+    draw_func(id, text, checked, x, y, w, h, extra);
+
+    return r;
+}
+
+int ui_do_button(void *id, const char *text, int checked, float x, float y, float w, float h, draw_button_callback draw_func)
+{
+	return ui_do_button(id, text, checked, x, y, w, h, draw_func, 0x0);
+}
+
diff --git a/src/engine/client/ui.h b/src/engine/client/ui.h
new file mode 100644
index 00000000..1a420906
--- /dev/null
+++ b/src/engine/client/ui.h
@@ -0,0 +1,33 @@
+#ifndef _UI_H
+#define _UI_H
+/*
+extern void *hot_item;
+extern void *active_item;
+extern void *becomming_hot_item;
+extern float mouse_x, mouse_y; // in gui space
+extern float mouse_wx, mouse_wy; // in world space
+extern unsigned mouse_buttons;*/
+
+int ui_update(float mx, float my, float mwx, float mwy, int buttons);
+
+float ui_mouse_x();
+float ui_mouse_y();
+float ui_mouse_world_x();
+float ui_mouse_world_y();
+int ui_mouse_button(int index);
+
+void ui_set_hot_item(void *id);
+void ui_set_active_item(void *id);
+void *ui_hot_item();
+void *ui_active_item();
+
+int ui_mouse_inside(float x, float y, float w, float h);
+
+typedef void (*draw_button_callback)(void *id, const char *text, int checked, float x, float y, float w, float h, void *extra);
+
+void ui_do_image(int texture, float x, float y, float w, float h);
+void ui_do_label(float x, float y, char *text);
+int ui_do_button(void *id, const char *text, int checked, float x, float y, float w, float h, draw_button_callback draw_func, void *extra);
+int ui_do_button(void *id, const char *text, int checked, float x, float y, float w, float h, draw_button_callback draw_func);
+
+#endif