about summary refs log tree commit diff
path: root/src/game/editor
diff options
context:
space:
mode:
authorMagnus Auvinen <magnus.auvinen@gmail.com>2010-05-29 07:25:38 +0000
committerMagnus Auvinen <magnus.auvinen@gmail.com>2010-05-29 07:25:38 +0000
commit72c06a258940696093f255fb1061beb58e1cdd0b (patch)
tree36b9a7712eec2d4f07837eab9c38ef1c5af85319 /src/game/editor
parente56feb597bc743677633432f77513b02907fd169 (diff)
downloadzcatch-72c06a258940696093f255fb1061beb58e1cdd0b.tar.gz
zcatch-72c06a258940696093f255fb1061beb58e1cdd0b.zip
copied refactor to trunk
Diffstat (limited to 'src/game/editor')
-rwxr-xr-xsrc/game/editor/array.hpp238
-rw-r--r--src/game/editor/ed_editor.cpp3505
-rw-r--r--src/game/editor/ed_editor.h615
-rw-r--r--src/game/editor/ed_editor.hpp589
-rw-r--r--src/game/editor/ed_io.cpp488
-rw-r--r--src/game/editor/ed_layer_game.cpp18
-rw-r--r--src/game/editor/ed_layer_quads.cpp245
-rw-r--r--src/game/editor/ed_layer_tiles.cpp354
-rw-r--r--src/game/editor/ed_popups.cpp426
9 files changed, 3138 insertions, 3340 deletions
diff --git a/src/game/editor/array.hpp b/src/game/editor/array.hpp
deleted file mode 100755
index fe9f2739..00000000
--- a/src/game/editor/array.hpp
+++ /dev/null
@@ -1,238 +0,0 @@
-

-template <class T>

-class array

-{

-	// 

-	//

-	void init()

-	{

-		list = 0;

-		clear();

-	}

-

-public:

-	array()

-	{

-		init();

-	}

-	

-	//

-	array(const array &other)

-	{

-		init();

-		setsize(other.len());

-		for(int i = 0; i < len(); i++)

-			(*this)[i] = other[i];

-	}

-

-

-	// 

-	//

-	virtual ~array()

-	{

-		delete [] list;

-		list = 0;

-	}

-

-	//

-	//

-	void deleteall()

-	{

-		for(int i = 0; i < len(); i++)

-			delete list[i];

-

-		clear();

-	}

-

-

-	//

-	//

-	void clear()

-	{

-		delete [] list;

-		

-		list_size = 1;

-		list = new T[1];

-		num_elements = 0;

-	}

-

-	int find(T val)

-	{

-		for(int i = 0; i < len(); i++)

-			if((*this)[i] == val)

-				return i;

-		return -1;

-	}

-

-	bool exist(T val)

-	{

-		return find(val) != -1;

-	}

-

-	//

-	// returns the number of elements in the list

-	//

-	int len() const

-	{

-		return num_elements;

-	}

-

-	//

-	// This doesn't conserve the order in the list. Be careful

-	//

-	void removebyindexfast(int index)

-	{

-		//ASSUME(_Pos >= 0 && _Pos < num_elements);

-		list[index] = list[num_elements-1];

-		setsize(len()-1);

-	}

-

-	void removefast(const T& _Elem)

-	{

-		for(int i = 0; i < len(); i++)

-			if(list[i] == _Elem)

-			{

-				removebyindexfast(i);

-				return;

-			}

-	}

-

-	//

-	//

-	void removebyindex(int index)

-	{

-		//ASSUME(_Pos >= 0 && _Pos < num_elements);

-

-		for(int i = index+1; i < num_elements; i++)

-			list[i-1] = list[i];

-		

-		setsize(len()-1);

-	}

-

-	void insert(int index, const T& element)

-	{

-		int some_len = len();

-		if (index < some_len)

-			setsize(some_len+1);

-		else

-			setsize(index + 1);

-

-		for(int i = num_elements-2; i >= index; i--)

-			list[i+1] = list[i];

-

-		list[index] = element;

-	}

-

-	bool remove(const T& element)

-	{

-		for(int i = 0; i < len(); i++)

-			if(list[i] == element)

-			{

-				removebyindex(i);

-				return true;

-			}

-		return false;

-	}

-

-	// 

-	//

-	int add(const T& element)

-	{

-		//if(num_elements == list_size)

-		setsize(len()+1);

-		list[num_elements-1] = element;

-		return num_elements-1;

-	}

-

-	// 

-	//

-	int add(const T& elem, int index)

-	{

-		setsize(len()+1);

-		

-		for(int i = num_elements-1; i > index; i--)

-			list[i] = list[i-1];

-

-		list[index] = elem;

-		

-		//num_elements++;

-		return num_elements-1;

-	}

-

-	// 

-	//

-	T& operator[] (int index)

-	{

-		return list[index];

-	}

-

-	const T& operator[] (int index) const

-	{

-		return list[index];

-	}

-

-	//

-	//

-	T *getptr()

-	{

-		return list;

-	}

-

-	const T *getptr() const

-	{

-		return list;

-	}

-

-	//

-	//

-	//

-	void setsize(int new_len)

-	{

-		if (list_size < new_len)

-			allocsize(new_len);

-		num_elements = new_len;

-	}

-

-	// removes unnessasary data, returns how many bytes was earned

-	int optimize()

-	{

-		int Before = memoryusage();

-		setsize(num_elements);

-		return Before - memoryusage();

-	}

-

-	// returns how much memory this dynamic array is using

-	int memoryusage()

-	{

-		return sizeof(array) + sizeof(T)*list_size;

-	}

-

-	//

-	array &operator = (const array &other)

-	{

-		setsize(other.len());

-		for(int i = 0; i < len(); i++)

-			(*this)[i] = other[i];

-		return *this;

-	}		

-private:

-	void allocsize(int new_len)

-	{

-		list_size = new_len;

-		T *new_list = new T[list_size];

-		

-		long end = num_elements < list_size ? num_elements : list_size;

-		for(int i = 0; i < end; i++)

-			new_list[i] = list[i];

-		

-		delete [] list;

-		list = 0;

-		num_elements = num_elements < list_size ? num_elements : list_size;

-		list = new_list;

-	}

-

-	T *list;

-	long list_size;

-	long num_elements;

-};

-

diff --git a/src/game/editor/ed_editor.cpp b/src/game/editor/ed_editor.cpp
index 17b891fc..480a6827 100644
--- a/src/game/editor/ed_editor.cpp
+++ b/src/game/editor/ed_editor.cpp
@@ -1,185 +1,180 @@
-/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
+// copyright (c) 2007 magnus auvinen, see licence.txt for more info
 
 #include <base/system.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 
-#include <engine/e_common_interface.h>
-#include <engine/e_datafile.h>
-#include <engine/e_config.h>
-#include <engine/e_engine.h>
-#include <engine/client/graphics.h>
+#include <engine/shared/datafile.h>
+#include <engine/shared/config.h>
+#include <engine/shared/engine.h>
+#include <engine/client.h>
+#include <engine/graphics.h>
+#include <engine/textrender.h>
+#include <engine/input.h>
+#include <engine/keys.h>
+#include <engine/storage.h>
 
-#include <game/client/ui.hpp>
-#include <game/gamecore.hpp>
-#include <game/client/render.hpp>
+#include <game/client/ui.h>
+#include <game/gamecore.h>
+#include <game/client/render.h>
 
-#include "ed_editor.hpp"
-#include <game/client/lineinput.hpp>
+#include "ed_editor.h"
+#include <game/client/lineinput.h>
 
-static int checker_texture = 0;
-static int background_texture = 0;
-static int cursor_texture = 0;
-static int entities_texture = 0;
+int CEditor::ms_CheckerTexture;
+int CEditor::ms_BackgroundTexture;
+int CEditor::ms_CursorTexture;
+int CEditor::ms_EntitiesTexture;
+const void* CEditor::ms_pUiGotContext;
 
 enum
 {
 	BUTTON_CONTEXT=1,
 };
 
-
-
-EDITOR_IMAGE::~EDITOR_IMAGE()
+CEditorImage::~CEditorImage()
 {
-	editor->Graphics()->UnloadTexture(tex_id);
+	m_pEditor->Graphics()->UnloadTexture(m_TexId);
 }
 
-static const void *ui_got_context = 0;
-
-LAYERGROUP::LAYERGROUP()
+CLayerGroup::CLayerGroup()
 {
-	name = "";
-	visible = true;
-	game_group = false;
-	offset_x = 0;
-	offset_y = 0;
-	parallax_x = 100;
-	parallax_y = 100;
-	
-	use_clipping = 0;
-	clip_x = 0;
-	clip_y = 0;
-	clip_w = 0;
-	clip_h = 0;
+	m_pName = "";
+	m_Visible = true;
+	m_GameGroup = false;
+	m_OffsetX = 0;
+	m_OffsetY = 0;
+	m_ParallaxX = 100;
+	m_ParallaxY = 100;
+
+	m_UseClipping = 0;
+	m_ClipX = 0;
+	m_ClipY = 0;
+	m_ClipW = 0;
+	m_ClipH = 0;
 }
 
-LAYERGROUP::~LAYERGROUP()
+CLayerGroup::~CLayerGroup()
 {
-	clear();
+	Clear();
 }
 
-void LAYERGROUP::convert(CUIRect *rect)
+void CLayerGroup::Convert(CUIRect *pRect)
 {
-	rect->x += offset_x;
-	rect->y += offset_y;
+	pRect->x += m_OffsetX;
+	pRect->y += m_OffsetY;
 }
 
-void LAYERGROUP::mapping(float *points)
+void CLayerGroup::Mapping(float *pPoints)
 {
-	m_pMap->editor->RenderTools()->mapscreen_to_world(
-		m_pMap->editor->world_offset_x, m_pMap->editor->world_offset_y,
-		parallax_x/100.0f, parallax_y/100.0f,
-		offset_x, offset_y,
-		m_pMap->editor->Graphics()->ScreenAspect(), m_pMap->editor->world_zoom, points);
-		
-	points[0] += m_pMap->editor->editor_offset_x;
-	points[1] += m_pMap->editor->editor_offset_y;
-	points[2] += m_pMap->editor->editor_offset_x;
-	points[3] += m_pMap->editor->editor_offset_y;
+	m_pMap->m_pEditor->RenderTools()->MapscreenToWorld(
+		m_pMap->m_pEditor->m_WorldOffsetX, m_pMap->m_pEditor->m_WorldOffsetY,
+		m_ParallaxX/100.0f, m_ParallaxY/100.0f,
+		m_OffsetX, m_OffsetY,
+		m_pMap->m_pEditor->Graphics()->ScreenAspect(), m_pMap->m_pEditor->m_WorldZoom, pPoints);
+
+	pPoints[0] += m_pMap->m_pEditor->m_EditorOffsetX;
+	pPoints[1] += m_pMap->m_pEditor->m_EditorOffsetY;
+	pPoints[2] += m_pMap->m_pEditor->m_EditorOffsetX;
+	pPoints[3] += m_pMap->m_pEditor->m_EditorOffsetY;
 }
 
-void LAYERGROUP::mapscreen()
+void CLayerGroup::MapScreen()
 {
-	float points[4];
-	mapping(points);
-	m_pMap->editor->Graphics()->MapScreen(points[0], points[1], points[2], points[3]);
+	float aPoints[4];
+	Mapping(aPoints);
+	m_pMap->m_pEditor->Graphics()->MapScreen(aPoints[0], aPoints[1], aPoints[2], aPoints[3]);
 }
 
-void LAYERGROUP::render()
+void CLayerGroup::Render()
 {
-	mapscreen();
-	IGraphics *pGraphics = m_pMap->editor->Graphics();
-	
-	if(use_clipping)
-	{
-		float points[4];
-		m_pMap->game_group->mapping(points);
-		float x0 = (clip_x - points[0]) / (points[2]-points[0]);
-		float y0 = (clip_y - points[1]) / (points[3]-points[1]);
-		float x1 = ((clip_x+clip_w) - points[0]) / (points[2]-points[0]);
-		float y1 = ((clip_y+clip_h) - points[1]) / (points[3]-points[1]);
-		
+	MapScreen();
+	IGraphics *pGraphics = m_pMap->m_pEditor->Graphics();
+
+	if(m_UseClipping)
+	{
+		float aPoints[4];
+		m_pMap->m_pGameGroup->Mapping(aPoints);
+		float x0 = (m_ClipX - aPoints[0]) / (aPoints[2]-aPoints[0]);
+		float y0 = (m_ClipY - aPoints[1]) / (aPoints[3]-aPoints[1]);
+		float x1 = ((m_ClipX+m_ClipW) - aPoints[0]) / (aPoints[2]-aPoints[0]);
+		float y1 = ((m_ClipY+m_ClipH) - aPoints[1]) / (aPoints[3]-aPoints[1]);
+
 		pGraphics->ClipEnable((int)(x0*pGraphics->ScreenWidth()), (int)(y0*pGraphics->ScreenHeight()),
 			(int)((x1-x0)*pGraphics->ScreenWidth()), (int)((y1-y0)*pGraphics->ScreenHeight()));
 	}
-	
-	for(int i = 0; i < layers.len(); i++)
+
+	for(int i = 0; i < m_lLayers.size(); i++)
 	{
-		if(layers[i]->visible && layers[i] != m_pMap->game_layer)
+		if(m_lLayers[i]->m_Visible && m_lLayers[i] != m_pMap->m_pGameLayer)
 		{
-			if(m_pMap->editor->show_detail || !(layers[i]->flags&LAYERFLAG_DETAIL))
-				layers[i]->render();
+			if(m_pMap->m_pEditor->m_ShowDetail || !(m_lLayers[i]->m_Flags&LAYERFLAG_DETAIL))
+				m_lLayers[i]->Render();
 		}
 	}
-	
+
 	pGraphics->ClipDisable();
 }
 
-bool LAYERGROUP::is_empty() const { return layers.len() == 0; }
-void LAYERGROUP::clear() { layers.deleteall(); }
-void LAYERGROUP::add_layer(LAYER *l) { layers.add(l); }
-
-void LAYERGROUP::delete_layer(int index)
+void CLayerGroup::DeleteLayer(int Index)
 {
-	if(index < 0 || index >= layers.len()) return;
-	delete layers[index];
-	layers.removebyindex(index);
-}	
+	if(Index < 0 || Index >= m_lLayers.size()) return;
+	delete m_lLayers[Index];
+	m_lLayers.remove_index(Index);
+}
 
-void LAYERGROUP::get_size(float *w, float *h)
+void CLayerGroup::GetSize(float *w, float *h)
 {
 	*w = 0; *h = 0;
-	for(int i = 0; i < layers.len(); i++)
+	for(int i = 0; i < m_lLayers.size(); i++)
 	{
 		float lw, lh;
-		layers[i]->get_size(&lw, &lh);
+		m_lLayers[i]->GetSize(&lw, &lh);
 		*w = max(*w, lw);
 		*h = max(*h, lh);
 	}
 }
 
 
-int LAYERGROUP::swap_layers(int index0, int index1)
+int CLayerGroup::SwapLayers(int Index0, int Index1)
 {
-	if(index0 < 0 || index0 >= layers.len()) return index0;
-	if(index1 < 0 || index1 >= layers.len()) return index0;
-	if(index0 == index1) return index0;
-	swap(layers[index0], layers[index1]);
-	return index1;
+	if(Index0 < 0 || Index0 >= m_lLayers.size()) return Index0;
+	if(Index1 < 0 || Index1 >= m_lLayers.size()) return Index0;
+	if(Index0 == Index1) return Index0;
+	swap(m_lLayers[Index0], m_lLayers[Index1]);
+	return Index1;
 }
 
-void EDITOR_IMAGE::analyse_tileflags()
+void CEditorImage::AnalyseTileFlags()
 {
-	mem_zero(tileflags, sizeof(tileflags));
-	
-	int tw = width/16; // tilesizes
-	int th = height/16;
-	if ( tw == th ) {
-		unsigned char *pixeldata = (unsigned char *)data;
-		
-		int tile_id = 0;
+	mem_zero(m_aTileFlags, sizeof(m_aTileFlags));
+
+	int tw = m_Width/16; // tilesizes
+	int th = m_Height/16;
+	if ( tw == th )
+    {
+		unsigned char *pPixelData = (unsigned char *)m_pData;
+
+		int TileId = 0;
 		for(int ty = 0; ty < 16; ty++)
-			for(int tx = 0; tx < 16; tx++, tile_id++)
+			for(int tx = 0; tx < 16; tx++, TileId++)
 			{
-				bool opaque = true;
+				bool Opaque = true;
 				for(int x = 0; x < tw; x++)
 					for(int y = 0; y < th; y++)
 					{
-						int p = (ty*tw+y)*width + tx*tw+x;
-						if(pixeldata[p*4+3] < 250)
+						int p = (ty*tw+y)*m_Width + tx*tw+x;
+						if(pPixelData[p*4+3] < 250)
 						{
-							opaque = false;
+							Opaque = false;
 							break;
 						}
 					}
-					
-				if(opaque)
-					tileflags[tile_id] |= TILEFLAG_OPAQUE;
+
+				if(Opaque)
+					m_aTileFlags[TileId] |= TILEFLAG_OPAQUE;
 			}
 	}
-	
+
 }
 
 /********************************************************
@@ -189,42 +184,42 @@ void EDITOR_IMAGE::analyse_tileflags()
 // copied from gc_menu.cpp, should be more generalized
 //extern int ui_do_edit_box(void *id, const CUIRect *rect, char *str, int str_size, float font_size, bool hidden=false);
 
-int EDITOR::DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, bool Hidden)
+int CEditor::DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, bool Hidden)
 {
     int Inside = UI()->MouseInside(pRect);
 	int ReturnValue = 0;
-	static int AtIndex = 0;
+	static int s_AtIndex = 0;
 
 	if(UI()->LastActiveItem() == pID)
 	{
-		int Len = strlen(pStr);
-			
+		int Len = str_length(pStr);
+
 		if(Inside && UI()->MouseButton(0))
 		{
-			int mx_rel = (int)(UI()->MouseX() - pRect->x);
+			int MxRel = (int)(UI()->MouseX() - pRect->x);
 
 			for (int i = 1; i <= Len; i++)
 			{
-				if (gfx_text_width(0, FontSize, pStr, i) + 10 > mx_rel)
+				if (TextRender()->TextWidth(0, FontSize, pStr, i) + 10 > MxRel)
 				{
-					AtIndex = i - 1;
+					s_AtIndex = i - 1;
 					break;
 				}
 
 				if (i == Len)
-					AtIndex = Len;
+					s_AtIndex = Len;
 			}
 		}
 
-		for(int i = 0; i < inp_num_events(); i++)
+		for(int i = 0; i < Input()->NumEvents(); i++)
 		{
-			Len = strlen(pStr);
-			LINEINPUT::manipulate(inp_get_event(i), pStr, StrSize, &Len, &AtIndex);
+			Len = str_length(pStr);
+			CLineInput::Manipulate(Input()->GetEvent(i), pStr, StrSize, &Len, &s_AtIndex);
 		}
 	}
 
 	bool JustGotActive = false;
-	
+
 	if(UI()->ActiveItem() == pID)
 	{
 		if(!UI()->MouseButton(0))
@@ -239,219 +234,138 @@ int EDITOR::DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrS
 			UI()->SetActiveItem(pID);
 		}
 	}
-	
+
 	if(Inside)
 		UI()->SetHotItem(pID);
 
 	CUIRect Textbox = *pRect;
-	RenderTools()->DrawUIRect(&Textbox, vec4(1,1,1,0.5f), CUI::CORNER_ALL, 5.0f);
-	Textbox.VMargin(5.0f, &Textbox);
-	
+	RenderTools()->DrawUIRect(&Textbox, vec4(1,1,1,0.5f), CUI::CORNER_ALL, 3.0f);
+	Textbox.VMargin(3.0f, &Textbox);
+
 	const char *pDisplayStr = pStr;
 	char aStars[128];
-	
+
 	if(Hidden)
 	{
-		unsigned s = strlen(pStr);
+		unsigned s = str_length(pStr);
 		if(s >= sizeof(aStars))
 			s = sizeof(aStars)-1;
-		memset(aStars, '*', s);
+		for(unsigned int i = 0; i < s; ++i)
+			aStars[i] = '*';
 		aStars[s] = 0;
 		pDisplayStr = aStars;
 	}
 
 	UI()->DoLabel(&Textbox, pDisplayStr, FontSize, -1);
 	
-	if (UI()->LastActiveItem() == pID && !JustGotActive)
+	//TODO: make it blink
+	if(UI()->LastActiveItem() == pID && !JustGotActive)
 	{
-		float w = gfx_text_width(0, FontSize, pDisplayStr, AtIndex);
+		float w = TextRender()->TextWidth(0, FontSize, pDisplayStr, s_AtIndex);
+		Textbox = *pRect;
+		Textbox.VSplitLeft(2.0f, 0, &Textbox);
 		Textbox.x += w*UI()->Scale();
-		UI()->DoLabel(&Textbox, "_", FontSize, -1);
-	}
-
-	return ReturnValue;
-}
-
-/*
-int ui_do_edit_box(void *id, const CUIRect *rect, char *str, int str_size, float font_size, bool hidden=false)
-{
-    int inside = UI()->MouseInside(rect);
-	int r = 0;
-	static int at_index = 0;
-
-	if(UI()->LastActiveItem() == id)
-	{
-		int len = strlen(str);
-
-		if (inside && UI()->MouseButton(0))
-		{
-			int mx_rel = (int)(UI()->MouseX() - rect->x);
-
-			for (int i = 1; i <= len; i++)
-			{
-				if (gfx_text_width(0, font_size, str, i) + 10 > mx_rel)
-				{
-					at_index = i - 1;
-					break;
-				}
-
-				if (i == len)
-					at_index = len;
-			}
-		}
-
-		for(int i = 0; i < inp_num_events(); i++)
-		{
-			len = strlen(str);
-			LINEINPUT::manipulate(inp_get_event(i), str, str_size, &len, &at_index);
-		}
+		Textbox.y -= FontSize/10.f;
 		
-		r = 1;
-	}
-
-	bool just_got_active = false;
-	
-	if(UI()->ActiveItem() == id)
-	{
-		if(!UI()->MouseButton(0))
-			UI()->SetActiveItem(0);
-	}
-	else if(UI()->HotItem() == id)
-	{
-		if(UI()->MouseButton(0))
-		{
-			if (UI()->LastActiveItem() != id)
-				just_got_active = true;
-			UI()->SetActiveItem(id);
-		}
-	}
-	
-	if(inside)
-		UI()->SetHotItem(id);
-
-	CUIRect textbox = *rect;
-	RenderTools()->DrawUIRect(&textbox, vec4(1,1,1,0.5f), CUI::CORNER_ALL, 5.0f);
-	textbox.VMargin(5.0f, &textbox);
-	
-	const char *display_str = str;
-	char stars[128];
-	
-	if(hidden)
-	{
-		unsigned s = strlen(str);
-		if(s >= sizeof(stars))
-			s = sizeof(stars)-1;
-		memset(stars, '*', s);
-		stars[s] = 0;
-		display_str = stars;
-	}
-
-	UI()->DoLabel(&textbox, display_str, font_size, -1);
-	
-	if (UI()->LastActiveItem() == id && !just_got_active)
-	{
-		float w = gfx_text_width(0, font_size, display_str, at_index);
-		textbox.x += w*UI()->Scale();
-		UI()->DoLabel(&textbox, "_", font_size, -1);
+		UI()->DoLabel(&Textbox, "|", FontSize*1.1f, -1);
 	}
 
-	return r;
+	return ReturnValue;
 }
-*/
 
-vec4 EDITOR::button_color_mul(const void *id)
+vec4 CEditor::ButtonColorMul(const void *pId)
 {
-	if(UI()->ActiveItem() == id)
+	if(UI()->ActiveItem() == pId)
 		return vec4(1,1,1,0.5f);
-	else if(UI()->HotItem() == id)
+	else if(UI()->HotItem() == pId)
 		return vec4(1,1,1,1.5f);
 	return vec4(1,1,1,1);
 }
 
-float EDITOR::ui_do_scrollbar_v(const void *id, const CUIRect *rect, float current)
+float CEditor::UiDoScrollbarV(const void *pId, const CUIRect *pRect, float Current)
 {
-	CUIRect handle;
-	static float offset_y;
-	rect->HSplitTop(33, &handle, 0);
+	CUIRect Handle;
+	static float s_OffsetY;
+	pRect->HSplitTop(33, &Handle, 0);
 
-	handle.y += (rect->h-handle.h)*current;
+	Handle.y += (pRect->h-Handle.h)*Current;
 
-	/* logic */
-    float ret = current;
-    int inside = UI()->MouseInside(&handle);
+	// logic
+    float Ret = Current;
+    int Inside = UI()->MouseInside(&Handle);
 
-	if(UI()->ActiveItem() == id)
+	if(UI()->ActiveItem() == pId)
 	{
 		if(!UI()->MouseButton(0))
 			UI()->SetActiveItem(0);
-		
-		float min = rect->y;
-		float max = rect->h-handle.h;
-		float cur = UI()->MouseY()-offset_y;
-		ret = (cur-min)/max;
-		if(ret < 0.0f) ret = 0.0f;
-		if(ret > 1.0f) ret = 1.0f;
+
+		float Min = pRect->y;
+		float Max = pRect->h-Handle.h;
+		float Cur = UI()->MouseY()-s_OffsetY;
+		Ret = (Cur-Min)/Max;
+		if(Ret < 0.0f) Ret = 0.0f;
+		if(Ret > 1.0f) Ret = 1.0f;
 	}
-	else if(UI()->HotItem() == id)
+	else if(UI()->HotItem() == pId)
 	{
 		if(UI()->MouseButton(0))
 		{
-			UI()->SetActiveItem(id);
-			offset_y = UI()->MouseY()-handle.y;
+			UI()->SetActiveItem(pId);
+			s_OffsetY = UI()->MouseY()-Handle.y;
 		}
 	}
-	
-	if(inside)
-		UI()->SetHotItem(id);
+
+	if(Inside)
+		UI()->SetHotItem(pId);
 
 	// render
-	CUIRect rail;
-	rect->VMargin(5.0f, &rail);
-	RenderTools()->DrawUIRect(&rail, vec4(1,1,1,0.25f), 0, 0.0f);
-
-	CUIRect slider = handle;
-	slider.w = rail.x-slider.x;
-	RenderTools()->DrawUIRect(&slider, vec4(1,1,1,0.25f), CUI::CORNER_L, 2.5f);
-	slider.x = rail.x+rail.w;
-	RenderTools()->DrawUIRect(&slider, vec4(1,1,1,0.25f), CUI::CORNER_R, 2.5f);
-
-	slider = handle;
-	slider.Margin(5.0f, &slider);
-	RenderTools()->DrawUIRect(&slider, vec4(1,1,1,0.25f)*button_color_mul(id), CUI::CORNER_ALL, 2.5f);
-	
-    return ret;
+	CUIRect Rail;
+	pRect->VMargin(5.0f, &Rail);
+	RenderTools()->DrawUIRect(&Rail, vec4(1,1,1,0.25f), 0, 0.0f);
+
+	CUIRect Slider = Handle;
+	Slider.w = Rail.x-Slider.x;
+	RenderTools()->DrawUIRect(&Slider, vec4(1,1,1,0.25f), CUI::CORNER_L, 2.5f);
+	Slider.x = Rail.x+Rail.w;
+	RenderTools()->DrawUIRect(&Slider, vec4(1,1,1,0.25f), CUI::CORNER_R, 2.5f);
+
+	Slider = Handle;
+	Slider.Margin(5.0f, &Slider);
+	RenderTools()->DrawUIRect(&Slider, vec4(1,1,1,0.25f)*ButtonColorMul(pId), CUI::CORNER_ALL, 2.5f);
+
+    return Ret;
 }
 
-vec4 EDITOR::get_button_color(const void *id, int checked)
+vec4 CEditor::GetButtonColor(const void *pId, int Checked)
 {
-	if(checked < 0)
+	if(Checked < 0)
 		return vec4(0,0,0,0.5f);
-		
-	if(checked > 0)
+
+	if(Checked > 0)
 	{
-		if(UI()->HotItem() == id)
+		if(UI()->HotItem() == pId)
 			return vec4(1,0,0,0.75f);
 		return vec4(1,0,0,0.5f);
 	}
-	
-	if(UI()->HotItem() == id)
+
+	if(UI()->HotItem() == pId)
 		return vec4(1,1,1,0.75f);
 	return vec4(1,1,1,0.5f);
 }
 
-int EDITOR::DoButton_Editor_Common(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
+int CEditor::DoButton_Editor_Common(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
 {
 	if(UI()->MouseInside(pRect))
 	{
 		if(Flags&BUTTON_CONTEXT)
-			ui_got_context = pID;
-		if(tooltip)
-			tooltip = pToolTip;
+			ms_pUiGotContext = pID;
+		if(m_pTooltip)
+			m_pTooltip = pToolTip;
 	}
-	
+
 	if(UI()->HotItem() == pID && pToolTip)
-		tooltip = (const char *)pToolTip;
-	
+		m_pTooltip = (const char *)pToolTip;
+
 	return UI()->DoButtonLogic(pID, pText, Checked, pRect);
 
 	// Draw here
@@ -459,444 +373,399 @@ int EDITOR::DoButton_Editor_Common(const void *pID, const char *pText, int Check
 }
 
 
-int EDITOR::DoButton_Editor(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
+int CEditor::DoButton_Editor(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
 {
-	RenderTools()->DrawUIRect(pRect, get_button_color(pID, Checked), CUI::CORNER_ALL, 3.0f);
-	UI()->DoLabel(pRect, pText, 10, 0, -1);
+	RenderTools()->DrawUIRect(pRect, GetButtonColor(pID, Checked), CUI::CORNER_ALL, 3.0f);
+    CUIRect NewRect = *pRect;
+    NewRect.y += NewRect.h/2.0f-7.0f;
+    UI()->DoLabel(&NewRect, pText, 10, 0, -1);
 	return DoButton_Editor_Common(pID, pText, Checked, pRect, Flags, pToolTip);
 }
 
-int EDITOR::DoButton_File(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
+int CEditor::DoButton_File(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
 {
 	if(UI()->HotItem() == pID)
-		RenderTools()->DrawUIRect(pRect, get_button_color(pID, Checked), CUI::CORNER_ALL, 3.0f);
-	
+		RenderTools()->DrawUIRect(pRect, GetButtonColor(pID, Checked), CUI::CORNER_ALL, 3.0f);
+
 	CUIRect t = *pRect;
 	t.VMargin(5.0f, &t);
 	UI()->DoLabel(&t, pText, 10, -1, -1);
 	return DoButton_Editor_Common(pID, pText, Checked, pRect, Flags, pToolTip);
 }
 
-//static void draw_editor_button_menu(const void *id, const char *text, int checked, const CUIRect *rect, const void *extra)
-int EDITOR::DoButton_Menu(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
+int CEditor::DoButton_Menu(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
 {
-	/*
-	if(UI()->HotItem() == id) if(extra) editor.tooltip = (const char *)extra;
-	if(UI()->HotItem() == id)
-		RenderTools()->DrawUIRect(r, get_button_color(id, checked), CUI::CORNER_ALL, 3.0f);
-	*/
-
 	CUIRect r = *pRect;
-	/*
-	if(ui_popups[id == id)
-	{
-		RenderTools()->DrawUIRect(&r, vec4(0.5f,0.5f,0.5f,0.75f), CUI::CORNER_T, 3.0f);
-		r.Margin(1.0f, &r);
-		RenderTools()->DrawUIRect(&r, vec4(0,0,0,0.75f), CUI::CORNER_T, 3.0f);
-	}
-	else*/
-		RenderTools()->DrawUIRect(&r, vec4(0.5f,0.5f,0.5f, 1.0f), CUI::CORNER_T, 3.0f);
-	
+    RenderTools()->DrawUIRect(&r, vec4(0.5f, 0.5f, 0.5f, 1.0f), CUI::CORNER_T, 3.0f);
 
 	r = *pRect;
 	r.VMargin(5.0f, &r);
 	UI()->DoLabel(&r, pText, 10, -1, -1);
-	
 	return DoButton_Editor_Common(pID, pText, Checked, pRect, Flags, pToolTip);
-	
-	//CUIRect t = *r;
 }
 
-int EDITOR::DoButton_MenuItem(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
+int CEditor::DoButton_MenuItem(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
 {
 	if(UI()->HotItem() == pID || Checked)
-		RenderTools()->DrawUIRect(pRect, get_button_color(pID, Checked), CUI::CORNER_ALL, 3.0f);
-	
+		RenderTools()->DrawUIRect(pRect, GetButtonColor(pID, Checked), CUI::CORNER_ALL, 3.0f);
+
 	CUIRect t = *pRect;
 	t.VMargin(5.0f, &t);
 	UI()->DoLabel(&t, pText, 10, -1, -1);
 	return DoButton_Editor_Common(pID, pText, Checked, pRect, 0, 0);
 }
 
-int EDITOR::DoButton_ButtonL(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
-{
-	RenderTools()->DrawUIRect(pRect, get_button_color(pID, Checked), CUI::CORNER_L, 3.0f);
-	UI()->DoLabel(pRect, pText, 10, 0, -1);
-	return DoButton_Editor_Common(pID, pText, Checked, pRect, Flags, pToolTip);
-}
-
-int EDITOR::DoButton_ButtonM(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
+int CEditor::DoButton_Tab(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
 {
-	RenderTools()->DrawUIRect(pRect, get_button_color(pID, Checked), 0, 3.0f);
-	UI()->DoLabel(pRect, pText, 10, 0, -1);
+	RenderTools()->DrawUIRect(pRect, GetButtonColor(pID, Checked), CUI::CORNER_T, 5.0f);
+    CUIRect NewRect = *pRect;
+    NewRect.y += NewRect.h/2.0f-7.0f;
+    UI()->DoLabel(&NewRect, pText, 10, 0, -1);
 	return DoButton_Editor_Common(pID, pText, Checked, pRect, Flags, pToolTip);
 }
 
-int EDITOR::DoButton_ButtonR(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
+int CEditor::DoButton_Ex(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip, int Corners)
 {
-	RenderTools()->DrawUIRect(pRect, get_button_color(pID, Checked), CUI::CORNER_R, 3.0f);
-	UI()->DoLabel(pRect, pText, 10, 0, -1);
+	RenderTools()->DrawUIRect(pRect, GetButtonColor(pID, Checked), Corners, 3.0f);
+    CUIRect NewRect = *pRect;
+    NewRect.y += NewRect.h/2.0f-7.0f;
+    UI()->DoLabel(&NewRect, pText, 10, 0, -1);
 	return DoButton_Editor_Common(pID, pText, Checked, pRect, Flags, pToolTip);
 }
 
-int EDITOR::DoButton_ButtonInc(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
+int CEditor::DoButton_ButtonInc(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
 {
-	RenderTools()->DrawUIRect(pRect, get_button_color(pID, Checked), CUI::CORNER_R, 3.0f);
-	UI()->DoLabel(pRect, pText?pText:">", 10, 0, -1);
+	RenderTools()->DrawUIRect(pRect, GetButtonColor(pID, Checked), CUI::CORNER_R, 3.0f);
+	UI()->DoLabel(pRect, pText?pText:"+", 10, 0, -1);
 	return DoButton_Editor_Common(pID, pText, Checked, pRect, Flags, pToolTip);
 }
 
-int EDITOR::DoButton_ButtonDec(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
+int CEditor::DoButton_ButtonDec(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip)
 {
-	RenderTools()->DrawUIRect(pRect, get_button_color(pID, Checked), CUI::CORNER_L, 3.0f);
-	UI()->DoLabel(pRect, pText?pText:"<", 10, 0, -1);
+	RenderTools()->DrawUIRect(pRect, GetButtonColor(pID, Checked), CUI::CORNER_L, 3.0f);
+	UI()->DoLabel(pRect, pText?pText:"-", 10, 0, -1);
 	return DoButton_Editor_Common(pID, pText, Checked, pRect, Flags, pToolTip);
 }
 
-/*
-static void draw_editor_button_l(const void *id, const char *text, int checked, const CUIRect *r, const void *extra)
-{
-	RenderTools()->DrawUIRect(r, get_button_color(id, checked), CUI::CORNER_L, 3.0f);
-	UI()->DoLabel(r, text, 10, 0, -1);
-}
-
-static void draw_editor_button_m(const void *id, const char *text, int checked, const CUIRect *r, const void *extra)
-{
-	if(UI()->HotItem() == id) if(extra) editor.tooltip = (const char *)extra;
-	RenderTools()->DrawUIRect(r, get_button_color(id, checked), 0, 3.0f);
-	UI()->DoLabel(r, text, 10, 0, -1);
-}
-
-static void draw_editor_button_r(const void *id, const char *text, int checked, const CUIRect *r, const void *extra)
-{
-	if(UI()->HotItem() == id) if(extra) editor.tooltip = (const char *)extra;
-	RenderTools()->DrawUIRect(r, get_button_color(id, checked), CUI::CORNER_R, 3.0f);
-	UI()->DoLabel(r, text, 10, 0, -1);
-}
-
-static void draw_inc_button(const void *id, const char *text, int checked, const CUIRect *r, const void *extra)
+void CEditor::RenderBackground(CUIRect View, int Texture, float Size, float Brightness)
 {
-	if(UI()->HotItem == id) if(extra) editor.tooltip = (const char *)extra;
-	RenderTools()->DrawUIRect(r, get_button_color(id, checked), CUI::CORNER_R, 3.0f);
-	UI()->DoLabel(r, text?text:">", 10, 0, -1);
-}
-
-static void draw_dec_button(const void *id, const char *text, int checked, const CUIRect *r, const void *extra)
-{
-	if(UI()->HotItem == id) if(extra) editor.tooltip = (const char *)extra;
-	RenderTools()->DrawUIRect(r, get_button_color(id, checked), CUI::CORNER_L, 3.0f);
-	UI()->DoLabel(r, text?text:"<", 10, 0, -1);
-}
-
-int do_editor_button(const void *id, const char *text, int checked, const CUIRect *r, ui_draw_button_func draw_func, int flags, const char *tooltip)
-{
-	if(UI()->MouseInside(r))
-	{
-		if(flags&BUTTON_CONTEXT)
-			ui_got_context = id;
-		if(tooltip)
-			editor.tooltip = tooltip;
-	}
-	
-	return UI()->DoButton(id, text, checked, r, draw_func, 0);
-}*/
-
-
-void EDITOR::render_background(CUIRect view, int texture, float size, float brightness)
-{
-	Graphics()->TextureSet(texture);
+	Graphics()->TextureSet(Texture);
 	Graphics()->BlendNormal();
 	Graphics()->QuadsBegin();
-	Graphics()->SetColor(brightness,brightness,brightness,1.0f);
-	Graphics()->QuadsSetSubset(0,0, view.w/size, view.h/size);
-	Graphics()->QuadsDrawTL(view.x, view.y, view.w, view.h);
+	Graphics()->SetColor(Brightness, Brightness, Brightness, 1.0f);
+	Graphics()->QuadsSetSubset(0,0, View.w/Size, View.h/Size);
+	IGraphics::CQuadItem QuadItem(View.x, View.y, View.w, View.h);
+	Graphics()->QuadsDrawTL(&QuadItem, 1);
 	Graphics()->QuadsEnd();
 }
 
-static LAYERGROUP brush;
-static LAYER_TILES tileset_picker(16, 16);
-
-int EDITOR::ui_do_value_selector(void *id, CUIRect *r, const char *label, int current, int min, int max, float scale)
+int CEditor::UiDoValueSelector(void *pId, CUIRect *r, const char *pLabel, int Current, int Min, int Max, float Scale)
 {
-    /* logic */
-    static float value;
-    int ret = 0;
-    int inside = UI()->MouseInside(r);
+    // logic
+    static float s_Value;
+    int Ret = 0;
+    int Inside = UI()->MouseInside(r);
 
-	if(UI()->ActiveItem() == id)
+	if(UI()->ActiveItem() == pId)
 	{
 		if(!UI()->MouseButton(0))
 		{
-			if(inside)
-				ret = 1;
-			lock_mouse = false;
+			if(Inside)
+				Ret = 1;
+			m_LockMouse = false;
 			UI()->SetActiveItem(0);
 		}
 		else
 		{
-			if(inp_key_pressed(KEY_LSHIFT) || inp_key_pressed(KEY_RSHIFT))
-				value += mouse_delta_x*0.05f;
+			if(Input()->KeyPressed(KEY_LSHIFT) || Input()->KeyPressed(KEY_RSHIFT))
+				s_Value += m_MouseDeltaX*0.05f;
 			else
-				value += mouse_delta_x;
-			
-			if(fabs(value) > scale)
+				s_Value += m_MouseDeltaX;
+
+			if(absolute(s_Value) > Scale)
 			{
-				int count = (int)(value/scale);
-				value = fmod(value, scale);
-				current += count;
-				if(current < min)
-					current = min;
-				if(current > max)
-					current = max;
+				int Count = (int)(s_Value/Scale);
+				s_Value = fmod(s_Value, Scale);
+				Current += Count;
+				if(Current < Min)
+					Current = Min;
+				if(Current > Max)
+					Current = Max;
 			}
 		}
 	}
-	else if(UI()->HotItem() == id)
+	else if(UI()->HotItem() == pId)
 	{
 		if(UI()->MouseButton(0))
 		{
-			lock_mouse = true;
-			value = 0;
-			UI()->SetActiveItem(id);
+			m_LockMouse = true;
+			s_Value = 0;
+			UI()->SetActiveItem(pId);
 		}
 	}
-	
-	if(inside)
-		UI()->SetHotItem(id);
+
+	if(Inside)
+		UI()->SetHotItem(pId);
 
 	// render
-	char buf[128];
-	sprintf(buf, "%s %d", label, current);
-	RenderTools()->DrawUIRect(r, get_button_color(id, 0), CUI::CORNER_ALL, 5.0f);
-	UI()->DoLabel(r, buf, 10, 0, -1);
-	return current;
+	char aBuf[128];
+	str_format(aBuf, sizeof(aBuf),"%s %d", pLabel, Current);
+	RenderTools()->DrawUIRect(r, GetButtonColor(pId, 0), CUI::CORNER_ALL, 5.0f);
+    r->y += r->h/2.0f-7.0f;
+    UI()->DoLabel(r, aBuf, 10, 0, -1);
+    
+	return Current;
 }
 
-LAYERGROUP *EDITOR::get_selected_group()
+CLayerGroup *CEditor::GetSelectedGroup()
 {
-	if(selected_group >= 0 && selected_group < map.groups.len())
-		return map.groups[selected_group];
+	if(m_SelectedGroup >= 0 && m_SelectedGroup < m_Map.m_lGroups.size())
+		return m_Map.m_lGroups[m_SelectedGroup];
 	return 0x0;
 }
 
-LAYER *EDITOR::get_selected_layer(int index)
+CLayer *CEditor::GetSelectedLayer(int Index)
 {
-	LAYERGROUP *group = get_selected_group();
-	if(!group)
+	CLayerGroup *pGroup = GetSelectedGroup();
+	if(!pGroup)
 		return 0x0;
 
-	if(selected_layer >= 0 && selected_layer < map.groups[selected_group]->layers.len())
-		return group->layers[selected_layer];
+	if(m_SelectedLayer >= 0 && m_SelectedLayer < m_Map.m_lGroups[m_SelectedGroup]->m_lLayers.size())
+		return pGroup->m_lLayers[m_SelectedLayer];
 	return 0x0;
 }
 
-LAYER *EDITOR::get_selected_layer_type(int index, int type)
+CLayer *CEditor::GetSelectedLayerType(int Index, int Type)
 {
-	LAYER *p = get_selected_layer(index);
-	if(p && p->type == type)
+	CLayer *p = GetSelectedLayer(Index);
+	if(p && p->m_Type == Type)
 		return p;
 	return 0x0;
 }
 
-QUAD *EDITOR::get_selected_quad()
+CQuad *CEditor::GetSelectedQuad()
 {
-	LAYER_QUADS *ql = (LAYER_QUADS *)get_selected_layer_type(0, LAYERTYPE_QUADS);
+	CLayerQuads *ql = (CLayerQuads *)GetSelectedLayerType(0, LAYERTYPE_QUADS);
 	if(!ql)
 		return 0;
-	if(selected_quad >= 0 && selected_quad < ql->quads.len())
-		return &ql->quads[selected_quad];
+	if(m_SelectedQuad >= 0 && m_SelectedQuad < ql->m_lQuads.size())
+		return &ql->m_lQuads[m_SelectedQuad];
 	return 0;
 }
 
-static void callback_open_map(const char *filename, void *user) { ((EDITOR*)user)->load(filename); }
-static void callback_append_map(const char *filename, void *user) { ((EDITOR*)user)->append(filename); }
-static void callback_save_map(const char *filename, void *user) { ((EDITOR*)user)->save(filename); }
+static void CallbackOpenMap(const char *pFileName, void *pUser) { if(((CEditor*)pUser)->Load(pFileName)) str_copy(((CEditor*)pUser)->m_aFileName, pFileName, 512); }
+static void CallbackAppendMap(const char *pFileName, void *pUser) { if(((CEditor*)pUser)->Append(pFileName)) ((CEditor*)pUser)->m_aFileName[0] = 0; }
+static void CallbackSaveMap(const char *pFileName, void *pUser){ if(((CEditor*)pUser)->Save(pFileName)) str_copy(((CEditor*)pUser)->m_aFileName, pFileName, 512); }
 
-void EDITOR::do_toolbar(CUIRect toolbar)
+void CEditor::DoToolbar(CUIRect ToolBar)
 {
-	CUIRect button;
+	CUIRect TB_Top, TB_Bottom;
+	CUIRect Button;
 	
-	// ctrl+o to open
-	if(inp_key_down('o') && (inp_key_pressed(KEY_LCTRL) || inp_key_pressed(KEY_RCTRL)))
-		invoke_file_dialog(LISTDIRTYPE_ALL, "Open Map", "Open", "maps/", "", callback_open_map, this);
+	ToolBar.HSplitTop(ToolBar.h/2.0f, &TB_Top, &TB_Bottom);
 	
+    TB_Top.HSplitBottom(2.5f, &TB_Top, 0);
+    TB_Bottom.HSplitTop(2.5f, 0, &TB_Bottom);
+
+	// ctrl+o to open
+	if(Input()->KeyDown('o') && (Input()->KeyPressed(KEY_LCTRL) || Input()->KeyPressed(KEY_RCTRL)))
+		InvokeFileDialog(IStorage::TYPE_ALL, "Open Map", "Open", "maps/", "", CallbackOpenMap, this);
+
 	// ctrl+s to save
-	if(inp_key_down('s') && (inp_key_pressed(KEY_LCTRL) || inp_key_pressed(KEY_RCTRL)))
-		invoke_file_dialog(LISTDIRTYPE_SAVE, "Save Map", "Save", "maps/", "", callback_save_map, this);
+	if(Input()->KeyDown('s') && (Input()->KeyPressed(KEY_LCTRL) || Input()->KeyPressed(KEY_RCTRL)))
+	{
+		if(m_aFileName[0])	
+			Save(m_aFileName);
+		else
+			InvokeFileDialog(IStorage::TYPE_SAVE, "Save Map", "Save", "maps/", "", CallbackSaveMap, this);
+	}
 
 	// detail button
-	toolbar.VSplitLeft(30.0f, &button, &toolbar);
-	static int hq_button = 0;
-	if(DoButton_Editor(&hq_button, "Detail", show_detail, &button, 0, "[ctrl+h] Toggle High Detail") ||
-		(inp_key_down('h') && (inp_key_pressed(KEY_LCTRL) || inp_key_pressed(KEY_RCTRL))))
+	TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+	static int s_HqButton = 0;
+	if(DoButton_Editor(&s_HqButton, "HD", m_ShowDetail, &Button, 0, "[ctrl+h] Toggle High Detail") ||
+		(Input()->KeyDown('h') && (Input()->KeyPressed(KEY_LCTRL) || Input()->KeyPressed(KEY_RCTRL))))
 	{
-		show_detail = !show_detail;
+		m_ShowDetail = !m_ShowDetail;
 	}
 
-	toolbar.VSplitLeft(5.0f, 0, &toolbar);
-	
+	TB_Top.VSplitLeft(5.0f, 0, &TB_Top);
+
 	// animation button
-	toolbar.VSplitLeft(30.0f, &button, &toolbar);
-	static int animate_button = 0;
-	if(DoButton_Editor(&animate_button, "Anim", animate, &button, 0, "[ctrl+m] Toggle animation") ||
-		(inp_key_down('m') && (inp_key_pressed(KEY_LCTRL) || inp_key_pressed(KEY_RCTRL))))
+	TB_Top.VSplitLeft(40.0f, &Button, &TB_Top);
+	static int s_AnimateButton = 0;
+	if(DoButton_Editor(&s_AnimateButton, "Anim", m_Animate, &Button, 0, "[ctrl+m] Toggle animation") ||
+		(Input()->KeyDown('m') && (Input()->KeyPressed(KEY_LCTRL) || Input()->KeyPressed(KEY_RCTRL))))
 	{
-		animate_start = time_get();
-		animate = !animate;
+		m_AnimateStart = time_get();
+		m_Animate = !m_Animate;
 	}
 
-	toolbar.VSplitLeft(5.0f, 0, &toolbar);
+	TB_Top.VSplitLeft(5.0f, 0, &TB_Top);
 
 	// proof button
-	toolbar.VSplitLeft(30.0f, &button, &toolbar);
-	static int proof_button = 0;
-	if(DoButton_Editor(&proof_button, "Proof", proof_borders, &button, 0, "[ctrl-p] Toggles proof borders. These borders represent what a player maximum can see.") ||
-		(inp_key_down('p') && (inp_key_pressed(KEY_LCTRL) || inp_key_pressed(KEY_RCTRL))))
+	TB_Top.VSplitLeft(40.0f, &Button, &TB_Top);
+	static int s_ProofButton = 0;
+	if(DoButton_Editor(&s_ProofButton, "Proof", m_ProofBorders, &Button, 0, "[ctrl-p] Toggles proof borders. These borders represent what a player maximum can see.") ||
+		(Input()->KeyDown('p') && (Input()->KeyPressed(KEY_LCTRL) || Input()->KeyPressed(KEY_RCTRL))))
 	{
-		proof_borders = !proof_borders;
+		m_ProofBorders = !m_ProofBorders;
 	}
 
-	toolbar.VSplitLeft(15.0f, 0, &toolbar);
-	
+	TB_Top.VSplitLeft(15.0f, 0, &TB_Top);
+
 	// zoom group
-	toolbar.VSplitLeft(16.0f, &button, &toolbar);
-	static int zoom_out_button = 0;
-	if(DoButton_ButtonL(&zoom_out_button, "ZO", 0, &button, 0, "[NumPad-] Zoom out") || inp_key_down(KEY_KP_MINUS))
-		zoom_level += 50;
-		
-	toolbar.VSplitLeft(16.0f, &button, &toolbar);
-	static int zoom_normal_button = 0;
-	if(DoButton_ButtonM(&zoom_normal_button, "1:1", 0, &button, 0, "[NumPad*] Zoom to normal and remove editor offset") || inp_key_down(KEY_KP_MULTIPLY))
+	TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+	static int s_ZoomOutButton = 0;
+	if(DoButton_Ex(&s_ZoomOutButton, "ZO", 0, &Button, 0, "[NumPad-] Zoom out", CUI::CORNER_L) || Input()->KeyDown(KEY_KP_MINUS))
+		m_ZoomLevel += 50;
+
+	TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+	static int s_ZoomNormalButton = 0;
+	if(DoButton_Ex(&s_ZoomNormalButton, "1:1", 0, &Button, 0, "[NumPad*] Zoom to normal and remove editor offset", 0) || Input()->KeyDown(KEY_KP_MULTIPLY))
 	{
-		editor_offset_x = 0;
-		editor_offset_y = 0;
-		zoom_level = 100;
+		m_EditorOffsetX = 0;
+		m_EditorOffsetY = 0;
+		m_ZoomLevel = 100;
 	}
-		
-	toolbar.VSplitLeft(16.0f, &button, &toolbar);
-	static int zoom_in_button = 0;
-	if(DoButton_ButtonR(&zoom_in_button, "ZI", 0, &button, 0, "[NumPad+] Zoom in") || inp_key_down(KEY_KP_PLUS))
-		zoom_level -= 50;
-	
-	toolbar.VSplitLeft(15.0f, 0, &toolbar);
-	
+
+	TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+	static int s_ZoomInButton = 0;
+	if(DoButton_Ex(&s_ZoomInButton, "ZI", 0, &Button, 0, "[NumPad+] Zoom in", CUI::CORNER_R) || Input()->KeyDown(KEY_KP_PLUS))
+		m_ZoomLevel -= 50;
+
+	TB_Top.VSplitLeft(10.0f, 0, &TB_Top);
+
 	// animation speed
-	toolbar.VSplitLeft(16.0f, &button, &toolbar);
-	static int anim_faster_button = 0;
-	if(DoButton_ButtonL(&anim_faster_button, "A+", 0, &button, 0, "Increase animation speed"))
-		animate_speed += 0.5f;
-	
-	toolbar.VSplitLeft(16.0f, &button, &toolbar);
-	static int anim_normal_button = 0;
-	if(DoButton_ButtonM(&anim_normal_button, "1", 0, &button, 0, "Normal animation speed"))
-		animate_speed = 1.0f;
-	
-	toolbar.VSplitLeft(16.0f, &button, &toolbar);
-	static int anim_slower_button = 0;
-	if(DoButton_ButtonR(&anim_slower_button, "A-", 0, &button, 0, "Decrease animation speed"))
+	TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+	static int s_AnimFasterButton = 0;
+	if(DoButton_Ex(&s_AnimFasterButton, "A+", 0, &Button, 0, "Increase animation speed", CUI::CORNER_L))
+		m_AnimateSpeed += 0.5f;
+
+	TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+	static int s_AnimNormalButton = 0;
+	if(DoButton_Ex(&s_AnimNormalButton, "1", 0, &Button, 0, "Normal animation speed", 0))
+		m_AnimateSpeed = 1.0f;
+
+	TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+	static int s_AnimSlowerButton = 0;
+	if(DoButton_Ex(&s_AnimSlowerButton, "A-", 0, &Button, 0, "Decrease animation speed", CUI::CORNER_R))
 	{
-		if(animate_speed > 0.5f)
-			animate_speed -= 0.5f;
+		if(m_AnimateSpeed > 0.5f)
+			m_AnimateSpeed -= 0.5f;
 	}
-	
-	if(inp_key_presses(KEY_MOUSE_WHEEL_UP) && dialog == DIALOG_NONE)
-		zoom_level -= 20;
-		
-	if(inp_key_presses(KEY_MOUSE_WHEEL_DOWN) && dialog == DIALOG_NONE)
-		zoom_level += 20;
-	
-	if(zoom_level < 50)
-		zoom_level = 50;
-	world_zoom = zoom_level/100.0f;
 
-	toolbar.VSplitLeft(10.0f, &button, &toolbar);
+	if(Input()->KeyPresses(KEY_MOUSE_WHEEL_UP) && m_Dialog == DIALOG_NONE)
+		m_ZoomLevel -= 20;
+
+	if(Input()->KeyPresses(KEY_MOUSE_WHEEL_DOWN) && m_Dialog == DIALOG_NONE)
+		m_ZoomLevel += 20;
+
+	if(m_ZoomLevel < 50)
+		m_ZoomLevel = 50;
+	m_WorldZoom = m_ZoomLevel/100.0f;
+
+	TB_Top.VSplitLeft(10.0f, &Button, &TB_Top);
 
 
 	// brush manipulation
-	{	
-		int enabled = brush.is_empty()?-1:0;
-		
+	{
+		int Enabled = m_Brush.IsEmpty()?-1:0;
+
 		// flip buttons
-		toolbar.VSplitLeft(20.0f, &button, &toolbar);
-		static int flipx_button = 0;
-		if(DoButton_ButtonL(&flipx_button, "^X", enabled, &button, 0, "[N] Flip brush horizontal") || inp_key_down('n'))
+		TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+		static int s_FlipXButton = 0;
+		if(DoButton_Ex(&s_FlipXButton, "X/X", Enabled, &Button, 0, "[N] Flip brush horizontal", CUI::CORNER_L) || Input()->KeyDown('n'))
 		{
-			for(int i = 0; i < brush.layers.len(); i++)
-				brush.layers[i]->brush_flip_x();
+			for(int i = 0; i < m_Brush.m_lLayers.size(); i++)
+				m_Brush.m_lLayers[i]->BrushFlipX();
 		}
-			
-		toolbar.VSplitLeft(20.0f, &button, &toolbar);
-		static int flipy_button = 0;
-		if(DoButton_ButtonR(&flipy_button, "^Y", enabled, &button, 0, "[M] Flip brush vertical") || inp_key_down('m'))
+
+		TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+		static int s_FlipyButton = 0;
+		if(DoButton_Ex(&s_FlipyButton, "Y/Y", Enabled, &Button, 0, "[M] Flip brush vertical", CUI::CORNER_R) || Input()->KeyDown('m'))
 		{
-			for(int i = 0; i < brush.layers.len(); i++)
-				brush.layers[i]->brush_flip_y();
+			for(int i = 0; i < m_Brush.m_lLayers.size(); i++)
+				m_Brush.m_lLayers[i]->BrushFlipY();
 		}
 
 		// rotate buttons
-		toolbar.VSplitLeft(20.0f, &button, &toolbar);
-		
-		toolbar.VSplitLeft(30.0f, &button, &toolbar);
-		static int rotation_amount = 90;
-		rotation_amount = ui_do_value_selector(&rotation_amount, &button, "", rotation_amount, 1, 360, 2.0f);
-		
-		toolbar.VSplitLeft(5.0f, &button, &toolbar);
-		toolbar.VSplitLeft(30.0f, &button, &toolbar);
-		static int ccw_button = 0;
-		if(DoButton_ButtonL(&ccw_button, "CCW", enabled, &button, 0, "[R] Rotates the brush counter clockwise") || inp_key_down('r'))
+		TB_Top.VSplitLeft(15.0f, &Button, &TB_Top);
+
+		TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+		static int s_RotationAmount = 90;
+		s_RotationAmount = UiDoValueSelector(&s_RotationAmount, &Button, "", s_RotationAmount, 1, 360, 2.0f);
+
+		TB_Top.VSplitLeft(5.0f, &Button, &TB_Top);
+		TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+		static int s_CcwButton = 0;
+		if(DoButton_Ex(&s_CcwButton, "CCW", Enabled, &Button, 0, "[R] Rotates the brush counter clockwise", CUI::CORNER_L) || Input()->KeyDown('r'))
 		{
-			for(int i = 0; i < brush.layers.len(); i++)
-				brush.layers[i]->brush_rotate(-rotation_amount/360.0f*pi*2);
+			for(int i = 0; i < m_Brush.m_lLayers.size(); i++)
+				m_Brush.m_lLayers[i]->BrushRotate(-s_RotationAmount/360.0f*pi*2);
 		}
-			
-		toolbar.VSplitLeft(30.0f, &button, &toolbar);
-		static int cw_button = 0;
-		if(DoButton_ButtonR(&cw_button, "CW", enabled, &button, 0, "[T] Rotates the brush clockwise") || inp_key_down('t'))
+
+		TB_Top.VSplitLeft(30.0f, &Button, &TB_Top);
+		static int s_CwButton = 0;
+		if(DoButton_Ex(&s_CwButton, "CW", Enabled, &Button, 0, "[T] Rotates the brush clockwise", CUI::CORNER_R) || Input()->KeyDown('t'))
 		{
-			for(int i = 0; i < brush.layers.len(); i++)
-				brush.layers[i]->brush_rotate(rotation_amount/360.0f*pi*2);
+			for(int i = 0; i < m_Brush.m_lLayers.size(); i++)
+				m_Brush.m_lLayers[i]->BrushRotate(s_RotationAmount/360.0f*pi*2);
 		}
 	}
 
 	// quad manipulation
 	{
 		// do add button
-		toolbar.VSplitLeft(10.0f, &button, &toolbar);
-		toolbar.VSplitLeft(60.0f, &button, &toolbar);
-		static int new_button = 0;
-		
-		LAYER_QUADS *qlayer = (LAYER_QUADS *)get_selected_layer_type(0, LAYERTYPE_QUADS);
-		//LAYER_TILES *tlayer = (LAYER_TILES *)get_selected_layer_type(0, LAYERTYPE_TILES);
-		if(DoButton_Editor(&new_button, "Add Quad", qlayer?0:-1, &button, 0, "Adds a new quad"))
+		TB_Top.VSplitLeft(10.0f, &Button, &TB_Top);
+		TB_Top.VSplitLeft(60.0f, &Button, &TB_Top);
+		static int s_NewButton = 0;
+
+		CLayerQuads *pQLayer = (CLayerQuads *)GetSelectedLayerType(0, LAYERTYPE_QUADS);
+		//CLayerTiles *tlayer = (CLayerTiles *)get_selected_layer_type(0, LAYERTYPE_TILES);
+		if(DoButton_Editor(&s_NewButton, "Add Quad", pQLayer?0:-1, &Button, 0, "Adds a new quad"))
 		{
-			if(qlayer)
+			if(pQLayer)
 			{
-				float mapping[4];
-				LAYERGROUP *g = get_selected_group();
-				g->mapping(mapping);
-				int add_x = f2fx(mapping[0] + (mapping[2]-mapping[0])/2);
-				int add_y = f2fx(mapping[1] + (mapping[3]-mapping[1])/2);
-				
-				QUAD *q = qlayer->new_quad();
+				float Mapping[4];
+				CLayerGroup *g = GetSelectedGroup();
+				g->Mapping(Mapping);
+				int AddX = f2fx(Mapping[0] + (Mapping[2]-Mapping[0])/2);
+				int AddY = f2fx(Mapping[1] + (Mapping[3]-Mapping[1])/2);
+
+				CQuad *q = pQLayer->NewQuad();
 				for(int i = 0; i < 5; i++)
 				{
-					q->points[i].x += add_x;
-					q->points[i].y += add_y;
+					q->m_aPoints[i].x += AddX;
+					q->m_aPoints[i].y += AddY;
 				}
 			}
 		}
 	}
+    
+	// tile manipulation
+	{
+		TB_Bottom.VSplitLeft(40.0f, &Button, &TB_Bottom);
+		static int s_BorderBut = 0;
+		CLayerTiles *pT = (CLayerTiles *)GetSelectedLayerType(0, LAYERTYPE_TILES);
+		
+		if(DoButton_Editor(&s_BorderBut, "Border", pT?0:-1, &Button, 0, "Border"))
+		{
+			if(pT)
+                DoMapBorder();
+		}
+	}
 }
 
-static void rotate(POINT *center, POINT *point, float rotation)
+static void Rotate(CPoint *pCenter, CPoint *pPoint, float Rotation)
 {
-	int x = point->x - center->x;
-	int y = point->y - center->y;
-	point->x = (int)(x * cosf(rotation) - y * sinf(rotation) + center->x);
-	point->y = (int)(x * sinf(rotation) + y * cosf(rotation) + center->y);
+	int x = pPoint->x - pCenter->x;
+	int y = pPoint->y - pCenter->y;
+	pPoint->x = (int)(x * cosf(Rotation) - y * sinf(Rotation) + pCenter->x);
+	pPoint->y = (int)(x * sinf(Rotation) + y * cosf(Rotation) + pCenter->y);
 }
 
-void EDITOR::do_quad(QUAD *q, int index)
+void CEditor::DoQuad(CQuad *q, int Index)
 {
 	enum
 	{
@@ -906,71 +775,72 @@ void EDITOR::do_quad(QUAD *q, int index)
 		OP_ROTATE,
 		OP_CONTEXT_MENU,
 	};
-	
+
 	// some basic values
-	void *id = &q->points[4]; // use pivot addr as id
-	static POINT rotate_points[4];
-	static float last_wx;
-	static float last_wy;
-	static int operation = OP_NONE;
-	static float rotate_angle = 0;
+	void *pId = &q->m_aPoints[4]; // use pivot addr as id
+	static CPoint s_RotatePoints[4];
+	static float s_LastWx;
+	static float s_LastWy;
+	static int s_Operation = OP_NONE;
+	static float s_RotateAngle = 0;
 	float wx = UI()->MouseWorldX();
 	float wy = UI()->MouseWorldY();
-	
+
 	// get pivot
-	float center_x = fx2f(q->points[4].x);
-	float center_y = fx2f(q->points[4].y);
+	float CenterX = fx2f(q->m_aPoints[4].x);
+	float CenterY = fx2f(q->m_aPoints[4].y);
 
-	float dx = (center_x - wx);
-	float dy = (center_y - wy);
+	float dx = (CenterX - wx);
+	float dy = (CenterY - wy);
 	if(dx*dx+dy*dy < 10*10)
-		UI()->SetHotItem(id);
+		UI()->SetHotItem(pId);
 
-	// draw selection background	
-	if(selected_quad == index)
+	// draw selection background
+	if(m_SelectedQuad == Index)
 	{
 		Graphics()->SetColor(0,0,0,1);
-		Graphics()->QuadsDraw(center_x, center_y, 7.0f, 7.0f);
+		IGraphics::CQuadItem QuadItem(CenterX, CenterY, 7.0f, 7.0f);
+		Graphics()->QuadsDraw(&QuadItem, 1);
 	}
-	
-	if(UI()->ActiveItem() == id)
+
+	if(UI()->ActiveItem() == pId)
 	{
 		// check if we only should move pivot
-		if(operation == OP_MOVE_PIVOT)
+		if(s_Operation == OP_MOVE_PIVOT)
 		{
-			q->points[4].x += f2fx(wx-last_wx);
-			q->points[4].y += f2fx(wy-last_wy);
+			q->m_aPoints[4].x += f2fx(wx-s_LastWx);
+			q->m_aPoints[4].y += f2fx(wy-s_LastWy);
 		}
-		else if(operation == OP_MOVE_ALL)
+		else if(s_Operation == OP_MOVE_ALL)
 		{
 			// move all points including pivot
 			for(int v = 0; v < 5; v++)
 			{
-				q->points[v].x += f2fx(wx-last_wx);
-				q->points[v].y += f2fx(wy-last_wy);
+				q->m_aPoints[v].x += f2fx(wx-s_LastWx);
+				q->m_aPoints[v].y += f2fx(wy-s_LastWy);
 			}
 		}
-		else if(operation == OP_ROTATE)
+		else if(s_Operation == OP_ROTATE)
 		{
 			for(int v = 0; v < 4; v++)
 			{
-				q->points[v] = rotate_points[v];
-				rotate(&q->points[4], &q->points[v], rotate_angle);
+				q->m_aPoints[v] = s_RotatePoints[v];
+				Rotate(&q->m_aPoints[4], &q->m_aPoints[v], s_RotateAngle);
 			}
 		}
-		
-		rotate_angle += (mouse_delta_x) * 0.002f;
-		last_wx = wx;
-		last_wy = wy;
-		
-		if(operation == OP_CONTEXT_MENU)
+
+		s_RotateAngle += (m_MouseDeltaX) * 0.002f;
+		s_LastWx = wx;
+		s_LastWy = wy;
+
+		if(s_Operation == OP_CONTEXT_MENU)
 		{
 			if(!UI()->MouseButton(1))
 			{
-				static int quad_popup_id = 0;
-				ui_invoke_popup_menu(&quad_popup_id, 0, UI()->MouseX(), UI()->MouseY(), 120, 150, popup_quad);
-				lock_mouse = false;
-				operation = OP_NONE;
+				static int s_QuadPopupId = 0;
+				UiInvokePopupMenu(&s_QuadPopupId, 0, UI()->MouseX(), UI()->MouseY(), 120, 150, PopupQuad);
+				m_LockMouse = false;
+				s_Operation = OP_NONE;
 				UI()->SetActiveItem(0);
 			}
 		}
@@ -978,79 +848,81 @@ void EDITOR::do_quad(QUAD *q, int index)
 		{
 			if(!UI()->MouseButton(0))
 			{
-				lock_mouse = false;
-				operation = OP_NONE;
+				m_LockMouse = false;
+				s_Operation = OP_NONE;
 				UI()->SetActiveItem(0);
 			}
-		}			
+		}
 
 		Graphics()->SetColor(1,1,1,1);
 	}
-	else if(UI()->HotItem() == id)
+	else if(UI()->HotItem() == pId)
 	{
-		ui_got_context = id;
-		
+		ms_pUiGotContext = pId;
+
 		Graphics()->SetColor(1,1,1,1);
-		tooltip = "Left mouse button to move. Hold shift to move pivot. Hold ctrl to rotate";
-		
+		m_pTooltip = "Left mouse button to move. Hold shift to move pivot. Hold ctrl to rotate";
+
 		if(UI()->MouseButton(0))
 		{
-			if(inp_key_pressed(KEY_LSHIFT) || inp_key_pressed(KEY_RSHIFT))
-				operation = OP_MOVE_PIVOT;
-			else if(inp_key_pressed(KEY_LCTRL) || inp_key_pressed(KEY_RCTRL))
+			if(Input()->KeyPressed(KEY_LSHIFT) || Input()->KeyPressed(KEY_RSHIFT))
+				s_Operation = OP_MOVE_PIVOT;
+			else if(Input()->KeyPressed(KEY_LCTRL) || Input()->KeyPressed(KEY_RCTRL))
 			{
-				lock_mouse = true;
-				operation = OP_ROTATE;
-				rotate_angle = 0;
-				rotate_points[0] = q->points[0];
-				rotate_points[1] = q->points[1];
-				rotate_points[2] = q->points[2];
-				rotate_points[3] = q->points[3];
+				m_LockMouse = true;
+				s_Operation = OP_ROTATE;
+				s_RotateAngle = 0;
+				s_RotatePoints[0] = q->m_aPoints[0];
+				s_RotatePoints[1] = q->m_aPoints[1];
+				s_RotatePoints[2] = q->m_aPoints[2];
+				s_RotatePoints[3] = q->m_aPoints[3];
 			}
 			else
-				operation = OP_MOVE_ALL;
-				
-			UI()->SetActiveItem(id);
-			selected_quad = index;
-			last_wx = wx;
-			last_wy = wy;
+				s_Operation = OP_MOVE_ALL;
+
+			UI()->SetActiveItem(pId);
+			m_SelectedQuad = Index;
+			s_LastWx = wx;
+			s_LastWy = wy;
 		}
-		
+
 		if(UI()->MouseButton(1))
 		{
-			selected_quad = index;
-			operation = OP_CONTEXT_MENU;
-			UI()->SetActiveItem(id);
+			m_SelectedQuad = Index;
+			s_Operation = OP_CONTEXT_MENU;
+			UI()->SetActiveItem(pId);
 		}
 	}
 	else
 		Graphics()->SetColor(0,1,0,1);
 
-	Graphics()->QuadsDraw(center_x, center_y, 5.0f, 5.0f);
+	IGraphics::CQuadItem QuadItem(CenterX, CenterY, 5.0f, 5.0f);
+	Graphics()->QuadsDraw(&QuadItem, 1);
 }
 
-void EDITOR::do_quad_point(QUAD *q, int quad_index, int v)
+void CEditor::DoQuadPoint(CQuad *q, int QuadIndex, int v)
 {
-	void *id = &q->points[v];
+	void *pId = &q->m_aPoints[v];
 
 	float wx = UI()->MouseWorldX();
 	float wy = UI()->MouseWorldY();
-	
-	float px = fx2f(q->points[v].x);
-	float py = fx2f(q->points[v].y);
-	
+
+	float px = fx2f(q->m_aPoints[v].x);
+	float py = fx2f(q->m_aPoints[v].y);
+
 	float dx = (px - wx);
 	float dy = (py - wy);
 	if(dx*dx+dy*dy < 10*10)
-		UI()->SetHotItem(id);
+		UI()->SetHotItem(pId);
 
-	// draw selection background	
-	if(selected_quad == quad_index && selected_points&(1<<v))
+	// draw selection background
+	if(m_SelectedQuad == QuadIndex && m_SelectedPoints&(1<<v))
 	{
 		Graphics()->SetColor(0,0,0,1);
-		Graphics()->QuadsDraw(px, py, 7.0f, 7.0f);
+		IGraphics::CQuadItem QuadItem(px, py, 7.0f, 7.0f);
+		Graphics()->QuadsDraw(&QuadItem, 1);
 	}
-	
+
 	enum
 	{
 		OP_NONE=0,
@@ -1058,48 +930,48 @@ void EDITOR::do_quad_point(QUAD *q, int quad_index, int v)
 		OP_MOVEUV,
 		OP_CONTEXT_MENU
 	};
-	
-	static bool moved;
-	static int operation = OP_NONE;
 
-	if(UI()->ActiveItem() == id)
+	static bool s_Moved;
+	static int s_Operation = OP_NONE;
+
+	if(UI()->ActiveItem() == pId)
 	{
-		float dx = mouse_delta_wx;
-		float dy = mouse_delta_wy;
-		if(!moved)
+		float dx = m_MouseDeltaWx;
+		float dy = m_MouseDeltaWy;
+		if(!s_Moved)
 		{
 			if(dx*dx+dy*dy > 0.5f)
-				moved = true;
+				s_Moved = true;
 		}
-		
-		if(moved)
+
+		if(s_Moved)
 		{
-			if(operation == OP_MOVEPOINT)
+			if(s_Operation == OP_MOVEPOINT)
 			{
 				for(int m = 0; m < 4; m++)
-					if(selected_points&(1<<m))
+					if(m_SelectedPoints&(1<<m))
 					{
-						q->points[m].x += f2fx(dx);
-						q->points[m].y += f2fx(dy);
+						q->m_aPoints[m].x += f2fx(dx);
+						q->m_aPoints[m].y += f2fx(dy);
 					}
 			}
-			else if(operation == OP_MOVEUV)
+			else if(s_Operation == OP_MOVEUV)
 			{
 				for(int m = 0; m < 4; m++)
-					if(selected_points&(1<<m))
+					if(m_SelectedPoints&(1<<m))
 					{
-						q->texcoords[m].x += f2fx(dx*0.001f);
-						q->texcoords[m].y += f2fx(dy*0.001f);
+						q->m_aTexcoords[m].x += f2fx(dx*0.001f);
+						q->m_aTexcoords[m].y += f2fx(dy*0.001f);
 					}
 			}
 		}
-		
-		if(operation == OP_CONTEXT_MENU)
+
+		if(s_Operation == OP_CONTEXT_MENU)
 		{
 			if(!UI()->MouseButton(1))
 			{
-				static int point_popup_id = 0;
-				ui_invoke_popup_menu(&point_popup_id, 0, UI()->MouseX(), UI()->MouseY(), 120, 150, popup_point);
+				static int s_PointPopupId = 0;
+				UiInvokePopupMenu(&s_PointPopupId, 0, UI()->MouseX(), UI()->MouseY(), 120, 150, PopupPoint);
 				UI()->SetActiveItem(0);
 			}
 		}
@@ -1107,210 +979,214 @@ void EDITOR::do_quad_point(QUAD *q, int quad_index, int v)
 		{
 			if(!UI()->MouseButton(0))
 			{
-				if(!moved)
+				if(!s_Moved)
 				{
-					if(inp_key_pressed(KEY_LSHIFT) || inp_key_pressed(KEY_RSHIFT))
-						selected_points ^= 1<<v;
+					if(Input()->KeyPressed(KEY_LSHIFT) || Input()->KeyPressed(KEY_RSHIFT))
+						m_SelectedPoints ^= 1<<v;
 					else
-						selected_points = 1<<v;
+						m_SelectedPoints = 1<<v;
 				}
-				lock_mouse = false;
+				m_LockMouse = false;
 				UI()->SetActiveItem(0);
 			}
 		}
 
 		Graphics()->SetColor(1,1,1,1);
 	}
-	else if(UI()->HotItem() == id)
+	else if(UI()->HotItem() == pId)
 	{
-		ui_got_context = id;
-		
+		ms_pUiGotContext = pId;
+
 		Graphics()->SetColor(1,1,1,1);
-		tooltip = "Left mouse button to move. Hold shift to move the texture.";
-		
+		m_pTooltip = "Left mouse button to move. Hold shift to move the texture.";
+
 		if(UI()->MouseButton(0))
 		{
-			UI()->SetActiveItem(id);
-			moved = false;
-			if(inp_key_pressed(KEY_LSHIFT) || inp_key_pressed(KEY_RSHIFT))
+			UI()->SetActiveItem(pId);
+			s_Moved = false;
+			if(Input()->KeyPressed(KEY_LSHIFT) || Input()->KeyPressed(KEY_RSHIFT))
 			{
-				operation = OP_MOVEUV;
-				lock_mouse = true;
+				s_Operation = OP_MOVEUV;
+				m_LockMouse = true;
 			}
 			else
-				operation = OP_MOVEPOINT;
-				
-			if(!(selected_points&(1<<v)))
+				s_Operation = OP_MOVEPOINT;
+
+			if(!(m_SelectedPoints&(1<<v)))
 			{
-				if(inp_key_pressed(KEY_LSHIFT) || inp_key_pressed(KEY_RSHIFT))
-					selected_points |= 1<<v;
+				if(Input()->KeyPressed(KEY_LSHIFT) || Input()->KeyPressed(KEY_RSHIFT))
+					m_SelectedPoints |= 1<<v;
 				else
-					selected_points = 1<<v;
-				moved = true;
+					m_SelectedPoints = 1<<v;
+				s_Moved = true;
 			}
-															
-			selected_quad = quad_index;
+
+			m_SelectedQuad = QuadIndex;
 		}
 		else if(UI()->MouseButton(1))
 		{
-			operation = OP_CONTEXT_MENU;
-			selected_quad = quad_index;
-			UI()->SetActiveItem(id);
+			s_Operation = OP_CONTEXT_MENU;
+			m_SelectedQuad = QuadIndex;
+			UI()->SetActiveItem(pId);
 		}
 	}
 	else
 		Graphics()->SetColor(1,0,0,1);
-	
-	Graphics()->QuadsDraw(px, py, 5.0f, 5.0f);	
+
+	IGraphics::CQuadItem QuadItem(px, py, 5.0f, 5.0f);
+	Graphics()->QuadsDraw(&QuadItem, 1);
 }
 
-void EDITOR::do_map_editor(CUIRect view, CUIRect toolbar)
+void CEditor::DoMapEditor(CUIRect View, CUIRect ToolBar)
 {
 	//UI()->ClipEnable(&view);
-	
-	bool show_picker = inp_key_pressed(KEY_SPACE) != 0 && dialog == DIALOG_NONE;
+
+	bool ShowPicker = Input()->KeyPressed(KEY_SPACE) != 0 && m_Dialog == DIALOG_NONE;
 
 	// render all good stuff
-	if(!show_picker)
+	if(!ShowPicker)
 	{
-		for(int g = 0; g < map.groups.len(); g++)
+		for(int g = 0; g < m_Map.m_lGroups.size(); g++)
 		{
-			if(map.groups[g]->visible)
-				map.groups[g]->render();
+			if(m_Map.m_lGroups[g]->m_Visible)
+				m_Map.m_lGroups[g]->Render();
 			//UI()->ClipEnable(&view);
 		}
-		
+
 		// render the game above everything else
-		if(map.game_group->visible && map.game_layer->visible)
+		if(m_Map.m_pGameGroup->m_Visible && m_Map.m_pGameLayer->m_Visible)
 		{
-			map.game_group->mapscreen();
-			map.game_layer->render();
+			m_Map.m_pGameGroup->MapScreen();
+			m_Map.m_pGameLayer->Render();
 		}
 	}
 
-	static void *editor_id = (void *)&editor_id;
-	int inside = UI()->MouseInside(&view);
+	static void *s_pEditorId = (void *)&s_pEditorId;
+	int Inside = UI()->MouseInside(&View);
 
 	// fetch mouse position
 	float wx = UI()->MouseWorldX();
 	float wy = UI()->MouseWorldY();
 	float mx = UI()->MouseX();
 	float my = UI()->MouseY();
-	
-	static float start_wx = 0;
-	static float start_wy = 0;
-	static float start_mx = 0;
-	static float start_my = 0;
-	
+
+	static float s_StartWx = 0;
+	static float s_StartWy = 0;
+	static float s_StartMx = 0;
+	static float s_StartMy = 0;
+
 	enum
 	{
 		OP_NONE=0,
 		OP_BRUSH_GRAB,
 		OP_BRUSH_DRAW,
+		OP_BRUSH_PAINT,
 		OP_PAN_WORLD,
 		OP_PAN_EDITOR,
 	};
 
 	// remap the screen so it can display the whole tileset
-	if(show_picker)
-	{
-		CUIRect screen = *UI()->Screen();
-		float size = 32.0*16.0f;
-		float w = size*(screen.w/view.w);
-		float h = size*(screen.h/view.h);
-		float x = -(view.x/screen.w)*w;
-		float y = -(view.y/screen.h)*h;
-		wx = x+w*mx/screen.w;
-		wy = y+h*my/screen.h;
+	if(ShowPicker)
+	{
+		CUIRect Screen = *UI()->Screen();
+		float Size = 32.0*16.0f;
+		float w = Size*(Screen.w/View.w);
+		float h = Size*(Screen.h/View.h);
+		float x = -(View.x/Screen.w)*w;
+		float y = -(View.y/Screen.h)*h;
+		wx = x+w*mx/Screen.w;
+		wy = y+h*my/Screen.h;
 		Graphics()->MapScreen(x, y, x+w, y+h);
-		LAYER_TILES *t = (LAYER_TILES *)get_selected_layer_type(0, LAYERTYPE_TILES);
+		CLayerTiles *t = (CLayerTiles *)GetSelectedLayerType(0, LAYERTYPE_TILES);
 		if(t)
 		{
-			tileset_picker.image = t->image;
-			tileset_picker.tex_id = t->tex_id;
-			tileset_picker.render();
+			m_TilesetPicker.m_Image = t->m_Image;
+			m_TilesetPicker.m_TexId = t->m_TexId;
+			m_TilesetPicker.Render();
 		}
 	}
-	
-	static int operation = OP_NONE;
-	
+
+	static int s_Operation = OP_NONE;
+
 	// draw layer borders
-	LAYER *edit_layers[16];
-	int num_edit_layers = 0;
-	num_edit_layers = 0;
-	
-	if(show_picker)
+	CLayer *pEditLayers[16];
+	int NumEditLayers = 0;
+	NumEditLayers = 0;
+
+	if(ShowPicker)
 	{
-		edit_layers[0] = &tileset_picker;
-		num_edit_layers++;
+		pEditLayers[0] = &m_TilesetPicker;
+		NumEditLayers++;
 	}
 	else
 	{
-		edit_layers[0] = get_selected_layer(0);
-		if(edit_layers[0])
-			num_edit_layers++;
+		pEditLayers[0] = GetSelectedLayer(0);
+		if(pEditLayers[0])
+			NumEditLayers++;
 
-		LAYERGROUP *g = get_selected_group();
+		CLayerGroup *g = GetSelectedGroup();
 		if(g)
 		{
-			g->mapscreen();
-			
-			for(int i = 0; i < num_edit_layers; i++)
+			g->MapScreen();
+
+			for(int i = 0; i < NumEditLayers; i++)
 			{
-				if(edit_layers[i]->type != LAYERTYPE_TILES)
+				if(pEditLayers[i]->m_Type != LAYERTYPE_TILES)
 					continue;
-					
+
 				float w, h;
-				edit_layers[i]->get_size(&w, &h);
+				pEditLayers[i]->GetSize(&w, &h);
 
+				IGraphics::CLineItem Array[4] = {
+					IGraphics::CLineItem(0, 0, w, 0),
+					IGraphics::CLineItem(w, 0, w, h),
+					IGraphics::CLineItem(w, h, 0, h),
+					IGraphics::CLineItem(0, h, 0, 0)};
 				Graphics()->TextureSet(-1);
 				Graphics()->LinesBegin();
-				Graphics()->LinesDraw(0,0, w,0);
-				Graphics()->LinesDraw(w,0, w,h);
-				Graphics()->LinesDraw(w,h, 0,h);
-				Graphics()->LinesDraw(0,h, 0,0);
+				Graphics()->LinesDraw(Array, 4);
 				Graphics()->LinesEnd();
 			}
 		}
 	}
-		
-	if(inside)
+
+	if(Inside)
 	{
-		UI()->SetHotItem(editor_id);
-				
+		UI()->SetHotItem(s_pEditorId);
+
 		// do global operations like pan and zoom
 		if(UI()->ActiveItem() == 0 && (UI()->MouseButton(0) || UI()->MouseButton(2)))
 		{
-			start_wx = wx;
-			start_wy = wy;
-			start_mx = mx;
-			start_my = my;
-					
-			if(inp_key_pressed(KEY_LCTRL) || inp_key_pressed(KEY_RCTRL) || UI()->MouseButton(2))
+			s_StartWx = wx;
+			s_StartWy = wy;
+			s_StartMx = mx;
+			s_StartMy = my;
+
+			if(Input()->KeyPressed(KEY_LCTRL) || Input()->KeyPressed(KEY_RCTRL) || UI()->MouseButton(2))
 			{
-				if(inp_key_pressed(KEY_LSHIFT))
-					operation = OP_PAN_EDITOR;
+				if(Input()->KeyPressed(KEY_LSHIFT))
+					s_Operation = OP_PAN_EDITOR;
 				else
-					operation = OP_PAN_WORLD;
-				UI()->SetActiveItem(editor_id);
+					s_Operation = OP_PAN_WORLD;
+				UI()->SetActiveItem(s_pEditorId);
 			}
 		}
 
 		// brush editing
-		if(UI()->HotItem() == editor_id)
+		if(UI()->HotItem() == s_pEditorId)
 		{
-			if(brush.is_empty())
-				tooltip = "Use left mouse button to drag and create a brush.";
+			if(m_Brush.IsEmpty())
+				m_pTooltip = "Use left mouse button to drag and create a brush.";
 			else
-				tooltip = "Use left mouse button to paint with the brush. Right button clears the brush.";
+				m_pTooltip = "Use left mouse button to paint with the brush. Right button clears the brush.";
 
-			if(UI()->ActiveItem() == editor_id)
+			if(UI()->ActiveItem() == s_pEditorId)
 			{
 				CUIRect r;
-				r.x = start_wx;
-				r.y = start_wy;
-				r.w = wx-start_wx;
-				r.h = wy-start_wy;
+				r.x = s_StartWx;
+				r.y = s_StartWy;
+				r.w = wx-s_StartWx;
+				r.h = wy-s_StartWy;
 				if(r.w < 0)
 				{
 					r.x += r.w;
@@ -1322,38 +1198,53 @@ void EDITOR::do_map_editor(CUIRect view, CUIRect toolbar)
 					r.y += r.h;
 					r.h = -r.h;
 				}
-				
-				if(operation == OP_BRUSH_DRAW)
-				{						
-					if(!brush.is_empty())
+
+				if(s_Operation == OP_BRUSH_DRAW)
+				{
+					if(!m_Brush.IsEmpty())
 					{
 						// draw with brush
-						for(int k = 0; k < num_edit_layers; k++)
+						for(int k = 0; k < NumEditLayers; k++)
 						{
-							if(edit_layers[k]->type == brush.layers[0]->type)
-								edit_layers[k]->brush_draw(brush.layers[0], wx, wy);
+							if(pEditLayers[k]->m_Type == m_Brush.m_lLayers[0]->m_Type)
+								pEditLayers[k]->BrushDraw(m_Brush.m_lLayers[0], wx, wy);
 						}
 					}
 				}
-				else if(operation == OP_BRUSH_GRAB)
+				else if(s_Operation == OP_BRUSH_GRAB)
 				{
 					if(!UI()->MouseButton(0))
 					{
 						// grab brush
 						dbg_msg("editor", "grabbing %f %f %f %f", r.x, r.y, r.w, r.h);
-						
+
 						// TODO: do all layers
-						int grabs = 0;
-						for(int k = 0; k < num_edit_layers; k++)
-							grabs += edit_layers[k]->brush_grab(&brush, r);
-						if(grabs == 0)
-							brush.clear();
+						int Grabs = 0;
+						for(int k = 0; k < NumEditLayers; k++)
+							Grabs += pEditLayers[k]->BrushGrab(&m_Brush, r);
+						if(Grabs == 0)
+							m_Brush.Clear();
 					}
 					else
 					{
 						//editor.map.groups[selected_group]->mapscreen();
-						for(int k = 0; k < num_edit_layers; k++)
-							edit_layers[k]->brush_selecting(r);
+						for(int k = 0; k < NumEditLayers; k++)
+							pEditLayers[k]->BrushSelecting(r);
+						Graphics()->MapScreen(UI()->Screen()->x, UI()->Screen()->y, UI()->Screen()->w, UI()->Screen()->h);
+					}
+				}
+				else if(s_Operation == OP_BRUSH_PAINT)
+				{
+					if(!UI()->MouseButton(0))
+					{
+                        for(int k = 0; k < NumEditLayers; k++)
+                            pEditLayers[k]->FillSelection(m_Brush.IsEmpty(), m_Brush.m_lLayers[0], r);
+					}
+					else
+					{
+						//editor.map.groups[selected_group]->mapscreen();
+						for(int k = 0; k < NumEditLayers; k++)
+							pEditLayers[k]->BrushSelecting(r);
 						Graphics()->MapScreen(UI()->Screen()->x, UI()->Screen()->y, UI()->Screen()->w, UI()->Screen()->h);
 					}
 				}
@@ -1361,182 +1252,196 @@ void EDITOR::do_map_editor(CUIRect view, CUIRect toolbar)
 			else
 			{
 				if(UI()->MouseButton(1))
-					brush.clear();
-					
-				if(UI()->MouseButton(0) && operation == OP_NONE)
+					m_Brush.Clear();
+
+				if(UI()->MouseButton(0) && s_Operation == OP_NONE)
 				{
-					UI()->SetActiveItem(editor_id);
-					
-					if(brush.is_empty())
-						operation = OP_BRUSH_GRAB;
+					UI()->SetActiveItem(s_pEditorId);
+
+					if(m_Brush.IsEmpty())
+						s_Operation = OP_BRUSH_GRAB;
 					else
 					{
-						operation = OP_BRUSH_DRAW;
-						for(int k = 0; k < num_edit_layers; k++)
+						s_Operation = OP_BRUSH_DRAW;
+						for(int k = 0; k < NumEditLayers; k++)
 						{
-							if(edit_layers[k]->type == brush.layers[0]->type)
-								edit_layers[k]->brush_place(brush.layers[0], wx, wy);
+							if(pEditLayers[k]->m_Type == m_Brush.m_lLayers[0]->m_Type)
+								pEditLayers[k]->BrushPlace(m_Brush.m_lLayers[0], wx, wy);
 						}
-						
+
 					}
+					
+					CLayerTiles *pLayer = (CLayerTiles*)GetSelectedLayerType(0, LAYERTYPE_TILES);
+					if((Input()->KeyPressed(KEY_LSHIFT) || Input()->KeyPressed(KEY_RSHIFT)) && pLayer)
+                        s_Operation = OP_BRUSH_PAINT;
 				}
-				
-				if(!brush.is_empty())
+
+				if(!m_Brush.IsEmpty())
 				{
-					brush.offset_x = -(int)wx;
-					brush.offset_y = -(int)wy;
-					for(int i = 0; i < brush.layers.len(); i++)
+					m_Brush.m_OffsetX = -(int)wx;
+					m_Brush.m_OffsetY = -(int)wy;
+					for(int i = 0; i < m_Brush.m_lLayers.size(); i++)
 					{
-						if(brush.layers[i]->type == LAYERTYPE_TILES)
+						if(m_Brush.m_lLayers[i]->m_Type == LAYERTYPE_TILES)
 						{
-							brush.offset_x = -(int)(wx/32.0f)*32;
-							brush.offset_y = -(int)(wy/32.0f)*32;
+							m_Brush.m_OffsetX = -(int)(wx/32.0f)*32;
+							m_Brush.m_OffsetY = -(int)(wy/32.0f)*32;
 							break;
 						}
 					}
-				
-					LAYERGROUP *g = get_selected_group();
-					brush.offset_x += g->offset_x;
-					brush.offset_y += g->offset_y;
-					brush.parallax_x = g->parallax_x;
-					brush.parallax_y = g->parallax_y;
-					brush.render();
+
+					CLayerGroup *g = GetSelectedGroup();
+					m_Brush.m_OffsetX += g->m_OffsetX;
+					m_Brush.m_OffsetY += g->m_OffsetY;
+					m_Brush.m_ParallaxX = g->m_ParallaxX;
+					m_Brush.m_ParallaxY = g->m_ParallaxY;
+					m_Brush.Render();
 					float w, h;
-					brush.get_size(&w, &h);
-					
+					m_Brush.GetSize(&w, &h);
+
+					IGraphics::CLineItem Array[4] = {
+						IGraphics::CLineItem(0, 0, w, 0),
+						IGraphics::CLineItem(w, 0, w, h),
+						IGraphics::CLineItem(w, h, 0, h),
+						IGraphics::CLineItem(0, h, 0, 0)};
 					Graphics()->TextureSet(-1);
 					Graphics()->LinesBegin();
-					Graphics()->LinesDraw(0,0, w,0);
-					Graphics()->LinesDraw(w,0, w,h);
-					Graphics()->LinesDraw(w,h, 0,h);
-					Graphics()->LinesDraw(0,h, 0,0);
+					Graphics()->LinesDraw(Array, 4);
 					Graphics()->LinesEnd();
-					
+
 				}
 			}
 		}
-		
+
 		// quad editing
 		{
-			if(!show_picker && brush.is_empty())
+			if(!ShowPicker && m_Brush.IsEmpty())
 			{
 				// fetch layers
-				LAYERGROUP *g = get_selected_group();
+				CLayerGroup *g = GetSelectedGroup();
 				if(g)
-					g->mapscreen();
-					
-				for(int k = 0; k < num_edit_layers; k++)
+					g->MapScreen();
+
+				for(int k = 0; k < NumEditLayers; k++)
 				{
-					if(edit_layers[k]->type == LAYERTYPE_QUADS)
+					if(pEditLayers[k]->m_Type == LAYERTYPE_QUADS)
 					{
-						LAYER_QUADS *layer = (LAYER_QUADS *)edit_layers[k];
-		
+						CLayerQuads *pLayer = (CLayerQuads *)pEditLayers[k];
+
 						Graphics()->TextureSet(-1);
-						Graphics()->QuadsBegin();				
-						for(int i = 0; i < layer->quads.len(); i++)
+						Graphics()->QuadsBegin();
+						for(int i = 0; i < pLayer->m_lQuads.size(); i++)
 						{
 							for(int v = 0; v < 4; v++)
-								do_quad_point(&layer->quads[i], i, v);
-								
-							do_quad(&layer->quads[i], i);
+								DoQuadPoint(&pLayer->m_lQuads[i], i, v);
+
+							DoQuad(&pLayer->m_lQuads[i], i);
 						}
 						Graphics()->QuadsEnd();
 					}
 				}
-				
+
 				Graphics()->MapScreen(UI()->Screen()->x, UI()->Screen()->y, UI()->Screen()->w, UI()->Screen()->h);
-			}		
-			
+			}
+
 			// do panning
-			if(UI()->ActiveItem() == editor_id)
+			if(UI()->ActiveItem() == s_pEditorId)
 			{
-				if(operation == OP_PAN_WORLD)
+				if(s_Operation == OP_PAN_WORLD)
 				{
-					world_offset_x -= mouse_delta_x*world_zoom;
-					world_offset_y -= mouse_delta_y*world_zoom;
+					m_WorldOffsetX -= m_MouseDeltaX*m_WorldZoom;
+					m_WorldOffsetY -= m_MouseDeltaY*m_WorldZoom;
 				}
-				else if(operation == OP_PAN_EDITOR)
+				else if(s_Operation == OP_PAN_EDITOR)
 				{
-					editor_offset_x -= mouse_delta_x*world_zoom;
-					editor_offset_y -= mouse_delta_y*world_zoom;
+					m_EditorOffsetX -= m_MouseDeltaX*m_WorldZoom;
+					m_EditorOffsetY -= m_MouseDeltaY*m_WorldZoom;
 				}
 
 				// release mouse
 				if(!UI()->MouseButton(0))
 				{
-					operation = OP_NONE;
+					s_Operation = OP_NONE;
 					UI()->SetActiveItem(0);
 				}
 			}
 		}
 	}
-	
-	if(get_selected_group() && get_selected_group()->use_clipping)
+
+	if(GetSelectedGroup() && GetSelectedGroup()->m_UseClipping)
 	{
-		LAYERGROUP *g = map.game_group;
-		g->mapscreen();
-		
+		CLayerGroup *g = m_Map.m_pGameGroup;
+		g->MapScreen();
+	
 		Graphics()->TextureSet(-1);
 		Graphics()->LinesBegin();
 
 			CUIRect r;
-			r.x = get_selected_group()->clip_x;
-			r.y = get_selected_group()->clip_y;
-			r.w = get_selected_group()->clip_w;
-			r.h = get_selected_group()->clip_h;
-			
+			r.x = GetSelectedGroup()->m_ClipX;
+			r.y = GetSelectedGroup()->m_ClipY;
+			r.w = GetSelectedGroup()->m_ClipW;
+			r.h = GetSelectedGroup()->m_ClipH;
+
+			IGraphics::CLineItem Array[4] = {
+				IGraphics::CLineItem(r.x, r.y, r.x+r.w, r.y),
+				IGraphics::CLineItem(r.x+r.w, r.y, r.x+r.w, r.y+r.h),
+				IGraphics::CLineItem(r.x+r.w, r.y+r.h, r.x, r.y+r.h),
+				IGraphics::CLineItem(r.x, r.y+r.h, r.x, r.y)};
 			Graphics()->SetColor(1,0,0,1);
-			Graphics()->LinesDraw(r.x, r.y, r.x+r.w, r.y);
-			Graphics()->LinesDraw(r.x+r.w, r.y, r.x+r.w, r.y+r.h);
-			Graphics()->LinesDraw(r.x+r.w, r.y+r.h, r.x, r.y+r.h);
-			Graphics()->LinesDraw(r.x, r.y+r.h, r.x, r.y);
-			
+			Graphics()->LinesDraw(Array, 4);
+
 		Graphics()->LinesEnd();
 	}
 
-	// render screen sizes	
-	if(proof_borders)
+	// render screen sizes
+	if(m_ProofBorders)
 	{
-		LAYERGROUP *g = map.game_group;
-		g->mapscreen();
-		
+		CLayerGroup *g = m_Map.m_pGameGroup;
+		g->MapScreen();
+
 		Graphics()->TextureSet(-1);
 		Graphics()->LinesBegin();
-		
-		float last_points[4];
-		float start = 1.0f; //9.0f/16.0f;
-		float end = 16.0f/9.0f;
-		const int num_steps = 20;
-		for(int i = 0; i <= num_steps; i++)
+
+		float aLastPoints[4];
+		float Start = 1.0f; //9.0f/16.0f;
+		float End = 16.0f/9.0f;
+		const int NumSteps = 20;
+		for(int i = 0; i <= NumSteps; i++)
 		{
-			float points[4];
-			float aspect = start + (end-start)*(i/(float)num_steps);
-			
-			RenderTools()->mapscreen_to_world(
-				world_offset_x, world_offset_y,
-				1.0f, 1.0f, 0.0f, 0.0f, aspect, 1.0f, points);
-			
+			float aPoints[4];
+			float Aspect = Start + (End-Start)*(i/(float)NumSteps);
+
+			RenderTools()->MapscreenToWorld(
+				m_WorldOffsetX, m_WorldOffsetY,
+				1.0f, 1.0f, 0.0f, 0.0f, Aspect, 1.0f, aPoints);
+
 			if(i == 0)
 			{
-				Graphics()->LinesDraw(points[0], points[1], points[2], points[1]);
-				Graphics()->LinesDraw(points[0], points[3], points[2], points[3]);
+				IGraphics::CLineItem Array[2] = {
+					IGraphics::CLineItem(aPoints[0], aPoints[1], aPoints[2], aPoints[1]),
+					IGraphics::CLineItem(aPoints[0], aPoints[3], aPoints[2], aPoints[3])};
+				Graphics()->LinesDraw(Array, 2);
 			}
 
 			if(i != 0)
 			{
-				Graphics()->LinesDraw(points[0], points[1], last_points[0], last_points[1]);
-				Graphics()->LinesDraw(points[2], points[1], last_points[2], last_points[1]);
-				Graphics()->LinesDraw(points[0], points[3], last_points[0], last_points[3]);
-				Graphics()->LinesDraw(points[2], points[3], last_points[2], last_points[3]);
+				IGraphics::CLineItem Array[4] = {
+					IGraphics::CLineItem(aPoints[0], aPoints[1], aLastPoints[0], aLastPoints[1]),
+					IGraphics::CLineItem(aPoints[2], aPoints[1], aLastPoints[2], aLastPoints[1]),
+					IGraphics::CLineItem(aPoints[0], aPoints[3], aLastPoints[0], aLastPoints[3]),
+					IGraphics::CLineItem(aPoints[2], aPoints[3], aLastPoints[2], aLastPoints[3])};
+				Graphics()->LinesDraw(Array, 4);
 			}
 
-			if(i == num_steps)
+			if(i == NumSteps)
 			{
-				Graphics()->LinesDraw(points[0], points[1], points[0], points[3]);
-				Graphics()->LinesDraw(points[2], points[1], points[2], points[3]);
+				IGraphics::CLineItem Array[2] = {
+					IGraphics::CLineItem(aPoints[0], aPoints[1], aPoints[0], aPoints[3]),
+					IGraphics::CLineItem(aPoints[2], aPoints[1], aPoints[2], aPoints[3])};
+				Graphics()->LinesDraw(Array, 2);
 			}
-			
-			mem_copy(last_points, points, sizeof(points));
+
+			mem_copy(aLastPoints, aPoints, sizeof(aPoints));
 		}
 
 		if(1)
@@ -1544,351 +1449,359 @@ void EDITOR::do_map_editor(CUIRect view, CUIRect toolbar)
 			Graphics()->SetColor(1,0,0,1);
 			for(int i = 0; i < 2; i++)
 			{
-				float points[4];
-				float aspects[] = {4.0f/3.0f, 16.0f/10.0f, 5.0f/4.0f, 16.0f/9.0f};
-				float aspect = aspects[i];
-				
-				RenderTools()->mapscreen_to_world(
-					world_offset_x, world_offset_y,
-					1.0f, 1.0f, 0.0f, 0.0f, aspect, 1.0f, points);
-				
+				float aPoints[4];
+				float aAspects[] = {4.0f/3.0f, 16.0f/10.0f, 5.0f/4.0f, 16.0f/9.0f};
+				float Aspect = aAspects[i];
+
+				RenderTools()->MapscreenToWorld(
+					m_WorldOffsetX, m_WorldOffsetY,
+					1.0f, 1.0f, 0.0f, 0.0f, Aspect, 1.0f, aPoints);
+
 				CUIRect r;
-				r.x = points[0];
-				r.y = points[1];
-				r.w = points[2]-points[0];
-				r.h = points[3]-points[1];
-				
-				Graphics()->LinesDraw(r.x, r.y, r.x+r.w, r.y);
-				Graphics()->LinesDraw(r.x+r.w, r.y, r.x+r.w, r.y+r.h);
-				Graphics()->LinesDraw(r.x+r.w, r.y+r.h, r.x, r.y+r.h);
-				Graphics()->LinesDraw(r.x, r.y+r.h, r.x, r.y);
+				r.x = aPoints[0];
+				r.y = aPoints[1];
+				r.w = aPoints[2]-aPoints[0];
+				r.h = aPoints[3]-aPoints[1];
+
+				IGraphics::CLineItem Array[4] = {
+					IGraphics::CLineItem(r.x, r.y, r.x+r.w, r.y),
+					IGraphics::CLineItem(r.x+r.w, r.y, r.x+r.w, r.y+r.h),
+					IGraphics::CLineItem(r.x+r.w, r.y+r.h, r.x, r.y+r.h),
+					IGraphics::CLineItem(r.x, r.y+r.h, r.x, r.y)};
+				Graphics()->LinesDraw(Array, 4);
 				Graphics()->SetColor(0,1,0,1);
 			}
 		}
-			
+
 		Graphics()->LinesEnd();
 	}
-	
+
 	Graphics()->MapScreen(UI()->Screen()->x, UI()->Screen()->y, UI()->Screen()->w, UI()->Screen()->h);
 	//UI()->ClipDisable();
 }
 
 
-int EDITOR::do_properties(CUIRect *toolbox, PROPERTY *props, int *ids, int *new_val)
+int CEditor::DoProperties(CUIRect *pToolBox, CProperty *pProps, int *pIds, int *pNewVal)
 {
-	int change = -1;
+	int Change = -1;
 
-	for(int i = 0; props[i].name; i++)
+	for(int i = 0; pProps[i].m_pName; i++)
 	{
-		CUIRect slot;
-		toolbox->HSplitTop(13.0f, &slot, toolbox);
-		CUIRect label, shifter;
-		slot.VSplitMid(&label, &shifter);
-		shifter.HMargin(1.0f, &shifter);
-		UI()->DoLabel(&label, props[i].name, 10.0f, -1, -1);
-		
-		if(props[i].type == PROPTYPE_INT_STEP)
+		CUIRect Slot;
+		pToolBox->HSplitTop(13.0f, &Slot, pToolBox);
+		CUIRect Label, Shifter;
+		Slot.VSplitMid(&Label, &Shifter);
+		Shifter.HMargin(1.0f, &Shifter);
+		UI()->DoLabel(&Label, pProps[i].m_pName, 10.0f, -1, -1);
+
+		if(pProps[i].m_Type == PROPTYPE_INT_STEP)
 		{
-			CUIRect inc, dec;
-			char buf[64];
-			
-			shifter.VSplitRight(10.0f, &shifter, &inc);
-			shifter.VSplitLeft(10.0f, &dec, &shifter);
-			sprintf(buf, "%d", props[i].value);
-			RenderTools()->DrawUIRect(&shifter, vec4(1,1,1,0.5f), 0, 0.0f);
-			UI()->DoLabel(&shifter, buf, 10.0f, 0, -1);
-			
-			if(DoButton_ButtonDec(&ids[i], 0, 0, &dec, 0, "Decrease"))
+			CUIRect Inc, Dec;
+			char aBuf[64];
+
+			Shifter.VSplitRight(10.0f, &Shifter, &Inc);
+			Shifter.VSplitLeft(10.0f, &Dec, &Shifter);
+			str_format(aBuf, sizeof(aBuf),"%d", pProps[i].m_Value);
+			RenderTools()->DrawUIRect(&Shifter, vec4(1,1,1,0.5f), 0, 0.0f);
+			UI()->DoLabel(&Shifter, aBuf, 10.0f, 0, -1);
+
+			if(DoButton_ButtonDec(&pIds[i], 0, 0, &Dec, 0, "Decrease"))
 			{
-				if(inp_key_pressed(KEY_LSHIFT) || inp_key_pressed(KEY_RSHIFT))
-					*new_val = props[i].value-5;
+				if(Input()->KeyPressed(KEY_LSHIFT) || Input()->KeyPressed(KEY_RSHIFT))
+					*pNewVal = pProps[i].m_Value-5;
 				else
-					*new_val = props[i].value-1;
-				change = i;
+					*pNewVal = pProps[i].m_Value-1;
+				Change = i;
 			}
-			if(DoButton_ButtonInc(((char *)&ids[i])+1, 0, 0, &inc, 0, "Increase"))
+			if(DoButton_ButtonInc(((char *)&pIds[i])+1, 0, 0, &Inc, 0, "Increase"))
 			{
-				if(inp_key_pressed(KEY_LSHIFT) || inp_key_pressed(KEY_RSHIFT))
-					*new_val = props[i].value+5;
+				if(Input()->KeyPressed(KEY_LSHIFT) || Input()->KeyPressed(KEY_RSHIFT))
+					*pNewVal = pProps[i].m_Value+5;
 				else
-					*new_val = props[i].value+1;
-				change = i;
+					*pNewVal = pProps[i].m_Value+1;
+				Change = i;
 			}
 		}
-		else if(props[i].type == PROPTYPE_BOOL)
+		else if(pProps[i].m_Type == PROPTYPE_BOOL)
 		{
-			CUIRect no, yes;
-			shifter.VSplitMid(&no, &yes);
-			if(DoButton_ButtonDec(&ids[i], "No", !props[i].value, &no, 0, ""))
+			CUIRect No, Yes;
+			Shifter.VSplitMid(&No, &Yes);
+			if(DoButton_ButtonDec(&pIds[i], "No", !pProps[i].m_Value, &No, 0, ""))
 			{
-				*new_val = 0;
-				change = i;
+				*pNewVal = 0;
+				Change = i;
 			}
-			if(DoButton_ButtonInc(((char *)&ids[i])+1, "Yes", props[i].value, &yes, 0, ""))
+			if(DoButton_ButtonInc(((char *)&pIds[i])+1, "Yes", pProps[i].m_Value, &Yes, 0, ""))
 			{
-				*new_val = 1;
-				change = i;
+				*pNewVal = 1;
+				Change = i;
 			}
-		}		
-		else if(props[i].type == PROPTYPE_INT_SCROLL)
+		}
+		else if(pProps[i].m_Type == PROPTYPE_INT_SCROLL)
 		{
-			int new_value = ui_do_value_selector(&ids[i], &shifter, "", props[i].value, props[i].min, props[i].max, 1.0f);
-			if(new_value != props[i].value)
+			int NewValue = UiDoValueSelector(&pIds[i], &Shifter, "", pProps[i].m_Value, pProps[i].m_Min, pProps[i].m_Max, 1.0f);
+			if(NewValue != pProps[i].m_Value)
 			{
-				*new_val = new_value;
-				change = i;
+				*pNewVal = NewValue;
+				Change = i;
 			}
 		}
-		else if(props[i].type == PROPTYPE_COLOR)
+		else if(pProps[i].m_Type == PROPTYPE_COLOR)
 		{
-			static const char *texts[4] = {"R", "G", "B", "A"};
-			static int shift[] = {24, 16, 8, 0};
-			int new_color = 0;
-			
+			static const char *s_paTexts[4] = {"R", "G", "B", "A"};
+			static int s_aShift[] = {24, 16, 8, 0};
+			int NewColor = 0;
+
 			for(int c = 0; c < 4; c++)
 			{
-				int v = (props[i].value >> shift[c])&0xff;
-				new_color |= ui_do_value_selector(((char *)&ids[i])+c, &shifter, texts[c], v, 0, 255, 1.0f)<<shift[c];
+				int v = (pProps[i].m_Value >> s_aShift[c])&0xff;
+				NewColor |= UiDoValueSelector(((char *)&pIds[i])+c, &Shifter, s_paTexts[c], v, 0, 255, 1.0f)<<s_aShift[c];
 
 				if(c != 3)
 				{
-					toolbox->HSplitTop(13.0f, &slot, toolbox);
-					slot.VSplitMid(0, &shifter);
-					shifter.HMargin(1.0f, &shifter);
+					pToolBox->HSplitTop(13.0f, &Slot, pToolBox);
+					Slot.VSplitMid(0, &Shifter);
+					Shifter.HMargin(1.0f, &Shifter);
 				}
 			}
-			
-			if(new_color != props[i].value)
+
+			if(NewColor != pProps[i].m_Value)
 			{
-				*new_val = new_color;
-				change = i;
+				*pNewVal = NewColor;
+				Change = i;
 			}
 		}
-		else if(props[i].type == PROPTYPE_IMAGE)
+		else if(pProps[i].m_Type == PROPTYPE_IMAGE)
 		{
-			char buf[64];
-			if(props[i].value < 0)
-				strcpy(buf, "None");
+			char aBuf[64];
+			if(pProps[i].m_Value < 0)
+				str_copy(aBuf, "None", sizeof(aBuf));
 			else
-				sprintf(buf, "%s",  map.images[props[i].value]->name);
-			
-			if(DoButton_Editor(&ids[i], buf, 0, &shifter, 0, 0))
-				popup_select_image_invoke(props[i].value, UI()->MouseX(), UI()->MouseY());
-			
-			int r = popup_select_image_result();
+				str_format(aBuf, sizeof(aBuf),"%s",  m_Map.m_lImages[pProps[i].m_Value]->m_aName);
+
+			if(DoButton_Editor(&pIds[i], aBuf, 0, &Shifter, 0, 0))
+				PopupSelectImageInvoke(pProps[i].m_Value, UI()->MouseX(), UI()->MouseY());
+
+			int r = PopupSelectImageResult();
 			if(r >= -1)
 			{
-				*new_val = r;
-				change = i;
+				*pNewVal = r;
+				Change = i;
 			}
 		}
 	}
 
-	return change;
+	return Change;
 }
 
-void EDITOR::render_layers(CUIRect toolbox, CUIRect toolbar, CUIRect view)
+void CEditor::RenderLayers(CUIRect ToolBox, CUIRect ToolBar, CUIRect View)
 {
-	CUIRect layersbox = toolbox;
+	CUIRect LayersBox = ToolBox;
 
-	if(!gui_active)
+	if(!m_GuiActive)
 		return;
-			
-	CUIRect slot, button;
-	char buf[64];
 
-	int valid_group = 0;
-	int valid_layer = 0;
-	if(selected_group >= 0 && selected_group < map.groups.len())
-		valid_group = 1;
+	CUIRect Slot, Button;
+	char aBuf[64];
 
-	if(valid_group && selected_layer >= 0 && selected_layer < map.groups[selected_group]->layers.len())
-		valid_layer = 1;
-		
-	// render layers	
+	int ValidGroup = 0;
+	int ValidLayer = 0;
+	if(m_SelectedGroup >= 0 && m_SelectedGroup < m_Map.m_lGroups.size())
+		ValidGroup = 1;
+
+	if(ValidGroup && m_SelectedLayer >= 0 && m_SelectedLayer < m_Map.m_lGroups[m_SelectedGroup]->m_lLayers.size())
+		ValidLayer = 1;
+
+	// render layers
 	{
-		for(int g = 0; g < map.groups.len(); g++)
+		for(int g = 0; g < m_Map.m_lGroups.size(); g++)
 		{
-			CUIRect visible_toggle;
-			layersbox.HSplitTop(12.0f, &slot, &layersbox);
-			slot.VSplitLeft(12, &visible_toggle, &slot);
-			if(DoButton_ButtonL(&map.groups[g]->visible, map.groups[g]->visible?"V":"H", 0, &visible_toggle, 0, "Toggle group visibility"))
-				map.groups[g]->visible = !map.groups[g]->visible;
-
-			sprintf(buf, "#%d %s", g, map.groups[g]->name);
-			if(int result = DoButton_ButtonR(&map.groups[g], buf, g==selected_group, &slot,
-				BUTTON_CONTEXT, "Select group. Right click for properties."))
+			CUIRect VisibleToggle;
+			LayersBox.HSplitTop(12.0f, &Slot, &LayersBox);
+			Slot.VSplitLeft(12, &VisibleToggle, &Slot);
+			if(DoButton_Ex(&m_Map.m_lGroups[g]->m_Visible, m_Map.m_lGroups[g]->m_Visible?"V":"H", 0, &VisibleToggle, 0, "Toggle group visibility", CUI::CORNER_L))
+				m_Map.m_lGroups[g]->m_Visible = !m_Map.m_lGroups[g]->m_Visible;
+
+			str_format(aBuf, sizeof(aBuf),"#%d %s", g, m_Map.m_lGroups[g]->m_pName);
+			if(int Result = DoButton_Ex(&m_Map.m_lGroups[g], aBuf, g==m_SelectedGroup, &Slot,
+				BUTTON_CONTEXT, "Select group. Right click for properties.", CUI::CORNER_R))
 			{
-				selected_group = g;
-				selected_layer = 0;
-				
-				static int group_popup_id = 0;
-				if(result == 2)
-					ui_invoke_popup_menu(&group_popup_id, 0, UI()->MouseX(), UI()->MouseY(), 120, 200, popup_group);
+				m_SelectedGroup = g;
+				m_SelectedLayer = 0;
+
+				static int s_GroupPopupId = 0;
+				if(Result == 2)
+					UiInvokePopupMenu(&s_GroupPopupId, 0, UI()->MouseX(), UI()->MouseY(), 120, 200, PopupGroup);
 			}
-			
-			
-			layersbox.HSplitTop(2.0f, &slot, &layersbox);
-			
-			for(int i = 0; i < map.groups[g]->layers.len(); i++)
+
+			LayersBox.HSplitTop(2.0f, &Slot, &LayersBox);
+
+			for(int i = 0; i < m_Map.m_lGroups[g]->m_lLayers.size(); i++)
 			{
 				//visible
-				layersbox.HSplitTop(12.0f, &slot, &layersbox);
-				slot.VSplitLeft(12.0f, 0, &button);
-				button.VSplitLeft(15, &visible_toggle, &button);
+				LayersBox.HSplitTop(12.0f, &Slot, &LayersBox);
+				Slot.VSplitLeft(12.0f, 0, &Button);
+				Button.VSplitLeft(15, &VisibleToggle, &Button);
 
-				if(DoButton_ButtonL(&map.groups[g]->layers[i]->visible, map.groups[g]->layers[i]->visible?"V":"H", 0, &visible_toggle, 0, "Toggle layer visibility"))
-					map.groups[g]->layers[i]->visible = !map.groups[g]->layers[i]->visible;
+				if(DoButton_Ex(&m_Map.m_lGroups[g]->m_lLayers[i]->m_Visible, m_Map.m_lGroups[g]->m_lLayers[i]->m_Visible?"V":"H", 0, &VisibleToggle, 0, "Toggle layer visibility", CUI::CORNER_L))
+					m_Map.m_lGroups[g]->m_lLayers[i]->m_Visible = !m_Map.m_lGroups[g]->m_lLayers[i]->m_Visible;
 
-				sprintf(buf, "#%d %s ", i, map.groups[g]->layers[i]->type_name);
-				if(int result = DoButton_ButtonR(map.groups[g]->layers[i], buf, g==selected_group&&i==selected_layer, &button,
-					BUTTON_CONTEXT, "Select layer. Right click for properties."))
+				str_format(aBuf, sizeof(aBuf),"#%d %s ", i, m_Map.m_lGroups[g]->m_lLayers[i]->m_pTypeName);
+				if(int Result = DoButton_Ex(m_Map.m_lGroups[g]->m_lLayers[i], aBuf, g==m_SelectedGroup&&i==m_SelectedLayer, &Button,
+					BUTTON_CONTEXT, "Select layer. Right click for properties.", CUI::CORNER_R))
 				{
-					selected_layer = i;
-					selected_group = g;
-					static int layer_popup_id = 0;
-					if(result == 2)
-						ui_invoke_popup_menu(&layer_popup_id, 0, UI()->MouseX(), UI()->MouseY(), 120, 150, popup_layer);
+					m_SelectedLayer = i;
+					m_SelectedGroup = g;
+					static int s_LayerPopupId = 0;
+					if(Result == 2)
+						UiInvokePopupMenu(&s_LayerPopupId, 0, UI()->MouseX(), UI()->MouseY(), 120, 150, PopupLayer);
 				}
-				
-				
-				layersbox.HSplitTop(2.0f, &slot, &layersbox);
+
+
+				LayersBox.HSplitTop(2.0f, &Slot, &LayersBox);
 			}
-			layersbox.HSplitTop(5.0f, &slot, &layersbox);
+			LayersBox.HSplitTop(5.0f, &Slot, &LayersBox);
 		}
 	}
-	
+
 
 	{
-		layersbox.HSplitTop(12.0f, &slot, &layersbox);
+		LayersBox.HSplitTop(12.0f, &Slot, &LayersBox);
 
-		static int new_group_button = 0;
-		if(DoButton_Editor(&new_group_button, "Add Group", 0, &slot, 0, "Adds a new group"))
+		static int s_NewGroupButton = 0;
+		if(DoButton_Editor(&s_NewGroupButton, "Add Group", 0, &Slot, 0, "Adds a new group"))
 		{
-			map.new_group();
-			selected_group = map.groups.len()-1;
+			m_Map.NewGroup();
+			m_SelectedGroup = m_Map.m_lGroups.size()-1;
 		}
 	}
 
-	layersbox.HSplitTop(5.0f, &slot, &layersbox);
-	
+	LayersBox.HSplitTop(5.0f, &Slot, &LayersBox);
+
 }
 
-static void extract_name(const char *filename, char *name)
+static void ExtractName(const char *pFileName, char *pName)
 {
-	int len = strlen(filename);
-	int start = len;
-	int end = len;
-	
-	while(start > 0)
+	int Len = str_length(pFileName);
+	int Start = Len;
+	int End = Len;
+
+	while(Start > 0)
 	{
-		start--;
-		if(filename[start] == '/' || filename[start] == '\\')
+		Start--;
+		if(pFileName[Start] == '/' || pFileName[Start] == '\\')
 		{
-			start++;
+			Start++;
 			break;
 		}
 	}
-	
-	end = start;
-	for(int i = start; i < len; i++)
+
+	End = Start;
+	for(int i = Start; i < Len; i++)
 	{
-		if(filename[i] == '.')
-			end = i;
+		if(pFileName[i] == '.')
+			End = i;
 	}
-	
-	if(end == start)
-		end = len;
-	
-	int final_len = end-start;
-	mem_copy(name, &filename[start], final_len);
-	name[final_len] = 0;
-	dbg_msg("", "%s %s %d %d", filename, name, start, end);
+
+	if(End == Start)
+		End = Len;
+
+	int FinalLen = End-Start;
+	mem_copy(pName, &pFileName[Start], FinalLen);
+	pName[FinalLen] = 0;
+	dbg_msg("", "%s %s %d %d", pFileName, pName, Start, End);
 }
 
-void EDITOR::replace_image(const char *filename, void *user)
+void CEditor::ReplaceImage(const char *pFileName, void *pUser)
 {
-	EDITOR *editor = (EDITOR *)user;
-	EDITOR_IMAGE imginfo(editor);
-	if(!editor->Graphics()->LoadPNG(&imginfo, filename))
+	CEditor *pEditor = (CEditor *)pUser;
+	CEditorImage ImgInfo(pEditor);
+	if(!pEditor->Graphics()->LoadPNG(&ImgInfo, pFileName))
 		return;
-	
-	EDITOR_IMAGE *img = editor->map.images[editor->selected_image];
-	editor->Graphics()->UnloadTexture(img->tex_id);
-	*img = imginfo;
-	extract_name(filename, img->name);
-	img->tex_id = editor->Graphics()->LoadTextureRaw(imginfo.width, imginfo.height, imginfo.format, imginfo.data, IMG_AUTO, 0);
+
+	CEditorImage *pImg = pEditor->m_Map.m_lImages[pEditor->m_SelectedImage];
+	pEditor->Graphics()->UnloadTexture(pImg->m_TexId);
+	*pImg = ImgInfo;
+	ExtractName(pFileName, pImg->m_aName);
+	pImg->m_TexId = pEditor->Graphics()->LoadTextureRaw(ImgInfo.m_Width, ImgInfo.m_Height, ImgInfo.m_Format, ImgInfo.m_pData, CImageInfo::FORMAT_AUTO, 0);
 }
 
-void EDITOR::add_image(const char *filename, void *user)
+void CEditor::AddImage(const char *pFileName, void *pUser)
 {
-	EDITOR *editor = (EDITOR *)user;
-	EDITOR_IMAGE imginfo(editor);
-	if(!editor->Graphics()->LoadPNG(&imginfo, filename))
+	CEditor *pEditor = (CEditor *)pUser;
+	CEditorImage ImgInfo(pEditor);
+	if(!pEditor->Graphics()->LoadPNG(&ImgInfo, pFileName))
 		return;
 
-	EDITOR_IMAGE *img = new EDITOR_IMAGE(editor);
-	*img = imginfo;
-	img->tex_id = editor->Graphics()->LoadTextureRaw(imginfo.width, imginfo.height, imginfo.format, imginfo.data, IMG_AUTO, 0);
-	img->external = 1; // external by default
-	extract_name(filename, img->name);
-	editor->map.images.add(img);
+	CEditorImage *pImg = new CEditorImage(pEditor);
+	*pImg = ImgInfo;
+	pImg->m_TexId = pEditor->Graphics()->LoadTextureRaw(ImgInfo.m_Width, ImgInfo.m_Height, ImgInfo.m_Format, ImgInfo.m_pData, CImageInfo::FORMAT_AUTO, 0);
+	pImg->m_External = 1; // external by default
+	ExtractName(pFileName, pImg->m_aName);
+
+	for(int i = 0; i < pEditor->m_Map.m_lImages.size(); ++i)
+	{
+	    if(!str_comp(pEditor->m_Map.m_lImages[i]->m_aName, pImg->m_aName))
+            return;
+	}
+
+	pEditor->m_Map.m_lImages.add(pImg);
 }
 
 
-static int modify_index_deleted_index;
-static void modify_index_deleted(int *index)
+static int gs_ModifyIndexDeletedIndex;
+static void ModifyIndexDeleted(int *pIndex)
 {
-	if(*index == modify_index_deleted_index)
-		*index = -1;
-	else if(*index > modify_index_deleted_index)
-		*index = *index - 1;
+	if(*pIndex == gs_ModifyIndexDeletedIndex)
+		*pIndex = -1;
+	else if(*pIndex > gs_ModifyIndexDeletedIndex)
+		*pIndex = *pIndex - 1;
 }
 
-int EDITOR::popup_image(EDITOR *pEditor, CUIRect view)
+int CEditor::PopupImage(CEditor *pEditor, CUIRect View)
 {
-	static int replace_button = 0;	
-	static int remove_button = 0;	
+	static int s_ReplaceButton = 0;
+	static int s_RemoveButton = 0;
 
-	CUIRect slot;
-	view.HSplitTop(2.0f, &slot, &view);
-	view.HSplitTop(12.0f, &slot, &view);
-	EDITOR_IMAGE *img = pEditor->map.images[pEditor->selected_image];
-	
-	static int external_button = 0;
-	if(img->external)
+	CUIRect Slot;
+	View.HSplitTop(2.0f, &Slot, &View);
+	View.HSplitTop(12.0f, &Slot, &View);
+	CEditorImage *pImg = pEditor->m_Map.m_lImages[pEditor->m_SelectedImage];
+
+	static int s_ExternalButton = 0;
+	if(pImg->m_External)
 	{
-		if(pEditor->DoButton_MenuItem(&external_button, "Embedd", 0, &slot, 0, "Embedds the image into the map file."))
+		if(pEditor->DoButton_MenuItem(&s_ExternalButton, "Embedd", 0, &Slot, 0, "Embedds the image into the map file."))
 		{
-			img->external = 0;
+			pImg->m_External = 0;
 			return 1;
 		}
 	}
 	else
-	{		
-		if(pEditor->DoButton_MenuItem(&external_button, "Make external", 0, &slot, 0, "Removes the image from the map file."))
+	{
+		if(pEditor->DoButton_MenuItem(&s_ExternalButton, "Make external", 0, &Slot, 0, "Removes the image from the map file."))
 		{
-			img->external = 1;
+			pImg->m_External = 1;
 			return 1;
 		}
 	}
 
-	view.HSplitTop(10.0f, &slot, &view);
-	view.HSplitTop(12.0f, &slot, &view);
-	if(pEditor->DoButton_MenuItem(&replace_button, "Replace", 0, &slot, 0, "Replaces the image with a new one"))
+	View.HSplitTop(10.0f, &Slot, &View);
+	View.HSplitTop(12.0f, &Slot, &View);
+	if(pEditor->DoButton_MenuItem(&s_ReplaceButton, "Replace", 0, &Slot, 0, "Replaces the image with a new one"))
 	{
-		pEditor->invoke_file_dialog(LISTDIRTYPE_ALL, "Replace Image", "Replace", "mapres/", "", replace_image, pEditor);
+		pEditor->InvokeFileDialog(IStorage::TYPE_ALL, "Replace Image", "Replace", "mapres/", "", ReplaceImage, pEditor);
 		return 1;
 	}
 
-	view.HSplitTop(10.0f, &slot, &view);
-	view.HSplitTop(12.0f, &slot, &view);
-	if(pEditor->DoButton_MenuItem(&remove_button, "Remove", 0, &slot, 0, "Removes the image from the map"))
+	View.HSplitTop(10.0f, &Slot, &View);
+	View.HSplitTop(12.0f, &Slot, &View);
+	if(pEditor->DoButton_MenuItem(&s_RemoveButton, "Remove", 0, &Slot, 0, "Removes the image from the map"))
 	{
-		delete img;
-		pEditor->map.images.removebyindex(pEditor->selected_image);
-		modify_index_deleted_index = pEditor->selected_image;
-		pEditor->map.modify_image_index(modify_index_deleted);
+		delete pImg;
+		pEditor->m_Map.m_lImages.remove_index(pEditor->m_SelectedImage);
+		gs_ModifyIndexDeletedIndex = pEditor->m_SelectedImage;
+		pEditor->m_Map.ModifyImageIndex(ModifyIndexDeleted);
 		return 1;
 	}
 
@@ -1896,712 +1809,722 @@ int EDITOR::popup_image(EDITOR *pEditor, CUIRect view)
 }
 
 
-void EDITOR::render_images(CUIRect toolbox, CUIRect toolbar, CUIRect view)
+void CEditor::RenderImages(CUIRect ToolBox, CUIRect ToolBar, CUIRect View)
 {
 	for(int e = 0; e < 2; e++) // two passes, first embedded, then external
 	{
-		CUIRect slot;
-		toolbox.HSplitTop(15.0f, &slot, &toolbox);
+		CUIRect Slot;
+		ToolBox.HSplitTop(15.0f, &Slot, &ToolBox);
 		if(e == 0)
-			UI()->DoLabel(&slot, "Embedded", 12.0f, 0);
+			UI()->DoLabel(&Slot, "Embedded", 12.0f, 0);
 		else
-			UI()->DoLabel(&slot, "External", 12.0f, 0);
-		
-		for(int i = 0; i < map.images.len(); i++)
+			UI()->DoLabel(&Slot, "External", 12.0f, 0);
+
+		for(int i = 0; i < m_Map.m_lImages.size(); i++)
 		{
-			if((e && !map.images[i]->external) ||
-				(!e && map.images[i]->external))
+			if((e && !m_Map.m_lImages[i]->m_External) ||
+				(!e && m_Map.m_lImages[i]->m_External))
 			{
 				continue;
 			}
-			
-			char buf[128];
-			sprintf(buf, "%s", map.images[i]->name);
-			toolbox.HSplitTop(12.0f, &slot, &toolbox);
-			
-			if(int result = DoButton_Editor(&map.images[i], buf, selected_image == i, &slot,
+
+			char aBuf[128];
+			str_copy(aBuf, m_Map.m_lImages[i]->m_aName, sizeof(aBuf));
+			ToolBox.HSplitTop(12.0f, &Slot, &ToolBox);
+
+			if(int Result = DoButton_Editor(&m_Map.m_lImages[i], aBuf, m_SelectedImage == i, &Slot,
 				BUTTON_CONTEXT, "Select image"))
 			{
-				selected_image = i;
-				
-				static int popup_image_id = 0;
-				if(result == 2)
-					ui_invoke_popup_menu(&popup_image_id, 0, UI()->MouseX(), UI()->MouseY(), 120, 80, popup_image);
+				m_SelectedImage = i;
+
+				static int s_PopupImageId = 0;
+				if(Result == 2)
+					UiInvokePopupMenu(&s_PopupImageId, 0, UI()->MouseX(), UI()->MouseY(), 120, 80, PopupImage);
 			}
-			
-			toolbox.HSplitTop(2.0f, 0, &toolbox);
-			
+
+			ToolBox.HSplitTop(2.0f, 0, &ToolBox);
+
 			// render image
-			if(selected_image == i)
+			if(m_SelectedImage == i)
 			{
 				CUIRect r;
-				view.Margin(10.0f, &r);
+				View.Margin(10.0f, &r);
 				if(r.h < r.w)
 					r.w = r.h;
 				else
 					r.h = r.w;
-				Graphics()->TextureSet(map.images[i]->tex_id);
+				Graphics()->TextureSet(m_Map.m_lImages[i]->m_TexId);
 				Graphics()->BlendNormal();
 				Graphics()->QuadsBegin();
-				Graphics()->QuadsDrawTL(r.x, r.y, r.w, r.h);
+				IGraphics::CQuadItem QuadItem(r.x, r.y, r.w, r.h);
+				Graphics()->QuadsDrawTL(&QuadItem, 1);
 				Graphics()->QuadsEnd();
-				
+
 			}
 		}
 	}
-	
-	CUIRect slot;
-	toolbox.HSplitTop(5.0f, &slot, &toolbox);
-	
-	// new image
-	static int new_image_button = 0;
-	toolbox.HSplitTop(10.0f, &slot, &toolbox);
-	toolbox.HSplitTop(12.0f, &slot, &toolbox);
-	if(DoButton_Editor(&new_image_button, "Add", 0, &slot, 0, "Load a new image to use in the map"))
-		invoke_file_dialog(LISTDIRTYPE_ALL, "Add Image", "Add", "mapres/", "", add_image, this);
-}
 
+	CUIRect Slot;
+	ToolBox.HSplitTop(5.0f, &Slot, &ToolBox);
 
-static int file_dialog_dirtypes = 0;
-static const char *file_dialog_title = 0;
-static const char *file_dialog_button_text = 0;
-static void (*file_dialog_func)(const char *filename, void *user);
-static void *file_dialog_user = 0;
-static char file_dialog_filename[512] = {0};
-static char file_dialog_path[512] = {0};
-static char file_dialog_complete_filename[512] = {0};
-static int files_num = 0;
-int files_startat = 0;
-int files_cur = 0;
-int files_stopat = 999;
-
-struct LISTDIRINFO
-{
-	CUIRect *rect;
-	EDITOR *editor;
+	// new image
+	static int s_NewImageButton = 0;
+	ToolBox.HSplitTop(10.0f, &Slot, &ToolBox);
+	ToolBox.HSplitTop(12.0f, &Slot, &ToolBox);
+	if(DoButton_Editor(&s_NewImageButton, "Add", 0, &Slot, 0, "Load a new image to use in the map"))
+		InvokeFileDialog(IStorage::TYPE_ALL, "Add Image", "Add", "mapres/", "", AddImage, this);
+}
+
+
+static int gs_FileDialogDirTypes = 0;
+static const char *gs_pFileDialogTitle = 0;
+static const char *gs_pFileDialogButtonText = 0;
+static void (*gs_pfnFileDialogFunc)(const char *pFileName, void *pUser);
+static void *gs_pFileDialogUser = 0;
+static char gs_FileDialogFileName[512] = {0};
+static char gs_aFileDialogPath[512] = {0};
+static char gs_aFileDialogCompleteFilename[512] = {0};
+static int gs_FilesNum = 0;
+int g_FilesStartAt = 0;
+int g_FilesCur = 0;
+int g_FilesStopAt = 999;
+
+struct CListDirInfo
+{
+	CUIRect *m_pRect;
+	CEditor *m_pEditor;
 };
 
-static void editor_listdir_callback(const char *name, int is_dir, void *user)
+static void EditorListdirCallback(const char *pName, int IsDir, void *pUser)
 {
-	if(name[0] == '.' || is_dir) // skip this shit!
+	if(pName[0] == '.' || IsDir) // skip this shit!
 		return;
-	
-	if(files_cur > files_num)
-		files_num = files_cur;
-	
-	files_cur++;
-	if(files_cur-1 < files_startat || files_cur > files_stopat)
+
+	if(g_FilesCur > gs_FilesNum)
+		gs_FilesNum = g_FilesCur;
+
+	g_FilesCur++;
+	if(g_FilesCur-1 < g_FilesStartAt || g_FilesCur > g_FilesStopAt)
 		return;
-	
-	LISTDIRINFO *info = (LISTDIRINFO *)user;
-	CUIRect *view = info->rect;
-	CUIRect button;
-	view->HSplitTop(15.0f, &button, view);
-	view->HSplitTop(2.0f, 0, view);
+
+	CListDirInfo *pInfo = (CListDirInfo *)pUser;
+	CUIRect *pView = pInfo->m_pRect;
+	CUIRect Button;
+	pView->HSplitTop(15.0f, &Button, pView);
+	pView->HSplitTop(2.0f, 0, pView);
 	//char buf[512];
-	
-	if(info->editor->DoButton_File((void*)(10+(int)button.y), name, 0, &button, 0, 0))
+
+	if(pInfo->m_pEditor->DoButton_File((void*)(10+(int)Button.y), pName, 0, &Button, 0, 0))
 	{
-		strncpy(file_dialog_filename, name, sizeof(file_dialog_filename));
-		
-		file_dialog_complete_filename[0] = 0;
-		strcat(file_dialog_complete_filename, file_dialog_path);
-		strcat(file_dialog_complete_filename, file_dialog_filename);
+		str_copy(gs_FileDialogFileName, pName, sizeof(gs_FileDialogFileName));
+
+		gs_aFileDialogCompleteFilename[0] = 0;
+		str_append(gs_aFileDialogCompleteFilename, gs_aFileDialogPath, sizeof(gs_aFileDialogCompleteFilename));
+		str_append(gs_aFileDialogCompleteFilename, gs_FileDialogFileName, sizeof(gs_aFileDialogCompleteFilename));
 
-		if(inp_mouse_doubleclick())
+		if(pInfo->m_pEditor->Input()->MouseDoubleClick())
 		{
-			if(file_dialog_func)
-				file_dialog_func(file_dialog_complete_filename, user);
-			info->editor->dialog = DIALOG_NONE;
+			if(gs_pfnFileDialogFunc)
+				gs_pfnFileDialogFunc(gs_aFileDialogCompleteFilename, pInfo->m_pEditor);
+			pInfo->m_pEditor->m_Dialog = DIALOG_NONE;
 		}
 	}
 }
 
-void EDITOR::render_file_dialog()
+void CEditor::RenderFileDialog()
 {
 	// GUI coordsys
 	Graphics()->MapScreen(UI()->Screen()->x, UI()->Screen()->y, UI()->Screen()->w, UI()->Screen()->h);
-	
-	CUIRect view = *UI()->Screen();
-	RenderTools()->DrawUIRect(&view, vec4(0,0,0,0.25f), 0, 0);
-	view.VMargin(150.0f, &view);
-	view.HMargin(50.0f, &view);
-	RenderTools()->DrawUIRect(&view, vec4(0,0,0,0.75f), CUI::CORNER_ALL, 5.0f);
-	view.Margin(10.0f, &view);
-
-	CUIRect title, filebox, filebox_label, buttonbar, scroll;
-	view.HSplitTop(18.0f, &title, &view);
-	view.HSplitTop(5.0f, 0, &view); // some spacing
-	view.HSplitBottom(14.0f, &view, &buttonbar);
-	view.HSplitBottom(10.0f, &view, 0); // some spacing
-	view.HSplitBottom(14.0f, &view, &filebox);
-	filebox.VSplitLeft(50.0f, &filebox_label, &filebox);
-	view.VSplitRight(15.0f, &view, &scroll);
-	
+
+	CUIRect View = *UI()->Screen();
+	RenderTools()->DrawUIRect(&View, vec4(0,0,0,0.25f), 0, 0);
+	View.VMargin(150.0f, &View);
+	View.HMargin(50.0f, &View);
+	RenderTools()->DrawUIRect(&View, vec4(0,0,0,0.75f), CUI::CORNER_ALL, 5.0f);
+	View.Margin(10.0f, &View);
+
+	CUIRect Title, FileBox, FileBoxLabel, ButtonBar, Scroll;
+	View.HSplitTop(18.0f, &Title, &View);
+	View.HSplitTop(5.0f, 0, &View); // some spacing
+	View.HSplitBottom(14.0f, &View, &ButtonBar);
+	View.HSplitBottom(10.0f, &View, 0); // some spacing
+	View.HSplitBottom(14.0f, &View, &FileBox);
+	FileBox.VSplitLeft(55.0f, &FileBoxLabel, &FileBox);
+	View.VSplitRight(15.0f, &View, &Scroll);
+
 	// title
-	RenderTools()->DrawUIRect(&title, vec4(1,1,1,0.25f), CUI::CORNER_ALL, 5.0f);
-	title.VMargin(10.0f, &title);
-	UI()->DoLabel(&title, file_dialog_title, 14.0f, -1, -1);
-	
+	RenderTools()->DrawUIRect(&Title, vec4(1, 1, 1, 0.25f), CUI::CORNER_ALL, 4.0f);
+	Title.VMargin(10.0f, &Title);
+	UI()->DoLabel(&Title, gs_pFileDialogTitle, 12.0f, -1, -1);
+
 	// filebox
-	UI()->DoLabel(&filebox_label, "Filename:", 10.0f, -1, -1);
-	
-	static int filebox_id = 0;
-	DoEditBox(&filebox_id, &filebox, file_dialog_filename, sizeof(file_dialog_filename), 10.0f);
+	static int s_FileBoxId = 0;
+	UI()->DoLabel(&FileBoxLabel, "Filename:", 10.0f, -1, -1);
+	DoEditBox(&s_FileBoxId, &FileBox, gs_FileDialogFileName, sizeof(gs_FileDialogFileName), 10.0f);
 
-	file_dialog_complete_filename[0] = 0;
-	strcat(file_dialog_complete_filename, file_dialog_path);
-	strcat(file_dialog_complete_filename, file_dialog_filename);
-	
-	int num = (int)(view.h/17.0);
-	static float scrollvalue = 0;
-	static int scrollbar = 0;
-	scroll.HMargin(5.0f, &scroll);
-	scrollvalue = ui_do_scrollbar_v(&scrollbar, &scroll, scrollvalue);
-	
-	int scrollnum = files_num-num+10;
-	if(scrollnum > 0)
+	gs_aFileDialogCompleteFilename[0] = 0;
+	str_append(gs_aFileDialogCompleteFilename, gs_aFileDialogPath, sizeof(gs_aFileDialogCompleteFilename));
+	str_append(gs_aFileDialogCompleteFilename, gs_FileDialogFileName, sizeof(gs_aFileDialogCompleteFilename));
+
+	int Num = (int)(View.h/17.0);
+	static float s_ScrollValue = 0;
+	static int ScrollBar = 0;
+	Scroll.HMargin(5.0f, &Scroll);
+	s_ScrollValue = UiDoScrollbarV(&ScrollBar, &Scroll, s_ScrollValue);
+
+	int ScrollNum = gs_FilesNum-Num+10;
+	if(ScrollNum > 0)
 	{
-		if(inp_key_presses(KEY_MOUSE_WHEEL_UP))
-			scrollvalue -= 3.0f/scrollnum;
-		if(inp_key_presses(KEY_MOUSE_WHEEL_DOWN))
-			scrollvalue += 3.0f/scrollnum;
-			
-		if(scrollvalue < 0) scrollvalue = 0;
-		if(scrollvalue > 1) scrollvalue = 1;
+		if(Input()->KeyPresses(KEY_MOUSE_WHEEL_UP))
+			s_ScrollValue -= 3.0f/ScrollNum;
+		if(Input()->KeyPresses(KEY_MOUSE_WHEEL_DOWN))
+			s_ScrollValue += 3.0f/ScrollNum;
+
+		if(s_ScrollValue < 0) s_ScrollValue = 0;
+		if(s_ScrollValue > 1) s_ScrollValue = 1;
 	}
 	else
-		scrollnum = 0;
-	
-	files_startat = (int)(scrollnum*scrollvalue);
-	if(files_startat < 0)
-		files_startat = 0;
-		
-	files_stopat = files_startat+num;
-	
-	files_cur = 0;
-	
+		ScrollNum = 0;
+
+	g_FilesStartAt = (int)(ScrollNum*s_ScrollValue);
+	if(g_FilesStartAt < 0)
+		g_FilesStartAt = 0;
+
+	g_FilesStopAt = g_FilesStartAt+Num;
+
+	g_FilesCur = 0;
+
 	// set clipping
-	UI()->ClipEnable(&view);
-	
+	UI()->ClipEnable(&View);
+
 	// the list
-	LISTDIRINFO info;
-	info.rect = &view;
-	info.editor = this;
-	engine_listdir(file_dialog_dirtypes, file_dialog_path, editor_listdir_callback, &info);
-	
+	CListDirInfo Info;
+	Info.m_pRect = &View;
+	Info.m_pEditor = this;
+
+	// TODO: lazy ass coding, should store the interface pointer somewere
+	Kernel()->RequestInterface<IStorage>()->ListDirectory(gs_FileDialogDirTypes, gs_aFileDialogPath, EditorListdirCallback, &Info);
+
 	// disable clipping again
 	UI()->ClipDisable();
-	
+
 	// the buttons
-	static int ok_button = 0;	
-	static int cancel_button = 0;	
+	static int s_OkButton = 0;
+	static int s_CancelButton = 0;
 
-	CUIRect button;
-	buttonbar.VSplitRight(50.0f, &buttonbar, &button);
-	if(DoButton_Editor(&ok_button, file_dialog_button_text, 0, &button, 0, 0) || inp_key_pressed(KEY_RETURN))
+	CUIRect Button;
+	ButtonBar.VSplitRight(50.0f, &ButtonBar, &Button);
+	if(DoButton_Editor(&s_OkButton, gs_pFileDialogButtonText, 0, &Button, 0, 0) || Input()->KeyPressed(KEY_RETURN))
 	{
-		if(file_dialog_func)
-			file_dialog_func(file_dialog_complete_filename, file_dialog_user);
-		dialog = DIALOG_NONE;
+		if(gs_pfnFileDialogFunc)
+			gs_pfnFileDialogFunc(gs_aFileDialogCompleteFilename, gs_pFileDialogUser);
+		m_Dialog = DIALOG_NONE;
 	}
 
-	buttonbar.VSplitRight(40.0f, &buttonbar, &button);
-	buttonbar.VSplitRight(50.0f, &buttonbar, &button);
-	if(DoButton_Editor(&cancel_button, "Cancel", 0, &button, 0, 0) || inp_key_pressed(KEY_ESCAPE))
-		dialog = DIALOG_NONE;
+	ButtonBar.VSplitRight(40.0f, &ButtonBar, &Button);
+	ButtonBar.VSplitRight(50.0f, &ButtonBar, &Button);
+	if(DoButton_Editor(&s_CancelButton, "Cancel", 0, &Button, 0, 0) || Input()->KeyPressed(KEY_ESCAPE))
+		m_Dialog = DIALOG_NONE;
 }
 
-void EDITOR::invoke_file_dialog(int listdirtypes, const char *title, const char *button_text,
-	const char *basepath, const char *default_name,
-	void (*func)(const char *filename, void *user), void *user)
+void CEditor::InvokeFileDialog(int ListDirTypes, const char *pTitle, const char *pButtonText,
+	const char *pBasePath, const char *pDefaultName,
+	void (*pfnFunc)(const char *pFileName, void *pUser), void *pUser)
 {
-	file_dialog_dirtypes = listdirtypes;
-	file_dialog_title = title;
-	file_dialog_button_text = button_text;
-	file_dialog_func = func;
-	file_dialog_user = user;
-	file_dialog_filename[0] = 0;
-	file_dialog_path[0] = 0;
-	
-	if(default_name)
-		strncpy(file_dialog_filename, default_name, sizeof(file_dialog_filename));
-	if(basepath)
-		strncpy(file_dialog_path, basepath, sizeof(file_dialog_path));
-		
-	dialog = DIALOG_FILE;
+	gs_FileDialogDirTypes = ListDirTypes;
+	gs_pFileDialogTitle = pTitle;
+	gs_pFileDialogButtonText = pButtonText;
+	gs_pfnFileDialogFunc = pfnFunc;
+	gs_pFileDialogUser = pUser;
+	gs_FileDialogFileName[0] = 0;
+	gs_aFileDialogPath[0] = 0;
+
+	if(pDefaultName)
+		str_copy(gs_FileDialogFileName, pDefaultName, sizeof(gs_FileDialogFileName));
+	if(pBasePath)
+		str_copy(gs_aFileDialogPath, pBasePath, sizeof(gs_aFileDialogPath));
+
+	m_Dialog = DIALOG_FILE;
 }
 
 
 
-void EDITOR::render_modebar(CUIRect view)
+void CEditor::RenderModebar(CUIRect View)
 {
-	CUIRect button;
+	CUIRect Button;
 
 	// mode buttons
 	{
-		view.VSplitLeft(40.0f, &button, &view);
-		static int tile_button = 0;
-		if(DoButton_ButtonM(&tile_button, "Layers", mode == MODE_LAYERS, &button, 0, "Switch to edit layers."))
-			mode = MODE_LAYERS;
-
-		view.VSplitLeft(40.0f, &button, &view);
-		static int img_button = 0;
-		if(DoButton_ButtonR(&img_button, "Images", mode == MODE_IMAGES, &button, 0, "Switch to manage images."))
-			mode = MODE_IMAGES;
+		View.VSplitLeft(65.0f, &Button, &View);
+		Button.HSplitTop(30.0f, 0, &Button);
+		static int s_Button = 0;
+		const char *pButName = m_Mode == MODE_LAYERS ? "Layers" : "Images";
+		if(DoButton_Tab(&s_Button, pButName, 0, &Button, 0, "Switch between images and layers managment."))
+		{
+		    if(m_Mode == MODE_LAYERS)
+                m_Mode = MODE_IMAGES;
+            else
+                m_Mode = MODE_LAYERS;
+		}
 	}
 
-	view.VSplitLeft(5.0f, 0, &view);
-	
-	// spacing
-	//view.VSplitLeft(10.0f, 0, &view);
+	View.VSplitLeft(5.0f, 0, &View);
 }
 
-void EDITOR::render_statusbar(CUIRect view)
+void CEditor::RenderStatusbar(CUIRect View)
 {
-	CUIRect button;
-	view.VSplitRight(60.0f, &view, &button);
-	static int envelope_button = 0;
-	if(DoButton_Editor(&envelope_button, "Envelopes", show_envelope_editor, &button, 0, "Toggles the envelope editor."))
-		show_envelope_editor = (show_envelope_editor+1)%4;
-	
-	if(tooltip)
+	CUIRect Button;
+	View.VSplitRight(60.0f, &View, &Button);
+	static int s_EnvelopeButton = 0;
+	if(DoButton_Editor(&s_EnvelopeButton, "Envelopes", m_ShowEnvelopeEditor, &Button, 0, "Toggles the envelope editor."))
+		m_ShowEnvelopeEditor = (m_ShowEnvelopeEditor+1)%4;
+
+	if(m_pTooltip)
 	{
-		if(ui_got_context && ui_got_context == UI()->HotItem())
+		if(ms_pUiGotContext && ms_pUiGotContext == UI()->HotItem())
 		{
-			char buf[512];
-			sprintf(buf, "%s Right click for context menu.", tooltip);
-			UI()->DoLabel(&view, buf, 10.0f, -1, -1);
+			char aBuf[512];
+			str_format(aBuf, sizeof(aBuf),"%s Right click for context menu.", m_pTooltip);
+			UI()->DoLabel(&View, aBuf, 10.0f, -1, -1);
 		}
 		else
-			UI()->DoLabel(&view, tooltip, 10.0f, -1, -1);
+			UI()->DoLabel(&View, m_pTooltip, 10.0f, -1, -1);
 	}
 }
 
-void EDITOR::render_envelopeeditor(CUIRect view)
+void CEditor::RenderEnvelopeEditor(CUIRect View)
 {
-	if(selected_envelope < 0) selected_envelope = 0;
-	if(selected_envelope >= map.envelopes.len()) selected_envelope--;
+	if(m_SelectedEnvelope < 0) m_SelectedEnvelope = 0;
+	if(m_SelectedEnvelope >= m_Map.m_lEnvelopes.size()) m_SelectedEnvelope--;
 
-	ENVELOPE *envelope = 0;
-	if(selected_envelope >= 0 && selected_envelope < map.envelopes.len())
-		envelope = map.envelopes[selected_envelope];
+	CEnvelope *pEnvelope = 0;
+	if(m_SelectedEnvelope >= 0 && m_SelectedEnvelope < m_Map.m_lEnvelopes.size())
+		pEnvelope = m_Map.m_lEnvelopes[m_SelectedEnvelope];
 
-	bool show_colorbar = false;
-	if(envelope && envelope->channels == 4)
-		show_colorbar = true;
+	bool ShowColorBar = false;
+	if(pEnvelope && pEnvelope->m_Channels == 4)
+		ShowColorBar = true;
 
-	CUIRect toolbar, curvebar, colorbar;
-	view.HSplitTop(15.0f, &toolbar, &view);
-	view.HSplitTop(15.0f, &curvebar, &view);
-	toolbar.Margin(2.0f, &toolbar);
-	curvebar.Margin(2.0f, &curvebar);
+	CUIRect ToolBar, CurveBar, ColorBar;
+	View.HSplitTop(15.0f, &ToolBar, &View);
+	View.HSplitTop(15.0f, &CurveBar, &View);
+	ToolBar.Margin(2.0f, &ToolBar);
+	CurveBar.Margin(2.0f, &CurveBar);
 
-	if(show_colorbar)
+	if(ShowColorBar)
 	{
-		view.HSplitTop(20.0f, &colorbar, &view);
-		colorbar.Margin(2.0f, &colorbar);
-		render_background(colorbar, checker_texture, 16.0f, 1.0f);
+		View.HSplitTop(20.0f, &ColorBar, &View);
+		ColorBar.Margin(2.0f, &ColorBar);
+		RenderBackground(ColorBar, ms_CheckerTexture, 16.0f, 1.0f);
 	}
 
-	render_background(view, checker_texture, 32.0f, 0.1f);
+	RenderBackground(View, ms_CheckerTexture, 32.0f, 0.1f);
 
 	// do the toolbar
 	{
-		CUIRect button;
-		ENVELOPE *new_env = 0;
-		
-		toolbar.VSplitRight(50.0f, &toolbar, &button);
-		static int new_4d_button = 0;
-		if(DoButton_Editor(&new_4d_button, "Color+", 0, &button, 0, "Creates a new color envelope"))
-			new_env = map.new_envelope(4);
-
-		toolbar.VSplitRight(5.0f, &toolbar, &button);
-		toolbar.VSplitRight(50.0f, &toolbar, &button);
-		static int new_2d_button = 0;
-		if(DoButton_Editor(&new_2d_button, "Pos.+", 0, &button, 0, "Creates a new pos envelope"))
-			new_env = map.new_envelope(3);
-		
-		if(new_env) // add the default points
+		CUIRect Button;
+		CEnvelope *pNewEnv = 0;
+
+		ToolBar.VSplitRight(50.0f, &ToolBar, &Button);
+		static int s_New4dButton = 0;
+		if(DoButton_Editor(&s_New4dButton, "Color+", 0, &Button, 0, "Creates a new color envelope"))
+			pNewEnv = m_Map.NewEnvelope(4);
+
+		ToolBar.VSplitRight(5.0f, &ToolBar, &Button);
+		ToolBar.VSplitRight(50.0f, &ToolBar, &Button);
+		static int s_New2dButton = 0;
+		if(DoButton_Editor(&s_New2dButton, "Pos.+", 0, &Button, 0, "Creates a new pos envelope"))
+			pNewEnv = m_Map.NewEnvelope(3);
+
+		if(pNewEnv) // add the default points
 		{
-			if(new_env->channels == 4)
+			if(pNewEnv->m_Channels == 4)
 			{
-				new_env->add_point(0, 1,1,1,1);
-				new_env->add_point(1000, 1,1,1,1);
+				pNewEnv->AddPoint(0, 1,1,1,1);
+				pNewEnv->AddPoint(1000, 1,1,1,1);
 			}
 			else
 			{
-				new_env->add_point(0, 0);
-				new_env->add_point(1000, 0);
+				pNewEnv->AddPoint(0, 0);
+				pNewEnv->AddPoint(1000, 0);
 			}
 		}
-		
-		CUIRect shifter, inc, dec;
-		toolbar.VSplitLeft(60.0f, &shifter, &toolbar);
-		shifter.VSplitRight(15.0f, &shifter, &inc);
-		shifter.VSplitLeft(15.0f, &dec, &shifter);
-		char buf[512];
-		sprintf(buf, "%d/%d", selected_envelope+1, map.envelopes.len());
-		RenderTools()->DrawUIRect(&shifter, vec4(1,1,1,0.5f), 0, 0.0f);
-		UI()->DoLabel(&shifter, buf, 10.0f, 0, -1);
-		
-		static int prev_button = 0;
-		if(DoButton_ButtonDec(&prev_button, 0, 0, &dec, 0, "Previous Envelope"))
-			selected_envelope--;
-		
-		static int next_button = 0;
-		if(DoButton_ButtonInc(&next_button, 0, 0, &inc, 0, "Next Envelope"))
-			selected_envelope++;
-			
-		if(envelope)
+
+		CUIRect Shifter, Inc, Dec;
+		ToolBar.VSplitLeft(60.0f, &Shifter, &ToolBar);
+		Shifter.VSplitRight(15.0f, &Shifter, &Inc);
+		Shifter.VSplitLeft(15.0f, &Dec, &Shifter);
+		char aBuf[512];
+		str_format(aBuf, sizeof(aBuf),"%d/%d", m_SelectedEnvelope+1, m_Map.m_lEnvelopes.size());
+		RenderTools()->DrawUIRect(&Shifter, vec4(1,1,1,0.5f), 0, 0.0f);
+		UI()->DoLabel(&Shifter, aBuf, 10.0f, 0, -1);
+
+		static int s_PrevButton = 0;
+		if(DoButton_ButtonDec(&s_PrevButton, 0, 0, &Dec, 0, "Previous Envelope"))
+			m_SelectedEnvelope--;
+
+		static int s_NextButton = 0;
+		if(DoButton_ButtonInc(&s_NextButton, 0, 0, &Inc, 0, "Next Envelope"))
+			m_SelectedEnvelope++;
+
+		if(pEnvelope)
 		{
-			toolbar.VSplitLeft(15.0f, &button, &toolbar);
-			toolbar.VSplitLeft(35.0f, &button, &toolbar);
-			UI()->DoLabel(&button, "Name:", 10.0f, -1, -1);
-
-			toolbar.VSplitLeft(80.0f, &button, &toolbar);
-			
-			static int name_box = 0;
-			DoEditBox(&name_box, &button, envelope->name, sizeof(envelope->name), 10.0f);
+			ToolBar.VSplitLeft(15.0f, &Button, &ToolBar);
+			ToolBar.VSplitLeft(35.0f, &Button, &ToolBar);
+			UI()->DoLabel(&Button, "Name:", 10.0f, -1, -1);
+
+			ToolBar.VSplitLeft(80.0f, &Button, &ToolBar);
+
+			static int s_NameBox = 0;
+			DoEditBox(&s_NameBox, &Button, pEnvelope->m_aName, sizeof(pEnvelope->m_aName), 10.0f);
 		}
 	}
-	
-	if(envelope)
+
+	if(pEnvelope)
 	{
-		static array<int> selection;
-		static int envelope_editor_id = 0;
-		static int active_channels = 0xf;
-		
-		if(envelope)
+		static array<int> Selection;
+		static int sEnvelopeEditorId = 0;
+		static int s_ActiveChannels = 0xf;
+
+		if(pEnvelope)
 		{
-			CUIRect button;	
-			
-			toolbar.VSplitLeft(15.0f, &button, &toolbar);
+			CUIRect Button;
+
+			ToolBar.VSplitLeft(15.0f, &Button, &ToolBar);
 
-			static const char *names[4][4] = {
+			static const char *s_paNames[4][4] = {
 				{"X", "", "", ""},
 				{"X", "Y", "", ""},
 				{"X", "Y", "R", ""},
 				{"R", "G", "B", "A"},
 			};
-			
-			static int channel_buttons[4] = {0};
-			int bit = 1;
-			/*ui_draw_button_func draw_func;*/
-			
-			for(int i = 0; i < envelope->channels; i++, bit<<=1)
+
+			static int s_aChannelButtons[4] = {0};
+			int Bit = 1;
+			//ui_draw_button_func draw_func;
+
+			for(int i = 0; i < pEnvelope->m_Channels; i++, Bit<<=1)
 			{
-				toolbar.VSplitLeft(15.0f, &button, &toolbar);
-				
+				ToolBar.VSplitLeft(15.0f, &Button, &ToolBar);
+
 				/*if(i == 0) draw_func = draw_editor_button_l;
 				else if(i == envelope->channels-1) draw_func = draw_editor_button_r;
 				else draw_func = draw_editor_button_m;*/
-				
-				if(DoButton_Editor(&channel_buttons[i], names[envelope->channels-1][i], active_channels&bit, &button, 0, 0))
-					active_channels ^= bit;
+
+				if(DoButton_Editor(&s_aChannelButtons[i], s_paNames[pEnvelope->m_Channels-1][i], s_ActiveChannels&Bit, &Button, 0, 0))
+					s_ActiveChannels ^= Bit;
 			}
-		}		
-		
-		float end_time = envelope->end_time();
-		if(end_time < 1)
-			end_time = 1;
-		
-		envelope->find_top_bottom(active_channels);
-		float top = envelope->top;
-		float bottom = envelope->bottom;
-		
-		if(top < 1)
-			top = 1;
-		if(bottom >= 0)
-			bottom = 0;
-		
-		float timescale = end_time/view.w;
-		float valuescale = (top-bottom)/view.h;
-		
-		if(UI()->MouseInside(&view))
-			UI()->SetHotItem(&envelope_editor_id);
-			
-		if(UI()->HotItem() == &envelope_editor_id)
+		}
+
+		float EndTime = pEnvelope->EndTime();
+		if(EndTime < 1)
+			EndTime = 1;
+
+		pEnvelope->FindTopBottom(s_ActiveChannels);
+		float Top = pEnvelope->m_Top;
+		float Bottom = pEnvelope->m_Bottom;
+
+		if(Top < 1)
+			Top = 1;
+		if(Bottom >= 0)
+			Bottom = 0;
+
+		float TimeScale = EndTime/View.w;
+		float ValueScale = (Top-Bottom)/View.h;
+
+		if(UI()->MouseInside(&View))
+			UI()->SetHotItem(&sEnvelopeEditorId);
+
+		if(UI()->HotItem() == &sEnvelopeEditorId)
 		{
 			// do stuff
-			if(envelope)
+			if(pEnvelope)
 			{
 				if(UI()->MouseButtonClicked(1))
 				{
 					// add point
-					int time = (int)(((UI()->MouseX()-view.x)*timescale)*1000.0f);
-					//float env_y = (UI()->MouseY()-view.y)/timescale;
-					float channels[4];
-					envelope->eval(time, channels);
-					envelope->add_point(time,
-						f2fx(channels[0]), f2fx(channels[1]),
-						f2fx(channels[2]), f2fx(channels[3]));
+					int Time = (int)(((UI()->MouseX()-View.x)*TimeScale)*1000.0f);
+					//float env_y = (UI()->MouseY()-view.y)/TimeScale;
+					float aChannels[4];
+					pEnvelope->Eval(Time, aChannels);
+					pEnvelope->AddPoint(Time,
+						f2fx(aChannels[0]), f2fx(aChannels[1]),
+						f2fx(aChannels[2]), f2fx(aChannels[3]));
 				}
-				
-				tooltip = "Press right mouse button to create a new point";
+
+				m_pTooltip = "Press right mouse button to create a new point";
 			}
 		}
 
-		vec3 colors[] = {vec3(1,0.2f,0.2f), vec3(0.2f,1,0.2f), vec3(0.2f,0.2f,1), vec3(1,1,0.2f)};
+		vec3 aColors[] = {vec3(1,0.2f,0.2f), vec3(0.2f,1,0.2f), vec3(0.2f,0.2f,1), vec3(1,1,0.2f)};
 
 		// render lines
 		{
-			UI()->ClipEnable(&view);
+			UI()->ClipEnable(&View);
 			Graphics()->TextureSet(-1);
 			Graphics()->LinesBegin();
-			for(int c = 0; c < envelope->channels; c++)
+			for(int c = 0; c < pEnvelope->m_Channels; c++)
 			{
-				if(active_channels&(1<<c))
-					Graphics()->SetColor(colors[c].r,colors[c].g,colors[c].b,1);
+				if(s_ActiveChannels&(1<<c))
+					Graphics()->SetColor(aColors[c].r,aColors[c].g,aColors[c].b,1);
 				else
-					Graphics()->SetColor(colors[c].r*0.5f,colors[c].g*0.5f,colors[c].b*0.5f,1);
-				
-				float prev_x = 0;
-				float results[4];
-				envelope->eval(0.000001f, results);
-				float prev_value = results[c];
-				
-				int steps = (int)((view.w/UI()->Screen()->w) * Graphics()->ScreenWidth());
-				for(int i = 1; i <= steps; i++)
+					Graphics()->SetColor(aColors[c].r*0.5f,aColors[c].g*0.5f,aColors[c].b*0.5f,1);
+
+				float PrevX = 0;
+				float aResults[4];
+				pEnvelope->Eval(0.000001f, aResults);
+				float PrevValue = aResults[c];
+
+				int Steps = (int)((View.w/UI()->Screen()->w) * Graphics()->ScreenWidth());
+				for(int i = 1; i <= Steps; i++)
 				{
-					float a = i/(float)steps;
-					envelope->eval(a*end_time, results);
-					float v = results[c];
-					v = (v-bottom)/(top-bottom);
-					
-					Graphics()->LinesDraw(view.x + prev_x*view.w, view.y+view.h - prev_value*view.h, view.x + a*view.w, view.y+view.h - v*view.h);
-					prev_x = a;
-					prev_value = v;
+					float a = i/(float)Steps;
+					pEnvelope->Eval(a*EndTime, aResults);
+					float v = aResults[c];
+					v = (v-Bottom)/(Top-Bottom);
+
+					IGraphics::CLineItem LineItem(View.x + PrevX*View.w, View.y+View.h - PrevValue*View.h, View.x + a*View.w, View.y+View.h - v*View.h);
+					Graphics()->LinesDraw(&LineItem, 1);
+					PrevX = a;
+					PrevValue = v;
 				}
 			}
 			Graphics()->LinesEnd();
 			UI()->ClipDisable();
 		}
-		
+
 		// render curve options
 		{
-			for(int i = 0; i < envelope->points.len()-1; i++)
+			for(int i = 0; i < pEnvelope->m_lPoints.size()-1; i++)
 			{
-				float t0 = envelope->points[i].time/1000.0f/end_time;
-				float t1 = envelope->points[i+1].time/1000.0f/end_time;
+				float t0 = pEnvelope->m_lPoints[i].m_Time/1000.0f/EndTime;
+				float t1 = pEnvelope->m_lPoints[i+1].m_Time/1000.0f/EndTime;
 
 				//dbg_msg("", "%f", end_time);
-				
+
 				CUIRect v;
-				v.x = curvebar.x + (t0+(t1-t0)*0.5f) * curvebar.w;
-				v.y = curvebar.y;
-				v.h = curvebar.h;
-				v.w = curvebar.h;
+				v.x = CurveBar.x + (t0+(t1-t0)*0.5f) * CurveBar.w;
+				v.y = CurveBar.y;
+				v.h = CurveBar.h;
+				v.w = CurveBar.h;
 				v.x -= v.w/2;
-				void *id = &envelope->points[i].curvetype;
-				const char *type_name[] = {
+				void *pId = &pEnvelope->m_lPoints[i].m_Curvetype;
+				const char *paTypeName[] = {
 					"N", "L", "S", "F", "M"
 					};
-				
-				if(DoButton_Editor(id, type_name[envelope->points[i].curvetype], 0, &v, 0, "Switch curve type"))
-					envelope->points[i].curvetype = (envelope->points[i].curvetype+1)%NUM_CURVETYPES;
+
+				if(DoButton_Editor(pId, paTypeName[pEnvelope->m_lPoints[i].m_Curvetype], 0, &v, 0, "Switch curve type"))
+					pEnvelope->m_lPoints[i].m_Curvetype = (pEnvelope->m_lPoints[i].m_Curvetype+1)%NUM_CURVETYPES;
 			}
 		}
-		
+
 		// render colorbar
-		if(show_colorbar)
+		if(ShowColorBar)
 		{
 			Graphics()->TextureSet(-1);
 			Graphics()->QuadsBegin();
-			for(int i = 0; i < envelope->points.len()-1; i++)
+			for(int i = 0; i < pEnvelope->m_lPoints.size()-1; i++)
 			{
-				float r0 = fx2f(envelope->points[i].values[0]);
-				float g0 = fx2f(envelope->points[i].values[1]);
-				float b0 = fx2f(envelope->points[i].values[2]);
-				float a0 = fx2f(envelope->points[i].values[3]);
-				float r1 = fx2f(envelope->points[i+1].values[0]);
-				float g1 = fx2f(envelope->points[i+1].values[1]);
-				float b1 = fx2f(envelope->points[i+1].values[2]);
-				float a1 = fx2f(envelope->points[i+1].values[3]);
-
-				Graphics()->SetColorVertex(0, r0, g0, b0, a0);
-				Graphics()->SetColorVertex(1, r1, g1, b1, a1);
-				Graphics()->SetColorVertex(2, r1, g1, b1, a1);
-				Graphics()->SetColorVertex(3, r0, g0, b0, a0);
-
-				float x0 = envelope->points[i].time/1000.0f/end_time;
+				float r0 = fx2f(pEnvelope->m_lPoints[i].m_aValues[0]);
+				float g0 = fx2f(pEnvelope->m_lPoints[i].m_aValues[1]);
+				float b0 = fx2f(pEnvelope->m_lPoints[i].m_aValues[2]);
+				float a0 = fx2f(pEnvelope->m_lPoints[i].m_aValues[3]);
+				float r1 = fx2f(pEnvelope->m_lPoints[i+1].m_aValues[0]);
+				float g1 = fx2f(pEnvelope->m_lPoints[i+1].m_aValues[1]);
+				float b1 = fx2f(pEnvelope->m_lPoints[i+1].m_aValues[2]);
+				float a1 = fx2f(pEnvelope->m_lPoints[i+1].m_aValues[3]);
+
+				IGraphics::CColorVertex Array[4] = {IGraphics::CColorVertex(0, r0, g0, b0, a0),
+													IGraphics::CColorVertex(1, r1, g1, b1, a1),
+													IGraphics::CColorVertex(2, r1, g1, b1, a1),
+													IGraphics::CColorVertex(3, r0, g0, b0, a0)};
+				Graphics()->SetColorVertex(Array, 4);
+
+				float x0 = pEnvelope->m_lPoints[i].m_Time/1000.0f/EndTime;
 //				float y0 = (fx2f(envelope->points[i].values[c])-bottom)/(top-bottom);
-				float x1 = envelope->points[i+1].time/1000.0f/end_time;
+				float x1 = pEnvelope->m_lPoints[i+1].m_Time/1000.0f/EndTime;
 				//float y1 = (fx2f(envelope->points[i+1].values[c])-bottom)/(top-bottom);
 				CUIRect v;
-				v.x = colorbar.x + x0*colorbar.w;
-				v.y = colorbar.y;
-				v.w = (x1-x0)*colorbar.w;
-				v.h = colorbar.h;
-				
-				Graphics()->QuadsDrawTL(v.x, v.y, v.w, v.h);
+				v.x = ColorBar.x + x0*ColorBar.w;
+				v.y = ColorBar.y;
+				v.w = (x1-x0)*ColorBar.w;
+				v.h = ColorBar.h;
+
+				IGraphics::CQuadItem QuadItem(v.x, v.y, v.w, v.h);
+				Graphics()->QuadsDrawTL(&QuadItem, 1);
 			}
 			Graphics()->QuadsEnd();
 		}
-		
+
 		// render handles
 		{
-			static bool move = false;
-			
-			int current_value = 0, current_time = 0;
-			
+			static bool s_Move = false;
+
+			int CurrentValue = 0, CurrentTime = 0;
+
 			Graphics()->TextureSet(-1);
 			Graphics()->QuadsBegin();
-			for(int c = 0; c < envelope->channels; c++)
+			for(int c = 0; c < pEnvelope->m_Channels; c++)
 			{
-				if(!(active_channels&(1<<c)))
+				if(!(s_ActiveChannels&(1<<c)))
 					continue;
-				
-				for(int i = 0; i < envelope->points.len(); i++)
+
+				for(int i = 0; i < pEnvelope->m_lPoints.size(); i++)
 				{
-					float x0 = envelope->points[i].time/1000.0f/end_time;
-					float y0 = (fx2f(envelope->points[i].values[c])-bottom)/(top-bottom);
-					CUIRect final;
-					final.x = view.x + x0*view.w;
-					final.y = view.y+view.h - y0*view.h;
-					final.x -= 2.0f;
-					final.y -= 2.0f;
-					final.w = 4.0f;
-					final.h = 4.0f;
-					
-					void *id = &envelope->points[i].values[c];
-					
-					if(UI()->MouseInside(&final))
-						UI()->SetHotItem(id);
-						
-					float colormod = 1.0f;
+					float x0 = pEnvelope->m_lPoints[i].m_Time/1000.0f/EndTime;
+					float y0 = (fx2f(pEnvelope->m_lPoints[i].m_aValues[c])-Bottom)/(Top-Bottom);
+					CUIRect Final;
+					Final.x = View.x + x0*View.w;
+					Final.y = View.y+View.h - y0*View.h;
+					Final.x -= 2.0f;
+					Final.y -= 2.0f;
+					Final.w = 4.0f;
+					Final.h = 4.0f;
 
-					if(UI()->ActiveItem() == id)
+					void *pId = &pEnvelope->m_lPoints[i].m_aValues[c];
+
+					if(UI()->MouseInside(&Final))
+						UI()->SetHotItem(pId);
+
+					float ColorMod = 1.0f;
+
+					if(UI()->ActiveItem() == pId)
 					{
 						if(!UI()->MouseButton(0))
 						{
 							UI()->SetActiveItem(0);
-							move = false;
+							s_Move = false;
 						}
 						else
 						{
-							envelope->points[i].values[c] -= f2fx(mouse_delta_y*valuescale);
-							if(inp_key_pressed(KEY_LSHIFT) || inp_key_pressed(KEY_RSHIFT))
+							pEnvelope->m_lPoints[i].m_aValues[c] -= f2fx(m_MouseDeltaY*ValueScale);
+							if(Input()->KeyPressed(KEY_LSHIFT) || Input()->KeyPressed(KEY_RSHIFT))
 							{
 								if(i != 0)
 								{
-									envelope->points[i].time += (int)((mouse_delta_x*timescale)*1000.0f);
-									if(envelope->points[i].time < envelope->points[i-1].time)
-										envelope->points[i].time = envelope->points[i-1].time + 1;
-									if(i+1 != envelope->points.len() && envelope->points[i].time > envelope->points[i+1].time)
-										envelope->points[i].time = envelope->points[i+1].time - 1;
+									pEnvelope->m_lPoints[i].m_Time += (int)((m_MouseDeltaX*TimeScale)*1000.0f);
+									if(pEnvelope->m_lPoints[i].m_Time < pEnvelope->m_lPoints[i-1].m_Time)
+										pEnvelope->m_lPoints[i].m_Time = pEnvelope->m_lPoints[i-1].m_Time + 1;
+									if(i+1 != pEnvelope->m_lPoints.size() && pEnvelope->m_lPoints[i].m_Time > pEnvelope->m_lPoints[i+1].m_Time)
+										pEnvelope->m_lPoints[i].m_Time = pEnvelope->m_lPoints[i+1].m_Time - 1;
 								}
 							}
 						}
-						
-						colormod = 100.0f;
+
+						ColorMod = 100.0f;
 						Graphics()->SetColor(1,1,1,1);
 					}
-					else if(UI()->HotItem() == id)
+					else if(UI()->HotItem() == pId)
 					{
 						if(UI()->MouseButton(0))
 						{
-							selection.clear();
-							selection.add(i);
-							UI()->SetActiveItem(id);
+							Selection.clear();
+							Selection.add(i);
+							UI()->SetActiveItem(pId);
 						}
 
 						// remove point
 						if(UI()->MouseButtonClicked(1))
-							envelope->points.removebyindex(i);
-							
-						colormod = 100.0f;
+							pEnvelope->m_lPoints.remove_index(i);
+
+						ColorMod = 100.0f;
 						Graphics()->SetColor(1,0.75f,0.75f,1);
-						tooltip = "Left mouse to drag. Hold shift to alter time point aswell. Right click to delete.";
+						m_pTooltip = "Left mouse to drag. Hold shift to alter time point aswell. Right click to delete.";
 					}
 
-					if(UI()->ActiveItem() == id || UI()->HotItem() == id)
+					if(UI()->ActiveItem() == pId || UI()->HotItem() == pId)
 					{
-						current_time = envelope->points[i].time;
-						current_value = envelope->points[i].values[c];
+						CurrentTime = pEnvelope->m_lPoints[i].m_Time;
+						CurrentValue = pEnvelope->m_lPoints[i].m_aValues[c];
 					}
-					
-					Graphics()->SetColor(colors[c].r*colormod, colors[c].g*colormod, colors[c].b*colormod, 1.0f);
-					Graphics()->QuadsDrawTL(final.x, final.y, final.w, final.h);
+
+					Graphics()->SetColor(aColors[c].r*ColorMod, aColors[c].g*ColorMod, aColors[c].b*ColorMod, 1.0f);
+					IGraphics::CQuadItem QuadItem(Final.x, Final.y, Final.w, Final.h);
+					Graphics()->QuadsDrawTL(&QuadItem, 1);
 				}
 			}
 			Graphics()->QuadsEnd();
 
-			char buf[512];
-			sprintf(buf, "%.3f %.3f", current_time/1000.0f, fx2f(current_value));
-			UI()->DoLabel(&toolbar, buf, 10.0f, 0, -1);
+			char aBuf[512];
+			str_format(aBuf, sizeof(aBuf),"%.3f %.3f", CurrentTime/1000.0f, fx2f(CurrentValue));
+			UI()->DoLabel(&ToolBar, aBuf, 10.0f, 0, -1);
 		}
 	}
 }
 
-int EDITOR::popup_menu_file(EDITOR *pEditor, CUIRect view)
+int CEditor::PopupMenuFile(CEditor *pEditor, CUIRect View)
 {
-	static int new_map_button = 0;
-	static int save_button = 0;
-	static int save_as_button = 0;
-	static int open_button = 0;
-	static int append_button = 0;
-	static int exit_button = 0;
-
-	CUIRect slot;
-	view.HSplitTop(2.0f, &slot, &view);
-	view.HSplitTop(12.0f, &slot, &view);
-	if(pEditor->DoButton_MenuItem(&new_map_button, "New", 0, &slot, 0, "Creates a new map"))
-	{
-		pEditor->reset();
+	static int s_NewMapButton = 0;
+	static int s_SaveButton = 0;
+	static int s_SaveAsButton = 0;
+	static int s_OpenButton = 0;
+	static int s_AppendButton = 0;
+	static int s_ExitButton = 0;
+
+	CUIRect Slot;
+	View.HSplitTop(2.0f, &Slot, &View);
+	View.HSplitTop(12.0f, &Slot, &View);
+	if(pEditor->DoButton_MenuItem(&s_NewMapButton, "New", 0, &Slot, 0, "Creates a new map"))
+	{
+		pEditor->Reset();
+		pEditor->m_aFileName[0] = 0;
 		return 1;
 	}
 
-	view.HSplitTop(10.0f, &slot, &view);
-	view.HSplitTop(12.0f, &slot, &view);
-	if(pEditor->DoButton_MenuItem(&open_button, "Open", 0, &slot, 0, "Opens a map for editing"))
+	View.HSplitTop(10.0f, &Slot, &View);
+	View.HSplitTop(12.0f, &Slot, &View);
+	if(pEditor->DoButton_MenuItem(&s_OpenButton, "Open", 0, &Slot, 0, "Opens a map for editing"))
 	{
-		pEditor->invoke_file_dialog(LISTDIRTYPE_ALL, "Open Map", "Open", "maps/", "", callback_open_map, pEditor);
+		pEditor->InvokeFileDialog(IStorage::TYPE_ALL, "Open Map", "Open", "maps/", "", CallbackOpenMap, pEditor);
 		return 1;
 	}
 
-	view.HSplitTop(10.0f, &slot, &view);
-	view.HSplitTop(12.0f, &slot, &view);
-	if(pEditor->DoButton_MenuItem(&append_button, "Append", 0, &slot, 0, "Opens a map and adds everything from that map to the current one"))
+	View.HSplitTop(10.0f, &Slot, &View);
+	View.HSplitTop(12.0f, &Slot, &View);
+	if(pEditor->DoButton_MenuItem(&s_AppendButton, "Append", 0, &Slot, 0, "Opens a map and adds everything from that map to the current one"))
 	{
-		pEditor->invoke_file_dialog(LISTDIRTYPE_ALL, "Append Map", "Append", "maps/", "", callback_append_map, pEditor);
+		pEditor->InvokeFileDialog(IStorage::TYPE_ALL, "Append Map", "Append", "maps/", "", CallbackAppendMap, pEditor);
 		return 1;
 	}
 
-	view.HSplitTop(10.0f, &slot, &view);
-	view.HSplitTop(12.0f, &slot, &view);
-	if(pEditor->DoButton_MenuItem(&save_button, "Save (NOT IMPL)", 0, &slot, 0, "Saves the current map"))
+	View.HSplitTop(10.0f, &Slot, &View);
+	View.HSplitTop(12.0f, &Slot, &View);
+	if(pEditor->DoButton_MenuItem(&s_SaveButton, "Save", 0, &Slot, 0, "Saves the current map"))
 	{
+		if(pEditor->m_aFileName[0])	
+			pEditor->Save(pEditor->m_aFileName);
+		else
+			pEditor->InvokeFileDialog(IStorage::TYPE_SAVE, "Save Map", "Save", "maps/", "", CallbackSaveMap, pEditor);
 		return 1;
 	}
 
-	view.HSplitTop(2.0f, &slot, &view);
-	view.HSplitTop(12.0f, &slot, &view);
-	if(pEditor->DoButton_MenuItem(&save_as_button, "Save As", 0, &slot, 0, "Saves the current map under a new name"))
+	View.HSplitTop(2.0f, &Slot, &View);
+	View.HSplitTop(12.0f, &Slot, &View);
+	if(pEditor->DoButton_MenuItem(&s_SaveAsButton, "Save As", 0, &Slot, 0, "Saves the current map under a new name"))
 	{
-		pEditor->invoke_file_dialog(LISTDIRTYPE_SAVE, "Save Map", "Save", "maps/", "", callback_save_map, pEditor);
+		pEditor->InvokeFileDialog(IStorage::TYPE_SAVE, "Save Map", "Save", "maps/", "", CallbackSaveMap, pEditor);
 		return 1;
 	}
-	
-	view.HSplitTop(10.0f, &slot, &view);
-	view.HSplitTop(12.0f, &slot, &view);
-	if(pEditor->DoButton_MenuItem(&exit_button, "Exit", 0, &slot, 0, "Exits from the editor"))
+
+	View.HSplitTop(10.0f, &Slot, &View);
+	View.HSplitTop(12.0f, &Slot, &View);
+	if(pEditor->DoButton_MenuItem(&s_ExitButton, "Exit", 0, &Slot, 0, "Exits from the editor"))
 	{
-		config.cl_editor = 0;
+		g_Config.m_ClEditor = 0;
 		return 1;
-	}	
-		
+	}
+
 	return 0;
 }
 
-void EDITOR::render_menubar(CUIRect menubar)
+void CEditor::RenderMenubar(CUIRect MenuBar)
 {
-	static CUIRect file /*, view, help*/;
+	static CUIRect s_File /*, view, help*/;
+
+	MenuBar.VSplitLeft(60.0f, &s_File, &MenuBar);
+	if(DoButton_Menu(&s_File, "File", 0, &s_File, 0, 0))
+		UiInvokePopupMenu(&s_File, 1, s_File.x, s_File.y+s_File.h-1.0f, 120, 150, PopupMenuFile, this);
 
-	menubar.VSplitLeft(60.0f, &file, &menubar);
-	if(DoButton_Menu(&file, "File", 0, &file, 0, 0))
-		ui_invoke_popup_menu(&file, 1, file.x, file.y+file.h-1.0f, 120, 150, popup_menu_file, this);
-	
 	/*
 	menubar.VSplitLeft(5.0f, 0, &menubar);
 	menubar.VSplitLeft(60.0f, &view, &menubar);
@@ -2615,288 +2538,320 @@ void EDITOR::render_menubar(CUIRect menubar)
 		*/
 }
 
-void EDITOR::render()
+void CEditor::Render()
 {
 	// basic start
-	Graphics()->Clear(1.0f,0.0f,1.0f);
-	CUIRect view = *UI()->Screen();
+	Graphics()->Clear(1.0f, 0.0f, 1.0f);
+	CUIRect View = *UI()->Screen();
 	Graphics()->MapScreen(UI()->Screen()->x, UI()->Screen()->y, UI()->Screen()->w, UI()->Screen()->h);
-	
+
 	// reset tip
-	tooltip = 0;
-	
+	m_pTooltip = 0;
+
 	// render checker
-	render_background(view, checker_texture, 32.0f, 1.0f);
-	
-	CUIRect menubar, modebar, toolbar, statusbar, envelope_editor, toolbox;
-	
-	if(gui_active)
+	RenderBackground(View, ms_CheckerTexture, 32.0f, 1.0f);
+
+	CUIRect MenuBar, CModeBar, ToolBar, StatusBar, EnvelopeEditor, ToolBox;
+
+	if(m_GuiActive)
 	{
-		
-		view.HSplitTop(16.0f, &menubar, &view);
-		view.VSplitLeft(80.0f, &toolbox, &view);
-		view.HSplitTop(16.0f, &toolbar, &view);
-		view.HSplitBottom(16.0f, &view, &statusbar);
 
-		if(show_envelope_editor)
+		View.HSplitTop(16.0f, &MenuBar, &View);
+		View.HSplitTop(53.0f, &ToolBar, &View);
+		View.VSplitLeft(80.0f, &ToolBox, &View);
+		View.HSplitBottom(16.0f, &View, &StatusBar);
+
+		if(m_ShowEnvelopeEditor)
 		{
 			float size = 125.0f;
-			if(show_envelope_editor == 2)
+			if(m_ShowEnvelopeEditor == 2)
 				size *= 2.0f;
-			else if(show_envelope_editor == 3)
+			else if(m_ShowEnvelopeEditor == 3)
 				size *= 3.0f;
-			view.HSplitBottom(size, &view, &envelope_editor);
+			View.HSplitBottom(size, &View, &EnvelopeEditor);
 		}
 	}
-	
+
 	//	a little hack for now
-	if(mode == MODE_LAYERS)
-		do_map_editor(view, toolbar);
-	
-	if(gui_active)
+	if(m_Mode == MODE_LAYERS)
+		DoMapEditor(View, ToolBar);
+
+	if(m_GuiActive)
 	{
-		float brightness = 0.25f;
-		render_background(menubar, background_texture, 128.0f, brightness*0);
-		menubar.Margin(2.0f, &menubar);
+		float Brightness = 0.25f;
+		RenderBackground(MenuBar, ms_BackgroundTexture, 128.0f, Brightness*0);
+		MenuBar.Margin(2.0f, &MenuBar);
 
-		render_background(toolbox, background_texture, 128.0f, brightness);
-		toolbox.Margin(2.0f, &toolbox);
-		
-		render_background(toolbar, background_texture, 128.0f, brightness);
-		toolbar.Margin(2.0f, &toolbar);
-		toolbar.VSplitLeft(150.0f, &modebar, &toolbar);
+		RenderBackground(ToolBox, ms_BackgroundTexture, 128.0f, Brightness);
+		ToolBox.Margin(2.0f, &ToolBox);
+
+		RenderBackground(ToolBar, ms_BackgroundTexture, 128.0f, Brightness);
+		ToolBar.Margin(2.0f, &ToolBar);
+		ToolBar.VSplitLeft(100.0f, &CModeBar, &ToolBar);
+
+		RenderBackground(StatusBar, ms_BackgroundTexture, 128.0f, Brightness);
+		StatusBar.Margin(2.0f, &StatusBar);
 
-		render_background(statusbar, background_texture, 128.0f, brightness);
-		statusbar.Margin(2.0f, &statusbar);
-		
 		// do the toolbar
-		if(mode == MODE_LAYERS)
-			do_toolbar(toolbar);
-		
-		if(show_envelope_editor)
+		if(m_Mode == MODE_LAYERS)
+			DoToolbar(ToolBar);
+
+		if(m_ShowEnvelopeEditor)
 		{
-			render_background(envelope_editor, background_texture, 128.0f, brightness);
-			envelope_editor.Margin(2.0f, &envelope_editor);
+			RenderBackground(EnvelopeEditor, ms_BackgroundTexture, 128.0f, Brightness);
+			EnvelopeEditor.Margin(2.0f, &EnvelopeEditor);
 		}
 	}
-		
-	
-	if(mode == MODE_LAYERS)
-		render_layers(toolbox, toolbar, view);
-	else if(mode == MODE_IMAGES)
-		render_images(toolbox, toolbar, view);
+
+
+	if(m_Mode == MODE_LAYERS)
+		RenderLayers(ToolBox, ToolBar, View);
+	else if(m_Mode == MODE_IMAGES)
+		RenderImages(ToolBox, ToolBar, View);
 
 	Graphics()->MapScreen(UI()->Screen()->x, UI()->Screen()->y, UI()->Screen()->w, UI()->Screen()->h);
 
-	if(gui_active)
+	if(m_GuiActive)
 	{
-		render_menubar(menubar);
-		
-		render_modebar(modebar);
-		if(show_envelope_editor)
-			render_envelopeeditor(envelope_editor);
+		RenderMenubar(MenuBar);
+
+		RenderModebar(CModeBar);
+		if(m_ShowEnvelopeEditor)
+			RenderEnvelopeEditor(EnvelopeEditor);
 	}
 
-	if(dialog == DIALOG_FILE)
+	if(m_Dialog == DIALOG_FILE)
 	{
-		static int null_ui_target = 0;
-		UI()->SetHotItem(&null_ui_target);
-		render_file_dialog();
+		static int s_NullUiTarget = 0;
+		UI()->SetHotItem(&s_NullUiTarget);
+		RenderFileDialog();
 	}
-	
-	
-	ui_do_popup_menu();
 
-	if(gui_active)
-		render_statusbar(statusbar);
+
+	UiDoPopupMenu();
+
+	if(m_GuiActive)
+		RenderStatusbar(StatusBar);
 
 	//
-	if(config.ed_showkeys)
+	if(g_Config.m_EdShowkeys)
 	{
 		Graphics()->MapScreen(UI()->Screen()->x, UI()->Screen()->y, UI()->Screen()->w, UI()->Screen()->h);
-		TEXT_CURSOR cursor;
-		gfx_text_set_cursor(&cursor, view.x+10, view.y+view.h-24-10, 24.0f, TEXTFLAG_RENDER);
-		
-		int nkeys = 0;
+		CTextCursor Cursor;
+		TextRender()->SetCursor(&Cursor, View.x+10, View.y+View.h-24-10, 24.0f, TEXTFLAG_RENDER);
+
+		int NKeys = 0;
 		for(int i = 0; i < KEY_LAST; i++)
 		{
-			if(inp_key_pressed(i))
+			if(Input()->KeyPressed(i))
 			{
-				if(nkeys)
-					gfx_text_ex(&cursor, " + ", -1);
-				gfx_text_ex(&cursor, inp_key_name(i), -1);
-				nkeys++;
+				if(NKeys)
+					TextRender()->TextEx(&Cursor, " + ", -1);
+				TextRender()->TextEx(&Cursor, Input()->KeyName(i), -1);
+				NKeys++;
 			}
 		}
 	}
-	
-	if(show_mouse_pointer)
+
+	if(m_ShowMousePointer)
 	{
 		// render butt ugly mouse cursor
 		float mx = UI()->MouseX();
 		float my = UI()->MouseY();
-		Graphics()->TextureSet(cursor_texture);
+		Graphics()->TextureSet(ms_CursorTexture);
 		Graphics()->QuadsBegin();
-		if(ui_got_context == UI()->HotItem())
+		if(ms_pUiGotContext == UI()->HotItem())
 			Graphics()->SetColor(1,0,0,1);
-		Graphics()->QuadsDrawTL(mx,my, 16.0f, 16.0f);
+		IGraphics::CQuadItem QuadItem(mx,my, 16.0f, 16.0f);
+		Graphics()->QuadsDrawTL(&QuadItem, 1);
 		Graphics()->QuadsEnd();
 	}
-	
+
 }
 
-void EDITOR::reset(bool create_default)
+void CEditor::Reset(bool CreateDefault)
 {
-	map.clean();
+	m_Map.Clean();
 
 	// create default layers
-	if(create_default)
-		map.create_default(entities_texture);
-	
+	if(CreateDefault)
+		m_Map.CreateDefault(ms_EntitiesTexture);
+
 	/*
 	{
 	}*/
-	
-	selected_layer = 0;
-	selected_group = 0;
-	selected_quad = -1;
-	selected_points = 0;
-	selected_envelope = 0;
-	selected_image = 0;
+
+	m_SelectedLayer = 0;
+	m_SelectedGroup = 0;
+	m_SelectedQuad = -1;
+	m_SelectedPoints = 0;
+	m_SelectedEnvelope = 0;
+	m_SelectedImage = 0;
 }
 
-void MAP::make_game_layer(LAYER *layer)
+void CEditorMap::MakeGameLayer(CLayer *pLayer)
 {
-	game_layer = (LAYER_GAME *)layer;
-	game_layer->tex_id = entities_texture;
+	m_pGameLayer = (CLayerGame *)pLayer;
+	m_pGameLayer->m_pEditor = m_pEditor;
+	m_pGameLayer->m_TexId = m_pEditor->ms_EntitiesTexture;
 }
 
-void MAP::make_game_group(LAYERGROUP *group)
+void CEditorMap::MakeGameGroup(CLayerGroup *pGroup)
 {
-	game_group = group;
-	game_group->game_group = true;
-	game_group->name = "Game";
+	m_pGameGroup = pGroup;
+	m_pGameGroup->m_GameGroup = true;
+	m_pGameGroup->m_pName = "Game";
 }
 
 
 
-void MAP::clean()
+void CEditorMap::Clean()
 {
-	groups.deleteall();
-	envelopes.deleteall();
-	images.deleteall();
-	
-	game_layer = 0x0;
-	game_group = 0x0;
+	m_lGroups.delete_all();
+	m_lEnvelopes.delete_all();
+	m_lImages.delete_all();
+
+	m_pGameLayer = 0x0;
+	m_pGameGroup = 0x0;
 }
 
-void MAP::create_default(int entities_texture)
+void CEditorMap::CreateDefault(int EntitiesTexture)
 {
-	make_game_group(new_group());
-	make_game_layer(new LAYER_GAME(50, 50));
-	game_group->add_layer(game_layer);
+	MakeGameGroup(NewGroup());
+	MakeGameLayer(new CLayerGame(50, 50));
+	m_pGameGroup->AddLayer(m_pGameLayer);
 }
 
-void EDITOR::Init(class IGraphics *pGraphics)
+void CEditor::Init()
 {
-	m_pGraphics = pGraphics;
-	
-	checker_texture = Graphics()->LoadTexture("editor/checker.png", IMG_AUTO, 0);
-	background_texture = Graphics()->LoadTexture("editor/background.png", IMG_AUTO, 0);
-	cursor_texture = Graphics()->LoadTexture("editor/cursor.png", IMG_AUTO, 0);
-	entities_texture = Graphics()->LoadTexture("editor/entities.png", IMG_AUTO, 0);
-	
-	tileset_picker.make_palette();
-	tileset_picker.readonly = true;
-	
-	reset();
+	m_pInput = Kernel()->RequestInterface<IInput>();
+	m_pClient = Kernel()->RequestInterface<IClient>();
+	m_pGraphics = Kernel()->RequestInterface<IGraphics>();
+	m_pTextRender = Kernel()->RequestInterface<ITextRender>();
+	m_RenderTools.m_pGraphics = m_pGraphics;
+	m_RenderTools.m_pUI = &m_UI;
+	m_UI.SetGraphics(m_pGraphics, m_pTextRender);
+	m_Map.m_pEditor = this;
+
+	ms_CheckerTexture = Graphics()->LoadTexture("editor/checker.png", CImageInfo::FORMAT_AUTO, 0);
+	ms_BackgroundTexture = Graphics()->LoadTexture("editor/background.png", CImageInfo::FORMAT_AUTO, 0);
+	ms_CursorTexture = Graphics()->LoadTexture("editor/cursor.png", CImageInfo::FORMAT_AUTO, 0);
+	ms_EntitiesTexture = Graphics()->LoadTexture("editor/entities.png", CImageInfo::FORMAT_AUTO, 0);
+
+	m_TilesetPicker.m_pEditor = this;
+	m_TilesetPicker.MakePalette();
+	m_TilesetPicker.m_Readonly = true;
+
+	m_Brush.m_pMap = &m_Map;
+
+	Reset();
 }
 
-void EDITOR::UpdateAndRender()
+void CEditor::DoMapBorder()
 {
-	static int mouse_x = 0;
-	static int mouse_y = 0;
-	
-	if(animate)
-		animate_time = (time_get()-animate_start)/(float)time_freq();
+    CLayerTiles *pT = (CLayerTiles *)GetSelectedLayerType(0, LAYERTYPE_TILES);
+    
+    for(int i = 0; i < pT->m_Width*2; ++i)
+        pT->m_pTiles[i].m_Index = 1;
+        
+    for(int i = 0; i < pT->m_Width*pT->m_Height; ++i)
+    {
+        if(i%pT->m_Width < 2 || i%pT->m_Width > pT->m_Width-3)
+            pT->m_pTiles[i].m_Index = 1;
+    }
+    
+    for(int i = ((pT->m_Width-2)*pT->m_Height); i < pT->m_Width*pT->m_Height; ++i)
+        pT->m_pTiles[i].m_Index = 1;
+}
+
+void CEditor::UpdateAndRender()
+{
+	static int s_MouseX = 0;
+	static int s_MouseY = 0;
+
+	if(m_Animate)
+		m_AnimateTime = (time_get()-m_AnimateStart)/(float)time_freq();
 	else
-		animate_time = 0;
-	ui_got_context = 0;
+		m_AnimateTime = 0;
+	ms_pUiGotContext = 0;
 
 	// handle mouse movement
-	float mx, my, mwx, mwy;
+	float mx, my, Mwx, Mwy;
 	int rx, ry;
 	{
-		inp_mouse_relative(&rx, &ry);
-		mouse_delta_x = rx;
-		mouse_delta_y = ry;
-		
-		if(!lock_mouse)
+		Input()->MouseRelative(&rx, &ry);
+		m_MouseDeltaX = rx;
+		m_MouseDeltaY = ry;
+
+		if(!m_LockMouse)
 		{
-			mouse_x += rx;
-			mouse_y += ry;
+			s_MouseX += rx;
+			s_MouseY += ry;
 		}
-		
-		if(mouse_x < 0) mouse_x = 0;
-		if(mouse_y < 0) mouse_y = 0;
-		if(mouse_x > UI()->Screen()->w) mouse_x = (int)UI()->Screen()->w;
-		if(mouse_y > UI()->Screen()->h) mouse_y = (int)UI()->Screen()->h;
+
+		if(s_MouseX < 0) s_MouseX = 0;
+		if(s_MouseY < 0) s_MouseY = 0;
+		if(s_MouseX > UI()->Screen()->w) s_MouseX = (int)UI()->Screen()->w;
+		if(s_MouseY > UI()->Screen()->h) s_MouseY = (int)UI()->Screen()->h;
 
 		// update the ui
-		mx = mouse_x;
-		my = mouse_y;
-		mwx = 0;
-		mwy = 0;
-		
+		mx = s_MouseX;
+		my = s_MouseY;
+		Mwx = 0;
+		Mwy = 0;
+
 		// fix correct world x and y
-		LAYERGROUP *g = get_selected_group();
+		CLayerGroup *g = GetSelectedGroup();
 		if(g)
 		{
-			float points[4];
-			g->mapping(points);
-
-			float world_width = points[2]-points[0];
-			float world_height = points[3]-points[1];
-			
-			mwx = points[0] + world_width * (mouse_x/UI()->Screen()->w);
-			mwy = points[1] + world_height * (mouse_y/UI()->Screen()->h);
-			mouse_delta_wx = mouse_delta_x*(world_width / UI()->Screen()->w);
-			mouse_delta_wy = mouse_delta_y*(world_height / UI()->Screen()->h);
+			float aPoints[4];
+			g->Mapping(aPoints);
+
+			float WorldWidth = aPoints[2]-aPoints[0];
+			float WorldHeight = aPoints[3]-aPoints[1];
+
+			Mwx = aPoints[0] + WorldWidth * (s_MouseX/UI()->Screen()->w);
+			Mwy = aPoints[1] + WorldHeight * (s_MouseY/UI()->Screen()->h);
+			m_MouseDeltaWx = m_MouseDeltaX*(WorldWidth / UI()->Screen()->w);
+			m_MouseDeltaWy = m_MouseDeltaY*(WorldHeight / UI()->Screen()->h);
 		}
-		
-		int buttons = 0;
-		if(inp_key_pressed(KEY_MOUSE_1)) buttons |= 1;
-		if(inp_key_pressed(KEY_MOUSE_2)) buttons |= 2;
-		if(inp_key_pressed(KEY_MOUSE_3)) buttons |= 4;
-		
-		UI()->Update(mx,my,mwx,mwy,buttons);
+
+		int Buttons = 0;
+		if(Input()->KeyPressed(KEY_MOUSE_1)) Buttons |= 1;
+		if(Input()->KeyPressed(KEY_MOUSE_2)) Buttons |= 2;
+		if(Input()->KeyPressed(KEY_MOUSE_3)) Buttons |= 4;
+
+		UI()->Update(mx,my,Mwx,Mwy,Buttons);
 	}
-	
+
 	// toggle gui
-	if(inp_key_down(KEY_TAB))
-		gui_active = !gui_active;
+	if(Input()->KeyDown(KEY_TAB))
+		m_GuiActive = !m_GuiActive;
 
-	if(inp_key_down(KEY_F5))
-		save("maps/debug_test2.map");
+	if(Input()->KeyDown(KEY_F5))
+		Save("maps/debug_test2.map");
 
-	if(inp_key_down(KEY_F6))
-		load("maps/debug_test2.map");
-	
-	if(inp_key_down(KEY_F8))
-		load("maps/debug_test.map");
+	if(Input()->KeyDown(KEY_F6))
+		Load("maps/debug_test2.map");
 	
-	if(inp_key_down(KEY_F10))
-		show_mouse_pointer = false;
+	if(Input()->KeyDown(KEY_F8))
+		Load("maps/debug_test.map");
 	
-	render();
-	
-	if(inp_key_down(KEY_F10))
+	if(Input()->KeyDown(KEY_F7))
+		Save("maps/quicksave.map");
+
+	if(Input()->KeyDown(KEY_F10))
+		m_ShowMousePointer = false;
+
+	Render();
+
+	if(Input()->KeyDown(KEY_F10))
 	{
 		Graphics()->TakeScreenshot();
-		show_mouse_pointer = true;
+		m_ShowMousePointer = true;
 	}
-	
-	inp_clear_events();
+
+	Input()->ClearEvents();
 }
 
-IEditor *CreateEditor() { return new EDITOR; }
+IEditor *CreateEditor() { return new CEditor; }
diff --git a/src/game/editor/ed_editor.h b/src/game/editor/ed_editor.h
new file mode 100644
index 00000000..1730fb0a
--- /dev/null
+++ b/src/game/editor/ed_editor.h
@@ -0,0 +1,615 @@
+#ifndef GAME_EDITOR_ED_EDITOR_H
+#define GAME_EDITOR_ED_EDITOR_H
+
+#include <base/system.h>
+#include <base/math.h>
+#include <base/tl/array.h>
+#include <base/tl/algorithm.h>
+
+#include <math.h>
+#include <game/mapitems.h>
+#include <game/client/render.h>
+
+#include <engine/shared/datafile.h>
+#include <engine/shared/config.h>
+#include <engine/editor.h>
+#include <engine/graphics.h>
+
+#include <game/client/ui.h>
+
+typedef void (*INDEX_MODIFY_FUNC)(int *pIndex);
+
+//CRenderTools m_RenderTools;
+
+// CEditor SPECIFIC
+enum
+{
+	MODE_LAYERS=0,
+	MODE_IMAGES,
+	
+	DIALOG_NONE=0,
+	DIALOG_FILE,
+};
+
+struct CEntity
+{
+	CPoint m_Position;
+	int m_Type;
+};
+
+class CEnvelope
+{
+public:
+	int m_Channels;
+	array<CEnvPoint> m_lPoints;
+	char m_aName[32];
+	float m_Bottom, m_Top;
+	
+	CEnvelope(int Chan)
+	{
+		m_Channels = Chan;
+		m_aName[0] = 0;
+		m_Bottom = 0;
+		m_Top = 0;
+	}
+	
+	void Resort()
+	{
+		sort(m_lPoints.all());
+		FindTopBottom(0xf);
+	}
+
+	void FindTopBottom(int ChannelMask)
+	{
+		m_Top = -1000000000.0f;
+		m_Bottom = 1000000000.0f;
+		for(int i = 0; i < m_lPoints.size(); i++)
+		{
+			for(int c = 0; c < m_Channels; c++)
+			{
+				if(ChannelMask&(1<<c))
+				{
+					float v = fx2f(m_lPoints[i].m_aValues[c]);
+					if(v > m_Top) m_Top = v;
+					if(v < m_Bottom) m_Bottom = v;
+				}
+			}
+		}
+	}
+	
+	int Eval(float Time, float *pResult)
+	{
+		CRenderTools::RenderEvalEnvelope(m_lPoints.base_ptr(), m_lPoints.size(), m_Channels, Time, pResult);
+		return m_Channels;
+	}
+	
+	void AddPoint(int Time, int v0, int v1=0, int v2=0, int v3=0)
+	{
+		CEnvPoint p;
+		p.m_Time = Time;
+		p.m_aValues[0] = v0;
+		p.m_aValues[1] = v1;
+		p.m_aValues[2] = v2;
+		p.m_aValues[3] = v3;
+		p.m_Curvetype = CURVETYPE_LINEAR;
+		m_lPoints.add(p);
+		Resort();
+	}
+	
+	float EndTime()
+	{
+		if(m_lPoints.size())
+			return m_lPoints[m_lPoints.size()-1].m_Time*(1.0f/1000.0f);
+		return 0;
+	}
+};
+
+
+class CLayer;
+class CLayerGroup;
+class CEditorMap;
+
+class CLayer
+{
+public:
+	class CEditor *m_pEditor;
+	class IGraphics *Graphics();
+	class ITextRender *TextRender();
+
+	CLayer()
+	{
+		m_Type = LAYERTYPE_INVALID;
+		m_pTypeName = "(invalid)";
+		m_Visible = true;
+		m_Readonly = false;
+		m_Flags = 0;
+		m_pEditor = 0;
+	}
+	
+	virtual ~CLayer()
+	{
+	}
+	
+	
+	virtual void BrushSelecting(CUIRect Rect) {}
+	virtual int BrushGrab(CLayerGroup *pBrush, CUIRect Rect) { return 0; }
+	virtual void FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect) {}
+	virtual void BrushDraw(CLayer *pBrush, float x, float y) {}
+	virtual void BrushPlace(CLayer *pBrush, float x, float y) {}
+	virtual void BrushFlipX() {}
+	virtual void BrushFlipY() {}
+	virtual void BrushRotate(float Amount) {}
+	
+	virtual void Render() {}
+	virtual int RenderProperties(CUIRect *pToolbox) { return 0; }
+	
+	virtual void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc) {}
+	virtual void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc) {}
+	
+	virtual void GetSize(float *w, float *h) { *w = 0; *h = 0;}
+	
+	const char *m_pTypeName;
+	int m_Type;
+	int m_Flags;
+
+	bool m_Readonly;
+	bool m_Visible;
+};
+
+class CLayerGroup
+{
+public:
+	class CEditorMap *m_pMap;
+	
+	array<CLayer*> m_lLayers;
+	
+	int m_OffsetX;
+	int m_OffsetY;
+
+	int m_ParallaxX;
+	int m_ParallaxY;
+	
+	int m_UseClipping;
+	int m_ClipX;
+	int m_ClipY;
+	int m_ClipW;
+	int m_ClipH;
+	
+	const char *m_pName;
+	bool m_GameGroup;
+	bool m_Visible;
+	
+	CLayerGroup();
+	~CLayerGroup();
+	
+	void Convert(CUIRect *pRect);
+	void Render();
+	void MapScreen();
+	void Mapping(float *pPoints);
+
+	void GetSize(float *w, float *h);
+	
+	void DeleteLayer(int Index);
+	int SwapLayers(int Index0, int Index1);
+	
+	bool IsEmpty() const 
+	{
+		return m_lLayers.size() == 0;
+	}
+	
+	void Clear() 
+	{ 
+		m_lLayers.delete_all();
+	}
+	
+	void AddLayer(CLayer *l)
+	{
+		m_lLayers.add(l);
+	}
+
+	void ModifyImageIndex(INDEX_MODIFY_FUNC Func)
+	{
+		for(int i = 0; i < m_lLayers.size(); i++)
+			m_lLayers[i]->ModifyImageIndex(Func);
+	}
+	
+	void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
+	{
+		for(int i = 0; i < m_lLayers.size(); i++)
+			m_lLayers[i]->ModifyEnvelopeIndex(Func);
+	}
+};
+
+class CEditorImage : public CImageInfo
+{
+public:
+	CEditor *m_pEditor;
+	
+	CEditorImage(CEditor *pEditor)
+	{
+		m_pEditor = pEditor;
+		m_TexId = -1;
+		m_aName[0] = 0;
+		m_External = 0;
+		m_Width = 0;
+		m_Height = 0;
+		m_pData = 0;
+		m_Format = 0;
+	}
+	
+	~CEditorImage();
+	
+	void AnalyseTileFlags();
+	
+	int m_TexId;
+	int m_External;
+	char m_aName[128];
+	unsigned char m_aTileFlags[256];
+};
+
+class CEditorMap
+{
+	void MakeGameGroup(CLayerGroup *pGroup);
+	void MakeGameLayer(CLayer *pLayer);
+public:
+	CEditor *m_pEditor;
+
+	CEditorMap()
+	{
+		Clean();
+	}
+
+	array<CLayerGroup*> m_lGroups;
+	array<CEditorImage*> m_lImages;
+	array<CEnvelope*> m_lEnvelopes;
+	
+	class CLayerGame *m_pGameLayer;
+	CLayerGroup *m_pGameGroup;
+	
+	CEnvelope *NewEnvelope(int Channels)
+	{
+		CEnvelope *e = new CEnvelope(Channels);
+		m_lEnvelopes.add(e);
+		return e;
+	}
+	
+	CLayerGroup *NewGroup()
+	{
+		CLayerGroup *g = new CLayerGroup;
+		g->m_pMap = this;
+		m_lGroups.add(g);
+		return g;
+	}
+	
+	int SwapGroups(int Index0, int Index1)
+	{
+		if(Index0 < 0 || Index0 >= m_lGroups.size()) return Index0;
+		if(Index1 < 0 || Index1 >= m_lGroups.size()) return Index0;
+		if(Index0 == Index1) return Index0;
+		swap(m_lGroups[Index0], m_lGroups[Index1]);
+		return Index1;
+	}
+	
+	void DeleteGroup(int Index)
+	{
+		if(Index < 0 || Index >= m_lGroups.size()) return;
+		delete m_lGroups[Index];
+		m_lGroups.remove_index(Index);
+	}
+	
+	void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc)
+	{
+		for(int i = 0; i < m_lGroups.size(); i++)
+			m_lGroups[i]->ModifyImageIndex(pfnFunc);
+	}
+	
+	void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc)
+	{
+		for(int i = 0; i < m_lGroups.size(); i++)
+			m_lGroups[i]->ModifyEnvelopeIndex(pfnFunc);
+	}
+	
+	void Clean();
+	void CreateDefault(int EntitiesTexture);
+
+	// io	
+	int Save(class IStorage *pStorage, const char *pFilename);
+	int Load(class IStorage *pStorage, const char *pFilename);
+};
+
+
+struct CProperty
+{
+	const char *m_pName;
+	int m_Value;
+	int m_Type;
+	int m_Min;
+	int m_Max;
+};
+
+enum
+{
+	PROPTYPE_NULL=0,
+	PROPTYPE_BOOL,
+	PROPTYPE_INT_STEP,
+	PROPTYPE_INT_SCROLL,
+	PROPTYPE_COLOR,
+	PROPTYPE_IMAGE,
+	PROPTYPE_ENVELOPE,
+};
+
+typedef struct
+{
+	int x, y;
+	int w, h;
+} RECTi;
+
+class CLayerTiles : public CLayer
+{
+public:
+	CLayerTiles(int w, int h);
+	~CLayerTiles();
+
+	void Resize(int NewW, int NewH);
+
+	void MakePalette();
+	virtual void Render();
+
+	int ConvertX(float x) const;
+	int ConvertY(float y) const;
+	void Convert(CUIRect Rect, RECTi *pOut);
+	void Snap(CUIRect *pRect);
+	void Clamp(RECTi *pRect);
+
+	virtual void BrushSelecting(CUIRect Rect);
+	virtual int BrushGrab(CLayerGroup *pBrush, CUIRect Rect);
+	virtual void FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect);
+	virtual void BrushDraw(CLayer *pBrush, float wx, float wy);
+	virtual void BrushFlipX();
+	virtual void BrushFlipY();
+	
+	virtual int RenderProperties(CUIRect *pToolbox);
+
+	virtual void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc);
+	virtual void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc);
+	
+	void PrepareForSave();
+
+	void GetSize(float *w, float *h) { *w = m_Width*32.0f;  *h = m_Height*32.0f; }
+	
+	int m_TexId;
+	int m_Game;
+	int m_Image;
+	int m_Width;
+	int m_Height;
+	CTile *m_pTiles;
+};
+
+class CLayerQuads : public CLayer
+{
+public:
+	CLayerQuads();
+	~CLayerQuads();
+
+	virtual void Render();
+	CQuad *NewQuad();
+
+	virtual void BrushSelecting(CUIRect Rect);
+	virtual int BrushGrab(CLayerGroup *pBrush, CUIRect Rect);
+	virtual void BrushPlace(CLayer *pBrush, float wx, float wy);
+	virtual void BrushFlipX();
+	virtual void BrushFlipY();
+	virtual void BrushRotate(float Amount);
+	
+	virtual int RenderProperties(CUIRect *pToolbox);
+
+	virtual void ModifyImageIndex(INDEX_MODIFY_FUNC pfnFunc);
+	virtual void ModifyEnvelopeIndex(INDEX_MODIFY_FUNC pfnFunc);
+	
+	void GetSize(float *w, float *h);
+	
+	int m_Image;
+	array<CQuad> m_lQuads;
+};
+
+class CLayerGame : public CLayerTiles
+{
+public:
+	CLayerGame(int w, int h);
+	~CLayerGame();
+
+	virtual int RenderProperties(CUIRect *pToolbox);
+};
+
+class CEditor : public IEditor
+{
+	class IInput *m_pInput;
+	class IClient *m_pClient;
+	class IGraphics *m_pGraphics;
+	class ITextRender *m_pTextRender;
+	CRenderTools m_RenderTools;
+	CUI m_UI;
+public:
+	class IInput *Input() { return m_pInput; };
+	class IClient *Client() { return m_pClient; };
+	class IGraphics *Graphics() { return m_pGraphics; };
+	class ITextRender *TextRender() { return m_pTextRender; };
+	CUI *UI() { return &m_UI; }
+	CRenderTools *RenderTools() { return &m_RenderTools; }
+
+	CEditor() : m_TilesetPicker(16, 16)
+	{
+		m_pInput = 0;
+		m_pClient = 0;
+		m_pGraphics = 0;
+		m_pTextRender = 0;
+
+		m_Mode = MODE_LAYERS;
+		m_Dialog = 0;
+		m_pTooltip = 0;
+
+		m_aFileName[0] = 0;
+
+		m_WorldOffsetX = 0;
+		m_WorldOffsetY = 0;
+		m_EditorOffsetX = 0.0f;
+		m_EditorOffsetY = 0.0f;
+		
+		m_WorldZoom = 1.0f;
+		m_ZoomLevel = 200;
+		m_LockMouse = false;
+		m_ShowMousePointer = true;
+		m_MouseDeltaX = 0;
+		m_MouseDeltaY = 0;
+		m_MouseDeltaWx = 0;
+		m_MouseDeltaWy = 0;
+		
+		m_GuiActive = true;
+		m_ProofBorders = false;
+		
+		m_ShowDetail = true;
+		m_Animate = false;
+		m_AnimateStart = 0;
+		m_AnimateTime = 0;
+		m_AnimateSpeed = 1;
+		
+		m_ShowEnvelopeEditor = 0;
+		
+		ms_CheckerTexture = 0;
+		ms_BackgroundTexture = 0;
+		ms_CursorTexture = 0;
+		ms_EntitiesTexture = 0;
+		
+		ms_pUiGotContext = 0;
+	}
+	
+	virtual void Init();
+	virtual void UpdateAndRender();
+	
+	void InvokeFileDialog(int ListdirType, const char *pTitle, const char *pButtonText,
+		const char *pBasepath, const char *pDefaultName,
+		void (*pfnFunc)(const char *pFilename, void *pUser), void *pUser);
+	
+	void Reset(bool CreateDefault=true);
+	int Save(const char *pFilename);
+	int Load(const char *pFilename);
+	int Append(const char *pFilename);
+	void Render();
+
+	CQuad *GetSelectedQuad();
+	CLayer *GetSelectedLayerType(int Index, int Type);
+	CLayer *GetSelectedLayer(int Index);
+	CLayerGroup *GetSelectedGroup();
+	
+	int DoProperties(CUIRect *pToolbox, CProperty *pProps, int *pIds, int *pNewVal);
+	
+	int m_Mode;
+	int m_Dialog;
+	const char *m_pTooltip;
+
+	char m_aFileName[512];
+
+	float m_WorldOffsetX;
+	float m_WorldOffsetY;
+	float m_EditorOffsetX;
+	float m_EditorOffsetY;
+	float m_WorldZoom;
+	int m_ZoomLevel;
+	bool m_LockMouse;
+	bool m_ShowMousePointer;
+	bool m_GuiActive;
+	bool m_ProofBorders;
+	float m_MouseDeltaX;
+	float m_MouseDeltaY;
+	float m_MouseDeltaWx;
+	float m_MouseDeltaWy;
+	
+	bool m_ShowDetail;
+	bool m_Animate;
+	int64 m_AnimateStart;
+	float m_AnimateTime;
+	float m_AnimateSpeed;
+	
+	int m_ShowEnvelopeEditor;
+	
+	int m_SelectedLayer;
+	int m_SelectedGroup;
+	int m_SelectedQuad;
+	int m_SelectedPoints;
+	int m_SelectedEnvelope;
+	int m_SelectedImage;
+	
+	static int ms_CheckerTexture;
+	static int ms_BackgroundTexture;
+	static int ms_CursorTexture;
+	static int ms_EntitiesTexture;
+	
+	CLayerGroup m_Brush;
+	CLayerTiles m_TilesetPicker;
+	
+	static const void *ms_pUiGotContext;
+	
+	CEditorMap m_Map;
+	
+    void DoMapBorder();
+	int DoButton_Editor_Common(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
+	int DoButton_Editor(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
+
+	int DoButton_Tab(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
+	int DoButton_Ex(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip, int Corners);
+	int DoButton_ButtonDec(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
+	int DoButton_ButtonInc(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
+
+	int DoButton_File(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
+	
+	int DoButton_Menu(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
+	int DoButton_MenuItem(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags=0, const char *pToolTip=0);
+	
+	int DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, bool Hidden=false);
+
+	void RenderBackground(CUIRect View, int Texture, float Size, float Brightness);
+
+	void UiInvokePopupMenu(void *pId, int Flags, float x, float y, float w, float h, int (*pfnFunc)(CEditor *pEditor, CUIRect Rect), void *pExtra=0);
+	void UiDoPopupMenu();
+	
+	int UiDoValueSelector(void *pId, CUIRect *r, const char *pLabel, int Current, int Min, int Max, float Scale);
+
+	static int PopupGroup(CEditor *pEditor, CUIRect View);
+	static int PopupLayer(CEditor *pEditor, CUIRect View);
+	static int PopupQuad(CEditor *pEditor, CUIRect View);
+	static int PopupPoint(CEditor *pEditor, CUIRect View);
+	static int PopupSelectImage(CEditor *pEditor, CUIRect View);
+	static int PopupImage(CEditor *pEditor, CUIRect View);
+	static int PopupMenuFile(CEditor *pEditor, CUIRect View);
+
+
+	void PopupSelectImageInvoke(int Current, float x, float y);
+	int PopupSelectImageResult();
+	
+	vec4 ButtonColorMul(const void *pId);
+
+	void DoQuadPoint(CQuad *pQuad, int QuadIndex, int v);
+	void DoMapEditor(CUIRect View, CUIRect Toolbar);
+	void DoToolbar(CUIRect Toolbar);
+	void DoQuad(CQuad *pQuad, int Index);
+	float UiDoScrollbarV(const void *id, const CUIRect *pRect, float Current);
+	vec4 GetButtonColor(const void *id, int Checked);
+	
+	static void ReplaceImage(const char *pFilename, void *pUser);
+	static void AddImage(const char *pFilename, void *pUser);
+	
+	void RenderImages(CUIRect Toolbox, CUIRect Toolbar, CUIRect View);
+	void RenderLayers(CUIRect Toolbox, CUIRect Toolbar, CUIRect View);
+	void RenderModebar(CUIRect View);
+	void RenderStatusbar(CUIRect View);
+	void RenderEnvelopeEditor(CUIRect View);
+	
+	void RenderMenubar(CUIRect Menubar);
+	void RenderFileDialog();
+};
+
+// make sure to inline this function
+inline class IGraphics *CLayer::Graphics() { return m_pEditor->Graphics(); }
+inline class ITextRender *CLayer::TextRender() { return m_pEditor->TextRender(); }
+
+#endif
diff --git a/src/game/editor/ed_editor.hpp b/src/game/editor/ed_editor.hpp
deleted file mode 100644
index 98d1d960..00000000
--- a/src/game/editor/ed_editor.hpp
+++ /dev/null
@@ -1,589 +0,0 @@
-/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
-
-#include <base/system.h>
-#include <base/math.hpp>
-
-#include <stdlib.h>
-#include <math.h>
-#include "array.hpp"
-#include "../mapitems.hpp"
-#include "../client/render.hpp"
-
-#include <engine/e_client_interface.h>
-#include <engine/e_datafile.h>
-#include <engine/e_config.h>
-#include <engine/client/editor.h>
-
-#include <game/client/ui.hpp>
-
-typedef void (*INDEX_MODIFY_FUNC)(int *index);
-
-//CRenderTools m_RenderTools;
-
-// EDITOR SPECIFIC
-template<typename T>
-void swap(T &a, T &b)
-{
-	T tmp = a;
-	a = b;
-	b = tmp;
-}
-
-enum
-{
-	MODE_LAYERS=0,
-	MODE_IMAGES,
-	
-	DIALOG_NONE=0,
-	DIALOG_FILE,
-};
-
-typedef struct
-{
-	POINT position;
-	int type;
-} ENTITY;
-
-class ENVELOPE
-{
-public:
-	int channels;
-	array<ENVPOINT> points;
-	char name[32];
-	float bottom, top;
-	
-	ENVELOPE(int chan)
-	{
-		channels = chan;
-		name[0] = 0;
-		bottom = 0;
-		top = 0;
-	}
-	
-	static int sort_comp(const void *v0, const void *v1)
-	{
-		const ENVPOINT *p0 = (const ENVPOINT *)v0;
-		const ENVPOINT *p1 = (const ENVPOINT *)v1;
-		if(p0->time < p1->time)
-			return -1;
-		if(p0->time > p1->time)
-			return 1;
-		return 0;
-	}
-	
-	void resort()
-	{
-		qsort(points.getptr(), points.len(), sizeof(ENVPOINT), sort_comp);
-		find_top_bottom(0xf);
-	}
-
-	void find_top_bottom(int channelmask)
-	{
-		top = -1000000000.0f;
-		bottom = 1000000000.0f;
-		for(int i = 0; i < points.len(); i++)
-		{
-			for(int c = 0; c < channels; c++)
-			{
-				if(channelmask&(1<<c))
-				{
-					float v = fx2f(points[i].values[c]);
-					if(v > top) top = v;
-					if(v < bottom) bottom = v;
-				}
-			}
-		}
-	}
-	
-	int eval(float time, float *result)
-	{
-		CRenderTools::render_eval_envelope(points.getptr(), points.len(), channels, time, result);
-		return channels;
-	}
-	
-	void add_point(int time, int v0, int v1=0, int v2=0, int v3=0)
-	{
-		ENVPOINT p;
-		p.time = time;
-		p.values[0] = v0;
-		p.values[1] = v1;
-		p.values[2] = v2;
-		p.values[3] = v3;
-		p.curvetype = CURVETYPE_LINEAR;
-		points.add(p);
-		resort();
-	}
-	
-	float end_time()
-	{
-		if(points.len())
-			return points[points.len()-1].time*(1.0f/1000.0f);
-		return 0;
-	}
-};
-
-
-class LAYER;
-class LAYERGROUP;
-class MAP;
-
-class LAYER
-{
-public:
-	class EDITOR *editor;
-	class IGraphics *Graphics();
-
-	LAYER()
-	{
-		type = LAYERTYPE_INVALID;
-		type_name = "(invalid)";
-		visible = true;
-		readonly = false;
-		flags = 0;
-		editor = 0;
-	}
-	
-	virtual ~LAYER()
-	{
-	}
-	
-	
-	virtual void brush_selecting(CUIRect rect) {}
-	virtual int brush_grab(LAYERGROUP *brush, CUIRect rect) { return 0; }
-	virtual void brush_draw(LAYER *brush, float x, float y) {}
-	virtual void brush_place(LAYER *brush, float x, float y) {}
-	virtual void brush_flip_x() {}
-	virtual void brush_flip_y() {}
-	virtual void brush_rotate(float amount) {}
-	
-	virtual void render() {}
-	virtual int render_properties(CUIRect *toolbox) { return 0; }
-	
-	virtual void modify_image_index(INDEX_MODIFY_FUNC func) {}
-	virtual void modify_envelope_index(INDEX_MODIFY_FUNC func) {}
-	
-	virtual void get_size(float *w, float *h) { *w = 0; *h = 0;}
-	
-	const char *type_name;
-	int type;
-	int flags;
-
-	bool readonly;
-	bool visible;
-};
-
-class LAYERGROUP
-{
-public:
-	class MAP *m_pMap;
-	
-	array<LAYER*> layers;
-	
-	int offset_x;
-	int offset_y;
-
-	int parallax_x;
-	int parallax_y;
-	
-	int use_clipping;
-	int clip_x;
-	int clip_y;
-	int clip_w;
-	int clip_h;
-	
-	const char *name;
-	bool game_group;
-	bool visible;
-	
-	LAYERGROUP();
-	~LAYERGROUP();
-	
-	void convert(CUIRect *rect);
-	void render();
-	void mapscreen();
-	void mapping(float *points);
-	
-	bool is_empty() const;
-	void clear();
-	void add_layer(LAYER *l);
-
-	void get_size(float *w, float *h);
-	
-	void delete_layer(int index);
-	int swap_layers(int index0, int index1);
-
-	void modify_image_index(INDEX_MODIFY_FUNC func)
-	{
-		for(int i = 0; i < layers.len(); i++)
-			layers[i]->modify_image_index(func);
-	}
-	
-	void modify_envelope_index(INDEX_MODIFY_FUNC func)
-	{
-		for(int i = 0; i < layers.len(); i++)
-			layers[i]->modify_envelope_index(func);
-	}
-};
-
-class EDITOR_IMAGE : public IMAGE_INFO
-{
-public:
-	EDITOR *editor;
-	
-	EDITOR_IMAGE(EDITOR *ed)
-	{
-		editor = editor;
-		tex_id = -1;
-		name[0] = 0;
-		external = 0;
-		width = 0;
-		height = 0;
-		data = 0;
-		format = 0;
-	}
-	
-	~EDITOR_IMAGE();
-	
-	void analyse_tileflags();
-	
-	int tex_id;
-	int external;
-	char name[128];
-	unsigned char tileflags[256];
-};
-
-class MAP
-{
-	void make_game_group(LAYERGROUP *group);
-	void make_game_layer(LAYER *layer);
-public:
-	EDITOR *editor;
-
-	MAP()
-	{
-		clean();
-	}
-
-	array<LAYERGROUP*> groups;
-	array<EDITOR_IMAGE*> images;
-	array<ENVELOPE*> envelopes;
-	
-	class LAYER_GAME *game_layer;
-	LAYERGROUP *game_group;
-	
-	ENVELOPE *new_envelope(int channels)
-	{
-		ENVELOPE *e = new ENVELOPE(channels);
-		envelopes.add(e);
-		return e;
-	}
-	
-	LAYERGROUP *new_group()
-	{
-		LAYERGROUP *g = new LAYERGROUP;
-		g->m_pMap = this;
-		groups.add(g);
-		return g;
-	}
-	
-	int swap_groups(int index0, int index1)
-	{
-		if(index0 < 0 || index0 >= groups.len()) return index0;
-		if(index1 < 0 || index1 >= groups.len()) return index0;
-		if(index0 == index1) return index0;
-		swap(groups[index0], groups[index1]);
-		return index1;
-	}
-	
-	void delete_group(int index)
-	{
-		if(index < 0 || index >= groups.len()) return;
-		delete groups[index];
-		groups.removebyindex(index);
-	}
-	
-	void modify_image_index(INDEX_MODIFY_FUNC func)
-	{
-		for(int i = 0; i < groups.len(); i++)
-			groups[i]->modify_image_index(func);
-	}
-	
-	void modify_envelope_index(INDEX_MODIFY_FUNC func)
-	{
-		for(int i = 0; i < groups.len(); i++)
-			groups[i]->modify_envelope_index(func);
-	}
-	
-	void clean();
-	void create_default(int entities_texture);
-
-	// io	
-	int save(const char *filename);
-	int load(const char *filename);
-};
-
-
-struct PROPERTY
-{
-	const char *name;
-	int value;
-	int type;
-	int min;
-	int max;
-};
-
-enum
-{
-	PROPTYPE_NULL=0,
-	PROPTYPE_BOOL,
-	PROPTYPE_INT_STEP,
-	PROPTYPE_INT_SCROLL,
-	PROPTYPE_COLOR,
-	PROPTYPE_IMAGE,
-	PROPTYPE_ENVELOPE,
-};
-
-class EDITOR : public IEditor
-{
-	class IGraphics *m_pGraphics;
-	CRenderTools m_RenderTools;
-	CUI m_UI;
-public:
-	
-	class IGraphics *Graphics() { return m_pGraphics; };
-	CUI *UI() { return &m_UI; }
-	CRenderTools *RenderTools() { return &m_RenderTools; }
-
-	EDITOR()
-	{
-		mode = MODE_LAYERS;
-		dialog = 0;
-		tooltip = 0;
-
-		world_offset_x = 0;
-		world_offset_y = 0;
-		editor_offset_x = 0.0f;
-		editor_offset_y = 0.0f;
-		
-		world_zoom = 1.0f;
-		zoom_level = 100;
-		lock_mouse = false;
-		show_mouse_pointer = true;
-		mouse_delta_x = 0;
-		mouse_delta_y = 0;
-		mouse_delta_wx = 0;
-		mouse_delta_wy = 0;
-		
-		gui_active = true;
-		proof_borders = false;
-		
-		show_detail = true;
-		animate = false;
-		animate_start = 0;
-		animate_time = 0;
-		animate_speed = 1;
-		
-		show_envelope_editor = 0;
-	}
-	
-	virtual void Init(class IGraphics *pGraphics);
-	virtual void UpdateAndRender();
-	
-	void invoke_file_dialog(int listdir_type, const char *title, const char *button_text,
-		const char *basepath, const char *default_name,
-		void (*func)(const char *filename, void *user), void *user);
-	
-	void reset(bool create_default=true);
-	int save(const char *filename);
-	int load(const char *filename);
-	int append(const char *filename);
-	void render();
-
-	QUAD *get_selected_quad();
-	LAYER *get_selected_layer_type(int index, int type);
-	LAYER *get_selected_layer(int index);
-	LAYERGROUP *get_selected_group();
-	
-	int do_properties(CUIRect *toolbox, PROPERTY *props, int *ids, int *new_val);
-	
-	int mode;
-	int dialog;
-	const char *tooltip;
-
-	float world_offset_x;
-	float world_offset_y;
-	float editor_offset_x;
-	float editor_offset_y;
-	float world_zoom;
-	int zoom_level;
-	bool lock_mouse;
-	bool show_mouse_pointer;
-	bool gui_active;
-	bool proof_borders;
-	float mouse_delta_x;
-	float mouse_delta_y;
-	float mouse_delta_wx;
-	float mouse_delta_wy;
-	
-	bool show_detail;
-	bool animate;
-	int64 animate_start;
-	float animate_time;
-	float animate_speed;
-	
-	int show_envelope_editor;
-	
-	int selected_layer;
-	int selected_group;
-	int selected_quad;
-	int selected_points;
-	int selected_envelope;
-	int selected_image;
-	
-	MAP map;
-	
-	int DoButton_Editor_Common(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
-	int DoButton_Editor(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
-
-	int DoButton_ButtonL(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
-	int DoButton_ButtonM(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
-	int DoButton_ButtonR(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
-	int DoButton_ButtonDec(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
-	int DoButton_ButtonInc(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
-
-	int DoButton_File(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
-	
-	int DoButton_Menu(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
-	int DoButton_MenuItem(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags=0, const char *pToolTip=0);
-	
-	int DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, bool Hidden=false);
-	//static void draw_editor_button(const void *id, const char *text, int checked, const CUIRect *r, const void *extra);
-	//static void draw_editor_button_menuitem(const void *id, const char *text, int checked, const CUIRect *r, const void *extra);
-
-	void render_background(CUIRect view, int texture, float size, float brightness);
-
-	void ui_invoke_popup_menu(void *id, int flags, float x, float y, float w, float h, int (*func)(EDITOR *pEditor, CUIRect rect), void *extra=0);
-	void ui_do_popup_menu();
-	
-	int ui_do_value_selector(void *id, CUIRect *r, const char *label, int current, int min, int max, float scale);
-
-	static int popup_group(EDITOR *pEditor, CUIRect view);
-	static int popup_layer(EDITOR *pEditor, CUIRect view);
-	static int popup_quad(EDITOR *pEditor, CUIRect view);
-	static int popup_point(EDITOR *pEditor, CUIRect view);
-	static int popup_select_image(EDITOR *pEditor, CUIRect view);
-	static int popup_image(EDITOR *pEditor, CUIRect view);
-	static int popup_menu_file(EDITOR *pEditor, CUIRect view);
-
-
-	void popup_select_image_invoke(int current, float x, float y);
-	int popup_select_image_result();
-	
-	vec4 button_color_mul(const void *id);
-
-	void do_quad_point(QUAD *q, int quad_index, int v);
-	void do_map_editor(CUIRect view, CUIRect toolbar);
-	void do_toolbar(CUIRect toolbar);
-	void do_quad(QUAD *q, int index);
-	float ui_do_scrollbar_v(const void *id, const CUIRect *rect, float current);
-	vec4 get_button_color(const void *id, int checked);
-	
-	static void replace_image(const char *filename, void *user);
-	static void add_image(const char *filename, void *user);
-	
-	void render_images(CUIRect toolbox, CUIRect toolbar, CUIRect view);
-	void render_layers(CUIRect toolbox, CUIRect toolbar, CUIRect view);
-	void render_modebar(CUIRect view);
-	void render_statusbar(CUIRect view);
-	void render_envelopeeditor(CUIRect view);
-	
-	void render_menubar(CUIRect menubar);
-	void render_file_dialog();
-};
-
-// make sure to inline this function
-inline class IGraphics *LAYER::Graphics() { return editor->Graphics(); }
-
-//extern EDITOR editor;
-
-typedef struct
-{
-	int x, y;
-	int w, h;
-} RECTi;
-
-class LAYER_TILES : public LAYER
-{
-public:
-	LAYER_TILES(int w, int h);
-	~LAYER_TILES();
-
-	void resize(int new_w, int new_h);
-
-	void make_palette();
-	virtual void render();
-
-	int convert_x(float x) const;
-	int convert_y(float y) const;
-	void convert(CUIRect rect, RECTi *out);
-	void snap(CUIRect *rect);
-	void clamp(RECTi *rect);
-
-	virtual void brush_selecting(CUIRect rect);
-	virtual int brush_grab(LAYERGROUP *brush, CUIRect rect);
-	virtual void brush_draw(LAYER *brush, float wx, float wy);
-	virtual void brush_flip_x();
-	virtual void brush_flip_y();
-	
-	virtual int render_properties(CUIRect *toolbox);
-
-	virtual void modify_image_index(INDEX_MODIFY_FUNC func);
-	virtual void modify_envelope_index(INDEX_MODIFY_FUNC func);
-	
-	void prepare_for_save();
-
-	void get_size(float *w, float *h) { *w = width*32.0f;  *h = height*32.0f; }
-	
-	int tex_id;
-	int game;
-	int image;
-	int width;
-	int height;
-	TILE *tiles;
-};
-
-class LAYER_QUADS : public LAYER
-{
-public:
-	LAYER_QUADS();
-	~LAYER_QUADS();
-
-	virtual void render();
-	QUAD *new_quad();
-
-	virtual void brush_selecting(CUIRect rect);
-	virtual int brush_grab(LAYERGROUP *brush, CUIRect rect);
-	virtual void brush_place(LAYER *brush, float wx, float wy);
-	virtual void brush_flip_x();
-	virtual void brush_flip_y();
-	virtual void brush_rotate(float amount);
-	
-	virtual int render_properties(CUIRect *toolbox);
-
-	virtual void modify_image_index(INDEX_MODIFY_FUNC func);
-	virtual void modify_envelope_index(INDEX_MODIFY_FUNC func);
-	
-	void get_size(float *w, float *h);
-	
-	int image;
-	array<QUAD> quads;
-};
-
-class LAYER_GAME : public LAYER_TILES
-{
-public:
-	LAYER_GAME(int w, int h);
-	~LAYER_GAME();
-
-	virtual int render_properties(CUIRect *toolbox);
-};
diff --git a/src/game/editor/ed_io.cpp b/src/game/editor/ed_io.cpp
index b8c025fb..8ca4f704 100644
--- a/src/game/editor/ed_io.cpp
+++ b/src/game/editor/ed_io.cpp
@@ -1,10 +1,9 @@
-#include <string.h>
-#include <stdio.h>
-#include <engine/client/graphics.h>
-#include "ed_editor.hpp"
+#include <engine/graphics.h>
+#include <engine/storage.h>
+#include "ed_editor.h"
 
 template<typename T>
-static int make_version(int i, const T &v)
+static int MakeVersion(int i, const T &v)
 { return (i<<16)+sizeof(T); }
 
 // backwards compatiblity
@@ -93,7 +92,7 @@ void editor_load_old(DATAFILE *df, MAP *map)
 		{
 			mapres_tilemap *tmap = (mapres_tilemap *)datafile_get_item(df, start+t,0,0);
 			
-			LAYER_TILES *l = new LAYER_TILES(tmap->width, tmap->height);
+			CLayerTiles *l = new CLayerTiles(tmap->width, tmap->height);
 			
 			if(tmap->main)
 			{
@@ -113,7 +112,7 @@ void editor_load_old(DATAFILE *df, MAP *map)
 
 			// process the data
 			unsigned char *src_data = (unsigned char *)datafile_get_data(df, tmap->data);
-			TILE *dst_data = l->tiles;
+			CTile *dst_data = l->tiles;
 			
 			for(int y = 0; y < tmap->height; y++)
 				for(int x = 0; x < tmap->width; x++, dst_data++, src_data+=2)
@@ -138,12 +137,12 @@ void editor_load_old(DATAFILE *df, MAP *map)
 			EDITOR_IMAGE *img = new EDITOR_IMAGE;
 			img->width = imgres->width;
 			img->height = imgres->height;
-			img->format = IMG_RGBA;
+			img->format = CImageInfo::FORMAT_RGBA;
 			
 			// copy image data
 			img->data = mem_alloc(img->width*img->height*4, 1);
 			mem_copy(img->data, data, img->width*img->height*4);
-			img->tex_id = Graphics()->LoadTextureRaw(img->width, img->height, img->format, img->data, IMG_AUTO, 0);
+			img->tex_id = Graphics()->LoadTextureRaw(img->width, img->height, img->format, img->data, CImageInfo::FORMAT_AUTO, 0);
 			map->images.add(img);
 			
 			// unload image
@@ -153,7 +152,7 @@ void editor_load_old(DATAFILE *df, MAP *map)
 	
 	// load entities
 	{
-		LAYER_GAME *g = map->game_layer;
+		CLayerGame *g = map->game_layer;
 		g->resize(game_width, game_height);
 		for(int t = MAPRES_ENTS_START; t < MAPRES_ENTS_END; t++)
 		{
@@ -190,190 +189,192 @@ void editor_load_old(DATAFILE *df, MAP *map)
 	}
 }*/
 
-int EDITOR::save(const char *filename)
+int CEditor::Save(const char *pFilename)
 {
-	return map.save(filename);
+	return m_Map.Save(Kernel()->RequestInterface<IStorage>(), pFilename);
 }
 
-int MAP::save(const char *filename)
+int CEditorMap::Save(class IStorage *pStorage, const char *pFileName)
 {
-	dbg_msg("editor", "saving to '%s'...", filename);
-	DATAFILE_OUT *df = datafile_create(filename);
-	if(!df)
+	dbg_msg("editor", "saving to '%s'...", pFileName);
+	CDataFileWriter df;
+	if(!df.Open(pStorage, pFileName))
 	{
-		dbg_msg("editor", "failed to open file '%s'...", filename);
+		dbg_msg("editor", "failed to open file '%s'...", pFileName);
 		return 0;
 	}
 		
 	// save version
 	{
-		MAPITEM_VERSION item;
-		item.version = 1;
-		datafile_add_item(df, MAPITEMTYPE_VERSION, 0, sizeof(item), &item);
+		CMapItemVersion Item;
+		Item.m_Version = 1;
+		df.AddItem(MAPITEMTYPE_VERSION, 0, sizeof(Item), &Item);
 	}
 
 	// save images
-	for(int i = 0; i < images.len(); i++)
+	for(int i = 0; i < m_lImages.size(); i++)
 	{
-		EDITOR_IMAGE *img = images[i];
+		CEditorImage *pImg = m_lImages[i];
 		
 		// analyse the image for when saving (should be done when we load the image)
 		// TODO!
-		img->analyse_tileflags();
+		pImg->AnalyseTileFlags();
 		
-		MAPITEM_IMAGE item;
-		item.version = 1;
+		CMapItemImage Item;
+		Item.m_Version = 1;
 		
-		item.width = img->width;
-		item.height = img->height;
-		item.external = img->external;
-		item.image_name = datafile_add_data(df, strlen(img->name)+1, img->name);
-		if(img->external)
-			item.image_data = -1;
+		Item.m_Width = pImg->m_Width;
+		Item.m_Height = pImg->m_Height;
+		Item.m_External = pImg->m_External;
+		Item.m_ImageName = df.AddData(str_length(pImg->m_aName)+1, pImg->m_aName);
+		if(pImg->m_External)
+			Item.m_ImageData = -1;
 		else
-			item.image_data = datafile_add_data(df, item.width*item.height*4, img->data);
-		datafile_add_item(df, MAPITEMTYPE_IMAGE, i, sizeof(item), &item);
+			Item.m_ImageData = df.AddData(Item.m_Width*Item.m_Height*4, pImg->m_pData);
+		df.AddItem(MAPITEMTYPE_IMAGE, i, sizeof(Item), &Item);
 	}
 	
 	// save layers
-	int layer_count = 0;
-	for(int g = 0; g < groups.len(); g++)
+	int LayerCount = 0;
+	for(int g = 0; g < m_lGroups.size(); g++)
 	{
-		LAYERGROUP *group = groups[g];
-		MAPITEM_GROUP gitem;
-		gitem.version = MAPITEM_GROUP::CURRENT_VERSION;
+		CLayerGroup *pGroup = m_lGroups[g];
+		CMapItemGroup GItem;
+		GItem.m_Version = CMapItemGroup::CURRENT_VERSION;
 		
-		gitem.parallax_x = group->parallax_x;
-		gitem.parallax_y = group->parallax_y;
-		gitem.offset_x = group->offset_x;
-		gitem.offset_y = group->offset_y;
-		gitem.use_clipping = group->use_clipping;
-		gitem.clip_x = group->clip_x;
-		gitem.clip_y = group->clip_y;
-		gitem.clip_w = group->clip_w;
-		gitem.clip_h = group->clip_h;
-		gitem.start_layer = layer_count;
-		gitem.num_layers = 0;
+		GItem.m_ParallaxX = pGroup->m_ParallaxX;
+		GItem.m_ParallaxY = pGroup->m_ParallaxY;
+		GItem.m_OffsetX = pGroup->m_OffsetX;
+		GItem.m_OffsetY = pGroup->m_OffsetY;
+		GItem.m_UseClipping = pGroup->m_UseClipping;
+		GItem.m_ClipX = pGroup->m_ClipX;
+		GItem.m_ClipY = pGroup->m_ClipY;
+		GItem.m_ClipW = pGroup->m_ClipW;
+		GItem.m_ClipH = pGroup->m_ClipH;
+		GItem.m_StartLayer = LayerCount;
+		GItem.m_NumLayers = 0;
 		
-		for(int l = 0; l < group->layers.len(); l++)
+		for(int l = 0; l < pGroup->m_lLayers.size(); l++)
 		{
-			if(group->layers[l]->type == LAYERTYPE_TILES)
+			if(pGroup->m_lLayers[l]->m_Type == LAYERTYPE_TILES)
 			{
 				dbg_msg("editor", "saving tiles layer");
-				LAYER_TILES *layer = (LAYER_TILES *)group->layers[l];
-				layer->prepare_for_save();
+				CLayerTiles *pLayer = (CLayerTiles *)pGroup->m_lLayers[l];
+				pLayer->PrepareForSave();
 				
-				MAPITEM_LAYER_TILEMAP item;
-				item.version = 2;
+				CMapItemLayerTilemap Item;
+				Item.m_Version = 2;
 				
-				item.layer.flags = layer->flags;
-				item.layer.type = layer->type;
+				Item.m_Layer.m_Flags = pLayer->m_Flags;
+				Item.m_Layer.m_Type = pLayer->m_Type;
 				
-				item.color.r = 255; // not in use right now
-				item.color.g = 255;
-				item.color.b = 255;
-				item.color.a = 255;
-				item.color_env = -1;
-				item.color_env_offset = 0;
+				Item.m_Color.r = 255; // not in use right now
+				Item.m_Color.g = 255;
+				Item.m_Color.b = 255;
+				Item.m_Color.a = 255;
+				Item.m_ColorEnv = -1;
+				Item.m_ColorEnvOffset = 0;
 				
-				item.width = layer->width;
-				item.height = layer->height;
-				item.flags = layer->game;
-				item.image = layer->image;
-				item.data = datafile_add_data(df, layer->width*layer->height*sizeof(TILE), layer->tiles);
-				datafile_add_item(df, MAPITEMTYPE_LAYER, layer_count, sizeof(item), &item);
+				Item.m_Width = pLayer->m_Width;
+				Item.m_Height = pLayer->m_Height;
+				Item.m_Flags = pLayer->m_Game;
+				Item.m_Image = pLayer->m_Image;
+				Item.m_Data = df.AddData(pLayer->m_Width*pLayer->m_Height*sizeof(CTile), pLayer->m_pTiles);
+				df.AddItem(MAPITEMTYPE_LAYER, LayerCount, sizeof(Item), &Item);
 				
-				gitem.num_layers++;
-				layer_count++;
+				GItem.m_NumLayers++;
+				LayerCount++;
 			}
-			else if(group->layers[l]->type == LAYERTYPE_QUADS)
+			else if(pGroup->m_lLayers[l]->m_Type == LAYERTYPE_QUADS)
 			{
 				dbg_msg("editor", "saving quads layer");
-				LAYER_QUADS *layer = (LAYER_QUADS *)group->layers[l];
-				if(layer->quads.len())
+				CLayerQuads *pLayer = (CLayerQuads *)pGroup->m_lLayers[l];
+				if(pLayer->m_lQuads.size())
 				{
-					MAPITEM_LAYER_QUADS item;
-					item.version = 1;
-					item.layer.flags =  layer->flags;
-					item.layer.type = layer->type;
-					item.image = layer->image;
+					CMapItemLayerQuads Item;
+					Item.m_Version = 1;
+					Item.m_Layer.m_Flags =  pLayer->m_Flags;
+					Item.m_Layer.m_Type = pLayer->m_Type;
+					Item.m_Image = pLayer->m_Image;
 					
 					// add the data
-					item.num_quads = layer->quads.len();
-					item.data = datafile_add_data_swapped(df, layer->quads.len()*sizeof(QUAD), layer->quads.getptr());
-					datafile_add_item(df, MAPITEMTYPE_LAYER, layer_count, sizeof(item), &item);
+					Item.m_NumQuads = pLayer->m_lQuads.size();
+					Item.m_Data = df.AddDataSwapped(pLayer->m_lQuads.size()*sizeof(CQuad), pLayer->m_lQuads.base_ptr());
+					df.AddItem(MAPITEMTYPE_LAYER, LayerCount, sizeof(Item), &Item);
 					
 					// clean up
 					//mem_free(quads);
 
-					gitem.num_layers++;
-					layer_count++;
+					GItem.m_NumLayers++;
+					LayerCount++;
 				}
 			}
 		}
 		
-		datafile_add_item(df, MAPITEMTYPE_GROUP, g, sizeof(gitem), &gitem);
+		df.AddItem(MAPITEMTYPE_GROUP, g, sizeof(GItem), &GItem);
 	}
 	
 	// save envelopes
-	int point_count = 0;
-	for(int e = 0; e < envelopes.len(); e++)
+	int PointCount = 0;
+	for(int e = 0; e < m_lEnvelopes.size(); e++)
 	{
-		MAPITEM_ENVELOPE item;
-		item.version = 1;
-		item.channels = envelopes[e]->channels;
-		item.start_point = point_count;
-		item.num_points = envelopes[e]->points.len();
-		item.name = -1;
+		CMapItemEnvelope Item;
+		Item.m_Version = 1;
+		Item.m_Channels = m_lEnvelopes[e]->m_Channels;
+		Item.m_StartPoint = PointCount;
+		Item.m_NumPoints = m_lEnvelopes[e]->m_lPoints.size();
+		Item.m_Name = -1;
 		
-		datafile_add_item(df, MAPITEMTYPE_ENVELOPE, e, sizeof(item), &item);
-		point_count += item.num_points;
+		df.AddItem(MAPITEMTYPE_ENVELOPE, e, sizeof(Item), &Item);
+		PointCount += Item.m_NumPoints;
 	}
 	
 	// save points
-	int totalsize = sizeof(ENVPOINT) * point_count;
-	ENVPOINT *points = (ENVPOINT *)mem_alloc(totalsize, 1);
-	point_count = 0;
+	int TotalSize = sizeof(CEnvPoint) * PointCount;
+	CEnvPoint *pPoints = (CEnvPoint *)mem_alloc(TotalSize, 1);
+	PointCount = 0;
 	
-	for(int e = 0; e < envelopes.len(); e++)
+	for(int e = 0; e < m_lEnvelopes.size(); e++)
 	{
-		int count = envelopes[e]->points.len();
-		mem_copy(&points[point_count], envelopes[e]->points.getptr(), sizeof(ENVPOINT)*count);
-		point_count += count;
+		int Count = m_lEnvelopes[e]->m_lPoints.size();
+		mem_copy(&pPoints[PointCount], m_lEnvelopes[e]->m_lPoints.base_ptr(), sizeof(CEnvPoint)*Count);
+		PointCount += Count;
 	}
 
-	datafile_add_item(df, MAPITEMTYPE_ENVPOINTS, 0, totalsize, points);
+	df.AddItem(MAPITEMTYPE_ENVPOINTS, 0, TotalSize, pPoints);
 	
 	// finish the data file
-	datafile_finish(df);
+	df.Finish();
 	dbg_msg("editor", "done");
 	
 	// send rcon.. if we can
-	if(client_rcon_authed())
+	/*
+	if(Client()->RconAuthed())
 	{
-		client_rcon("sv_map_reload 1");
-	}
+		Client()->Rcon("sv_map_reload 1");
+	}*/
 	
 	return 1;
 }
 
-int EDITOR::load(const char *filename)
+int CEditor::Load(const char *pFileName)
 {
-	reset();
-	return map.load(filename);
+	Reset();
+	return m_Map.Load(Kernel()->RequestInterface<IStorage>(), pFileName);
 }
 
-int MAP::load(const char *filename)
+int CEditorMap::Load(class IStorage *pStorage, const char *pFileName)
 {
-	DATAFILE *df = datafile_load(filename);
-	if(!df)
+	CDataFileReader DataFile;
+	//DATAFILE *df = datafile_load(filename);
+	if(!DataFile.Open(pStorage, pFileName))
 		return 0;
 		
-	clean();
+	Clean();
 
 	// check version
-	MAPITEM_VERSION *item = (MAPITEM_VERSION *)datafile_find_item(df, MAPITEMTYPE_VERSION, 0);
-	if(!item)
+	CMapItemVersion *pItem = (CMapItemVersion *)DataFile.FindItem(MAPITEMTYPE_VERSION, 0);
+	if(!pItem)
 	{
 		// import old map
 		/*MAP old_mapstuff;
@@ -381,224 +382,231 @@ int MAP::load(const char *filename)
 		editor_load_old(df, this);
 		*/
 	}
-	else if(item->version == 1)
+	else if(pItem->m_Version == 1)
 	{
 		//editor.reset(false);
 		
 		// load images
 		{
-			int start, num;
-			datafile_get_type(df, MAPITEMTYPE_IMAGE, &start, &num);
-			for(int i = 0; i < num; i++)
+			int Start, Num;
+			DataFile.GetType( MAPITEMTYPE_IMAGE, &Start, &Num);
+			for(int i = 0; i < Num; i++)
 			{
-				MAPITEM_IMAGE *item = (MAPITEM_IMAGE *)datafile_get_item(df, start+i, 0, 0);
-				char *name = (char *)datafile_get_data(df, item->image_name);
+				CMapItemImage *pItem = (CMapItemImage *)DataFile.GetItem(Start+i, 0, 0);
+				char *pName = (char *)DataFile.GetData(pItem->m_ImageName);
 
 				// copy base info				
-				EDITOR_IMAGE *img = new EDITOR_IMAGE(editor);
-				img->external = item->external;
+				CEditorImage *pImg = new CEditorImage(m_pEditor);
+				pImg->m_External = pItem->m_External;
 
-				if(item->external)
+				if(pItem->m_External)
 				{
-					char buf[256];
-					sprintf(buf, "mapres/%s.png", name);
+					char aBuf[256];
+					str_format(aBuf, sizeof(aBuf),"mapres/%s.png", pName);
 					
 					// load external
-					EDITOR_IMAGE imginfo(editor);
-					if(editor->Graphics()->LoadPNG(&imginfo, buf))
+					CEditorImage ImgInfo(m_pEditor);
+					if(m_pEditor->Graphics()->LoadPNG(&ImgInfo, aBuf))
 					{
-						*img = imginfo;
-						img->tex_id = editor->Graphics()->LoadTextureRaw(imginfo.width, imginfo.height, imginfo.format, imginfo.data, IMG_AUTO, 0);
-						img->external = 1;
+						*pImg = ImgInfo;
+						pImg->m_TexId = m_pEditor->Graphics()->LoadTextureRaw(ImgInfo.m_Width, ImgInfo.m_Height, ImgInfo.m_Format, ImgInfo.m_pData, CImageInfo::FORMAT_AUTO, 0);
+						pImg->m_External = 1;
 					}
 				}
 				else
 				{
-					img->width = item->width;
-					img->height = item->height;
-					img->format = IMG_RGBA;
+					pImg->m_Width = pItem->m_Width;
+					pImg->m_Height = pItem->m_Height;
+					pImg->m_Format = CImageInfo::FORMAT_RGBA;
 					
 					// copy image data
-					void *data = datafile_get_data(df, item->image_data);
-					img->data = mem_alloc(img->width*img->height*4, 1);
-					mem_copy(img->data, data, img->width*img->height*4);
-					img->tex_id = editor->Graphics()->LoadTextureRaw(img->width, img->height, img->format, img->data, IMG_AUTO, 0);
+					void *pData = DataFile.GetData(pItem->m_ImageData);
+					pImg->m_pData = mem_alloc(pImg->m_Width*pImg->m_Height*4, 1);
+					mem_copy(pImg->m_pData, pData, pImg->m_Width*pImg->m_Height*4);
+					pImg->m_TexId = m_pEditor->Graphics()->LoadTextureRaw(pImg->m_Width, pImg->m_Height, pImg->m_Format, pImg->m_pData, CImageInfo::FORMAT_AUTO, 0);
 				}
 
 				// copy image name
-				if(name)
-					strncpy(img->name, name, 128);
+				if(pName)
+					str_copy(pImg->m_aName, pName, 128);
 
-				images.add(img);
+				m_lImages.add(pImg);
 				
 				// unload image
-				datafile_unload_data(df, item->image_data);
-				datafile_unload_data(df, item->image_name);
+				DataFile.UnloadData(pItem->m_ImageData);
+				DataFile.UnloadData(pItem->m_ImageName);
 			}
 		}
 		
 		// load groups
 		{
-			int layers_start, layers_num;
-			datafile_get_type(df, MAPITEMTYPE_LAYER, &layers_start, &layers_num);
+			int LayersStart, LayersNum;
+			DataFile.GetType(MAPITEMTYPE_LAYER, &LayersStart, &LayersNum);
 			
-			int start, num;
-			datafile_get_type(df, MAPITEMTYPE_GROUP, &start, &num);
-			for(int g = 0; g < num; g++)
+			int Start, Num;
+			DataFile.GetType(MAPITEMTYPE_GROUP, &Start, &Num);
+			for(int g = 0; g < Num; g++)
 			{
-				MAPITEM_GROUP *gitem = (MAPITEM_GROUP *)datafile_get_item(df, start+g, 0, 0);
+				CMapItemGroup *pGItem = (CMapItemGroup *)DataFile.GetItem(Start+g, 0, 0);
 				
-				if(gitem->version < 1 || gitem->version > MAPITEM_GROUP::CURRENT_VERSION)
+				if(pGItem->m_Version < 1 || pGItem->m_Version > CMapItemGroup::CURRENT_VERSION)
 					continue;
 				
-				LAYERGROUP *group = new_group();
-				group->parallax_x = gitem->parallax_x;
-				group->parallax_y = gitem->parallax_y;
-				group->offset_x = gitem->offset_x;
-				group->offset_y = gitem->offset_y;
+				CLayerGroup *pGroup = NewGroup();
+				pGroup->m_ParallaxX = pGItem->m_ParallaxX;
+				pGroup->m_ParallaxY = pGItem->m_ParallaxY;
+				pGroup->m_OffsetX = pGItem->m_OffsetX;
+				pGroup->m_OffsetY = pGItem->m_OffsetY;
 				
-				if(gitem->version >= 2)
+				if(pGItem->m_Version >= 2)
 				{
-					group->use_clipping = gitem->use_clipping;
-					group->clip_x = gitem->clip_x;
-					group->clip_y = gitem->clip_y;
-					group->clip_w = gitem->clip_w;
-					group->clip_h = gitem->clip_h;
+					pGroup->m_UseClipping = pGItem->m_UseClipping;
+					pGroup->m_ClipX = pGItem->m_ClipX;
+					pGroup->m_ClipY = pGItem->m_ClipY;
+					pGroup->m_ClipW = pGItem->m_ClipW;
+					pGroup->m_ClipH = pGItem->m_ClipH;
 				}
 				
-				for(int l = 0; l < gitem->num_layers; l++)
+				for(int l = 0; l < pGItem->m_NumLayers; l++)
 				{
-					LAYER *layer = 0;
-					MAPITEM_LAYER *layer_item = (MAPITEM_LAYER *)datafile_get_item(df, layers_start+gitem->start_layer+l, 0, 0);
-					if(!layer_item)
+					CLayer *pLayer = 0;
+					CMapItemLayer *pLayerItem = (CMapItemLayer *)DataFile.GetItem(LayersStart+pGItem->m_StartLayer+l, 0, 0);
+					if(!pLayerItem)
 						continue;
 						
-					if(layer_item->type == LAYERTYPE_TILES)
+					if(pLayerItem->m_Type == LAYERTYPE_TILES)
 					{
-						MAPITEM_LAYER_TILEMAP *tilemap_item = (MAPITEM_LAYER_TILEMAP *)layer_item;
-						LAYER_TILES *tiles = 0;
+						CMapItemLayerTilemap *pTilemapItem = (CMapItemLayerTilemap *)pLayerItem;
+						CLayerTiles *pTiles = 0;
 						
-						if(tilemap_item->flags&1)
+						if(pTilemapItem->m_Flags&1)
 						{
-							tiles = new LAYER_GAME(tilemap_item->width, tilemap_item->height);
-							make_game_layer(tiles);
-							make_game_group(group);
+							pTiles = new CLayerGame(pTilemapItem->m_Width, pTilemapItem->m_Height);
+							MakeGameLayer(pTiles);
+							MakeGameGroup(pGroup);
 						}
 						else
-							tiles = new LAYER_TILES(tilemap_item->width, tilemap_item->height);
+						{
+							pTiles = new CLayerTiles(pTilemapItem->m_Width, pTilemapItem->m_Height);
+							pTiles->m_pEditor = m_pEditor;
+						}
 
-						layer = tiles;
+						pLayer = pTiles;
 						
-						group->add_layer(tiles);
-						void *data = datafile_get_data(df, tilemap_item->data);
-						tiles->image = tilemap_item->image;
-						tiles->game = tilemap_item->flags&1;
+						pGroup->AddLayer(pTiles);
+						void *pData = DataFile.GetData(pTilemapItem->m_Data);
+						pTiles->m_Image = pTilemapItem->m_Image;
+						pTiles->m_Game = pTilemapItem->m_Flags&1;
 						
-						mem_copy(tiles->tiles, data, tiles->width*tiles->height*sizeof(TILE));
+						mem_copy(pTiles->m_pTiles, pData, pTiles->m_Width*pTiles->m_Height*sizeof(CTile));
 						
-						if(tiles->game && tilemap_item->version == make_version(1, *tilemap_item))
+						if(pTiles->m_Game && pTilemapItem->m_Version == MakeVersion(1, *pTilemapItem))
 						{
-							for(int i = 0; i < tiles->width*tiles->height; i++)
+							for(int i = 0; i < pTiles->m_Width*pTiles->m_Height; i++)
 							{
-								if(tiles->tiles[i].index)
-									tiles->tiles[i].index += ENTITY_OFFSET;
+								if(pTiles->m_pTiles[i].m_Index)
+									pTiles->m_pTiles[i].m_Index += ENTITY_OFFSET;
 							}
 						}
 						
-						datafile_unload_data(df, tilemap_item->data);
+						DataFile.UnloadData(pTilemapItem->m_Data);
 					}
-					else if(layer_item->type == LAYERTYPE_QUADS)
+					else if(pLayerItem->m_Type == LAYERTYPE_QUADS)
 					{
-						MAPITEM_LAYER_QUADS *quads_item = (MAPITEM_LAYER_QUADS *)layer_item;
-						LAYER_QUADS *quads = new LAYER_QUADS;
-						layer = quads;
-						quads->image = quads_item->image;
-						if(quads->image < -1 || quads->image >= images.len())
-							quads->image = -1;
-						void *data = datafile_get_data_swapped(df, quads_item->data);
-						group->add_layer(quads);
-						quads->quads.setsize(quads_item->num_quads);
-						mem_copy(quads->quads.getptr(), data, sizeof(QUAD)*quads_item->num_quads);
-						datafile_unload_data(df, quads_item->data);
+						CMapItemLayerQuads *pQuadsItem = (CMapItemLayerQuads *)pLayerItem;
+						CLayerQuads *pQuads = new CLayerQuads;
+						pQuads->m_pEditor = m_pEditor;
+						pLayer = pQuads;
+						pQuads->m_Image = pQuadsItem->m_Image;
+						if(pQuads->m_Image < -1 || pQuads->m_Image >= m_lImages.size())
+							pQuads->m_Image = -1;
+						void *pData = DataFile.GetDataSwapped(pQuadsItem->m_Data);
+						pGroup->AddLayer(pQuads);
+						pQuads->m_lQuads.set_size(pQuadsItem->m_NumQuads);
+						mem_copy(pQuads->m_lQuads.base_ptr(), pData, sizeof(CQuad)*pQuadsItem->m_NumQuads);
+						DataFile.UnloadData(pQuadsItem->m_Data);
 					}
 					
-					if(layer)
-						layer->flags = layer_item->flags;
+					if(pLayer)
+						pLayer->m_Flags = pLayerItem->m_Flags;
 				}
 			}
 		}
 		
 		// load envelopes
 		{
-			ENVPOINT *points = 0;
+			CEnvPoint *pPoints = 0;
 			
 			{
-				int start, num;
-				datafile_get_type(df, MAPITEMTYPE_ENVPOINTS, &start, &num);
-				if(num)
-					points = (ENVPOINT *)datafile_get_item(df, start, 0, 0);
+				int Start, Num;
+				DataFile.GetType(MAPITEMTYPE_ENVPOINTS, &Start, &Num);
+				if(Num)
+					pPoints = (CEnvPoint *)DataFile.GetItem(Start, 0, 0);
 			}
 			
-			int start, num;
-			datafile_get_type(df, MAPITEMTYPE_ENVELOPE, &start, &num);
-			for(int e = 0; e < num; e++)
+			int Start, Num;
+			DataFile.GetType(MAPITEMTYPE_ENVELOPE, &Start, &Num);
+			for(int e = 0; e < Num; e++)
 			{
-				MAPITEM_ENVELOPE *item = (MAPITEM_ENVELOPE *)datafile_get_item(df, start+e, 0, 0);
-				ENVELOPE *env = new ENVELOPE(item->channels);
-				env->points.setsize(item->num_points);
-				mem_copy(env->points.getptr(), &points[item->start_point], sizeof(ENVPOINT)*item->num_points);
-				envelopes.add(env);
+				CMapItemEnvelope *pItem = (CMapItemEnvelope *)DataFile.GetItem(Start+e, 0, 0);
+				CEnvelope *pEnv = new CEnvelope(pItem->m_Channels);
+				pEnv->m_lPoints.set_size(pItem->m_NumPoints);
+				mem_copy(pEnv->m_lPoints.base_ptr(), &pPoints[pItem->m_StartPoint], sizeof(CEnvPoint)*pItem->m_NumPoints);
+				m_lEnvelopes.add(pEnv);
 			}
 		}
 	}
 	
-	datafile_unload(df);
-	
-	return 0;
+	return 1;
 }
 
-static int modify_add_amount = 0;
-static void modify_add(int *index)
+static int gs_ModifyAddAmount = 0;
+static void ModifyAdd(int *pIndex)
 {
-	if(*index >= 0)
-		*index += modify_add_amount;
+	if(*pIndex >= 0)
+		*pIndex += gs_ModifyAddAmount;
 }
 
-int EDITOR::append(const char *filename)
+int CEditor::Append(const char *pFileName)
 {
-	MAP new_map;
-	int err;
-	err = new_map.load(filename);
-	if(err)
-		return err;
+	CEditorMap NewMap;
+	NewMap.m_pEditor = this;
+
+	int Err;
+	Err = NewMap.Load(Kernel()->RequestInterface<IStorage>(), pFileName);
+	if(Err)
+		return Err;
 
 	// modify indecies	
-	modify_add_amount = map.images.len();
-	new_map.modify_image_index(modify_add);
+	gs_ModifyAddAmount = m_Map.m_lImages.size();
+	NewMap.ModifyImageIndex(ModifyAdd);
 	
-	modify_add_amount = map.envelopes.len();
-	new_map.modify_envelope_index(modify_add);
+	gs_ModifyAddAmount = m_Map.m_lEnvelopes.size();
+	NewMap.ModifyEnvelopeIndex(ModifyAdd);
 	
 	// transfer images
-	for(int i = 0; i < new_map.images.len(); i++)
-		map.images.add(new_map.images[i]);
-	new_map.images.clear();
+	for(int i = 0; i < NewMap.m_lImages.size(); i++)
+		m_Map.m_lImages.add(NewMap.m_lImages[i]);
+	NewMap.m_lImages.clear();
 	
 	// transfer envelopes
-	for(int i = 0; i < new_map.envelopes.len(); i++)
-		map.envelopes.add(new_map.envelopes[i]);
-	new_map.envelopes.clear();
+	for(int i = 0; i < NewMap.m_lEnvelopes.size(); i++)
+		m_Map.m_lEnvelopes.add(NewMap.m_lEnvelopes[i]);
+	NewMap.m_lEnvelopes.clear();
 
 	// transfer groups
 	
-	for(int i = 0; i < new_map.groups.len(); i++)
+	for(int i = 0; i < NewMap.m_lGroups.size(); i++)
 	{
-		if(new_map.groups[i] == new_map.game_group)
-			delete new_map.groups[i];
+		if(NewMap.m_lGroups[i] == NewMap.m_pGameGroup)
+			delete NewMap.m_lGroups[i];
 		else
-			map.groups.add(new_map.groups[i]);
+		{
+			NewMap.m_lGroups[i]->m_pMap = &m_Map;
+			m_Map.m_lGroups.add(NewMap.m_lGroups[i]);
+		}
 	}
-	new_map.groups.clear();
+	NewMap.m_lGroups.clear();
 	
 	// all done \o/
 	return 0;
diff --git a/src/game/editor/ed_layer_game.cpp b/src/game/editor/ed_layer_game.cpp
index 9010bc71..82a9cb1d 100644
--- a/src/game/editor/ed_layer_game.cpp
+++ b/src/game/editor/ed_layer_game.cpp
@@ -1,20 +1,20 @@
-#include "ed_editor.hpp"
+#include "ed_editor.h"
 
 
-LAYER_GAME::LAYER_GAME(int w, int h)
-: LAYER_TILES(w, h)
+CLayerGame::CLayerGame(int w, int h)
+: CLayerTiles(w, h)
 {
-	type_name = "Game";
-	game = 1;
+	m_pTypeName = "Game";
+	m_Game = 1;
 }
 
-LAYER_GAME::~LAYER_GAME()
+CLayerGame::~CLayerGame()
 {
 }
 
-int LAYER_GAME::render_properties(CUIRect *toolbox)
+int CLayerGame::RenderProperties(CUIRect *pToolbox)
 {
-	int r = LAYER_TILES::render_properties(toolbox);
-	image = -1;
+	int r = CLayerTiles::RenderProperties(pToolbox);
+	m_Image = -1;
 	return r;
 }
diff --git a/src/game/editor/ed_layer_quads.cpp b/src/game/editor/ed_layer_quads.cpp
index ce1ba4b6..3aeffc73 100644
--- a/src/game/editor/ed_layer_quads.cpp
+++ b/src/game/editor/ed_layer_quads.cpp
@@ -1,213 +1,216 @@
-#include <base/math.hpp>
+#include <base/math.h>
 
-#include <engine/client/graphics.h>
+#include <engine/graphics.h>
 
-#include "ed_editor.hpp"
-#include <game/generated/gc_data.hpp>
-#include <game/client/render.hpp>
+#include "ed_editor.h"
+#include <game/generated/client_data.h>
+#include <game/client/render.h>
 
-LAYER_QUADS::LAYER_QUADS()
+CLayerQuads::CLayerQuads()
 {
-	type = LAYERTYPE_QUADS;
-	type_name = "Quads";
-	image = -1;
+	m_Type = LAYERTYPE_QUADS;
+	m_pTypeName = "Quads";
+	m_Image = -1;
 }
 
-LAYER_QUADS::~LAYER_QUADS()
+CLayerQuads::~CLayerQuads()
 {
 }
 
-static void envelope_eval(float time_offset, int env, float *channels, void *user)
+static void EnvelopeEval(float TimeOffset, int Env, float *pChannels, void *pUser)
 {
-	EDITOR *pEditor = (EDITOR *)user;
-	if(env < 0 || env > pEditor->map.envelopes.len())
+	CEditor *pEditor = (CEditor *)pUser;
+	if(Env < 0 || Env > pEditor->m_Map.m_lEnvelopes.size())
 	{
-		channels[0] = 0;
-		channels[1] = 0;
-		channels[2] = 0;
-		channels[3] = 0;
+		pChannels[0] = 0;
+		pChannels[1] = 0;
+		pChannels[2] = 0;
+		pChannels[3] = 0;
 		return;
 	}
 		
-	ENVELOPE *e = pEditor->map.envelopes[env];
-	float t = pEditor->animate_time+time_offset;
-	t *= pEditor->animate_speed;
-	e->eval(t, channels);
+	CEnvelope *e = pEditor->m_Map.m_lEnvelopes[Env];
+	float t = pEditor->m_AnimateTime+TimeOffset;
+	t *= pEditor->m_AnimateSpeed;
+	e->Eval(t, pChannels);
 }
 
-void LAYER_QUADS::render()
+void CLayerQuads::Render()
 {
 	Graphics()->TextureSet(-1);
-	if(image >= 0 && image < editor->map.images.len())
-		Graphics()->TextureSet(editor->map.images[image]->tex_id);
+	if(m_Image >= 0 && m_Image < m_pEditor->m_Map.m_lImages.size())
+		Graphics()->TextureSet(m_pEditor->m_Map.m_lImages[m_Image]->m_TexId);
 		
-	editor->RenderTools()->render_quads(quads.getptr(), quads.len(), LAYERRENDERFLAG_OPAQUE|LAYERRENDERFLAG_TRANSPARENT, envelope_eval, editor);
+	m_pEditor->RenderTools()->RenderQuads(m_lQuads.base_ptr(), m_lQuads.size(), LAYERRENDERFLAG_OPAQUE|LAYERRENDERFLAG_TRANSPARENT, EnvelopeEval, m_pEditor);
 }
 
-QUAD *LAYER_QUADS::new_quad()
+CQuad *CLayerQuads::NewQuad()
 {
-	QUAD *q = &quads[quads.add(QUAD())];
+	CQuad *q = &m_lQuads[m_lQuads.add(CQuad())];
 
-	q->pos_env = -1;
-	q->color_env = -1;
-	q->pos_env_offset = 0;
-	q->color_env_offset = 0;
+	q->m_PosEnv = -1;
+	q->m_ColorEnv = -1;
+	q->m_PosEnvOffset = 0;
+	q->m_ColorEnvOffset = 0;
 	int x = 0, y = 0;
-	q->points[0].x = x;
-	q->points[0].y = y;
-	q->points[1].x = x+64;
-	q->points[1].y = y;
-	q->points[2].x = x;
-	q->points[2].y = y+64;
-	q->points[3].x = x+64;
-	q->points[3].y = y+64;
-
-	q->points[4].x = x+32; // pivot
-	q->points[4].y = y+32;
+	q->m_aPoints[0].x = x;
+	q->m_aPoints[0].y = y;
+	q->m_aPoints[1].x = x+64;
+	q->m_aPoints[1].y = y;
+	q->m_aPoints[2].x = x;
+	q->m_aPoints[2].y = y+64;
+	q->m_aPoints[3].x = x+64;
+	q->m_aPoints[3].y = y+64;
+
+	q->m_aPoints[4].x = x+32; // pivot
+	q->m_aPoints[4].y = y+32;
 	
 	for(int i = 0; i < 5; i++)
 	{
-		q->points[i].x <<= 10;
-		q->points[i].y <<= 10;
+		q->m_aPoints[i].x <<= 10;
+		q->m_aPoints[i].y <<= 10;
 	}
 	
 
-	q->texcoords[0].x = 0;
-	q->texcoords[0].y = 0;
+	q->m_aTexcoords[0].x = 0;
+	q->m_aTexcoords[0].y = 0;
 	
-	q->texcoords[1].x = 1<<10;
-	q->texcoords[1].y = 0;
+	q->m_aTexcoords[1].x = 1<<10;
+	q->m_aTexcoords[1].y = 0;
 	
-	q->texcoords[2].x = 0;
-	q->texcoords[2].y = 1<<10;
+	q->m_aTexcoords[2].x = 0;
+	q->m_aTexcoords[2].y = 1<<10;
 	
-	q->texcoords[3].x = 1<<10;
-	q->texcoords[3].y = 1<<10;
+	q->m_aTexcoords[3].x = 1<<10;
+	q->m_aTexcoords[3].y = 1<<10;
 	
-	q->colors[0].r = 255; q->colors[0].g = 255; q->colors[0].b = 255; q->colors[0].a = 255;
-	q->colors[1].r = 255; q->colors[1].g = 255; q->colors[1].b = 255; q->colors[1].a = 255;
-	q->colors[2].r = 255; q->colors[2].g = 255; q->colors[2].b = 255; q->colors[2].a = 255;
-	q->colors[3].r = 255; q->colors[3].g = 255; q->colors[3].b = 255; q->colors[3].a = 255;
+	q->m_aColors[0].r = 255; q->m_aColors[0].g = 255; q->m_aColors[0].b = 255; q->m_aColors[0].a = 255;
+	q->m_aColors[1].r = 255; q->m_aColors[1].g = 255; q->m_aColors[1].b = 255; q->m_aColors[1].a = 255;
+	q->m_aColors[2].r = 255; q->m_aColors[2].g = 255; q->m_aColors[2].b = 255; q->m_aColors[2].a = 255;
+	q->m_aColors[3].r = 255; q->m_aColors[3].g = 255; q->m_aColors[3].b = 255; q->m_aColors[3].a = 255;
 
 	return q;
 }
 
-void LAYER_QUADS::brush_selecting(CUIRect rect)
+void CLayerQuads::BrushSelecting(CUIRect Rect)
 {
 	// draw selection rectangle
+	IGraphics::CLineItem Array[4] = {
+		IGraphics::CLineItem(Rect.x, Rect.y, Rect.x+Rect.w, Rect.y),
+		IGraphics::CLineItem(Rect.x+Rect.w, Rect.y, Rect.x+Rect.w, Rect.y+Rect.h),
+		IGraphics::CLineItem(Rect.x+Rect.w, Rect.y+Rect.h, Rect.x, Rect.y+Rect.h),
+		IGraphics::CLineItem(Rect.x, Rect.y+Rect.h, Rect.x, Rect.y)};
 	Graphics()->TextureSet(-1);
 	Graphics()->LinesBegin();
-	Graphics()->LinesDraw(rect.x, rect.y, rect.x+rect.w, rect.y);
-	Graphics()->LinesDraw(rect.x+rect.w, rect.y, rect.x+rect.w, rect.y+rect.h);
-	Graphics()->LinesDraw(rect.x+rect.w, rect.y+rect.h, rect.x, rect.y+rect.h);
-	Graphics()->LinesDraw(rect.x, rect.y+rect.h, rect.x, rect.y);
+	Graphics()->LinesDraw(Array, 4);
 	Graphics()->LinesEnd();
 }
 
-int LAYER_QUADS::brush_grab(LAYERGROUP *brush, CUIRect rect)
+int CLayerQuads::BrushGrab(CLayerGroup *pBrush, CUIRect Rect)
 {
 	// create new layers
-	LAYER_QUADS *grabbed = new LAYER_QUADS();
-	grabbed->image = image;
-	brush->add_layer(grabbed);
+	CLayerQuads *pGrabbed = new CLayerQuads();
+	pGrabbed->m_pEditor = m_pEditor;
+	pGrabbed->m_Image = m_Image;
+	pBrush->AddLayer(pGrabbed);
 	
 	//dbg_msg("", "%f %f %f %f", rect.x, rect.y, rect.w, rect.h);
-	for(int i = 0; i < quads.len(); i++)
+	for(int i = 0; i < m_lQuads.size(); i++)
 	{
-		QUAD *q = &quads[i];
-		float px = fx2f(q->points[4].x);
-		float py = fx2f(q->points[4].y);
+		CQuad *q = &m_lQuads[i];
+		float px = fx2f(q->m_aPoints[4].x);
+		float py = fx2f(q->m_aPoints[4].y);
 		
-		if(px > rect.x && px < rect.x+rect.w && py > rect.y && py < rect.y+rect.h)
+		if(px > Rect.x && px < Rect.x+Rect.w && py > Rect.y && py < Rect.y+Rect.h)
 		{
 			dbg_msg("", "grabbed one");
-			QUAD n;
+			CQuad n;
 			n = *q;
 			
 			for(int p = 0; p < 5; p++)
 			{
-				n.points[p].x -= f2fx(rect.x);
-				n.points[p].y -= f2fx(rect.y);
+				n.m_aPoints[p].x -= f2fx(Rect.x);
+				n.m_aPoints[p].y -= f2fx(Rect.y);
 			}
 			
-			grabbed->quads.add(n);
+			pGrabbed->m_lQuads.add(n);
 		}
 	}
 	
-	return grabbed->quads.len()?1:0;
+	return pGrabbed->m_lQuads.size()?1:0;
 }
 
-void LAYER_QUADS::brush_place(LAYER *brush, float wx, float wy)
+void CLayerQuads::BrushPlace(CLayer *pBrush, float wx, float wy)
 {
-	LAYER_QUADS *l = (LAYER_QUADS *)brush;
-	for(int i = 0; i < l->quads.len(); i++)
+	CLayerQuads *l = (CLayerQuads *)pBrush;
+	for(int i = 0; i < l->m_lQuads.size(); i++)
 	{
-		QUAD n = l->quads[i];
+		CQuad n = l->m_lQuads[i];
 		
 		for(int p = 0; p < 5; p++)
 		{
-			n.points[p].x += f2fx(wx);
-			n.points[p].y += f2fx(wy);
+			n.m_aPoints[p].x += f2fx(wx);
+			n.m_aPoints[p].y += f2fx(wy);
 		}
 			
-		quads.add(n);
+		m_lQuads.add(n);
 	}
 }
 
-void LAYER_QUADS::brush_flip_x()
+void CLayerQuads::BrushFlipX()
 {
 }
 
-void LAYER_QUADS::brush_flip_y()
+void CLayerQuads::BrushFlipY()
 {
 }
 
-void rotate(vec2 *center, vec2 *point, float rotation)
+void Rotate(vec2 *pCenter, vec2 *pPoint, float Rotation)
 {
-	float x = point->x - center->x;
-	float y = point->y - center->y;
-	point->x = x * cosf(rotation) - y * sinf(rotation) + center->x;
-	point->y = x * sinf(rotation) + y * cosf(rotation) + center->y;
+	float x = pPoint->x - pCenter->x;
+	float y = pPoint->y - pCenter->y;
+	pPoint->x = x * cosf(Rotation) - y * sinf(Rotation) + pCenter->x;
+	pPoint->y = x * sinf(Rotation) + y * cosf(Rotation) + pCenter->y;
 }
 
-void LAYER_QUADS::brush_rotate(float amount)
+void CLayerQuads::BrushRotate(float Amount)
 {
-	vec2 center;
-	get_size(&center.x, &center.y);
-	center.x /= 2;
-	center.y /= 2;
+	vec2 Center;
+	GetSize(&Center.x, &Center.y);
+	Center.x /= 2;
+	Center.y /= 2;
 
-	for(int i = 0; i < quads.len(); i++)
+	for(int i = 0; i < m_lQuads.size(); i++)
 	{
-		QUAD *q = &quads[i];
+		CQuad *q = &m_lQuads[i];
 		
 		for(int p = 0; p < 5; p++)
 		{
-			vec2 pos(fx2f(q->points[p].x), fx2f(q->points[p].y));
-			rotate(&center, &pos, amount);
-			q->points[p].x = f2fx(pos.x);
-			q->points[p].y = f2fx(pos.y);
+			vec2 Pos(fx2f(q->m_aPoints[p].x), fx2f(q->m_aPoints[p].y));
+			Rotate(&Center, &Pos, Amount);
+			q->m_aPoints[p].x = f2fx(Pos.x);
+			q->m_aPoints[p].y = f2fx(Pos.y);
 		}
 	}
 }
 
-void LAYER_QUADS::get_size(float *w, float *h)
+void CLayerQuads::GetSize(float *w, float *h)
 {
 	*w = 0; *h = 0;
 	
-	for(int i = 0; i < quads.len(); i++)
+	for(int i = 0; i < m_lQuads.size(); i++)
 	{
 		for(int p = 0; p < 5; p++)
 		{
-			*w = max(*w, fx2f(quads[i].points[p].x));
-			*h = max(*h, fx2f(quads[i].points[p].y));
+			*w = max(*w, fx2f(m_lQuads[i].m_aPoints[p].x));
+			*h = max(*h, fx2f(m_lQuads[i].m_aPoints[p].y));
 		}
 	}
 }
 
-extern int selected_points;
+extern int gs_SelectedPoints;
 
-int LAYER_QUADS::render_properties(CUIRect *toolbox)
+int CLayerQuads::RenderProperties(CUIRect *pToolBox)
 {
 	// layer props
 	enum
@@ -216,37 +219,37 @@ int LAYER_QUADS::render_properties(CUIRect *toolbox)
 		NUM_PROPS,
 	};
 	
-	PROPERTY props[] = {
-		{"Image", image, PROPTYPE_IMAGE, -1, 0},
+	CProperty aProps[] = {
+		{"Image", m_Image, PROPTYPE_IMAGE, -1, 0},
 		{0},
 	};
 	
-	static int ids[NUM_PROPS] = {0};
-	int new_val = 0;
-	int prop = editor->do_properties(toolbox, props, ids, &new_val);		
+	static int s_aIds[NUM_PROPS] = {0};
+	int NewVal = 0;
+	int Prop = m_pEditor->DoProperties(pToolBox, aProps, s_aIds, &NewVal);		
 	
-	if(prop == PROP_IMAGE)
+	if(Prop == PROP_IMAGE)
 	{
-		if(new_val >= 0)
-			image = new_val%editor->map.images.len();
+		if(NewVal >= 0)
+			m_Image = NewVal%m_pEditor->m_Map.m_lImages.size();
 		else
-			image = -1;
+			m_Image = -1;
 	}
 
 	return 0;
 }
 
 
-void LAYER_QUADS::modify_image_index(INDEX_MODIFY_FUNC func)
+void CLayerQuads::ModifyImageIndex(INDEX_MODIFY_FUNC Func)
 {
-	func(&image);
+	Func(&m_Image);
 }
 
-void LAYER_QUADS::modify_envelope_index(INDEX_MODIFY_FUNC func)
+void CLayerQuads::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
 {
-	for(int i = 0; i < quads.len(); i++)
+	for(int i = 0; i < m_lQuads.size(); i++)
 	{
-		func(&quads[i].pos_env);
-		func(&quads[i].color_env);
+		Func(&m_lQuads[i].m_PosEnv);
+		Func(&m_lQuads[i].m_ColorEnv);
 	}
 }
diff --git a/src/game/editor/ed_layer_tiles.cpp b/src/game/editor/ed_layer_tiles.cpp
index 0d42cb78..ecd7c62c 100644
--- a/src/game/editor/ed_layer_tiles.cpp
+++ b/src/game/editor/ed_layer_tiles.cpp
@@ -1,247 +1,281 @@
-#include <base/math.hpp>
+#include <base/math.h>
 
-#include <engine/client/graphics.h>
+#include <engine/graphics.h>
+#include <engine/textrender.h>
 
-#include <game/generated/gc_data.hpp>
-#include <game/client/render.hpp>
-#include "ed_editor.hpp"
+#include <game/generated/client_data.h>
+#include <game/client/render.h>
+#include "ed_editor.h"
 
-LAYER_TILES::LAYER_TILES(int w, int h)
+CLayerTiles::CLayerTiles(int w, int h)
 {
-	type = LAYERTYPE_TILES;
-	type_name = "Tiles";
-	width = w;
-	height = h;
-	image = -1;
-	tex_id = -1;
-	game = 0;
+	m_Type = LAYERTYPE_TILES;
+	m_pTypeName = "Tiles";
+	m_Width = w;
+	m_Height = h;
+	m_Image = -1;
+	m_TexId = -1;
+	m_Game = 0;
 	
-	tiles = new TILE[width*height];
-	mem_zero(tiles, width*height*sizeof(TILE));
+	m_pTiles = new CTile[m_Width*m_Height];
+	mem_zero(m_pTiles, m_Width*m_Height*sizeof(CTile));
 }
 
-LAYER_TILES::~LAYER_TILES()
+CLayerTiles::~CLayerTiles()
 {
-	delete [] tiles;
+	delete [] m_pTiles;
 }
 
-void LAYER_TILES::prepare_for_save()
+void CLayerTiles::PrepareForSave()
 {
-	for(int y = 0; y < height; y++)
-		for(int x = 0; x < width; x++)
-			tiles[y*width+x].flags &= TILEFLAG_VFLIP|TILEFLAG_HFLIP;
+	for(int y = 0; y < m_Height; y++)
+		for(int x = 0; x < m_Width; x++)
+			m_pTiles[y*m_Width+x].m_Flags &= TILEFLAG_VFLIP|TILEFLAG_HFLIP;
 
-	if(image != -1)
+	if(m_Image != -1)
 	{
-		for(int y = 0; y < height; y++)
-			for(int x = 0; x < width; x++)
-				tiles[y*width+x].flags |= editor->map.images[image]->tileflags[tiles[y*width+x].index];
+		for(int y = 0; y < m_Height; y++)
+			for(int x = 0; x < m_Width; x++)
+				m_pTiles[y*m_Width+x].m_Flags |= m_pEditor->m_Map.m_lImages[m_Image]->m_aTileFlags[m_pTiles[y*m_Width+x].m_Index];
 	}
 }
 
-void LAYER_TILES::make_palette()
+void CLayerTiles::MakePalette()
 {
-	for(int y = 0; y < height; y++)
-		for(int x = 0; x < width; x++)
-			tiles[y*width+x].index = y*16+x;
+	for(int y = 0; y < m_Height; y++)
+		for(int x = 0; x < m_Width; x++)
+			m_pTiles[y*m_Width+x].m_Index = y*16+x;
 }
 
-void LAYER_TILES::render()
+void CLayerTiles::Render()
 {
-	if(image >= 0 && image < editor->map.images.len())
-		tex_id = editor->map.images[image]->tex_id;
-	Graphics()->TextureSet(tex_id);
-	editor->RenderTools()->render_tilemap(tiles, width, height, 32.0f, vec4(1,1,1,1), LAYERRENDERFLAG_OPAQUE|LAYERRENDERFLAG_TRANSPARENT);
+	if(m_Image >= 0 && m_Image < m_pEditor->m_Map.m_lImages.size())
+		m_TexId = m_pEditor->m_Map.m_lImages[m_Image]->m_TexId;
+	Graphics()->TextureSet(m_TexId);
+	m_pEditor->RenderTools()->RenderTilemap(m_pTiles, m_Width, m_Height, 32.0f, vec4(1,1,1,1), LAYERRENDERFLAG_OPAQUE|LAYERRENDERFLAG_TRANSPARENT);
 }
 
-int LAYER_TILES::convert_x(float x) const { return (int)(x/32.0f); }
-int LAYER_TILES::convert_y(float y) const { return (int)(y/32.0f); }
+int CLayerTiles::ConvertX(float x) const { return (int)(x/32.0f); }
+int CLayerTiles::ConvertY(float y) const { return (int)(y/32.0f); }
 
-void LAYER_TILES::convert(CUIRect rect, RECTi *out)
+void CLayerTiles::Convert(CUIRect Rect, RECTi *pOut)
 {
-	out->x = convert_x(rect.x);
-	out->y = convert_y(rect.y);
-	out->w = convert_x(rect.x+rect.w+31) - out->x;
-	out->h = convert_y(rect.y+rect.h+31) - out->y;
+	pOut->x = ConvertX(Rect.x);
+	pOut->y = ConvertY(Rect.y);
+	pOut->w = ConvertX(Rect.x+Rect.w+31) - pOut->x;
+	pOut->h = ConvertY(Rect.y+Rect.h+31) - pOut->y;
 }
 
-void LAYER_TILES::snap(CUIRect *rect)
+void CLayerTiles::Snap(CUIRect *pRect)
 {
-	RECTi out;
-	convert(*rect, &out);
-	rect->x = out.x*32.0f;
-	rect->y = out.y*32.0f;
-	rect->w = out.w*32.0f;
-	rect->h = out.h*32.0f;
+	RECTi Out;
+	Convert(*pRect, &Out);
+	pRect->x = Out.x*32.0f;
+	pRect->y = Out.y*32.0f;
+	pRect->w = Out.w*32.0f;
+	pRect->h = Out.h*32.0f;
 }
 
-void LAYER_TILES::clamp(RECTi *rect)
+void CLayerTiles::Clamp(RECTi *pRect)
 {
-	if(rect->x < 0)
+	if(pRect->x < 0)
 	{
-		rect->w += rect->x;
-		rect->x = 0;
+		pRect->w += pRect->x;
+		pRect->x = 0;
 	}
 		
-	if(rect->y < 0)
+	if(pRect->y < 0)
 	{
-		rect->h += rect->y;
-		rect->y = 0;
+		pRect->h += pRect->y;
+		pRect->y = 0;
 	}
 	
-	if(rect->x+rect->w > width)
-		rect->w = width-rect->x;
+	if(pRect->x+pRect->w > m_Width)
+		pRect->w = m_Width - pRect->x;
 
-	if(rect->y+rect->h > height)
-		rect->h = height-rect->y;
+	if(pRect->y+pRect->h > m_Height)
+		pRect->h = m_Height - pRect->y;
 		
-	if(rect->h < 0)
-		rect->h = 0;
-	if(rect->w < 0)
-		rect->w = 0;
+	if(pRect->h < 0)
+		pRect->h = 0;
+	if(pRect->w < 0)
+		pRect->w = 0;
 }
 
-void LAYER_TILES::brush_selecting(CUIRect rect)
+void CLayerTiles::BrushSelecting(CUIRect Rect)
 {
 	Graphics()->TextureSet(-1);
-	editor->Graphics()->QuadsBegin();
-	editor->Graphics()->SetColor(1,1,1,0.4f);
-	snap(&rect);
-	editor->Graphics()->QuadsDrawTL(rect.x, rect.y, rect.w, rect.h);
-	editor->Graphics()->QuadsEnd();
-	char buf[16];
-	str_format(buf, sizeof(buf), "%d,%d", convert_x(rect.w), convert_y(rect.h));
-	gfx_text(0, rect.x+3.0f, rect.y+3.0f, 15.0f*editor->world_zoom, buf, -1);
+	m_pEditor->Graphics()->QuadsBegin();
+	m_pEditor->Graphics()->SetColor(1.0f, 1.0f, 1.0f, 0.4f);
+	Snap(&Rect);
+	IGraphics::CQuadItem QuadItem(Rect.x, Rect.y, Rect.w, Rect.h);
+	m_pEditor->Graphics()->QuadsDrawTL(&QuadItem, 1);
+	m_pEditor->Graphics()->QuadsEnd();
+	char aBuf[16];
+	str_format(aBuf, sizeof(aBuf), "%d,%d", ConvertX(Rect.w), ConvertY(Rect.h));
+	TextRender()->Text(0, Rect.x+3.0f, Rect.y+3.0f, 15.0f*m_pEditor->m_WorldZoom, aBuf, -1);
 }
 
-int LAYER_TILES::brush_grab(LAYERGROUP *brush, CUIRect rect)
+int CLayerTiles::BrushGrab(CLayerGroup *pBrush, CUIRect Rect)
 {
 	RECTi r;
-	convert(rect, &r);
-	clamp(&r);
+	Convert(Rect, &r);
+	Clamp(&r);
 	
 	if(!r.w || !r.h)
 		return 0;
 	
 	// create new layers
-	LAYER_TILES *grabbed = new LAYER_TILES(r.w, r.h);
-	grabbed->tex_id = tex_id;
-	grabbed->image = image;
-	brush->add_layer(grabbed);
+	CLayerTiles *pGrabbed = new CLayerTiles(r.w, r.h);
+	pGrabbed->m_pEditor = m_pEditor;
+	pGrabbed->m_TexId = m_TexId;
+	pGrabbed->m_Image = m_Image;
+	pBrush->AddLayer(pGrabbed);
 	
 	// copy the tiles
 	for(int y = 0; y < r.h; y++)
 		for(int x = 0; x < r.w; x++)
-			grabbed->tiles[y*grabbed->width+x] = tiles[(r.y+y)*width+(r.x+x)];
+			pGrabbed->m_pTiles[y*pGrabbed->m_Width+x] = m_pTiles[(r.y+y)*m_Width+(r.x+x)];
 	
 	return 1;
 }
 
-void LAYER_TILES::brush_draw(LAYER *brush, float wx, float wy)
+void CLayerTiles::FillSelection(bool Empty, CLayer *pBrush, CUIRect Rect)
 {
-	if(readonly)
+	if(m_Readonly)
+		return;
+		
+	int sx = ConvertX(Rect.x);
+	int sy = ConvertY(Rect.y);
+	int w = ConvertX(Rect.w);
+	int h = ConvertY(Rect.h);
+	
+	CLayerTiles *pLt = static_cast<CLayerTiles*>(pBrush);
+	
+	for(int y = 0; y <= h; y++)
+	{
+		for(int x = 0; x <= w; x++)
+		{
+			int fx = x+sx;
+			int fy = y+sy;
+			
+			if(fx < 0 || fx >= m_Width || fy < 0 || fy >= m_Height)
+				continue;
+			
+			if(Empty)	
+                m_pTiles[fy*m_Width+fx].m_Index = 1;
+            else
+                m_pTiles[fy*m_Width+fx] = pLt->m_pTiles[(y*pLt->m_Width + x%pLt->m_Width) % (pLt->m_Width*pLt->m_Height)];
+		}
+	}
+}
+
+void CLayerTiles::BrushDraw(CLayer *pBrush, float wx, float wy)
+{
+	if(m_Readonly)
 		return;
 	
 	//
-	LAYER_TILES *l = (LAYER_TILES *)brush;
-	int sx = convert_x(wx);
-	int sy = convert_y(wy);
+	CLayerTiles *l = (CLayerTiles *)pBrush;
+	int sx = ConvertX(wx);
+	int sy = ConvertY(wy);
 	
-	for(int y = 0; y < l->height; y++)
-		for(int x = 0; x < l->width; x++)
+	for(int y = 0; y < l->m_Height; y++)
+		for(int x = 0; x < l->m_Width; x++)
 		{
 			int fx = x+sx;
 			int fy = y+sy;
-			if(fx<0 || fx >= width || fy < 0 || fy >= height)
+			if(fx<0 || fx >= m_Width || fy < 0 || fy >= m_Height)
 				continue;
 				
-			tiles[fy*width+fx] = l->tiles[y*l->width+x];
+			m_pTiles[fy*m_Width+fx] = l->m_pTiles[y*l->m_Width+x];
 		}
 }
 
-void LAYER_TILES::brush_flip_x()
+void CLayerTiles::BrushFlipX()
 {
-	for(int y = 0; y < height; y++)
-		for(int x = 0; x < width/2; x++)
+	for(int y = 0; y < m_Height; y++)
+		for(int x = 0; x < m_Width/2; x++)
 		{
-			TILE tmp = tiles[y*width+x];
-			tiles[y*width+x] = tiles[y*width+width-1-x];
-			tiles[y*width+width-1-x] = tmp;
+			CTile Tmp = m_pTiles[y*m_Width+x];
+			m_pTiles[y*m_Width+x] = m_pTiles[y*m_Width+m_Width-1-x];
+			m_pTiles[y*m_Width+m_Width-1-x] = Tmp;
 		}
 
-	for(int y = 0; y < height; y++)
-		for(int x = 0; x < width; x++)
-			tiles[y*width+x].flags ^= TILEFLAG_VFLIP;
+	for(int y = 0; y < m_Height; y++)
+		for(int x = 0; x < m_Width; x++)
+			m_pTiles[y*m_Width+x].m_Flags ^= TILEFLAG_VFLIP;
 }
 
-void LAYER_TILES::brush_flip_y()
+void CLayerTiles::BrushFlipY()
 {
-	for(int y = 0; y < height/2; y++)
-		for(int x = 0; x < width; x++)
+	for(int y = 0; y < m_Height/2; y++)
+		for(int x = 0; x < m_Width; x++)
 		{
-			TILE tmp = tiles[y*width+x];
-			tiles[y*width+x] = tiles[(height-1-y)*width+x];
-			tiles[(height-1-y)*width+x] = tmp;
+			CTile Tmp = m_pTiles[y*m_Width+x];
+			m_pTiles[y*m_Width+x] = m_pTiles[(m_Height-1-y)*m_Width+x];
+			m_pTiles[(m_Height-1-y)*m_Width+x] = Tmp;
 		}
 
-	for(int y = 0; y < height; y++)
-		for(int x = 0; x < width; x++)
-			tiles[y*width+x].flags ^= TILEFLAG_HFLIP;
+	for(int y = 0; y < m_Height; y++)
+		for(int x = 0; x < m_Width; x++)
+			m_pTiles[y*m_Width+x].m_Flags ^= TILEFLAG_HFLIP;
 }
 
-void LAYER_TILES::resize(int new_w, int new_h)
+void CLayerTiles::Resize(int NewW, int NewH)
 {
-	TILE *new_data = new TILE[new_w*new_h];
-	mem_zero(new_data, new_w*new_h*sizeof(TILE));
+	CTile *pNewData = new CTile[NewW*NewH];
+	mem_zero(pNewData, NewW*NewH*sizeof(CTile));
 
 	// copy old data	
-	for(int y = 0; y < min(new_h, height); y++)
-		mem_copy(&new_data[y*new_w], &tiles[y*width], min(width, new_w)*sizeof(TILE));
+	for(int y = 0; y < min(NewH, m_Height); y++)
+		mem_copy(&pNewData[y*NewW], &m_pTiles[y*m_Width], min(m_Width, NewW)*sizeof(CTile));
 	
 	// replace old
-	delete [] tiles;
-	tiles = new_data;
-	width = new_w;
-	height = new_h;
+	delete [] m_pTiles;
+	m_pTiles = pNewData;
+	m_Width = NewW;
+	m_Height = NewH;
 }
 
 
-int LAYER_TILES::render_properties(CUIRect *toolbox)
+int CLayerTiles::RenderProperties(CUIRect *pToolBox)
 {
-	CUIRect button;
-	toolbox->HSplitBottom(12.0f, toolbox, &button);
-	bool in_gamegroup = editor->map.game_group->layers.find(this) != -1;
-	if(editor->map.game_layer == this)
-		in_gamegroup = false;
-	static int colcl_button = 0;
-	if(editor->DoButton_Editor(&colcl_button, "Clear Collision", in_gamegroup?0:-1, &button, 0, "Removes collision from this layer"))
+	CUIRect Button;
+	pToolBox->HSplitBottom(12.0f, pToolBox, &Button);
+	
+	bool InGameGroup = !find_linear(m_pEditor->m_Map.m_pGameGroup->m_lLayers.all(), this).empty();
+	if(m_pEditor->m_Map.m_pGameLayer == this)
+		InGameGroup = false;
+	static int s_ColclButton = 0;
+	if(m_pEditor->DoButton_Editor(&s_ColclButton, "Clear Collision", InGameGroup?0:-1, &Button, 0, "Removes collision from this layer"))
 	{
-		LAYER_TILES *gl = editor->map.game_layer;
-		int w = min(gl->width, width);
-		int h = min(gl->height, height);
+		CLayerTiles *gl = m_pEditor->m_Map.m_pGameLayer;
+		int w = min(gl->m_Width, m_Width);
+		int h = min(gl->m_Height, m_Height);
 		for(int y = 0; y < h; y++)
 			for(int x = 0; x < w; x++)
 			{
-				if(gl->tiles[y*gl->width+x].index <= TILE_SOLID)
-					if(tiles[y*width+x].index)
-						gl->tiles[y*gl->width+x].index = TILE_AIR;
+				if(gl->m_pTiles[y*gl->m_Width+x].m_Index <= TILE_SOLID)
+					if(m_pTiles[y*m_Width+x].m_Index)
+						gl->m_pTiles[y*gl->m_Width+x].m_Index = TILE_AIR;
 			}
 			
 		return 1;
 	}
-	static int col_button = 0;
-	toolbox->HSplitBottom(5.0f, toolbox, &button);
-	toolbox->HSplitBottom(12.0f, toolbox, &button);
-	if(editor->DoButton_Editor(&col_button, "Make Collision", in_gamegroup?0:-1, &button, 0, "Constructs collision from this layer"))
+	static int s_ColButton = 0;
+	pToolBox->HSplitBottom(5.0f, pToolBox, &Button);
+	pToolBox->HSplitBottom(12.0f, pToolBox, &Button);
+	if(m_pEditor->DoButton_Editor(&s_ColButton, "Make Collision", InGameGroup?0:-1, &Button, 0, "Constructs collision from this layer"))
 	{
-		LAYER_TILES *gl = editor->map.game_layer;
-		int w = min(gl->width, width);
-		int h = min(gl->height, height);
+		CLayerTiles *gl = m_pEditor->m_Map.m_pGameLayer;
+		int w = min(gl->m_Width, m_Width);
+		int h = min(gl->m_Height, m_Height);
 		for(int y = 0; y < h; y++)
 			for(int x = 0; x < w; x++)
 			{
-				if(gl->tiles[y*gl->width+x].index <= TILE_SOLID)
-					gl->tiles[y*gl->width+x].index = tiles[y*width+x].index?TILE_SOLID:TILE_AIR;
+				if(gl->m_pTiles[y*gl->m_Width+x].m_Index <= TILE_SOLID)
+					gl->m_pTiles[y*gl->m_Width+x].m_Index = m_pTiles[y*m_Width+x].m_Index?TILE_SOLID:TILE_AIR;
 			}
 			
 		return 1;
@@ -255,44 +289,44 @@ int LAYER_TILES::render_properties(CUIRect *toolbox)
 		NUM_PROPS,
 	};
 	
-	PROPERTY props[] = {
-		{"Width", width, PROPTYPE_INT_STEP, 1, 1000000000},
-		{"Height", height, PROPTYPE_INT_STEP, 1, 1000000000},
-		{"Image", image, PROPTYPE_IMAGE, 0, 0},
+	CProperty aProps[] = {
+		{"Width", m_Width, PROPTYPE_INT_STEP, 1, 1000000000},
+		{"Height", m_Height, PROPTYPE_INT_STEP, 1, 1000000000},
+		{"Image", m_Image, PROPTYPE_IMAGE, 0, 0},
 		{0},
 	};
 	
-	if(editor->map.game_layer == this) // remove the image from the selection if this is the game layer
-		props[2].name = 0;
+	if(m_pEditor->m_Map.m_pGameLayer == this) // remove the image from the selection if this is the game layer
+		aProps[2].m_pName = 0;
 	
-	static int ids[NUM_PROPS] = {0};
-	int new_val = 0;
-	int prop = editor->do_properties(toolbox, props, ids, &new_val);		
+	static int s_aIds[NUM_PROPS] = {0};
+	int NewVal = 0;
+	int Prop = m_pEditor->DoProperties(pToolBox, aProps, s_aIds, &NewVal);		
 	
-	if(prop == PROP_WIDTH && new_val > 1)
-		resize(new_val, height);
-	else if(prop == PROP_HEIGHT && new_val > 1)
-		resize(width, new_val);
-	else if(prop == PROP_IMAGE)
+	if(Prop == PROP_WIDTH && NewVal > 1)
+		Resize(NewVal, m_Height);
+	else if(Prop == PROP_HEIGHT && NewVal > 1)
+		Resize(m_Width, NewVal);
+	else if(Prop == PROP_IMAGE)
 	{
-		if (new_val == -1)
+		if (NewVal == -1)
 		{
-			tex_id = -1;
-			image = -1;
+			m_TexId = -1;
+			m_Image = -1;
 		}
 		else
-			image = new_val%editor->map.images.len();
+			m_Image = NewVal%m_pEditor->m_Map.m_lImages.size();
 	}
 	
 	return 0;
 }
 
 
-void LAYER_TILES::modify_image_index(INDEX_MODIFY_FUNC func)
+void CLayerTiles::ModifyImageIndex(INDEX_MODIFY_FUNC Func)
 {
-	func(&image);
+	Func(&m_Image);
 }
 
-void LAYER_TILES::modify_envelope_index(INDEX_MODIFY_FUNC func)
+void CLayerTiles::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
 {
 }
diff --git a/src/game/editor/ed_popups.cpp b/src/game/editor/ed_popups.cpp
index 59140153..2e58ae3a 100644
--- a/src/game/editor/ed_popups.cpp
+++ b/src/game/editor/ed_popups.cpp
@@ -1,111 +1,114 @@
-#include <stdio.h>
-#include <engine/client/graphics.h>
-#include "ed_editor.hpp"
+#include <engine/graphics.h>
+#include <engine/input.h>
+#include <engine/keys.h>
+#include "ed_editor.h"
 
 
 // popup menu handling
 static struct
 {
-	CUIRect rect;
-	void *id;
-	int (*func)(EDITOR *pEditor, CUIRect rect);
-	int is_menu;
-	void *extra;
-} ui_popups[8];
+	CUIRect m_Rect;
+	void *m_pId;
+	int (*m_pfnFunc)(CEditor *pEditor, CUIRect Rect);
+	int m_IsMenu;
+	void *m_pExtra;
+} s_UiPopups[8];
 
-static int ui_num_popups = 0;
+static int g_UiNumPopups = 0;
 
-void EDITOR::ui_invoke_popup_menu(void *id, int flags, float x, float y, float w, float h, int (*func)(EDITOR *pEditor, CUIRect rect), void *extra)
+void CEditor::UiInvokePopupMenu(void *Id, int Flags, float x, float y, float w, float h, int (*pfnFunc)(CEditor *pEditor, CUIRect Rect), void *pExtra)
 {
 	dbg_msg("", "invoked");
-	ui_popups[ui_num_popups].id = id;
-	ui_popups[ui_num_popups].is_menu = flags;
-	ui_popups[ui_num_popups].rect.x = x;
-	ui_popups[ui_num_popups].rect.y = y;
-	ui_popups[ui_num_popups].rect.w = w;
-	ui_popups[ui_num_popups].rect.h = h;
-	ui_popups[ui_num_popups].func = func;
-	ui_popups[ui_num_popups].extra = extra;
-	ui_num_popups++;
+	s_UiPopups[g_UiNumPopups].m_pId = Id;
+	s_UiPopups[g_UiNumPopups].m_IsMenu = Flags;
+	s_UiPopups[g_UiNumPopups].m_Rect.x = x;
+	s_UiPopups[g_UiNumPopups].m_Rect.y = y;
+	s_UiPopups[g_UiNumPopups].m_Rect.w = w;
+	s_UiPopups[g_UiNumPopups].m_Rect.h = h;
+	s_UiPopups[g_UiNumPopups].m_pfnFunc = pfnFunc;
+	s_UiPopups[g_UiNumPopups].m_pExtra = pExtra;
+	g_UiNumPopups++;
 }
 
-void EDITOR::ui_do_popup_menu()
+void CEditor::UiDoPopupMenu()
 {
-	for(int i = 0; i < ui_num_popups; i++)
+	for(int i = 0; i < g_UiNumPopups; i++)
 	{
-		bool inside = UI()->MouseInside(&ui_popups[i].rect);
-		UI()->SetHotItem(&ui_popups[i].id);
+		bool Inside = UI()->MouseInside(&s_UiPopups[i].m_Rect);
+		UI()->SetHotItem(&s_UiPopups[i].m_pId);
 		
-		if(UI()->ActiveItem() == &ui_popups[i].id)
+		if(UI()->ActiveItem() == &s_UiPopups[i].m_pId)
 		{
 			if(!UI()->MouseButton(0))
 			{
-				if(!inside)
-					ui_num_popups--;
+				if(!Inside)
+					g_UiNumPopups--;
 				UI()->SetActiveItem(0);
 			}
 		}
-		else if(UI()->HotItem() == &ui_popups[i].id)
+		else if(UI()->HotItem() == &s_UiPopups[i].m_pId)
 		{
 			if(UI()->MouseButton(0))
-				UI()->SetActiveItem(&ui_popups[i].id);
+				UI()->SetActiveItem(&s_UiPopups[i].m_pId);
 		}
 		
-		int corners = CUI::CORNER_ALL;
-		if(ui_popups[i].is_menu)
-			corners = CUI::CORNER_R|CUI::CORNER_B;
+		int Corners = CUI::CORNER_ALL;
+		if(s_UiPopups[i].m_IsMenu)
+			Corners = CUI::CORNER_R|CUI::CORNER_B;
 		
-		CUIRect r = ui_popups[i].rect;
-		RenderTools()->DrawUIRect(&r, vec4(0.5f,0.5f,0.5f,0.75f), corners, 3.0f);
+		CUIRect r = s_UiPopups[i].m_Rect;
+		RenderTools()->DrawUIRect(&r, vec4(0.5f,0.5f,0.5f,0.75f), Corners, 3.0f);
 		r.Margin(1.0f, &r);
-		RenderTools()->DrawUIRect(&r, vec4(0,0,0,0.75f), corners, 3.0f);
+		RenderTools()->DrawUIRect(&r, vec4(0,0,0,0.75f), Corners, 3.0f);
 		r.Margin(4.0f, &r);
 		
-		if(ui_popups[i].func(this, r))
-			ui_num_popups--;
+		if(s_UiPopups[i].m_pfnFunc(this, r))
+			g_UiNumPopups--;
 			
-		if(inp_key_down(KEY_ESCAPE))
-			ui_num_popups--;
+		if(Input()->KeyDown(KEY_ESCAPE))
+			g_UiNumPopups--;
 	}
 }
 
 
-int EDITOR::popup_group(EDITOR *pEditor, CUIRect view)
+int CEditor::PopupGroup(CEditor *pEditor, CUIRect View)
 {
 	// remove group button
-	CUIRect button;
-	view.HSplitBottom(12.0f, &view, &button);
-	static int delete_button = 0;
+	CUIRect Button;
+	View.HSplitBottom(12.0f, &View, &Button);
+	static int s_DeleteButton = 0;
 	
 	// don't allow deletion of game group
-	if(pEditor->map.game_group != pEditor->get_selected_group() &&
-		pEditor->DoButton_Editor(&delete_button, "Delete Group", 0, &button, 0, "Delete group"))
+	if(pEditor->m_Map.m_pGameGroup != pEditor->GetSelectedGroup() &&
+		pEditor->DoButton_Editor(&s_DeleteButton, "Delete Group", 0, &Button, 0, "Delete group"))
 	{
-		pEditor->map.delete_group(pEditor->selected_group);
+		pEditor->m_Map.DeleteGroup(pEditor->m_SelectedGroup);
 		return 1;
 	}
 
 	// new tile layer
-	view.HSplitBottom(10.0f, &view, &button);
-	view.HSplitBottom(12.0f, &view, &button);
-	static int new_quad_layer_button = 0;
-	if(pEditor->DoButton_Editor(&new_quad_layer_button, "Add Quads Layer", 0, &button, 0, "Creates a new quad layer"))
+	View.HSplitBottom(10.0f, &View, &Button);
+	View.HSplitBottom(12.0f, &View, &Button);
+	static int s_NewQuadLayerButton = 0;
+	if(pEditor->DoButton_Editor(&s_NewQuadLayerButton, "Add Quads Layer", 0, &Button, 0, "Creates a new quad layer"))
 	{
-		LAYER *l = new LAYER_QUADS;
-		pEditor->map.groups[pEditor->selected_group]->add_layer(l);
-		pEditor->selected_layer = pEditor->map.groups[pEditor->selected_group]->layers.len()-1;
+		CLayer *l = new CLayerQuads;
+		l->m_pEditor = pEditor;
+		pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->AddLayer(l);
+		pEditor->m_SelectedLayer = pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_lLayers.size()-1;
 		return 1;
 	}
 
 	// new quad layer
-	view.HSplitBottom(5.0f, &view, &button);
-	view.HSplitBottom(12.0f, &view, &button);
-	static int new_tile_layer_button = 0;
-	if(pEditor->DoButton_Editor(&new_tile_layer_button, "Add Tile Layer", 0, &button, 0, "Creates a new tile layer"))
+	View.HSplitBottom(5.0f, &View, &Button);
+	View.HSplitBottom(12.0f, &View, &Button);
+	static int s_NewTileLayerButton = 0;
+	if(pEditor->DoButton_Editor(&s_NewTileLayerButton, "Add Tile Layer", 0, &Button, 0, "Creates a new tile layer"))
 	{
-		LAYER *l = new LAYER_TILES(50, 50);
-		pEditor->map.groups[pEditor->selected_group]->add_layer(l);
-		pEditor->selected_layer = pEditor->map.groups[pEditor->selected_group]->layers.len()-1;
+		CLayer *l = new CLayerTiles(50, 50);
+		l->m_pEditor = pEditor;
+		pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->AddLayer(l);
+		pEditor->m_SelectedLayer = pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_lLayers.size()-1;
 		return 1;
 	}
 	
@@ -124,68 +127,68 @@ int EDITOR::popup_group(EDITOR *pEditor, CUIRect view)
 		NUM_PROPS,
 	};
 	
-	PROPERTY props[] = {
-		{"Order", pEditor->selected_group, PROPTYPE_INT_STEP, 0, pEditor->map.groups.len()-1},
-		{"Pos X", -pEditor->map.groups[pEditor->selected_group]->offset_x, PROPTYPE_INT_SCROLL, -1000000, 1000000},
-		{"Pos Y", -pEditor->map.groups[pEditor->selected_group]->offset_y, PROPTYPE_INT_SCROLL, -1000000, 1000000},
-		{"Para X", pEditor->map.groups[pEditor->selected_group]->parallax_x, PROPTYPE_INT_SCROLL, -1000000, 1000000},
-		{"Para Y", pEditor->map.groups[pEditor->selected_group]->parallax_y, PROPTYPE_INT_SCROLL, -1000000, 1000000},
-
-		{"Use Clipping", pEditor->map.groups[pEditor->selected_group]->use_clipping, PROPTYPE_BOOL, 0, 1},
-		{"Clip X", pEditor->map.groups[pEditor->selected_group]->clip_x, PROPTYPE_INT_SCROLL, -1000000, 1000000},
-		{"Clip Y", pEditor->map.groups[pEditor->selected_group]->clip_y, PROPTYPE_INT_SCROLL, -1000000, 1000000},
-		{"Clip W", pEditor->map.groups[pEditor->selected_group]->clip_w, PROPTYPE_INT_SCROLL, -1000000, 1000000},
-		{"Clip H", pEditor->map.groups[pEditor->selected_group]->clip_h, PROPTYPE_INT_SCROLL, -1000000, 1000000},
+	CProperty aProps[] = {
+		{"Order", pEditor->m_SelectedGroup, PROPTYPE_INT_STEP, 0, pEditor->m_Map.m_lGroups.size()-1},
+		{"Pos X", -pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_OffsetX, PROPTYPE_INT_SCROLL, -1000000, 1000000},
+		{"Pos Y", -pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_OffsetY, PROPTYPE_INT_SCROLL, -1000000, 1000000},
+		{"Para X", pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ParallaxX, PROPTYPE_INT_SCROLL, -1000000, 1000000},
+		{"Para Y", pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ParallaxY, PROPTYPE_INT_SCROLL, -1000000, 1000000},
+
+		{"Use Clipping", pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_UseClipping, PROPTYPE_BOOL, 0, 1},
+		{"Clip X", pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ClipX, PROPTYPE_INT_SCROLL, -1000000, 1000000},
+		{"Clip Y", pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ClipY, PROPTYPE_INT_SCROLL, -1000000, 1000000},
+		{"Clip W", pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ClipW, PROPTYPE_INT_SCROLL, -1000000, 1000000},
+		{"Clip H", pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ClipH, PROPTYPE_INT_SCROLL, -1000000, 1000000},
 		{0},
 	};
 	
-	static int ids[NUM_PROPS] = {0};
-	int new_val = 0;
+	static int s_aIds[NUM_PROPS] = {0};
+	int NewVal = 0;
 	
 	// cut the properties that isn't needed
-	if(pEditor->get_selected_group()->game_group)
-		props[PROP_POS_X].name = 0;
+	if(pEditor->GetSelectedGroup()->m_GameGroup)
+		aProps[PROP_POS_X].m_pName = 0;
 		
-	int prop = pEditor->do_properties(&view, props, ids, &new_val);
-	if(prop == PROP_ORDER)
-		pEditor->selected_group = pEditor->map.swap_groups(pEditor->selected_group, new_val);
+	int Prop = pEditor->DoProperties(&View, aProps, s_aIds, &NewVal);
+	if(Prop == PROP_ORDER)
+		pEditor->m_SelectedGroup = pEditor->m_Map.SwapGroups(pEditor->m_SelectedGroup, NewVal);
 		
 	// these can not be changed on the game group
-	if(!pEditor->get_selected_group()->game_group)
+	if(!pEditor->GetSelectedGroup()->m_GameGroup)
 	{
-		if(prop == PROP_PARA_X) pEditor->map.groups[pEditor->selected_group]->parallax_x = new_val;
-		else if(prop == PROP_PARA_Y) pEditor->map.groups[pEditor->selected_group]->parallax_y = new_val;
-		else if(prop == PROP_POS_X) pEditor->map.groups[pEditor->selected_group]->offset_x = -new_val;
-		else if(prop == PROP_POS_Y) pEditor->map.groups[pEditor->selected_group]->offset_y = -new_val;
-		else if(prop == PROP_USE_CLIPPING) pEditor->map.groups[pEditor->selected_group]->use_clipping = new_val;
-		else if(prop == PROP_CLIP_X) pEditor->map.groups[pEditor->selected_group]->clip_x = new_val;
-		else if(prop == PROP_CLIP_Y) pEditor->map.groups[pEditor->selected_group]->clip_y = new_val;
-		else if(prop == PROP_CLIP_W) pEditor->map.groups[pEditor->selected_group]->clip_w = new_val;
-		else if(prop == PROP_CLIP_H) pEditor->map.groups[pEditor->selected_group]->clip_h = new_val;
+		if(Prop == PROP_PARA_X) pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ParallaxX = NewVal;
+		else if(Prop == PROP_PARA_Y) pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ParallaxY = NewVal;
+		else if(Prop == PROP_POS_X) pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_OffsetX = -NewVal;
+		else if(Prop == PROP_POS_Y) pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_OffsetY = -NewVal;
+		else if(Prop == PROP_USE_CLIPPING) pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_UseClipping = NewVal;
+		else if(Prop == PROP_CLIP_X) pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ClipX = NewVal;
+		else if(Prop == PROP_CLIP_Y) pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ClipY = NewVal;
+		else if(Prop == PROP_CLIP_W) pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ClipW = NewVal;
+		else if(Prop == PROP_CLIP_H) pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->m_ClipH = NewVal;
 	}
 	
 	return 0;
 }
 
-int EDITOR::popup_layer(EDITOR *pEditor, CUIRect view)
+int CEditor::PopupLayer(CEditor *pEditor, CUIRect View)
 {
 	// remove layer button
-	CUIRect button;
-	view.HSplitBottom(12.0f, &view, &button);
-	static int delete_button = 0;
+	CUIRect Button;
+	View.HSplitBottom(12.0f, &View, &Button);
+	static int s_DeleteButton = 0;
 	
 	// don't allow deletion of game layer
-	if(pEditor->map.game_layer != pEditor->get_selected_layer(0) &&
-		pEditor->DoButton_Editor(&delete_button, "Delete Layer", 0, &button, 0, "Deletes the layer"))
+	if(pEditor->m_Map.m_pGameLayer != pEditor->GetSelectedLayer(0) &&
+		pEditor->DoButton_Editor(&s_DeleteButton, "Delete Layer", 0, &Button, 0, "Deletes the layer"))
 	{
-		pEditor->map.groups[pEditor->selected_group]->delete_layer(pEditor->selected_layer);
+		pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup]->DeleteLayer(pEditor->m_SelectedLayer);
 		return 1;
 	}
 
-	view.HSplitBottom(10.0f, &view, 0);
+	View.HSplitBottom(10.0f, &View, 0);
 	
-	LAYERGROUP *current_group = pEditor->map.groups[pEditor->selected_group];
-	LAYER *current_layer = pEditor->get_selected_layer(0);
+	CLayerGroup *pCurrentGroup = pEditor->m_Map.m_lGroups[pEditor->m_SelectedGroup];
+	CLayer *pCurrentLayer = pEditor->GetSelectedLayer(0);
 	
 	enum
 	{
@@ -195,82 +198,88 @@ int EDITOR::popup_layer(EDITOR *pEditor, CUIRect view)
 		NUM_PROPS,
 	};
 	
-	PROPERTY props[] = {
-		{"Group", pEditor->selected_group, PROPTYPE_INT_STEP, 0, pEditor->map.groups.len()-1},
-		{"Order", pEditor->selected_layer, PROPTYPE_INT_STEP, 0, current_group->layers.len()},
-		{"Detail", current_layer->flags&LAYERFLAG_DETAIL, PROPTYPE_BOOL, 0, 1},
+	CProperty aProps[] = {
+		{"Group", pEditor->m_SelectedGroup, PROPTYPE_INT_STEP, 0, pEditor->m_Map.m_lGroups.size()-1},
+		{"Order", pEditor->m_SelectedLayer, PROPTYPE_INT_STEP, 0, pCurrentGroup->m_lLayers.size()},
+		{"Detail", pCurrentLayer->m_Flags&LAYERFLAG_DETAIL, PROPTYPE_BOOL, 0, 1},
 		{0},
 	};
+
+	if(pEditor->m_Map.m_pGameLayer == pEditor->GetSelectedLayer(0)) // dont use Group and Detail from the selection if this is the game layer
+	{
+		aProps[0].m_Type = PROPTYPE_NULL;
+		aProps[2].m_Type = PROPTYPE_NULL;
+	}
 	
-	static int ids[NUM_PROPS] = {0};
-	int new_val = 0;
-	int prop = pEditor->do_properties(&view, props, ids, &new_val);		
+	static int s_aIds[NUM_PROPS] = {0};
+	int NewVal = 0;
+	int Prop = pEditor->DoProperties(&View, aProps, s_aIds, &NewVal);		
 	
-	if(prop == PROP_ORDER)
-		pEditor->selected_layer = current_group->swap_layers(pEditor->selected_layer, new_val);
-	else if(prop == PROP_GROUP && current_layer->type != LAYERTYPE_GAME)
+	if(Prop == PROP_ORDER)
+		pEditor->m_SelectedLayer = pCurrentGroup->SwapLayers(pEditor->m_SelectedLayer, NewVal);
+	else if(Prop == PROP_GROUP && pCurrentLayer->m_Type != LAYERTYPE_GAME)
 	{
-		if(new_val >= 0 && new_val < pEditor->map.groups.len())
+		if(NewVal >= 0 && NewVal < pEditor->m_Map.m_lGroups.size())
 		{
-			current_group->layers.remove(current_layer);
-			pEditor->map.groups[new_val]->layers.add(current_layer);
-			pEditor->selected_group = new_val;
-			pEditor->selected_layer = pEditor->map.groups[new_val]->layers.len()-1;
+			pCurrentGroup->m_lLayers.remove(pCurrentLayer);
+			pEditor->m_Map.m_lGroups[NewVal]->m_lLayers.add(pCurrentLayer);
+			pEditor->m_SelectedGroup = NewVal;
+			pEditor->m_SelectedLayer = pEditor->m_Map.m_lGroups[NewVal]->m_lLayers.size()-1;
 		}
 	}
-	else if(prop == PROP_HQ)
+	else if(Prop == PROP_HQ)
 	{
-		current_layer->flags &= ~LAYERFLAG_DETAIL;
-		if(new_val)
-			current_layer->flags |= LAYERFLAG_DETAIL;
+		pCurrentLayer->m_Flags &= ~LAYERFLAG_DETAIL;
+		if(NewVal)
+			pCurrentLayer->m_Flags |= LAYERFLAG_DETAIL;
 	}
 		
-	return current_layer->render_properties(&view);
+	return pCurrentLayer->RenderProperties(&View);
 }
 
-int EDITOR::popup_quad(EDITOR *pEditor, CUIRect view)
+int CEditor::PopupQuad(CEditor *pEditor, CUIRect View)
 {
-	QUAD *quad = pEditor->get_selected_quad();
+	CQuad *pQuad = pEditor->GetSelectedQuad();
 
-	CUIRect button;
+	CUIRect Button;
 	
 	// delete button
-	view.HSplitBottom(12.0f, &view, &button);
-	static int delete_button = 0;
-	if(pEditor->DoButton_Editor(&delete_button, "Delete", 0, &button, 0, "Deletes the current quad"))
+	View.HSplitBottom(12.0f, &View, &Button);
+	static int s_DeleteButton = 0;
+	if(pEditor->DoButton_Editor(&s_DeleteButton, "Delete", 0, &Button, 0, "Deletes the current quad"))
 	{
-		LAYER_QUADS *layer = (LAYER_QUADS *)pEditor->get_selected_layer_type(0, LAYERTYPE_QUADS);
-		if(layer)
+		CLayerQuads *pLayer = (CLayerQuads *)pEditor->GetSelectedLayerType(0, LAYERTYPE_QUADS);
+		if(pLayer)
 		{
-			layer->quads.removebyindex(pEditor->selected_quad);
-			pEditor->selected_quad--;
+			pLayer->m_lQuads.remove_index(pEditor->m_SelectedQuad);
+			pEditor->m_SelectedQuad--;
 		}
 		return 1;
 	}
 
 	// square button
-	view.HSplitBottom(10.0f, &view, &button);
-	view.HSplitBottom(12.0f, &view, &button);
-	static int sq_button = 0;
-	if(pEditor->DoButton_Editor(&sq_button, "Square", 0, &button, 0, "Squares the current quad"))
+	View.HSplitBottom(10.0f, &View, &Button);
+	View.HSplitBottom(12.0f, &View, &Button);
+	static int s_Button = 0;
+	if(pEditor->DoButton_Editor(&s_Button, "Square", 0, &Button, 0, "Squares the current quad"))
 	{
-		int top = quad->points[0].y;
-		int left = quad->points[0].x;
-		int bottom = quad->points[0].y;
-		int right = quad->points[0].x;
+		int Top = pQuad->m_aPoints[0].y;
+		int Left = pQuad->m_aPoints[0].x;
+		int Bottom = pQuad->m_aPoints[0].y;
+		int Right = pQuad->m_aPoints[0].x;
 		
 		for(int k = 1; k < 4; k++)
 		{
-			if(quad->points[k].y < top) top = quad->points[k].y;
-			if(quad->points[k].x < left) left = quad->points[k].x;
-			if(quad->points[k].y > bottom) bottom = quad->points[k].y;
-			if(quad->points[k].x > right) right = quad->points[k].x;
+			if(pQuad->m_aPoints[k].y < Top) Top = pQuad->m_aPoints[k].y;
+			if(pQuad->m_aPoints[k].x < Left) Left = pQuad->m_aPoints[k].x;
+			if(pQuad->m_aPoints[k].y > Bottom) Bottom = pQuad->m_aPoints[k].y;
+			if(pQuad->m_aPoints[k].x > Right) Right = pQuad->m_aPoints[k].x;
 		}
 		
-		quad->points[0].x = left; quad->points[0].y = top;
-		quad->points[1].x = right; quad->points[1].y = top;
-		quad->points[2].x = left; quad->points[2].y = bottom;
-		quad->points[3].x = right; quad->points[3].y = bottom;
+		pQuad->m_aPoints[0].x = Left; pQuad->m_aPoints[0].y = Top;
+		pQuad->m_aPoints[1].x = Right; pQuad->m_aPoints[1].y = Top;
+		pQuad->m_aPoints[2].x = Left; pQuad->m_aPoints[2].y = Bottom;
+		pQuad->m_aPoints[3].x = Right; pQuad->m_aPoints[3].y = Bottom;
 		return 1;
 	}
 
@@ -284,30 +293,30 @@ int EDITOR::popup_quad(EDITOR *pEditor, CUIRect view)
 		NUM_PROPS,
 	};
 	
-	PROPERTY props[] = {
-		{"Pos. Env", quad->pos_env, PROPTYPE_INT_STEP, -1, pEditor->map.envelopes.len()},
-		{"Pos. TO", quad->pos_env_offset, PROPTYPE_INT_SCROLL, -1000000, 1000000},
-		{"Color Env", quad->color_env, PROPTYPE_INT_STEP, -1, pEditor->map.envelopes.len()},
-		{"Color TO", quad->color_env_offset, PROPTYPE_INT_SCROLL, -1000000, 1000000},
+	CProperty aProps[] = {
+		{"Pos. Env", pQuad->m_PosEnv, PROPTYPE_INT_STEP, -1, pEditor->m_Map.m_lEnvelopes.size()},
+		{"Pos. TO", pQuad->m_PosEnvOffset, PROPTYPE_INT_SCROLL, -1000000, 1000000},
+		{"Color Env", pQuad->m_ColorEnv, PROPTYPE_INT_STEP, -1, pEditor->m_Map.m_lEnvelopes.size()},
+		{"Color TO", pQuad->m_ColorEnvOffset, PROPTYPE_INT_SCROLL, -1000000, 1000000},
 		
 		{0},
 	};
 	
-	static int ids[NUM_PROPS] = {0};
-	int new_val = 0;
-	int prop = pEditor->do_properties(&view, props, ids, &new_val);		
+	static int s_aIds[NUM_PROPS] = {0};
+	int NewVal = 0;
+	int Prop = pEditor->DoProperties(&View, aProps, s_aIds, &NewVal);		
 	
-	if(prop == PROP_POS_ENV) quad->pos_env = clamp(new_val, -1, pEditor->map.envelopes.len()-1);
-	if(prop == PROP_POS_ENV_OFFSET) quad->pos_env_offset = new_val;
-	if(prop == PROP_COLOR_ENV) quad->color_env = clamp(new_val, -1, pEditor->map.envelopes.len()-1);
-	if(prop == PROP_COLOR_ENV_OFFSET) quad->color_env_offset = new_val;
+	if(Prop == PROP_POS_ENV) pQuad->m_PosEnv = clamp(NewVal, -1, pEditor->m_Map.m_lEnvelopes.size()-1);
+	if(Prop == PROP_POS_ENV_OFFSET) pQuad->m_PosEnvOffset = NewVal;
+	if(Prop == PROP_COLOR_ENV) pQuad->m_ColorEnv = clamp(NewVal, -1, pEditor->m_Map.m_lEnvelopes.size()-1);
+	if(Prop == PROP_COLOR_ENV_OFFSET) pQuad->m_ColorEnvOffset = NewVal;
 	
 	return 0;
 }
 
-int EDITOR::popup_point(EDITOR *pEditor, CUIRect view)
+int CEditor::PopupPoint(CEditor *pEditor, CUIRect View)
 {
-	QUAD *quad = pEditor->get_selected_quad();
+	CQuad *pQuad = pEditor->GetSelectedQuad();
 	
 	enum
 	{
@@ -315,40 +324,40 @@ int EDITOR::popup_point(EDITOR *pEditor, CUIRect view)
 		NUM_PROPS,
 	};
 	
-	int color = 0;
+	int Color = 0;
 
 	for(int v = 0; v < 4; v++)
 	{
-		if(pEditor->selected_points&(1<<v))
+		if(pEditor->m_SelectedPoints&(1<<v))
 		{
-			color = 0;
-			color |= quad->colors[v].r<<24;
-			color |= quad->colors[v].g<<16;
-			color |= quad->colors[v].b<<8;
-			color |= quad->colors[v].a;
+			Color = 0;
+			Color |= pQuad->m_aColors[v].r<<24;
+			Color |= pQuad->m_aColors[v].g<<16;
+			Color |= pQuad->m_aColors[v].b<<8;
+			Color |= pQuad->m_aColors[v].a;
 		}
 	}
 	
 	
-	PROPERTY props[] = {
-		{"Color", color, PROPTYPE_COLOR, -1, pEditor->map.envelopes.len()},
+	CProperty aProps[] = {
+		{"Color", Color, PROPTYPE_COLOR, -1, pEditor->m_Map.m_lEnvelopes.size()},
 		{0},
 	};
 	
-	static int ids[NUM_PROPS] = {0};
-	int new_val = 0;
-	int prop = pEditor->do_properties(&view, props, ids, &new_val);		
-	if(prop == PROP_COLOR)
+	static int s_aIds[NUM_PROPS] = {0};
+	int NewVal = 0;
+	int Prop = pEditor->DoProperties(&View, aProps, s_aIds, &NewVal);		
+	if(Prop == PROP_COLOR)
 	{
 		for(int v = 0; v < 4; v++)
 		{
-			if(pEditor->selected_points&(1<<v))
+			if(pEditor->m_SelectedPoints&(1<<v))
 			{
-				color = 0;
-				quad->colors[v].r = (new_val>>24)&0xff;
-				quad->colors[v].g = (new_val>>16)&0xff;
-				quad->colors[v].b = (new_val>>8)&0xff;
-				quad->colors[v].a = new_val&0xff;
+				Color = 0;
+				pQuad->m_aColors[v].r = (NewVal>>24)&0xff;
+				pQuad->m_aColors[v].g = (NewVal>>16)&0xff;
+				pQuad->m_aColors[v].b = (NewVal>>8)&0xff;
+				pQuad->m_aColors[v].a = NewVal&0xff;
 			}
 		}
 	}
@@ -358,65 +367,66 @@ int EDITOR::popup_point(EDITOR *pEditor, CUIRect view)
 
 
 
-static int select_image_selected = -100;
-static int select_image_current = -100;
+static int g_SelectImageSelected = -100;
+static int g_SelectImageCurrent = -100;
 
-int EDITOR::popup_select_image(EDITOR *pEditor, CUIRect view)
+int CEditor::PopupSelectImage(CEditor *pEditor, CUIRect View)
 {
-	CUIRect buttonbar, imageview;
-	view.VSplitLeft(80.0f, &buttonbar, &view);
-	view.Margin(10.0f, &imageview);
+	CUIRect ButtonBar, ImageView;
+	View.VSplitLeft(80.0f, &ButtonBar, &View);
+	View.Margin(10.0f, &ImageView);
 	
-	int show_image = select_image_current;
+	int ShowImage = g_SelectImageCurrent;
 	
-	for(int i = -1; i < pEditor->map.images.len(); i++)
+	for(int i = -1; i < pEditor->m_Map.m_lImages.size(); i++)
 	{
-		CUIRect button;
-		buttonbar.HSplitTop(12.0f, &button, &buttonbar);
-		buttonbar.HSplitTop(2.0f, 0, &buttonbar);
+		CUIRect Button;
+		ButtonBar.HSplitTop(12.0f, &Button, &ButtonBar);
+		ButtonBar.HSplitTop(2.0f, 0, &ButtonBar);
 		
-		if(pEditor->UI()->MouseInside(&button))
-			show_image = i;
+		if(pEditor->UI()->MouseInside(&Button))
+			ShowImage = i;
 			
 		if(i == -1)
 		{
-			if(pEditor->DoButton_MenuItem(&pEditor->map.images[i], "None", i==select_image_current, &button))
-				select_image_selected = -1;
+			if(pEditor->DoButton_MenuItem(&pEditor->m_Map.m_lImages[i], "None", i==g_SelectImageCurrent, &Button))
+				g_SelectImageSelected = -1;
 		}
 		else
 		{
-			if(pEditor->DoButton_MenuItem(&pEditor->map.images[i], pEditor->map.images[i]->name, i==select_image_current, &button))
-				select_image_selected = i;
+			if(pEditor->DoButton_MenuItem(&pEditor->m_Map.m_lImages[i], pEditor->m_Map.m_lImages[i]->m_aName, i==g_SelectImageCurrent, &Button))
+				g_SelectImageSelected = i;
 		}
 	}
 	
-	if(show_image >= 0 && show_image < pEditor->map.images.len())
-		pEditor->Graphics()->TextureSet(pEditor->map.images[show_image]->tex_id);
+	if(ShowImage >= 0 && ShowImage < pEditor->m_Map.m_lImages.size())
+		pEditor->Graphics()->TextureSet(pEditor->m_Map.m_lImages[ShowImage]->m_TexId);
 	else
 		pEditor->Graphics()->TextureSet(-1);
 	pEditor->Graphics()->QuadsBegin();
-	pEditor->Graphics()->QuadsDrawTL(imageview.x, imageview.y, imageview.w, imageview.h);
+	IGraphics::CQuadItem QuadItem(ImageView.x, ImageView.y, ImageView.w, ImageView.h);
+	pEditor->Graphics()->QuadsDrawTL(&QuadItem, 1);
 	pEditor->Graphics()->QuadsEnd();
 
 	return 0;
 }
 
-void EDITOR::popup_select_image_invoke(int current, float x, float y)
+void CEditor::PopupSelectImageInvoke(int Current, float x, float y)
 {
-	static int select_image_popup_id = 0;
-	select_image_selected = -100;
-	select_image_current = current;
-	ui_invoke_popup_menu(&select_image_popup_id, 0, x, y, 400, 300, popup_select_image);
+	static int s_SelectImagePopupId = 0;
+	g_SelectImageSelected = -100;
+	g_SelectImageCurrent = Current;
+	UiInvokePopupMenu(&s_SelectImagePopupId, 0, x, y, 400, 300, PopupSelectImage);
 }
 
-int EDITOR::popup_select_image_result()
+int CEditor::PopupSelectImageResult()
 {
-	if(select_image_selected == -100)
+	if(g_SelectImageSelected == -100)
 		return -100;
 		
-	select_image_current = select_image_selected;
-	select_image_selected = -100;
-	return select_image_current;
+	g_SelectImageCurrent = g_SelectImageSelected;
+	g_SelectImageSelected = -100;
+	return g_SelectImageCurrent;
 }