Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » fastest way to plot pixels A5

Credits go to Trent Gamblin for helping out!
This thread is locked; no one can reply to it. rss feed Print
fastest way to plot pixels A5
verthex
Member #11,340
September 2009
avatar

I'm wondering how to go about increasing the speed of my program. The question is what is the fastest way to draw pixels across the entire screen in one loop?

Kazzmir told me to lock the bitmap like so, but is this correct or am I supposed to draw onto a bitmap instead?

al_lock_bitmap(al_get_target_bitmap(), ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READWRITE);              

  for(int y = 0; y < SCREEN_HEIGHT_Y; y++)
  {
    for(int x = 0; x < SCREEN_WIDTH_X; x++)
    {
      al_draw_pixel(x,y, COLOR);        
    }
  }
al_unlock_bitmap(al_get_target_bitmap());

Trent Gamblin
Member #261
April 2000
avatar

You aren't allowed to use al_draw_pixel on a locked bitmap AFAIK (maybe that has changed though.) You should use al_put_pixel. Experiment with al_put_pixel+locking and ALLEGRO_VERTEXs+al_draw_prim+ALLEGRO_POINT_LIST.

verthex
Member #11,340
September 2009
avatar

ALLEGRO_VERTEXs+al_draw_prim+ALLEGRO_POINT_LIST

I'm not sure what you mean? Can you give an example?

Trent Gamblin
Member #261
April 2000
avatar

Sure.

ALLEGRO_VERTEX my_100_pixels[100];
for (int i = 0; i < 100; i++) {
   // initialize pixels with random x, y
   my_100_pixels[i].x = rand() % SCR_W;
   my_100_pixels[i].y = rand() % SCR_H;
   my_100_pixels[i].z = 0;
   // set the pixels color
   my_100_pixels[i].color = al_map_rgb_f(1, 0, 0); // red
}
al_draw_prim(my_100_pixels, NULL, NULL, 0, 100, ALLEGRO_PRIM_POINT_LIST);

Obviously you don't have to do random pixels, you can set their x/y/color to whatever you want, but you probably want z to be 0 if you're doing 2d.

verthex
Member #11,340
September 2009
avatar

Obviously you don't have to do random pixels, you can set their x/y/color to whatever you want, but you probably want z to be 0 if you're doing 2d.

Does this mean I can create a 3d plot. If so, how would I move the camera or rotate the world? Is it rendered using opengl or directx?

Trent Gamblin
Member #261
April 2000
avatar

Yeah, you can do 3d, but you have to do transformations yourself as Allegro only supports 2d transformations. You can still use the al_compose_transform, al_use_transform, al_set_projection_transform with 3d transformations but al_rotate_transform etc are only 2d. Allegro 5 always renders with OpenGL or D3D. Default is D3D on Windows and OpenGL everywhere else but you can specify with al_set_new_display_flag(ALLEGRO_OPENGL/ALLEGRO_DIRECT3D).

verthex
Member #11,340
September 2009
avatar

Basically I would need to create my own camera with culling methods and what not? What (practical) use of 3d does allegro 5 have so far then?

Trent Gamblin
Member #261
April 2000
avatar

No, you don't need to create any of that. That's all built into OpenGL and D3D. Allegro is a 2D lib but it allows you to use OpenGL or D3D directly for any 3D stuff you want to do, and you can mix 2D Allegro drawing with your own 3D drawing.

verthex
Member #11,340
September 2009
avatar

So I would use openGL methods in the allegro code?

Trent Gamblin
Member #261
April 2000
avatar

You'd use OpenGL calls in your own code, intermixed with Allegro calls.

verthex
Member #11,340
September 2009
avatar

I'm trying to create a class where the ALLEGRO_VERTEX is a variable size at class construction but it doesn't compile, any ideas if this is possible?

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3 4class pixel_class 5{ 6 private: 7 8 int SCREEN_HEIGHT_Y; 9 int SCREEN_WIDTH_X; 10 ALLEGRO_VERTEX pixels[SCREEN_HEIGHT_Y*SCREEN_WIDTH_X]; 11 12 public: 13 14 pixel_class(){} 15 ~pixel_class(){} 16 pixel_class(int sw, int sh) 17 { 18 SCREEN_HEIGHT_Y = sh; 19 SCREEN_WIDTH_X = sw; 20 } 21};

C:\pixel_class.h|11|error: invalid use of non-static data member 'pixel_class::SCREEN_HEIGHT_Y'|
C:\pixel_class.h|13|error: from this location|
C:\pixel_class.h|12|error: invalid use of non-static data member 'pixel_class::SCREEN_WIDTH_X'|
C:\pixel_class.h|13|error: from this location|
C:\pixel_class.h|13|error: array bound is not an integer constant|
||=== Build finished: 5 errors, 0 warnings ===|

Trent Gamblin
Member #261
April 2000
avatar

Make it a pointer and new/delete it.

J-Gamer
Member #12,491
January 2011
avatar

You'd use OpenGL calls in your own code, intermixed with Allegro calls.

In a previous thread(I can't find it anymore), someone told me that you can't mix Allegro calls with OpenGL calls because the former mess up the OpenGL state. I tried drawing text with Allegro after rendering a scene with OpenGL and I got weird results...
Could you give some example code where OpenGL is mixed with allegro drawing?

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

Trent Gamblin
Member #261
April 2000
avatar

As long as you're aware of the state changes, you can do it. I have several 3d mini games that draw in 3d with 2d overlays drawn with Allegro. Actually, I draw some of the 3d parts with al_draw_prim too.

Mark Oates
Member #1,146
March 2001
avatar

Just curious, would it be faster to use a shader than al_put_pixel?

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Arthur Kalliokoski
Second in Command
February 2005
avatar

Just curious, would it be faster to use a shader than al_put_pixel?

Alex the Alligator frowns on that.

They all watch too much MSNBC... they get ideas.

verthex
Member #11,340
September 2009
avatar

Just curious, would it be faster to use a shader than al_put_pixel?

I could only imagine a shader would be made up of the functions that render a pixel anyway.

Mark Oates
Member #1,146
March 2001
avatar

I guess I'm just curious if a shader has some extra magic that you wouldn't otherwise get. Point list is a cool idea, though.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Trent Gamblin
Member #261
April 2000
avatar

Depends. If your data is stored in a texture, sure. But uploading 1000 pixels to a shader ever frame isn't going to be faster (and I'm not even sure you can do it.)

Go to: