Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » starfield - direct acces to video memory

This thread is locked; no one can reply to it. rss feed Print
starfield - direct acces to video memory
mscava
Member #6,815
January 2006
avatar

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
Member #1,632
November 2001
avatar

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

A J
Member #3,025
December 2002
avatar

your method will be slow, and prone to crash

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

___________________________
The more you talk, the more AJ is right. - ML

Andrei Ellman
Member #3,434
April 2003

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.

--
Don't let the illegitimates turn you into carbon.

Elverion
Member #6,239
September 2005
avatar

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.

--
SolarStrike Software - MicroMacro home - Automation software.

Kitty Cat
Member #2,815
October 2002
avatar

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.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

mscava
Member #6,815
January 2006
avatar

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
Member #3,025
December 2002
avatar

do you know how to use a debugger ?

___________________________
The more you talk, the more AJ is right. - ML

Kitty Cat
Member #2,815
October 2002
avatar

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);

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

mscava
Member #6,815
January 2006
avatar

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

Go to: