Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Tags: CSS,#include,#define,.apache,.com,.n177-kode,.n177-code-start, Created At: 10/24/2016 8:22:43 PMViews:

HTML view:
Copy Source | Copy HTML
  1. /*
     * Copyright (C) 2010 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     *
     */
  2. //unit.n117@gmail.com  code
  3. #include <jni.h>
  4. #include <errno.h>
  5. #include <EGL/egl.h>
  6. #include <GLES/gl.h>
  7. #include <android/sensor.h>
  8. #include <android/log.h>
  9. #include <android_native_app_glue.h>
  10. #include <time.h>
  11. #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
  12. #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))
  13. /**nativecode
        /**
     * Our saved state data.
     */
  14. struct saved_state {
  15.     float angle;
  16.     int32_t x;
  17.     int32_t y;
  18. };
  19. /**
     * Shared state for our app.
     */
  20. struct engine {
  21.     struct android_app* app;
  22.     ASensorManager* sensorManager;
  23.     const ASensor* accelerometerSensor;
  24.     ASensorEventQueue* sensorEventQueue;
  25.     int animating;
  26.     EGLDisplay display;
  27.     EGLSurface surface;
  28.     EGLContext context;
  29.     int32_t width;
  30.     int32_t height;
  31.     struct saved_state state;
  32.     //unit.n177-kode
  33.     struct MxSpriteData *mxBall;
  34.     struct MxSpriteData *mxPlatform;
  35. };
  36. //unit.n177-code-start
  37. /////////////////////////////////////////////////
  38. float deltaTime =  0;
  39. /**
     * Base sprite variables.
     */
  40. struct MxSpriteData {
  41.     float * coords;
  42.     float * colors;
  43.     int coordsCount;
  44.     int colorsCount;
  45.     float px;
  46.     float py;
  47.     float dx;
  48.     float dy;
  49.     int heightSprite;
  50.     int widhtSprite;
  51. };
  52. enum MxGameState {
  53.     START, GAME, RESTART, EXIT
  54. };
  55. //////////////////////////////////////////////////////////////////////////////////////////////////////
  56. void MxInitialize(struct MxSpriteData * mxBall, struct MxSpriteData * mxPlatform);
  57. void MxInitializeSpriteData(struct MxSpriteData * data, int coordsCount,
            int  colorsCount, float px,    float py, float dx,    float dy,
            int widhtSprite, int heightSprite);
  58. void MxCalculateVectoorCoords(struct MxSpriteData* data);
  59. void MxCreateVectorColors(struct MxSpriteData* data);
  60. void MxMoveVectoorCoords(struct MxSpriteData* data);
  61. //
  62. void MxUpdate(struct engine * engine);
  63. void MxMove(struct MxSpriteData * data);
  64. void MxCollisionScreen(struct engine * engine,struct MxSpriteData * data);
  65. //void MxCollisionScreen(struct MxSpriteData * data)
  66. void MxCollisionPlatform(struct MxSpriteData * ball, struct MxSpriteData * platform);
  67. //
  68. void MxDraw(struct MxSpriteData * mxBall,struct  MxSpriteData * mxPlatform);
  69. void MxDrawBall(struct MxSpriteData * mxBall);
  70. void MxDrawPlatform(struct MxSpriteData * mxPlatform);
  71. //Initialize Block
  72. //////////////////////////////////////////////////////////////////////////////////////////////////////
  73. void MxInitialize(struct MxSpriteData * mxBall, struct MxSpriteData * mxPlatform)
  74. {
  75.     memset(mxBall, 0, sizeof(struct MxSpriteData));
  76.     memset(mxPlatform, 0, sizeof(struct MxSpriteData));
  77.     MxInitializeSpriteData(mxBall,    12,16,100,100,2,2,20,20);
  78.     MxInitializeSpriteData(mxPlatform,12,16,200,200,2,2,80,15);
  79. };
  80. void MxInitializeSpriteData(struct MxSpriteData * data, int coordsCount,
            int  colorsCount, float px,    float py, float dx,    float dy,
            int widhtSprite, int heightSprite)
  81. {
  82.     data->coordsCount = coordsCount;
  83.     data->colorsCount = colorsCount;
  84.     data->px = px;
  85.     data->py = py;
  86.     data->dx = dx;
  87.     data->dy = dy;
  88.     data->widhtSprite = widhtSprite;
  89.     data->heightSprite = heightSprite;
  90.     data->coords = (float*)malloc(sizeof(*data->coords)*coordsCount);
  91.     data->colors = (float*)malloc(sizeof(*data->colors)*colorsCount);
  92.     data->dx =2;
  93.     //cout<<"data.dx = "<<data->dx<<endl;
  94.     MxCalculateVectoorCoords(data);
  95.     MxCreateVectorColors(data);
  96. };
  97. void MxCalculateVectoorCoords(struct MxSpriteData* data)
  98. {
  99.     //1 LD
  100.     data->coords[ 0] = data->px;
  101.     data->coords[1] = data->py + data->heightSprite;
  102.     data->coords[2] =  0;
  103.     //2 LU
  104.     data->coords[3] = data->px;
  105.     data->coords[4] = data->py;
  106.     data->coords[5] =  0;
  107.     //3 RU
  108.     data->coords[6] = data->px + data->widhtSprite;
  109.     data->coords[7] = data->py;
  110.     data->coords[8] =  0;
  111.     //4 RD
  112.     data->coords[9] = data->px + data->widhtSprite;
  113.     data->coords[10] = data->py + data->heightSprite;
  114.     data->coords[11] =  0;
  115. };
  116. void MxCreateVectorColors(struct MxSpriteData* data)
  117. {
  118.     float r[16] = {
  119.              0.0f, 1.0f,  0.0f,  0.0f, // A
  120.              0.0f, 1.0f,  0.0f,  0.0f, // B
  121.              0.0f, 1.0f,  0.0f,  0.0f, // C
  122.              0.0f, 1.0f,  0.0f,  0.0f // D
  123.         };
  124.     int i= 0;
  125.     for(;i<16;i++)
  126.         data->colors[i] = r[i];
  127. };
  128. void MxMoveVectoorCoords(struct MxSpriteData* data)
  129. {
  130.     //1 LD
  131.     data->coords[ 0] = data->px;
  132.     data->coords[1] = data->py + data->heightSprite;
  133.     //2 LU
  134.     data->coords[3] = data->px;
  135.     data->coords[4] = data->py;
  136.     //3 RU
  137.     data->coords[6] = data->px + data->widhtSprite;
  138.     data->coords[7] = data->py;
  139.     //4 RD
  140.     data->coords[9] = data->px + data->widhtSprite;
  141.     data->coords[10] = data->py + data->heightSprite;
  142. };
  143. //Update Block
  144. //////////////////////////////////////////////////////////////////////////////////////////////////////
  145. void MxUpdate(struct engine * engine)
  146. {
  147.     MxMove(engine->mxBall);
  148.     MxCollisionPlatform(engine->mxBall,engine->mxPlatform);
  149.     MxCollisionScreen(engine, engine->mxBall);
  150. };
  151. void MxMove(struct MxSpriteData * data)
  152. {
  153.     data->px = data->px + data->dx;
  154.     data->py = data->py + data->dy;
  155.     MxMoveVectoorCoords(data);
  156. };
  157. /**
        Collision
    */
  158. void MxCollisionScreen(struct engine * engine,struct MxSpriteData * data)
  159. {
  160.     if(data->px  < 0 || data->px +  data->widhtSprite > engine->width)
  161.         data->dx = data->dx *(-1);
  162.     if(data->py < 0 || data->py+ data->heightSprite > engine->height)
  163.         data->dy = data->dy *(-1);
  164. };
  165. void MxCollisionPlatform(struct MxSpriteData * ball, struct MxSpriteData * platform)
  166. {
  167.     if(ball->px  > platform->px &&  ball->px +  ball->widhtSprite < platform->px+platform->widhtSprite)
  168.         if(ball->py + ball->heightSprite> platform->py && ball->py + ball->heightSprite < platform->py + 3 )
  169.                 ball->dy = ball->dy *(-1);
  170. };
  171. //Draw Block
  172. //////////////////////////////////////////////////////////////////////////////////////////////////////
  173. void MxDraw(struct MxSpriteData * mxBall, struct MxSpriteData * mxPlatform)
  174. {
  175.     glClearColor(0,0,0,0);
  176.     glClear(GL_COLOR_BUFFER_BIT);
  177.     glEnableClientState(GL_VERTEX_ARRAY);
  178.     glEnableClientState(GL_COLOR_ARRAY);
  179.         MxDrawBall(mxBall);
  180.         MxDrawPlatform(mxPlatform);
  181.     glDisableClientState(GL_COLOR_ARRAY);
  182.     glDisableClientState(GL_VERTEX_ARRAY);
  183. };
  184. void MxDrawBall(struct MxSpriteData * mxBall)
  185. {
  186.     glVertexPointer(3, GL_FLOAT, 0, mxBall->coords);//vertices);
  187.     glColorPointer(4, GL_FLOAT, 0, mxBall->colors);
  188.     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  189. };
  190. void MxDrawPlatform(struct MxSpriteData * mxPlatform)
  191. {
  192.     glVertexPointer(3, GL_FLOAT, 0, mxPlatform->coords);
  193.     glColorPointer(4, GL_FLOAT, 0, mxPlatform->colors);
  194.     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  195. };
  196. //////////////////////////////////////////////////
  197. //unit.n177-code-end
  198. /**
     * Initialize an EGL context for the current display.
     */
  199. static int engine_init_display(struct engine* engine) {
  200.     // initialize OpenGL ES and EGL
  201.     /*
         * Here specify the attributes of the desired configuration.
         * Below, we select an EGLConfig with at least 8 bits per color
         * component compatible with on-screen windows
         */
  202.     const EGLint attribs[] = {
  203.             EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  204.             EGL_BLUE_SIZE, 8,
  205.             EGL_GREEN_SIZE, 8,
  206.             EGL_RED_SIZE, 8,
  207.             EGL_NONE
  208.     };
  209.     EGLint w, h, dummy, format;
  210.     EGLint numConfigs;
  211.     EGLConfig config;
  212.     EGLSurface surface;
  213.     EGLContext context;
  214.     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  215.     eglInitialize(display, 0, 0);
  216.     /* Here, the application chooses the configuration it desires. In this
         * sample, we have a very simplified selection process, where we pick
         * the first EGLConfig that matches our criteria */
  217.     eglChooseConfig(display, attribs, &config, 1, &numConfigs);
  218.     /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
         * guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
         * As soon as we picked a EGLConfig, we can safely reconfigure the
         * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
  219.     eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
  220.     ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
  221.     surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
  222.     context = eglCreateContext(display, config, NULL, NULL);
  223.     if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
  224.         LOGW("Unable to eglMakeCurrent");
  225.         return -1;
  226.     }
  227.     eglQuerySurface(display, surface, EGL_WIDTH, &w);
  228.     eglQuerySurface(display, surface, EGL_HEIGHT, &h);
  229.     engine->display = display;
  230.     engine->context = context;
  231.     engine->surface = surface;
  232.     engine->width = w;
  233.     engine->height = h;
  234.     engine->state.angle =  0;
  235.     // Initialize GL state.
  236.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  237.     glEnable(GL_CULL_FACE);
  238.     glShadeModel(GL_SMOOTH);
  239.     glDisable(GL_DEPTH_TEST);
  240.     //unit.n177-sart-code
  241.  glMatrixMode(GL_PROJECTION);
  242.  glLoadIdentity();
  243.  glViewport(0, 0, w, h);
  244.  glLoadIdentity();
  245.  glOrthof(0.0f,w , 0.0f, h, -1.0f, 1.0f);
  246.     return  0;
  247. }
  248. /**
     * Just the current frame in the display.
     */
  249. static void engine_draw_frame(struct engine* engine) {
  250.     if (engine->display == NULL) {
  251.         // No display.
  252.         return;
  253.     }
  254. // Just fill the screen with a color.
  255. //glClearColor(((float)engine->state.x)/engine->width, engine->state.angle,
  256. //     ((float)engine->state.y)/engine->height, 1);
  257. //    glClear(GL_COLOR_BUFFER_BIT);
  258.     //unit.n177-code-start
  259.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  260.         glClearColor(0, 0, 1, 1);
  261.         glViewport(0, 0, 320, 480);
  262.         MxDraw(engine->mxBall,engine->mxPlatform);
  263.     //unit.n177-code-end
  264.     eglSwapBuffers(engine->display, engine->surface);
  265. }
  266. /**
     * Tear down the EGL context currently associated with the display.
     */
  267. static void engine_term_display(struct engine* engine) {
  268.     if (engine->display != EGL_NO_DISPLAY) {
  269.         eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  270.         if (engine->context != EGL_NO_CONTEXT) {
  271.             eglDestroyContext(engine->display, engine->context);
  272.         }
  273.         if (engine->surface != EGL_NO_SURFACE) {
  274.             eglDestroySurface(engine->display, engine->surface);
  275.         }
  276.         eglTerminate(engine->display);
  277.     }
  278.     engine->animating =  0;
  279.     engine->display = EGL_NO_DISPLAY;
  280.     engine->context = EGL_NO_CONTEXT;
  281.     engine->surface = EGL_NO_SURFACE;
  282. }
  283. /**
     * Process the next input event.
     */
  284. static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) {
  285.     struct engine* engine = (struct engine*)app->userData;
  286.     if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
  287.         engine->animating = 1;
  288.         engine->state.x = AMotionEvent_getX(event, 0);
  289.         engine->state.y = AMotionEvent_getY(event, 0);
  290.        if(AMotionEvent_getX(event,0)< 160)
  291.             {
  292.                 engine->mxPlatform->px -=engine->mxPlatform->dx;
  293.                 MxMoveVectoorCoords(engine->mxPlatform);
  294.             }
  295.          if(AMotionEvent_getX(event,0)>160)
  296.             {
  297.                 engine->mxPlatform->px +=engine->mxPlatform->dx;
  298.                 MxMoveVectoorCoords(engine->mxPlatform);
  299.             }
  300.         return 1;
  301.     }
  302.     return  0;
  303. }
  304. /**
     * Process the next main command.
     */
  305. static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
  306.     struct engine* engine = (struct engine*)app->userData;
  307.     switch (cmd) {
  308.         case APP_CMD_SAVE_STATE:
  309.             // The system has asked us to save our current state.  Do so.
  310.             engine->app->savedState = malloc(sizeof(struct saved_state));
  311.             *((struct saved_state*)engine->app->savedState) = engine->state;
  312.             engine->app->savedStateSize = sizeof(struct saved_state);
  313.             break;
  314.         case APP_CMD_INIT_WINDOW:
  315.             // The window is being shown, get it ready.
  316.             if (engine->app->window != NULL) {
  317.                 engine_init_display(engine);
  318.                 engine_draw_frame(engine);
  319.             }
  320.             break;
  321.         case APP_CMD_TERM_WINDOW:
  322.             // The window is being hidden or closed, clean it up.
  323.             engine_term_display(engine);
  324.             break;
  325.         case APP_CMD_GAINED_FOCUS:
  326.             // When our app gains focus, we start monitoring the accelerometer.
  327.             if (engine->accelerometerSensor != NULL) {
  328.                 ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                            engine->accelerometerSensor);
  329.                 // We'd like to get 60 events per second (in us).
  330.                 ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                            engine->accelerometerSensor, (1000L/60)*1000);
  331.             }
  332.             break;
  333.         case APP_CMD_LOST_FOCUS:
  334.             // When our app loses focus, we stop monitoring the accelerometer.
  335.             // This is to avoid consuming battery while not being used.
  336.             if (engine->accelerometerSensor != NULL) {
  337.                 ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                            engine->accelerometerSensor);
  338.             }
  339.             // Also stop animating.
  340.             engine->animating =  0;
  341.             engine_draw_frame(engine);
  342.             break;
  343.     }
  344. }
  345. /**
     * This is the main entry point of a native application that is using
     * android_native_app_glue.  It runs in its own thread, with its own
     * event loop for receiving input events and doing other things.
     */
  346. void android_main(struct android_app* state) {
  347.     struct engine engine;
  348.     struct MxSpriteData mxBall;
  349.     struct MxSpriteData mxPlatform;
  350.     MxInitialize( &mxBall, &mxPlatform);
  351.     // Make sure glue isn't stripped.
  352.     app_dummy();
  353.     memset(&engine, 0, sizeof(engine));
  354.     state->userData = &engine;
  355.     state->onAppCmd = engine_handle_cmd;
  356.     state->onInputEvent = engine_handle_input;
  357.     engine.app = state;
  358.     //unit.n177 kode
  359.     engine.mxBall = &mxBall;
  360.     engine.mxPlatform = &mxPlatform;
  361.     // Prepare to monitor accelerometer
  362.     engine.sensorManager = ASensorManager_getInstance();
  363.     engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
                ASENSOR_TYPE_ACCELEROMETER);
  364.     engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager,
                state->looper, LOOPER_ID_USER, NULL, NULL);
  365.     if (state->savedState != NULL) {
  366.         // We are starting with a previous saved state; restore from it.
  367.         engine.state = *(struct saved_state*)state->savedState;
  368.     }
  369.     // loop waiting for stuff to do.
  370.     while (1) {
  371.         // Read all pending events.
  372.         int ident;
  373.         int events;
  374.         struct android_poll_source* source;
  375.         // If not animating, we will block forever waiting for events.
  376.         // If animating, we loop until all events are read, then continue
  377.         // to draw the next frame of animation.
  378.         while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events,
                    (void**)&source)) >=  0) {
  379.             // Process this event.
  380.             if (source != NULL) {
  381.                 source->process(state, source);
  382.             }
  383.             // If a sensor has data, process it now.
  384.             if (ident == LOOPER_ID_USER) {
  385.                 if (engine.accelerometerSensor != NULL) {
  386.                     ASensorEvent event;
  387.                     while (ASensorEventQueue_getEvents(engine.sensorEventQueue,
                                &event, 1) >  0) {
  388.                         LOGI("accelerometer: x=%f y=%f z=%f",
                                    event.acceleration.x, event.acceleration.y,
                                    event.acceleration.z);
  389.                     }
  390.                 }
  391.             }
  392.             // Check if we are exiting.
  393.             if (state->destroyRequested !=  0) {
  394.                 engine_term_display(&engine);
  395.                 return;
  396.             }
  397.         }
  398.         if (engine.animating) {
  399.             // Done with events; draw next animation frame.
  400.             engine.state.angle += .01f;
  401.             if (engine.state.angle > 1) {
  402.                 engine.state.angle =  0;
  403.             }
  404.             // Drawing is throttled to the screen update rate, so there
  405.             // is no need to do timing here.
  406.             //unit.n177-code-start
  407.              MxUpdate(&engine);
  408.             //unit.n177-code-end
  409.             engine_draw_frame(&engine);
  410.         }
  411.     }
  412. }

Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2017