/**
 * @file Obj.h
 * @brief スプライトの表示属性関連の実装を行う。
 * 
 * @author S.F.
 * @version $Id:
 *
 * Copyright (C) 2000-2002 Satoshi Fujiwara. All Rights Reserved.
 */


// メモリーリーク検出用
#ifdef _DEBUG
#ifndef _BORLAND
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#endif

#include <string>
#include "stdio.h"

#include "system.h"
#include "console.h"

#include "Obj.h"

//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////

using namespace sf::system::console;

int Obj::List::mCount = 0;
Obj* Obj::List::mspBottom = NULL;
Obj* Obj::List::mspTop = NULL;
Obj* Obj::List::mspCurrent = NULL;

//----------------------------------------------------------------------------
// Obj Class
//----------------------------------------------------------------------------

// コンストラクタ -------------------------------------------------------------
Obj::Obj()
{

}// Obj

// デストラクタ ---------------------------------------------------------------
Obj::~Obj()
{

}// Obj

//----------------------------------------------------------------------------
// Obj::List Class
//----------------------------------------------------------------------------

// ObjをZ値でソートしながら挿入する --------------------------------------------
void Obj::List::insertObj(Obj *value)
{
	Obj *pobj = mspTop;
	if(mspTop == NULL){
		mspTop = value;
		mspCurrent = value;
		mspBottom = value;
		value->next(NULL);
		value->previous(NULL);
		mCount++;
	} else {
		while(pobj != NULL)
		{
			if(value->z() <= pobj->z())
			{
				pobj = pobj->next();
				
			} else {
	
				value->next(pobj);
				
				if(pobj->previous() != NULL){
					value->previous(pobj->previous());
					value->previous()->next(value);
					pobj->previous(value);
				} else {
					value->previous(NULL);
					mspTop = value; 
					pobj->previous(value);
				}

				mCount++;
				return;
			}

		}

		mspBottom->next(value);
		value->previous(mspBottom);
		value->next(NULL);
		mspBottom = value;
		mCount++;
	}
}// insertObj()

// Objを削除する --------------------------------------------------------------
void Obj::List::deleteObj(Obj *value)
{
	if(value->next() != NULL)
	{
		if(value->previous() != NULL)
		{
			value->previous()->next(value->next());
			value->next()->previous(value->previous());
			value->next(NULL);
			value->previous(NULL);
		} else {
			mspTop = value->next();
			value->next()->previous(NULL);
			value->next(NULL);
		}
	} else {
		if(value->previous() != NULL){
			mspBottom = value->previous();
			value->previous()->next(NULL);
			value->previous(NULL);
		} else {
			value->next(NULL);
			value->previous(NULL);
			mspBottom = NULL;
			mspTop = NULL;
		}
	}
	mCount --;
} // deleteObj()

Obj * Obj::List::moveNext()
{
	if(mspCurrent != NULL)
		mspCurrent = mspCurrent->next();
	return mspCurrent;
};

Obj * Obj::List::movePrevious()
{	if(mspCurrent != NULL)
		mspCurrent = mspCurrent->previous();
	return mspCurrent;
};

Obj * Obj::List::moveTop(){mspCurrent = mspTop ; return mspTop ;};
Obj * Obj::List::moveBottom(){mspCurrent = mspBottom ; return  mspBottom;};
