1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include <array>
#include <cstring>
#include "System/bitops.h"
#include "CommandColors.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/RenderDataBuffer.hpp"
#include "Rendering/Textures/Bitmap.h"
#include "MouseCursor.h"
#include "HwMouseCursor.h"
#include "System/Log/ILog.h"
#include "System/SpringMath.h"
#include "System/SafeUtil.h"
#include "System/StringUtil.h"
#include "System/UnorderedMap.hpp"
#include "System/FileSystem/FileHandler.h"
#include "System/FileSystem/FileSystem.h"
#include "System/FileSystem/SimpleParser.h"
// FIXME: "array must be initialized with a brace-enclosed initializer" if constexpr
static const VA_TYPE_TC CURSOR_VERTS[] = {
VA_TYPE_TC{{0.0f, 0.0f, 0.0f}, 0.0f, 0.0f, {1.0f, 1.0f, 1.0f, 1.0f}}, // tl
VA_TYPE_TC{{0.0f, 1.0f, 0.0f}, 0.0f, 1.0f, {1.0f, 1.0f, 1.0f, 1.0f}}, // bl
VA_TYPE_TC{{1.0f, 1.0f, 0.0f}, 1.0f, 1.0f, {1.0f, 1.0f, 1.0f, 1.0f}}, // br
VA_TYPE_TC{{1.0f, 1.0f, 0.0f}, 1.0f, 1.0f, {1.0f, 1.0f, 1.0f, 1.0f}}, // br
VA_TYPE_TC{{1.0f, 0.0f, 0.0f}, 1.0f, 0.0f, {1.0f, 1.0f, 1.0f, 1.0f}}, // tr
VA_TYPE_TC{{0.0f, 0.0f, 0.0f}, 0.0f, 0.0f, {1.0f, 1.0f, 1.0f, 1.0f}}, // tl
};
CMouseCursor::CMouseCursor(const std::string& name, HotSpot hs): hotSpot(hs)
{
frames.reserve(8);
images.reserve(8);
Build(name);
for (FrameData& frame: frames) {
frame.startTime = animPeriod;
frame.endTime = (animPeriod += frame.length);
xmaxsize = std::max(images[frame.imageIdx].xOrigSize, xmaxsize);
ymaxsize = std::max(images[frame.imageIdx].yOrigSize, ymaxsize);
}
if (hotSpot == Center) {
xofs = xmaxsize / 2;
yofs = ymaxsize / 2;
}
}
CMouseCursor::~CMouseCursor()
{
if (hwCursor != nullptr) {
hwCursor->Kill();
IHardwareCursor::Free(hwCursor);
}
for (const auto& image: images) {
glDeleteTextures(1, &image.texture);
}
}
CMouseCursor& CMouseCursor::operator = (CMouseCursor&& mc) noexcept {
images = std::move(mc.images);
frames = std::move(mc.frames);
if (mc.hwCursor != nullptr) {
memcpy(hwCursorMem, mc.hwCursorMem, sizeof(hwCursorMem));
memset(mc.hwCursorMem, 0, sizeof(hwCursorMem));
hwCursor = reinterpret_cast<IHardwareCursor*>(hwCursorMem);
mc.hwCursor = nullptr;
}
hotSpot = mc.hotSpot;
animTime = mc.animTime;
animPeriod = mc.animPeriod;
currentFrame = mc.currentFrame;
xmaxsize = mc.xmaxsize;
ymaxsize = mc.ymaxsize;
xofs = mc.xofs;
yofs = mc.yofs;
hwValid = mc.hwValid;
return *this;
}
bool CMouseCursor::Build(const std::string& name)
{
int lastFrame = 1000;
hwCursor = IHardwareCursor::Alloc(hwCursorMem);
hwCursor->Init(hotSpot);
if (!BuildFromSpecFile(name, lastFrame))
BuildFromFileNames(name, lastFrame);
hwCursor->Finish();
return (hwValid = hwCursor->IsValid(), IsValid());
}
bool CMouseCursor::BuildFromSpecFile(const std::string& name, int& lastFrame)
{
const std::string specFileName = "anims/" + name + ".txt";
if (!CFileHandler::FileExists(specFileName, SPRING_VFS_RAW_FIRST))
return false;
CFileHandler specFile(specFileName);
CSimpleParser specParser(specFile);
const std::array<std::pair<std::string, int>, 3> commandsMap = {{{"frame", 0}, {"hotspot", 1}, {"lastframe", 2}}};
spring::unsynced_map<std::string, int> imageIdxMap;
for (std::string line = std::move(specParser.GetCleanLine()); !line.empty(); line = std::move(specParser.GetCleanLine())) {
const std::vector<std::string>& words = specParser.Tokenize(line, 2);
const std::string& command = StringToLower(words[0]);
const auto cmdPred = [&](const std::pair<std::string, int>& p) { return (p.first == command); };
const auto cmdIter = std::find_if(commandsMap.cbegin(), commandsMap.cend(), cmdPred);
if (cmdIter == commandsMap.end()) {
LOG_L(L_ERROR, "[MouseCursor::%s] unknown command \"%s\" in file \"%s\"", __func__, command.c_str(), specFileName.c_str());
continue;
}
if ((cmdIter->second == 0) && (words.size() >= 2)) {
const std::string& imageName = words[1];
float length = MIN_FRAME_LENGTH;
if (words.size() >= 3)
length = std::max(length, float(std::atof(words[2].c_str())));
const auto imageIdxIt = imageIdxMap.find(imageName);
if (imageIdxIt != imageIdxMap.end()) {
frames.emplace_back(imageIdxIt->second, length);
hwCursor->PushFrame(imageIdxIt->second, length);
continue;
}
ImageData image;
if (LoadCursorImage(imageName, image)) {
hwCursor->SetDelay(length);
imageIdxMap[imageName] = images.size();
images.push_back(image);
frames.emplace_back(images.size() - 1, length);
}
continue;
}
if ((cmdIter->second == 1) && (!words.empty())) {
if (words[1] == "topleft") {
hwCursor->SetHotSpot(hotSpot = TopLeft);
continue;
}
if (words[1] == "center") {
hwCursor->SetHotSpot(hotSpot = Center);
continue;
}
LOG_L(L_ERROR, "[MouseCursor::%s] unknown hotspot \"%s\" in file \"%s\"", __func__, words[1].c_str(), specFileName.c_str());
continue;
}
if ((cmdIter->second == 2) && (!words.empty())) {
lastFrame = atoi(words[1].c_str());
continue;
}
}
return (IsValid());
}
bool CMouseCursor::BuildFromFileNames(const std::string& name, int lastFrame)
{
// find the image file type to use
const char* ext = "";
const char* exts[] = {"png", "tga", "bmp"};
char animFrameStr[512];
for (auto& e: exts) {
memset(animFrameStr, 0, sizeof(animFrameStr));
snprintf(animFrameStr, sizeof(animFrameStr) - 1, "anims/%s_%d.%s", name.c_str(), 0, ext = e);
if (CFileHandler::FileExists(animFrameStr, SPRING_VFS_RAW_FIRST))
break;
}
while (int(frames.size()) < lastFrame) {
memset(animFrameStr, 0, sizeof(animFrameStr));
snprintf(animFrameStr, sizeof(animFrameStr) - 1, "anims/%s_%d.%s", name.c_str(), static_cast<int>(frames.size()), ext);
ImageData image;
if (!LoadCursorImage(animFrameStr, image))
break;
images.push_back(image);
frames.emplace_back(images.size() - 1, float(DEF_FRAME_LENGTH));
}
return (IsValid());
}
#if 0
bool CMouseCursor::LoadDummyImage()
{
ImageData id;
CBitmap bn;
bn.Alloc(16, 16, 4);
bn.Fill(SColor(255, 255 * 0, 255, 255));
id.texture = bn.CreateTexture();
id.xOrigSize = bn.xsize;
id.yOrigSize = bn.ysize;
id.xAlignedSize = bn.xsize;
id.yAlignedSize = bn.ysize;
images.emplace_back(id);
frames.emplace_back(images.size() - 1, float(DEF_FRAME_LENGTH));
hwCursor->PushImage(bn.xsize, bn.ysize, bn.GetRawMem());
}
#endif
bool CMouseCursor::LoadCursorImage(const std::string& name, ImageData& image)
{
if (!CFileHandler::FileExists(name, SPRING_VFS_RAW_FIRST))
return false;
CBitmap b;
if (!b.Load(name)) {
LOG_L(L_ERROR, "[MouseCursor::%s] bad image file \"%s\"", __func__, name.c_str());
return false;
}
// hardcoded bmp transparency mask
if (FileSystem::GetExtension(name) == "bmp")
b.SetTransparent(SColor(84, 84, 252));
if (hwCursor->NeedsYFlip()) {
// WINDOWS
b.ReverseYAxis();
hwCursor->PushImage(b.xsize, b.ysize, b.GetRawMem());
} else {
// X11
hwCursor->PushImage(b.xsize, b.ysize, b.GetRawMem());
b.ReverseYAxis();
}
const int nx = next_power_of_2(b.xsize);
const int ny = next_power_of_2(b.ysize);
if (b.xsize != nx || b.ysize != ny) {
CBitmap bn;
bn.Alloc(nx, ny);
bn.CopySubImage(b, 0, ny - b.ysize);
image.texture = bn.CreateTexture();
image.xOrigSize = b.xsize;
image.yOrigSize = b.ysize;
image.xAlignedSize = bn.xsize;
image.yAlignedSize = bn.ysize;
} else {
image.texture = b.CreateTexture();
image.xOrigSize = b.xsize;
image.yOrigSize = b.ysize;
image.xAlignedSize = b.xsize;
image.yAlignedSize = b.ysize;
}
return true;
}
void CMouseCursor::Draw(int x, int y, float scale) const
{
if (frames.empty())
return;
const FrameData& frame = frames[currentFrame];
const ImageData& image = images[frame.imageIdx];
GL::RenderDataBufferTC* buffer = GL::GetRenderBufferTC();
Shader::IProgramObject* shader = buffer->GetShader();
const float3 winCoors = {x * 1.0f, (globalRendering->viewSizeY - y) * 1.0f, 0.0f};
const float2 winScale = {std::max(scale, -scale), 1.0f};
const float4& matParams = CalcFrameMatrixParams(winCoors, winScale);
CMatrix44f cursorMat;
cursorMat.Translate(matParams.x, matParams.y, 0.0f);
cursorMat.Scale({matParams.z, matParams.w, 1.0f});
glAttribStatePtr->EnableBlendMask();
glAttribStatePtr->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, image.texture);
shader->Enable();
shader->SetUniformMatrix4x4<float>("u_movi_mat", false, cursorMat);
shader->SetUniformMatrix4x4<float>("u_proj_mat", false, CMatrix44f::ClipOrthoProj01(globalRendering->supportClipSpaceControl * 1.0f));
shader->SetUniform("u_alpha_test_ctrl", 0.01f, 1.0f, 0.0f, 0.0f); // test > 0.01
buffer->SafeAppend(&CURSOR_VERTS[0], sizeof(CURSOR_VERTS) / sizeof(CURSOR_VERTS[0]));
buffer->Submit(GL_TRIANGLES);
shader->SetUniform("u_alpha_test_ctrl", 0.0f, 0.0f, 0.0f, 1.0f); // no test
shader->Disable();
}
void CMouseCursor::Update()
{
if (frames.empty())
return;
animTime = math::fmod(animTime + globalRendering->lastFrameTime * 0.001f, animPeriod);
if (animTime < frames[currentFrame].startTime) {
currentFrame = 0;
return;
}
while (animTime > frames[currentFrame].endTime) {
currentFrame += 1;
currentFrame %= frames.size();
}
}
void CMouseCursor::BindTexture() const
{
if (frames.empty())
return;
const FrameData& frame = frames[currentFrame];
const ImageData& image = images[frame.imageIdx];
glBindTexture(GL_TEXTURE_2D, image.texture);
}
void CMouseCursor::BindHwCursor() const
{
assert(hwCursor != nullptr);
hwCursor->Bind();
}
float4 CMouseCursor::CalcFrameMatrixParams(const float3& winCoors, const float2& winScale) const
{
if (winCoors.z > 1.0f || frames.empty())
return {0.0f, 0.0f, 0.0f, 0.0f};
const FrameData& frame = frames[currentFrame];
const ImageData& image = images[frame.imageIdx];
const float qis = winScale.x;
const float xs = image.xAlignedSize * qis;
const float ys = image.yAlignedSize * qis;
const float rxs = xs * globalRendering->pixelX;
const float rys = ys * globalRendering->pixelY;
// center on hotspot
const float xp = (winCoors.x - (xofs * qis )) * globalRendering->pixelX;
const float yp = (winCoors.y - winScale.y * (ys - (yofs * qis))) * globalRendering->pixelY;
return {xp, yp, rxs, rys};
}
|