#ifndef GLAD #define GLAD #include #endif #include #include #include "font.hpp" #include "text.hpp" #include "shader.hpp" #include "math.h" #include "font/metadata.hpp" Text::Text(){} //makes the text fit in a rectangle Text::Text(const char* s,Rectangle rectangle, float screenRatio): Text{s, CENTER, rectangle.height, rectangle.width, rectangle.x+rectangle.width/2, rectangle.y+rectangle.height/2,screenRatio } { } Text::Text(Positioning positioning,float maxHeight,float maxWidth,float xPos, float yPos,float screenRatio,const char* s): Text{s,positioning, maxHeight, maxWidth, xPos, yPos, screenRatio} { } Text::Text(const char* s,Positioning positioning,float maxHeight,float maxWidth,float xPos, float yPos,float screenRatio){ glGenVertexArrays(1,&VAO); glGenBuffers(1,&VBO); glGenBuffers(1,&EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER,VBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO); glVertexAttribPointer(0,2,GL_FLOAT, GL_FALSE, 4*sizeof(float),NULL); glEnableVertexAttribArray(0); glVertexAttribPointer(1,2,GL_FLOAT, GL_FALSE, 4*sizeof(float),(void*)(2*sizeof(float))); glEnableVertexAttribArray(1); std::vector vertices; std::vector indices; float x=0.0f; for(int i =0; s[i]!='\0'; i++){ char c = s[i]; float y_top = (float)characterMeta[c].bearingY/16.0; float y_bottom = y_top-(float)characterMeta[c].sizeY/16.0; float sizeX= (float)characterMeta[c].sizeX; float sizeY= (float)characterMeta[c].sizeY; vertices.insert(vertices.end(),{ (float)x, y_top, (c%16)/16.0f, (c/16)/8.0f, (float)x, y_bottom, (c%16)/16.0f, (c/16+sizeY/16.0f)/8.0f, (float)x+sizeX/16.0f, y_bottom, (c%16+sizeX/16.0f)/16.0f, (c/16+sizeY/16.0f)/8.0f, (float)x+sizeX/16.0f, y_top, (c%16+sizeX/16.0f)/16.0f, (c/16)/8.0f }); unsigned int s = vertices.size()/4-4; indices.insert(indices.end(), { s+0,s+1,s+2, s+0,s+2,s+3 }); x+= ((float)(characterMeta[c].Advance>>6))/16.0; } glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(float), &vertices[0], GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); amElements= indices.size(); float width,height; //Check if width is not too big if(maxHeight*x *screenRatio< maxWidth){ height= maxHeight; width = maxHeight*x*screenRatio; }else{ height= maxWidth/x/screenRatio; width = maxWidth; } transform.M[0][0]=width/x; transform.M[1][1]=height; switch(positioning){ case(LEFT_BOTTOM): transform.M[0][3]=xPos; transform.M[1][3]=yPos; break; case(CENTER): transform.M[0][3]=xPos-width/2; transform.M[1][3]=yPos-height/2; } } void Text::Delete(){ glDeleteVertexArrays(1,&VAO); glDeleteBuffers(1,&VBO); } void Text::Draw(){ this->shader.activate(); this->shader.setUniformMat4("transform",transform); glBindTexture(GL_TEXTURE_2D,this->texture); glBindVertexArray(VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO); glDrawElements(GL_TRIANGLES,amElements,GL_UNSIGNED_INT,0); } void Text::initFont(){ texture =createTexture("font/combined.png"); shader = Shader("shaders/text.vs","shaders/text.fs"); }