Hi Raster Rex,
First of all, don't forget to clean the screen every frame, removing all the previously drawn stars.
I'm not sure if you want to do a 3d or 2d starfield.
I'd suggest starting with a 2d star field first!
what you should do is basically make an array of 'stars', and initialize these withrandom values. then every frame update the entire array and draw them to the screen.
so your 'Start Struct' should basically look like this (for a side star scroller):
- Code: Select all
1 2 3 4 5 6 7 8 9
| #define NROFSTARS (256) struct Star { int x, int y, int spdx };
Star StarTab[NROFSTARS];
|
| 9 lines; 5 keywds; 1 nums; 10 ops; 0 strs; 0 coms Syntactic Coloring v0.4 - Dan East |
with an initialize function like this:
- Code: Select all
1 2 3 4 5 6 7 8 9 10
| void InitStars(void) { int i; for (i=0; i<NROFSTARS; i++) { StarTab[i].x = rand()&255; StarTab[i].y = rand()%320; StarTab[i].spdx = (rand()&7)+1; } };
|
| 10 lines; 4 keywds; 5 nums; 43 ops; 0 strs; 0 coms Syntactic Coloring v0.4 - Dan East |
then every frame you should have a function which resembles this:
- Code: Select all
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| void DrawStars(void) { int i; for (i=0; i<NROFSTARS; i++) { StarTab[i].x += StarTab[i].spdx; StarTab[i].x &= 255;
if(StarTab[i].x<240) //clipping { DrawPixel(x,y); } } }
|
| 14 lines; 5 keywds; 3 nums; 42 ops; 0 strs; 1 coms Syntactic Coloring v0.4 - Dan East |
hope this helps
R
p.s. further improvements might be using fixed point for the x position and its speed... resulting in more speeds then these 8.