replaced log.h with common.h, added easier shorthand for int types

This commit is contained in:
Noel Berry
2021-03-20 17:33:04 -07:00
parent 9f9ed08007
commit d73241e8fe
58 changed files with 416 additions and 408 deletions

View File

@ -2,7 +2,7 @@
#include "blah/core/app.h"
#include "blah/core/filesystem.h"
#include "blah/core/log.h"
#include "blah/core/common.h"
#include "blah/core/time.h"
#include "blah/containers/vector.h"

View File

@ -1,5 +1,5 @@
#pragma once
#include <blah/core/log.h>
#include <blah/core/common.h>
#include <new>
#include <initializer_list>

View File

@ -1,5 +1,5 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
#include <stdarg.h>
#include <cstdio>
#include <blah/containers/vector.h>
@ -78,7 +78,7 @@ namespace Blah
// Returns the unicode value at the given index.
// Assumes the index is a valid utf8 starting point.
uint32_t utf8_at(int index) const;
u32 utf8_at(int index) const;
// Returns the byte-length of the utf8 character.
// Assumes the index is a valid utf8 starting point.
@ -88,7 +88,7 @@ namespace Blah
Str& append(char c);
// appends the given unicode character
Str& append(uint32_t c);
Str& append(u32 c);
// appends the given c string
Str& append(const char* start, const char* end = nullptr);
@ -100,7 +100,7 @@ namespace Blah
Str& append_fmt(const char* fmt, ...);
// appends a utf16 string
Str& append_utf16(const uint16_t* start, const uint16_t* end = nullptr, bool swapEndian = false);
Str& append_utf16(const u16* start, const u16* end = nullptr, bool swapEndian = false);
// trims whitespace
Str& trim();

View File

@ -1,5 +1,5 @@
#pragma once
#include <blah/core/log.h>
#include <blah/core/common.h>
#include <type_traits>
#include <new>

View File

@ -1,7 +1,7 @@
#pragma once
#include <memory>
#include <functional>
#include <blah/core/log.h>
#include <blah/core/common.h>
namespace Blah
{

View File

@ -1,21 +1,22 @@
#pragma once
#include <cstdint>
// error / abort
#if defined(DEBUG) || defined(_DEBUG)
#include <stdlib.h>
#define BLAH_ERROR(message) \
#include <stdlib.h>
#define BLAH_ERROR(message) \
do { Blah::Log::error(message "\n\tin file: %s:%d", __FILE__, __LINE__); abort(); } while(0)
#define BLAH_ERROR_FMT(message, ...) \
#define BLAH_ERROR_FMT(message, ...) \
do { Blah::Log::error(message "\n\tin file: %s:%d", __VA_ARGS__, __FILE__, __LINE__); abort(); } while(0)
#else
#define BLAH_ERROR(message) \
#define BLAH_ERROR(message) \
Blah::Log::error(message "\n\tin file: %s:%d", __FILE__, __LINE__)
#define BLAH_ERROR_FMT(message, ...) \
#define BLAH_ERROR_FMT(message, ...) \
Blah::Log::error(message "\n\tin file: %s:%d", __VA_ARGS__, __FILE__, __LINE__)
#endif
@ -33,6 +34,16 @@
namespace Blah
{
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
namespace Log
{
enum class Category
@ -42,8 +53,8 @@ namespace Blah
Error
};
void print(const char* info, ...);
void warn(const char* info, ...);
void error(const char* info, ...);
void info(const char* message, ...);
void warn(const char* message, ...);
void error(const char* message, ...);
}
}

View File

@ -1,21 +1,21 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
namespace Blah
{
struct Time
{
// ticks per second (microseconds, in this case)
static constexpr uint64_t ticks_per_second = 1000000;
static constexpr u64 ticks_per_second = 1000000;
// uptime, in ticks
static uint64_t ticks;
static u64 ticks;
// uptime, in seconds
static double seconds;
// previous frame uptime, in ticks
static uint64_t previous_ticks;
static u64 previous_ticks;
// previous frame uptime, in seconds
static double previous_seconds;

View File

@ -187,10 +187,10 @@ namespace Blah
Vec2 tex;
Color col;
uint8_t mult;
uint8_t wash;
uint8_t fill;
uint8_t pad;
u8 mult;
u8 wash;
u8 fill;
u8 pad;
};
struct DrawBatch
@ -219,11 +219,11 @@ namespace Blah
MeshRef m_mesh;
Mat3x2 m_matrix;
ColorMode m_color_mode;
uint8_t m_tex_mult;
uint8_t m_tex_wash;
u8 m_tex_mult;
u8 m_tex_wash;
DrawBatch m_batch;
Vector<Vertex> m_vertices;
Vector<uint32_t> m_indices;
Vector<u32> m_indices;
Vector<Mat3x2> m_matrix_stack;
Vector<Rect> m_scissor_stack;
Vector<BlendMode> m_blend_stack;

View File

@ -1,5 +1,5 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
#include <blah/containers/str.h>
#include <blah/containers/vector.h>
#include <blah/drawing/subtexture.h>
@ -21,14 +21,14 @@ namespace Blah
};
private:
// charset & kerning maps
std::unordered_map<uint32_t, Character> m_characters;
std::unordered_map<uint64_t, float> m_kerning;
std::unordered_map<u32, Character> m_characters;
std::unordered_map<u64, float> m_kerning;
// built texture
Vector<TextureRef> m_atlas;
public:
static const uint32_t* ASCII;
static const u32* ASCII;
String name;
float size;
@ -41,9 +41,9 @@ namespace Blah
SpriteFont();
SpriteFont(const char* file, float size);
SpriteFont(const char* file, float size, const uint32_t* charset);
SpriteFont(const char* file, float size, const u32* charset);
SpriteFont(const Font& font, float size);
SpriteFont(const Font& font, float size, const uint32_t* charset);
SpriteFont(const Font& font, float size, const u32* charset);
SpriteFont(const SpriteFont&) = delete;
SpriteFont(SpriteFont&& src) noexcept;
~SpriteFont();
@ -62,15 +62,15 @@ namespace Blah
float width_of_line(const String& text, int start = 0) const;
float height_of(const String& text) const;
void build(const char* file, float size, const uint32_t* charset);
void build(const Font& font, float size, const uint32_t* charset);
void build(const char* file, float size, const u32* charset);
void build(const Font& font, float size, const u32* charset);
float get_kerning(uint32_t codepoint0, uint32_t codepoint1) const;
void set_kerning(uint32_t codepoint0, uint32_t codepoint1, float kerning);
float get_kerning(u32 codepoint0, u32 codepoint1) const;
void set_kerning(u32 codepoint0, u32 codepoint1, float kerning);
Character& get_character(uint32_t codepoint) { return m_characters[codepoint]; }
const Character& get_character(uint32_t codepoint) const;
Character& operator[](uint32_t codepoint) { return m_characters[codepoint]; }
const Character& operator[](uint32_t codepoint) const;
Character& get_character(u32 codepoint) { return m_characters[codepoint]; }
const Character& get_character(u32 codepoint) const;
Character& operator[](u32 codepoint) { return m_characters[codepoint]; }
const Character& operator[](u32 codepoint) const;
};
}

View File

@ -1,5 +1,5 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
namespace Blah
{
@ -61,7 +61,7 @@ namespace Blah
BlendFactor alpha_src;
BlendFactor alpha_dst;
BlendMask mask;
uint32_t rgba;
u32 rgba;
BlendMode() = default;
@ -78,7 +78,7 @@ namespace Blah
BlendMode(
BlendOp color_op, BlendFactor color_src, BlendFactor color_dst,
BlendOp alpha_op, BlendFactor alpha_src, BlendFactor alpha_dst,
BlendMask blend_mask, uint32_t blend_rgba) :
BlendMask blend_mask, u32 blend_rgba) :
color_op(color_op),
color_src(color_src),
color_dst(color_dst),

View File

@ -65,7 +65,7 @@ namespace Blah
virtual int height() const = 0;
// Clears the FrameBuffer
virtual void clear(Color color = Color::black, float depth = 1.0f, uint8_t stencil = 0, ClearMask mask = ClearMask::All) = 0;
virtual void clear(Color color = Color::black, float depth = 1.0f, u8 stencil = 0, ClearMask mask = ClearMask::All) = 0;
};
}

View File

@ -59,10 +59,10 @@ namespace Blah
// Sets the value. `length` is the total number of floats to set
// For example if the uniform is a float2[4], a total of 8 float values
// can be set.
void set_value(const char* name, const float* value, int64_t length);
void set_value(const char* name, const float* value, i64 length);
// Gets a pointer to the values of the given Uniform, or nullptr if it doesn't exist.
const float* get_value(const char* name, int64_t* length = nullptr) const;
const float* get_value(const char* name, i64* length = nullptr) const;
// Returns the internal Texture buffer
const Vector<TextureRef>& textures() const;

View File

@ -1,5 +1,5 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
#include <memory>
#include <blah/containers/stackvector.h>
@ -73,21 +73,21 @@ namespace Blah
static MeshRef create();
// Uploads the given index buffer to the Mesh
virtual void index_data(IndexFormat format, const void* indices, int64_t count) = 0;
virtual void index_data(IndexFormat format, const void* indices, i64 count) = 0;
// Uploads the given vertex buffer to the Mesh
virtual void vertex_data(const VertexFormat& format, const void* vertices, int64_t count) = 0;
virtual void vertex_data(const VertexFormat& format, const void* vertices, i64 count) = 0;
// Uploads the given instance buffer to the Mesh
virtual void instance_data(const VertexFormat& format, const void* instances, int64_t count) = 0;
virtual void instance_data(const VertexFormat& format, const void* instances, i64 count) = 0;
// Gets the index count of the Mesh
virtual int64_t index_count() const = 0;
virtual i64 index_count() const = 0;
// Gets the vertex count of the Mesh
virtual int64_t vertex_count() const = 0;
virtual i64 vertex_count() const = 0;
// Gets the instance count of the Mesh
virtual int64_t instance_count() const = 0;
virtual i64 instance_count() const = 0;
};
}

View File

@ -1,5 +1,5 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
#include <blah/math/rect.h>
#include <blah/containers/str.h>
#include <blah/graphics/texture.h>
@ -55,13 +55,13 @@ namespace Blah
Rect scissor;
// First index in the Mesh to draw from
int64_t index_start;
i64 index_start;
// Total amount of indices to draw from the Mesh
int64_t index_count;
i64 index_count;
// Total amount of instances to draw from the Mesh
int64_t instance_count;
i64 instance_count;
// Depth Compare Function
Compare depth;

View File

@ -5,7 +5,7 @@
namespace Blah
{
typedef uint32_t Codepoint;
typedef u32 Codepoint;
class Font
{

View File

@ -18,15 +18,15 @@ namespace Blah
{
friend class Packer;
private:
int64_t memory_index;
i64 memory_index;
public:
uint64_t id;
u64 id;
int page;
bool empty;
RectI frame;
RectI packed;
Entry(uint64_t id, const RectI& frame)
Entry(u64 id, const RectI& frame)
: memory_index(0), id(id), page(0), empty(true), frame(frame), packed(0, 0, 0, 0) {}
};
@ -46,9 +46,9 @@ namespace Blah
Packer& operator=(Packer&& src) noexcept;
~Packer();
void add(uint64_t id, int width, int height, const Color* pixels);
void add(uint64_t id, const Image& bitmap);
void add(uint64_t id, const String& path);
void add(u64 id, int width, int height, const Color* pixels);
void add(u64 id, const Image& bitmap);
void add(u64 id, const String& path);
void pack();
void clear();
@ -70,6 +70,6 @@ namespace Blah
bool m_dirty;
BufferStream m_buffer;
void add_entry(uint64_t id, int w, int h, const Color* pixels);
void add_entry(u64 id, int w, int h, const Color* pixels);
};
}

View File

@ -1,6 +1,6 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
#include <blah/math/vec2.h>
// These are generally copied from the SDL2 Scancode Keys
@ -293,19 +293,19 @@ namespace Blah
float axis[Input::max_controller_axis];
// Timestamp, in milliseconds, since each button was last pressed
uint64_t button_timestamp[Input::max_controller_buttons];
u64 button_timestamp[Input::max_controller_buttons];
// Timestamp, in milliseconds, since each axis last had a value set
uint64_t axis_timestamp[Input::max_controller_axis];
u64 axis_timestamp[Input::max_controller_axis];
// The USB Vendor ID
uint16_t vendor;
u16 vendor;
// The USB Product ID
uint16_t product;
u16 product;
// the Product Version
uint16_t version;
u16 version;
};
struct KeyboardState
@ -313,7 +313,7 @@ namespace Blah
bool pressed[Input::max_keyboard_keys];
bool down[Input::max_keyboard_keys];
bool released[Input::max_keyboard_keys];
uint64_t timestamp[Input::max_keyboard_keys];
u64 timestamp[Input::max_keyboard_keys];
char text[Input::max_text_input];
};
@ -322,7 +322,7 @@ namespace Blah
bool pressed[Input::max_mouse_buttons];
bool down[Input::max_mouse_buttons];
bool released[Input::max_mouse_buttons];
uint64_t timestamp[Input::max_mouse_buttons];
u64 timestamp[Input::max_mouse_buttons];
Vec2 screen_position;
Vec2 draw_position;
Vec2 position;

View File

@ -1,5 +1,5 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
#include <blah/math/vec2.h>
namespace Blah
@ -27,10 +27,6 @@ namespace Blah
Vec2 approach(const Vec2& t, const Vec2& target, float delta);
float clamp(float t, float min, float max);
int clamp_int(int t, int min, int max);
float map(float t, float old_min, float old_max, float new_min, float new_max);
float clamped_map(float t, float old_min, float old_max, float new_min, float new_max);
@ -43,6 +39,9 @@ namespace Blah
float abs(float x);
template<class T>
T clamp(T value, T min, T max) { return value < min ? min : (value > max ? max : value); }
template<class T, class U>
T min(T a, U b) { return (T)(a < b ? a : b); }

View File

@ -1,5 +1,5 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
#include <blah/containers/str.h>
namespace Blah
@ -8,16 +8,16 @@ namespace Blah
struct Color
{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
u8 r;
u8 g;
u8 b;
u8 a;
Color();
Color(int rgb);
Color(int rgb, float alpha);
Color(uint8_t r, uint8_t g, uint8_t b);
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
Color(u8 r, u8 g, u8 b);
Color(u8 r, u8 g, u8 b, u8 a);
Color(const Vec4& vec4);
// Parses a Hex string in the format of "#00000000" or "0x00000000" or "00000000"
@ -40,17 +40,17 @@ namespace Blah
// Returns an RGB hex string of the color
String to_hex_rgb() const;
// Convers the Color to a uint32_t
uint32_t to_rgba() const;
// Convers the Color to a u32
u32 to_rgba() const;
// Converts the Color to a Vec4
Vec4 to_vec4() const;
// Returns a RGBA Color representation of the integer value
static Color from_rgba(uint32_t value);
static Color from_rgba(u32 value);
// Returns a RGB Color representation of the integer value, with Alpha = 255
static Color from_rgb(uint32_t value);
static Color from_rgb(u32 value);
// Lerps between two colors
static Color lerp(Color a, Color b, float amount);

View File

@ -1,6 +1,6 @@
#pragma once
#include <blah/math/calc.h>
#include <blah/core/log.h>
#include <blah/core/common.h>
namespace Blah
{

View File

@ -1,5 +1,5 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
namespace Blah
{
@ -8,9 +8,9 @@ namespace Blah
public:
Stopwatch();
void reset();
uint64_t microseconds();
uint64_t milliseconds();
u64 microseconds();
u64 milliseconds();
private:
uint64_t start_time;
u64 start_time;
};
}

View File

@ -12,9 +12,9 @@ namespace Blah
BufferStream& operator=(BufferStream&& bs) noexcept;
~BufferStream();
virtual int64_t length() const override { return m_length; }
virtual int64_t position() const override { return m_position; }
virtual int64_t seek(int64_t seekTo) override { return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo)); }
virtual i64 length() const override { return m_length; }
virtual i64 position() const override { return m_position; }
virtual i64 seek(i64 seekTo) override { return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo)); }
virtual bool is_open() const override { return m_buffer != nullptr; }
virtual bool is_readable() const override { return true; }
virtual bool is_writable() const override { return true; }
@ -25,13 +25,13 @@ namespace Blah
const char* data() const { return m_buffer; }
protected:
virtual int64_t read_into(void* ptr, int64_t length) override;
virtual int64_t write_from(const void* ptr, int64_t length) override;
virtual i64 read_into(void* ptr, i64 length) override;
virtual i64 write_from(const void* ptr, i64 length) override;
private:
char* m_buffer;
int64_t m_capacity;
int64_t m_length;
int64_t m_position;
i64 m_capacity;
i64 m_length;
i64 m_position;
};
}

View File

@ -13,17 +13,17 @@ namespace Blah
FileStream& operator=(FileStream&& fs) noexcept;
~FileStream();
virtual int64_t length() const override;
virtual int64_t position() const override;
virtual int64_t seek(int64_t seekTo) override;
virtual i64 length() const override;
virtual i64 position() const override;
virtual i64 seek(i64 seekTo) override;
virtual bool is_open() const override { return m_handle != nullptr; }
virtual bool is_readable() const override { return m_handle != nullptr && (m_mode == FileMode::ReadWrite || m_mode == FileMode::Read); }
virtual bool is_writable() const override { return m_handle != nullptr && (m_mode == FileMode::ReadWrite || m_mode == FileMode::Write); }
virtual void close() override;
protected:
virtual int64_t read_into(void* ptr, int64_t length) override;
virtual int64_t write_from(const void* ptr, int64_t length) override;
virtual i64 read_into(void* ptr, i64 length) override;
virtual i64 write_from(const void* ptr, i64 length) override;
private:
FileMode m_mode;

View File

@ -7,14 +7,14 @@ namespace Blah
{
public:
MemoryStream();
MemoryStream(char* data, int64_t length);
MemoryStream(char* data, i64 length);
MemoryStream(MemoryStream&& ms) noexcept;
MemoryStream& operator=(MemoryStream&& ms) noexcept;
~MemoryStream() { m_data = nullptr; m_length = m_position = 0; }
virtual int64_t length() const override { return m_length; }
virtual int64_t position() const override { return m_position; }
virtual int64_t seek(int64_t seekTo) override { return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo)); }
virtual i64 length() const override { return m_length; }
virtual i64 position() const override { return m_position; }
virtual i64 seek(i64 seekTo) override { return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo)); }
virtual bool is_open() const override { return m_data != nullptr; }
virtual bool is_readable() const override { return true; }
virtual bool is_writable() const override { return true; }
@ -24,12 +24,12 @@ namespace Blah
const char* data() const { return m_data; }
protected:
virtual int64_t read_into(void* ptr, int64_t length) override;
virtual int64_t write_from(const void* ptr, int64_t length) override;
virtual i64 read_into(void* ptr, i64 length) override;
virtual i64 write_from(const void* ptr, i64 length) override;
private:
char* m_data;
int64_t m_length;
int64_t m_position;
i64 m_length;
i64 m_position;
};
}

View File

@ -1,5 +1,5 @@
#pragma once
#include <inttypes.h>
#include <blah/core/common.h>
#include <blah/containers/str.h>
#include <blah/streams/endian.h>
#include <string.h>
@ -16,13 +16,13 @@ namespace Blah
virtual ~Stream() = default;
// returns the length of the stream
virtual int64_t length() const = 0;
virtual i64 length() const = 0;
// returns the position of the stream
virtual int64_t position() const = 0;
virtual i64 position() const = 0;
// seeks the position of the stream
virtual int64_t seek(int64_t seek_to) = 0;
virtual i64 seek(i64 seek_to) = 0;
// returns true of the stream is open
virtual bool is_open() const = 0;
@ -37,10 +37,10 @@ namespace Blah
virtual void close() = 0;
// pipes the contents of this tream to another stream
int64_t pipe(Stream& to, int64_t length);
i64 pipe(Stream& to, i64 length);
// reads the amount of bytes into the given buffer, and returns the amount read
int64_t read(void* buffer, int64_t length) { return read_into(buffer, length); }
i64 read(void* buffer, i64 length) { return read_into(buffer, length); }
// reads a string. if length < 0, assumes null-terminated
String read_string(int length = -1);
@ -67,21 +67,21 @@ namespace Blah
}
// writes the amount of bytes to the stream from the given buffer, and returns the amount written
int64_t write(const void* buffer, int64_t length);
i64 write(const void* buffer, i64 length);
// writes the contents of a string to the stream
int64_t write(const String& string);
i64 write(const String& string);
// writes a number
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
int64_t write(const T& value)
i64 write(const T& value)
{
return write<T>(value, Endian::Little);
}
// writes a number
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
int64_t write(const T& value, Endian endian)
i64 write(const T& value, Endian endian)
{
T writing = value;
@ -93,9 +93,9 @@ namespace Blah
protected:
// reads from the stream into the given buffer, and returns the number of bytes read
virtual int64_t read_into(void* buffer, int64_t length) = 0;
virtual i64 read_into(void* buffer, i64 length) = 0;
// writes from the stream from the given buffer, and returns the number of bytes written
virtual int64_t write_from(const void* buffer, int64_t length) = 0;
virtual i64 write_from(const void* buffer, i64 length) = 0;
};
}