[ad_1]
Main.cpp
//Variables
const unsigned int width = 896, top = 504;
//Initiating GLFW Window
glfwInit();
glfwWindowTrace(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowTrace(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowTrace(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//Creating a GLFW window
window = glfwCreateWindow(width, top, "Jaguar", NULL, NULL);
//Checking if Window was initiated
if (window == NULL) {
std::cout << "GLFW FAILED TO INITIATE WINDOW!n";
glfwTerminate();
}
glfwMakeContextCurrent(window);
//Centering Window
int windowWidth, windowHeight;
glfwGetWindowDimension(window, &windowWidth, &windowHeight);
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, mode->width / 2 - windowWidth / 2, mode->top / 2 - windowHeight / 2);
//Setting-Up window's icon
GLFWimage icon[1];
icon[0].pixels = stbi_load("sources/pictures/gui/icon/icon.png", &icon[0].width, &icon[0].top, 0, 4);
glfwSetWindowIcon(window, 1, icon);
stbi_image_free(icon[0].pixels);
//Checking if Glad was initiated
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "GLAD FAILED TO BE INITIATEDn";
}
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
stbi_set_flip_vertically_on_load(true);
//Setting-Up Viewport
glViewport(0, 0, width, top);
//Intitiating MainMenu
states.push(new MainMenuState(*window, &states));
font.cpp
FT_Library ft;
if (FT_Init_FreeType(&ft)) {
std::cout << "FREETYPE::Failed to initialize libraryn";
}
FT_Face face;
if (FT_New_Face(ft, filePath, 0, &face)) {
std::cout << "FREETYPE::Failed to load to font: " << filePath << "n";
}
// set dimension to load glyphs as
FT_Set_Pixel_Sizes(face, 0, px);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (unsigned char c = 0; c < 128; c++) {
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
std::cout << "FREETYPE::Failed to load glpyhn";
}
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width,
face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Character character = {
texture,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top)
,face->glyph->advance.x };
Characters.insert(std::pair<char, Character>(c, character));
}
FT_Done_Face(face);
FT_Done_FreeType(ft);
textual content.cpp
// activate corresponding render state
shader.use();
glUniform3f(glGetUniformLocation(shader.id, "textColor"), coloration.x, coloration.y,
coloration.z);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO);
// iterate by way of all characters
std::string::const_iterator c;
for (c = string.start(); c != string.finish(); c++)
{
Character ch = font.Characters[*c];
float xpos = place.x + ch.bearing.x * scale;
float ypos = place.y - (ch.dimension.y - ch.bearing.y) * scale;
float w = ch.dimension.x * scale;
float h = ch.dimension.y * scale;
// replace VBO for every character
float vertices[6][4] = {
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos, ypos, 0.0f, 1.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
{ xpos + w, ypos + h, 1.0f, 0.0f }
};
// render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.textureId);
// replace content material of VBO reminiscence
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// render quad
glDrawArrays(GL_TRIANGLES, 0, 6);
// now advance cursors for subsequent glyph (observe that advance is variety of
1/64 pixels)
place.x += (ch.advance >> 6) * scale; // bitshift by 6 to get worth in
pixels (2^6 = 64)
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
[ad_2]
