base init
This commit is contained in:
7
src/libgame/CMakeLists.txt
Normal file
7
src/libgame/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
file(GLOB_RECURSE LIBGAME_SOURCES "*.h" "*.cpp")
|
||||
|
||||
add_library(libgame STATIC ${LIBGAME_SOURCES})
|
||||
|
||||
target_link_libraries(libgame
|
||||
common
|
||||
)
|
||||
263
src/libgame/attribute.cpp
Normal file
263
src/libgame/attribute.cpp
Normal file
@@ -0,0 +1,263 @@
|
||||
#include "../../libthecore/include/stdafx.h"
|
||||
#include "attribute.h"
|
||||
|
||||
#define SET_BIT(var,bit) ((var) |= (bit))
|
||||
#define REMOVE_BIT(var,bit) ((var) &= ~(bit))
|
||||
|
||||
void CAttribute::Initialize(DWORD width, DWORD height)
|
||||
{
|
||||
dataType = D_DWORD;
|
||||
defaultAttr = 0;
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
data = NULL;
|
||||
bytePtr = NULL;
|
||||
wordPtr = NULL;
|
||||
dwordPtr = NULL;
|
||||
}
|
||||
|
||||
void CAttribute::Alloc()
|
||||
{
|
||||
size_t memSize;
|
||||
|
||||
switch (dataType)
|
||||
{
|
||||
case D_DWORD:
|
||||
memSize = width * height * sizeof(DWORD);
|
||||
break;
|
||||
|
||||
case D_WORD:
|
||||
memSize = width * height * sizeof(WORD);
|
||||
break;
|
||||
|
||||
case D_BYTE:
|
||||
memSize = width * height;
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(!"dataType error!");
|
||||
return;
|
||||
}
|
||||
|
||||
//sys_log(0, "Alloc::dataType %u width %d height %d memSize %d", dataType, width, height, memSize);
|
||||
data = malloc(memSize);
|
||||
|
||||
switch (dataType)
|
||||
{
|
||||
case D_DWORD:
|
||||
dwordPtr = new DWORD * [height];
|
||||
dwordPtr[0] = (DWORD *) data;
|
||||
|
||||
for (DWORD y = 1; y < height; ++y)
|
||||
dwordPtr[y] = dwordPtr[y - 1] + width;
|
||||
|
||||
for (DWORD y = 0; y < height; ++y)
|
||||
for (DWORD x = 0; x < width; ++x)
|
||||
dwordPtr[y][x] = defaultAttr;
|
||||
|
||||
break;
|
||||
|
||||
case D_WORD:
|
||||
wordPtr = new WORD * [height];
|
||||
wordPtr[0] = (WORD *) data;
|
||||
|
||||
for (DWORD y = 1; y < height; ++y)
|
||||
wordPtr[y] = wordPtr[y - 1] + width;
|
||||
|
||||
for (DWORD y = 0; y < height; ++y)
|
||||
for (DWORD x = 0; x < width; ++x)
|
||||
wordPtr[y][x] = defaultAttr;
|
||||
|
||||
break;
|
||||
|
||||
case D_BYTE:
|
||||
bytePtr = new BYTE * [height];
|
||||
bytePtr[0] = (BYTE *) data;
|
||||
|
||||
for (DWORD y = 1; y < height; ++y)
|
||||
bytePtr[y] = bytePtr[y - 1] + width;
|
||||
|
||||
for (DWORD y = 0; y < height; ++y)
|
||||
for (DWORD x = 0; x < width; ++x)
|
||||
bytePtr[y][x] = defaultAttr;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CAttribute::CAttribute(DWORD width, DWORD height) // dword 타잎으로 모두 0을 채운다.
|
||||
{
|
||||
Initialize(width, height);
|
||||
Alloc();
|
||||
}
|
||||
|
||||
CAttribute::CAttribute(DWORD * attr, DWORD width, DWORD height) // attr을 읽어서 smart하게 속성을 읽어온다.
|
||||
{
|
||||
Initialize(width, height);
|
||||
|
||||
int i;
|
||||
int size = width * height;
|
||||
|
||||
for (i = 0; i < size; ++i)
|
||||
if (attr[0] != attr[i])
|
||||
break;
|
||||
|
||||
// 속성이 전부 같으면 단지 defaultAttr만 설정한다.
|
||||
if (i == size)
|
||||
defaultAttr = attr[0];
|
||||
else
|
||||
{
|
||||
int allAttr = 0;
|
||||
|
||||
for (i = 0; i < size; ++i)
|
||||
allAttr |= attr[i];
|
||||
|
||||
// 하위 8비트만 사용할 경우 D_BYTE
|
||||
if (!(allAttr & 0xffffff00))
|
||||
dataType = D_BYTE;
|
||||
// 하위 16비트만 사용할 경우 D_WORD
|
||||
else if (!(allAttr & 0xffff0000))
|
||||
dataType = D_WORD;
|
||||
else // 그 이외에는 D_DWORD
|
||||
dataType = D_DWORD;
|
||||
|
||||
Alloc();
|
||||
|
||||
if (dataType == D_DWORD) // D_DWORD일 때는 원본 속성과 같으므로 단지 복사.
|
||||
thecore_memcpy(data, attr, sizeof(DWORD) * width * height);
|
||||
else
|
||||
{
|
||||
// 아니면 컨버트 해야 한다.
|
||||
DWORD * pdw = (DWORD *) attr;
|
||||
|
||||
if (dataType == D_BYTE)
|
||||
{
|
||||
for (DWORD y = 0; y < height; ++y)
|
||||
for (DWORD x = 0; x < width; ++x)
|
||||
bytePtr[y][x] = *(pdw++);
|
||||
}
|
||||
else if (dataType == D_WORD)
|
||||
{
|
||||
for (DWORD y = 0; y < height; ++y)
|
||||
for (DWORD x = 0; x < width; ++x)
|
||||
wordPtr[y][x] = *(pdw++);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CAttribute::~CAttribute()
|
||||
{
|
||||
if (data)
|
||||
free(data);
|
||||
|
||||
if (bytePtr)
|
||||
delete [] bytePtr;
|
||||
|
||||
if (wordPtr)
|
||||
delete [] wordPtr;
|
||||
|
||||
if (dwordPtr)
|
||||
delete [] dwordPtr;
|
||||
}
|
||||
|
||||
int CAttribute::GetDataType()
|
||||
{
|
||||
return dataType;
|
||||
}
|
||||
|
||||
void * CAttribute::GetDataPtr()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
void CAttribute::Set(DWORD x, DWORD y, DWORD attr)
|
||||
{
|
||||
if (x > width || y > height)
|
||||
return;
|
||||
|
||||
if (!data)
|
||||
Alloc();
|
||||
|
||||
if (bytePtr)
|
||||
{
|
||||
SET_BIT(bytePtr[y][x], attr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (wordPtr)
|
||||
{
|
||||
SET_BIT(wordPtr[y][x], attr);
|
||||
return;
|
||||
}
|
||||
|
||||
SET_BIT(dwordPtr[y][x], attr);
|
||||
}
|
||||
|
||||
void CAttribute::Remove(DWORD x, DWORD y, DWORD attr)
|
||||
{
|
||||
if (x > width || y > height)
|
||||
return;
|
||||
|
||||
if (!data) // 속성을 삭제할 때 만약 데이터가 없으면 그냥 리턴한다.
|
||||
return;
|
||||
|
||||
if (bytePtr)
|
||||
{
|
||||
REMOVE_BIT(bytePtr[y][x], attr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (wordPtr)
|
||||
{
|
||||
REMOVE_BIT(wordPtr[y][x], attr);
|
||||
return;
|
||||
}
|
||||
|
||||
REMOVE_BIT(dwordPtr[y][x], attr);
|
||||
}
|
||||
|
||||
DWORD CAttribute::Get(DWORD x, DWORD y)
|
||||
{
|
||||
if (x > width || y > height)
|
||||
return 0;
|
||||
|
||||
if (!data)
|
||||
return defaultAttr;
|
||||
|
||||
if (bytePtr)
|
||||
return bytePtr[y][x];
|
||||
|
||||
if (wordPtr)
|
||||
return wordPtr[y][x];
|
||||
|
||||
return dwordPtr[y][x];
|
||||
}
|
||||
|
||||
void CAttribute::CopyRow(DWORD y, DWORD * row)
|
||||
{
|
||||
if (!data)
|
||||
{
|
||||
for (DWORD x = 0; x < width; ++x)
|
||||
row[x] = defaultAttr;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (dwordPtr)
|
||||
thecore_memcpy(row, dwordPtr[y], sizeof(DWORD) * width);
|
||||
else
|
||||
{
|
||||
if (bytePtr)
|
||||
{
|
||||
for (DWORD x = 0; x < width; ++x)
|
||||
row[x] = bytePtr[y][x];
|
||||
}
|
||||
else if (wordPtr)
|
||||
{
|
||||
for (DWORD x = 0; x < width; ++x)
|
||||
row[x] = wordPtr[y][x];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
43
src/libgame/attribute.h
Normal file
43
src/libgame/attribute.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef __INC_METIN_II_ATTRIBUTE_H__
|
||||
#define __INC_METIN_II_ATTRIBUTE_H__
|
||||
|
||||
enum EDataType
|
||||
{
|
||||
D_DWORD,
|
||||
D_WORD,
|
||||
D_BYTE
|
||||
};
|
||||
|
||||
//
|
||||
// 맵 속성들을 처리할 때 사용
|
||||
//
|
||||
class CAttribute
|
||||
{
|
||||
public:
|
||||
CAttribute(DWORD width, DWORD height); // dword 타잎으로 모두 0을 채운다.
|
||||
CAttribute(DWORD * attr, DWORD width, DWORD height); // attr을 읽어서 smart하게 속성을 읽어온다.
|
||||
~CAttribute();
|
||||
void Alloc();
|
||||
int GetDataType();
|
||||
void * GetDataPtr();
|
||||
void Set(DWORD x, DWORD y, DWORD attr);
|
||||
void Remove(DWORD x, DWORD y, DWORD attr);
|
||||
DWORD Get(DWORD x, DWORD y);
|
||||
void CopyRow(DWORD y, DWORD * row);
|
||||
|
||||
private:
|
||||
void Initialize(DWORD width, DWORD height);
|
||||
|
||||
private:
|
||||
int dataType;
|
||||
DWORD defaultAttr;
|
||||
DWORD width, height;
|
||||
|
||||
void * data;
|
||||
|
||||
BYTE ** bytePtr;
|
||||
WORD ** wordPtr;
|
||||
DWORD ** dwordPtr;
|
||||
};
|
||||
|
||||
#endif
|
||||
133
src/libgame/grid.cpp
Normal file
133
src/libgame/grid.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "../../libthecore/include/memcpy.h"
|
||||
#include "../../common/stl.h"
|
||||
#include "grid.h"
|
||||
#include <algorithm>
|
||||
CGrid::CGrid(int w, int h) : m_iWidth(w), m_iHeight(h)
|
||||
{
|
||||
m_pGrid = new char[m_iWidth * m_iHeight];
|
||||
memset(m_pGrid, 0, sizeof(char) * m_iWidth * m_iHeight);
|
||||
}
|
||||
|
||||
CGrid::CGrid(CGrid * pkGrid, int w, int h) : m_iWidth(w), m_iHeight(h)
|
||||
{
|
||||
m_pGrid = new char[m_iWidth * m_iHeight];
|
||||
int iSize = std::min(w * h, pkGrid->m_iWidth * pkGrid->m_iHeight);
|
||||
thecore_memcpy(m_pGrid, pkGrid->m_pGrid, sizeof(char) * iSize);
|
||||
}
|
||||
|
||||
CGrid::~CGrid()
|
||||
{
|
||||
delete [] m_pGrid;
|
||||
}
|
||||
|
||||
void CGrid::Clear()
|
||||
{
|
||||
memset(m_pGrid, 0, sizeof(char) * m_iWidth * m_iHeight);
|
||||
}
|
||||
|
||||
int CGrid::FindBlank(int w, int h)
|
||||
{
|
||||
// 크기가 더 크다면 확인할 필요 없이 그냥 리턴
|
||||
if (w > m_iWidth || h > m_iHeight)
|
||||
return -1;
|
||||
|
||||
int iRow;
|
||||
|
||||
for (iRow = 0; iRow < m_iHeight; ++iRow)
|
||||
{
|
||||
for (int iCol = 0; iCol < m_iWidth; ++iCol)
|
||||
{
|
||||
int iIndex = iRow * m_iWidth + iCol;
|
||||
|
||||
if (IsEmpty(iIndex, w, h))
|
||||
return iIndex;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool CGrid::Put(int iPos, int w, int h)
|
||||
{
|
||||
if (!IsEmpty(iPos, w, h))
|
||||
return false;
|
||||
|
||||
for (int y = 0; y < h; ++y)
|
||||
{
|
||||
int iStart = iPos + (y * m_iWidth);
|
||||
m_pGrid[iStart] = true;
|
||||
|
||||
int x = 1;
|
||||
while (x < w)
|
||||
m_pGrid[iStart + x++] = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CGrid::Get(int iPos, int w, int h)
|
||||
{
|
||||
if (iPos < 0 || iPos >= m_iWidth * m_iHeight)
|
||||
return;
|
||||
|
||||
for (int y = 0; y < h; ++y)
|
||||
{
|
||||
int iStart = iPos + (y * m_iWidth);
|
||||
m_pGrid[iStart] = false;
|
||||
|
||||
int x = 1;
|
||||
while (x < w)
|
||||
m_pGrid[iStart + x++] = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CGrid::IsEmpty(int iPos, int w, int h)
|
||||
{
|
||||
if (iPos < 0)
|
||||
return false;
|
||||
|
||||
int iRow = iPos / m_iWidth;
|
||||
|
||||
// Grid 안쪽인가를 먼저 검사
|
||||
if (iRow + h > m_iHeight)
|
||||
return false;
|
||||
|
||||
if (iPos + w > iRow * m_iWidth + m_iWidth)
|
||||
return false;
|
||||
|
||||
for (int y = 0; y < h; ++y)
|
||||
{
|
||||
int iStart = iPos + (y * m_iWidth);
|
||||
|
||||
if (m_pGrid[iStart])
|
||||
return false;
|
||||
|
||||
int x = 1;
|
||||
while (x < w)
|
||||
if (m_pGrid[iStart + x++])
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CGrid::Print()
|
||||
{
|
||||
printf("Grid %p\n", this);
|
||||
|
||||
for (int y = 0; y < m_iHeight; ++y)
|
||||
{
|
||||
for (int x = 0; x < m_iWidth; ++x)
|
||||
printf("%d", m_pGrid[y * m_iWidth + x]);
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int CGrid::GetSize()
|
||||
{
|
||||
return m_iWidth * m_iHeight;
|
||||
}
|
||||
|
||||
26
src/libgame/grid.h
Normal file
26
src/libgame/grid.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef __INC_METIN_II_GRID_H__
|
||||
#define __INC_METIN_II_GRID_H__
|
||||
|
||||
class CGrid
|
||||
{
|
||||
public:
|
||||
CGrid(int w, int h);
|
||||
CGrid(CGrid * pkGrid, int w, int h);
|
||||
~CGrid();
|
||||
|
||||
void Clear();
|
||||
int FindBlank(int w, int h);
|
||||
bool IsEmpty(int iPos, int w, int h);
|
||||
bool Put(int iPos, int w, int h);
|
||||
void Get(int iPos, int w, int h);
|
||||
void Print();
|
||||
unsigned int GetSize();
|
||||
|
||||
protected:
|
||||
int m_iWidth;
|
||||
int m_iHeight;
|
||||
|
||||
char * m_pGrid;
|
||||
};
|
||||
|
||||
#endif
|
||||
53
src/libgame/targa.cpp
Normal file
53
src/libgame/targa.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "../../libthecore/include/stdafx.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "targa.h"
|
||||
|
||||
CTargaImage::CTargaImage()
|
||||
: m_pbuf(NULL), m_x(0), m_y(0)
|
||||
{
|
||||
memset( &m_header, 0, sizeof(m_header) );
|
||||
}
|
||||
|
||||
CTargaImage::~CTargaImage()
|
||||
{
|
||||
if (m_pbuf)
|
||||
delete [] m_pbuf;
|
||||
}
|
||||
|
||||
char * CTargaImage::GetBasePointer(int line)
|
||||
{
|
||||
return m_pbuf + (m_x * line) * sizeof(DWORD);
|
||||
}
|
||||
|
||||
void CTargaImage::Create(int x, int y)
|
||||
{
|
||||
memset(&m_header, 0, sizeof(m_header));
|
||||
|
||||
m_header.imgType = 2;
|
||||
m_header.width = x;
|
||||
m_header.height = y;
|
||||
m_header.colorBits = 32;
|
||||
m_header.desc = 0x20;
|
||||
|
||||
m_pbuf = new char[x * y * sizeof(DWORD)];
|
||||
memset(m_pbuf, 0, x * y * sizeof(DWORD));
|
||||
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
}
|
||||
|
||||
bool CTargaImage::Save(const char * filename)
|
||||
{
|
||||
FILE * fp = fopen(filename, "wb");
|
||||
|
||||
if (!fp)
|
||||
return false;
|
||||
|
||||
fwrite(&m_header, sizeof(TGA_HEADER), 1, fp);
|
||||
fwrite(m_pbuf, m_x * m_y * sizeof(DWORD), 1, fp);
|
||||
|
||||
fclose(fp);
|
||||
return true;
|
||||
}
|
||||
|
||||
46
src/libgame/targa.h
Normal file
46
src/libgame/targa.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef __INC_METIN_II_SERVER_TARGA_H__
|
||||
#define __INC_METIN_II_SERVER_TARGA_H__
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
struct TGA_HEADER
|
||||
{
|
||||
char idLen; // 0
|
||||
char palType; // 파레트있으면 1, 없음 0
|
||||
char imgType; // 파레트있으면 1, 없음 2
|
||||
WORD colorBegin; // 0
|
||||
WORD colorCount; // 파레트 있으면 256, 없음 0
|
||||
char palEntrySize; // 파레트 있으면 24, 없음 0
|
||||
WORD left;
|
||||
WORD top;
|
||||
WORD width;
|
||||
WORD height;
|
||||
char colorBits;
|
||||
char desc;
|
||||
};
|
||||
|
||||
#define IMAGEDESC_ORIGIN_MASK 0x30
|
||||
#define IMAGEDESC_TOPLEFT 0x20
|
||||
#define IMAGEDESC_BOTLEFT 0x00
|
||||
#define IMAGEDESC_BOTRIGHT 0x10
|
||||
#define IMAGEDESC_TOPRIGHT 0x30
|
||||
|
||||
#pragma pack()
|
||||
|
||||
class CTargaImage
|
||||
{
|
||||
public:
|
||||
CTargaImage();
|
||||
~CTargaImage();
|
||||
|
||||
void Create(int x, int y);
|
||||
char * GetBasePointer(int line = 0);
|
||||
bool Save(const char * filename);
|
||||
|
||||
protected:
|
||||
TGA_HEADER m_header;
|
||||
char * m_pbuf;
|
||||
int m_x, m_y;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user