Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbo Badger: Adjusted backend to new integration interface #236

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions code/addons/tbui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ add_shaders(tbui.fx)

fips_dir(backend)
fips_files(
tbuifontrenderer.cc
tbuibatch.h
tbuibitmap.cc
tbuibitmap.h
tbuiclipboard.cc
tbuifile.cc
tbuifile.h
tbuiclipboardinterface.cc
tbuiclipboardinterface.h
tbuifileinterface.cc
tbuifileinterface.h
tbuirenderer.cc
tbuirenderer.h
tbuisystem.cc
tbuisysteminterface.cc
tbuisysteminterface.h
tbuivertex.h
)

Expand Down
37 changes: 37 additions & 0 deletions code/addons/tbui/backend/tbuiclipboardinterface.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//------------------------------------------------------------------------------
// backend/tbuiclipboard.cc
// (C) 2024 Individual contributors, see AUTHORS file
//------------------------------------------------------------------------------
#include "render/stdneb.h"
#include "tbuiclipboardinterface.h"

namespace TBUI
{

// == TBClipboard =====================================

void
TBUIClipboardInterface::Empty()
{
clipboard.Clear();
}

bool
TBUIClipboardInterface::HasText()
{
return !clipboard.IsEmpty();
}

bool
TBUIClipboardInterface::SetText(const char* text)
{
return clipboard.Set(text);
}

bool
TBUIClipboardInterface::GetText(tb::TBStr& text)
{
return text.Set(clipboard);
}

} // namespace tb
23 changes: 23 additions & 0 deletions code/addons/tbui/backend/tbuiclipboardinterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//------------------------------------------------------------------------------
// backend/tbuiclipboard.cc
// (C) 2024 Individual contributors, see AUTHORS file
//------------------------------------------------------------------------------
#include "render/stdneb.h"
#include "platform/tb_clipboard_interface.h"

namespace TBUI
{

class TBUIClipboardInterface : public tb::TBClipboardInterface
{
public:
void Empty() override;
bool HasText() override;
bool SetText(const char* text) override;
bool GetText(tb::TBStr& text) override;

private:
tb::TBStr clipboard;
};

} // namespace tb
77 changes: 77 additions & 0 deletions code/addons/tbui/backend/tbuifileinterface.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//------------------------------------------------------------------------------
// backend/tbuifile.cc
// (C) 2024 Individual contributors, see AUTHORS file
//------------------------------------------------------------------------------
#include "render/stdneb.h"
#include "io/ioserver.h"
#include "io/stream.h"
#include "tbuifileinterface.h"

namespace
{
size_t
allocateFileId()
{
static size_t s_fileId = 0;
return ++s_fileId;
}
}

namespace TBUI
{
tb::TBFileHandle
TBUIFileInterface::Open(const char* filename, TBFileMode mode)
{
if (mode != TBFileMode::MODE_READ)
return 0;

Ptr<IO::FileStream> stream = IO::IoServer::Instance()->CreateStream(filename).downcast<IO::FileStream>();
stream->SetAccessMode(IO::Stream::AccessMode::ReadAccess);
if (!stream->Open())
{
return 0;
}

size_t fileId = allocateFileId();
openFiles.Add(fileId, stream);
return static_cast<tb::TBFileHandle>(fileId);
}

void
TBUIFileInterface::Close(tb::TBFileHandle file)
{
size_t fileId = static_cast<size_t>(file);

if (openFiles.Contains(fileId))
{
openFiles[fileId]->Close();
openFiles.Erase(fileId);
}
}

long
TBUIFileInterface::Size(tb::TBFileHandle file)
{
size_t fileId = static_cast<size_t>(file);

if (openFiles.Contains(fileId))
{
return openFiles[fileId]->GetSize();
}

return 0;
}

size_t
TBUIFileInterface::Read(tb::TBFileHandle file, void* buf, size_t elemSize, size_t count)
{
size_t fileId = static_cast<size_t>(file);

if (openFiles.Contains(fileId))
{
return openFiles[fileId]->Read(buf, count);
}

return 0;
}
}
38 changes: 38 additions & 0 deletions code/addons/tbui/backend/tbuifileinterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once
//------------------------------------------------------------------------------
/**
Turbobadger UI File interface

@copyright
(C) 2024 Individual contributors, see AUTHORS file
*/
//------------------------------------------------------------------------------

#include "io/filestream.h"
#include "util/string.h"
#include "util/dictionary.h"
#include "platform/tb_file_interface.h"

namespace TBUI
{
class TBUIFileInterface : public tb::TBFileInterface
{
public:
tb::TBFileHandle Open(const char* filename, TBFileMode mode) override;
void Close(tb::TBFileHandle file) override;

long Size(tb::TBFileHandle file) override;
size_t Read(tb::TBFileHandle file, void* buf, size_t elemSize, size_t count) override;

bool IsOpen(tb::TBFileHandle file) const;

private:
Util::Dictionary<size_t, Ptr<IO::FileStream>> openFiles;
};

inline bool
TBUIFileInterface::IsOpen(tb::TBFileHandle file) const
{
return openFiles.Contains(static_cast<size_t>(file));
}
}
101 changes: 101 additions & 0 deletions code/addons/tbui/backend/tbuifontrenderer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// ================================================================================
// == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
// == See tb_core.h for more information. ==
// ================================================================================

#include <math.h>
#include "tb_renderer.h"
#include "platform/tb_system_interface.h"
#include "tb_tempbuffer.h"
#include "tbuifontrenderer.h"

namespace TBUI
{
//#define STB_TRUETYPE_IMPLEMENTATION // force following include to generate
#include "stb_truetype.h"

TBUISTBFontRenderer::TBUISTBFontRenderer()
: render_data(nullptr)
{
}

TBUISTBFontRenderer::~TBUISTBFontRenderer()
{
stbtt_FreeBitmap(render_data, &font);
}

tb::TBFontMetrics
TBUISTBFontRenderer::GetMetrics()
{
tb::TBFontMetrics metrics;
int ascent, descent, lineGap;
stbtt_GetFontVMetrics(&font, &ascent, &descent, &lineGap);
metrics.ascent = (int)(ascent * scale + 0.5f);
metrics.descent = (int)((-descent) * scale + 0.5f);
metrics.height = (int)((ascent - descent + lineGap) * scale + 0.5f);
return metrics;
}

bool
TBUISTBFontRenderer::RenderGlyph(tb::TBFontGlyphData* data, UCS4 cp)
{
stbtt_FreeBitmap(render_data, &font);
render_data = stbtt_GetCodepointBitmap(&font, 0, scale, cp, &data->w, &data->h, 0, 0);
data->data8 = render_data;
data->stride = data->w;
data->rgb = false;
return data->data8 ? true : false;
}

void
TBUISTBFontRenderer::GetGlyphMetrics(tb::TBGlyphMetrics* metrics, UCS4 cp)
{
int advance, leftSideBearing;
const int gi = stbtt_FindGlyphIndex(&font, cp);
stbtt_GetGlyphHMetrics(&font, gi, &advance, &leftSideBearing);
metrics->advance = (int)roundf(advance * scale);

int ix0, iy0;
stbtt_GetGlyphBitmapBoxSubpixel(&font, gi, scale, scale, 0.f, 0.f, &ix0, &iy0, 0, 0);
metrics->x = ix0;
metrics->y = iy0;
}

int
TBUISTBFontRenderer::GetAdvance(UCS4 cp1, UCS4 cp2)
{
int advance, leftSideBearing;
const int gi1 = stbtt_FindGlyphIndex(&font, cp1);
stbtt_GetGlyphHMetrics(&font, gi1, &advance, &leftSideBearing);
if (font.kern)
advance += stbtt_GetGlyphKernAdvance(&font, gi1, stbtt_FindGlyphIndex(&font, cp2));
return (int)roundf(advance * scale);
}

bool
TBUISTBFontRenderer::Load(const char* filename, int size)
{
if (!ttf_buffer.AppendFile(filename))
return false;

const unsigned char* ttf_ptr = (const unsigned char*)ttf_buffer.GetData();
stbtt_InitFont(&font, ttf_ptr, stbtt_GetFontOffsetForIndex(ttf_ptr, 0));

scale = stbtt_ScaleForPixelHeight(&font, (float)size);
return true;
}

tb::TBFontFace*
TBUISTBFontRenderer::Create(tb::TBFontManager* font_manager, const char* filename, const tb::TBFontDescription& font_desc)
{
if (TBUISTBFontRenderer* fr = new TBUISTBFontRenderer())
{
if (fr->Load(filename, (int)font_desc.GetSize()))
if (tb::TBFontFace* font = new tb::TBFontFace(font_manager->GetGlyphCache(), fr, font_desc))
return font;
delete fr;
}
return nullptr;
}

} // namespace TBUI
37 changes: 37 additions & 0 deletions code/addons/tbui/backend/tbuifontrenderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ================================================================================
// == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
// == See tb_core.h for more information. ==
// ================================================================================

#include <math.h>
#include "tb_font_renderer.h"
#include "tb_renderer.h"
#include "platform/tb_system_interface.h"
#include "tb_tempbuffer.h"

namespace TBUI
{
/** STBFontRenderer renders fonts using stb_truetype.h (http://nothings.org/) */

class TBUISTBFontRenderer : public tb::TBFontRenderer
{
public:
TBUISTBFontRenderer();
~TBUISTBFontRenderer();

bool Load(const char* filename, int size);

virtual tb::TBFontFace* Create(tb::TBFontManager* font_manager, const char* filename, const tb::TBFontDescription& font_desc);

virtual tb::TBFontMetrics GetMetrics();
virtual bool RenderGlyph(tb::TBFontGlyphData* dst_bitmap, UCS4 cp);
virtual void GetGlyphMetrics(tb::TBGlyphMetrics* metrics, UCS4 cp);
virtual int GetAdvance(UCS4 cp1, UCS4 cp2);

private:
stbtt_fontinfo font;
tb::TBTempBuffer ttf_buffer;
unsigned char* render_data;
float scale;
};
}
54 changes: 54 additions & 0 deletions code/addons/tbui/backend/tbuisysteminterface.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//------------------------------------------------------------------------------
// backend/tbuisystem.cc
// (C) 2024 Individual contributors, see AUTHORS file
//------------------------------------------------------------------------------
#include "render/stdneb.h"
#include "core/sysfunc.h"
#include "timing/calendartime.h"
#include "tbuicontext.h"
#include "tbuisysteminterface.h"

namespace TBUI
{
void
TBUISystemInterface::DebugOut(const char* str)
{
Core::SysFunc::DebugOut(str);
}

double
TBUISystemInterface::GetTimeMS()
{
n_assert(TBUI::TBUIContext::state.timer.isvalid());
return TBUI::TBUIContext::state.timer->GetTicks();
}

void
TBUISystemInterface::RescheduleTimer(double fire_time)
{
}

int
TBUISystemInterface::GetLongClickDelayMS()
{
return 500;
}

int
TBUISystemInterface::GetPanThreshold()
{
return 40;
}

int
TBUISystemInterface::GetPixelsPerLine()
{
return 40;
}

int
TBUISystemInterface::GetDPI()
{
return 96;
}
}
Loading
Loading