Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

252 righe
7.9 KiB

  1. // Test stuff
  2. #ifdef WIN32
  3. # define WIN32_LEAN_AND_MEAN
  4. # include <windows.h>
  5. #endif
  6. #if defined __APPLE__ && defined __MACH__
  7. # include <OpenGL/gl.h>
  8. #else
  9. # define GL_GLEXT_PROTOTYPES
  10. # include <GL/gl.h>
  11. # include <GL/glext.h>
  12. #endif
  13. #include <SDL.h>
  14. #include <SDL_image.h>
  15. #include <math.h>
  16. int frames;
  17. /* Storage for one texture */
  18. GLuint texture[1];
  19. /* Storage for 3 vertex buffers */
  20. GLuint buflist[3];
  21. // Load Bitmaps And Convert To Textures
  22. void LoadGLTextures(void)
  23. {
  24. SDL_Surface *image1 = IMG_Load("art/test/groundtest.png");
  25. if (!image1)
  26. {
  27. SDL_Quit();
  28. exit(1);
  29. }
  30. glGenTextures(1, &texture[0]);
  31. glBindTexture(GL_TEXTURE_2D, texture[0]);
  32. glTexImage2D(GL_TEXTURE_2D, 0, 4, image1->w, image1->h, 0,
  33. GL_RGBA, GL_UNSIGNED_BYTE, image1->pixels);
  34. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  35. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  36. };
  37. void MakeVBOs(void)
  38. {
  39. glGenBuffers(3, buflist);
  40. float vertices[8 * 20 * 15];
  41. for (int y = 0; y < 15; y++)
  42. for (int x = 0; x < 20; x++)
  43. {
  44. vertices[8 * (20 * y + x) + 0] = x * 32;
  45. vertices[8 * (20 * y + x) + 1] = y * 32;
  46. vertices[8 * (20 * y + x) + 2] = x * 32 + 32;
  47. vertices[8 * (20 * y + x) + 3] = y * 32;
  48. vertices[8 * (20 * y + x) + 4] = x * 32 + 32;
  49. vertices[8 * (20 * y + x) + 5] = y * 32 + 32;
  50. vertices[8 * (20 * y + x) + 6] = x * 32;
  51. vertices[8 * (20 * y + x) + 7] = y * 32 + 32;
  52. }
  53. glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
  54. glBufferData(GL_ARRAY_BUFFER,
  55. 8 * 20 * 15 * sizeof(float), vertices, GL_STATIC_DRAW);
  56. int indices[4 * 20 * 15];
  57. for (int n = 0; n < 4 * 20 * 15; n++)
  58. indices[n] = n;
  59. glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
  60. glBufferData(GL_ARRAY_BUFFER,
  61. 4 * 20 * 15 * sizeof(int), indices, GL_STATIC_DRAW);
  62. }
  63. void InitGL(int Width, int Height)
  64. {
  65. // Resize method
  66. glViewport(0, 0, Width, Height);
  67. glMatrixMode(GL_PROJECTION);
  68. glLoadIdentity();
  69. glOrtho(0, Width, Height, 0, -1, 10);
  70. glMatrixMode(GL_MODELVIEW);
  71. glLoadIdentity();
  72. // Init method
  73. glEnable(GL_TEXTURE_2D);
  74. LoadGLTextures();
  75. MakeVBOs();
  76. glShadeModel(GL_SMOOTH);
  77. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  78. glClearDepth(1.0);
  79. glEnable(GL_DEPTH_TEST);
  80. glDepthFunc(GL_LEQUAL);
  81. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  82. glEnable(GL_BLEND);
  83. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  84. }
  85. void PutMap(int const *themap)
  86. {
  87. // Put map
  88. float uvs[8 * 20 * 15];
  89. for (int y = 0; y < 15; y++)
  90. for (int x = 0; x < 20; x++)
  91. {
  92. int tile = themap[20 * y + x];
  93. float ty = .0625f * (tile / 16);
  94. float tx = .0625f * (tile % 16);
  95. uvs[8 * (20 * y + x) + 0] = tx;
  96. uvs[8 * (20 * y + x) + 1] = ty;
  97. uvs[8 * (20 * y + x) + 2] = tx + .0625f;
  98. uvs[8 * (20 * y + x) + 3] = ty;
  99. uvs[8 * (20 * y + x) + 4] = tx + .0625f;
  100. uvs[8 * (20 * y + x) + 5] = ty + .0625f;
  101. uvs[8 * (20 * y + x) + 6] = tx;
  102. uvs[8 * (20 * y + x) + 7] = ty + .0625f;
  103. }
  104. glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
  105. glBufferData(GL_ARRAY_BUFFER,
  106. 8 * 20 * 15 * sizeof(float), uvs, GL_STATIC_DRAW);
  107. glEnableClientState(GL_VERTEX_ARRAY);
  108. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  109. glEnableClientState(GL_INDEX_ARRAY);
  110. glBindTexture(GL_TEXTURE_2D, texture[0]);
  111. glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
  112. glVertexPointer(2, GL_FLOAT, 0, NULL);
  113. glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
  114. glTexCoordPointer(2, GL_FLOAT, 0, NULL);
  115. glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
  116. glIndexPointer(GL_INT, 0, NULL);
  117. glDrawArrays(GL_QUADS, 0, 4 * 20 * 15);
  118. glDisableClientState(GL_VERTEX_ARRAY);
  119. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  120. glDisableClientState(GL_INDEX_ARRAY);
  121. }
  122. /* The main drawing function. */
  123. void DrawScene()
  124. {
  125. int ground[20 * 15] =
  126. {
  127. 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
  128. 18, 1, 2, 2, 2, 34, 2, 2, 2, 2, 2, 2, 3, 34, 4, 18, 18, 18, 18, 18,
  129. 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 20, 4, 18, 18, 18, 18,
  130. 18, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 19, 18, 18, 18, 18,
  131. 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 18, 18, 18, 18,
  132. 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 18, 20, 36, 18, 18, 18, 18,
  133. 18, 33, 2, 2, 2, 2, 2, 2, 2, 2, 34, 2, 35, 2, 36, 18, 18, 18, 18, 18,
  134. 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
  135. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  136. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  137. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  138. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  139. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  140. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  141. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  142. };
  143. int l1objects[20 * 15] =
  144. {
  145. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  146. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  147. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  148. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  149. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0,
  150. 0, 0, 0, 0, 49, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  151. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  152. 0, 0, 0, 0, 49, 49, 49, 49, 49, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  153. 0, 0, 0, 0, 0, 49, 49, 49, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  154. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 32, 49, 0, 0, 0,
  155. 0, 0, 0, 49, 49, 32, 0, 50, 0, 0, 0, 48, 0, 64, 0, 49, 49, 0, 0, 0,
  156. 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0,
  157. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0,
  158. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  159. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  160. };
  161. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  162. glLoadIdentity();
  163. PutMap(ground);
  164. //glTranslatef(10.0f * sinf(0.16f * frames), 10.0f * cosf(0.16f * frames), 0.0f);
  165. PutMap(l1objects);
  166. SDL_GL_SwapBuffers();
  167. }
  168. int main(int argc, char **argv)
  169. {
  170. int done;
  171. /* Initialize SDL for video output */
  172. if (SDL_Init(SDL_INIT_VIDEO) < 0)
  173. {
  174. fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
  175. exit(1);
  176. }
  177. /* Create a 640x480 OpenGL screen */
  178. if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL)
  179. {
  180. fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
  181. SDL_Quit();
  182. exit(2);
  183. }
  184. /* Set the title bar in environments that support it */
  185. SDL_WM_SetCaption("Deus Hax", NULL);
  186. /* Loop, drawing and checking events */
  187. InitGL(640, 480);
  188. done = 0;
  189. frames = 0;
  190. Uint32 ticks = SDL_GetTicks();
  191. Uint32 start = ticks;
  192. while (!done)
  193. {
  194. DrawScene();
  195. frames++;
  196. /* This could go in a separate function */
  197. SDL_Event event;
  198. while (SDL_PollEvent(&event))
  199. {
  200. if (event.type == SDL_QUIT)
  201. done = 1;
  202. if (event.type == SDL_KEYDOWN)
  203. if (event.key.keysym.sym == SDLK_ESCAPE)
  204. done = 1;
  205. }
  206. while (SDL_GetTicks() < ticks + 33)
  207. SDL_Delay(1);
  208. ticks = SDL_GetTicks();
  209. }
  210. printf("%i fps\n", frames * 1000 / (SDL_GetTicks() - start));
  211. SDL_Quit();
  212. return EXIT_SUCCESS;
  213. }