Make threading and networking optional

This commit is contained in:
2025-02-27 04:17:12 -06:00
parent 29c53b171d
commit 02767f8710
39 changed files with 2054 additions and 99 deletions

View File

@ -0,0 +1,176 @@
#pragma once
#include "../Common.hpp"
namespace Tesses::Framework::Graphics {
struct Color {
uint8_t Red=0;
uint8_t Green=0;
uint8_t Blue=0;
uint8_t Alpha=255;
Color();
Color(uint8_t r, uint8_t g, uint8_t b);
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
Color(Color c, uint8_t a);
Color(uint8_t* color_data);
std::string ToString(bool alphaIf255=false);
void ToArray(uint8_t* color_data);
static bool TryParse(std::string color, Color& col);
static Color Parse(std::string color);
bool operator!=(Color other);
bool operator==(Color other);
};
namespace Colors {
extern const Color AliceBlue;
extern const Color AntiqueWhite;
extern const Color Aqua;
extern const Color Aquamarine;
extern const Color Azure;
extern const Color Beige;
extern const Color Bisque;
extern const Color Black;
extern const Color BlanchedAlmond;
extern const Color Blue;
extern const Color BlueViolet;
extern const Color Brown;
extern const Color BurlyWood;
extern const Color CadetBlue;
extern const Color Chartreuse;
extern const Color Chocolate;
extern const Color Coral;
extern const Color CornflowerBlue;
extern const Color Cornsilk;
extern const Color Crimson;
extern const Color Cyan;
extern const Color DarkBlue;
extern const Color DarkCyan;
extern const Color DarkGoldenRod;
extern const Color DarkGray;
extern const Color DarkGrey;
extern const Color DarkGreen;
extern const Color DarkKhaki;
extern const Color DarkMagenta;
extern const Color DarkOliveGreen;
extern const Color DarkOrange;
extern const Color DarkOrchid;
extern const Color DarkRed;
extern const Color DarkSalmon;
extern const Color DarkSeaGreen;
extern const Color DarkSlateBlue;
extern const Color DarkSlateGray;
extern const Color DarkSlateGrey;
extern const Color DarkTurquoise;
extern const Color DarkViolet;
extern const Color DeepPink;
extern const Color DeepSkyBlue;
extern const Color DimGray;
extern const Color DimGrey;
extern const Color DodgerBlue;
extern const Color FireBrick;
extern const Color FloralWhite;
extern const Color ForestGreen;
extern const Color Fuchsia;
extern const Color Gainsboro;
extern const Color GhostWhite;
extern const Color Gold;
extern const Color GoldenRod;
extern const Color Gray;
extern const Color Grey;
extern const Color Green;
extern const Color GreenYellow;
extern const Color HoneyDew;
extern const Color HotPink;
extern const Color IndianRed;
extern const Color Indigo;
extern const Color Ivory;
extern const Color Khaki;
extern const Color Lavender;
extern const Color LavenderBlush;
extern const Color LawnGreen;
extern const Color LemonChiffon;
extern const Color LightBlue;
extern const Color LightCoral;
extern const Color LightCyan;
extern const Color LightGoldenRodYellow;
extern const Color LightGray;
extern const Color LightGrey;
extern const Color LightGreen;
extern const Color LightPink;
extern const Color LightSalmon;
extern const Color LightSeaGreen;
extern const Color LightSkyBlue;
extern const Color LightSlateGray;
extern const Color LightSlateGrey;
extern const Color LightSteelBlue;
extern const Color LightYellow;
extern const Color Lime;
extern const Color LimeGreen;
extern const Color Linen;
extern const Color Magenta;
extern const Color Maroon;
extern const Color MediumAquaMarine;
extern const Color MediumBlue;
extern const Color MediumOrchid;
extern const Color MediumPurple;
extern const Color MediumSeaGreen;
extern const Color MediumSlateBlue;
extern const Color MediumSpringGreen;
extern const Color MediumTurquoise;
extern const Color MediumVioletRed;
extern const Color MidnightBlue;
extern const Color MintCream;
extern const Color MistyRose;
extern const Color Moccasin;
extern const Color NavajoWhite;
extern const Color Navy;
extern const Color OldLace;
extern const Color Olive;
extern const Color OliveDrab;
extern const Color Orange;
extern const Color OrangeRed;
extern const Color Orchid;
extern const Color PaleGoldenRod;
extern const Color PaleGreen;
extern const Color PaleTurquoise;
extern const Color PaleVioletRed;
extern const Color PapayaWhip;
extern const Color PeachPuff;
extern const Color Peru;
extern const Color Pink;
extern const Color Plum;
extern const Color PowderBlue;
extern const Color Purple;
extern const Color RebeccaPurple;
extern const Color Red;
extern const Color RosyBrown;
extern const Color SaddleBrown;
extern const Color Salmon;
extern const Color SandyBrown;
extern const Color SeaGreen;
extern const Color SeaShell;
extern const Color Sienna;
extern const Color Silver;
extern const Color SkyBlue;
extern const Color SlateBlue;
extern const Color SlateGray;
extern const Color SlateGrey;
extern const Color Snow;
extern const Color SpringGreen;
extern const Color SteelBlue;
extern const Color Tan;
extern const Color Teal;
extern const Color Thistle;
extern const Color Tomato;
extern const Color Turquoise;
extern const Color Violet;
extern const Color Wheat;
extern const Color White;
extern const Color WhiteSmoke;
extern const Color Yellow;
extern const Color YellowGreen;
};
};

View File

@ -0,0 +1,24 @@
#pragma once
#include "Color.hpp"
#include <vector>
namespace Tesses::Framework::Graphics {
class Image {
uint32_t w=0;
uint32_t h=0;
std::vector<Color> data={};
public:
Image();
Image(uint32_t w, uint32_t h);
Image(uint32_t w,uint32_t h,std::vector<Color> data);
uint32_t Width();
uint32_t Height();
void SetSize(uint32_t w, uint32_t h);
void SetSize(uint32_t w, uint32_t h, Color c);
void SetPixel(uint32_t x, uint32_t y, Color c);
Color GetPixel(uint32_t x, uint32_t y);
std::vector<Color>& Data();
};
}

View File

@ -0,0 +1,10 @@
#pragma once
#include "ImageFormat.hpp"
namespace Tesses::Framework::Graphics::ImageFormats {
class Bitmap : public ImageFormat {
public:
Bitmap();
void Load(Tesses::Framework::Streams::Stream* strm, Image* image);
void Save(Tesses::Framework::Streams::Stream* strm, Image* image,std::string flags="");
};
};

View File

@ -0,0 +1,13 @@
#pragma once
#include "../../Streams/Stream.hpp"
#include "../Image.hpp"
namespace Tesses::Framework::Graphics::ImageFormats {
class ImageFormat {
public:
virtual void Load(Tesses::Framework::Streams::Stream* strm, Image* image)=0;
virtual void Save(Tesses::Framework::Streams::Stream* strm, Image* image,std::string flags="")=0;
virtual ~ImageFormat();
};
}

View File

@ -0,0 +1,23 @@
#pragma once
#include <cstdint>
#include <string>
namespace Tesses::Framework::Graphics {
class Point {
public:
Point()
{
}
Point(int32_t x, int32_t y)
{
this->X = x;
this->Y = y;
}
int32_t X=0;
int32_t Y=0;
std::string ToString()
{
return std::to_string(X) + ", " + std::to_string(Y);
}
};
};

View File

@ -0,0 +1,42 @@
#pragma once
#include "Point.hpp"
#include "Size.hpp"
namespace Tesses::Framework::Graphics {
class Rectangle {
public:
Rectangle()
{
}
Rectangle(Point pt, int32_t square) : Rectangle(pt, Graphics::Size(square))
{
}
Rectangle(int32_t x, int32_t y, int32_t square) : Rectangle(Point(x,y),square)
{
}
Rectangle(int32_t x, int32_t y, int32_t width, int32_t height) : Rectangle(Point(x,y),Graphics::Size(width,height))
{
}
Rectangle(Point pt, Size sz)
{
this->Location = pt;
this->Size = sz;
}
Graphics::Point Location;
Graphics::Size Size;
std::string ToString()
{
return std::to_string(Location.X) + ", " + std::to_string(Location.Y) + ", " + std::to_string(Size.Width) + ", " + std::to_string(Size.Height);
}
bool Intersects(Point pt)
{
}
};
};

View File

@ -0,0 +1,17 @@
#pragma once
#include "Renderer.hpp"
namespace Tesses::Framework::Graphics::Renderers {
class ImageRenderer : public Renderer {
Image* image;
public:
ImageRenderer(Image* image);
uint32_t Width();
uint32_t Height();
void SetPixel(Point pt,Color c);
};
};

View File

@ -0,0 +1,17 @@
#pragma once
#include "../Image.hpp"
#include "../Rectangle.hpp"
namespace Tesses::Framework::Graphics::Renderers {
class Renderer {
public:
virtual uint32_t Width()=0;
virtual uint32_t Height()=0;
virtual void SetPixel(Point pt,Color c) = 0;
virtual void DrawRectangle(Rectangle rect,Color c,bool fill);
virtual void DrawImage(Point pt, Image* image);
virtual ~Renderer();
};
};

View File

@ -0,0 +1,32 @@
#pragma once
#include <cstdint>
#include <string>
namespace Tesses::Framework::Graphics {
class Size {
public:
Size()
{
}
Size(int32_t square)
{
this->Width = square;
this->Height = square;
}
Size(int32_t width, int32_t height)
{
this->Width = width;
this->Height = height;
}
int32_t Width=0;
int32_t Height=0;
bool IsSquare()
{
return Width == Height;
}
std::string ToString()
{
return std::to_string(Width) + "x" + std::to_string(Height);
}
};
};

View File

@ -0,0 +1,32 @@
#pragma once
#include <functional>
namespace Tesses::Framework {
class HiddenFieldData {
public:
virtual ~HiddenFieldData();
};
class HiddenField {
private:
HiddenFieldData* ptr;
public:
HiddenField();
HiddenField(HiddenFieldData* data);
void SetField(HiddenFieldData* data);
template<typename T>
T GetField()
{
return dynamic_cast<T>(ptr);
}
template<typename T>
T AllocField()
{
auto v = new T();
SetField(v);
return v;
}
~HiddenField();
};
}

View File

@ -68,14 +68,9 @@ namespace Tesses::Framework::Http
NotExtended=510,
NetworkAuthenticationRequired=511
} StatusCode;
struct CaseInsensitiveLess {
bool operator() (const std::string& s1, const std::string& s2) const {
std::string str1(s1.length(),' ');
std::string str2(s2.length(),' ');
std::transform(s1.begin(), s1.end(), str1.begin(), tolower);
std::transform(s2.begin(), s2.end(), str2.begin(), tolower);
return str1 < str2;
}
bool operator() (const std::string& s1, const std::string& s2) const;
};
class HttpDictionary {
public:
@ -146,6 +141,8 @@ struct CaseInsensitiveLess {
static std::string HtmlEncode(std::string v);
static std::vector<std::string> SplitString(std::string text, std::string delimiter,std::size_t maxCnt = std::string::npos);
static std::string StatusCodeString(StatusCode code);
static std::string ToLower(std::string str);
static std::string ToUpper(std::string str);
};

View File

@ -0,0 +1,33 @@
#pragma once
#include "Stream.hpp"
namespace Tesses::Framework::Streams
{
class ByteReader {
Stream* strm;
bool owns;
public:
Stream* GetStream();
ByteReader(Stream* strm, bool owns);
ByteReader(Stream& strm);
uint8_t ReadU8();
uint16_t ReadU16BE();
uint16_t ReadU16LE();
uint32_t ReadU32BE();
uint32_t ReadU32LE();
uint64_t ReadU64BE();
uint64_t ReadU64LE();
int8_t ReadI8();
int16_t ReadI16BE();
int16_t ReadI16LE();
int32_t ReadI32BE();
int32_t ReadI32LE();
int64_t ReadI64BE();
int64_t ReadI64LE();
float ReadF32BE();
float ReadF32LE();
double ReadF64BE();
double ReadF64LE();
~ByteReader();
};
}

View File

@ -0,0 +1,33 @@
#pragma once
#include "Stream.hpp"
namespace Tesses::Framework::Streams
{
class ByteWriter {
Stream* strm;
bool owns;
public:
Stream* GetStream();
ByteWriter(Stream* strm, bool owns);
ByteWriter(Stream& strm);
void WriteU8(uint8_t v);
void WriteU16BE(uint16_t v);
void WriteU16LE(uint16_t v);
void WriteU32BE(uint32_t v);
void WriteU32LE(uint32_t v);
void WriteU64BE(uint64_t v);
void WriteU64LE(uint64_t v);
void WriteI8(int8_t v);
void WriteI16BE(int16_t v);
void WriteI16LE(int16_t v);
void WriteI32BE(int32_t v);
void WriteI32LE(int32_t v);
void WriteI64BE(int64_t v);
void WriteI64LE(int64_t v);
void WriteF32BE(float v);
void WriteF32LE(float v);
void WriteF64BE(double v);
void WriteF64LE(double v);
~ByteWriter();
};
}

View File

@ -9,6 +9,8 @@
#include "Streams/MemoryStream.hpp"
#include "Streams/NetworkStream.hpp"
#include "Streams/BufferedStream.hpp"
#include "Streams/ByteReader.hpp"
#include "Streams/ByteWriter.hpp"
#include "TextStreams/StreamReader.hpp"
#include "TextStreams/StreamWriter.hpp"
#include "Threading/Thread.hpp"
@ -21,4 +23,11 @@
#include "Filesystem/MemoryFilesystem.hpp"
#include "Crypto/ClientTLSStream.hpp"
#include "Crypto/MbedHelpers.hpp"
#include "Lazy.hpp"
#include "Lazy.hpp"
#include "Graphics/ImageFormats/ImageFormat.hpp"
#include "Graphics/ImageFormats/Bitmap.hpp"
#include "Graphics/Renderers/ImageRenderer.hpp"
#include "Graphics/Renderers/Renderer.hpp"
#include "Graphics/Image.hpp"
#include "Graphics/Color.hpp"
#include "HiddenField.hpp"

View File

@ -1,22 +1,10 @@
#pragma once
#if defined(_WIN32)
#include <windows.h>
#elif defined(GEKKO)
#include <ogc/mutex.h>
#else
#include <pthread.h>
#endif
#include "../HiddenField.hpp"
namespace Tesses::Framework::Threading
{
class Mutex {
#if defined(_WIN32)
HANDLE mtx;
#elif defined(GEKKO)
mutex_t mtx;
#else
pthread_mutex_t mtx;
pthread_mutexattr_t attr;
#endif
HiddenField data;
public:
Mutex();
void Lock();

View File

@ -8,26 +8,13 @@
#include <pthread.h>
#endif
#include <atomic>
#include "../HiddenField.hpp"
namespace Tesses::Framework::Threading
{
class Thread
{
#if defined(_WIN32)
HANDLE thrd;
DWORD thrdId;
public:
#elif defined(GEKKO)
lwp_t thrd;
static void* cb(void* ptr);
#else
pthread_t thrd;
static void* cb(void* ptr);
#endif
std::function<void()> fn;
std::atomic<bool> hasInvoked;
HiddenField data;
public:
Thread(std::function<void()> fn);
void Join();