Browse Source

🚚 engine.h: Extract `CelSprite` and file loading

pull/2204/head
Gleb Mazovetskiy 5 years ago committed by Anders Jenbo
parent
commit
e40b1963f9
  1. 2
      CMakeLists.txt
  2. 1
      Source/DiabloUI/fonts.cpp
  3. 1
      Source/automap.cpp
  4. 2
      Source/control.cpp
  5. 1
      Source/cursor.cpp
  6. 1
      Source/cursor.h
  7. 2
      Source/debug.cpp
  8. 3
      Source/diablo.cpp
  9. 2
      Source/doom.cpp
  10. 1
      Source/drlg_l1.cpp
  11. 1
      Source/drlg_l2.cpp
  12. 1
      Source/drlg_l3.cpp
  13. 2
      Source/drlg_l4.cpp
  14. 41
      Source/engine.cpp
  15. 100
      Source/engine.h
  16. 2
      Source/engine/animationinfo.h
  17. 62
      Source/engine/cel_sprite.hpp
  18. 17
      Source/engine/load_cel.cpp
  19. 13
      Source/engine/load_cel.hpp
  20. 38
      Source/engine/load_file.cpp
  21. 54
      Source/engine/load_file.hpp
  22. 1
      Source/engine/render/cel_render.hpp
  23. 1
      Source/engine/render/cl2_render.hpp
  24. 1
      Source/engine/render/text_render.cpp
  25. 1
      Source/engine/render/text_render.hpp
  26. 2
      Source/gendung.cpp
  27. 1
      Source/gendung.h
  28. 2
      Source/gmenu.cpp
  29. 2
      Source/interfac.cpp
  30. 2
      Source/inv.cpp
  31. 2
      Source/items.cpp
  32. 1
      Source/lighting.cpp
  33. 2
      Source/minitext.cpp
  34. 1
      Source/missiles.cpp
  35. 1
      Source/monster.cpp
  36. 1
      Source/monster.h
  37. 1
      Source/objects.cpp
  38. 2
      Source/palette.cpp
  39. 1
      Source/player.cpp
  40. 7
      Source/player.h
  41. 2
      Source/quests.cpp
  42. 1
      Source/quests.h
  43. 1
      Source/setmaps.cpp
  44. 1
      Source/stores.cpp
  45. 1
      Source/stores.h
  46. 1
      Source/town.cpp
  47. 1
      Source/towners.cpp

2
CMakeLists.txt

@ -391,6 +391,8 @@ set(libdevilutionx_SRCS
Source/controls/touch.cpp
Source/controls/keymapper.cpp
Source/engine/animationinfo.cpp
Source/engine/load_cel.cpp
Source/engine/load_file.cpp
Source/engine/render/automap_render.cpp
Source/engine/render/cel_render.cpp
Source/engine/render/cl2_render.cpp

1
Source/DiabloUI/fonts.cpp

@ -1,6 +1,7 @@
#include "DiabloUI/fonts.h"
#include "diablo.h"
#include "engine/load_file.hpp"
#include "utils/file_util.h"
#include "utils/paths.h"
#include "utils/log.hpp"

1
Source/automap.cpp

@ -8,6 +8,7 @@
#include <fmt/format.h>
#include "control.h"
#include "engine/load_file.hpp"
#include "engine/render/automap_render.hpp"
#include "inv.h"
#include "monster.h"

2
Source/control.cpp

@ -14,6 +14,8 @@
#include "automap.h"
#include "controls/keymapper.hpp"
#include "cursor.h"
#include "engine/cel_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/render/cel_render.hpp"
#include "error.h"
#include "gamemenu.h"

1
Source/cursor.cpp

@ -9,6 +9,7 @@
#include "control.h"
#include "doom.h"
#include "engine/load_cel.hpp"
#include "engine/point.hpp"
#include "engine.h"
#include "engine/render/cel_render.hpp"

1
Source/cursor.h

@ -9,6 +9,7 @@
#include <utility>
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "miniwin/miniwin.h"
#include "utils/stdcompat/optional.hpp"

2
Source/debug.cpp

@ -5,6 +5,8 @@
*/
#include "cursor.h"
#include "engine/cel_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/point.hpp"
#include "inv.h"
#include "spells.h"

3
Source/diablo.cpp

@ -25,6 +25,9 @@
#include "drlg_l4.h"
#include "dx.h"
#include "encrypt.h"
#include "engine/cel_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/load_file.hpp"
#include "error.h"
#include "gamemenu.h"
#include "gmenu.h"

2
Source/doom.cpp

@ -7,6 +7,8 @@
#include "control.h"
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/render/cel_render.hpp"
#include "utils/stdcompat/optional.hpp"

1
Source/drlg_l1.cpp

@ -6,6 +6,7 @@
#include "drlg_l1.h"
#include "engine/point.hpp"
#include "engine/load_file.hpp"
#include "gendung.h"
#include "lighting.h"
#include "player.h"

1
Source/drlg_l2.cpp

@ -10,6 +10,7 @@
#include "diablo.h"
#include "drlg_l1.h"
#include "engine/load_file.hpp"
#include "objects.h"
#include "player.h"
#include "quests.h"

1
Source/drlg_l3.cpp

@ -7,6 +7,7 @@
#include <algorithm>
#include "drlg_l1.h"
#include "engine/load_file.hpp"
#include "lighting.h"
#include "monster.h"
#include "objdat.h"

2
Source/drlg_l4.cpp

@ -3,8 +3,10 @@
*
* Implementation of the hell level generation algorithms.
*/
#include "drlg_l4.h"
#include "drlg_l1.h"
#include "engine/load_file.hpp"
#include "monster.h"
#include "multi.h"
#include "objdat.h"

41
Source/engine.cpp

@ -17,7 +17,6 @@
#include "lighting.h"
#include "movie.h"
#include "options.h"
#include "storm/storm.h"
namespace devilution {
@ -80,16 +79,6 @@ void CelOutputBuffer::BlitFromSkipColorIndexZero(const CelOutputBuffer &src, SDL
BufferBlit</*SkipColorIndexZero=*/true>(src, srcRect, *this, targetPosition);
}
CelSprite LoadCel(const char *pszName, int width)
{
return CelSprite(LoadFileInMem(pszName), width);
}
CelSprite LoadCel(const char *pszName, const int *widths)
{
return CelSprite(LoadFileInMem(pszName), widths);
}
void DrawHorizontalLine(const CelOutputBuffer &out, Point from, int width, std::uint8_t colorIndex)
{
if (from.y < 0 || from.y >= out.h() || from.x >= out.w() || width <= 0 || from.x + width <= 0)
@ -296,36 +285,6 @@ int32_t GenerateRnd(int32_t v)
return AdvanceRndSeed() % v;
}
size_t GetFileSize(const char *pszName)
{
HANDLE file;
if (!SFileOpenFile(pszName, &file)) {
if (!gbQuietMode)
app_fatal("GetFileSize - SFileOpenFile failed for file:\n%s", pszName);
return 0;
}
const size_t fileLen = SFileGetFileSize(file);
SFileCloseFileThreadSafe(file);
return fileLen;
}
void LoadFileData(const char *pszName, byte *buffer, size_t fileLen)
{
HANDLE file;
if (!SFileOpenFile(pszName, &file)) {
if (!gbQuietMode)
app_fatal("LoadFileData - SFileOpenFile failed for file:\n%s", pszName);
return;
}
if (fileLen == 0)
app_fatal("Zero length SFILE:\n%s", pszName);
SFileReadFileThreadSafe(file, buffer, fileLen);
SFileCloseFileThreadSafe(file);
}
/**
* @brief Fade to black and play a video
* @param pszMovie file path of movie

100
Source/engine.h

@ -275,64 +275,6 @@ struct CelOutputBuffer {
void BlitFromSkipColorIndexZero(const CelOutputBuffer &src, SDL_Rect srcRect, Point targetPosition) const;
};
/**
* Stores a CEL or CL2 sprite and its width(s).
*
* The data may be unowned.
* Eventually we'd like to remove the unowned version.
*/
class CelSprite {
public:
CelSprite(std::unique_ptr<byte[]> data, int width)
: data_(std::move(data))
, data_ptr_(data_.get())
, width_(width)
{
}
CelSprite(std::unique_ptr<byte[]> data, const int *widths)
: data_(std::move(data))
, data_ptr_(data_.get())
, widths_(widths)
{
}
/**
* Constructs an unowned sprite.
* Ideally we'd like to remove all uses of this constructor.
*/
CelSprite(const byte *data, int width)
: data_ptr_(data)
, width_(width)
{
}
CelSprite(CelSprite &&) noexcept = default;
CelSprite &operator=(CelSprite &&) noexcept = default;
[[nodiscard]] const byte *Data() const
{
return data_ptr_;
}
[[nodiscard]] int Width(std::size_t frame = 1) const
{
return widths_ == nullptr ? width_ : widths_[frame];
}
private:
std::unique_ptr<byte[]> data_;
const byte *data_ptr_;
int width_ = 0;
const int *widths_ = nullptr; // unowned
};
/**
* @brief Loads a Cel sprite and sets its width
*/
CelSprite LoadCel(const char *pszName, int width);
CelSprite LoadCel(const char *pszName, const int *widths);
/**
* @brief Draw a horizontal line segment in the target buffer (left to right)
* @param out Target buffer
@ -391,48 +333,6 @@ int32_t GetRndSeed();
uint32_t GetLCGEngineState();
int32_t GenerateRnd(int32_t v);
size_t GetFileSize(const char *pszName);
void LoadFileData(const char *pszName, byte *buffer, size_t bufferSize);
template <typename T>
void LoadFileInMem(const char *path, T *data, std::size_t count = 0)
{
if (count == 0)
count = GetFileSize(path);
LoadFileData(path, reinterpret_cast<byte *>(data), count * sizeof(T));
}
template <typename T, std::size_t N>
void LoadFileInMem(const char *path, std::array<T, N> &data)
{
LoadFileInMem(path, &data, N);
}
/**
* @brief Load a file in to a buffer
* @param path Path of file
* @param elements Number of T elements read
* @return Buffer with content of file
*/
template <typename T = byte>
std::unique_ptr<T[]> LoadFileInMem(const char *path, size_t *elements = nullptr)
{
const size_t fileLen = GetFileSize(path);
if ((fileLen % sizeof(T)) != 0)
app_fatal("File size does not align with type\n%s", path);
if (elements != nullptr)
*elements = fileLen / sizeof(T);
std::unique_ptr<T[]> buf { new T[fileLen / sizeof(T)] };
LoadFileData(path, reinterpret_cast<byte *>(buf.get()), fileLen);
return buf;
}
void PlayInGameMovie(const char *pszMovie);
} // namespace devilution

2
Source/engine/animationinfo.h

@ -8,7 +8,7 @@
#include <stdint.h>
#include <type_traits>
#include "engine.h"
#include "engine/cel_sprite.hpp"
namespace devilution {

62
Source/engine/cel_sprite.hpp

@ -0,0 +1,62 @@
#pragma once
#include <memory>
#include <utility>
#include "utils/stdcompat/cstddef.hpp"
namespace devilution {
/**
* Stores a CEL or CL2 sprite and its width(s).
*
* The data may be unowned.
* Eventually we'd like to remove the unowned version.
*/
class CelSprite {
public:
CelSprite(std::unique_ptr<byte[]> data, int width)
: data_(std::move(data))
, data_ptr_(data_.get())
, width_(width)
{
}
CelSprite(std::unique_ptr<byte[]> data, const int *widths)
: data_(std::move(data))
, data_ptr_(data_.get())
, widths_(widths)
{
}
/**
* Constructs an unowned sprite.
* Ideally we'd like to remove all uses of this constructor.
*/
CelSprite(const byte *data, int width)
: data_ptr_(data)
, width_(width)
{
}
CelSprite(CelSprite &&) noexcept = default;
CelSprite &operator=(CelSprite &&) noexcept = default;
[[nodiscard]] const byte *Data() const
{
return data_ptr_;
}
[[nodiscard]] int Width(std::size_t frame = 1) const
{
return widths_ == nullptr ? width_ : widths_[frame];
}
private:
std::unique_ptr<byte[]> data_;
const byte *data_ptr_;
int width_ = 0;
const int *widths_ = nullptr; // unowned
};
} // namespace devilution

17
Source/engine/load_cel.cpp

@ -0,0 +1,17 @@
#include "engine/load_cel.hpp"
#include "engine/load_file.hpp"
namespace devilution {
CelSprite LoadCel(const char *pszName, int width)
{
return CelSprite(LoadFileInMem(pszName), width);
}
CelSprite LoadCel(const char *pszName, const int *widths)
{
return CelSprite(LoadFileInMem(pszName), widths);
}
} // namespace devilution

13
Source/engine/load_cel.hpp

@ -0,0 +1,13 @@
#pragma once
#include "engine/cel_sprite.hpp"
namespace devilution {
/**
* @brief Loads a Cel sprite and sets its width
*/
CelSprite LoadCel(const char *pszName, int width);
CelSprite LoadCel(const char *pszName, const int *widths);
} // namespace devilution

38
Source/engine/load_file.cpp

@ -0,0 +1,38 @@
#include "load_file.hpp"
#include "diablo.h"
#include "storm/storm.h"
namespace devilution {
size_t GetFileSize(const char *pszName)
{
HANDLE file;
if (!SFileOpenFile(pszName, &file)) {
if (!gbQuietMode)
app_fatal("GetFileSize - SFileOpenFile failed for file:\n%s", pszName);
return 0;
}
const size_t fileLen = SFileGetFileSize(file);
SFileCloseFileThreadSafe(file);
return fileLen;
}
void LoadFileData(const char *pszName, byte *buffer, size_t fileLen)
{
HANDLE file;
if (!SFileOpenFile(pszName, &file)) {
if (!gbQuietMode)
app_fatal("LoadFileData - SFileOpenFile failed for file:\n%s", pszName);
return;
}
if (fileLen == 0)
app_fatal("Zero length SFILE:\n%s", pszName);
SFileReadFileThreadSafe(file, buffer, fileLen);
SFileCloseFileThreadSafe(file);
}
} // namespace devilution

54
Source/engine/load_file.hpp

@ -0,0 +1,54 @@
#pragma once
#include <array>
#include <memory>
#include "appfat.h"
#include "utils/stdcompat/cstddef.hpp"
namespace devilution {
size_t GetFileSize(const char *pszName);
void LoadFileData(const char *pszName, byte *buffer, size_t bufferSize);
template <typename T>
void LoadFileInMem(const char *path, T *data, std::size_t count = 0)
{
if (count == 0)
count = GetFileSize(path);
LoadFileData(path, reinterpret_cast<byte *>(data), count * sizeof(T));
}
template <typename T, std::size_t N>
void LoadFileInMem(const char *path, std::array<T, N> &data)
{
LoadFileInMem(path, &data, N);
}
/**
* @brief Load a file in to a buffer
* @param path Path of file
* @param elements Number of T elements read
* @return Buffer with content of file
*/
template <typename T = byte>
std::unique_ptr<T[]> LoadFileInMem(const char *path, size_t *elements = nullptr)
{
const size_t fileLen = GetFileSize(path);
if ((fileLen % sizeof(T)) != 0)
app_fatal("File size does not align with type\n%s", path);
if (elements != nullptr)
*elements = fileLen / sizeof(T);
std::unique_ptr<T[]> buf { new T[fileLen / sizeof(T)] };
LoadFileData(path, reinterpret_cast<byte *>(buf.get()), fileLen);
return buf;
}
} // namespace devilution

1
Source/engine/render/cel_render.hpp

@ -8,6 +8,7 @@
#include <utility>
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "engine/point.hpp"
namespace devilution {

1
Source/engine/render/cl2_render.hpp

@ -9,6 +9,7 @@
#include <cstdint>
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "engine/point.hpp"
namespace devilution {

1
Source/engine/render/text_render.cpp

@ -8,6 +8,7 @@
#include "DiabloUI/ui_item.h"
#include "cel_render.hpp"
#include "engine.h"
#include "engine/load_cel.hpp"
#include "engine/point.hpp"
#include "palette.h"

1
Source/engine/render/text_render.hpp

@ -11,6 +11,7 @@
#include "DiabloUI/ui_item.h"
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "utils/stdcompat/optional.hpp"
namespace devilution {

2
Source/gendung.cpp

@ -3,7 +3,9 @@
*
* Implementation of general dungeon generation code.
*/
#include "gendung.h"
#include "engine/load_file.hpp"
#include "init.h"
#include "options.h"

1
Source/gendung.h

@ -9,6 +9,7 @@
#include <memory>
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "engine/point.hpp"
#include "scrollrt.h"
#include "utils/stdcompat/optional.hpp"

2
Source/gmenu.cpp

@ -9,6 +9,8 @@
#include "controls/axis_direction.h"
#include "controls/controller_motion.h"
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/render/cel_render.hpp"
#include "engine/render/text_render.hpp"
#include "stores.h"

2
Source/interfac.cpp

@ -10,6 +10,8 @@
#include "control.h"
#include "dx.h"
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/render/cel_render.hpp"
#include "hwcursor.hpp"
#include "init.h"

2
Source/inv.cpp

@ -9,6 +9,8 @@
#include <fmt/format.h>
#include "cursor.h"
#include "engine/cel_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/render/cel_render.hpp"
#include "engine/render/text_render.hpp"
#include "engine/size.hpp"

2
Source/items.cpp

@ -14,6 +14,8 @@
#include "cursor.h"
#include "doom.h"
#include "dx.h"
#include "engine/cel_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/render/cel_render.hpp"
#include "engine/render/text_render.hpp"
#include "init.h"

1
Source/lighting.cpp

@ -7,6 +7,7 @@
#include "automap.h"
#include "diablo.h"
#include "engine/load_file.hpp"
#include "player.h"
namespace devilution {

2
Source/minitext.cpp

@ -9,6 +9,8 @@
#include "control.h"
#include "dx.h"
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "engine/load_cel.hpp"
#include "engine/render/cel_render.hpp"
#include "engine/render/text_render.hpp"
#include "textdat.h"

1
Source/missiles.cpp

@ -10,6 +10,7 @@
#include "control.h"
#include "cursor.h"
#include "dead.h"
#include "engine/load_file.hpp"
#include "init.h"
#include "inv.h"
#include "lighting.h"

1
Source/monster.cpp

@ -16,6 +16,7 @@
#include "dead.h"
#include "drlg_l1.h"
#include "drlg_l4.h"
#include "engine/load_file.hpp"
#include "engine/render/cl2_render.hpp"
#include "init.h"
#include "lighting.h"

1
Source/monster.h

@ -10,6 +10,7 @@
#include "engine.h"
#include "engine/actor_position.hpp"
#include "engine/cel_sprite.hpp"
#include "engine/point.hpp"
#include "miniwin/miniwin.h"
#include "utils/stdcompat/optional.hpp"

1
Source/objects.cpp

@ -12,6 +12,7 @@
#include "cursor.h"
#include "drlg_l1.h"
#include "drlg_l4.h"
#include "engine/load_file.hpp"
#include "error.h"
#include "init.h"
#include "lighting.h"

2
Source/palette.cpp

@ -5,9 +5,9 @@
*/
#include "dx.h"
#include "engine/load_file.hpp"
#include "hwcursor.hpp"
#include "options.h"
#include "storm/storm.h"
#include "utils/display.h"
#include "utils/sdl_compat.h"

1
Source/player.cpp

@ -9,6 +9,7 @@
#include "control.h"
#include "cursor.h"
#include "dead.h"
#include "engine/load_file.hpp"
#include "gamemenu.h"
#include "init.h"
#include "lighting.h"

7
Source/player.h

@ -11,15 +11,16 @@
#include "diablo.h"
#include "engine.h"
#include "engine/actor_position.hpp"
#include "engine/animationinfo.h"
#include "engine/cel_sprite.hpp"
#include "engine/point.hpp"
#include "gendung.h"
#include "interfac.h"
#include "items.h"
#include "multi.h"
#include "spelldat.h"
#include "path.h"
#include "interfac.h"
#include "spelldat.h"
#include "utils/enum_traits.h"
#include "engine/animationinfo.h"
namespace devilution {

2
Source/quests.cpp

@ -3,11 +3,13 @@
*
* Implementation of functionality for handling quests.
*/
#include "quests.h"
#include <fmt/format.h>
#include "control.h"
#include "cursor.h"
#include "engine/load_file.hpp"
#include "engine/render/cel_render.hpp"
#include "engine/render/text_render.hpp"
#include "gendung.h"

1
Source/quests.h

@ -8,6 +8,7 @@
#include <cstdint>
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "engine/point.hpp"
#include "gendung.h"
#include "textdat.h"

1
Source/setmaps.cpp

@ -8,6 +8,7 @@
#include "drlg_l1.h"
#include "drlg_l2.h"
#include "drlg_l3.h"
#include "engine/load_file.hpp"
#include "objdat.h"
#include "objects.h"
#include "palette.h"

1
Source/stores.cpp

@ -10,6 +10,7 @@
#include <fmt/format.h>
#include "cursor.h"
#include "engine/load_cel.hpp"
#include "engine/render/cel_render.hpp"
#include "engine/render/text_render.hpp"
#include "init.h"

1
Source/stores.h

@ -7,6 +7,7 @@
#include "control.h"
#include "engine.h"
#include "engine/cel_sprite.hpp"
#include "utils/stdcompat/optional.hpp"
namespace devilution {

1
Source/town.cpp

@ -6,6 +6,7 @@
#include "town.h"
#include "drlg_l1.h"
#include "engine/load_file.hpp"
#include "init.h"
#include "player.h"
#include "quests.h"

1
Source/towners.cpp

@ -1,6 +1,7 @@
#include "towners.h"
#include "cursor.h"
#include "engine/load_file.hpp"
#include "inv.h"
#include "minitext.h"
#include "stores.h"

Loading…
Cancel
Save