Simplify folder sturcture + String refactor

1) Over time the total amount of files has decreased, and so it made sense to just simplify the folder structure and remove all of the subfolders.
2) Refactor the String class to utilize the existing Vector and StackVector classes instead of managing everything itself.
This commit is contained in:
Noel Berry
2022-10-01 13:29:51 -07:00
parent c94e372e7d
commit dcd3e11b16
47 changed files with 1018 additions and 1091 deletions

View File

@ -1,9 +1,9 @@
#include <blah/app.h>
#include <blah/common.h>
#include <blah/time.h>
#include "internal/internal.h"
#include "internal/platform.h"
#include "internal/renderer.h"
#include <blah_app.h>
#include <blah_common.h>
#include <blah_time.h>
#include "internal/blah_internal.h"
#include "internal/blah_platform.h"
#include "internal/blah_renderer.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
@ -400,8 +400,10 @@ void App::set_flag(u32 flag, bool enabled)
if (was != app_flags)
{
Internal::platform->set_app_flags(app_flags);
Internal::renderer->set_app_flags(app_flags);
if (Internal::platform)
Internal::platform->set_app_flags(app_flags);
if (Internal::renderer)
Internal::renderer->set_app_flags(app_flags);
}
}

View File

@ -1,10 +1,10 @@
#include <blah/images/aseprite.h>
#include <blah/filesystem.h>
#include <blah/math/calc.h>
#include <blah_aseprite.h>
#include <blah_filesystem.h>
#include <blah_calc.h>
#define STBI_NO_STDIO
#define STBI_ONLY_ZLIB
#include "../third_party/stb_image.h"
#include "third_party/stb_image.h"
using namespace Blah;
@ -133,7 +133,7 @@ void Aseprite::parse_layer(Stream& stream, int frame)
layer.alpha = stream.read_u8(Endian::Little);
stream.seek(stream.position() + 3); // for future
layer.name.set_length(stream.read_u16(Endian::Little));
layer.name.append('\0', stream.read_u16(Endian::Little));
stream.read(layer.name.cstr(), layer.name.length());
layer.userdata.color = 0xffffff;
@ -261,7 +261,7 @@ void Aseprite::parse_user_data(Stream& stream, int frame)
// has text
if (flags & (1 << 0))
{
m_last_userdata->text.set_length(stream.read_u16(Endian::Little));
m_last_userdata->text.append('\0', stream.read_u16(Endian::Little));
stream.read(m_last_userdata->text.cstr(), m_last_userdata->text.length());
}
@ -287,7 +287,7 @@ void Aseprite::parse_tag(Stream& stream, int frame)
tag.color = Color(stream.read_i8(), stream.read_i8(), stream.read_i8(Endian::Little), 255);
stream.seek(stream.position() + 1);
tag.name.set_length(stream.read_u16(Endian::Little));
tag.name.append('\0', stream.read_u16(Endian::Little));
stream.read(tag.name.cstr(), tag.name.length());
tags.push_back(tag);
@ -301,7 +301,7 @@ void Aseprite::parse_slice(Stream& stream, int frame)
stream.read_u32(Endian::Little); // reserved
String name;
name.set_length(stream.read_u16(Endian::Little));
name.append('\0', stream.read_u16(Endian::Little));
stream.read(name.cstr(), name.length());
for (int s = 0; s < count; s++)

View File

@ -1,7 +1,7 @@
#include <blah/drawing/batch.h>
#include <blah/math/calc.h>
#include <blah/app.h>
#include "../internal/internal.h"
#include <blah_batch.h>
#include <blah_calc.h>
#include <blah_app.h>
#include "internal/blah_internal.h"
using namespace Blah;
@ -969,11 +969,11 @@ void Batch::str(const SpriteFont& font, const String& text, const Vec2f& pos, co
offset.y -= font.height_of(text) * justify.y;
u32 last = 0;
for (int i = 0, l = text.length(); i < l; i += text.utf8_length(i))
Utf8 utf8(text.cstr());
int i = 0;
while (utf8.character)
{
u32 next = text.utf8_at(i);
if (next == '\n')
if (utf8.character == '\n')
{
offset.x = 0;
offset.y += font.line_height();
@ -982,22 +982,24 @@ void Batch::str(const SpriteFont& font, const String& text, const Vec2f& pos, co
offset.x -= font.width_of_line(text, i + 1) * justify.x;
last = 0;
continue;
}
const auto& ch = font[next];
if (ch.subtexture.texture)
else
{
Vec2f at = offset + ch.offset;
const auto& ch = font[utf8.character];
if (ch.subtexture.texture)
{
Vec2f at = offset + ch.offset;
if (last)
at.x += font.get_kerning(last, utf8.character);
tex(ch.subtexture, at, color);
}
if (i > 0 && text[i - 1] != '\n')
at.x += font.get_kerning(last, next);
tex(ch.subtexture, at, color);
offset.x += ch.advance;
last = utf8.character;
}
offset.x += ch.advance;
last = next;
i += utf8.character_size;
utf8.next();
}
pop_matrix();

View File

@ -1,7 +1,5 @@
#include <blah/common.h>
#include <blah/app.h>
#include <stdarg.h> // for logging methods
#include <stdio.h> // for sprintf
#include <blah_common.h>
#include <blah_app.h>
using namespace Blah;

View File

@ -1,5 +1,5 @@
#include <blah/filesystem.h>
#include "internal/internal.h"
#include <blah_filesystem.h>
#include "internal/blah_internal.h"
using namespace Blah;
@ -70,7 +70,10 @@ Vector<FilePath> Directory::enumerate(const FilePath& path, bool recursive)
{
App::Internal::platform->dir_enumerate(list, path.cstr(), recursive);
for (auto& it : list)
it.replace('\\', '/');
{
for (int n = 0; n < it.length(); n ++)
if (it[n] == '\\') it[n] = '/';
}
}
return list;
@ -114,10 +117,10 @@ FilePath Path::get_directory_name(const FilePath& path)
{
FilePath directory = path;
while (directory.ends_with("/"))
directory = directory.substr(0, -1);
directory = FilePath(directory.begin(), directory.end() - 1);
auto last = directory.last_index_of('/');
if (last >= 0)
directory = directory.substr(0, last + 1);
directory = FilePath(directory.begin(), directory.begin() + last + 1);
return directory;
}
@ -156,7 +159,7 @@ FilePath Path::normalize(const FilePath& path)
for (auto k = normalized.length() - 1; k > 0; k--)
if (normalized[k - 1] == '/')
{
normalized = normalized.substr(0, k - 1);
normalized = FilePath(normalized.begin(), normalized.begin() + k - 1);
could_move_up = true;
break;
}
@ -181,5 +184,5 @@ FilePath Path::join(const FilePath& a, const FilePath& b)
else if (b.length() <= 0)
return normalize(a);
else
return normalize(FilePath(a).append("/").append(b));
return normalize(a + "/" + b);
}

View File

@ -1,15 +1,15 @@
#include <blah/images/font.h>
#include <blah/math/calc.h>
#include <blah_font.h>
#include <blah_calc.h>
using namespace Blah;
#define STBTT_STATIC
#define STB_TRUETYPE_IMPLEMENTATION
#include "../third_party/stb_truetype.h"
#include "third_party/stb_truetype.h"
namespace
{
String get_font_name(stbtt_fontinfo* font, int nameId)
String blah_get_font_name(stbtt_fontinfo* font, int name_id)
{
int length = 0;
@ -18,14 +18,14 @@ namespace
STBTT_PLATFORM_ID_MICROSOFT,
STBTT_MS_EID_UNICODE_BMP,
STBTT_MS_LANG_ENGLISH,
nameId);
name_id);
// we want the size in wide chars
length /= 2;
String str;
if (length > 0)
str.append_utf16(ptr, ptr + length, Calc::is_little_endian());
str.append(ptr, ptr + length, Calc::is_little_endian());
return str;
}
}
@ -56,8 +56,8 @@ FontRef Font::create(Stream& stream)
auto font = FontRef(new Font());
font->m_font = stbtt;
font->m_buffer = std::move(buffer);
font->m_family_name = get_font_name(fn, 1);
font->m_style_name = get_font_name(fn, 2);
font->m_family_name = blah_get_font_name(fn, 1);
font->m_style_name = blah_get_font_name(fn, 2);
stbtt_GetFontVMetrics(fn, &font->m_ascent, &font->m_descent, &font->m_line_gap);
return font;
}

View File

@ -1,5 +1,6 @@
#include <blah/graphics.h>
#include "internal/internal.h"
#include <blah_graphics.h>
#include <blah_stream.h>
#include "internal/blah_internal.h"
using namespace Blah;

View File

@ -1,14 +1,14 @@
#include <blah/images/image.h>
#include <blah_image.h>
using namespace Blah;
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_JPEG
#define STBI_ONLY_PNG
#include "../third_party/stb_image.h"
#include "third_party/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "../third_party/stb_image_write.h"
#include "third_party/stb_image_write.h"
namespace
{

View File

@ -1,11 +1,10 @@
#include <blah/input.h>
#include <blah/app.h>
#include <blah/time.h>
#include <blah/common.h>
#include <blah/math/calc.h>
#include "internal/internal.h"
#include "internal/platform.h"
#include <cstring>
#include <blah_input.h>
#include <blah_app.h>
#include <blah_time.h>
#include <blah_common.h>
#include <blah_calc.h>
#include "internal/blah_internal.h"
#include "internal/blah_platform.h"
using namespace Blah;
@ -33,9 +32,9 @@ void Input::Internal::init()
Input::last_state = g_empty_state;
Input::state = g_empty_state;
g_buttons.dispose();
g_axes.dispose();
g_sticks.dispose();
g_buttons = Vector<Ref<ButtonBinding>>();
g_axes = Vector<Ref<AxisBinding>>();
g_sticks = Vector<Ref<StickBinding>>();
}
void Input::Internal::shutdown()

View File

@ -1,6 +1,5 @@
#include <blah/images/packer.h>
#include <blah_packer.h>
#include <algorithm>
#include <cstring>
using namespace Blah;

View File

@ -1,5 +1,5 @@
#include <blah/drawing/spritefont.h>
#include <blah/images/packer.h>
#include <blah_spritefont.h>
#include <blah_packer.h>
using namespace Blah;
@ -46,21 +46,25 @@ float SpriteFont::width_of(const String& text) const
float line_width = 0;
Codepoint last = 0;
for (int i = 0; i < text.length(); i += text.utf8_length(i))
Utf8 utf8(text.cstr());
while (utf8.character)
{
if (text[i] == '\n')
if (utf8.character == '\n')
{
line_width = 0;
continue;
}
else
{
line_width += get_character(utf8.character).advance;
if (last)
line_width += get_kerning(last, utf8.character);
if (line_width > width)
width = line_width;
last = utf8.character;
}
auto next = text.utf8_at(i);
line_width += get_character(next).advance;
if (i > 0)
line_width += get_kerning(last, next);
if (line_width > width)
width = line_width;
last = next;
utf8.next();
}
return width;
@ -72,18 +76,19 @@ float SpriteFont::width_of_line(const String& text, int start) const
if (start >= text.length()) return 0;
float width = 0;
Codepoint last = 0;
for (auto i = start; i < text.length(); i += text.utf8_length(i))
{
if (text[i] == '\n')
return width;
auto next = text.utf8_at(i);
width += get_character(next).advance;
if (i > 0)
width += get_kerning(last, next);
last = next;
Utf8 utf8(text.cstr());
while (utf8.character)
{
if (utf8.character == '\n')
break;
width += get_character(utf8.character).advance;
if (last)
width += get_kerning(last, utf8.character);
last = utf8.character;
utf8.next();
}
return width;
@ -95,10 +100,13 @@ float SpriteFont::height_of(const String& text) const
return 0;
float height = line_height();
for (auto i = 0; i < text.length(); i += text.utf8_length(i))
Utf8 utf8(text.cstr());
while (utf8.character)
{
if (text[i] == '\n')
if (utf8.character == '\n')
height += line_height();
utf8.next();
}
return height - line_gap;

View File

@ -1,6 +1,5 @@
#include <blah/stream.h>
#include <blah/containers/str.h>
#include <string.h>
#include <blah_stream.h>
#include <blah_string.h>
using namespace Blah;
@ -47,7 +46,7 @@ String Stream::read_string(int length)
}
else
{
result.set_length(length);
result.append('\0', length);
read(result.cstr(), length);
}

360
src/blah_string.cpp Normal file
View File

@ -0,0 +1,360 @@
#include <blah_string.h>
using namespace Blah;
namespace
{
char blah_to_lower(char c)
{
if (c >= 'A' && c <= 'Z') return c - 'A' + 'a';
return c;
}
int blah_strlen(const char* cstr)
{
int len = 0;
if (cstr)
while (*(cstr + len) != '\0' && len < INT32_MAX) len++;
return len;
}
}
void BaseString::assign(const char* cstr, const char* cstr_end)
{
s_clear();
append(cstr, cstr_end);
}
void BaseString::append(const char* cstr, const char* cstr_end)
{
// make sure values are valid
if (cstr == nullptr || *cstr == '\0')
return;
if (cstr_end == nullptr)
cstr_end = cstr + blah_strlen(cstr);
// reserve (+1 for null-terminator)
auto len = length();
s_ensure(len + (cstr_end - cstr) + 1);
// copy value over to our buffer
char* dst = s_ptr() + len;
while (cstr < cstr_end)
*(dst++) = *(cstr++);
}
void BaseString::append(const u16* u16_cstr, const u16* u16_cstr_end, bool swap_endian)
{
// converts utf16 into utf8
// more info: https://en.wikipedia.org/wiki/UTF-16#Description
const u16 surrogate_min = 0xd800u;
const u16 surrogate_max = 0xdbffu;
while ((u16_cstr_end == nullptr && *u16_cstr != 0) || (u16_cstr_end != nullptr && u16_cstr != u16_cstr_end))
{
u16 next = (*u16_cstr++);
if (swap_endian)
next = ((next & 0xff) << 8 | ((next & 0xff00) >> 8));
u32 cp = 0xffff & next;
if ((cp >= surrogate_min && cp <= surrogate_max))
{
next = (*u16_cstr++);
if (swap_endian)
next = ((next & 0xff) << 8 | ((next & 0xff00) >> 8));
u32 trail = 0xffff & next;
cp = (cp << 10) + trail + 0x10000u - (surrogate_min << 10) - 0xdc00u;
}
append(cp);
}
}
void BaseString::append(char ch, int count)
{
// reserve (+1 for null-terminator)
auto len = length();
s_ensure(len + count + 1);
// copy value over to our buffer
char* dst = s_ptr() + len;
char* end = dst + count;
while (dst < end)
*(dst++) = ch;
}
void BaseString::append(u32 c)
{
// one octet
if (c < 0x80)
{
append((char)c);
}
// two octets
else if (c < 0x800)
{
append((char)((c >> 6) | 0xc0));
append((char)((c & 0x3f) | 0x80));
}
// three octets
else if (c < 0x10000)
{
append((char)((c >> 12) | 0xe0));
append((char)(((c >> 6) & 0x3f) | 0x80));
append((char)((c & 0x3f) | 0x80));
}
// four octets
else
{
append((char)((c >> 18) | 0xf0));
append((char)(((c >> 12) & 0x3f) | 0x80));
append((char)(((c >> 6) & 0x3f) | 0x80));
append((char)((c & 0x3f) | 0x80));
}
}
void BaseString::append_fmt(const char* fmt, ...)
{
va_list args;
// determine arg m_length
va_start(args, fmt);
auto add = vsnprintf(nullptr, 0, fmt, args);
va_end(args);
if (add <= 0)
return;
// reserve (+1 for null-terminator)
auto len = length();
s_ensure(len + add + 1);
// print out
va_start(args, fmt);
vsnprintf(s_ptr() + len, add + 1, fmt, args);
va_end(args);
}
bool BaseString::starts_with(const char* cstr, bool ignore_case) const
{
if (cstr == nullptr || *cstr == '\0')
return length() == 0;
const char* ptr = s_ptr();
if (ignore_case)
{
while (*cstr != '\0')
{
if (blah_to_lower(*cstr) != blah_to_lower(*ptr))
return false;
cstr++; ptr++;
}
}
else
{
while (*cstr != '\0')
{
if (*cstr != *ptr)
return false;
cstr++; ptr++;
}
}
return true;
}
bool BaseString::ends_with(const char* cstr, bool ignore_case) const
{
if (cstr == nullptr || *cstr == '\0')
return length() == 0;
int len = blah_strlen(cstr);
if (len > length())
return false;
const char* ptr = s_ptr() + length() - len;
if (ignore_case)
{
while (*cstr != '\0')
{
if (blah_to_lower(*cstr) != blah_to_lower(*ptr))
return false;
cstr++; ptr++;
}
}
else
{
while (*cstr != '\0')
{
if (*cstr != *ptr)
return false;
cstr++; ptr++;
}
}
return true;
}
bool BaseString::contains(const char* cstr, bool ignore_case) const
{
if (cstr == nullptr || *cstr == '\0')
return length() == 0;
int len = blah_strlen(cstr);
if (len > length())
return false;
const char* ptr = s_ptr();
const char* end = s_ptr() + len;
while (ptr < end)
{
const char* at = ptr;
bool match = true;
if (ignore_case)
{
while (*cstr != '\0')
{
if (blah_to_lower(*cstr) != blah_to_lower(*at))
{
match = false;
break;
}
cstr++; at++;
}
}
else
{
while (*cstr != '\0')
{
if (*cstr != *at)
{
match = false;
break;
}
cstr++; at++;
}
}
if (match)
return true;
}
return false;
}
int BaseString::first_index_of(char ch) const
{
const char* ptr = s_ptr();
const char* end = ptr + length();
while (ptr < end)
{
if (*ptr == ch)
return ptr - s_ptr();
ptr++;
}
return -1;
}
int BaseString::last_index_of(char ch) const
{
const char* ptr = s_ptr();
const char* end = ptr + length() - 1;
while (end >= ptr)
{
if (*end == ch)
return end - s_ptr();
end--;
}
return -1;
}
bool BaseString::equals(const char* other, bool ignore_case) const
{
const char* a = s_ptr(); const char* b = other;
if (ignore_case)
{
while (blah_to_lower(*a) == blah_to_lower(*b) && *a != '\0') { a++; b++; }
return blah_to_lower(*a) == blah_to_lower(*b);
}
else
{
while (*a == *b && *a != '\0') { a++; b++; }
return *a == *b;
}
}
namespace
{
u32 utf8_character_at(const char* str)
{
u32 charcode = 0;
int t = (unsigned char)(*(str++));
if (t < 128)
return t;
int high_bit_mask = (1 << 6) - 1;
int high_bit_shift = 0;
int total_bits = 0;
int other_bits = 6;
while ((t & 0xC0) == 0xC0)
{
t <<= 1;
t &= 0xff;
total_bits += 6;
high_bit_mask >>= 1;
high_bit_shift++;
charcode <<= other_bits;
charcode |= ((unsigned char)(*(str++))) & ((1 << other_bits) - 1);
}
charcode |= ((t >> high_bit_shift) & high_bit_mask) << total_bits;
return charcode;
}
u32 utf8_character_size(const char* str)
{
char c = *str;
if (c == '\0')
return 0;
if ((c & 0xFE) == 0xFC)
return 6;
if ((c & 0xFC) == 0xF8)
return 5;
if ((c & 0xF8) == 0xF0)
return 4;
if ((c & 0xF0) == 0xE0)
return 3;
if ((c & 0xE0) == 0xC0)
return 2;
return 1;
}
}
Utf8::Utf8(const char* cstr)
: str(cstr)
{
character = utf8_character_at(str);
character_size = utf8_character_size(str);
}
bool Utf8::next()
{
str += character_size;
character = utf8_character_at(str);
character_size = utf8_character_size(str);
return character_size > 0;
}

View File

@ -1,5 +1,5 @@
#include <blah/drawing/subtexture.h>
#include <blah/math/calc.h>
#include <blah_subtexture.h>
#include <blah_calc.h>
using namespace Blah;

View File

@ -1,5 +1,5 @@
#include <blah/time.h>
#include "internal/internal.h"
#include <blah_time.h>
#include "internal/blah_internal.h"
using namespace Blah;

View File

@ -1,492 +0,0 @@
#include <blah/containers/str.h>
#include <string.h> // for strcpy etc
#include <stdarg.h> // for format methods
#include <stdio.h> // for sprintf
#include <cctype> // toupper
using namespace Blah;
char Str::empty_buffer[1] = { '\0' };
bool Str::operator==(const Str& rhs) const
{
return strcmp(cstr(), rhs.cstr()) == 0;
}
bool Str::operator!=(const Str& rhs) const
{
return strcmp(cstr(), rhs.cstr()) != 0;
}
bool Str::operator==(const char* rhs) const
{
return strcmp(cstr(), rhs) == 0;
}
bool Str::operator!=(const char* rhs) const
{
return strcmp(cstr(), rhs) != 0;
}
void Str::reserve(int size)
{
int buffer_length = size + 1;
if (buffer_length > m_capacity)
{
if (m_capacity <= 0)
m_capacity = 16;
while (m_capacity < buffer_length)
m_capacity *= 2;
// expand from local buffer
if (m_buffer == nullptr)
{
char* local = data();
m_buffer = new char[m_capacity];
memcpy(m_buffer, local, m_local_size);
m_buffer[m_local_size] = '\0';
}
// expand from empty buffer
else if (m_buffer == empty_buffer)
{
m_buffer = new char[m_capacity];
m_buffer[0] = '\0';
}
// expand from existing heap buffer
else
{
char* new_buffer = new char[m_capacity];
memcpy(new_buffer, m_buffer, m_length);
new_buffer[m_length] = '\0';
delete[] m_buffer;
m_buffer = new_buffer;
}
}
}
void Str::set_length(int len)
{
reserve(len);
data()[len] = '\0';
m_length = len;
}
u32 Str::utf8_at(int index) const
{
u32 charcode = 0;
int t = (unsigned char)(this->operator[](index++));
if (t < 128)
return t;
int high_bit_mask = (1 << 6) - 1;
int high_bit_shift = 0;
int total_bits = 0;
int other_bits = 6;
while ((t & 0xC0) == 0xC0)
{
t <<= 1;
t &= 0xff;
total_bits += 6;
high_bit_mask >>= 1;
high_bit_shift++;
charcode <<= other_bits;
charcode |= ((unsigned char)(this->operator[](index++))) & ((1 << other_bits) - 1);
}
charcode |= ((t >> high_bit_shift) & high_bit_mask) << total_bits;
return charcode;
}
int Str::utf8_length(int index) const
{
auto c = this->operator[](index);
if ((c & 0xFE) == 0xFC)
return 6;
if ((c & 0xFC) == 0xF8)
return 5;
if ((c & 0xF8) == 0xF0)
return 4;
else if ((c & 0xF0) == 0xE0)
return 3;
else if ((c & 0xE0) == 0xC0)
return 2;
return 1;
}
Str& Str::append(char c)
{
reserve(m_length + 1);
data()[m_length++] = c;
data()[m_length] = '\0';
return *this;
}
Str& Str::append(u32 c)
{
// one octet
if (c < 0x80)
{
append((char)c);
}
// two octets
else if (c < 0x800)
{
append((char)((c >> 6) | 0xc0));
append((char)((c & 0x3f) | 0x80));
}
// three octets
else if (c < 0x10000)
{
append((char)((c >> 12) | 0xe0));
append((char)(((c >> 6) & 0x3f) | 0x80));
append((char)((c & 0x3f) | 0x80));
}
// four octets
else
{
append((char)((c >> 18) | 0xf0));
append((char)(((c >> 12) & 0x3f) | 0x80));
append((char)(((c >> 6) & 0x3f) | 0x80));
append((char)((c & 0x3f) | 0x80));
}
return *this;
}
Str& Str::append(const char* start, const char* end)
{
if (end == nullptr)
end = start + strlen(start);
int add = (int)(end - start);
if (add <= 0)
return *this;
reserve(m_length + add);
memcpy(data() + m_length, start, add);
m_length += add;
data()[m_length] = '\0';
return *this;
}
Str& Str::append(const Str& str, int start, int end)
{
if (end < 0) end = str.m_length;
if (end > str.m_length) end = str.m_length;
if (start < 0) start = 0;
if (start > end) start = end;
return append(str.begin() + start, str.begin() + end);
}
Str& Str::append_fmt(const char* fmt, ...)
{
int add, diff;
// determine arg m_length
va_list args;
va_start(args, fmt);
add = vsnprintf(NULL, 0, fmt, args);
va_end(args);
// reserve
reserve(m_length + add);
diff = m_capacity - m_length;
if (diff <= 0) diff = 0;
// print out
va_start(args, fmt);
vsnprintf(data() + m_length, (size_t)diff, fmt, args);
va_end(args);
// increment size
m_length += add;
data()[m_length] = '\0';
return *this;
}
Str& Str::append_utf16(const u16* start, const u16* end, bool swap_endian)
{
// converts utf16 into utf8
// more info: https://en.wikipedia.org/wiki/UTF-16#Description
const u16 surrogate_min = 0xd800u;
const u16 surrogate_max = 0xdbffu;
while ((end == nullptr && *start != 0) || (end != nullptr && start != end))
{
u16 next = (*start++);
if (swap_endian)
next = ((next & 0xff) << 8 | ((next & 0xff00) >> 8));
u32 cp = 0xffff & next;
if ((cp >= surrogate_min && cp <= surrogate_max))
{
next = (*start++);
if (swap_endian)
next = ((next & 0xff) << 8 | ((next & 0xff00) >> 8));
u32 trail = 0xffff & next;
cp = (cp << 10) + trail + 0x10000u - (surrogate_min << 10) - 0xdc00u;
}
append(cp);
}
return *this;
}
Str& Str::trim()
{
if (m_length > 0)
{
const char* s = begin();
const char* e = end() - 1;
while (isspace(*s) && s != e)
s++;
while (isspace(*e) && s != e)
e--;
set(s, e + 1);
}
return *this;
}
bool Str::starts_with(const char* str, bool ignoreCase) const
{
if (str == nullptr)
return m_length == 0;
int len = (int)strlen(str);
if (len > m_length)
return false;
const char* a = data();
const char* b = str;
if (ignoreCase)
{
for (int n = 0; n < len; n++)
if (tolower(a[n]) != tolower(b[n]))
return false;
}
else
{
for (int n = 0; n < len; n++)
if (a[n] != b[n])
return false;
}
return true;
}
bool Str::contains(const char* str, bool ignoreCase) const
{
int len = (int)strlen(str);
if (len > m_length || len <= 0)
return false;
const char* a = data();
const char* b = str;
for (int start = 0; start < m_length - len + 1; start++)
{
bool match = true;
if (ignoreCase)
{
for (int n = 0; n < len && match; n++)
if (tolower(a[start + n]) != tolower(b[n]))
match = false;
}
else
{
for (int n = 0; n < len && match; n++)
if (a[start + n] != b[n])
match = false;
}
if (match)
return true;
}
return false;
}
bool Str::ends_with(const char* str, bool ignoreCase) const
{
if (str == nullptr)
return m_length == 0;
int len = (int)strlen(str);
if (len > m_length || len <= 0)
return false;
const char* a = data();
const char* b = str;
if (ignoreCase)
{
for (int n = m_length - len, i = 0; n < m_length; n++, i++)
if (tolower(a[n]) != tolower(b[i]))
return false;
}
else
{
for (int n = m_length - len, i = 0; n < m_length; n++, i++)
if (a[n] != b[i])
return false;
}
return true;
}
int Str::first_index_of(char ch) const
{
const char* ptr = data();
for (int n = 0; n < m_length; n++)
if (ptr[n] == ch)
return n;
return -1;
}
int Str::last_index_of(char ch) const
{
const char* ptr = data();
for (int n = m_length - 1; n >= 0; n--)
if (ptr[n] == ch)
return n;
return -1;
}
String Str::substr(int start) const
{
if (start < 0) start = 0;
if (start > m_length) start = m_length;
return String(data() + start);
}
String Str::substr(int start, int end) const
{
if (start < 0) start = 0;
if (start > m_length) start = m_length;
if (end < 0) end = m_length + end;
if (end < start) end = start;
if (end > m_length) end = m_length;
return Str(data() + start, data() + end);
}
Vector<String> Str::split(char ch) const
{
Vector<String> result;
const char* ptr = data();
int last = 0;
int index = 1;
while (index < m_length)
{
if (ptr[index] == ch)
{
result.push_back(substr(last, index));
last = index + 1;
}
index++;
}
if (last < index)
result.push_back(substr(last, index));
return result;
}
Str& Str::replace(const Str& os, const Str& ns)
{
for (int i = 0; i < m_length - os.m_length + 1; i++)
{
if (strncmp(data() + i, os.data(), os.m_length) == 0)
{
if (ns.m_length > os.m_length)
reserve(ns.m_length - os.m_length);
memmove(data() + i + ns.m_length, data() + i + os.m_length, m_length - i - os.m_length);
memcpy(data() + i, ns.cstr(), ns.m_length);
set_length(m_length + ns.m_length - os.m_length);
i += os.m_length - 1;
}
}
return *this;
}
Str& Str::replace(char c, char r)
{
char* ptr = data();
for (int n = 0; n < m_length; n++)
if (ptr[n] == c)
ptr[n] = r;
return *this;
}
void Str::clear()
{
if (m_capacity > 0)
data()[0] = '\0';
m_length = 0;
}
void Str::dispose()
{
if (m_buffer != nullptr && m_buffer != empty_buffer)
delete[] m_buffer;
if (m_local_size > 0)
{
m_buffer = nullptr;
m_capacity = m_local_size;
data()[0] = '\0';
}
else
{
m_buffer = empty_buffer;
m_capacity = 0;
}
m_length = 0;
}
void Str::set(const char* start, const char* end)
{
// find the end
if (end == nullptr)
end = start + strlen(start);
// make sure it actually contains characters
int len = (int)(end - start);
if (len <= 0)
{
clear();
}
else
{
m_length = len;
// reserve
reserve(m_length);
// copy the data over
char* ptr = data();
memcpy(ptr, start, m_length);
ptr[m_length] = '\0';
}
}

View File

@ -1,6 +1,6 @@
#pragma once
#include "renderer.h"
#include "platform.h"
#include "blah_renderer.h"
#include "blah_platform.h"
#define BLAH_ASSERT_RENDERER() BLAH_ASSERT(App::Internal::renderer, "Renderer has not been created")
#define BLAH_ASSERT_PLATFORM() BLAH_ASSERT(App::Internal::platform, "Platform has not been created")

View File

@ -1,8 +1,8 @@
#pragma once
#include <blah/common.h>
#include <blah/input.h>
#include <blah/filesystem.h>
#include <blah/containers/vector.h>
#include <blah_common.h>
#include <blah_input.h>
#include <blah_filesystem.h>
#include <blah_vector.h>
namespace Blah
{

View File

@ -1,13 +1,13 @@
#ifdef BLAH_PLATFORM_SDL2
#include "platform.h"
#include "renderer.h"
#include "internal.h"
#include <blah/input.h>
#include <blah/app.h>
#include <blah/filesystem.h>
#include <blah/common.h>
#include <blah/time.h>
#include "blah_platform.h"
#include "blah_renderer.h"
#include "blah_internal.h"
#include <blah_input.h>
#include <blah_app.h>
#include <blah_filesystem.h>
#include <blah_common.h>
#include <blah_time.h>
#include <SDL.h>

View File

@ -3,13 +3,13 @@
// Note:
// This is unfinished! It is missing Controller Support!
#include "platform.h"
#include "internal.h"
#include <blah/input.h>
#include <blah/app.h>
#include <blah/filesystem.h>
#include <blah/common.h>
#include <blah/time.h>
#include "blah_platform.h"
#include "blah_internal.h"
#include <blah_input.h>
#include <blah_app.h>
#include <blah_filesystem.h>
#include <blah_common.h>
#include <blah_time.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

View File

@ -1,7 +1,7 @@
#pragma once
#include <blah/app.h>
#include <blah/graphics.h>
#include <blah/math/color.h>
#include <blah_app.h>
#include <blah_graphics.h>
#include <blah_color.h>
namespace Blah
{

View File

@ -3,10 +3,10 @@
// TODO:
// Note the D3D11 Implementation is still a work-in-progress
#include "renderer.h"
#include "internal.h"
#include "platform.h"
#include <blah/common.h>
#include "blah_renderer.h"
#include "blah_internal.h"
#include "blah_platform.h"
#include <blah_common.h>
#include <cstdio>
#include <cstring>
#include <cstddef>

View File

@ -1,9 +1,9 @@
#ifdef BLAH_RENDERER_OPENGL
#include "renderer.h"
#include "internal.h"
#include "platform.h"
#include <blah/common.h>
#include "blah_renderer.h"
#include "blah_internal.h"
#include "blah_platform.h"
#include <blah_common.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
@ -942,7 +942,7 @@ namespace Blah
m_uniforms.push_back(tex_uniform);
UniformInfo sampler_uniform;
sampler_uniform.name = String(name).append("_sampler");
sampler_uniform.name = String(name) + "_sampler";
sampler_uniform.register_index = sampler_uniforms;
sampler_uniform.buffer_index = 0;
sampler_uniform.array_length = size;