Add SMART_CTOR
Begin with Render2 (Draw2 replacement)
Add UI7 DrawLists
This commit is contained in:
2024-06-08 16:30:01 +02:00
parent f399f032e7
commit 07ed5af300
8 changed files with 398 additions and 233 deletions

View File

@ -21,6 +21,7 @@
#include <cstring>
#include <memory>
#include <renderd7/smart_ctor.hpp>
#include <string>
#include <vector>
@ -28,9 +29,7 @@
#define UNPACK_BGRA(col) (uint8_t)(col >> 8), (col >> 16), (col >> 24), (col)
inline uint32_t RGBA8(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) {
#define ISIMPLEPAK(x, y) (((x)&0xff) << y)
return (ISIMPLEPAK(r, 0) | ISIMPLEPAK(g, 8) | ISIMPLEPAK(b, 16) |
ISIMPLEPAK(a, 24));
return (r | g << 8 | b << 16 | a << 24);
}
typedef int RD7Color;
@ -98,8 +97,7 @@ class Theme {
std::vector<unsigned int> &GetTableRef() { return clr_tab; }
// For Smart Pointer
using Ref = std::shared_ptr<Theme>;
static Ref New() { return std::make_shared<Theme>(); }
RD7_SMART_CTOR(Theme);
// Loader method
void CopyOther(Theme::Ref theme);
@ -188,7 +186,7 @@ class RGBA {
/// @brief Get as Uint32
/// @return color
uint32_t toRGBA() const { return RGBA8(m_r, m_g, m_b, m_a); }
unsigned int toRGBA() const { return RGBA8(m_r, m_g, m_b, m_a); }
// Just calculate the "lightness" f.e. to use Text or Text2
float luminance() const {
@ -202,7 +200,7 @@ class RGBA {
return (luminance() >= 0.5);
}
uint8_t m_r, m_g, m_b, m_a;
uint8_t m_r = 0, m_g = 0, m_b = 0, m_a = 0;
};
std::string RGBA2Hex(unsigned int c32);
/// @brief Convert RGB to Hex

View File

@ -0,0 +1,44 @@
/**
* This file is part of RenderD7
* Copyright (C) 2021-2024 NPI-D7, tobid7
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <map>
#include <renderd7/Color.hpp>
#include <renderd7/Font.hpp>
#include <renderd7/R7Vec.hpp>
#include <renderd7/smart_ctor.hpp>
namespace RenderD7 {
class R2Base {
public:
R2Base();
~R2Base() = default;
void SetFont();
Font::Ref GetFont();
void DefaultFont();
private:
const float default_text_size = 0.5f;
float text_size = 0.5;
Font::Ref font[2];
R7Vec2 max_wh;
std::map<std::string, float> ts;
std::map<std::string, int> mln;
};
} // namespace RenderD7

View File

@ -20,6 +20,7 @@
#include <renderd7/DrawV2.hpp>
#include <renderd7/R7Vec.hpp>
#include <renderd7/smart_ctor.hpp>
// UI7: The new RenderD7 UI Standart based on
// Draw2 (based on Citro2D)
@ -35,6 +36,32 @@ enum UI7MenuFlags_ {
UI7MenuFlags_Scrolling = MAKEFLAG(2),
};
class DrawCmd;
class UI7DrawList {
public:
UI7DrawList() = default;
~UI7DrawList() = default;
void AddRectangle(R7Vec2 pos, R7Vec2 szs, RD7Color clr);
void AddRectangle(R7Vec2 pos, R7Vec2 szs, unsigned int clr);
void AddTriangle(R7Vec2 pos0, R7Vec2 pos1, R7Vec2 pos2, RD7Color clr);
void AddTriangle(R7Vec2 pos0, R7Vec2 pos1, R7Vec2 pos2, unsigned int clr);
void AddText(R7Vec2 pos, const std::string &text, RD7Color clr,
RD7TextFlags flags = 0, R7Vec2 box = R7Vec2());
void AddText(R7Vec2 pos, const std::string &text, unsigned int clr,
RD7TextFlags flags = 0, R7Vec2 box = R7Vec2());
void AddCall(std::shared_ptr<DrawCmd> cmd);
void Process(bool auto_clear = true);
void Clear();
RD7_SMART_CTOR(UI7DrawList)
private:
void AddDebugCall(std::shared_ptr<DrawCmd> cmd);
std::vector<std::shared_ptr<DrawCmd>> list;
};
namespace UI7 {
// Key functions
void Init();
@ -73,4 +100,7 @@ void SetCursorPos(R7Vec2 cp);
void RestoreCursor();
void SameLine();
float GetScrollingOffset();
// DrawLists
UI7DrawList::Ref GetForegroundList();
UI7DrawList::Ref GetBackgroundList();
} // namespace UI7

View File

@ -23,6 +23,7 @@
#include <fstream>
#include <memory>
#include <renderd7/Error.hpp>
#include <renderd7/smart_ctor.hpp>
namespace RenderD7 {
class Font {
@ -30,11 +31,8 @@ class Font {
Font() = default;
Font(const std::string& path) { Load(path); };
~Font() { Unload(); }
using Ref = std::shared_ptr<Font>;
template <typename... args>
inline static Ref New(args&&... a) {
return std::make_shared<Font>(std::forward<args>(a)...);
}
RD7_SMART_CTOR(Font)
void Load(const std::string& path) {
std::ifstream ft(path, std::ios::in | std::ios::binary);
bool io = ft.is_open();

View File

@ -0,0 +1,28 @@
/**
* This file is part of RenderD7
* Copyright (C) 2021-2024 NPI-D7, tobid7
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#define RD7_SMART_CTOR(type) \
using Ref = std::shared_ptr<type>; \
template <typename... args> \
static Ref New(args&&... cargs) { \
return std::make_shared<type>(std::forward<args>(cargs)...); \
}