# Tutorial

### Stages

SGE's stages are some kine of world, holding sprites and background layers. They are usefull for parallax scrolling games like jump&run's or sidescroll shooters. Basically they are a container for different graphic layers of different sizes and one layer containing spritegroups.

The examples assume, that you have opened a data file on the varialbe myfile

### Setup

First you have to set up the stage:

    SGESTAGE *stage;
    SGESPRITEGROUP *enemies;
    SGESPRITE *tmpenemy;
    SGESPRITE *player=sgeSpriteNewFile(myfile, "data/player.png");;

    stage=sgeStageNew(6400, 480);
    enemies=sgeSpriteGroupNew();
    sgeStageAddSpriteGroup(stage, enemies);

    tmpenemy=sgeSpriteNewFile(myfile, "data/enemy1.png");
    tmpenemy->x=3000;
    tmpenemy->y=100;
    sgeSpriteGroupAddSprite(enemies, tmpenemy);

    tmpenemy=sgeSpriteNewFile(myfile, "data/enemy2.png");
    tmpenemy->x=1000;
    tmpenemy->y=250;
    sgeSpriteGroupAddSprite(enemies, tmpenemy);

    sgeStageAddLayer(stage, sgeSpriteNewFile(tmp, "data/backgroundlayer.png"), 0, 0);
    sgeStageSetLayerHeight(stage,
    	sgeStageAddLayer(stage, sgeSpriteNewFile(tmp, "data/foregroundlayer.png"), 0, 240),
    	800);

First we have to create the stage:

    stage=sgeStageNew(6400, 480);

This will create a stage, of a size 6400 x 480. The stage will have to variables that let you control the camera position of this stage: stage->cameraX and stage->cameraY.

Then we add a spritegoup that will contain our enemies:

    enemies=sgeSpriteGroupNew();
    sgeStageAddSpriteGroup(stage, enemies);

We then add two enemy sprites and set them within our 6400x480 world.

    tmpenemy=sgeSpriteNewFile(myfile, "data/enemy1.png");
    tmpenemy->x=1000;
    tmpenemy->y=250;
    sgeSpriteGroupAddSprite(enemies, tmpenemy);

Lets add a background layer. Layers have to be at least as big as the screen resolution, but can be a lot bigger. The two zeros at the end of the command set the offset the layer will have to the camera resolution. We do not want one for the background layer.

    sgeStageAddLayer(stage, sgeSpriteNewFile(tmp, "data/backgroundlayer.png"), 0, 0);

Now comes a little tricky thing. We'll set up a foreground layer. The foregroundlayer is 240 pixel in height, so assuming, our screen resolution would be 640x480, it would cover half of the screen. We want it to look like scrolling in front of the sprites, so it should be at the bottom of the screen, so we give it a y offset of 240 pixel. With the function sgeStageSetLayerHeight we set the relative height to the world. The foreground layer will be handled if it would be 800 pixel in height, so it scrolls around vertical.

    sgeStageSetLayerHeight(stage,
    	sgeStageAddLayer(stage, sgeSpriteNewFile(tmp, "data/foregroundlayer.png"), 0, 240),
    	800);

### Drawing

In our games redraw routing, we'll have to handle the drawing.

    sgeStageDrawLayer(stage, screen, 0);
    sgeStageDrawSpriteGroup(stage, 0);
    sgeSpriteDraw(player);
    if (sgeStageSpriteGroupCollideSprite(stage, 0, player, RELATIVE)) {
    	// this is executed, if player collides
    	// spritegroup 0 (enemies)
    }
    sgeStageDrawLayer(stage, screen, 1);

First, we draw the background, followed by the enemies spritegroup (0) and the player sprite

Next we check if the player collides our enemies

    if (sgeStageSpriteGroupCollideSprite(stage, 0, player, RELATIVE)) {

So this function checks if collision takes place between the sprite player and the stages spritegroup 0 (enemies), but whats thate RELATIVE for?

You have two ways, to handly your player sprite, you could either position it relative to the screen, or to the world. So imagine our world of 6400x480 at a camera resolution of 640x480. Now our camera will be positioned with stage->cameraX=128, 128 pixels from the left stage border. if we now set player->x=192, it could be either drawn out on screens coordinate 192 - which is what we currently to, as wie use sgeSpriteDraw, which always is using the screen position - or it could be shown on screen coordinate 64, because our camera is on position 128 already. It is not possible the shown way, we would had to define a new stage spritegroup (e.g. players) and add the player sprite to this group, letting sgeStageDrawSpriteGroup() do the job of calculating the real screen address. You'll see this later on the first reallive example.

There are two other collision detection functions, depending on what you want to detect. The 1st detects collision with animated sprites, the second detects collision between two stage spritegroups.

    sgeStageSpriteGroupCollideAnimatedSprite(stage, groupnumber, animatedsprite, RELATIVE|ABSOLUTE);
    sgeStageSpriteGroupCollideSpriteGroup(stage, groupnumber, othergroupnumber, RELATIVE|ABSOLUTE);