starfield - direct acces to video memory
mscava

i'm sorry but another problem occured...
now it's with direct acces to video memory. i want to draw a starfield. here is the code:

int stars_[800][540];

for(short x=0;x<800;x++)
    for(short y=0;y<540;y++)
       stars_[x][y] = makecol16(255,255,random(175,255));

acquire_bitmap(bitmap);
bmp_select(bitmap);
for(short y = 0; y<540; y++)
{
   unsigned long address = bmp_read_line(bitmap,y);
   for (short x=0; x<800; x += sizeof(uint16_t))
       bmp_write16(address+x,(uint16_t)stars_[y]+x);       
}
release_bitmap(bitmap);

and bitmap is video_bitmap. whole program just crashes.

Richard Phipps

Why don't you just use an Allegro bitmap and blit?

A J

your method will be slow, and prone to crash

just use a regular memory bitmap, then blit.
it will be safer, and faster.

Andrei Ellman

If you are accessing your array with stars_[y]+x, you have got the X and Y coordiantes the wrong way round for this to work. Try int stars_[540][800]; and stars_[y][x] = makecol16(255,255,random(175,255));

AE.

Elverion

Yeah...you're method seems as though it would be rather slow. What's with the whole reading and writting lines of the bitmap? The way I would do it is as follows:

create an array/vector/etc. of star objects/structs
each "frame":
clear the double buffer to black
move each star towards the edge of the screen,
if it passes the edge, reset it to center
draw each star to the double buffer using putpixil

You might want to also consider scaling the "stars" based on distance from center of the screen. Or, you can also make them fade in from gray to white as they reach the edges of the screen, too.

Kitty Cat
Quote:

draw each star to the double buffer using putpixil

What he's doing is faster than putpixel (check the exflame example ;)). With memory bitmaps it won't be much worse than accesing line[] directly, and on system/video bitmaps it (is supposed to) remove a lot of redundant code from being executed all the time.

mscava

thanks kitty...what i am trying to do is to write those stars onto video_buffer. with putpixel it is slooow. exflame example is showing how to make it faster. so i tried to do something like that. but my code just seems to crash the program... so any ideas how to rewrite it so it will be fast and working?

and i've tried to exchange x and y but it still crashes...

A J

do you know how to use a debugger ?

Kitty Cat

You need to keep consistant with your x/y usage with the array. In bmp_write16, you were adding x instead of sub-scripting it for the array. And, you were calling bmp_read_line (which is for reading) instead of bmp_write_line (which is for writing).

int stars_[540][800];

for(short y=0;y<540;y++)
    for(short x=0;x<800;x++)
       stars_[y][x] = makecol16(255,255,random(175,255));

acquire_bitmap(bitmap);
bmp_select(bitmap);
for(short y = 0; y<540; y++)
{
   unsigned long address = bmp_write_line(bitmap, y);
   for (short x=0; x<800; x++)
       bmp_write16(address + x*sizeof(uint16_t), (uint16_t)stars_[y][x]);
}
bmp_unwrite_line(bitmap);
release_bitmap(bitmap);

mscava

thank you very much kitty cat... now it works... and i am less confused with all those bmp_read/write methods... thanks again...

Thread #583544. Printed from Allegro.cc