Advertisement
Kirabo

Untitled

Jul 31st, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.92 KB | None | 0 0
  1. /*
  2. OUR FIRST VIDEO GAME
  3.  
  4. This is our first attempt at creating a video game using OpenGL and C++.
  5. The team members are, from youngest to oldest:
  6. - Toni, Ugo, Vanja.
  7.  
  8. The project started on Saturday, July 30, 2016, after a straight week of learning OpenGL without stopping.
  9. After a short discussion in a local cafe, we decided what the main concepts of the game would be, and what
  10. problems we'll be looking to solve while developing the game. The game is basically a prototype for our
  11. upcoming gargantuan project, (codename: Sektor Exitium) so it leaves much to be desired for, which is fine.
  12.  
  13. The basic premise is that you're a jailbird on a prison colony planet, with the goal of fleeing from the
  14. facility. This way, we get to experiment with various game mechanics, while avoiding the more complicated
  15. issues such as AI, procedural generation of open worlds, complex combat and crafting systems, etc...
  16.  
  17. Here's to a successful first project!
  18. */
  19.  
  20. #include <iostream> // STD includes
  21. #include <cstdlib>
  22. #include <math.h>
  23. #include <string>
  24. #include <conio.h>
  25.  
  26. #define GLEW_STATIC
  27.  
  28. #include <GL/glew.h> // GLEW includes
  29.  
  30. #include <GLFW/glfw3.h> // GLFW includes
  31.  
  32. #include "Shader.h" // GL includes
  33.  
  34. #include <glm/glm.hpp> // GLM Mathematics
  35. #include <glm/gtc/matrix_transform.hpp>
  36. #include <glm/gtc/type_ptr.hpp>
  37.  
  38. #include <SOIL.h> // Other Libs
  39.  
  40. using namespace std;
  41. using namespace glm;
  42.  
  43. // Window properties
  44. GLuint screenWidth = 1200, screenHeight = 675;
  45.  
  46. // Function prototypes
  47. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
  48. void mouse_callback(GLFWwindow* window, double xpos, double ypos);
  49.  
  50. int nextTile, previousTile;
  51. int h = 20, w = 36, a, b;
  52. int x = 2, y = 2;
  53. int Matrix[20][36] =
  54. {
  55. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  74. { 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 }
  75. };
  76.  
  77. int main() {
  78.  
  79. system("COLOR 0B");
  80.  
  81. Matrix[x][y] = 5;
  82. previousTile = 0;
  83.  
  84. for (a = 0; a < h; a++) {
  85. cout << endl;
  86. for (b = 0; b < w; b++) {
  87. cout << Matrix[a][b] << ' ';
  88. }
  89. }
  90.  
  91. // Sets up everything window related using GLFW
  92. glfwInit();
  93. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  94. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  95. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  96. glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  97. glfwWindowHint(GLFW_SAMPLES, 4);
  98.  
  99. GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "Sektor Exitium: Escape", nullptr, nullptr);
  100. glfwMakeContextCurrent(window); // Start the application windowed
  101.  
  102. glfwSetKeyCallback(window, key_callback); // Set the required callback functions
  103. glfwSetCursorPosCallback(window, mouse_callback);
  104.  
  105.  
  106. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); // Cursor options
  107.  
  108.  
  109. glewExperimental = GL_TRUE; // Initialize GLEW to setup the OpenGL Function pointers
  110. glewInit();
  111.  
  112. glViewport(0, 0, screenWidth, screenHeight); // Define the viewport dimensions
  113.  
  114. Shader ourShader("shader.vs", "shader.frag"); // Shader setup
  115.  
  116. // Set up vertex data (and buffer(s)) and attribute pointers
  117. GLfloat vertices[] = {
  118. // Positions // Colors // Texture Coords
  119. 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right
  120. 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Bottom Right
  121. -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Left
  122. -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // Top Left
  123. };
  124. GLuint indices[] = { // Note that we start from 0!
  125. 0, 1, 3, // First Triangle
  126. 1, 2, 3 // Second Triangle
  127. };
  128. GLuint VBO, VAO, EBO;
  129. glGenVertexArrays(1, &VAO);
  130. glGenBuffers(1, &VBO);
  131. glGenBuffers(1, &EBO);
  132.  
  133. glBindVertexArray(VAO);
  134.  
  135. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  136. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  137.  
  138. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  139. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  140.  
  141. // Position attribute
  142. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
  143. glEnableVertexAttribArray(0);
  144. // Color attribute
  145. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
  146. glEnableVertexAttribArray(1);
  147. // TexCoord attribute
  148. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
  149. glEnableVertexAttribArray(2);
  150.  
  151. glBindVertexArray(0); // Unbind VAO
  152.  
  153.  
  154. // Load and create a texture
  155. GLuint texture1;
  156. GLuint texture2;
  157.  
  158.  
  159. // TEXTURE 1
  160.  
  161. glGenTextures(1, &texture1);
  162. glBindTexture(GL_TEXTURE_2D, texture1); // All upcoming GL_TEXTURE_2D operations now have effect on our texture object
  163. // Set our texture parameters
  164. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT
  165. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  166. // Set texture filtering
  167. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  168. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  169. // Load, create texture and generate mipmaps
  170. int width, height;
  171. unsigned char* image = SOIL_load_image("Door.png", &width, &height, 0, SOIL_LOAD_RGB);
  172. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  173. glGenerateMipmap(GL_TEXTURE_2D);
  174. SOIL_free_image_data(image);
  175. glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture when done, so we won't accidentily mess up our texture.
  176.  
  177.  
  178. // TEXTURE 2
  179.  
  180. glGenTextures(1, &texture2);
  181. glBindTexture(GL_TEXTURE_2D, texture2);
  182. // Set our texture parameters
  183. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  184. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  185. // Set texture filtering
  186. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  187. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  188. // Load, create texture and generate mipmaps
  189. image = SOIL_load_image("Wallpng.png", &width, &height, 0, SOIL_LOAD_RGB);
  190. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  191. glGenerateMipmap(GL_TEXTURE_2D);
  192. SOIL_free_image_data(image);
  193. glBindTexture(GL_TEXTURE_2D, 0);
  194.  
  195. // GAME LOOP STARTS HERE
  196. while (!glfwWindowShouldClose(window))
  197. {
  198.  
  199. glfwPollEvents();
  200.  
  201. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  202. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  203.  
  204. ourShader.Use();
  205.  
  206. // Bind Textures using texture units
  207. glActiveTexture(GL_TEXTURE0);
  208. glBindTexture(GL_TEXTURE_2D, texture1);
  209. glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0);
  210. glActiveTexture(GL_TEXTURE1);
  211. glBindTexture(GL_TEXTURE_2D, texture2);
  212. glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture2"), 1);
  213.  
  214. // Draw container
  215. glBindVertexArray(VAO);
  216. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  217. glBindVertexArray(0);
  218.  
  219. glfwSwapBuffers(window);
  220. }
  221. // GAME LOOP ENDS HERE
  222.  
  223. // Properly de-allocate all resources once they've outlived their purpose
  224. glDeleteVertexArrays(1, &VAO);
  225. glDeleteBuffers(1, &VBO);
  226. glDeleteBuffers(1, &EBO);
  227. // Terminate GLFW, clearing any resources allocated by GLFW.
  228. glfwTerminate();
  229. return 0;
  230. }
  231.  
  232. // Function for keyboard input
  233.  
  234. void key_callback(GLFWwindow * window, int key, int scancode, int action, int mode)
  235. {
  236. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  237. glfwSetWindowShouldClose(window, GL_TRUE);
  238. if (key == GLFW_KEY_UP && action == GLFW_PRESS) {
  239. if (Matrix[y - 1][x] == 1) {
  240. cout << "You hit a wall!";
  241. }
  242. else {
  243. Matrix[y][x] = 0;
  244. Matrix[y - 1][x] = 5;
  245. y = y - 1;
  246. for (a = 0; a < h; a++) {
  247. cout << endl;
  248. for (b = 0; b < w; b++) {
  249. cout << Matrix[a][b] << ' ';
  250. }
  251. }
  252. }
  253. }
  254. if (key == GLFW_KEY_DOWN && action == GLFW_PRESS) {
  255.  
  256. if (Matrix[y + 1][x] == 1) {
  257. cout << "You hit a wall!";
  258. }
  259. else {
  260. Matrix[y][x] = 0;
  261. Matrix[y + 1][x] = 5;
  262. y = y + 1;
  263. for (a = 0; a < h; a++) {
  264. cout << endl;
  265. for (b = 0; b < w; b++) {
  266. cout << Matrix[a][b] << ' ';
  267. }
  268. }
  269. }
  270. }
  271. if (key == GLFW_KEY_LEFT && action == GLFW_PRESS) {
  272. if (Matrix[y][x - 1] == 1) {
  273. cout << "You hit a wall!";
  274. }
  275. else {
  276. Matrix[y][x] = 0;
  277. Matrix[y][x - 1] = 5;
  278. x = x - 1;
  279. for (a = 0; a < h; a++) {
  280. cout << endl;
  281. for (b = 0; b < w; b++) {
  282. cout << Matrix[a][b] << ' ';
  283. }
  284. }
  285. }
  286. }
  287. if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) {
  288. if (Matrix[y][x + 1] == 1) {
  289. cout << "You hit a wall!";
  290. }
  291. else {
  292. Matrix[y][x] = 0;
  293. Matrix[y][x + 1] = 5;
  294. x = x + 1;
  295. for (a = 0; a < h; a++) {
  296. cout << endl;
  297. for (b = 0; b < w; b++) {
  298. cout << Matrix[a][b] << ' ';
  299. }
  300. }
  301. }
  302. }
  303. }
  304.  
  305. // Function for mouse input
  306. void mouse_callback(GLFWwindow * window, double xpos, double ypos)
  307. {
  308.  
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement