Skip to content

Commit

Permalink
finish up some Font stuff, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed Jan 5, 2023
1 parent c46f221 commit 84997dc
Show file tree
Hide file tree
Showing 29 changed files with 169 additions and 68 deletions.
2 changes: 1 addition & 1 deletion include/common/luax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace luax
void RegisterTypeInner(lua_State* L, std::span<const luaL_Reg> values);

template<typename... T>
int RegisterType(lua_State* L, love::Type* type, T&&... values)
inline int RegisterType(lua_State* L, love::Type* type, T&&... values)
{
RegisterTypeInit(L, type);
(RegisterTypeInner(L, std::forward<T>(values)), ...);
Expand Down
4 changes: 1 addition & 3 deletions include/common/screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace love

enum Screen : int8_t;

static Screen currentScreen = (Screen)0;
inline Screen currentScreen = (Screen)0;
static constexpr Screen SCREEN_INVALID = (Screen)-1;

const ScreenInfo& GetScreenInfo(Screen);
Expand Down Expand Up @@ -49,15 +49,13 @@ namespace love
inline int GetScreenWidth(Screen id = currentScreen)
{
const auto& info = GetScreenInfo(id);
LOG("%d", info.width);

return info.width;
}

inline int GetScreenHeight(Screen id = currentScreen)
{
const auto& info = GetScreenInfo(id);
LOG("%d", info.height);

return info.height;
}
Expand Down
2 changes: 2 additions & 0 deletions include/objects/font/wrap_font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ namespace Wrap_Font
int Register(lua_State* L);

extern std::function<void(lua_State*)> wrap_extension;

extern std::span<const luaL_Reg> extensions;
} // namespace Wrap_Font
1 change: 1 addition & 0 deletions platform/cafe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ target_sources(${PROJECT_NAME} PRIVATE
source/objects/shader_ext.cpp
source/objects/source_ext.cpp
source/objects/texture_ext.cpp
source/objects/wrap_font_ext.cpp
source/objects/wrap_imagedata_ext.cpp
source/utilities/driver/dsp_ext.cpp
source/utilities/driver/framebuffer.cpp
Expand Down
2 changes: 1 addition & 1 deletion platform/cafe/include/common/screen_ext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace love
};
// clang-format on

void SetScreenSize(Screen id, int width, int height)
inline void SetScreenSize(Screen id, int width, int height)
{
auto& info = screenInfo[id];

Expand Down
18 changes: 13 additions & 5 deletions platform/cafe/include/modules/keyboard_ext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

#include <modules/keyboard/keyboard.tcc>

#include <objects/shader_ext.hpp>
#include <utilities/driver/framebuffer.hpp>

#include <coreinit/filesystem.h>
#include <coreinit/memdefaultheap.h>
#include <nn/swkbd.h>

#include <utilities/driver/framebuffer.hpp>
#include <gx2/context.h>
#include <nn/swkbd.h>

namespace love
{
Expand All @@ -22,19 +25,22 @@ namespace love

virtual ~Keyboard();

void Draw()
void Draw(GX2ContextState* restored)
{
if (!this->showing)
return;

nn::swkbd::DrawDRC();

GX2SetContextState(restored);
Shader<>::current->Attach(true);
}

void SetTextInput(const KeyboardOptions& options);

const uint32_t GetMaxEncodingLength(const uint32_t in)
{
return in * 0x03;
return in * 0x04;
}

const bool IsShowing() const
Expand All @@ -44,6 +50,7 @@ namespace love

void HideKeyboard()
{
nn::swkbd::DisappearInputForm();
this->showing = false;
}

Expand All @@ -58,12 +65,13 @@ namespace love
// clang-format on

private:
GX2ContextState* state;

nn::swkbd::CreateArg createArgs;
nn::swkbd::AppearArg appearArgs;
FSClient* client;

bool inited;
bool showing;
Screen screen;
};
} // namespace love
2 changes: 1 addition & 1 deletion platform/cafe/include/objects/shader_ext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace love

virtual ~Shader();

void Attach();
void Attach(bool forced = false);

static void AttachDefault(StandardShader type);

Expand Down
2 changes: 1 addition & 1 deletion platform/cafe/include/utilities/driver/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace love
}

private:
Vertex* data;
GX2RBuffer* buffer;
Vertex* data;
};

public:
Expand Down
1 change: 1 addition & 0 deletions platform/cafe/include/utilities/driver/framebuffer.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <common/screen_ext.hpp>
#include <common/vector.hpp>

#include <coreinit/memfrmheap.h>
Expand Down
32 changes: 20 additions & 12 deletions platform/cafe/source/modules/keyboard_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#include <utilities/result.hpp>

#include <codecvt>
#include <locale>
#include <string>
#include <utf8.h>

#include <malloc.h>

using namespace love;

Keyboard<Console::CAFE>::Keyboard() :
Keyboard<>(this->GetMaxEncodingLength(MAX_INPUT_LENGTH)),
state(nullptr),
createArgs {},
appearArgs {},
client(nullptr),
Expand All @@ -19,7 +20,10 @@ Keyboard<Console::CAFE>::Keyboard() :

void Keyboard<Console::CAFE>::Initialize()
{
this->state = (GX2ContextState*)memalign(GX2_CONTEXT_STATE_ALIGNMENT, sizeof(GX2ContextState));

this->client = (FSClient*)MEMAllocFromDefaultHeap(sizeof(FSClient));
GX2SetupContextStateEx(this->state, false);

if (!this->client)
throw love::Exception("Failed to allocate FSClient for nn::swkbd!");
Expand Down Expand Up @@ -52,11 +56,18 @@ Keyboard<Console::CAFE>::~Keyboard()

void Keyboard<Console::CAFE>::Utf16toUtf8Text()
{
const auto* str = nn::swkbd::GetInputFormString();
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> to_utf8;
const auto* line = nn::swkbd::GetInputFormString();
const auto utf8 = utf8::utf16to8(line);

std::copy_n(utf8.c_str(), utf8.size(), this->text.get());
}

static nn::swkbd::PasswordMode GetPasswordMode(bool isPassword)
{
if (isPassword)
return nn::swkbd::PasswordMode::Fade;

std::string converted = to_utf8.to_bytes(str);
std::copy_n(converted.c_str(), converted.size(), this->text.get());
return nn::swkbd::PasswordMode::Clear;
}

void Keyboard<Console::CAFE>::SetTextInput(const KeyboardOptions& options)
Expand All @@ -66,14 +77,11 @@ void Keyboard<Console::CAFE>::SetTextInput(const KeyboardOptions& options)

this->appearArgs.keyboardArg.configArg.languageType = nn::swkbd::LanguageType::English;

/* utf8 to utf16le needed */
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> to_utf16;
std::u16string hintText = to_utf16.from_bytes(options.hint.data());
const auto hintText = utf8::utf8to16(options.hint);

this->appearArgs.inputFormArg.hintText = hintText.c_str();
this->appearArgs.inputFormArg.maxTextLength = options.maxLength;
this->appearArgs.inputFormArg.passwordMode =
options.isPassword ? nn::swkbd::PasswordMode::Fade : nn::swkbd::PasswordMode::Clear;
this->appearArgs.inputFormArg.passwordMode = GetPasswordMode(options.isPassword);

if (!nn::swkbd::AppearInputForm(this->appearArgs))
return;
Expand Down
12 changes: 6 additions & 6 deletions platform/cafe/source/objects/font_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ const Font<Console::CAFE>::Glyph& Font<Console::CAFE>::AddGlyph(uint32_t glyph)
const vertex::Vertex vertices[4] =
{
/* x y z u v */
{{ -offset, -offset, 0.0f }, color, { vertex::normto16t((x - offset) / _width), vertex::normto16t((y - offset) / _height) }},
{{ -offset, (height + offset) / dpiScale, 0.0f }, color, { vertex::normto16t((x - offset) / _width), vertex::normto16t((y + height + offset) / _height) }},
{{ (width + offset) / dpiScale, (height + offset) / dpiScale, 0.0f }, color, { vertex::normto16t((x + width + offset) / _width), vertex::normto16t((y + height + offset) / _height) }},
{{ (width + offset) / dpiScale, -offset, 0.0f }, color, { vertex::normto16t((x + width + offset) / _width), vertex::normto16t((y - offset) / _height) }}
{{ -offset, -offset, 0.0f }, color, { float((x - offset) / _width), float((y - offset) / _height) }},
{{ -offset, (height + offset) / dpiScale, 0.0f }, color, { float((x - offset) / _width), float((y + height + offset) / _height) }},
{{ (width + offset) / dpiScale, (height + offset) / dpiScale, 0.0f }, color, { float((x + width + offset) / _width), float((y + height + offset) / _height) }},
{{ (width + offset) / dpiScale, -offset, 0.0f }, color, { float((x + width + offset) / _width), float((y - offset) / _height) }}
};
// clang-format on

Expand Down Expand Up @@ -641,7 +641,7 @@ std::vector<Font<Console::CAFE>::DrawCommand> Font<Console::CAFE>::GenerateVerti
auto previous = commands.back();

size_t total = (previous.start + previous.count);
if (previous.texture == first->texture && total == first->start)
if (previous.texture == first->texture && (int)total == first->start)
{
commands.back().count += first->count;
++first;
Expand Down Expand Up @@ -684,7 +684,7 @@ void Font<Console::CAFE>::Printv(Graphics<Console::CAFE>& graphics,
drawCommand.shader = Shader<>::STANDARD_TEXTURE;
drawCommand.format = vertex::CommonFormat::TEXTURE;
drawCommand.primitveType = vertex::PRIMITIVE_QUADS;
drawCommand.handles = { command.texture->GetHandle() };
drawCommand.handles = { command.texture };

matrix.TransformXYVert(drawCommand.Positions().get(), &vertices[command.start],
command.count);
Expand Down
4 changes: 2 additions & 2 deletions platform/cafe/source/objects/shader_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ void Shader<Console::CAFE>::AttachDefault(StandardShader type)
defaultshader->Attach();
}

void Shader<Console::CAFE>::Attach()
void Shader<Console::CAFE>::Attach(bool forced)
{
if (Shader::current != this)
if (Shader::current != this || forced)
{
Renderer<Console::CAFE>::Instance().UseProgram(this->program);
Renderer<>::shaderSwitches++;
Expand Down
42 changes: 42 additions & 0 deletions platform/cafe/source/objects/wrap_font_ext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <objects/font/wrap_font.hpp>

using namespace love;

int Wrap_Font::GetWrap(lua_State* L)
{
auto* self = Wrap_Font::CheckFont(L, 1);

std::vector<Font<>::ColoredString> text;
Wrap_Font::CheckColoredString(L, 2, text);

float wrap = luaL_checknumber(L, 3);

int maxWidth = 0;
std::vector<std::string> lines;
std::vector<int> widths;

luax::CatchException(L, [&]() { self->GetWrap(text, wrap, lines, &widths); });

for (const auto width : widths)
maxWidth = std::max(maxWidth, width);

lua_pushinteger(L, maxWidth);

lua_createtable(L, (int)lines.size(), 0);
for (size_t index = 0; index < lines.size(); index++)
{
lua_pushstring(L, lines[index].c_str());
lua_rawseti(L, -2, index + 1);
}

return 2;
}

// clang-format off
static constexpr luaL_Reg functions[] =
{
{ "getWrap", Wrap_Font::GetWrap }
};
// clang-format on

std::span<const luaL_Reg> Wrap_Font::extensions = functions;
4 changes: 2 additions & 2 deletions platform/cafe/source/objects/wrap_imagedata_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ int Wrap_ImageData::__MapPixelUnsafe(lua_State* L)
}

// clang-format off
static constexpr luaL_Reg extensions[] =
static constexpr luaL_Reg functions[] =
{
{ "_mapPixelUnsafe", Wrap_ImageData::__MapPixelUnsafe }
};
// clang-forma ton

std::span<const luaL_Reg> Wrap_ImageData::extensions = extensions;
std::span<const luaL_Reg> Wrap_ImageData::extensions = functions;
1 change: 0 additions & 1 deletion platform/cafe/source/utilities/driver/hid_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ void HID<Console::CAFE>::CheckSoftwareKeyboard(VPADStatus vpadStatus)

if (isOkButtonPressed || isCancelPressed)
{
nn::swkbd::DisappearInputForm();
Keyboard()->HideKeyboard();

if (isOkButtonPressed)
Expand Down
8 changes: 4 additions & 4 deletions platform/cafe/source/utilities/driver/renderer_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Renderer<Console::CAFE>::Renderer() :
GX2SetupContextStateEx(this->state, false);
GX2SetContextState(this->state);

GX2SetDepthOnlyControl(false, false, GX2_COMPARE_FUNC_ALWAYS);
GX2SetDepthOnlyControl(true, true, GX2_COMPARE_FUNC_ALWAYS);

GX2SetColorControl(GX2_LOGIC_OP_COPY, 0xFF, false, true);
GX2SetSwapInterval(1);
Expand Down Expand Up @@ -249,12 +249,12 @@ void Renderer<Console::CAFE>::BindFramebuffer(Texture<Console::CAFE>* texture)

void Renderer<Console::CAFE>::Present()
{
if (Keyboard() != nullptr)
Keyboard()->Draw(this->state);

/* flush commands before copying color buffers */
GX2Flush();

if (Keyboard() != nullptr)
Keyboard()->Draw();

/* copy our color buffers to their scan buffers */
for (auto& framebuffer : this->framebuffers)
framebuffer.second.CopyScanBuffer();
Expand Down
6 changes: 3 additions & 3 deletions platform/ctr/include/common/screen_ext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ namespace love
};

// clang-format off
static constexpr ScreenInfo screenInfo[0x03] =
inline constinit ScreenInfo screenInfo[0x03] =
{
{ Screen::LEFT, "left", 400, 240 },
{ Screen::RIGHT, "right", 400, 240 },
{ Screen::BOTTOM, "bottom", 320, 240 }
};

static constexpr ScreenInfo altScreenInfo[0x02] =
inline constinit ScreenInfo altScreenInfo[0x02] =
{
{ Screen::LEFT, "top", 400, 240 },
{ Screen::BOTTOM, "bottom", 320, 240 }
};

static constexpr ScreenInfo wideScreenInfo[0x02] =
inline constinit ScreenInfo wideScreenInfo[0x02] =
{
{ Screen::LEFT, "top", 800, 240 },
{ Screen::BOTTOM, "bottom", 320, 240 }
Expand Down
Loading

0 comments on commit 84997dc

Please sign in to comment.