BlockGame/blocks/RenderBase.h

74 lines
1.3 KiB
C
Raw Normal View History

2022-02-05 20:44:31 +00:00
//
// Created by lukas on 05.02.22.
//
#ifndef OPENGLTEST_RENDERBASE_H
#define OPENGLTEST_RENDERBASE_H
2022-02-06 21:53:29 +00:00
#include <iostream>
2022-02-05 20:44:31 +00:00
#include "../gl/Renderer.h"
#include "../gl/Shader.h"
2022-02-06 21:53:29 +00:00
template<class T>
2022-02-05 20:44:31 +00:00
class RenderBase {
2022-02-06 21:53:29 +00:00
private:
static RenderBase *instance;
2022-02-10 17:24:56 +00:00
protected:
RenderBase() {
if (instance) throw std::logic_error("Instance already exists");
instance = this;
}
2022-02-05 20:44:31 +00:00
public:
2022-02-06 21:53:29 +00:00
virtual VertexArray *setVertexArray() = 0;
2022-02-05 20:44:31 +00:00
virtual Shader setShader() = 0;
2022-02-06 21:53:29 +00:00
virtual IndexBuffer *setIndexBuffer() = 0;
static RenderBase *getInstance() {
2022-02-05 20:44:31 +00:00
if (instance == nullptr) {
2022-02-10 17:24:56 +00:00
new T();
2022-02-05 20:44:31 +00:00
instance->init();
}
return instance;
}
2022-02-06 21:53:29 +00:00
public:
2022-02-05 20:44:31 +00:00
Renderer r;
2022-02-06 21:53:29 +00:00
VertexArray *va;
IndexBuffer *ib;
2022-02-05 20:44:31 +00:00
Shader s;
public:
2022-02-06 21:53:29 +00:00
2022-02-10 17:24:56 +00:00
virtual void render() {
2022-02-05 20:44:31 +00:00
r.render(*va, *ib, s);
}
2022-02-06 21:53:29 +00:00
2022-02-05 20:44:31 +00:00
void init() {
s = setShader();
va = setVertexArray();
ib = setIndexBuffer();
}
2022-02-10 17:24:56 +00:00
void deinit() {
glDeleteProgram(s.getHandle());
}
2022-02-06 21:53:29 +00:00
unsigned getMVPhandle() {
2022-02-05 20:44:31 +00:00
return s.getUniformHandle("MVP");
}
2022-02-06 21:53:29 +00:00
unsigned getUniformhandle(std::string name) {
2022-02-05 20:44:31 +00:00
return s.getUniformHandle(std::move(name));
}
};
template<class T>
RenderBase<T> *RenderBase<T>::instance = nullptr;
#endif //OPENGLTEST_RENDERBASE_H