示例#1
0
文件: theme.c 项目: beoran/WidgetZ
static void wz_draw_3d_rectangle(float x1, float y1, float x2, float y2, float border, ALLEGRO_COLOR col, bool invert)
{
	ALLEGRO_VERTEX vtx[6];
	ALLEGRO_COLOR hi, lo;
	int ii;
	
	if(invert)
	{
		lo = wz_scale_color(col, 1.5);
		hi = wz_scale_color(col, 0.5);
	}
	else
	{
		hi = wz_scale_color(col, 1.5);
		lo = wz_scale_color(col, 0.5);
	}
	
	for(ii = 0; ii < 6; ii++)
	{
		vtx[ii].color = hi;
		vtx[ii].z = 0;
	}
	
	vtx[0].x = x1 + border; vtx[0].y = y1 + border;
	vtx[1].x = x1 + border; vtx[1].y = y2 - border;
	vtx[2].x = x1;          vtx[2].y = y2;
	vtx[3].x = x1;          vtx[3].y = y1;
	vtx[4].x = x2;          vtx[4].y = y1;
	vtx[5].x = x2 - border; vtx[5].y = y1 + border;
	
	al_draw_prim(vtx, 0, 0, 0, 6, ALLEGRO_PRIM_TRIANGLE_FAN);
	
	vtx[0].x = x2 - border; vtx[0].y = y2 - border;
	vtx[1].x = x1 + border; vtx[1].y = y2 - border;
	vtx[2].x = x1;          vtx[2].y = y2;
	vtx[3].x = x2;          vtx[3].y = y2;
	vtx[4].x = x2;          vtx[4].y = y1;
	vtx[5].x = x2 - border; vtx[5].y = y1 + border;
	
	for(ii = 0; ii < 6; ii++)
	{
		vtx[ii].color = lo;
	}
	
	al_draw_prim(vtx, 0, 0, 0, 6, ALLEGRO_PRIM_TRIANGLE_FAN);
	
	al_draw_filled_rectangle(x1 + border, y1 + border, x2 - border, y2 - border, col);
}
ALLEGRO_BITMAP *generate_triangle_bitmap(float x1, float y1, float x2, float y2, float x3, float y3, ALLEGRO_COLOR col)
{
   // find the width and height of the bitmap (this could be more comprehensive? What about -negative points?)
   float max_x = std::max(std::max(x1, x2), x2);
   float max_y = std::max(std::max(y1, y2), y3);

   // start drawing
   ALLEGRO_BITMAP *surface = al_create_bitmap(max_x, max_y);
   ALLEGRO_STATE state;
   al_store_state(&state, ALLEGRO_STATE_TARGET_BITMAP);
   al_clear_to_color(color::transparent);
   al_set_target_bitmap(surface);

   // build our triangle
   ALLEGRO_VERTEX v[3];
   v[0] = build_vertex(x1, y1, 0, col, 0, 0);
   v[1] = build_vertex(x2, y2, 0, col, 0, 0);
   v[2] = build_vertex(x3, y3, 0, col, 0, 0);

   // draw the triangle
   al_draw_prim(v, NULL, NULL, 0, 3, ALLEGRO_PRIM_TRIANGLE_LIST);

   // restore drawing state
   al_restore_state(&state);
   return surface;
}
ALLEGRO_BITMAP *generate_gradient_bitmap(float size, ALLEGRO_COLOR top_color, ALLEGRO_COLOR bottom_color, int padding)
{
   // set everything up for rendering
   int w = size;
   int h = size;
   ALLEGRO_BITMAP *surface = al_create_bitmap(w, h);
   ALLEGRO_STATE previous_state;
   al_store_state(&previous_state, ALLEGRO_STATE_TARGET_BITMAP);

   // start drawing on the bitmap
   al_set_target_bitmap(surface);
   al_clear_to_color(color::transparent);

   // build the gradient as a primitive
   ALLEGRO_VERTEX v[4];
   v[0] = build_vertex(0+padding, 0+padding, 0, top_color, 0, 0);
   v[1] = build_vertex(w-padding, 0+padding, 0, top_color, 0, 0);
   v[2] = build_vertex(w-padding, h-padding, 0, bottom_color, 0, 0);
   v[3] = build_vertex(0+padding, h-padding, 0, bottom_color, 0, 0);

   // draw it to the surface
   al_draw_prim(v, NULL, NULL, 0, 4, ALLEGRO_PRIM_TRIANGLE_FAN);

   // restore everything back to where it was
   al_restore_state(&previous_state);

   // return the generated image
   return surface; 
}
示例#4
0
static duk_ret_t
js_PointSeries(duk_context* ctx)
{
	duk_require_object_coercible(ctx, 0);
	color_t color = duk_require_sphere_color(ctx, 1);

	size_t          num_points;
	int             x, y;
	ALLEGRO_VERTEX* vertices;
	ALLEGRO_COLOR   vtx_color;

	size_t i;

	if (!duk_is_array(ctx, 0))
		duk_error_ni(ctx, -1, DUK_ERR_ERROR, "PointSeries(): First argument must be an array");
	duk_get_prop_string(ctx, 0, "length"); num_points = duk_get_uint(ctx, 0); duk_pop(ctx);
	if (num_points < 1)
		duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "PointSeries(): One or more vertices required");
	if (num_points > INT_MAX)
		duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "PointSeries(): Too many vertices");
	if ((vertices = calloc(num_points, sizeof(ALLEGRO_VERTEX))) == NULL)
		duk_error_ni(ctx, -1, DUK_ERR_ERROR, "PointSeries(): Failed to allocate vertex buffer");
	vtx_color = nativecolor(color);
	for (i = 0; i < num_points; ++i) {
		duk_get_prop_index(ctx, 0, (duk_uarridx_t)i);
		duk_get_prop_string(ctx, 0, "x"); x = duk_require_int(ctx, -1); duk_pop(ctx);
		duk_get_prop_string(ctx, 0, "y"); y = duk_require_int(ctx, -1); duk_pop(ctx);
		duk_pop(ctx);
		vertices[i].x = x + 0.5; vertices[i].y = y + 0.5;
		vertices[i].color = vtx_color;
	}
	al_draw_prim(vertices, NULL, NULL, 0, (int)num_points, ALLEGRO_PRIM_POINT_LIST);
	free(vertices);
	return 0;
}
示例#5
0
static void FilledTexturePrimitives(int mode)
{
   static ALLEGRO_VERTEX vtx[21];
   if (mode == INIT) {
      int ii = 0;
      for (ii = 0; ii < 21; ii++) {
         float x, y;
         ALLEGRO_COLOR color;
         if (ii % 2 == 0) {
            x = 150 * cosf((float)ii / 20 * 2 * ALLEGRO_PI);
            y = 150 * sinf((float)ii / 20 * 2 * ALLEGRO_PI);
         } else {
            x = 200 * cosf((float)ii / 20 * 2 * ALLEGRO_PI);
            y = 200 * sinf((float)ii / 20 * 2 * ALLEGRO_PI);
         }
         
         if (ii == 0) {
            x = y = 0;
         }
         
         color = al_map_rgb((7 * ii + 1) % 3 * 64, (2 * ii + 2) % 3 * 64, (ii) % 3 * 64);
         
         vtx[ii].x = x; vtx[ii].y = y; vtx[ii].z = 0;
         vtx[ii].u = 64 * x / 100; vtx[ii].v = 64 * y / 100;
         if(ii < 10)
            vtx[ii].color = al_map_rgba_f(1, 1, 1, 1);
         else
            vtx[ii].color = color;
      }
   } else if (mode == LOGIC) {
      Theta += Speed;
      al_build_transform(&MainTrans, ScreenW / 2, ScreenH / 2, 1, 1, Theta);
   } else if (mode == DRAW) {
      if (Blend)
         al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);
      else
         al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
  
      al_use_transform(&MainTrans);
      
      al_draw_prim(vtx, 0, Texture, 0, 6, ALLEGRO_PRIM_TRIANGLE_FAN);
      al_draw_prim(vtx, 0, Texture, 7, 13, ALLEGRO_PRIM_TRIANGLE_LIST);
      al_draw_prim(vtx, 0, Texture, 14, 20, ALLEGRO_PRIM_TRIANGLE_STRIP);
      
      al_use_transform(&Identity);
   }
}
示例#6
0
void twoDPrimitive::draw(){
	ALLEGRO_BITMAP* texture = NULL;

	if(this->texture != NULL)
		texture = this->texture->getBitmap();

	al_draw_prim(this->vertices, NULL, texture, 0, this->numVertices, this->type);
}
示例#7
0
static void TexturePrimitives(int mode)
{
   static ALLEGRO_VERTEX vtx[13];
   static ALLEGRO_VERTEX vtx2[13];
   if (mode == INIT) {
      int ii = 0;
      ALLEGRO_COLOR color;
      for (ii = 0; ii < 13; ii++) {
         float x, y;
         x = 200 * cosf((float)ii / 13.0f * 2 * ALLEGRO_PI);
         y = 200 * sinf((float)ii / 13.0f * 2 * ALLEGRO_PI);
         
         color = al_map_rgb((ii + 1) % 3 * 64, (ii + 2) % 3 * 64, (ii) % 3 * 64);
         
         vtx[ii].x = x; vtx[ii].y = y; vtx[ii].z = 0;
         vtx2[ii].x = 0.1 * x; vtx2[ii].y = 0.1 * y;
         vtx[ii].u = 64 * x / 100; vtx[ii].v = 64 * y / 100;
         vtx2[ii].u = 64 * x / 100; vtx2[ii].v = 64 * y / 100;
         if(ii < 10)
            vtx[ii].color = al_map_rgba_f(1, 1, 1, 1);
         else
            vtx[ii].color = color;
         vtx2[ii].color = vtx[ii].color;
      }
   } else if (mode == LOGIC) {
      Theta += Speed;
      al_build_transform(&MainTrans, ScreenW / 2, ScreenH / 2, 1, 1, Theta);
   } else if (mode == DRAW) {
      if (Blend)
         al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);
      else
         al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
      
      al_use_transform(&MainTrans);
      
      al_draw_prim(vtx, 0, Texture, 0, 4, ALLEGRO_PRIM_LINE_LIST);
      al_draw_prim(vtx, 0, Texture, 4, 9, ALLEGRO_PRIM_LINE_STRIP);
      al_draw_prim(vtx, 0, Texture, 9, 13, ALLEGRO_PRIM_LINE_LOOP);
      al_draw_prim(vtx2, 0, Texture, 0, 13, ALLEGRO_PRIM_POINT_LIST);
      
      al_use_transform(&Identity);
   }
}
示例#8
0
int main(void)
{
	al_init();

	ALLEGRO_DISPLAY *d = al_create_display(W, H);

	ALLEGRO_VERTEX v[POINTS];
	ALLEGRO_COLOR c;

	v[0].x = 0;
	v[0].y = 0;
	c = al_map_rgb(rand()%256, rand()%256, rand()%256);
	v[0].color = al_get_prim_color(c);
	v[1].x = 0+R;
	v[1].y = 0;
	c = al_map_rgb(rand()%256, rand()%256, rand()%256);
	v[1].color = al_get_prim_color(c);

	float a = 0;
	float r = R;
	int i;

	for (i = 2; i < POINTS; i++) {
		v[i].x = 0+cos(a)*r;
		v[i].y = 0+sin(a)*r;
		a += 0.3f;
		r -= 1.5f;
		c = al_map_rgb(rand()%256, rand()%256, rand()%256);
		v[i].color = al_get_prim_color(c);
	}

	int frames = 0;
	ALLEGRO_TRANSFORM t;

	while (true) {
		al_clear_to_color(al_map_rgb(0, 0, 0));
		al_identity_transform(&t);
		al_rotate_transform(&t, frames*0.1f);
		al_translate_transform(&t, W/2, H/2);
		al_use_transform(&t);
		al_draw_prim(v, NULL, 0, POINTS, ALLEGRO_PRIM_TRIANGLE_FAN);
		al_flip_display();
		/* GP2X Wiz is locked to 60FPS using OpenGL */
		frames++;
		if (frames > 400)
			break;
	}

	al_uninstall_system();

	return 0;
}
示例#9
0
void fd_flush_cache(FAST_DRAW_CACHE* cache)
{
	assert(cache);

	if(cache->size == 0)
		return;

	if(cache->use_indices)
		al_draw_indexed_prim(cache->vertices, 0, cache->bitmap, cache->indices, 6 * cache->size, ALLEGRO_PRIM_TRIANGLE_LIST);
	else
		al_draw_prim(cache->vertices, 0, cache->bitmap, 0, 6 * cache->size, ALLEGRO_PRIM_TRIANGLE_LIST);

	cache->bitmap = 0;
	cache->size = 0;
}
示例#10
0
void AIMap::drawObstacle(AIGameClient_Obstacle obstacle) {
/*
 float v[obstacle.vertex.size()*2];
 for(int i = 0; i < obstacle.vertex.size(); i++) {
   v[2*i] = obstacle.vertex[i].first;
   v[2*i+1] = obstacle.vertex[i].second;
 }
 al_draw_filled_polygon(v, obstacle.vertex.size(), al_map_rgb(50, 50, 50));
*/
  ALLEGRO_VERTEX v[obstacle.vertex.size()];
  for(int i = 0; i < obstacle.vertex.size(); i++) {
    ALLEGRO_VERTEX tmp = {obstacle.vertex[i].first, obstacle.vertex[i].second, 0, 0, 0, al_map_rgb(0,0,0)};
    v[i] = tmp;
  }
  al_draw_prim(v, NULL, 0, 0, obstacle.vertex.size(), ALLEGRO_PRIM_LINE_LOOP);
};
示例#11
0
文件: al3d.c 项目: bjorndm/ekq
void draw_textured_colored_rectangle_3d(
  float x, float y, float z, float w, float h, float d, float up, float vp,
  ALLEGRO_BITMAP * texture,  ALLEGRO_COLOR color) {
    float u, v;
    float tw, th;
    ALLEGRO_VERTEX p[4];
    if (texture) {
      tw = al_get_bitmap_width(texture);
      th = al_get_bitmap_height(texture);
      u  = tw * up;
      v  = th * vp;
    } else {
      u = v = 0.0f;
    }
    
    init_vertex_color(p+0, x    , y     ,  z    ,   0,   0, color);
    init_vertex_color(p+1, x    , y + h ,  z + d,   0,   v, color);
    init_vertex_color(p+2, x + w, y     ,  z + d,   u,   0, color);
    init_vertex_color(p+3, x + w, y + h ,  z + d,   u,   v, color);
    al_draw_prim(p, NULL, texture, 0, 4, ALLEGRO_PRIM_TRIANGLE_STRIP);
}
示例#12
0
static void CustomVertexFormatPrimitives(int mode)
{
   static CUSTOM_VERTEX vtx[4];
   static ALLEGRO_VERTEX_DECL* decl;
   if (mode == INIT) {
      int ii = 0;
      ALLEGRO_VERTEX_ELEMENT elems[] = {
         {ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_SHORT_2, offsetof(CUSTOM_VERTEX, x)},
         {ALLEGRO_PRIM_TEX_COORD_PIXEL, ALLEGRO_PRIM_SHORT_2, offsetof(CUSTOM_VERTEX, u)},
         {ALLEGRO_PRIM_COLOR_ATTR, 0, offsetof(CUSTOM_VERTEX, color)},
         {0, 0, 0}
      };
      decl = al_create_vertex_decl(elems, sizeof(CUSTOM_VERTEX));

      for (ii = 0; ii < 4; ii++) {
         float x, y;
         x = 200 * cosf((float)ii / 4.0f * 2 * ALLEGRO_PI);
         y = 200 * sinf((float)ii / 4.0f * 2 * ALLEGRO_PI);
         
         vtx[ii].x = x; vtx[ii].y = y;
         vtx[ii].u = 64 * x / 100; vtx[ii].v = 64 * y / 100;
         vtx[ii].color = al_map_rgba_f(1, 1, 1, 1);
      }
   } else if (mode == LOGIC) {
      Theta += Speed;
      al_build_transform(&MainTrans, ScreenW / 2, ScreenH / 2, 1, 1, Theta);
   } else if (mode == DRAW) {
      if (Blend)
         al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);
      else
         al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
      
      al_use_transform(&MainTrans);
      
      al_draw_prim(vtx, decl, Texture, 0, 4, ALLEGRO_PRIM_TRIANGLE_FAN);
      
      al_use_transform(&Identity);
   }
}
示例#13
0
/* emulate polygon() for convex polygons only */
void polygon(int vertices, const int *points, ALLEGRO_COLOR color)
{
   ALLEGRO_VERTEX vtxs[MAX_POLYGON_VERTICES + 2];
   ALLEGRO_PRIM_COLOR pc;
   int i;

   assert(vertices <= MAX_POLYGON_VERTICES);

   pc = al_get_prim_color(color);

   vtxs[0].x = 0.0;
   vtxs[0].y = 0.0;
   vtxs[0].z = 0.0;
   vtxs[0].color = pc;
   vtxs[0].u = 0;
   vtxs[0].v = 0;

   for (i = 0; i < vertices; i++) {
      vtxs[0].x += points[i*2];
      vtxs[0].y += points[i*2 + 1];
   }

   vtxs[0].x /= vertices;
   vtxs[0].y /= vertices;

   for (i = 1; i <= vertices; i++) {
      vtxs[i].x = points[0];
      vtxs[i].y = points[1];
      vtxs[i].z = 0.0;
      vtxs[i].color = pc;
      vtxs[i].u = 0;
      vtxs[i].v = 0;
      points += 2;
   }

   vtxs[vertices + 1] = vtxs[1];

   al_draw_prim(vtxs, NULL, NULL, 0, vertices + 2, ALLEGRO_PRIM_TRIANGLE_FAN);
}
示例#14
0
static duk_ret_t
js_GradientRectangle(duk_context* ctx)
{
	int x1 = duk_require_int(ctx, 0);
	int y1 = duk_require_int(ctx, 1);
	int x2 = x1 + duk_require_int(ctx, 2);
	int y2 = y1 + duk_require_int(ctx, 3);
	color_t color_ul = duk_require_sphere_color(ctx, 4);
	color_t color_ur = duk_require_sphere_color(ctx, 5);
	color_t color_lr = duk_require_sphere_color(ctx, 6);
	color_t color_ll = duk_require_sphere_color(ctx, 7);

	if (!is_skipped_frame()) {
		ALLEGRO_VERTEX verts[] = {
			{ x1, y1, 0, 0, 0, nativecolor(color_ul) },
			{ x2, y1, 0, 0, 0, nativecolor(color_ur) },
			{ x1, y2, 0, 0, 0, nativecolor(color_ll) },
			{ x2, y2, 0, 0, 0, nativecolor(color_lr) }
		};
		al_draw_prim(verts, NULL, NULL, 0, 4, ALLEGRO_PRIM_TRIANGLE_STRIP);
	}
	return 0;
}
示例#15
0
// Render function.
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data)
{
    // Backup Allegro state that will be modified
    ALLEGRO_TRANSFORM last_transform = *al_get_current_transform();
    ALLEGRO_TRANSFORM last_projection_transform = *al_get_current_projection_transform();
    int last_clip_x, last_clip_y, last_clip_w, last_clip_h;
    al_get_clipping_rectangle(&last_clip_x, &last_clip_y, &last_clip_w, &last_clip_h);
    int last_blender_op, last_blender_src, last_blender_dst;
    al_get_blender(&last_blender_op, &last_blender_src, &last_blender_dst);

    // Setup render state
    al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);

    // Setup orthographic projection matrix
    // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
    {
        float L = draw_data->DisplayPos.x;
        float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
        float T = draw_data->DisplayPos.y;
        float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
        ALLEGRO_TRANSFORM transform;
        al_identity_transform(&transform);
        al_use_transform(&transform);
        al_orthographic_transform(&transform, L, T, 1.0f, R, B, -1.0f);
        al_use_projection_transform(&transform);
    }

    for (int n = 0; n < draw_data->CmdListsCount; n++)
    {
        const ImDrawList* cmd_list = draw_data->CmdLists[n];

        // Allegro's implementation of al_draw_indexed_prim() for DX9 is completely broken. Unindex our buffers ourselves.
        // FIXME-OPT: Unfortunately Allegro doesn't support 32-bits packed colors so we have to convert them to 4 float as well..
        static ImVector<ImDrawVertAllegro> vertices;
        vertices.resize(cmd_list->IdxBuffer.Size);
        for (int i = 0; i < cmd_list->IdxBuffer.Size; i++)
        {
            const ImDrawVert* src_v = &cmd_list->VtxBuffer[cmd_list->IdxBuffer[i]];
            ImDrawVertAllegro* dst_v = &vertices[i];
            dst_v->pos = src_v->pos;
            dst_v->uv = src_v->uv;
            unsigned char* c = (unsigned char*)&src_v->col;
            dst_v->col = al_map_rgba(c[0], c[1], c[2], c[3]);
        }

        const int* indices = NULL;
        if (sizeof(ImDrawIdx) == 2)
        {
            // FIXME-OPT: Unfortunately Allegro doesn't support 16-bit indices.. You can '#define ImDrawIdx int' in imconfig.h to request Dear ImGui to output 32-bit indices.
            // Otherwise, we convert them from 16-bit to 32-bit at runtime here, which works perfectly but is a little wasteful.
            static ImVector<int> indices_converted;
            indices_converted.resize(cmd_list->IdxBuffer.Size);
            for (int i = 0; i < cmd_list->IdxBuffer.Size; ++i)
                indices_converted[i] = (int)cmd_list->IdxBuffer.Data[i];
            indices = indices_converted.Data;
        }
        else if (sizeof(ImDrawIdx) == 4)
        {
            indices = (const int*)cmd_list->IdxBuffer.Data;
        }

        // Render command lists
        int idx_offset = 0;
        ImVec2 clip_off = draw_data->DisplayPos;
        for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
        {
            const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
            if (pcmd->UserCallback)
            {
                pcmd->UserCallback(cmd_list, pcmd);
            }
            else
            {
                ALLEGRO_BITMAP* texture = (ALLEGRO_BITMAP*)pcmd->TextureId;
                al_set_clipping_rectangle(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y, pcmd->ClipRect.z - pcmd->ClipRect.x, pcmd->ClipRect.w - pcmd->ClipRect.y);
                al_draw_prim(&vertices[0], g_VertexDecl, texture, idx_offset, idx_offset + pcmd->ElemCount, ALLEGRO_PRIM_TRIANGLE_LIST);
            }
            idx_offset += pcmd->ElemCount;
        }
    }

    // Restore modified Allegro state
    al_set_blender(last_blender_op, last_blender_src, last_blender_dst);
    al_set_clipping_rectangle(last_clip_x, last_clip_y, last_clip_w, last_clip_h);
    al_use_transform(&last_transform);
    al_use_projection_transform(&last_projection_transform);
}
示例#16
0
文件: al3d.c 项目: bjorndm/ekq
void stamod_draw(struct Stamod * me) {     
  int stop = me->tsize * 3;
  al_draw_prim(me->triangles, NULL, me->texture, 0, stop, ALLEGRO_PRIM_TRIANGLE_LIST);
}
示例#17
0
文件: al3d.c 项目: bjorndm/ekq
int main(void) {
  ALLEGRO_VERTEX        v[8];
  ALLEGRO_DISPLAY     * display;
  ALLEGRO_TRANSFORM     perst;
  ALLEGRO_TRANSFORM     camt;
  ALLEGRO_BITMAP      * texture, * texture2;
  ALLEGRO_EVENT_QUEUE * queue;
  ALLEGRO_EVENT         event;
  ALLEGRO_FONT        * font;
  int                   busy  = 1;
  float                 cx    = 0;
  float                 cy    = -2; // 128;
  float                 cz    = 0;
  int                   face  = 0; 
  int                   hori  = 0;
  float                 angle = 0;
  float                 theta = 0;  
  float                 near  = 2;
  float                 far   = 8192;
  float                 zoom  = 1;
  float                 scale = 1.0;
  float                 us    = 20; 
  float                 ratio = 480.0 / 640.0;
  al_init();  
  al_init_image_addon();
  al_init_font_addon();
  al_install_keyboard();
  font  = al_create_builtin_font();
  
  queue = al_create_event_queue();  
  al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 16, ALLEGRO_SUGGEST);
  
  display  = al_create_display(640, 480);
  al_register_event_source(queue, al_get_keyboard_event_source());  
  texture  = al_load_bitmap("tile_1.png");
  texture2 = al_load_bitmap("tile_2.png");
  
  /* Allegro coordinates: +Y is down, +X is to the right, 
   * and +Z comes "out of" the screen.
   * 
   */
  
  
  while (busy) {
    init_vertex(v+0,   0,   0,   0,   0,   0, 1, 0, 0, 1);
    init_vertex(v+1,   0,   0,  us,   0, 256, 0, 1, 0, 1);
    init_vertex(v+2,  us,   0,  us, 256, 256, 0, 0, 1, 1);
    init_vertex(v+3,  us,   0,   0, 256,   0, 1, 1, 0, 1);
    init_vertex(v+4,  us, -us,   0, 256, 256, 1, 0, 1, 1);
    init_vertex(v+5,   0, -us,   0, 256,   0, 0, 1, 1, 1); 
    init_vertex(v+6,   0, -us,  us, 256, 256, 0, 0, 0, 1); 
    init_vertex(v+7,   0,   0,  us,   0, 256, 1, 1, 1, 1); 
  
  
    al_identity_transform(&camt);
    al_scale_transform_3d(&camt, scale, scale, scale);
    al_translate_transform_3d(&camt, cx, cy, cz);
  
    angle = face * 0.125 * ALLEGRO_PI;
    al_rotate_transform_3d(&camt, 0, -1, 0, angle); 
    theta = hori * 0.125 * ALLEGRO_PI;
    al_rotate_transform_3d(&camt, -1, 0, 0, theta); 
    
    al_use_transform(&camt);
    // al_set_projection_transform(display, &perst);

  
    al_clear_to_color(al_map_rgb_f(0.75, 0.75, 0.95));
    al_set_render_state(ALLEGRO_DEPTH_TEST, 1);
    al_clear_depth_buffer(far);

    
    
    al_identity_transform(&perst);
    al_perspective_transform(&perst, -1, ratio, near, 
                                      1,  -ratio, far);
    al_use_projection_transform(&perst);

    al_draw_filled_rectangle(0, 0, 4, 5, al_map_rgb_f(0, 0.25, 0.25));
    
        
    al_draw_prim(v, NULL, texture, 0, 8, ALLEGRO_PRIM_TRIANGLE_FAN);
    
    draw_textured_colored_rectangle_3d(0 , -us, 0, us, us, 0, 
                                       1.0, 1.0, texture2, al_map_rgb_f(1,1,1));
    
    draw_textured_colored_rectangle_3d(0 , -us, 0, 0, us, us, 
                                       1.0, 1.0, texture2, al_map_rgb_f(1,1,1));

    draw_textured_colored_rectangle_3d(0 , 0, 0, us, 0, us,
                                       1.0, 1.0, texture2, al_map_rgb_f(1,1,1));
     
    
    al_identity_transform(&perst);
    al_orthographic_transform(&perst, 0, 0, -1.0, 640, 480, 1.0);
 
    al_identity_transform(&camt);
    al_use_projection_transform(&perst);
    al_use_transform(&camt);
    al_draw_filled_rectangle(111, 22, 133, 44, al_map_rgb_f(0.25, 0.25, 0));

    
    al_draw_multiline_textf(font, al_map_rgb_f(1,1,1), 10, 10, 620, 0, 0,
      "Coords: (%f %f %f)\nAngle: (%f %f)\nView: [%f %f %f %f]\nScale: %f", 
               cx, cy, cz,        angle, theta,   near, far, zoom, scale); 
    
    
    
    al_flip_display();  
    al_wait_for_event(queue, &event);
    
    if (event.type == ALLEGRO_EVENT_KEY_DOWN) { 
      switch (event.keyboard.keycode) {
        case ALLEGRO_KEY_RIGHT:
          cx += 8;
          break;

        case ALLEGRO_KEY_LEFT:
          cx -= 8;
          break;
          
        case ALLEGRO_KEY_UP:
          cy += 8;
          break;
          
        case ALLEGRO_KEY_DOWN:
          cy -= 8;
          break;  
        
        case ALLEGRO_KEY_HOME:
          cz += 8;
          break;
          
        case ALLEGRO_KEY_END:
          cz -= 8;
          break;  
          
        case ALLEGRO_KEY_R:
          face++;
          break;
      
        case ALLEGRO_KEY_L:
          face--;
          break;
          
        case ALLEGRO_KEY_H:
          hori++;
          break;
      
        case ALLEGRO_KEY_G:
          hori--;
          break;
  
          
        case ALLEGRO_KEY_N:
          near *= 2.0;
          break;
          
        case ALLEGRO_KEY_M:
          near /= 2.0;
          break;

        case ALLEGRO_KEY_F:
          far += 64;
          break;
          
        case ALLEGRO_KEY_V:
          far -= 64;
          break;
        
        case ALLEGRO_KEY_Z:
          zoom *= 2.0f;
          break;
          
        case ALLEGRO_KEY_S:
          zoom /= 2.0f;
          break;
        
        case ALLEGRO_KEY_A:
          scale *= 2.0f;
          break;
          
        case ALLEGRO_KEY_Q:
          scale /= 2.0f;
          break;
          
        case ALLEGRO_KEY_ESCAPE:
          busy = 0 ;
          break;
  
        default:
          break;
      }
    }
  }
  
  al_destroy_bitmap(texture);
  al_destroy_bitmap(texture2);
  
  
  return 0;  
}
示例#18
0
void m_draw_prim (const void* vtxs, const ALLEGRO_VERTEX_DECL* decl, MBITMAP* texture, int start, int end, int type)
{
	if (type == ALLEGRO_PRIM_POINT_LIST) {
		int n = end-start;
		ALLEGRO_VERTEX *v = new ALLEGRO_VERTEX[n*6];
		ALLEGRO_VERTEX *verts = (ALLEGRO_VERTEX *)vtxs;
		for (int i = 0; i < n; i++) {
			v[i*6+0].x = verts[i+start].x;
			v[i*6+0].y = verts[i+start].y;
			v[i*6+0].z = 0;
			v[i*6+0].color = verts[i+start].color;
			v[i*6+1].x = verts[i+start].x+1;
			v[i*6+1].y = verts[i+start].y;
			v[i*6+1].z = 0;
			v[i*6+1].color = verts[i+start].color;
			v[i*6+2].x = verts[i+start].x+1;
			v[i*6+2].y = verts[i+start].y+1;
			v[i*6+2].z = 0;
			v[i*6+2].color = verts[i+start].color;
			v[i*6+3].x = verts[i+start].x;
			v[i*6+3].y = verts[i+start].y+1;
			v[i*6+3].z = 0;
			v[i*6+3].color = verts[i+start].color;
			v[i*6+4].x = verts[i+start].x;
			v[i*6+4].y = verts[i+start].y;
			v[i*6+4].z = 0;
			v[i*6+4].color = verts[i+start].color;
			v[i*6+5].x = verts[i+start].x+1;
			v[i*6+5].y = verts[i+start].y+1;
			v[i*6+5].z = 0;
			v[i*6+5].color = verts[i+start].color;
		}
		al_draw_prim(v, decl, (texture ? texture->bitmap : NULL), start, n*6, ALLEGRO_PRIM_TRIANGLE_LIST);
		delete[] v;
	}
	else if (type == ALLEGRO_PRIM_LINE_LIST) {
		int n = (end-start)/2;
		ALLEGRO_VERTEX *v = new ALLEGRO_VERTEX[n*6];
		ALLEGRO_VERTEX *verts = (ALLEGRO_VERTEX *)vtxs;
		for (int i = 0; i < n; i++) {
			float x1 = verts[(i*2+0)+start].x;
			float y1 = verts[(i*2+0)+start].y;
			float x2 = verts[(i*2+1)+start].x;
			float y2 = verts[(i*2+1)+start].y;
			float dx = x2 - x1;
			float dy = y2 - y1;
			float a = atan2(dy, dx);
			float a1 = a + M_PI/2;
			float a2 = a - M_PI/2;
			float newx1 = x1 + cos(a1)/2;
			float newy1 = y1 + sin(a1)/2;
			float newx2 = x1 + cos(a2)/2;
			float newy2 = y1 + sin(a2)/2;
			float newx3 = x2 + cos(a1)/2;
			float newy3 = y2 + sin(a1)/2;
			float newx4 = x2 + cos(a2)/2;
			float newy4 = y2 + sin(a2)/2;
			float u1 = verts[(i*2+0)+start].u;
			float v1 = verts[(i*2+0)+start].v;
			float u2 = verts[(i*2+1)+start].u;
			float v2 = verts[(i*2+1)+start].v;
			v[i*6+0].x = newx1;
			v[i*6+0].y = newy1;
			v[i*6+0].z = 0;
			v[i*6+0].u = u1;
			v[i*6+0].v = v1;
			v[i*6+0].color = verts[(i*2+0)+start].color;
			v[i*6+1].x = newx3;
			v[i*6+1].y = newy3;
			v[i*6+1].z = 0;
			v[i*6+1].color = verts[(i*2+0)+start].color;
			v[i*6+1].u = u2;
			v[i*6+1].v = v2;
			v[i*6+2].x = newx4;
			v[i*6+2].y = newy4;
			v[i*6+2].z = 0;
			v[i*6+2].color = verts[(i*2+0)+start].color;
			v[i*6+2].u = u2;
			v[i*6+2].v = v2;
			v[i*6+3].x = newx1;
			v[i*6+3].y = newy1;
			v[i*6+3].z = 0;
			v[i*6+3].color = verts[(i*2+0)+start].color;
			v[i*6+3].u = u1;
			v[i*6+3].v = v1;
			v[i*6+4].x = newx2;
			v[i*6+4].y = newy2;
			v[i*6+4].z = 0;
			v[i*6+4].color = verts[(i*2+0)+start].color;
			v[i*6+4].u = u1;
			v[i*6+4].v = v1;
			v[i*6+5].x = newx4;
			v[i*6+5].y = newy4;
			v[i*6+5].z = 0;
			v[i*6+5].color = verts[(i*2+0)+start].color;
			v[i*6+5].u = u2;
			v[i*6+5].v = v2;
		}
		al_draw_prim(v, decl, (texture ? texture->bitmap : NULL), start, n*6, ALLEGRO_PRIM_TRIANGLE_LIST);
		delete[] v;
	}
	else
		al_draw_prim(vtxs, decl, (texture ? texture->bitmap : NULL), start, end, type);
}
示例#19
0
void RenderManager::RenderObject(const Shape& shape, bool filled, ALLEGRO_BITMAP* texture) {
    //shape->Render(al_get_backbuffer(_display_context));
    auto& verts = shape.GetVerticies();
    std::vector<ALLEGRO_VERTEX> allegro_verts;
	allegro_verts.reserve(verts.size());
    for(const auto& v : verts) {
        auto p = a2de::Math::ToScreenScale(v.GetPosition());
        auto uv = v.GetUV();
        auto c = v.GetColor();
        allegro_verts.push_back(
                        std::move(ALLEGRO_VERTEX{
                            static_cast<float>(p.GetX()),
                            static_cast<float>(p.GetY()),
                            static_cast<float>(p.GetZ()),
                            static_cast<float>(uv.GetX()),
                            static_cast<float>(uv.GetY()),
                            c
                        }));
    }
    switch(shape.GetShapeType()) {
        case Shape::ShapeType::Point: {
            al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_POINT_LIST);
            break;
        } case Shape::ShapeType::Line: {
            al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_LINE_LIST);
            break;
        } case Shape::ShapeType::Rectangle: {
            filled ?
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_TRIANGLE_LIST)
                :
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_LINE_LOOP);
            break;
        } case Shape::ShapeType::Circle: {
            filled ?
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_TRIANGLE_FAN)
                :
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_POINT_LIST);
            break;
        } case Shape::ShapeType::Ellipse: {
            filled ?
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_TRIANGLE_FAN)
                :
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_POINT_LIST);
            break;
        } case Shape::ShapeType::Triangle: {
            filled ?
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_TRIANGLE_LIST)
                :
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_LINE_LOOP);
            break;
        } case Shape::ShapeType::Arc: {
            al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_LINE_STRIP);
            break;
        } case Shape::ShapeType::Polygon: {
            filled ?
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_TRIANGLE_FAN)
                :
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_LINE_LOOP);
            break;
        } case Shape::ShapeType::Spline: {
            al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_LINE_STRIP);
            break;
        } case Shape::ShapeType::Sector: {
            filled ?
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_TRIANGLE_FAN)
                :
                al_draw_prim(allegro_verts.data(), nullptr, texture, 0, allegro_verts.size(), ALLEGRO_PRIM_LINE_LOOP);
            break;
        } default: {
            /* DO NOTHING: All cases handled */;
        }
    }
}