/***********************************************************************
*Copyright (c)2012 , 天际边工作室
*All rights reserved.
**
文件名称：	LibOperator.cpp
*摘要：		游戏引擎类，开始游戏界面
**
*修改历史：
*	版本    日期     作者     改动内容和原因
*   ----------------------------------------------------
*	1.0    2012-10-5  wstjb     基本的功能完成
***********************************************************************/

#include "LibOperator.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>



extern  PAINTSTRUCT ps;
extern 	HDC hdc;
extern 	HBITMAP hbmp; 
extern 	BITMAP bmp;
extern 	HBITMAP hbmp2; 
extern 	HDC hdcMem;
extern 	HDC hdcTmp;
extern 	RECT rect;
extern 	HWND hwnd;
extern int flag;
extern int tasknum;

extern int KeyReturn;
extern int KeyEsc;
extern int KeyHome;
extern int KeySpace;
extern int KeyLeft0;
extern int KeyUp0;
extern int KeyRight0;
extern int KeyDown0;

extern byte libByte[2097152];
extern byte resByte[2731][16384];//存储lib资源
extern int resByteLen[2731];
extern int resPos[2731][6];//存储lib地址和编号 第六位为长度
extern int resCount;

//extern SIZE size;
//extern HDC hdcPaint;

void LibOperator::Init()
{
	memset(libByte,0,sizeof(libByte));
	memset(tmpByte,0,sizeof(tmpByte));
	memset(resByte,0,sizeof(resByte));
}

void LibOperator::KeyDown(WPARAM KeyCode)
{


}

void LibOperator::GetAllLibResToRes()
{

	/*for (int i = 1; i < 14; i++)
	{
		for(int j = 0;j < 256;j++)
		{
			for(int  k = 0 ;k < 256;k++)
			{*/
				for (int ii = 0; ii < resCount / 3; ii++)//原有
				{
					//if (resPos[ii][0] == i && resPos[ii][1] == j && resPos[ii][2] == k)
					{
						//将数据插入newlibbyte 包含编号段 地址段 资源段
						//获取数据到tmpbuf
						GetResFromLib(resPos[ii][0],resPos[ii][1],resPos[ii][2]);
						for(int jj = 0 ;jj<tmpByteLen;jj++)
						{
							resByte[ii][jj] = tmpByte[jj];
						}
						resByteLen[ii] = tmpByteLen;
					}
				}
			
}

void LibOperator::GetData()
{
	//lib 用于从lib中获取资源编号到respos
	

	resCount  = BytesToInt2(&libByte[12]);

	memcpy(libName,libByte+3,8);

	wlogfile(1, __FUNCTION__ ,"-----------------------------------------------",0);
	wlogfile(1, __FUNCTION__ ,libName,0);
	int j = 0;
	for(int i = 16;i<resCount+16;i+=3)
	{
		//获取资源编号区数据
		resPos[j][0] = BytesToInt1(&libByte[i]);
		resPos[j][1] = BytesToInt1(&libByte[i+1]);
		resPos[j][2] = BytesToInt1(&libByte[i+2]);
		//获取资源位移区数据
		resPos[j][3] = BytesToInt1(&libByte[i + 8176]);
		resPos[j][4] = BytesToInt2(&libByte[i + 8177]);
		j++;
	}
	GetAllLibResToRes();
}

void LibOperator::GetResFromLib(int type,int restype,int index)
{
	int i = 0;
	for (i = 0; i < resCount / 3; i++)
	{
		if (resPos[i][0] == type && resPos[i][1] == restype && resPos[i][2] == index)
		{
			segment = resPos[i][3];
			offset = resPos[i][4];
			break;
		}
	}
	GetResLen(type,restype,index);
	for (int ii = 0; ii < resPos[i][5]; ii++)
	{
		tmpByte[ii] = libByte[segment * 16384 + offset + ii];
	}
	tmpByteLen = resPos[i][5];
}

void LibOperator::GetResLen(int type,int restype,int index)
{
	int i = 0;
	for (i = 0; i < resCount / 3; i++)
	{
		if (resPos[i][0] == type && resPos[i][1] == restype && resPos[i][2] == index)
		{
			segment = resPos[i][3];
			offset = resPos[i][4];
			break;
		}
	}
	resPos[i][5] = CountResLen(i);

}

int LibOperator::CountResLen(int start)
{
	//计算资源长度
	//1大于段实际最大使用值
	//2大于下一个资源的地址（需循环判断下一个资源的存在，不一定是i+1）
	for (int i = start + 1; i < resCount / 3; i++)
	{
		if (resPos[i][3] == segment && resPos[i][4] > offset)//找到下一个,计算差值即可
		{

			return (resPos[i][4] - offset);
		}

	}
	return (GetSegmentLen(segment) - offset);
}

int LibOperator::GetSegmentLen(int segment)
{
	return BytesToInt2(&libByte[segment * 16384 + 12]);
}