Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Atelier on Sat 21/01/2012 18:23:08

Title: Scrolling and Locking (solved)
Post by: Atelier on Sat 21/01/2012 18:23:08
I'm trying to make the Print function for my text game more versatile, but I've messed around with it and can't seem to get anywhere. I'm not sure whether the existing code can be adapted, or whether it needs to be rewritten.

I'd like to scroll up and down a sprite using the up/down arrow keys, to give an effect similar to a list box. When another bit of text is added to the sprite (at the bottom), the view of the sprite jumps to the bottom if the player has been reading text further up. And to prevent this, the player can toggle scroll lock on and off, so if something is added and they are reading further up, it does not jump down to show what was just added.

Here's the current function, Print, which works fine (but without scrolling enabled):


DynamicSprite*SpriteOne;
DynamicSprite*SpriteTwo;
DynamicSprite*Final;
DrawingSurface*Surface;
bool FirstPrint = true;

function Print(String text, int colour, bool space)
{
         if (space) Print(" ", 0);

         if (FirstPrint) {
                   SpriteOne = DynamicSprite.Create(DisplayBox.Width, DisplayBox.Height, true);
                   SpriteTwo = DynamicSprite.Create(DisplayBox.Width, DisplayBox.Height, true);
                   Final = DynamicSprite.CreateFromExistingSprite(2);
                   FirstPrint = false;
         }

         Surface = SpriteOne.GetDrawingSurface();
         Surface.Clear();

         //draw new text
         Surface.DrawingColor = colour;
         int y = DisplayBox.Height - GetTextHeight(text, DisplayBox.Font, DisplayBox.Width)-2;
         Surface.DrawStringWrapped(0, y, DisplayBox.Width, DisplayBox.Font, eAlignLeft, text);          //draw new text at very bottom, minus however high the new text is

         //draw old text (shift current text up)
         Surface.DrawImage(0, 0 - GetTextHeight(text, DisplayBox.Font, DisplayBox.Width)-2, SpriteTwo.Graphic);          //shift old text up by however high new text is
         Surface.Release();

         SpriteTwo = DynamicSprite.CreateFromExistingSprite(SpriteOne.Graphic);

         Surface = Final.GetDrawingSurface();
         Surface.Clear();
         Surface.DrawImage(0, 0, 2);                                    //draw a leather panel
         Surface.DrawImage(0, 0, SpriteOne.Graphic);           //draw final text on top
         Surface.Release();

         DisplayBox.NormalGraphic = Final.Graphic;
}


As far as I'm aware, the sprites are cropped after each time it's called to fit the height and width of DisplayBox? So if I wanted to scroll up and down, one sprite with all the previous text on would need to be stored?

Also, because I'm using AA text, I have to overlay the text onto a leather panel before it becomes the normal graphic of DisplayBox. This therefore means that when the player scrolls up for example, the part of the sprite you're looking at is moved equivalent to 1 line of text downwards, and this section has to be captured and overlaid on top of the leather panel, so it becomes seamless with the rest of the GUI. How can I do this?

Another small thing I noticed, I can't find an enum for the scroll lock key. I can use something else for toggling the scroll lock bool, it just seemed fitting :)

Thanks
Atelier
Title: Re: Scrolling and Locking
Post by: Atelier on Sat 21/01/2012 23:05:56
I'm getting somewhere...


//rewrote Print function

DynamicSprite*Raw;
DynamicSprite*Final;
DrawingSurface*Surface;
bool FirstPrint = true;

function Print(String text, int colour, bool space)
{
    if (space) Print(" ");
    int textheight = GetTextHeight(text, DisplayBox.Font, DisplayBox.Width);
    if (FirstPrint) { Raw = DynamicSprite.Create(DisplayBox.Width, textheight, true); FirstPrint = false; }
    else Raw.ChangeCanvasSize(DisplayBox.Width, Raw.Height+textheight, 0, 0);

    Surface = Raw.GetDrawingSurface();
    Surface.DrawingColor = colour;
    Surface.DrawStringWrapped(0, Raw.Height-textheight, DisplayBox.Width, DisplayBox.Font, eAlignLeft, text);
    Surface.Release();

    if (scroll_lock) return;

    Final = DynamicSprite.CreateFromExistingSprite(2);
    Surface = Final.GetDrawingSurface();
    Surface.DrawImage(0, DisplayBox.Height-Raw.Height, Raw.Graphic);
    Surface.Release();

    DisplayBox.NormalGraphic = Final.Graphic;

    //Raw.SaveToFile("raw.bmp");
}



function on_key_press(eKeyCode keycode)
{
    if (keycode == eKeyUpArrow || keycode == eKeyDownArrow) scroll(keycode);
}



function scroll(int direction)
{
    if (direction == eKeyUpArrow) {

        Final = DynamicSprite.CreateFromExistingSprite(2);
        Surface = Final.GetDrawingSurface();
        Surface.DrawImage(0, DisplayBox.Height-Raw.Height+1, Raw.Graphic);
        Surface.Release();
        DisplayBox.NormalGraphic = Final.Graphic;

    }

    if (direction == eKeyDownArrow) {

        Final = DynamicSprite.CreateFromExistingSprite(2);
        Surface = Final.GetDrawingSurface();
        Surface.DrawImage(0, DisplayBox.Height-Raw.Height-1, Raw.Graphic);
        Surface.Release();
        DisplayBox.NormalGraphic = Final.Graphic;

    }
}


So at the moment my scroll function only moves the text up or down one pixel... I also need to put in checks to see whether the player can't scroll up or down any more. Any clues please?