Advertisement
Guest User

Android SDL GLES Test

a guest
Mar 12th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #ifdef _WIN32
  6.     #include <windows.h>
  7. #endif
  8.  
  9. #ifdef __APPLE__
  10. #include <SDL.h>
  11. #include <SDL_mixer.h>
  12. #include <SDL_image.h>
  13. #include <SDL_ttf.h>
  14. #include <SDL_opengl.h>
  15. #include <OpenGL/gl.h>
  16. #include <OpenGL/glu.h>
  17. #else
  18. //  #if SDL_VERSION_ATLEAST(2,0,0)
  19.         /* SDL 2.0 code */
  20.         #include <SDL.h>
  21.         #include <SDL_mixer.h>
  22.         #include <SDL_image.h>
  23.         #include <SDL_ttf.h>
  24.         //#include <SDL_opengl.h>
  25. /*  #else
  26.         /* SDL 1.2 code
  27.         #include <SDL/SDL.h>
  28.         #include <SDL/SDL_mixer.h>
  29.         #include <SDL/SDL_image.h>
  30.         #include <SDL/SDL_ttf.h>
  31.         #include <SDL/SDL_opengl.h>
  32.     #endif
  33. */
  34.  
  35. #include <GLES/gl.h>
  36. //#include <GL/glu.h>
  37. #endif
  38.  
  39. #ifdef __APPLE__
  40. #include "CoreFoundation/CoreFoundation.h"
  41. #endif
  42.  
  43. #include <math.h>
  44. #include <time.h>
  45. #include "../../common/include/gui.h"
  46. #include <jni.h>
  47.  
  48. // g_env is set in SDL_android_main.cpp
  49. extern JNIEnv *g_env;
  50.  
  51. //#define LOSEPOWER
  52. #define MAXTEX 128
  53. #define MAXWH 256
  54. #define TURRETMAX MAXTEX
  55. #define WIDTH 800
  56. #define HEIGHT 480
  57. #define BPP 32
  58. #define EN(x) printf("Entering %s\n",x);
  59. #define EX(x) printf("Exiting %s\n",x);
  60. #define MAXARRAY 256
  61. #define EMAX 128
  62. #define BOSSAT 9
  63. #define PI 3.14159265
  64.  
  65. const int FRAMES_PER_SECOND = 100;
  66.  
  67. #include <android/log.h>
  68.  
  69. #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "org.libsdl.app",__VA_ARGS__)
  70. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG  , "org.libsdl.app",__VA_ARGS__)
  71. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO   , "org.libsdl.app",__VA_ARGS__)
  72. #define LOGW(...) __android_log_print(ANDROID_LOG_WARN   , "org.libsdl.app",__VA_ARGS__)
  73. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , "org.libsdl.app",__VA_ARGS__)
  74.  
  75.  
  76. int main(int argc, char *argv[])
  77. {
  78.  
  79. // PROGRAM VARABLES : REQUIRED VARABLES
  80.     SDL_Event event;
  81.     SDL_Keysym keysym;
  82.     int quit=0;
  83.     GLubyte *vendor;
  84.     GLubyte *renderer;
  85.     GLubyte *version;
  86.     GLubyte *extensions;
  87.  
  88. // PROGRAM VARABLES
  89.     struct container screen;
  90.     int len=0;
  91.     int key=0;
  92.     int d,e,l,f;
  93.  
  94.     struct textures menuList[MAXTEX];
  95.     GLuint menuCount=0;
  96.     GLuint menuTextures[MAXTEX];
  97.     int menuW[MAXTEX]; int menuW2[MAXTEX];
  98.     int menuH[MAXTEX]; int menuH2[MAXTEX];
  99.     GLfloat texcoord[4];
  100.     char text[100];
  101.  
  102.     // Set screen resolution
  103.     screen.area.x=0;
  104.     screen.area.y=0;
  105.     screen.area.w=WIDTH;
  106.     screen.area.h=HEIGHT;
  107.     int windowed=0;
  108.     int err=0;
  109.    
  110.     // Run OGL/SDL Startup code
  111.     startup(&screen,&windowed,vendor,renderer,version,extensions);
  112.     LOGD("SDL Started OK");
  113.     // You should check the return value, here we assume logo.png is found
  114.  
  115.     // Do whatever you want with the rwops
  116.     SDL_Surface *i = IMG_Load("cover.png");
  117.    
  118.     menuCount=1;
  119.     SDL_GL_LoadTexture(i,menuTextures,&menuCount, texcoord,menuW,menuH,menuW2, menuH2);
  120.     LOGD("Texture Loaded OK");
  121.    
  122.     // Draw Title While loading
  123.     SDL_GL_Enter2DMode();
  124.     err=glGetError();
  125.     sprintf(text,"GL ERROR CODE 1 IN MAIN IS %d",err);
  126.     LOGD(text);
  127.     // Draw Loading Progress
  128.  
  129.  
  130.     GLint texSize; glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
  131.     sprintf(text,"Dimensions: %d %d %d %d Max Texture Size: %d",menuW[0],menuH[0],menuW2[0],menuH2[0],texSize);
  132.     LOGD(text);
  133.  
  134.     // Main Loop
  135.     while ( ! quit )
  136.     {
  137.    
  138.         // Event Handling
  139.         while ( SDL_PollEvent( &event ) )
  140.         {
  141.             switch(event.type)
  142.             {
  143.                 case SDL_FINGERDOWN:
  144.                     quit=1;
  145.                 break;
  146.  
  147.                 case SDL_QUIT:
  148.                     quit=1;
  149.                 break;
  150.  
  151.             }
  152.         }
  153.  
  154.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  155.         glLoadIdentity();
  156.  
  157.         draw(&screen,0,menuTextures,0,0,menuW[0],menuH[0],menuW2[0],menuH2[0]);
  158.         SDL_GL_SwapWindow(screen.image);
  159.    
  160.     }  
  161.  
  162.     LOGD("Program Closing, Cleaning up");
  163.     glDeleteTextures(menuCount,menuTextures);
  164.  
  165.     SDL_Quit();
  166.     exit(0);
  167.  
  168.     return(0);
  169. }
  170.  
  171. int SDL_GL_LoadTexture(SDL_Surface *tempSurf, GLuint *texture, GLuint *texcount, GLfloat *texcoord, int *w, int *h, int *w2, int *h2)
  172. {
  173.     //SDL_Surface *tempSurf[100];
  174.     SDL_Surface *surface[100];
  175.     int d=0;
  176.     int x,y;
  177.     int tempW[100];
  178.     int tempH[100];
  179.     int err=0;
  180.     char text[100];
  181.     SDL_Rect area;
  182.     Uint32 saved_flags;
  183.     Uint8  saved_alpha;
  184.  
  185.     // Load all surfaces in list
  186.     //for(d=0;d<*(texcount);d++)
  187.     //{
  188.         //tempSurf=loadSurface(list[d].name);
  189.         *(w)=0;
  190.         *(h)=0;
  191.         *(w) = tempSurf->w;
  192.         *(h) = tempSurf->h;
  193.         tempW[0]=0;
  194.         tempH[0]=0;
  195.         tempW[0]=pow2(*(w+d));
  196.         tempH[0]=pow2(*(h+d));
  197.        
  198.         *(w2)=0;
  199.         *(h2)=0;
  200.         *(w2)=tempW[0];
  201.         *(h2)=tempH[0];
  202.    
  203.  
  204.     for(d=0;d<*(texcount);d++)
  205.     {
  206.         if ( tempSurf->format->BytesPerPixel == 3 )
  207.         {
  208.  
  209.         surface[d] = SDL_CreateRGBSurface(
  210.                 SDL_SWSURFACE,
  211.                 tempW[d], tempH[d],
  212.                 24,
  213.         #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
  214.                     0x000000FF,
  215.                     0x0000FF00,
  216.                     0x00FF0000,
  217.                     0xFF000000
  218.         #else
  219.                     0xFF000000,
  220.                     0x00FF0000,
  221.                     0x0000FF00,
  222.                     0x000000FF
  223.         #endif
  224.                    );
  225.         }else{
  226.             surface[d] = SDL_CreateRGBSurface(
  227.                     SDL_SWSURFACE,
  228.                     tempW[d], tempH[d],
  229.                     32,
  230.         #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
  231.                     0x000000FF,
  232.                     0x0000FF00,
  233.                     0x00FF0000,
  234.                     0xFF000000
  235.         #else
  236.                     0xFF000000,
  237.                     0x00FF0000,
  238.                     0x0000FF00,
  239.                     0x000000FF
  240.         #endif
  241.                        );
  242.  
  243.         }
  244. /*
  245.     // Save the alpha blending attributes
  246.         saved_flags = tempSurf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
  247.         saved_alpha = tempSurf->format->alpha;
  248.         if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
  249.         {
  250.                 SDL_SetAlpha(tempSurf, 0, 0);
  251.         }*/
  252.         SDL_SetSurfaceBlendMode(tempSurf, SDL_BLENDMODE_NONE);
  253.         SDL_GetSurfaceAlphaMod(tempSurf,&saved_alpha);
  254.         SDL_SetSurfaceAlphaMod(tempSurf,255);
  255.         SDL_BlitSurface(tempSurf, NULL, surface[0], NULL);
  256.         SDL_SetSurfaceAlphaMod(tempSurf,saved_alpha);
  257. /*
  258.     // Restore the alpha blending attributes
  259.         if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
  260.         {
  261.                 SDL_SetAlpha(tempSurf, saved_flags, saved_alpha);
  262.         }*/
  263.  
  264.     }
  265.  
  266.     // Genorate and bind textures in list
  267.     glGenTextures(*(texcount), texture);
  268.     for(d=0;d<*(texcount);d++)
  269.     {
  270.         glBindTexture(GL_TEXTURE_2D, *(texture+d));
  271.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  272.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  273.         if ( surface[d]->format->BytesPerPixel == 3 )
  274.         {
  275.             glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,tempW[d],tempH[d],0,GL_RGB,GL_UNSIGNED_BYTE,surface[d]->pixels);
  276.         }else{
  277.             glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,tempW[d],tempH[d],0,GL_RGBA,GL_UNSIGNED_BYTE,surface[d]->pixels);
  278.         }
  279.        
  280.     }
  281.  
  282.     err=glGetError();
  283.     sprintf(text,"GL ERROR CODE IS %d",err);
  284.     LOGD(text);
  285.  
  286.     if ( surface[0]->pixels != NULL )
  287.     {
  288.         LOGD("Image has data");
  289.     }
  290.  
  291.     SDL_FreeSurface(tempSurf);
  292.  
  293.     for(d=0;d<*(texcount);d++)
  294.     {
  295.             SDL_FreeSurface(surface[d]);
  296.     }
  297.     return(0);
  298. }
  299.  
  300. int draw(struct container *screen, int entity, GLuint *texture, int x, int y, int w, int h, int tempW, int tempH)
  301. {
  302.  
  303.     // open png for tile
  304.     //SDL_GL_Enter2DMode();
  305.     //int tempW=pow2(w);
  306.     //int tempH=pow2(h);
  307.     float height=(float)h/(float)tempH;
  308.     float width=(float)w/(float)tempW;
  309.  
  310.     glBindTexture( GL_TEXTURE_2D, *(texture+entity) );
  311.  
  312.     const GLfloat t[8] = { 0.0, 0.0, width,   0.0, width,   height,   0.0, height };
  313.     const GLshort v[8] = { x,   y,   x+w,     y,   x+w,     y+h,      x,   y+h };
  314.  
  315.     glVertexPointer( 2, GL_SHORT, 0, v );
  316.     glEnableClientState( GL_VERTEX_ARRAY );
  317.  
  318.     glTexCoordPointer( 2, GL_FLOAT, 0, t );
  319.     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  320.  
  321.     glDrawArrays( GL_TRIANGLE_FAN, 0, 4 );
  322.  
  323.     return(0);
  324.  
  325. }
  326.  
  327. void SDL_GL_Enter2DMode()
  328. {
  329.     glColor4ub(255,255,255,255);
  330.     glEnable(GL_TEXTURE_2D);   
  331.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);//GL_DECAL);
  332.     //glPushAttrib(GL_ENABLE_BIT);
  333.     glDisable(GL_DEPTH_TEST);
  334.     glDisable(GL_CULL_FACE);
  335.     glDisable( GL_LIGHTING );
  336.     glEnable(GL_BLEND);
  337.     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  338.     glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  339.    
  340.    
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement