Add rendering functions (crashes)
This commit is contained in:
@@ -83,7 +83,8 @@ class c3d {
|
||||
static void deleteScreen(screen* screen);
|
||||
static void drawArrays(int start, int count,
|
||||
GPU_Primitive_t prim = GPU_TRIANGLES);
|
||||
static void drawElements(int count, const void* idx_ptr, int type = GPU_SHORT,
|
||||
static void drawElements(int count, const void* idx_ptr,
|
||||
int type = C3D_UNSIGNED_SHORT,
|
||||
GPU_Primitive_t prim = GPU_TRIANGLES);
|
||||
static void depthTest(bool on, GPU_TESTFUNC func = GPU_GREATER,
|
||||
GPU_WRITEMASK mask = GPU_WRITE_ALL);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <amethyst/c3d.hpp>
|
||||
#include <amethyst/linearAlloc.hpp>
|
||||
#include <amethyst/maths/mat.hpp>
|
||||
#include <amethyst/maths/vec.hpp>
|
||||
#include <amethyst/rect.hpp>
|
||||
#include <amethyst/texture.hpp>
|
||||
#include <amethyst/types.hpp>
|
||||
|
||||
@@ -12,18 +12,23 @@ namespace amy {
|
||||
class iron {
|
||||
public:
|
||||
struct vertex {
|
||||
vertex(float x, float y, float u, float v, u32 clr) {
|
||||
vertex(float x, float y, float u, float v, ui clr) {
|
||||
pos.x = x;
|
||||
pos.y = y;
|
||||
uv.x = x;
|
||||
uv.y = y;
|
||||
color = clr;
|
||||
}
|
||||
vertex(const fvec2& pos, const fvec2& uv, ui clr) {
|
||||
this->pos = pos;
|
||||
this->uv = uv;
|
||||
this->color = clr;
|
||||
}
|
||||
vertex() {}
|
||||
|
||||
amy::fvec2 pos;
|
||||
amy::fvec2 uv;
|
||||
u32 color = 0;
|
||||
ui color = 0;
|
||||
};
|
||||
class command {
|
||||
public:
|
||||
@@ -57,6 +62,8 @@ class iron {
|
||||
drawlist(drawlist&&) noexcept = default;
|
||||
drawlist& operator=(drawlist&&) noexcept = default;
|
||||
|
||||
std::vector<command::ref>& data() { return m_data; }
|
||||
|
||||
void merge(drawlist* list);
|
||||
command::ref newCommand();
|
||||
void push(command ::ref cmd);
|
||||
@@ -65,9 +72,56 @@ class iron {
|
||||
void drawSolid();
|
||||
void drawTex(texture* tex) { m_tex = tex; }
|
||||
|
||||
/** Draw Api */
|
||||
void drawRect(const fvec2& pos, const fvec2& size, ui color,
|
||||
int thickness = 1);
|
||||
void drawRectFilled(const fvec2& pos, const fvec2& size, ui color);
|
||||
void drawTriangle(const fvec2& a, const fvec2& b, const fvec2& c, ui color,
|
||||
int thickness = 1);
|
||||
void drawTriangleFilled(const fvec2& a, const fvec2& b, const fvec2& c,
|
||||
ui color);
|
||||
void drawCircle(const fvec2& center, float radius, ui color, int segments,
|
||||
int thickness = 1);
|
||||
void drawCircleFilled(const fvec2& center, float radius, ui color,
|
||||
int segments);
|
||||
void drawText(const fvec2& pos, const std::string& text, ui color);
|
||||
void drawTextEx(const fvec2& pos, const std::string& text, ui color,
|
||||
ui flags, const fvec2& box = 0);
|
||||
void drawLine(const fvec2& a, const fvec2& b, ui color, int thickness = 1);
|
||||
void drawPolyLine(const std::vector<fvec2>& points, ui color, ui flags = 0,
|
||||
int thickness = 1);
|
||||
void drawConvexPolyFilled(const std::vector<fvec2>& points, ui color);
|
||||
|
||||
/** Path api */
|
||||
void pathAdd(const fvec2& pos) { m_path.push_back(std::move(pos)); }
|
||||
void pathClear() { m_path.clear(); }
|
||||
void pathReserve(size_t count) { m_path.reserve(m_path.size() + count); }
|
||||
void pathStroke(ui color, int thickness = 1, ui flags = 0) {
|
||||
drawPolyLine(m_path, color, flags, thickness);
|
||||
pathClear();
|
||||
}
|
||||
void pathFill(ui color) { drawConvexPolyFilled(m_path, color); }
|
||||
void pathArcToN(const fvec2& c, float radius, float a_min, float a_max,
|
||||
int segments);
|
||||
void pathFastArcToN(const fvec2& c, float radius, float a_min, float a_max,
|
||||
int segments);
|
||||
void pathRect(const fvec2& a, const fvec2& b, float rounding = 0.f);
|
||||
void pathRectEx(const fvec2& a, const fvec2& b, float rounfing, ui flags);
|
||||
|
||||
void pushClipRect(const fvec4& clip) { clipRects.push(clip); }
|
||||
void popClipRect() {
|
||||
if (!clipRects.empty()) clipRects.pop();
|
||||
}
|
||||
|
||||
operator std::vector<command::ref>&() { return m_data; }
|
||||
|
||||
private:
|
||||
void clipCmd(command* ptr);
|
||||
std::vector<command::ref> m_data;
|
||||
texture* m_tex;
|
||||
std::vector<fvec2> m_path;
|
||||
texture* m_tex = nullptr;
|
||||
std::stack<fvec4> clipRects;
|
||||
int m_layer = 0;
|
||||
};
|
||||
iron() = default;
|
||||
~iron() = default;
|
||||
@@ -76,10 +130,28 @@ class iron {
|
||||
static void newFrame();
|
||||
static void drawOn(c3d::screen* screen);
|
||||
static void draw(const std::vector<command::ref>& data);
|
||||
static texture* whiteTex() { return m_solid; }
|
||||
|
||||
/** Static renderer utility funcs */
|
||||
|
||||
static void rotateCorner(fvec2& pos, float s, float c);
|
||||
static rect primRect(const fvec2& pos, const fvec2& size, float angle = 0.f);
|
||||
static rect primLine(const fvec2& a, const fvec2& b, int thickness = 1);
|
||||
static void cmdQuad(command* cmd, const rect& q, const rect& uv, ui color);
|
||||
static void cmdTriangle(command* cmd, const fvec2& a, const fvec2& b,
|
||||
const fvec2& c, ui clr);
|
||||
static void cmdConvexPolyFilled(command* cmd,
|
||||
const std::vector<fvec2>& points, ui clr,
|
||||
texture* tex);
|
||||
static bool inBox(const fvec2& pos, const fvec2& size, const fvec4& area);
|
||||
static bool inBox(const fvec2& pos, const fvec4& area);
|
||||
static bool inBox(const fvec2& a, const fvec2& b, const fvec2& c,
|
||||
const fvec4& area);
|
||||
|
||||
private:
|
||||
static void setupShader();
|
||||
static void fragConfig();
|
||||
static void initSolidTex();
|
||||
|
||||
static std::vector<vertex, linearAllocator<vertex>> m_vbuf;
|
||||
static std::vector<u16, linearAllocator<u16>> m_ibuf;
|
||||
@@ -87,5 +159,6 @@ class iron {
|
||||
static c3d::shader* m_shader;
|
||||
static mat4 m_mtx;
|
||||
static int m_idx, m_vtx;
|
||||
static texture* m_solid;
|
||||
};
|
||||
} // namespace amy
|
||||
61
include/amethyst/rect.hpp
Normal file
61
include/amethyst/rect.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <amethyst/types.hpp>
|
||||
|
||||
namespace amy {
|
||||
class rect {
|
||||
public:
|
||||
rect() : m_top(0), m_bot(0) {}
|
||||
~rect() = default;
|
||||
rect(const fvec4& t, const fvec4& b) : m_top(t), m_bot(b) {}
|
||||
rect(const fvec2& tl, const fvec2& tr, const fvec2& bl, const fvec2& br)
|
||||
: m_top(tl, tr), m_bot(bl, br) {}
|
||||
rect(const fvec4& duv)
|
||||
: m_top(duv.x, duv.y, duv.z, duv.y), m_bot(duv.x, duv.w, duv.z, duv.w) {}
|
||||
|
||||
fvec2 topLeft() const { return fvec2(m_top.x, m_top.y); }
|
||||
fvec2 topRight() const { return fvec2(m_top.z, m_top.w); }
|
||||
fvec2 botLeft() const { return fvec2(m_bot.x, m_bot.y); }
|
||||
fvec2 botRight() const { return fvec2(m_bot.z, m_bot.w); }
|
||||
|
||||
fvec4 top() const { return m_top; }
|
||||
fvec4& top() { return m_top; }
|
||||
fvec4 bot() const { return m_bot; }
|
||||
fvec4& bot() { return m_bot; }
|
||||
|
||||
rect& topLeft(const fvec2& v) {
|
||||
m_top.x = v.x;
|
||||
m_top.y = v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
rect& topRight(const fvec2& v) {
|
||||
m_top.z = v.x;
|
||||
m_top.w = v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
rect& botLeft(const fvec2& v) {
|
||||
m_bot.x = v.x;
|
||||
m_bot.y = v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
rect& botRight(const fvec2& v) {
|
||||
m_bot.z = v.x;
|
||||
m_bot.w = v.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void swapVec2XY() {
|
||||
m_top.SwapXY();
|
||||
m_top.SwapZW();
|
||||
m_bot.SwapXY();
|
||||
m_bot.SwapZW();
|
||||
}
|
||||
|
||||
private:
|
||||
fvec4 m_top;
|
||||
fvec4 m_bot;
|
||||
};
|
||||
} // namespace amy
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <amethyst/asset.hpp>
|
||||
#include <amethyst/image.hpp>
|
||||
#include <amethyst/rect.hpp>
|
||||
#include <amethyst/types.hpp>
|
||||
|
||||
namespace amy {
|
||||
@@ -13,6 +14,8 @@ class texture : public asset {
|
||||
texture(cstr& path);
|
||||
~texture();
|
||||
void load(cstr& path);
|
||||
void load(const std::vector<uc>& pixels, int w, int h, int bpp = 4,
|
||||
image::format fmt = image::RGBA);
|
||||
void unload();
|
||||
|
||||
int w() const { return m_size.x; }
|
||||
@@ -21,6 +24,7 @@ class texture : public asset {
|
||||
int& h() { return m_size.y; }
|
||||
ivec2 size() const { return m_size; }
|
||||
ivec2& size() { return m_size; }
|
||||
rect& uv() { return m_uv; }
|
||||
|
||||
C3D_Tex* ptr() { return m_loaded ? &m_tex : nullptr; }
|
||||
|
||||
@@ -29,6 +33,7 @@ class texture : public asset {
|
||||
private:
|
||||
C3D_Tex m_tex;
|
||||
ivec2 m_size;
|
||||
rect m_uv;
|
||||
bool m_loaded = false;
|
||||
};
|
||||
} // namespace amy
|
||||
@@ -1,10 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <amethyst/maths/vec.hpp>
|
||||
#include <cinttypes>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <numbers>
|
||||
#include <stack>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
Reference in New Issue
Block a user