Skip to content

Commit

Permalink
fixes and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed Jan 5, 2025
1 parent 9030513 commit 45f8b62
Show file tree
Hide file tree
Showing 33 changed files with 604 additions and 181 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ source/common/Module.cpp
source/common/Object.cpp
source/common/pixelformat.cpp
source/common/Reference.cpp
source/common/screen.cpp
source/common/Stream.cpp
source/common/types.cpp
source/common/Variant.cpp
Expand All @@ -428,9 +429,12 @@ source/modules/data/wrap_DataView.cpp
source/modules/event/Event.cpp
source/modules/event/wrap_Event.cpp
source/modules/filesystem/FileData.cpp
source/modules/filesystem/File.cpp
source/modules/filesystem/NativeFile.cpp
source/modules/filesystem/wrap_File.cpp
source/modules/filesystem/wrap_FileData.cpp
source/modules/filesystem/wrap_Filesystem.cpp
source/modules/filesystem/wrap_NativeFile.cpp
source/modules/font/Font.cpp
source/modules/font/GlyphData.cpp
source/modules/font/Rasterizer.cpp
Expand Down
11 changes: 5 additions & 6 deletions include/common/Exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ namespace love

#if __DEBUG__
#include <cstdio>
#define __FILENAME__ (__FILE__ + (sizeof(__FILE__) - 1) - sizeof(basename(__FILE__)))

// Macro to log to both stdout and a debug.log file
#define LOG(format, ...) \
do \
{ \
static const char* data = "[C++] %s %s:%d: " format "\n"; \
std::printf(data, __FILENAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
#define LOG(format, ...) \
do \
{ \
static const char* data = "[C++] %s %s:%d: " format "\n"; \
std::printf(data, __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
} while (0)
#endif
} // namespace love
5 changes: 3 additions & 2 deletions include/common/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ namespace love
// #region Filesystem
#define E_PHYSFS_NOT_INITIALIZED "PHYSFS is not initialized."
#define E_DATA_NOT_WRITTEN "Data could not be written."
#define E_COULD_NOT_OPEN_FILE "Could not open file at path {}."
#define E_PHYSFS_COULD_NOT_OPEN_FILE "Could not open file {} ({:s})"
#define E_COULD_NOT_OPEN_FILE "Could not open file at path {:s}."
#define E_PHYSFS_COULD_NOT_OPEN_FILE "Could not open file {:s} ({:s})"
#define E_FILE_NOT_OPEN_FOR_WRITING "File not open for writing."
#define E_FILE_NOT_OPEN_FOR_READING "File not open for reading."
#define E_INVALID_READ_SIZE "Invalid read size."
#define E_INVALID_WRITE_SIZE "Invalid write size."
#define E_NO_FILE_IN_LOVE_DIRS "\n\tno '{}' in LOVE game directories."
Expand Down
31 changes: 2 additions & 29 deletions include/common/screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,7 @@ namespace love

std::span<ScreenInfo> getScreenInfo();

const inline ScreenInfo& getScreenInfo(Screen id)
{
const auto& info = getScreenInfo();

id = (Screen)std::clamp<int8_t>(id, 0, info.size() - 1);

return info[id];
}

inline Screen getScreenId(std::string_view name)
{
const auto& info = getScreenInfo();

for (size_t i = 0; i < info.size(); ++i)
{
if (info[i].name == name)
return Screen(i);
}

return INVALID_SCREEN;
}

inline void setScreen(Screen id)
{
const auto& info = getScreenInfo();

id = (Screen)std::clamp<int8_t>(id, 0, info.size() - 1);
const ScreenInfo& getScreenInfo(Screen id);

currentScreen = id;
}
Screen getScreenId(const std::string& name);
} // namespace love
56 changes: 11 additions & 45 deletions include/modules/filesystem/File.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace love
static constexpr int64_t MAX_FILE_SIZE = 0x20000000000000LL;
static constexpr int64_t MAX_MODTIME = 0x20000000000000LL;

static constexpr int64_t SIZE_ALL = -1;

enum Mode
{
MODE_CLOSED,
Expand All @@ -40,7 +42,7 @@ namespace love
BUFFER_MAX_ENUM
};

FileBase(std::string_view filename) :
FileBase(const std::string& filename) :
filename(filename),
mode(MODE_CLOSED),
bufferMode(BUFFER_NONE),
Expand Down Expand Up @@ -82,60 +84,24 @@ namespace love

virtual bool isOpen() const = 0;

FileData* read(int64_t size)
{
bool isOpen = this->isOpen();

if (!isOpen && !this->open(MODE_READ))
throw love::Exception("Could not read file {}.", this->getFilename());

int64_t max = this->getSize();
int64_t current = this->tell();

if (size < 0)
throw love::Exception("Invalid read size.");

current = std::clamp(current, (int64_t)0, max);

if (current + size > max)
size = max - current;

StrongRef<FileData> data(new FileData(size, this->getFilename()), Acquire::NO_RETAIN);
int64_t bytesRead = this->read(data->getData(), size);

if (bytesRead < 0 || (bytesRead == 0 && bytesRead != size))
{
delete data;
throw love::Exception("Could not read from file");
}

if (bytesRead < size)
{
// clang-format off
StrongRef<FileData> temp(new FileData(bytesRead, this->getFilename()), Acquire::NO_RETAIN);
std::memcpy(temp->getData(), data->getData(), bytesRead);
// clang-format on

data = temp;
}

if (!isOpen)
this->close();

data->retain();
return data;
}
FileData* read(int64_t size);

FileData* read()
{
return this->read(this->getSize());
return this->read(SIZE_ALL);
}

Mode getMode() const
{
return this->mode;
}

virtual bool isEOF() = 0;

virtual bool setBuffer(BufferMode bufferMode, int64_t size) = 0;

virtual BufferMode getBuffer(int64_t& size) const = 0;

virtual const std::string& getFilename() const = 0;

std::string getExtension() const
Expand Down
51 changes: 51 additions & 0 deletions include/modules/filesystem/NativeFile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include "modules/filesystem/File.tcc"

namespace love
{
class NativeFile : public FileBase
{
public:
NativeFile(const std::string& filename, Mode mode);

virtual ~NativeFile();

NativeFile* clone() const override;

int64_t read(void* destination, int64_t size) override;

bool write(const void* data, int64_t size) override;

bool flush() override;

int64_t getSize() override;

int64_t tell() override;

bool seek(int64_t position, SeekOrigin origin) override;

using FileBase::read;
using FileBase::write;

bool open(Mode mode) override;

bool close() override;

bool isOpen() const override;

bool isEOF() override;

bool setBuffer(BufferMode bufferMode, int64_t size) override;

BufferMode getBuffer(int64_t& size) const override;

const std::string& getFilename() const override;

private:
NativeFile(const NativeFile& other);
static const char* getModeString(Mode mode);

FILE* file;
};
} // namespace love
10 changes: 5 additions & 5 deletions include/modules/filesystem/physfs/File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace love
class File : public FileBase
{
public:
File(std::string_view filename, Mode mode);
File(const std::string& filename, Mode mode);

virtual ~File();

Expand Down Expand Up @@ -37,13 +37,13 @@ namespace love

bool isOpen() const override;

bool isEOF();
bool isEOF() override;

bool setBuffer(BufferMode mode, int64_t size);
bool setBuffer(BufferMode mode, int64_t size) override;

BufferMode getBuffer(int64_t& size) const;
BufferMode getBuffer(int64_t& size) const override;

const std::string& getFilename() const;
const std::string& getFilename() const override;

private:
File(const File& other);
Expand Down
18 changes: 10 additions & 8 deletions include/modules/filesystem/physfs/Filesystem.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "modules/filesystem/Filesystem.tcc"
#include "modules/filesystem/NativeFile.hpp"
#include "modules/filesystem/physfs/File.hpp"

#include <map>
Expand Down Expand Up @@ -51,7 +52,9 @@ namespace love

bool unmountFullPath(const char* fullpath);

File* openFile(std::string_view filename, File::Mode mode) const;
FileBase* openFile(const std::string& filename, File::Mode mode) const;

FileBase* openNativeFile(const std::string& filepath, File::Mode mode) const;

std::string getFullCommonPath(CommonPath path);

Expand All @@ -75,13 +78,13 @@ namespace love

bool remove(const char* filepath);

FileData* read(std::string_view filename, int64_t size) const;
FileData* read(const std::string& filename, int64_t size) const;

FileData* read(std::string_view filename) const;
FileData* read(const std::string& filename) const;

void write(std::string_view filename, const void* data, int64_t size) const;
void write(const std::string& filename, const void* data, int64_t size) const;

void append(std::string_view filename, const void* data, int64_t size) const;
void append(const std::string& filename, const void* data, int64_t size) const;

bool getDirectoryItems(const char*, std::vector<std::string>& items);

Expand All @@ -101,9 +104,8 @@ namespace love
MountPermissions permissions;
};

bool mountCommonPathInternal(CommonPath path, const char* mountPoint,
MountPermissions permissions, bool appendToPath,
bool createDirectory);
bool mountCommonPathInternal(CommonPath path, const char* mountPoint, MountPermissions permissions,
bool appendToPath, bool createDirectory);

std::string currentDirectory;

Expand Down
2 changes: 2 additions & 0 deletions include/modules/filesystem/wrap_File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ namespace Wrap_File
int getFilename(lua_State* L);

int getExtension(lua_State* L);

extern const luaL_Reg w_File_functions[16];
} // namespace Wrap_File
4 changes: 3 additions & 1 deletion include/modules/filesystem/wrap_Filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace love

bool luax_cangetfiledata(lua_State* L, int idx);

File* luax_getfile(lua_State* L, int index);
FileBase* luax_getfile(lua_State* L, int index);

bool luax_cangetfile(lua_State* L, int idx);

Expand Down Expand Up @@ -52,6 +52,8 @@ namespace Wrap_Filesystem

int openFile(lua_State* L);

int openNativeFile(lua_State* L);

int getFullCommonPath(lua_State* L);

int getWorkingDirectory(lua_State* L);
Expand Down
9 changes: 9 additions & 0 deletions include/modules/filesystem/wrap_NativeFile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "common/luax.hpp"
#include "modules/filesystem/NativeFile.hpp"

namespace love
{
int open_nativefile(lua_State* L);
} // namespace love
24 changes: 24 additions & 0 deletions include/modules/image/ImageData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#include "common/Color.hpp"
#include "common/Data.hpp"
#include "common/Exception.hpp"
#include "common/Map.hpp"
#include "common/float.hpp"
#include "common/int.hpp"
#include "common/math.hpp"
#include "common/pixelformat.hpp"

#include "modules/filesystem/FileData.hpp"
Expand All @@ -14,6 +16,8 @@
#include "modules/image/FormatHandler.hpp"
#include "modules/image/ImageDataBase.hpp"

#define E_IMAGEDATA_NOT_MULTIPLE_OF_8 "ImageData only supports sizes that are multiples of 8."

namespace love
{
class ImageData : public ImageDataBase
Expand Down Expand Up @@ -88,6 +92,26 @@ namespace love
);
// clang-format on

template<typename T>
void copyBytesTiled(const void* buffer, const int width, const int height)
{
if (width % 8 != 0 && height % 8 != 0)
throw love::Exception(E_IMAGEDATA_NOT_MULTIPLE_OF_8);

const auto nextWidth = NextPo2(width);

auto* destination = (T*)this->data;
const auto* source = (const T*)buffer;

for (int j = 0; j < height; j += 8)
{
std::copy(source, source + width * 8, destination);

source += width * 8;
destination += nextWidth * 8;
}
}

private:
void create(int width, int height, PixelFormat format, void* data = nullptr);

Expand Down
Loading

0 comments on commit 45f8b62

Please sign in to comment.