• Advertisement
Sign in to follow this  

Background Question

This topic is 4675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Draw one layer, then after you're finished draw another on top of it. It's not rocket science ;)

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
You must register, after that there's a New thread button. Read this forum's FAQ I think it says all there

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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... ;)

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
I would like a map as background.

So I need to:

1) Generate my Background as bmp {1st layer}
2) Load this layer
3) Scroll this layer {left edge will scroll left...etc}

hope this helps clarify.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement