Platformer in OpenGL
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

186 wiersze
4.9 KiB

5 lat temu
  1. //========================================================================
  2. // Context sharing test program
  3. // Copyright (c) Camilla Berglund <elmindreda@glfw.org>
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would
  16. // be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not
  19. // be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. //
  24. //========================================================================
  25. //
  26. // This program is used to test sharing of objects between contexts
  27. //
  28. //========================================================================
  29. #include <glad/glad.h>
  30. #include <GLFW/glfw3.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #define WIDTH 400
  34. #define HEIGHT 400
  35. #define OFFSET 50
  36. static GLFWwindow* windows[2];
  37. static void error_callback(int error, const char* description)
  38. {
  39. fprintf(stderr, "Error: %s\n", description);
  40. }
  41. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  42. {
  43. if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
  44. glfwSetWindowShouldClose(window, GLFW_TRUE);
  45. }
  46. static GLFWwindow* open_window(const char* title, GLFWwindow* share, int posX, int posY)
  47. {
  48. GLFWwindow* window;
  49. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  50. window = glfwCreateWindow(WIDTH, HEIGHT, title, NULL, share);
  51. if (!window)
  52. return NULL;
  53. glfwMakeContextCurrent(window);
  54. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  55. glfwSwapInterval(1);
  56. glfwSetWindowPos(window, posX, posY);
  57. glfwShowWindow(window);
  58. glfwSetKeyCallback(window, key_callback);
  59. return window;
  60. }
  61. static GLuint create_texture(void)
  62. {
  63. int x, y;
  64. char pixels[256 * 256];
  65. GLuint texture;
  66. glGenTextures(1, &texture);
  67. glBindTexture(GL_TEXTURE_2D, texture);
  68. for (y = 0; y < 256; y++)
  69. {
  70. for (x = 0; x < 256; x++)
  71. pixels[y * 256 + x] = rand() % 256;
  72. }
  73. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 256, 256, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
  74. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  75. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  76. return texture;
  77. }
  78. static void draw_quad(GLuint texture)
  79. {
  80. int width, height;
  81. glfwGetFramebufferSize(glfwGetCurrentContext(), &width, &height);
  82. glViewport(0, 0, width, height);
  83. glMatrixMode(GL_PROJECTION);
  84. glLoadIdentity();
  85. glOrtho(0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
  86. glEnable(GL_TEXTURE_2D);
  87. glBindTexture(GL_TEXTURE_2D, texture);
  88. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  89. glBegin(GL_QUADS);
  90. glTexCoord2f(0.f, 0.f);
  91. glVertex2f(0.f, 0.f);
  92. glTexCoord2f(1.f, 0.f);
  93. glVertex2f(1.f, 0.f);
  94. glTexCoord2f(1.f, 1.f);
  95. glVertex2f(1.f, 1.f);
  96. glTexCoord2f(0.f, 1.f);
  97. glVertex2f(0.f, 1.f);
  98. glEnd();
  99. }
  100. int main(int argc, char** argv)
  101. {
  102. int x, y, width;
  103. GLuint texture;
  104. glfwSetErrorCallback(error_callback);
  105. if (!glfwInit())
  106. exit(EXIT_FAILURE);
  107. windows[0] = open_window("First", NULL, OFFSET, OFFSET);
  108. if (!windows[0])
  109. {
  110. glfwTerminate();
  111. exit(EXIT_FAILURE);
  112. }
  113. // This is the one and only time we create a texture
  114. // It is created inside the first context, created above
  115. // It will then be shared with the second context, created below
  116. texture = create_texture();
  117. glfwGetWindowPos(windows[0], &x, &y);
  118. glfwGetWindowSize(windows[0], &width, NULL);
  119. // Put the second window to the right of the first one
  120. windows[1] = open_window("Second", windows[0], x + width + OFFSET, y);
  121. if (!windows[1])
  122. {
  123. glfwTerminate();
  124. exit(EXIT_FAILURE);
  125. }
  126. // Set drawing color for both contexts
  127. glfwMakeContextCurrent(windows[0]);
  128. glColor3f(0.6f, 0.f, 0.6f);
  129. glfwMakeContextCurrent(windows[1]);
  130. glColor3f(0.6f, 0.6f, 0.f);
  131. glfwMakeContextCurrent(windows[0]);
  132. while (!glfwWindowShouldClose(windows[0]) &&
  133. !glfwWindowShouldClose(windows[1]))
  134. {
  135. glfwMakeContextCurrent(windows[0]);
  136. draw_quad(texture);
  137. glfwMakeContextCurrent(windows[1]);
  138. draw_quad(texture);
  139. glfwSwapBuffers(windows[0]);
  140. glfwSwapBuffers(windows[1]);
  141. glfwWaitEvents();
  142. }
  143. glfwTerminate();
  144. exit(EXIT_SUCCESS);
  145. }