You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

258 regels
8.0 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. }
  41. void InitGL(int Width, int Height)
  42. {
  43. // Resize method
  44. glViewport(0, 0, Width, Height);
  45. glMatrixMode(GL_PROJECTION);
  46. glLoadIdentity();
  47. glOrtho(0, Width, Height, 0, -1, 10);
  48. glMatrixMode(GL_MODELVIEW);
  49. glLoadIdentity();
  50. // Init method
  51. glEnable(GL_TEXTURE_2D);
  52. LoadGLTextures();
  53. MakeVBOs();
  54. glShadeModel(GL_SMOOTH);
  55. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  56. glClearDepth(1.0);
  57. glEnable(GL_DEPTH_TEST);
  58. glDepthFunc(GL_LEQUAL);
  59. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  60. glEnable(GL_BLEND);
  61. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  62. }
  63. void PutMap(int const *themap)
  64. {
  65. // Put map
  66. float uvs[8 * 20 * 15];
  67. for (int y = 0; y < 15; y++)
  68. for (int x = 0; x < 20; x++)
  69. {
  70. int tile = themap[20 * y + x];
  71. float ty = .0625f * (tile / 16);
  72. float tx = .0625f * (tile % 16);
  73. uvs[8 * (20 * y + x) + 0] = tx;
  74. uvs[8 * (20 * y + x) + 1] = ty;
  75. uvs[8 * (20 * y + x) + 2] = tx + .0625f;
  76. uvs[8 * (20 * y + x) + 3] = ty;
  77. uvs[8 * (20 * y + x) + 4] = tx + .0625f;
  78. uvs[8 * (20 * y + x) + 5] = ty + .0625f;
  79. uvs[8 * (20 * y + x) + 6] = tx;
  80. uvs[8 * (20 * y + x) + 7] = ty + .0625f;
  81. }
  82. glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
  83. glBufferData(GL_ARRAY_BUFFER,
  84. 8 * 20 * 15 * sizeof(float), uvs, GL_STATIC_DRAW);
  85. float vertices[8 * 20 * 15];
  86. for (int y = 0; y < 15; y++)
  87. for (int x = 0; x < 20; x++)
  88. {
  89. vertices[8 * (20 * y + x) + 0] = x * 32;
  90. vertices[8 * (20 * y + x) + 1] = y * 32;
  91. vertices[8 * (20 * y + x) + 2] = x * 32 + 32;
  92. vertices[8 * (20 * y + x) + 3] = y * 32;
  93. vertices[8 * (20 * y + x) + 4] = x * 32 + 32;
  94. vertices[8 * (20 * y + x) + 5] = y * 32 + 32;
  95. vertices[8 * (20 * y + x) + 6] = x * 32;
  96. vertices[8 * (20 * y + x) + 7] = y * 32 + 32;
  97. }
  98. glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
  99. glBufferData(GL_ARRAY_BUFFER,
  100. 8 * 20 * 15 * sizeof(float), vertices, GL_STATIC_DRAW);
  101. int indices[4 * 20 * 15];
  102. for (int n = 0; n < 4 * 20 * 15; n++)
  103. indices[n] = n;
  104. glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
  105. glBufferData(GL_ARRAY_BUFFER,
  106. 4 * 20 * 15 * sizeof(int), indices, 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. SDL_Surface *video;
  171. int done;
  172. /* Initialize SDL for video output */
  173. if (SDL_Init(SDL_INIT_VIDEO) < 0)
  174. {
  175. fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
  176. exit(1);
  177. }
  178. /* Create a 640x480 OpenGL screen */
  179. video = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
  180. if (!video)
  181. {
  182. fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
  183. SDL_Quit();
  184. exit(2);
  185. }
  186. /* Set the title bar in environments that support it */
  187. SDL_WM_SetCaption("Deus Hax", NULL);
  188. /* Loop, drawing and checking events */
  189. InitGL(640, 480);
  190. done = 0;
  191. frames = 0;
  192. Uint32 ticks = SDL_GetTicks();
  193. Uint32 start = ticks;
  194. while (!done)
  195. {
  196. DrawScene();
  197. frames++;
  198. /* This could go in a separate function */
  199. SDL_Event event;
  200. while (SDL_PollEvent(&event))
  201. {
  202. if (event.type == SDL_QUIT)
  203. done = 1;
  204. if (event.type == SDL_KEYDOWN)
  205. {
  206. if (event.key.keysym.sym == SDLK_RETURN)
  207. SDL_WM_ToggleFullScreen(video);
  208. else if (event.key.keysym.sym == SDLK_ESCAPE)
  209. done = 1;
  210. }
  211. }
  212. while (SDL_GetTicks() < ticks + 33)
  213. SDL_Delay(1);
  214. ticks = SDL_GetTicks();
  215. }
  216. printf("%i fps\n", frames * 1000 / (SDL_GetTicks() - start));
  217. SDL_Quit();
  218. return EXIT_SUCCESS;
  219. }