Map texture painting

dev-ui
Piotr Dziwinski 2012-09-30 00:23:26 +02:00
parent a8554cfae3
commit b46dc3850f
5 changed files with 67 additions and 40 deletions

View File

@ -147,6 +147,13 @@ CImage::CImage()
m_data = nullptr; m_data = nullptr;
} }
CImage::CImage(Math::IntPoint size)
{
m_data = new ImageData();
m_data->surface = SDL_CreateRGBSurface(0, size.x, size.y, 32,
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
}
CImage::~CImage() CImage::~CImage()
{ {
Free(); Free();

View File

@ -60,6 +60,8 @@ private:
public: public:
//! Constructs empty image (with NULL data) //! Constructs empty image (with NULL data)
CImage(); CImage();
//! Constructs a RGBA image of given size
CImage(Math::IntPoint size);
//! Destroys image, calling Free() //! Destroys image, calling Free()
virtual ~CImage(); virtual ~CImage();

View File

@ -2074,7 +2074,7 @@ void CEngine::SetViewParams(const Math::Vector& eyePt, const Math::Vector& looka
m_sound->SetListener(eyePt, lookatPt); m_sound->SetListener(eyePt, lookatPt);
} }
Texture CEngine::CreateTexture(const std::string& texName, const TextureCreateParams& params) Texture CEngine::CreateTexture(const std::string& texName, const TextureCreateParams& params, CImage* image)
{ {
if (texName.empty()) if (texName.empty())
return Texture(); // invalid texture return Texture(); // invalid texture
@ -2082,16 +2082,25 @@ Texture CEngine::CreateTexture(const std::string& texName, const TextureCreatePa
if (m_texBlacklist.find(texName) != m_texBlacklist.end()) if (m_texBlacklist.find(texName) != m_texBlacklist.end())
return Texture(); // invalid texture return Texture(); // invalid texture
CImage img; Texture tex;
if (! img.Load(m_app->GetDataFilePath(DIR_TEXTURE, texName)))
{
std::string error = img.GetError();
GetLogger()->Error("Couldn't load texture '%s': %s, blacklisting\n", texName.c_str(), error.c_str());
m_texBlacklist.insert(texName);
return Texture(); // invalid texture
}
Texture tex = m_device->CreateTexture(&img, params); if (image == nullptr)
{
CImage img;
if (! img.Load(m_app->GetDataFilePath(DIR_TEXTURE, texName)))
{
std::string error = img.GetError();
GetLogger()->Error("Couldn't load texture '%s': %s, blacklisting\n", texName.c_str(), error.c_str());
m_texBlacklist.insert(texName);
return Texture(); // invalid texture
}
tex = m_device->CreateTexture(&img, params);
}
else
{
tex = m_device->CreateTexture(image, params);
}
if (! tex.Valid()) if (! tex.Valid())
{ {
@ -2111,6 +2120,12 @@ Texture CEngine::LoadTexture(const std::string& name)
return LoadTexture(name, m_defaultTexParams); return LoadTexture(name, m_defaultTexParams);
} }
Texture CEngine::LoadTexture(const std::string& name, CImage* image)
{
Texture tex = CreateTexture(name, m_defaultTexParams, image);
return tex;
}
Texture CEngine::LoadTexture(const std::string& name, const TextureCreateParams& params) Texture CEngine::LoadTexture(const std::string& name, const TextureCreateParams& params)
{ {
if (m_texBlacklist.find(name) != m_texBlacklist.end()) if (m_texBlacklist.find(name) != m_texBlacklist.end())

View File

@ -48,6 +48,7 @@ class CApplication;
class CInstanceManager; class CInstanceManager;
class CObject; class CObject;
class CSoundInterface; class CSoundInterface;
class CImage;
// Graphics module namespace // Graphics module namespace
@ -891,11 +892,14 @@ public:
//! Loads texture, creating it if not already present //! Loads texture, creating it if not already present
Texture LoadTexture(const std::string& name); Texture LoadTexture(const std::string& name);
//! Loads texture from existing image
Texture LoadTexture(const std::string& name, CImage* image);
//! Loads texture, creating it with given params if not already present //! Loads texture, creating it with given params if not already present
Texture LoadTexture(const std::string& name, const TextureCreateParams& params); Texture LoadTexture(const std::string& name, const TextureCreateParams& params);
//! Loads all necessary textures //! Loads all necessary textures
bool LoadAllTextures(); bool LoadAllTextures();
//! Changes colors in a texture
bool ChangeTextureColor(const std::string& texName, bool ChangeTextureColor(const std::string& texName,
Color colorRef1, Color colorNew1, Color colorRef1, Color colorNew1,
Color colorRef2, Color colorNew2, Color colorRef2, Color colorNew2,
@ -1196,7 +1200,7 @@ protected:
const Material& mat, int state); const Material& mat, int state);
//! Create texture and add it to cache //! Create texture and add it to cache
Texture CreateTexture(const std::string &texName, const TextureCreateParams &params); Texture CreateTexture(const std::string &texName, const TextureCreateParams &params, CImage* image = nullptr);
//! Tests whether the given object is visible //! Tests whether the given object is visible
bool IsVisible(int objRank); bool IsVisible(int objRank);

View File

@ -20,6 +20,8 @@
#include "ui/map.h" #include "ui/map.h"
#include "common/image.h"
#include <string.h> #include <string.h>
@ -986,63 +988,60 @@ void CMap::DrawVertex(Math::Point uv1, Math::Point uv2, float zoom)
void CMap::UpdateTerrain() void CMap::UpdateTerrain()
{ {
Gfx::Color color; if (m_fixImage[0] != 0) return; // still image?
Math::Vector pos;
float scale, water, level, intensity;
int x, y;
if ( m_fixImage[0] != 0 ) return; // still image? CImage img(Math::IntPoint(256, 256));
// TODO: map texture manipulation float scale = m_terrain->GetReliefScale();
return; float water = m_water->GetLevel();
// if ( !m_engine->OpenImage("map.png") ) return; Gfx::Color color;
scale = m_terrain->GetReliefScale();
water = m_water->GetLevel();
color.a = 0.0f; color.a = 0.0f;
for ( y=0 ; y<256 ; y++ ) for (int y = 0; y < 256; y++)
{ {
for ( x=0 ; x<256 ; x++ ) for (int x = 0; x < 256; x++)
{ {
pos.x = (static_cast<float>(x)-128.0f)*m_half/128.0f; Math::Vector pos;
pos.z = -(static_cast<float>(y)-128.0f)*m_half/128.0f; pos.x = (static_cast<float>(x) - 128.0f) * m_half / 128.0f;
pos.z = -(static_cast<float>(y) - 128.0f) * m_half / 128.0f;
pos.y = 0.0f; pos.y = 0.0f;
float level;
if ( pos.x >= -m_half && pos.x <= m_half && if ( pos.x >= -m_half && pos.x <= m_half &&
pos.z >= -m_half && pos.z <= m_half ) pos.z >= -m_half && pos.z <= m_half )
{ {
level = m_terrain->GetFloorLevel(pos, true)/scale; level = m_terrain->GetFloorLevel(pos, true) / scale;
} }
else else
{ {
level = 1000.0f; level = 1000.0f;
} }
intensity = level/256.0f; float intensity = level / 256.0f;
if ( intensity < 0.0f ) intensity = 0.0f; if (intensity < 0.0f) intensity = 0.0f;
if ( intensity > 1.0f ) intensity = 1.0f; if (intensity > 1.0f) intensity = 1.0f;
if ( level >= water ) // on water? if (level >= water) // on water?
{ {
color.r = m_floorColor.r + (intensity-0.5f); color.r = Math::Norm(m_floorColor.r + (intensity - 0.5f));
color.g = m_floorColor.g + (intensity-0.5f); color.g = Math::Norm(m_floorColor.g + (intensity - 0.5f));
color.b = m_floorColor.b + (intensity-0.5f); color.b = Math::Norm(m_floorColor.b + (intensity - 0.5f));
} }
else // underwater? else // underwater?
{ {
color.r = m_waterColor.r + (intensity-0.5f); color.r = Math::Norm(m_waterColor.r + (intensity - 0.5f));
color.g = m_waterColor.g + (intensity-0.5f); color.g = Math::Norm(m_waterColor.g + (intensity - 0.5f));
color.b = m_waterColor.b + (intensity-0.5f); color.b = Math::Norm(m_waterColor.b + (intensity - 0.5f));
} }
//m_engine->SetDot(x, y, color); img.SetPixel(Math::IntPoint(x, y), color);
} }
} }
//m_engine->CopyImage(); // copy the ground drawing m_engine->DeleteTexture("map.png");
//m_engine->CloseImage(); m_engine->LoadTexture("map.png", &img);
} }
// Updates the field in the map. // Updates the field in the map.