Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Defining Game World and Creating Expansive Background

This thread is locked; no one can reply to it. rss feed Print
Defining Game World and Creating Expansive Background
Andynonomous
Member #12,867
May 2011

Hi all,

Just getting back into programming and C++ for the first time in years. Never used allegro but so far I love it. Learning a lot. So I'm trying to make a game based within a physically modeled solar system. I've got the orbital mechanics down and have been drawing my planets as primitive circles, but I'm ready to graduate to bitmaps I think. I can't find a simple example for loading a bitmap image from a file.

Am i right to assume that I create an ALLEGRO_BITMAP *someBitmap and then say
someBitmap = al_load_bitmap("filename.bmp"). Or something like that? Also, I want to define a world which will be about 12000 by 9000 pixels in dimension, and I want to create a background which will span the whole area.. what's the best way to go about that?

Appreciate any clear and simple advice guys, like I said I'm pretty much brand new at this. Thanks!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Am i right to assume that I create an ALLEGRO_BITMAP *someBitmap and then say
someBitmap = al_load_bitmap("filename.bmp"). Or something like that?

Yes, see the Allegro 5 manual. You'll need to initialize the image addon before you can load bitmaps though. Use al_init_image_addon to do this.

Andynonomous said:

Also, I want to define a world which will be about 12000 by 9000 pixels in dimension, and I want to create a background which will span the whole area.. what's the best way to go about that?

Well, with Allegro 5 you're going to have some trouble doing that. You can either load the whole thing as a memory bitmap, which will be really slow to draw, or you can turn it into a grid of video bitmaps and then draw the appropriate sectors as you need them, which is annoying.

Neil Walker
Member #210
April 2000
avatar

or use a tile map engine...

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Andynonomous
Member #12,867
May 2011

Ok, so I dont want to use another engine as im trying to learn allegro from the bottom up before moving on to other things. I have loading and displaying bitmaps working now so im thinking i'll have to use some method like tiling bitmaps for my background.
So additional question. If I have a bitmap of a starfield which can tile seamlessly, would it just be a matter of checking the tiles near the edges and clipping appropriately? It seems to me that if I do something like that I could fly around indefinitely and just keep tiling my star background to give the illusion of motion? Does that make any sense as an approach?
Once again, any advice is much appreciated!

Audric
Member #907
January 2001

The tiling texture idea is a solid system.
The drawing consists in something like:

for (y=-(camera_y%texture_height); y<screen_height; y+=texture_height)
  for (x=-(camera_x%texture_width); x<screen_width; x+=texture_width)
    // draw bitmap at screen coordinates x,y

camera_x, camera_y is the coordinates (in game world) of the top left pixel shown on screen.
(edit: fixed starting values)

Andynonomous
Member #12,867
May 2011

Thanks guys. Thats a great example, any links or documents that expand on that kind of thing would be great.
The one thing I don't understand in that example is the y=-(camera_y%texture_height)

I see whats going on but I'm not sure why it works. Also I know that y-= means
y = y- but what does the y=- within the for loop mean? Thanks again.. lots to learn!

Audric
Member #907
January 2001

- is simply the "make negative" unary operator :)

foo = -bar;
// Equivalent to
foo = 0-bar;

I'll explain for the X axis, the logic is the same on the Y axis:
Allegro can do clipping automatically, so in order to draw the right side of the bitmap on the left edge of screen, you can simply provide a negative coordinate.

If the texture has a width of 256 pixels for example, the formula that I listed will always give a starting X that is from -255 included to 0 included.

Andynonomous
Member #12,867
May 2011

Thanks! This has been unbelievably helpful. I've got a scrolling star background now. Though its obvious looking at it that I'm only using one tile. This should still work if I have an array of tiles and a map structure to keep track of how the tiles are distributed in the world right? I just find out which tiles to draw based on where the viewscreen is on the world grid?

The one other thing is that I seem to be having a slight problem where i see a line moving vertically up along the screen whenever there's movement. I thought allegro automatically waited for vertical re-synch so Im not sure if thats the problem.

Audric
Member #907
January 2001

This should still work if I have an array of tiles and a map structure to keep track of how the tiles are distributed in the world right? I just find out which tiles to draw based on where the viewscreen is on the world grid?

Exactly :) This technique is generally called "tile engine"...
In this case, unless you use very limited number of tiles, you will feel the need for some kind of tile editor to help you "draw the map". Since it's very boring work usually, I'd recommend using any already-made tilemapping program such as Mappy. Your game will have to load the files into memory, as bitmaps and an array of indices.

Neil Roy
Member #2,229
April 2002
avatar

Also, I want to define a world which will be about 12000 by 9000 pixels in dimension, and I want to create a background which will span the whole area.. what's the best way to go about that?

Your universe can be really large, but the background doesn't need to be that size. The best way would be to have a black background, then have individual images like planets, galaxies, stars etc... loaded in as the player moves.

You could store the positions of the planets for example then display them if they move into the player's view. You could store the center position of an image, then check if it's boundries (checking width and height) fall inside the players view then blitting it to screen as needed. In this way you could have your universe just about as large as you could imagine.

Another method would be to tile space. If your individual tiles were say 64x64 pixels in size, your map array would be roughly 188 x 140 (or 12032 x 8960 pixels). For images larger than 64x64 you just divide them up into 64x64 chunks and display them that way. Or make the tile sizes larger of course. This could also make detecting where you are simple enough, if certain map locations contain a planet, than if your ship flies into tile location, you land on it (or collide).

Edit: For images larger than 64x64, say a 128x128 image, you could mark the four map locations that would hold this image as containing it, then draw the upper left corner of the image in the upper left corner of the first map position (no dividing of images).

Good luck with your game.

---
“I love you too.” - last words of Wanda Roy

Andynonomous
Member #12,867
May 2011

Thanks once again. I cant believe the progress I've been making. Years ago this seemed really hard, but I'm on a bit of a roll. What I do now is create an array of 4 128 by 128 bitmaps (maybe more later) and 4 more temporary bitmaps. I load images that I've found of individual stars into the temp bitmaps, then I draw a number of them on each bitmap in the array with a random distribution and a random scale (2, 4, 6, or 8 pixels). Once the array has 4 unique starfield images, I create a map of 40 by 40 tiles and randomly assign my tiles to fill it. That makes 5120 ^ 2 pixels in my world. It creates a totally unique background every time! And the scrolling is working great except for that vertical line moving across the screen. Its minor.. im hoping I can fix it.. or its just my monitor.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Andynonomous
Member #12,867
May 2011

Awesome.. Inserting the new display option seems to have fixed things. I now have an even better solution than I was looking for. I have a randomly generated background that I can scroll around in smoothly. I much appreciate all the help. I'll probably be posting more questions in the near future. Peace!

Go to: