Displaying Pixels in C

slinga

Established Member
Hey guys,

What's the easiest way to modify individual pixels in C? I'm working on a program to simulate a random monomer chain and it's movement through pores. The grid is 200x600. I obviously can't use regular print statements as the screen is much smaller than that. I want to be able to create an array of pixels and print them to the screen. What's the simplest method? As this is a school project I can't get too fancy...
 
In that respect I would recommend a graphics library, depending on the OS I would recommend Allegro or SDL.
 
Thanks Dibz, I got allegro mostly working. I went through some basic tutorials and I got bitmaps loading. Now all I have to do is figure out how to modify bitmaps on the fly in memory and I'm done. Thanks a lot.
 
Originally posted by slinga@Tue, 2005-12-13 @ 01:04 AM

Thanks Dibz, I got allegro mostly working. I went through some basic tutorials and I got bitmaps loading. Now all I have to do is figure out how to modify bitmaps on the fly in memory and I'm done. Thanks a lot.

[post=142472]Quoted post[/post]​


You can modify them using the graphics functions: putpixel, getpixel, rectfill, line, etc.

For example:

Code:
BITMAP *bmp = NULL;

PALETTE pal;

allegro_init();

set_color_depth(8);

set_graphics_mode(GFX_VGA, 320, 200, 0, 0);

// Clear palette

memset(pal, 0, sizeof(pal));

// Make colors

pal[0].r = pal[0].g = pal[0].b = 0x00; // Color #0 is black

pal[1].r = pal[1].g = pal[1].b = 0xFF; // Color #1 is white

// Assign palette to display device

set_palette(pal);

// Create bitmap in memory

bmp = create_bitmap(320, 200);

// Clear to color 0

clear_to_color(bmp, 0); 

// Put a white dot at (0,0)

putpixel(bmp, 0, 0, 1);

// Draw a white line from (10,10)-(20,20)

line(bmp, 10, 10, 20, 20, 1);

// Copy bitmap to display device

blit(bmp, screen, 0, 0, 0, 0, 320, 200);

// Save results for fun

save_pcx("test.pcx", bmp, pal);

You can also directly access the bitmap memory like this:

Code:
void my_putpixel(BITMAP *bmp, int x, int y, int color)

{

  bmp->line[y][x] = color;

}

Good for custom drawing routines where the overhead of a zillion putpixel() calls would affect performance. Just don't touch the 'screen' bitmap that way. :)
 
Thanks cgfm,

I was doing the direct bitmap manipulation method as you showed. The only trick for me was figuring out it's line[y][x] and not line[x][y].
 
Back
Top