Advertisement
Guest User

Untitled

a guest
Sep 9th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/time.h>
  3.  
  4. #include <jni.h>
  5. #include <android/log.h>
  6. #include <GLES2/gl2.h>
  7. #include <GLES2/gl2ext.h>
  8. #include <GLES/gl.h>
  9.  
  10. #include "main.h"
  11. #include "Pacman.h"
  12.  
  13. float enemyRect[18] = {
  14.     0, 1, 0,
  15.     0, 0, 0,
  16.     1, 0, 0,
  17.     0, 1, 0,
  18.     1, 0, 0,
  19.     1, 1, 0
  20. };
  21.  
  22. int pacmanDirection = DIR_Y;
  23. int pacmanMoveValue = VALUE_POSITIVE;
  24.  
  25. Pacman pacman;
  26.  
  27. static void printGLString(const char *name, GLenum s) {
  28.     const char *v = (const char *) glGetString(s);
  29.     LOGI("GL %s = %s\n", name, v);
  30. }
  31.  
  32. static void checkGlError(const char* op) {
  33.     for (GLint error = glGetError(); error; error = glGetError()) {
  34.         LOGE("after %s() glError (0x%x)\n", op, error);
  35.     }
  36. }
  37.  
  38. /**
  39.  * Initialize the GL subsystem
  40.  */
  41. void Java_com_wiagames_pacman_PacmanRenderer_nativeInit(JNIEnv* env) {
  42.       printGLString("Version", GL_VERSION);
  43.       printGLString("Vendor", GL_VENDOR);
  44.       printGLString("Renderer", GL_RENDERER);
  45.       printGLString("Extensions", GL_EXTENSIONS);
  46.  
  47.       glEnableClientState(GL_VERTEX_ARRAY);
  48.       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  49.       glEnable(GL_BLEND);
  50.       glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  51.       glEnable(GL_TEXTURE_2D);
  52.       glClearColor(0,0,0,0);
  53.       glColor4f(1,1,1,1);
  54.       glDisable(GL_DEPTH_TEST);
  55.       glDisable(GL_CULL_FACE);
  56.  
  57.       LOGE("Native start");
  58. }
  59.  
  60. /**
  61.  * Resize window
  62.  */
  63. void Java_com_wiagames_pacman_PacmanRenderer_nativeResize(JNIEnv* env,
  64.         jobject thiz, jint w, jint h) {
  65.       LOGI("nativeResize (%i,%i)", w, h);
  66.       if (h == 0) {
  67.         h = 1;
  68.       }
  69.  
  70.       glViewport(0, 0, w, h);
  71.       checkGlError("glViewport");
  72.       glMatrixMode(GL_PROJECTION);
  73.       const float ratio=w/(float)h;
  74.       glLoadIdentity();
  75.       glOrthof(0, 15, 15/ratio, 0, -1, 1);
  76.       checkGlError("glViewport");
  77.       glMatrixMode(GL_MODELVIEW);
  78. }
  79.  
  80. /**
  81.  * Finalize the graphics state
  82.  */
  83. void Java_com_wiagames_pacman_PacmanRenderer_nativeDone(JNIEnv* env) {
  84.     // TODO
  85. }
  86.  
  87. /**
  88.  * Pause event from Android activity lifecycle loop
  89.  */
  90. void Java_com_wiagames_PacmanGLSurfaceView_nativePause(JNIEnv* env) {
  91.     LOGE("Pause");
  92. }
  93.  
  94. /**
  95.  * Move the pacman & the enemy
  96.  */
  97. //void moveObjects() {
  98. //    struct timeval tv;
  99. //    struct timezone tz;
  100. //
  101. //    gettimeofday(&tv, &tz);
  102. //
  103. //    if (tvSet) {
  104. //        if (tv.tv_usec - lastTv.tv_usec < 1000) {
  105. //            return;
  106. //        }
  107. //    } else {
  108. //        tvSet = TRUE;
  109. //    }
  110. //
  111. //    lastTv = tv;
  112. //
  113. //    movePlayer(pacmanRect, pacmanDirection, pacmanMoveValue);
  114. //}
  115.  
  116. /**
  117.  * Draw the given object
  118.  */
  119. void drawObject(const GameObject &object) {
  120.     glVertexPointer(3, GL_FLOAT, 0, object.vertexArray);
  121.     glDrawArrays(GL_TRIANGLES, 0, object.vertexCount);
  122. }
  123.  
  124. /**
  125.  * Draw the objects in the scene
  126.  */
  127. void drawObjects() {
  128.     drawObject(pacman);
  129. }
  130.  
  131. /**
  132.  * Render the next frame
  133.  */
  134. void Java_com_wiagames_pacman_PacmanRenderer_nativeRender(JNIEnv* env) {
  135.       glClear(GL_COLOR_BUFFER_BIT);
  136.       glLoadIdentity();
  137.  
  138.       glPushMatrix();
  139.       drawObjects();
  140.       glPopMatrix();
  141. }
  142.  
  143. /**
  144.  * Move down event
  145.  */
  146. void Java_com_wiagames_pacman_PacmanGLSurfaceView_nativeDown(JNIEnv* env) {
  147.       LOGE("DOWN");
  148.  
  149.       pacmanDirection = DIR_Y;
  150.       pacmanMoveValue = VALUE_POSITIVE;
  151. }
  152.  
  153. /**
  154.  * Move up event
  155.  */
  156. void Java_com_wiagames_pacman_PacmanGLSurfaceView_nativeUp(JNIEnv* env) {
  157.       LOGE("UP");
  158.  
  159.       pacmanDirection = DIR_Y;
  160.       pacmanMoveValue = VALUE_NEGATIVE;
  161. }
  162.  
  163. /**
  164.  * Move down event
  165.  */
  166. void Java_com_wiagames_pacman_PacmanGLSurfaceView_nativeLeft(JNIEnv* env) {
  167.       LOGE("LEFT");
  168.  
  169.       pacmanDirection = DIR_X;
  170.       pacmanMoveValue = VALUE_NEGATIVE;
  171. }
  172.  
  173. /**
  174.  * Move down event
  175.  */
  176. void Java_com_wiagames_pacman_PacmanGLSurfaceView_nativeRight(JNIEnv* env) {
  177.       LOGE("RIGHT");
  178.  
  179.       pacmanDirection = DIR_X;
  180.       pacmanMoveValue = VALUE_POSITIVE;
  181. }
  182.  
  183. // Move the player
  184. void movePlayer(float *player, int direction, int value) {
  185.     for (int i = 0; i < 18; i++) {
  186.         if (direction == DIR_Y) {
  187.             if (i != 1 && i != 4 && i != 7 && i != 10 && i != 13 && i != 16) {
  188.                 LOGE("Skipping x or z coordinate");
  189.                 continue;
  190.             }
  191.         } else if (direction == DIR_X) {
  192.             if (i != 0 && i != 3 && i != 6 && i != 9 && i != 12 && i != 15) {
  193.                 LOGE("Skipping y or z coordinate");
  194.                 continue;
  195.             }
  196.         }
  197.  
  198.         player[i] += value;
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement