- item_config, entity_config and mod_manager take over their families as idiomatic classes (std containers, unique_ptr, bitset tear flags). Every big loader reads the imported JSON: entities2.xml gives 1349 entities. - manager.h/.cpp take over Manager (g_manager) and IsaacStartup. Members are the curated subsystems by value. - kage_graphics, kage_input and kage_sound take over KAGE's graphics (shaders, render targets, batches, PNG through stb_image), input (SDL3 keyboard, mouse, gamepads) and sound (SDL3 audio, wav and stb_vorbis ogg, pitch and volume). - source_tree.py: classes inside a namespace block in src/ headers count as taken over, and a still-generated class's vtable slot keeps its stub unless src/ defines that exact method. build/isaac opens the window, sets up GL 4.6, framebuffers and PulseAudio, logs the version, and stops at LuaEngine::Init. 522 functions taken over, boot path 156/615. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
379 lines
11 KiB
C++
379 lines
11 KiB
C++
#pragma once
|
|
|
|
#include "classes.h"
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
namespace KAGE::Graphics
|
|
{
|
|
class ImageBase;
|
|
class RenderBatch;
|
|
|
|
using ImagePtr = std::shared_ptr<ImageBase>;
|
|
|
|
constexpr int QUAD_CORNERS = 4;
|
|
constexpr int TEXTURE_SLOTS = 8;
|
|
// The depth the first quad of a frame is drawn at, and how much nearer each next one is.
|
|
constexpr float DEPTH_START = -1;
|
|
constexpr float DEPTH_STEP = 0.01f;
|
|
constexpr const char* COLOR_SHADER = "KAGE_ColorShader";
|
|
constexpr const char* COLOR_TEXTURE_SHADER = "KAGE_ColorTextureShader";
|
|
constexpr const char* INDEXED_TEXTURE_SHADER = "KAGE_IndexedTextureShader";
|
|
|
|
// REPENTOGON's eBlendEquation.
|
|
enum BlendEquation
|
|
{
|
|
EQUATION_ADD,
|
|
EQUATION_SUBTRACT,
|
|
EQUATION_REVERSE_SUBTRACT,
|
|
EQUATION_MIN,
|
|
EQUATION_MAX
|
|
};
|
|
|
|
// REPENTOGON's eBlendFactor.
|
|
enum BlendFactor
|
|
{
|
|
FACTOR_ZERO,
|
|
FACTOR_ONE,
|
|
FACTOR_SRC_COLOR,
|
|
FACTOR_ONE_MINUS_SRC_COLOR,
|
|
FACTOR_DST_COLOR,
|
|
FACTOR_ONE_MINUS_DST_COLOR,
|
|
FACTOR_SRC_ALPHA,
|
|
FACTOR_ONE_MINUS_SRC_ALPHA,
|
|
FACTOR_DST_ALPHA,
|
|
FACTOR_ONE_MINUS_DST_ALPHA
|
|
};
|
|
|
|
// The blend modes BlendMode::SetMode knows.
|
|
enum BlendType
|
|
{
|
|
BLEND_OPAQUE,
|
|
BLEND_NORMAL,
|
|
BLEND_ADDITIVE
|
|
};
|
|
|
|
// REPENTOGON's eVertexAttributeFormat: float vectors of 1 to 4 components, and the three slots images fill in.
|
|
// Format 8 is one more single float.
|
|
enum VertexFormat
|
|
{
|
|
VERTEX_END,
|
|
VERTEX_FLOAT,
|
|
VERTEX_VEC2,
|
|
VERTEX_VEC3,
|
|
VERTEX_VEC4,
|
|
VERTEX_POSITION,
|
|
VERTEX_COLOR,
|
|
VERTEX_TEXCOORD,
|
|
VERTEX_FLOAT_OTHER,
|
|
VERTEX_FORMAT_COUNT
|
|
};
|
|
|
|
// A shader uniform's type. Types below UNIFORM_FLOAT are texture slots.
|
|
enum UniformType
|
|
{
|
|
UNIFORM_FLOAT = TEXTURE_SLOTS,
|
|
UNIFORM_VEC2,
|
|
UNIFORM_VEC3,
|
|
UNIFORM_VEC4,
|
|
UNIFORM_INT,
|
|
UNIFORM_IVEC2,
|
|
UNIFORM_IVEC3,
|
|
UNIFORM_IVEC4,
|
|
UNIFORM_UINT,
|
|
UNIFORM_UVEC2,
|
|
UNIFORM_UVEC3,
|
|
UNIFORM_UVEC4,
|
|
UNIFORM_MAT2,
|
|
UNIFORM_MAT2X3,
|
|
UNIFORM_MAT2X4,
|
|
UNIFORM_MAT3X2,
|
|
UNIFORM_MAT3,
|
|
UNIFORM_MAT3X4,
|
|
UNIFORM_MAT4X2,
|
|
UNIFORM_MAT4X3,
|
|
UNIFORM_MAT4,
|
|
UNIFORM_TYPE_END
|
|
};
|
|
|
|
// How an image's texels are stored: palette indices, RGB or RGBA, one byte a channel.
|
|
enum ImageFormat
|
|
{
|
|
IMAGE_INDEXED = 0,
|
|
IMAGE_RGB = 6,
|
|
IMAGE_RGBA = 8,
|
|
IMAGE_NONE = 11
|
|
};
|
|
|
|
// The file types image_create decodes: the image files, and raw RGBA (16-bit width and height, then the rows).
|
|
enum ImageFileType
|
|
{
|
|
FILE_PNG,
|
|
FILE_JPG,
|
|
FILE_BMP,
|
|
FILE_PCX,
|
|
FILE_RGBA,
|
|
FILE_PLATFORM
|
|
};
|
|
|
|
// Whether a source quad's corners are texel positions or already fractions of the image.
|
|
enum CoordinateSpace
|
|
{
|
|
COORDINATES_NORMALIZED,
|
|
COORDINATES_PIXEL
|
|
};
|
|
|
|
// Whether a render target is drawn at its own size or with the screen's projection.
|
|
enum TargetSize
|
|
{
|
|
TARGET_OWN_SIZE,
|
|
TARGET_SCREEN_SIZE
|
|
};
|
|
|
|
// Whether Present ends the frame on the screen (swapping) or only clears the depth of the bound render target.
|
|
enum PresentMode
|
|
{
|
|
PRESENT_FLUSH,
|
|
PRESENT_FINISH
|
|
};
|
|
|
|
// How a draw blends into its target: the equation, then the color and alpha factors.
|
|
struct BlendMode
|
|
{
|
|
BlendEquation equation = EQUATION_ADD;
|
|
BlendFactor rgbSource = FACTOR_SRC_ALPHA;
|
|
BlendFactor rgbDest = FACTOR_ONE_MINUS_SRC_ALPHA;
|
|
BlendFactor alphaSource = FACTOR_ONE;
|
|
BlendFactor alphaDest = FACTOR_ONE_MINUS_SRC_ALPHA;
|
|
|
|
bool operator==(const BlendMode&) const = default;
|
|
void SetMode(BlendType type);
|
|
};
|
|
|
|
constexpr BlendMode BLEND_MODE_OPAQUE = {EQUATION_ADD, FACTOR_ONE, FACTOR_ZERO, FACTOR_ONE, FACTOR_ZERO};
|
|
|
|
// A destination quad with a color at each corner, in the order of its corners.
|
|
struct ColorQuad : DestinationQuad
|
|
{
|
|
std::array<KColor, QUAD_CORNERS> colors = {};
|
|
|
|
ColorQuad() = default;
|
|
ColorQuad(const DestinationQuad& quad, KColor color);
|
|
};
|
|
|
|
// A vertex attribute a shader reads, by name.
|
|
struct VertexAttribute
|
|
{
|
|
std::string name;
|
|
VertexFormat format = VERTEX_END;
|
|
};
|
|
|
|
// The GLSL of a shader's two stages.
|
|
struct ShaderSource
|
|
{
|
|
std::string_view vertex;
|
|
std::string_view fragment;
|
|
};
|
|
|
|
// A uniform the game set by name, with every value it took since the frame began: a batch replays the values that
|
|
// were current when it was recorded.
|
|
struct ShaderUniform
|
|
{
|
|
std::string name;
|
|
std::vector<std::vector<uint8_t>> values;
|
|
UniformType type = UNIFORM_FLOAT;
|
|
};
|
|
|
|
// KAGE's GLSL program (ShaderBase and Shader): its vertex attributes, the textures it samples, and its uniforms with
|
|
// their history. uniformState holds the index of each uniform's current value.
|
|
class Shader
|
|
{
|
|
public:
|
|
std::array<std::string, TEXTURE_SLOTS> textureNames;
|
|
std::vector<VertexAttribute> attributes;
|
|
std::vector<ShaderUniform> uniforms;
|
|
std::vector<uint32_t> uniformState;
|
|
std::string name;
|
|
unsigned int program = 0;
|
|
bool isLoaded = false;
|
|
bool isUsed = false;
|
|
|
|
~Shader();
|
|
bool load(std::string_view shaderName, std::span<const VertexAttribute> vertexAttributes, ShaderSource source);
|
|
void Shutdown();
|
|
void apply(const std::vector<uint32_t>& state);
|
|
void frame_end();
|
|
void set_uniform(std::string_view uniformName, UniformType type, const void* data);
|
|
ShaderUniform* find_uniform(std::string_view uniformName);
|
|
int vertex_size_get() const;
|
|
void attributes_enable(const float* vertices, int stride);
|
|
void attributes_disable();
|
|
|
|
private:
|
|
bool compile(ShaderSource source);
|
|
void texture_bind(int slot, const ImageBase& image);
|
|
void uniform_upload(const ShaderUniform& uniform, const void* data);
|
|
};
|
|
|
|
// A texture the renderer draws from: a decoded image file, raw pixels, or a render target the game draws into.
|
|
// The texture is padded to powers of two, and uvScale takes the image's far corner to the padded texture's.
|
|
// batches lists the opaque batches it drew into this frame, which later opaque draws of it may join.
|
|
class ImageBase : public std::enable_shared_from_this<ImageBase>
|
|
{
|
|
public:
|
|
std::vector<RenderBatch*> batches;
|
|
std::string name;
|
|
std::string path;
|
|
ImageFormat format = IMAGE_NONE;
|
|
float uvScaleX = 0;
|
|
float uvScaleY = 0;
|
|
int height = 0;
|
|
int textureHeight = 0;
|
|
int textureWidth = 0;
|
|
int width = 0;
|
|
int magFilter = -1;
|
|
int minFilter = -1;
|
|
int wrapS = -1;
|
|
int wrapT = -1;
|
|
unsigned int texture = 0;
|
|
bool isLoaded = false;
|
|
bool isRenderTarget = false;
|
|
|
|
ImageBase() = default;
|
|
ImageBase(const ImageBase&) = delete;
|
|
~ImageBase();
|
|
|
|
float* Render(std::span<const SourceQuad> sources, std::span<const ColorQuad> quads);
|
|
float* Render(const SourceQuad& source, const ColorQuad& quad, const KColor& color);
|
|
float* Render(const SourceQuad& source, const Vector& position, const KColor& color);
|
|
float* Render(const ColorQuad& quad, const KColor& color);
|
|
float* Render(const Vector& position, const KColor& color);
|
|
void bind(int slot);
|
|
bool set_size(int imageWidth, int imageHeight);
|
|
bool texels_load(std::vector<uint8_t>& texels);
|
|
void filter_set(int min, int mag);
|
|
void wrap_set(int s, int t);
|
|
void loaded_set();
|
|
};
|
|
|
|
// One draw call's worth of quads: the image, blend mode and shader they share, with the shader's uniform values when
|
|
// the batch began. An opaque batch (BLEND_MODE_OPAQUE) can take quads drawn later with the same state.
|
|
// Destruction (00a66d90) is the default. The vertex and index buffers (GraphicsBufferObject: 00a67fe0, 00a680a0,
|
|
// 00a680d0, 00a68160, 00a68210, 00a68240, 00a68280, 00a68320, 00a68350, 00a68360) are std::vector.
|
|
class RenderBatch
|
|
{
|
|
public:
|
|
BlendMode blendMode;
|
|
ImagePtr image;
|
|
std::vector<uint32_t> uniformState;
|
|
std::vector<float> vertices;
|
|
ImageBase* imageKey = nullptr;
|
|
Shader* shader = nullptr;
|
|
bool isListed = false;
|
|
bool isOpaque = false;
|
|
|
|
void set_state(ImageBase* target);
|
|
bool is_state_equal(const ImageBase* target) const;
|
|
float* quads_add(std::span<const SourceQuad> sources, std::span<const ColorQuad> quads);
|
|
void submit();
|
|
};
|
|
|
|
// What a render target is made with: its size, its name in the image cache and the color its texels start as.
|
|
struct RenderTargetDesc
|
|
{
|
|
KColor clearColor = {};
|
|
std::string name;
|
|
int height = 0;
|
|
int width = 0;
|
|
};
|
|
|
|
// A render target pushed with Manager::render_target_push, to go back to.
|
|
struct RenderTargetEntry
|
|
{
|
|
ImageBase* target = nullptr;
|
|
TargetSize size = TARGET_OWN_SIZE;
|
|
};
|
|
|
|
// KAGE's Graphics::Manager, with its image and shader managers: the GL state, the batches of the frame, the render
|
|
// target stack and the image and shader caches. The window and its framebuffer are Platform's.
|
|
// Images and render targets are cached by their normalized path or name, holding them only while used elsewhere.
|
|
class Manager
|
|
{
|
|
public:
|
|
std::array<ImageBase*, TEXTURE_SLOTS> textures = {};
|
|
std::unordered_multimap<std::string, std::weak_ptr<ImageBase>> images;
|
|
std::unordered_map<std::string, std::unique_ptr<Shader>> shaders;
|
|
std::vector<std::unique_ptr<RenderBatch>> batchPool;
|
|
std::vector<RenderBatch*> freeBatches;
|
|
std::vector<RenderBatch*> pendingBatches;
|
|
std::vector<RenderTargetEntry> renderTargets;
|
|
std::vector<Shader*> shaderStack;
|
|
std::vector<Shader*> usedShaders;
|
|
std::vector<uint16_t> quadIndices;
|
|
BlendMode blendMode;
|
|
KColor clearColor = {};
|
|
ImageBase* renderTarget = nullptr;
|
|
RenderBatch* currentBatch = nullptr;
|
|
Shader* currentShader = nullptr;
|
|
float depth = DEPTH_START;
|
|
float orthographicHeight = 1080;
|
|
float orthographicWidth = 1920;
|
|
int defaultMagFilter = 0;
|
|
int defaultMinFilter = 0;
|
|
int defaultWrapS = 1;
|
|
int defaultWrapT = 1;
|
|
bool isCullOffscreen = true;
|
|
bool isInitialized = false;
|
|
bool isPixelSnap = true;
|
|
bool isPremultipliedAlpha = false;
|
|
bool isScreenTarget = true;
|
|
bool isSrgb = false;
|
|
bool isTargetOffset = false;
|
|
|
|
bool Init();
|
|
void Shutdown();
|
|
void Clear();
|
|
void Present(PresentMode mode);
|
|
void SetRenderTarget(ImageBase* target, TargetSize size);
|
|
void render_target_push(ImageBase* target, TargetSize size, PresentMode mode);
|
|
void render_target_pop(PresentMode mode);
|
|
void update_projection();
|
|
void clear_color_set(float red, float green, float blue);
|
|
void blend_set(BlendType type);
|
|
void batches_flush();
|
|
RenderBatch* batch_get(ImageBase* image);
|
|
void quads_draw(int vertexCount);
|
|
float view_width_get();
|
|
float view_height_get();
|
|
int target_width_get();
|
|
int target_height_get();
|
|
|
|
bool shader_create(std::string_view shaderName, std::span<const VertexAttribute> attributes, ShaderSource source);
|
|
bool shader_load(std::string_view shaderName, std::span<const VertexAttribute> attributes, ShaderSource files);
|
|
Shader* shader_find(std::string_view shaderName);
|
|
bool shader_default_set();
|
|
bool shader_push(std::string_view shaderName);
|
|
void shader_pop();
|
|
void shader_use(Shader& shader, const std::vector<uint32_t>& state);
|
|
|
|
ImagePtr image_load(std::string_view imagePath);
|
|
ImagePtr image_create(std::span<const uint8_t> file, ImageFileType type, std::string_view imageName);
|
|
ImagePtr render_target_create(const RenderTargetDesc& desc);
|
|
ImagePtr image_find(std::string_view imagePath);
|
|
void image_cache(ImageBase& image);
|
|
};
|
|
|
|
extern Manager manager;
|
|
|
|
std::string path_key(std::string_view path);
|
|
ColorQuad color_quad_rect(const Vector& position, const Vector& size, const KColor& color);
|
|
SourceQuad source_quad_rect(const Vector& position, const Vector& size);
|
|
bool is_quad_on_screen(const DestinationQuad& quad);
|
|
}
|