Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 13th, 2012  |  syntax: None  |  size: 4.12 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <allegro5/allegro.h>
  3. #include <allegro5/allegro_font.h>
  4. #include <allegro5/allegro_ttf.h>
  5. #include <allegro5/allegro_primitives.h>
  6. #include <gl/gl.h>
  7. #include <gl/glu.h>
  8. #include <allegro5/allegro_opengl.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11.  
  12. using namespace std;
  13.  
  14. /////////
  15. // Global variables specific to the demo
  16.  
  17. static const unsigned int palleteSize = 3;
  18.  
  19. struct palleteRGBA{
  20.         unsigned char r;
  21.         unsigned char g;
  22.         unsigned char b;
  23.         unsigned char a;
  24. };
  25.  
  26. static palleteRGBA palette[palleteSize];
  27.  
  28.  
  29. // Just one texture right now.
  30. static const unsigned int numTextures = 1;
  31.  
  32. GLuint textures[numTextures];
  33.  
  34.  
  35. // Texture Data
  36.  
  37. // This contains INDICES into our palette array!
  38. static unsigned char textureToUse[256][256];
  39.  
  40. /////////
  41.  
  42.  
  43.  
  44. int main(int argc, char **argv)
  45. {
  46.         ALLEGRO_DISPLAY *display = NULL;
  47.         ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  48.  
  49.                 if(!al_init()) {
  50.                 fprintf(stderr, "failed to initialize allegro!\n");
  51.                 return -1;
  52.         }
  53.  
  54.         al_set_new_display_flags(ALLEGRO_OPENGL);
  55.         display = al_create_display(640, 480);
  56.         if(!display) {
  57.                 fprintf(stderr, "failed to create display!\n");
  58.                 return -1;
  59.         }
  60.  
  61.         event_queue = al_create_event_queue();
  62.         if(!event_queue) {
  63.                 fprintf(stderr, "failed to create event_queue!\n");
  64.                 al_destroy_display(display);
  65.                 return -1;
  66.         }
  67.  
  68.  
  69.         ////////
  70.         // Initialize OpenGL functions we want
  71.         glEnable(GL_TEXTURE_2D);
  72.         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  73.  
  74.  
  75.         glGenTextures(numTextures ,textures);
  76.  
  77.  
  78.  
  79.         // EXT OpenGL functions
  80.  
  81.         // This isn't passing.
  82.  
  83. //*
  84. // define the type of the function
  85. ALLEGRO_DEFINE_PROC_TYPE(void, COLOR_TABLE_FUNC,
  86.       (GLenum, GLenum, GLsizei, GLenum, GLenum, const GLvoid *));
  87.  
  88.  
  89.  
  90. //void glColorTableEXT(GLenum target,
  91. //                     GLenum internalformat,
  92. //                     GLsizei width,
  93. //                     GLenum format,
  94. //                     GLenum type,
  95. //                     const GLvoid *table)
  96.  
  97.  
  98.  
  99. // declare the function pointer
  100. COLOR_TABLE_FUNC glColorTableEXT;
  101.  
  102. // get the address of the function
  103. glColorTableEXT = (COLOR_TABLE_FUNC) al_get_opengl_proc_address("glColorTableEXT");
  104. //*/
  105.  
  106.         if(!al_have_opengl_extension("GL_EXT_paletted_texture"))
  107.                 //exit(1); // We don't have it!
  108.  
  109.         ////////
  110.  
  111.         // Initialize a palette (temporary) to use:
  112.         // Red
  113.         palette[0].r = 255;
  114.         palette[0].g = 0;
  115.         palette[0].b = 0;
  116.         palette[0].a = 255;
  117.         // Green
  118.         palette[1].r = 0;
  119.         palette[1].g = 255;
  120.         palette[1].b = 0;
  121.         palette[1].a = 255;
  122.         // Blue
  123.         palette[2].r = 0;
  124.         palette[2].g = 0;
  125.         palette[2].b = 255;
  126.         palette[2].a = 255;
  127.  
  128.         // Populate the indicies
  129.         for(int x = 0; x < 100; x++){
  130.                 for(int y = 0; y < 256; y++)
  131.                         textureToUse[x][y] = 0; //red
  132.         }
  133.         for(int x = 101; x < 200; x++){
  134.                 for(int y = 0; y < 256; y++)
  135.                         textureToUse[x][y] = 1; //green
  136.         }
  137.         for(int x = 201; x < 256; x++){
  138.                 for(int y = 0; y < 256; y++)
  139.                         textureToUse[x][y] = 2; //blue
  140.         }
  141.  
  142.         al_register_event_source(event_queue, al_get_display_event_source(display));
  143.  
  144.         al_clear_to_color(al_map_rgb(0,0,0));
  145.  
  146.         al_flip_display();
  147.  
  148.  
  149.         while(1)
  150. {
  151.                 ALLEGRO_EVENT ev;
  152.                 ALLEGRO_TIMEOUT timeout;
  153.                 al_init_timeout(&timeout, 0.06);
  154.  
  155.                 bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
  156.  
  157.                 if(get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  158.                         break;
  159.                 }
  160.  
  161.  
  162.  
  163.                 //////////
  164.  
  165.                 // Our OpenGL-Specific code goes here:
  166.  
  167.                 glBindTexture(GL_TEXTURE_2D, 1);
  168.  
  169.                 glColorTableEXT(GL_TEXTURE_2D, GL_RGBA8, 256, GL_RGBA, GL_UNSIGNED_BYTE, palette);
  170.  
  171.                 glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, 256, 256, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, textureToUse);
  172.  
  173.  
  174.                 glBegin(GL_QUADS);                      // Draw A Quad
  175.                         glTexCoord2f(0.0f, 1.0f);glVertex3f(-1.0f, 1.0f, 0.0f);
  176.                         glTexCoord2f(1.0f, 1.0f);glVertex3f( 1.0f, 1.0f, 0.0f);
  177.                         glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
  178.                         glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
  179.                 glEnd();
  180.  
  181.                 //////////
  182.  
  183.                 al_clear_to_color(al_map_rgb(0,0,0));
  184.                 al_flip_display();
  185.         }
  186.  
  187.         al_destroy_display(display);
  188.         al_destroy_event_queue(event_queue);
  189.  
  190.         return 0;
  191. }