ddnet/src/game/client/components/mapimages.cpp

199 lines
5.3 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#include <engine/graphics.h>
#include <engine/map.h>
2010-10-06 21:07:35 +00:00
#include <engine/storage.h>
#include <engine/serverbrowser.h>
2010-05-29 07:25:38 +00:00
#include <game/client/component.h>
#include <engine/textrender.h>
2010-05-29 07:25:38 +00:00
#include <game/mapitems.h>
2008-09-01 05:54:00 +00:00
2010-05-29 07:25:38 +00:00
#include "mapimages.h"
2008-09-01 05:54:00 +00:00
2010-05-29 07:25:38 +00:00
CMapImages::CMapImages()
2008-09-01 05:54:00 +00:00
{
2010-05-29 07:25:38 +00:00
m_Count = 0;
2011-03-16 12:48:16 +00:00
m_EntitiesTextures = -1;
m_OverlayBottomTexture = -1;
m_OverlayTopTexture = -1;
m_OverlayCenterTexture = -1;
}
2017-09-13 18:33:58 +00:00
void CMapImages::OnInit()
{
//TODO: improve this a bit -- with better fron sizes etc.
2017-09-13 18:33:58 +00:00
if(m_OverlayBottomTexture == -1)
{
void *pMem = calloc(1024 * 1024, 1);
m_OverlayBottomTexture = Graphics()->LoadTextureRaw(1024, 1024, CImageInfo::FORMAT_ALPHA, pMem, CImageInfo::FORMAT_ALPHA, IGraphics::TEXLOAD_NOMIPMAPS);
free(pMem);
2017-09-13 18:33:58 +00:00
for(int i = 0; i < 256; ++i)
{
char buff[4];
str_format(buff, 4, "%d", i);
float x = (i%16) * 64;
float y = (int)(i/16)* 64;
TextRender()->UploadEntityLayerText(m_OverlayBottomTexture, buff, -1, x+1, y + 12 + 32, 20, 64-1);
}
}
2017-09-13 18:33:58 +00:00
if(m_OverlayTopTexture == -1)
{
void *pMem = calloc(1024 * 1024, 1);
m_OverlayTopTexture = Graphics()->LoadTextureRaw(1024, 1024, CImageInfo::FORMAT_ALPHA, pMem, CImageInfo::FORMAT_ALPHA, IGraphics::TEXLOAD_NOMIPMAPS);
free(pMem);
2017-09-13 18:33:58 +00:00
for(int i = 0; i < 256; ++i)
{
char buff[4];
str_format(buff, 4, "%d", i);
float x = (i%16) * 64;
float y = (int)(i/16)* 64;
TextRender()->UploadEntityLayerText(m_OverlayTopTexture, buff, -1, x+1, y+1, 20, 64-1);
}
}
2017-09-13 18:33:58 +00:00
if(m_OverlayCenterTexture == -1)
{
void *pMem = calloc(1024 * 1024, 1);
m_OverlayCenterTexture = Graphics()->LoadTextureRaw(1024, 1024, CImageInfo::FORMAT_ALPHA, pMem, CImageInfo::FORMAT_ALPHA, IGraphics::TEXLOAD_NOMIPMAPS);
free(pMem);
2017-09-13 18:33:58 +00:00
for(int i = 0; i < 256; ++i)
{
char buff[4];
str_format(buff, 4, "%d", i);
int len = str_length(buff);
float x = (i%16) * 64;
float y = (int)(i/16)* 64;
int MinSize = (len == 3 ? 20 : 50);
int OffY = (len == 3 ? 10 : 5);
int OffX = (len == 3 ? 5 : 1);
TextRender()->UploadEntityLayerText(m_OverlayCenterTexture, buff, -1, x + OffX, y + OffY, -1, 64-(OffX*2), 64, MinSize);
}
}
2008-09-01 05:54:00 +00:00
}
2010-05-29 07:25:38 +00:00
void CMapImages::OnMapLoad()
2008-09-01 05:54:00 +00:00
{
2010-05-29 07:25:38 +00:00
IMap *pMap = Kernel()->RequestInterface<IMap>();
2008-09-01 05:54:00 +00:00
// unload all textures
2010-05-29 07:25:38 +00:00
for(int i = 0; i < m_Count; i++)
2008-09-01 05:54:00 +00:00
{
2010-05-29 07:25:38 +00:00
Graphics()->UnloadTexture(m_aTextures[i]);
m_aTextures[i] = -1;
2008-09-01 05:54:00 +00:00
}
2010-05-29 07:25:38 +00:00
m_Count = 0;
2008-09-01 05:54:00 +00:00
2010-05-29 07:25:38 +00:00
int Start;
pMap->GetType(MAPITEMTYPE_IMAGE, &Start, &m_Count);
2008-09-01 05:54:00 +00:00
// load new textures
2015-08-25 00:11:04 +00:00
for(int i = 0; i < m_Count; i++)
{
m_aTextures[i] = 0;
CMapItemImage *pImg = (CMapItemImage *)pMap->GetItem(Start+i, 0, 0);
if(pImg->m_External)
{
char Buf[256];
char *pName = (char *)pMap->GetData(pImg->m_ImageName);
str_format(Buf, sizeof(Buf), "mapres/%s.png", pName);
m_aTextures[i] = Graphics()->LoadTexture(Buf, IStorage::TYPE_ALL, CImageInfo::FORMAT_AUTO, 0);
}
else
{
void *pData = pMap->GetData(pImg->m_ImageData);
m_aTextures[i] = Graphics()->LoadTextureRaw(pImg->m_Width, pImg->m_Height, CImageInfo::FORMAT_RGBA, pData, CImageInfo::FORMAT_RGBA, 0);
pMap->UnloadData(pImg->m_ImageData);
}
}
}
void CMapImages::LoadBackground(class IMap *pMap)
{
// unload all textures
for(int i = 0; i < m_Count; i++)
{
Graphics()->UnloadTexture(m_aTextures[i]);
m_aTextures[i] = -1;
}
m_Count = 0;
int Start;
pMap->GetType(MAPITEMTYPE_IMAGE, &Start, &m_Count);
// load new textures
2010-05-29 07:25:38 +00:00
for(int i = 0; i < m_Count; i++)
2008-09-01 05:54:00 +00:00
{
2010-05-29 07:25:38 +00:00
m_aTextures[i] = 0;
2010-05-29 07:25:38 +00:00
CMapItemImage *pImg = (CMapItemImage *)pMap->GetItem(Start+i, 0, 0);
if(pImg->m_External)
2008-09-01 05:54:00 +00:00
{
2010-05-29 07:25:38 +00:00
char Buf[256];
char *pName = (char *)pMap->GetData(pImg->m_ImageName);
str_format(Buf, sizeof(Buf), "mapres/%s.png", pName);
2010-10-06 21:07:35 +00:00
m_aTextures[i] = Graphics()->LoadTexture(Buf, IStorage::TYPE_ALL, CImageInfo::FORMAT_AUTO, 0);
2008-09-01 05:54:00 +00:00
}
else
{
2010-05-29 07:25:38 +00:00
void *pData = pMap->GetData(pImg->m_ImageData);
m_aTextures[i] = Graphics()->LoadTextureRaw(pImg->m_Width, pImg->m_Height, CImageInfo::FORMAT_RGBA, pData, CImageInfo::FORMAT_RGBA, 0);
pMap->UnloadData(pImg->m_ImageData);
2008-09-01 05:54:00 +00:00
}
}
}
2011-03-16 12:48:16 +00:00
int CMapImages::GetEntities()
{
CServerInfo Info;
Client()->GetServerInfo(&Info);
2017-03-02 08:27:03 +00:00
if(m_EntitiesTextures == -1 || str_comp(m_aEntitiesGameType, Info.m_aGameType))
2011-03-16 12:48:16 +00:00
{
// DDNet default to prevent delay in seeing entities
char file[64] = "ddnet";
if(IsDDNet(&Info))
str_copy(file, "ddnet", sizeof(file));
else if(IsDDRace(&Info))
str_copy(file, "ddrace", sizeof(file));
else if(IsRace(&Info))
str_copy(file, "race", sizeof(file));
else if(IsFNG(&Info))
str_copy(file, "fng", sizeof(file));
2016-05-05 18:21:34 +00:00
else if(IsVanilla(&Info))
str_copy(file, "vanilla", sizeof(file));
char path[64];
str_format(path, sizeof(path), "editor/entities_clear/%s.png", file);
2017-03-02 08:27:03 +00:00
if(m_EntitiesTextures >= 0)
Graphics()->UnloadTexture(m_EntitiesTextures);
m_EntitiesTextures = Graphics()->LoadTexture(path, IStorage::TYPE_ALL, CImageInfo::FORMAT_AUTO, 0);
str_copy(m_aEntitiesGameType, Info.m_aGameType, sizeof(m_aEntitiesGameType));
2011-03-16 12:48:16 +00:00
}
return m_EntitiesTextures;
}
2017-09-13 18:33:58 +00:00
int CMapImages::GetOverlayBottom()
{
return m_OverlayBottomTexture;
}
2017-09-13 18:33:58 +00:00
int CMapImages::GetOverlayTop()
{
return m_OverlayTopTexture;
}
2017-09-13 18:33:58 +00:00
int CMapImages::GetOverlayCenter()
{
return m_OverlayCenterTexture;
}