-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRenderer.h
129 lines (112 loc) · 3.62 KB
/
Renderer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <map>
#include "Camera.h"
#include "SpriteInfo.h"
#include "EntityCoordinator.h"
#include "RenderComponent.h"
#include "TextComponent.h"
#include "Transform.h"
#include "file_manager.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stb/stb_image.h>
#include "Animator.h"
#include "Character.h"
#include "ShaderFactory.h"
#include "NBSemaphore.h"
extern GLFWwindow* window;
enum class WindowSize {
WINDOWED,
MAXIMIZED_WINDOWED,
FULLSCREEN
};
struct ImgConfig {
string name;
int rows;
int columns;
Animation anims[5];
int width;
int height;
stbi_uc* imgData;
};
class Renderer : public IObserver
{
public:
int init(int viewWidth, int viewHeight, glm::vec4 newBackgroundColor, WindowSize windowSize);
void setCamViewSize(int viewWidth, int viewHeight);
int update(EntityCoordinator* coordinator);
int teardown(bool closeWindow);
static Renderer* getInstance();
ImgConfig* configs[8];
Animation* getAnimation(std::string animName, std::string spriteName);
int getWindowWidth();
int getWindowHeight();
Interpolator* getTextXInterpolator();
Interpolator* getTextYInterpolator();
void setWindowWidth(int width);
void setWindowHeight(int height);
NBSemaphore* imgReaderSem;
NBSemaphore* imgWriterSem;
Camera* getCamera();
// event receiver
void Receive(Event e, void* args) override;
private:
static Renderer* renderer;
// the vertex array object (VAO)
// this describes how the vertex attributes are
// stored in the a vertex buffer object (VBO)
// see the glVertexAttribPointer function calls
// to understand how it works
GLuint vertexAttribs;
// the vertex buffer object (VBO) containing the
// vertex position data
GLuint positionBuffer;
// the vertex buffer object (VBO) containing the
// vertex tex coordinates data
GLuint texCoordBuffer;
// the element buffer object (EBO) contains the vertex indices
GLuint indicesBuffer;
float time;
int counter;
// store the sprites that have been read
// from the image files
std::map<std::string, SpriteInfo> sprites;
// store the text characters
std::map<unsigned char, Character> characters;
glm::mat4 textProjectionMat;
// the background color of the scene
glm::vec4 backgroundColor;
// size info of the window and camera
int windowWidth;
int windowHeight;
// helper classes
ShaderFactory shaderFactory;
Camera camera;
// for the texts' transforms
// since it relies on screenspace, it fits in Renderer more
Interpolator textPosInterpolX;
Interpolator textPosInterpolY;
GLFWwindow* setupGLFW(WindowSize windowSize);
static void windowedResizedCallback(GLFWwindow* window, int width, int height);
void resizeWindow(int width, int height);
void updateInterpolation();
void prepareGLBuffers();
void resetVerticesData(bool flipUV);
GLuint createTexBuffer(int height, int width, unsigned char* imgData);
void loadTextLibrary();
void loadImagesOld();
void loadImages();
void loadIntoImageData();
void updateTexCoord(RenderComponent comp, SpriteInfo& info);
void drawText(TextComponent* text, Transform* transform);
int findTextWidth(TextComponent* text);
void startDrawGameObjectsPhase(EntityCoordinator* coordinator);
void startDrawUIPhase(EntityCoordinator* coordinator);
void startDrawTextPhase(EntityCoordinator* coordinator);
};