Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Mode 7 graphics in Allegro 5...

This thread is locked; no one can reply to it. rss feed Print
Mode 7 graphics in Allegro 5...
PCwizard200
Member #16,127
December 2015
avatar

Hy guys. I really want to learn a certain extra technique with allegro 5, and that technique is Mode 7 graphics. In case you don't know what that is, think the graphics in super Mario kart or F-Zero for the Super Nintendo. I did find an article on this, but it is for allegro 4:'(. The reason I want to learn this is because I want to play around with it for practice and some time I would want to make a kart racer. Is there any way I can be given an example on how to code these graphics, but just for allegro 5, or any other learning resources?

Chris Katko
Member #1,881
January 2002
avatar

Mode 7 is a special hardware only mode for the SNES. Technically, it cannot be done without an SNES.

However, mathematically, the "look" can be replicated and Allegro 5 or any other library works the same way. It's just a coordinate transformation. The question then is, what kind of transformation are you looking for?

If it's simply Mario Kart, you're just using a single texture at an angle and using bill board sprites for the cars will probably work. Billboard sprites are just polygons that always point toward the screen. Older games (and many modern) used them for trees to speed things up.

So you've got the "track" which is a single textured polygon (technically two triangles forming a square) that's rotated and a polygon for each car. And depending on the angle between the car and the camera, you "turn" the car by selecting a different angle picture. Much like Wolfenstein 3-D or Doom.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Kris Asick
Member #1,424
July 2001

One other thing to keep in mind when mimicking Mode 7 with a modern graphics API is that SNES games like Super Mario Kart and F-Zero set artificial boundaries for the rendering of the 3D floor of the track because of the angle it was being rendered at. You'll notice when playing either of those games things at the very edge of the horizon seem to come up relatively quickly, as though more stuff should be visible beyond the horizon, and that's because more stuff SHOULD be. The rendering is cut off early so that a horizon can be drawn at all! :o

The reason this is important is because if you actually used the right angle so that the horizon would really be where you intended it to be, everything on the floor would look squished because of the angle and track elements would be almost impossible to make out unless you were right on top of them. :P

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Mark Oates
Member #1,146
March 2001
avatar

Hey again PCWizard200. :)

I'm guessing you're referencing Amarillion's legendary article on sin/cos where he also describes how to achieve Mode 7?

I'm looking at the mode_7() function in that tutorial and it looks like the only difference between Allegro 4 and Allegro 5 is this part:

#SelectExpand
1 // calculate the starting position 2 space_x = cx + fmul (distance, fcos(angle)) - bmp->w/2 * line_dx; 3 space_y = cy + fmul (distance, fsin(angle)) - bmp->w/2 * line_dy; 4 5 // go through all points in this screen line 6 for (screen_x = 0; screen_x < bmp->w; screen_x++) 7 { 8 // get a pixel from the tile and put it on the screen 9 putpixel (bmp, screen_x, screen_y, 10 getpixel (tile, 11 fixtoi (space_x) & mask_x, 12 fixtoi (space_y) & mask_y)); 13 // advance to the next position in space 14 space_x += line_dx; 15 space_y += line_dy; 16 }

Even though I haven't tested this code, updating it to Allegro 5 would look like this:

#SelectExpand
1 // calculate the starting position 2 space_x = cx + fmul (distance, fcos(angle)) - al_get_bitmap_width(bmp)/2 * line_dx; 3 space_y = cy + fmul (distance, fsin(angle)) - al_get_bitmap_width(bmp)/2 * line_dy; 4 5 // set our drawing target 6 ALLEGRO_STATE previous_state; 7 al_store_state(&previous_state, ALLEGRO_STATE_TARGET_BITMAP); 8 al_set_target_bitmap(bmp); 9 10 // lock our bitmaps for faster pixel access 11 al_lock_bitmap(tile, ALLEGRO_LOCK_READONLY, ALLEGRO_PIXEL_FORMAT_ANY); 12 al_lock_bitmap(bmp, ALLEGRO_LOCK_WRITEONLY, ALLEGRO_PIXEL_FORMAT_ANY); 13 14 // go through all points in this screen line 15 for (screen_x = 0; screen_x < al_get_bitmap_width(bmp); screen_x++) 16 { 17 // get a pixel from the tile and put it on the screen 18 al_put_pixel (screen_x, screen_y, 19 al_get_pixel (tile, 20 fixtoi (space_x) & mask_x, 21 fixtoi (space_y) & mask_y)); 22 // advance to the next position in space 23 space_x += line_dx; 24 space_y += line_dy; 25 } 26 27 // unlock our bitmaps 28 al_unlock_bitmap(tile); 29 al_unlock_bitmap(bmp); 30 31 // restore our previous drawing target 32 al_restore_state(&previous_state);

PCwizard200
Member #16,127
December 2015
avatar

Hey everyone. Sorry for the late reply, for some reason I did not get an email that said you guys reply to my post. I guess this it is because this forum works in a different manner? Well, anyway I want to start with a simple f-zero type racing game, or mario kart style, then I will want to build up on that by making a prototype on a starfox type mode7, just without the polygons. (Meaning, that It would have a track like a f-zero game, but the player sprite would be able to move up and down and such, and shoot enemys coming at you, think the speeder stages from Super Star Wars). However full games will be for a later time, right now I just want to start by playing around with this technique, to get a better understanding of it, kinda like making graphic demos, along with making simple 2D games for practice. So thanks for your input on mode 7 graphics for me to visualize it Chris Katko and Kris Asick. Mark Oats I am happy to also play around with your sample code, ya never know, I might build upon it[:. Anyway, Im off to code! (:

Bruce Pascoe
Member #15,931
April 2015
avatar

If you want to be emailed on reply, you have to click the "Subscribe" button at the bottom of the thread.

Kris Asick
Member #1,424
July 2001

Just to clear something up: Star Fox does NOT use Mode 7 graphics for anything. Virtually all rendering in Star Fox is done by the SuperFX chip on the cart as it was designed specifically for manipulating and rendering polygons. Mode 7 on its own is incapable of rendering polygons as it's just a transformation engine. You can witness this yourself in Super Mario Kart in that the sprites which show up on the track as you're racing do not smoothly scale and instead have different sprites for different distances from the camera. Same with F-Zero or any other such game using this sort of perspective transformation.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

PCwizard200
Member #16,127
December 2015
avatar

Yes Kris Asick, I meant that I would want to eventually create a StarFox game just without the polygons, and entirely in mode 7 graphics. But that will be for another project.

Oscar Giner
Member #2,207
April 2002
avatar

starfox type mode7, just without the polygons. (Meaning, that It would have a track like a f-zero game, but the player sprite would be able to move up and down and such, and shoot enemys coming at you, think the speeder stages from Super Star Wars)

This game actually existed: HyperZone

Go to: