Ejemplo n.º 1
0
static void setup_state(const char* vtxs, const ALLEGRO_VERTEX_DECL* decl, ALLEGRO_BITMAP* texture)
{
   if(decl) {
      ALLEGRO_VERTEX_ELEMENT* e;
      e = &decl->elements[ALLEGRO_PRIM_POSITION];
      if(e->attribute) {
         int ncoord = 0;
         GLenum type = 0;
         bool normalized;

         glEnableClientState(GL_VERTEX_ARRAY);

         convert_storage(e->storage, &type, &ncoord, &normalized);

         glVertexPointer(ncoord, type, decl->stride, vtxs + e->offset);
      } else {
         glDisableClientState(GL_VERTEX_ARRAY);
      }

      e = &decl->elements[ALLEGRO_PRIM_TEX_COORD];
      if(!e->attribute)
         e = &decl->elements[ALLEGRO_PRIM_TEX_COORD_PIXEL];
      if(texture && e->attribute) {
         int ncoord = 0;
         GLenum type = 0;
         bool normalized;

         glEnableClientState(GL_TEXTURE_COORD_ARRAY);

         convert_storage(e->storage, &type, &ncoord, &normalized);

         glTexCoordPointer(ncoord, type, decl->stride, vtxs + e->offset);
      } else {
         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
      }

      e = &decl->elements[ALLEGRO_PRIM_COLOR_ATTR];
      if(e->attribute) {
         glEnableClientState(GL_COLOR_ARRAY);

         glColorPointer(4, GL_FLOAT, decl->stride, vtxs + e->offset);
      } else {
         glDisableClientState(GL_COLOR_ARRAY);
         glColor4f(1, 1, 1, 1);
      }
   } else {
      const ALLEGRO_VERTEX* vtx = (const ALLEGRO_VERTEX*)vtxs;
   
      glEnableClientState(GL_COLOR_ARRAY);
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_TEXTURE_COORD_ARRAY);

      glVertexPointer(3, GL_FLOAT, sizeof(ALLEGRO_VERTEX), &vtx[0].x);
      glColorPointer(4, GL_FLOAT, sizeof(ALLEGRO_VERTEX), &vtx[0].color.r);
      glTexCoordPointer(2, GL_FLOAT, sizeof(ALLEGRO_VERTEX), &vtx[0].u);
   }

   if (texture) {
      GLuint gl_texture = al_get_opengl_texture(texture);
      int true_w, true_h;
      int tex_x, tex_y;
      GLuint current_texture;
      float mat[4][4] = {
         {1,  0,  0, 0},
         {0, -1,  0, 0},
         {0,  0,  1, 0},
         {0,  0,  0, 1}
      };
      int height;

      if (texture->parent)
         height = texture->parent->h;
      else
         height = texture->h;
      
      al_get_opengl_texture_size(texture, &true_w, &true_h);
      al_get_opengl_texture_position(texture, &tex_x, &tex_y);
      
      mat[3][0] = (float)tex_x / true_w;
      mat[3][1] = (float)(height - tex_y) / true_h;
         
      if(decl) {
         if(decl->elements[ALLEGRO_PRIM_TEX_COORD_PIXEL].attribute) {
            mat[0][0] = 1.0f / true_w;
            mat[1][1] = -1.0f / true_h;
         } else {
            mat[0][0] = (float)al_get_bitmap_width(texture) / true_w;
            mat[1][1] = -(float)al_get_bitmap_height(texture) / true_h;
         }
      } else {
         mat[0][0] = 1.0f / true_w;
         mat[1][1] = -1.0f / true_h;
      }

      glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&current_texture);
      if (current_texture != gl_texture) {
         glBindTexture(GL_TEXTURE_2D, gl_texture);
      }

      glMatrixMode(GL_TEXTURE);
      glLoadMatrixf(mat[0]);
      glMatrixMode(GL_MODELVIEW);
   } else {
      glBindTexture(GL_TEXTURE_2D, 0);
   }
}
Ejemplo n.º 2
0
void fd_draw_tinted_scaled_bitmap(FAST_DRAW_CACHE* cache, ALLEGRO_BITMAP* bmp, ALLEGRO_COLOR tint, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh)
{
	int ii;
	int offx = 0;
	int offy = 0;
	ALLEGRO_VERTEX* vertices;
	int* indices;
	ALLEGRO_BITMAP* parent = al_get_parent_bitmap(bmp);

	if(parent == 0)
		parent = bmp;
	else
		al_get_opengl_texture_position(bmp, &offx, &offy); /* HACK */

	if(parent != cache->bitmap)
		fd_flush_cache(cache);

	cache->bitmap = parent;

	get_pointers(cache, &vertices, &indices);

	if(cache->use_indices)
	{
		/* 0,3    4    0     1
		 *   o---o      o---o
		 *   |\  |      |\  |
		 *   | \ |      | \ |
		 *   |  \|      |  \|
		 *   o---o      o---o
		 *  2    1,5   2     3
		 */
		int offi = (cache->size - 1) * 4;
		indices[0] = offi + 0;
		indices[1] = offi + 2;
		indices[2] = offi + 3;
		indices[3] = offi + 0;
		indices[4] = offi + 1;
		indices[5] = offi + 2;

		vertices[0].x = dx;
		vertices[0].y = dy;
		vertices[0].u = sx + offx;
		vertices[0].v = sy + offy;

		vertices[1].x = dx + dw;
		vertices[1].y = dy;
		vertices[1].u = sx + sw + offx;
		vertices[1].v = sy + offy;

		vertices[2].x = dx + dw;
		vertices[2].y = dy + dh;
		vertices[2].u = sx + sw + offx;
		vertices[2].v = sy + sh + offy;

		vertices[3].x = dx;
		vertices[3].y = dy + dh;
		vertices[3].u = sx + offx;
		vertices[3].v = sy + sh + offy;

		for(ii = 0; ii < 4; ii++)
		{
			vertices[ii].z = 0;
			vertices[ii].color = tint;
		}
	}
	else
	{
		/* 0,3    4
		 *   o---o
		 *   |\  |
		 *   | \ |
		 *   |  \|
		 *   o---o
		 *  2    1,5
		 */

		vertices[0].x = dx;
		vertices[0].y = dy;
		vertices[0].u = sx + offx;
		vertices[0].v = sy + offy;

		vertices[1].x = dx + dw;
		vertices[1].y = dy + dh;
		vertices[1].u = sx + sw + offx;
		vertices[1].v = sy + sh + offy;

		vertices[2].x = dx;
		vertices[2].y = dy + dh;
		vertices[2].u = sx + offx;
		vertices[2].v = sy + sh + offy;

		vertices[3].x = dx;
		vertices[3].y = dy;
		vertices[3].u = sx + offx;
		vertices[3].v = sy + offy;

		vertices[4].x = dx + dw;
		vertices[4].y = dy;
		vertices[4].u = sx + sw + offx;
		vertices[4].v = sy + offy;

		vertices[5].x = dx + dw;
		vertices[5].y = dy + dh;
		vertices[5].u = sx + sw + offx;
		vertices[5].v = sy + sh + offy;

		for(ii = 0; ii < 6; ii++)
		{
			vertices[ii].z = 0;
			vertices[ii].color = tint;
		}
	}
}