Untitled
unknown
plain_text
a year ago
6.6 kB
2
Indexable
Never
#include <iostream> #include <string> #include <assert.h> #include <stb_image.h> #include "Sprite.h" #include "Timer.h" using namespace std; void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); int setupTexture(string filePath, int& width, int& height); Sprite character, background, mazeWall; const GLuint WIDTH = 800, HEIGHT = 600; void DrawMazeWall(float posX, float posY) { mazeWall.SetPosition(glm::vec3(posX * 32, posY * 32, 0)); mazeWall.UpdateSprite(); mazeWall.DrawSprite(); if (mazeWall.CheckColision(character)) { character.SetPosition(glm::vec3(17 * 32.0, 4 * 32.0, 0.0)); } } int main() { glfwInit(); GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Trabalho GA - Igor Flores e Tiago Gazolla", nullptr, nullptr); glfwMakeContextCurrent(window); glfwSetKeyCallback(window, key_callback); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; } const GLubyte* renderer = glGetString(GL_RENDERER); const GLubyte* version = glGetString(GL_VERSION); cout << "Renderer: " << renderer << endl; cout << "OpenGL version supported " << version << endl; Shader shader("../Shaders/ShaderVS.vs", "../Shaders/ShaderFS.fs"); int spriteWidth, spriteHeight; GLuint texIdle = setupTexture("../../textures/characters/PNG/Rogue/Run_Attack/green.png", spriteWidth, spriteHeight); character.InitializeSprite(1, 1); character.SetTexId(texIdle); character.SetPosition(glm::vec3(17 * 32.0, 4 * 32.0, 0.0)); character.SetDimension(glm::vec3(spriteWidth, spriteHeight, 0)); character.SetShader(&shader); GLuint texBd = setupTexture("../../textures/backgrounds/PNG/Postapocalypce4/Pale/postapocalypse4.png", spriteWidth, spriteHeight); background.InitializeSprite(1, 1); background.SetTexId(texBd); background.SetPosition(glm::vec3(400.0, 300.0, 0)); background.SetDimension(glm::vec3(spriteWidth / 2, spriteHeight / 2, 0)); background.SetShader(&shader); GLuint texWall = setupTexture("../../textures/characters/PNG/Rogue/Run_Attack/red.png", spriteWidth, spriteHeight); mazeWall.InitializeSprite(1, 1); mazeWall.SetTexId(texWall); mazeWall.SetDimension(glm::vec3(spriteWidth, spriteHeight, 0)); mazeWall.SetShader(&shader); shader.Use(); glm::mat4 projection = glm::ortho(0.0, 800.0, 0.0, 600.0, -1.0, 1.0); shader.setMat4("projection", glm::value_ptr(projection)); glActiveTexture(GL_TEXTURE0); shader.setInt("texBuffer", 0); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_ALWAYS); Timer timer; const int CordX = 32; const int CordY = 32; while (!glfwWindowShouldClose(window)) { timer.Start(); glfwPollEvents(); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //fundo background.UpdateSprite(); background.DrawSprite(); //personagem character.UpdateSprite(); character.DrawSprite(); //Bottom Line for (int x = 6; x < 19; x++) { DrawMazeWall(x, 3); } //Top Line for (int x = 6; x < 19; x++) { DrawMazeWall(x, 15); } //Left Line for (int y = 4; y < 15; y++) {DrawMazeWall(6, y); } //Right Line for (int y = 4; y < 15; y++) {DrawMazeWall(18, y); } //Inside Lines (Left to Right - Top to bottom) DrawMazeWall(14, 14); DrawMazeWall( 7, 13); DrawMazeWall( 8, 13); DrawMazeWall( 9, 13); DrawMazeWall(10, 13); DrawMazeWall(12, 13); DrawMazeWall(14, 13); DrawMazeWall(16, 13); DrawMazeWall(10, 12); DrawMazeWall(12, 12); DrawMazeWall(16, 12); DrawMazeWall( 8, 11); DrawMazeWall( 9, 11); DrawMazeWall(10, 11); DrawMazeWall(12, 11); DrawMazeWall(13, 11); DrawMazeWall(14, 11); DrawMazeWall(15, 11); DrawMazeWall(16, 11); DrawMazeWall(10, 10); DrawMazeWall( 7, 9); DrawMazeWall( 8, 9); DrawMazeWall(10, 9); DrawMazeWall(12, 9); DrawMazeWall(14, 9); DrawMazeWall(15, 9); DrawMazeWall(16, 9); DrawMazeWall(12, 8); DrawMazeWall(16, 8); DrawMazeWall( 7, 7); DrawMazeWall( 8, 7); DrawMazeWall( 9, 7); DrawMazeWall(10, 7); DrawMazeWall(11, 7); DrawMazeWall(12, 7); DrawMazeWall(13, 7); DrawMazeWall(14, 7); DrawMazeWall(16, 7); DrawMazeWall(16, 6); DrawMazeWall( 8, 5); DrawMazeWall( 9, 5); DrawMazeWall(10, 5); DrawMazeWall(12, 5); DrawMazeWall(13, 5); DrawMazeWall(14, 5); DrawMazeWall(15, 5); DrawMazeWall(16, 5); DrawMazeWall(17, 5); DrawMazeWall(10, 4); timer.Finish(); double waitingTime = timer.calcWaitingTime(24, timer.getElapsedTimeMs()); if (waitingTime) { std::this_thread::sleep_for(std::chrono::milliseconds((int)waitingTime)); } glfwSwapBuffers(window); } glfwTerminate(); return 0; } void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE); if (key == GLFW_KEY_A && action == GLFW_PRESS) { character.SetAnimation(1); character.MoveLeft(); } else if (key == GLFW_KEY_D && action == GLFW_PRESS) { character.SetAnimation(1); character.MoveRight(); } else if (key == GLFW_KEY_W && action == GLFW_PRESS) { character.MoveUp(); } else if (key == GLFW_KEY_S && action == GLFW_PRESS) { character.MoveDown(); } if (action == GLFW_RELEASE) { character.SetAnimation(0); } } int setupTexture(string filePath, int& width, int& height) { GLuint texID; glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int nrChannels; unsigned char* data = stbi_load(filePath.c_str(), &width, &height, &nrChannels, 0); if (data) { if (nrChannels == 3) //jpg, bmp { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); } else //png { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); } glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout << "Failed to load texture" << std::endl; } stbi_image_free(data); glBindTexture(GL_TEXTURE_2D, 0); return texID; }