Background Question

Started by
9 comments, last by jmschaub 19 years ago
The game I intend on building will have a background that I want to move. This will be a map that when I move my cursor to edges it moves that direction. Any good sample code out there for this? Is there a size limit to map? How do I go about creating this map, which could be huge (real planet earth... scale tbd)?
Advertisement
Check out:

This article

[EDIT]Made the address a link.

[Edited by - ricekrispyw on March 29, 2005 5:48:07 PM]
The best way to predict the future is to invent it.
Quote:Original post by ricekrispyw
Check out:

This article

[EDIT]Made the address a link.


It's close but what if it's not a tile map? What about multiple layers... like background, borders, stuff (like city pics, trees), layers representing different modes... that kind of thing.
Perhaps you could do the same thing with each layer as the article did with one...
The best way to predict the future is to invent it.
Draw one layer, then after you're finished draw another on top of it. It's not rocket science ;)
pls help me that how to post a new question .
It's my first time to the forum.I wasted serveral hours to find hw to post a new message,but I failed.So I reply your question and hope you to help me.
Thanks very much!
You must register, after that there's a New thread button. Read this forum's FAQ I think it says all there
Hey I'm trying to do something very similar to this article too, but my task is to scroll simulated stars like in a space shooter. Using 2D graphics (allegro), first I generate a bitmap that is twice the height of the screen. Then I save it as an actual .bmp file, then later load that static file as my background.
At the top of my game loop I had a function movestars() that would peel one line off the bottom of the bitmap, shift everything down one pixel, then paste the temp line back on top, thus moving stars. Well, it didn't work very well at all, and it made my program WAY too slow.
To make a long question longer, what is a good way of scrolling stars without slowing up the game too much?
Why do you save the bitmap to a file? Just generate it and keep it in the memory. Btw. what kind of scrolling do you want, horizontal/vertical or rotating?
If you're doing horizontal/vertical you could just generate the starts "on the fly".
1) You have a bitmap named Offscreen
2) Fill the entire Offscreen with stars
To generate stars you could do it like this:
for (int y=0; y<SCREEN_H; y++){  for (int x=0; x<SCREEN_W; x++)  {    if (rand() % 100 <= 1) putpixel(Offscreen,x,y,makecol(255,255,255));  }}

3) blit(Offscreen,screen,0,0,0,0,SCREEN_W,SCREEN_H);
4) Now shift the entire screen to the left (by 5, for example)
for (int y=0; y<SCREEN_H; y++){  for (int x=5; x<SCREEN_W-5; x++)  {     putpixel(Offscreen,x-5,y,getpixel(Offscreen,x,y));  }}

5) Fill the new "gap" with new random generated stars and go to step (3)
Hope this helped... ;)
Here's a complete program written in Allegro:
#include <allegro.h>int main(){   allegro_init();   set_color_depth(16);   set_gfx_mode(GFX_AUTODETECT,640,480,0,0);   install_keyboard();   BITMAP* Offscreen = create_bitmap(SCREEN_W,SCREEN_H);   /* generating initial starmap */   clear_bitmap(Offscreen);   for (int y=0; y<SCREEN_H; y++)   {      for (int x=0; x<SCREEN_W; x++)      {         if (rand() % 1000 <= 1) putpixel(Offscreen,x,y,makecol(255,255,255));      }   }      while (!key[KEY_ESC])   {       blit(Offscreen,screen,0,0,0,0,SCREEN_W,SCREEN_H);       for (int y=0; y<SCREEN_H; y++)       {          /* scroll the starmap */          for (int x=5; x<SCREEN_W; x++)          {            putpixel(Offscreen,x-5,y,getpixel(Offscreen,x,y));          }          /* generate new stars */          for (int x=SCREEN_W-5; x<SCREEN_W; x++)          {             if (rand() % 1000 <= 1)                putpixel(Offscreen,x,y,makecol(255,255,255));  // white             else                putpixel(Offscreen,x,y,makecol(0,0,0));  // black          }       }   }   /* free memory */   destroy_bitmap(Offscreen);   allegro_exit();   return 0;}END_OF_MAIN();

This will however prevent you from placing a space ship on the screen cause it will "scroll" the spaceship too. What you could do is make a bitmap the size of the screen and name it Starmap, generate the stars on that, draw it on the offscreen and then draw the spaceship on the offscreen too. This would be eating too much memory though :(

One another solution you could use is have a small bitmap the size of 50x50, generate the stars on that, and then fill the entire offscreen with that one bitmap. Not sure how it would look though..

This topic is closed to new replies.

Advertisement