120 lines
3.3 KiB
C++
120 lines
3.3 KiB
C++
#pragma once
|
|
#include <SDL.h>
|
|
#include <SDL_image.h>
|
|
#include <glad/glad.h>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/ext/matrix_clip_space.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include <map>
|
|
#include <string>
|
|
#include <json.hpp>
|
|
#include "EngineFrame/Render/Texture.h"
|
|
class RenderManager
|
|
{
|
|
public:
|
|
// 绘制纹理的逻辑函数
|
|
using DrawLogicFunc = std::function<void(RefPtr<Texture>, const SDL_Rect *, const SDL_FRect *,
|
|
double, const SDL_FPoint *, SDL_RendererFlip,
|
|
void *)>;
|
|
|
|
struct GL_RenderParams
|
|
{
|
|
GLuint VAO;
|
|
GLuint VBO;
|
|
GLuint EBO;
|
|
std::vector<GLint> UnimLocs;
|
|
DrawLogicFunc DrawFunc = nullptr;
|
|
};
|
|
|
|
private:
|
|
SDL_Window *_window;
|
|
// 窗口尺寸
|
|
int _windowWidth;
|
|
int _windowHeight;
|
|
// 游戏正交投影矩阵
|
|
glm::mat4 _GameOrthoMatrix;
|
|
// UI的正交投影矩阵
|
|
glm::mat4 _UIOrthoMatrix;
|
|
// 渲染的正交投影矩阵
|
|
glm::mat4 _OrthoMatrix;
|
|
/**视口 */
|
|
SDL_Rect _viewport;
|
|
|
|
// 渲染器上下文
|
|
SDL_GLContext _ctx;
|
|
// 着色器程序
|
|
std::map<std::string, GLuint> _shaderProgramMap;
|
|
// 渲染参数
|
|
std::map<std::string, GL_RenderParams> _RenderParamsMap;
|
|
|
|
// 当前使用着色器程序
|
|
GLuint _currentShaderProgram;
|
|
// 当前使用渲染参数
|
|
GL_RenderParams _currentRenderParams;
|
|
|
|
// 渲染透明度
|
|
float opacity_ = 1.0f;
|
|
// 渲染矩阵
|
|
glm::mat4 _currentMatrix;
|
|
|
|
public:
|
|
// 单帧渲染调用次数
|
|
int _frameRenderCount = 0;
|
|
|
|
public:
|
|
RenderManager(SDL_Window *window);
|
|
~RenderManager();
|
|
|
|
/**初始化着色器程序 */
|
|
void InitShaderProgram();
|
|
/**初始化绘制2D纹理缓冲程序 */
|
|
void Init2DTextureProgram();
|
|
/**初始化绘制矩形缓冲程序 */
|
|
void InitRectProgram();
|
|
/**设定当前使用的着色器程序 */
|
|
void SetCurrentShaderProgram(std::string name);
|
|
/**设定当前使用的缓冲对象 */
|
|
void SetCurrentBufferObject(std::string name);
|
|
|
|
/**清空屏幕 */
|
|
void ClearScreen();
|
|
/**交换缓冲区 */
|
|
void SwapBuffer();
|
|
/**设置裁切区域 */
|
|
void SetClipRect(const SDL_Rect *rect);
|
|
/**关闭裁切区域 */
|
|
void CloseClipRect();
|
|
/**设置渲染透明度 */
|
|
void SetOpacity(float opacity);
|
|
/**获取渲染透明度 */
|
|
float GetOpacity();
|
|
/**设置渲染矩阵 */
|
|
void SetMatrix(glm::mat4 matrix);
|
|
/**获取渲染矩阵 */
|
|
glm::mat4 GetMatrix();
|
|
/**设置混合模式 */
|
|
void SetBlendMode(LE_BlEND_MODE blendMode);
|
|
|
|
/**绘制纹理 */
|
|
void DrawTexture(RefPtr<Texture> texture);
|
|
/**绘制矩形 */
|
|
void DrawRect(const SDL_Rect *rect, glm::vec4 color);
|
|
|
|
/**设置正交投影矩阵类型 */
|
|
void SetOrthoMatrixType(int type);
|
|
/**获取渲染的正交投影矩阵 */
|
|
glm::mat4 GetOrthoMatrix();
|
|
/**设置渲染的正交投影矩阵 */
|
|
void SetOrthoMatrix(glm::mat4 matrix);
|
|
/**设置视口 */
|
|
void SetViewport(SDL_Rect viewport);
|
|
/**获取视口 */
|
|
SDL_Rect GetViewport();
|
|
|
|
private:
|
|
// 编译着色器
|
|
GLuint CompileShader(GLenum type, std::string Path);
|
|
// 新增缓冲程序
|
|
void AddBufferObject(std::string name, GL_RenderParams bufferObject);
|
|
};
|