Draw Pixel

I know that there are lots of ways you can draw a single pixel, though none of them seem to be fast. I'm trying to make an electronics simulator. (I want it to be scroll-able by the way if it changes anything). SFML has a LoadFromPixels() function though it apparently is also a little slow.

Really my question is, what would be the fastest way of doing what I'm doing. I want each dot on my program's grid to be the same as the ones on the screen. A bit like powder toy (www.powdertoy.co.uk).

Thanks for any help.
After doing some research, I think OpenGL is the fastest...I'd still some tips though :D
Last edited on
OpenGL would be a good idea if you need this to be rendered in realtime, however it's pretty low-level and not very easy to learn (in my opinion anyway). I think you might find that you could be more effective with something like SDL if you haven't done much graphics programming before. If it doesnt need to be in realtime, there are lots of libs for generating all sorts of image files, and google would be your friend for that:)
Why do you want to draw a single pixel? Manually drawing pixels is usually always slow.
Any library you load single pixels will be relatively slow, as it can't use any optimizations for rendering that it would otherwise have the chance to. However if you're stuck on the idea- here's how you do it in SDL. (also bonus material : how to draw a line)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
bool putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel); //applies pixel to surface
void putline(SDL_Surface* surface,int x0, int y0, int x1, int y1,Uint32 pixel); //basic interpolation

bool putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel){
 if (y<0 || x < 0 || x > surface->w || y > surface->h)
 return false;

 int bpp = surface->format->BytesPerPixel;
 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

 if (bpp == 1){
   *p = pixel;
 }
 else if (bpp == 2){
   *(Uint16 *)p = pixel;
 }
 else if (bpp == 3){

   if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
   {
    p[0] = (pixel >> 16) & 0xff;
    p[1] = (pixel >> 8) & 0xff;
    p[2] = pixel & 0xff;
   } else {
    p[0] = pixel & 0xff;
    p[1] = (pixel >> 8) & 0xff;
    p[2] = (pixel >> 16) & 0xff;
   }
 }
 else if (bpp == 4){
   *(Uint32 *)p = pixel;
 }
return true;
}


void putline( SDL_Surface* surface,int x0, int y0, int x1, int y1, Uint32 pixel){
   int dx = abs(x1-x0);
   int dy = abs(y1-y0);
   int sx=0,sy=0;

   if (x0 < x1)
    sx = 1;
   else
    sx = -1;

   if (y0 < y1)
   sy = 1;
   else
   sy = -1;

   int err = dx-dy;
   int e2;

   while(x0!=x1 || y0!=y1){
       putpixel(surface,x0,y0,pixel);
       e2=2*err;
       if (e2>-dy){
         err -= dy;
         x0 += sx;
       }

       if (e2 < dx){
         err += dx;
         y0 += sy;
       }
   }

}



not very easy to learn (in my opinion anyway)


In this case in particular, using immediate mode, OpenGL would be cake to learn. But I agree with you in general it's a pain.

Depending on your setup with OpenGL here's how you'd draw a bunch of points on the screen.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();



      glBegin(GL_POINTS);
      glColor3f(.5,.5,.5);
      for (int x = -20; x < 40; x++)
      for (int y = -20; y < 40; y++)
      glVertex3f(x, y, -10);

      glEnd();




      SDL_GL_SwapBuffers();//If you're using SDL with OpenGL 

Last edited on
Ok, thanks for all the help. I have a OpenGL thing working though one thing I wouldn't mind some help with is the fact that the pixels aren't everywhere and not always changing. I would rather not draw all the empty spaces every time. The first thing that came to my mind was display lists though they are non-editable. Does anyone have any suggestions, or do I just have to go through the drawing loop thousands of times.
Again - why do you want to draw single pixels? What for?

And display lists are deprecated (or removed, depending on the OpenGL version you're using). Use VBOs/Vertex Arrays.
I want each piece, whether it be a transistor, or a battery, or some kind of wire, to only be one pixel big. If you still don't know what I mean, I want it to be kind of like powder toy's interface. (www.powdertoy.co.uk)
I sincerely doubt they manually draw every single pixel in powder toy.
And display lists are deprecated (or removed, depending on the OpenGL version you're using). Use VBOs/Vertex Arrays.


Definitely don't use immediate mode, it's quite slow when compared with VBOs depending on the static nature of your objects.

Vertex Arrays (everything stored in ram, fine for dynamic stuff) : http://www.opengl.org/wiki/Vertex_Arrays

VBO vertex buffer objects (everything stored in video card, best for pretty much everything) : http://www.opengl.org/wiki/Vertex_Buffer_Object

With VBOs just call glBufferData with a null pointer if you're using a ton of dynamic stuff.

Definitely definitely definitely don't use display lists.
Last edited on
Topic archived. No new replies allowed.