Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- OUR FIRST VIDEO GAME
- This is our first attempt at creating a video game using OpenGL and C++.
- The team members are, from youngest to oldest:
- - Toni, Ugo, Vanja.
- The project started on Saturday, July 30, 2016, after a straight week of learning OpenGL without stopping.
- After a short discussion in a local cafe, we decided what the main concepts of the game would be, and what
- problems we'll be looking to solve while developing the game. The game is basically a prototype for our
- upcoming gargantuan project, (codename: Sektor Exitium) so it leaves much to be desired for, which is fine.
- The basic premise is that you're a jailbird on a prison colony planet, with the goal of fleeing from the
- facility. This way, we get to experiment with various game mechanics, while avoiding the more complicated
- issues such as AI, procedural generation of open worlds, complex combat and crafting systems, etc...
- Here's to a successful first project!
- */
- #include <iostream> // STD includes
- #include <cstdlib>
- #include <math.h>
- #include <string>
- #include <conio.h>
- #define GLEW_STATIC
- #include <GL/glew.h> // GLEW includes
- #include <GLFW/glfw3.h> // GLFW includes
- #include "Shader.h" // GL includes
- #include <glm/glm.hpp> // GLM Mathematics
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <SOIL.h> // Other Libs
- using namespace std;
- using namespace glm;
- // Window properties
- GLuint screenWidth = 1200, screenHeight = 675;
- // Function prototypes
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
- void mouse_callback(GLFWwindow* window, double xpos, double ypos);
- int nextTile, previousTile;
- int h = 20, w = 36, a, b;
- int x = 2, y = 2;
- int Matrix[20][36] =
- {
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
- { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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out << endl;
- for (b = 0; b < w; b++) {
- cout << Matrix[a][b] << ' ';
- }
- }
- // Sets up everything window related using GLFW
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- glfwWindowHint(GLFW_SAMPLES, 4);
- GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "Sektor Exitium: Escape", nullptr, nullptr);
- glfwMakeContextCurrent(window); // Start the application windowed
- glfwSetKeyCallback(window, key_callback); // Set the required callback functions
- glfwSetCursorPosCallback(window, mouse_callback);
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); // Cursor options
- glewExperimental = GL_TRUE; // Initialize GLEW to setup the OpenGL Function pointers
- glewInit();
- glViewport(0, 0, screenWidth, screenHeight); // Define the viewport dimensions
- Shader ourShader("shader.vs", "shader.frag"); // Shader setup
- // Set up vertex data (and buffer(s)) and attribute pointers
- GLfloat vertices[] = {
- // Positions // Colors // Texture Coords
- 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right
- 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Bottom Right
- -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Left
- -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // Top Left
- };
- GLuint indices[] = { // Note that we start from 0!
- 0, 1, 3, // First Triangle
- 1, 2, 3 // Second Triangle
- };
- GLuint VBO, VAO, EBO;
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
- // Position attribute
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
- glEnableVertexAttribArray(0);
- // Color attribute
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
- glEnableVertexAttribArray(1);
- // TexCoord attribute
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
- glEnableVertexAttribArray(2);
- glBindVertexArray(0); // Unbind VAO
- // Load and create a texture
- GLuint texture1;
- GLuint texture2;
- // TEXTURE 1
- glGenTextures(1, &texture1);
- glBindTexture(GL_TEXTURE_2D, texture1); // All upcoming GL_TEXTURE_2D operations now have effect on our texture object
- // Set our texture parameters
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- // Set texture filtering
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- // Load, create texture and generate mipmaps
- int width, height;
- unsigned char* image = SOIL_load_image("Door.png", &width, &height, 0, SOIL_LOAD_RGB);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- glGenerateMipmap(GL_TEXTURE_2D);
- SOIL_free_image_data(image);
- glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture when done, so we won't accidentily mess up our texture.
- // TEXTURE 2
- glGenTextures(1, &texture2);
- glBindTexture(GL_TEXTURE_2D, texture2);
- // Set our texture parameters
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- // Set texture filtering
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- // Load, create texture and generate mipmaps
- image = SOIL_load_image("Wallpng.png", &width, &height, 0, SOIL_LOAD_RGB);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- glGenerateMipmap(GL_TEXTURE_2D);
- SOIL_free_image_data(image);
- glBindTexture(GL_TEXTURE_2D, 0);
- // GAME LOOP STARTS HERE
- while (!glfwWindowShouldClose(window))
- {
- glfwPollEvents();
- glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- // Bind Textures using texture units
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, texture1);
- glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, texture2);
- glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture2"), 1);
- ourShader.Use();
- // Create transformations
- glm::mat4 model;
- glm::mat4 view;
- glm::mat4 projection;
- model = glm::translate(model, glm::vec3(-2.0f, 0.0f, 0.0f));
- model = glm::scale(model, glm::vec3(0.05f));
- view = glm::translate(view, glm::vec3(0.0f, 0.0f, -1.0f));
- projection = glm::perspective(45.0f, (GLfloat)screenWidth / (GLfloat)screenHeight, 0.1f, 100.0f);
- // Get their uniform locations and pass to shaders
- GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
- glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
- GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
- glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
- GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
- glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
- glBindVertexArray(VAO);
- for (GLuint i = 0; i < 10; i++)
- {
- // Calculate the model matrix for each object and pass it to shader before drawing
- glm::mat4 model;
- model = glm::scale(model, glm::vec3(0.05f));
- model = glm::translate(model, vec3((GLfloat)(i - 5.0f), (GLfloat)(i), 0.0f));
- glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- }
- // Draw container
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- glfwSwapBuffers(window);
- }
- // GAME LOOP ENDS HERE
- // Properly de-allocate all resources once they've outlived their purpose
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &VBO);
- glDeleteBuffers(1, &EBO);
- // Terminate GLFW, clearing any resources allocated by GLFW.
- glfwTerminate();
- return 0;
- }
- // Function for keyboard input
- 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_UP && action == GLFW_PRESS) {
- if (Matrix[y - 1][x] == 1) {
- cout << "You hit a wall!";
- }
- else {
- Matrix[y][x] = 0;
- Matrix[y - 1][x] = 5;
- y = y - 1;
- for (a = 0; a < h; a++) {
- cout << endl;
- for (b = 0; b < w; b++) {
- cout << Matrix[a][b] << ' ';
- }
- }
- }
- }
- if (key == GLFW_KEY_DOWN && action == GLFW_PRESS) {
- if (Matrix[y + 1][x] == 1) {
- cout << "You hit a wall!";
- }
- else {
- Matrix[y][x] = 0;
- Matrix[y + 1][x] = 5;
- y = y + 1;
- for (a = 0; a < h; a++) {
- cout << endl;
- for (b = 0; b < w; b++) {
- cout << Matrix[a][b] << ' ';
- }
- }
- }
- }
- if (key == GLFW_KEY_LEFT && action == GLFW_PRESS) {
- if (Matrix[y][x - 1] == 1) {
- cout << "You hit a wall!";
- }
- else {
- Matrix[y][x] = 0;
- Matrix[y][x - 1] = 5;
- x = x - 1;
- for (a = 0; a < h; a++) {
- cout << endl;
- for (b = 0; b < w; b++) {
- cout << Matrix[a][b] << ' ';
- }
- }
- }
- }
- if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) {
- if (Matrix[y][x + 1] == 1) {
- cout << "You hit a wall!";
- }
- else {
- Matrix[y][x] = 0;
- Matrix[y][x + 1] = 5;
- x = x + 1;
- for (a = 0; a < h; a++) {
- cout << endl;
- for (b = 0; b < w; b++) {
- cout << Matrix[a][b] << ' ';
- }
- }
- }
- }
- }
- // Function for mouse input
- void mouse_callback(GLFWwindow * window, double xpos, double ypos)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement