Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Atelier on Thu 12/08/2010 19:50:40

Title: Enigmas of Space [SOLVED]
Post by: Atelier on Thu 12/08/2010 19:50:40
I hope you can help me with something which has been puzzling me for a long time. Before the problem, here's the code:


//Adapted from NsMn's original code.

DynamicSprite*TextSprite;
DynamicSprite*TextSpriteText;
DrawingSurface*TextSurface;
DynamicSprite*Texties;

function Add(String Text, bool Space)
{
  TextSpriteText = DynamicSprite.CreateFromExistingSprite(TextSprite.Graphic, true);
  Texties = DynamicSprite.CreateFromExistingSprite(DisplayBox.Graphic, true);

  TextSurface = TextSpriteText.GetDrawingSurface();
  TextSurface.DrawImage(0, 0 - GetTextHeight(Text, eFontFont1, 306), Texties.Graphic);

  ------>     <------

  TextSurface.DrawStringWrapped(0, 211 - GetTextHeight(Text, eFontFont1, 306), DisplayBox.Width, eFontFont1, eAlignLeft, Text);
  TextSurface.Release();

  TextSprite = DynamicSprite.CreateFromExistingSprite(TextSpriteText.Graphic, true);
  DisplayBox.NormalGraphic = TextSprite.Graphic;
}

//Half of the code isn't relevant to the problem but I added it just in case it's useful.


Bool Space is an optional parameter, defaulted as false. So when I call the function above, and set Space to true, I want a blank area to be added just before DrawStringWrapped is called.

So where the arrows are, I want to do this:


if (Space == true) ... //Add a blank space of x pixels high, THEN draw the text.


Obviously, the ... part is what I'm asking for your help with :=

Cheers
Atelier
Title: Re: Enigmas of Space
Post by: Joe on Thu 12/08/2010 22:01:00
DrawRectangle?
Title: Re: Enigmas of Space
Post by: Khris on Thu 12/08/2010 23:31:49
Like this:

  int y = 211 - GetTextHeight(Text, eFontFont1, 306);
  if (Space) y += 15;  // lower text by 15 pixels
  TextSurface.DrawStringWrapped(0, y, DisplayBox.Width, eFontFont1, eAlignLeft, Text);


?
Title: Re: Enigmas of Space
Post by: Atelier on Fri 13/08/2010 10:13:52
Thanks Joe and Khris for the replies. Your code works Khris, however something odd happens. Sometimes I call Add twice, ie:


Add("Line one.", true);
Add("Line two.");


It appears if I do this, one right after another (with the first set to true), the top line doesn't appear. There is however a space of 15 pixels added. Or, as is the case, this has bigger implications:


In Box_OnActivate:

Add(String.Format("> %s", saycommand), true);   //shows the last input, with a space above

...

if (Parser.Said("Input one")) Add("Response one.");   //could be in any script


Basically, the lines with 'true' in them don't appear if there's something added right after it (yet there is a space where there should be).

Atelier
Title: Re: Enigmas of Space
Post by: Khris on Fri 13/08/2010 11:02:56
Yeah, to be honest, I was waiting for that problem.
The function is called "Add", so I assumed it's about adding a line to some existing text. However, the function draws text on the same position every time, i.e. literally overwrites whatever was there before.
How does this work, exactly? Could you point me to the original code?

Btw, wouldn't it work to simply call Add("    ");?
Title: Re: Enigmas of Space
Post by: Atelier on Fri 13/08/2010 11:36:44
This is the code (there's one extra parameter, Colour, which I omitted earlier for clarity):


//Top of Global

DynamicSprite*TextSprite;
DynamicSprite*TextSpriteText;
DrawingSurface*TextSurface;
DynamicSprite*Texties;

function Add(String Text, int Colour, bool Space) //Add just literally means add a piece of text
{
     TextSpriteText = DynamicSprite.CreateFromExistingSprite(TextSprite.Graphic, true);
     Texties = DynamicSprite.CreateFromExistingSprite(DisplayBox.Graphic, true);

     TextSurface = TextSpriteText.GetDrawingSurface();
     TextSurface.Clear(COLOR_TRANSPARENT);

     TextSurface.DrawImage(0, 0 - GetTextHeight(Text, eFontFont1, 306), Texties.Graphic);
     TextSurface.DrawingColor = Colour;
     
     int y = 211 - GetTextHeight(Text, eFontFont1, 306);

     TextSurface.DrawStringWrapped(0, y, DisplayBox.Width, eFontFont1, eAlignLeft, Text);
     TextSurface.Release();

     TextSprite = DynamicSprite.CreateFromExistingSprite(TextSpriteText.Graphic, true);
     DisplayBox.NormalGraphic = TextSprite.Graphic;
}


Then I use the above for practically everything:


if (Parser.Said("Input one")) Add("Response one.", 15);   //Adds this line of text to the screen, in green
if (Parser.Said("Input two")) Add("Response two.", 15);
...


In actuality the code works perfectly - but there are no spaces separating any of the chunks of text, so it can look quite messy or hard to read.

Quote from: Khris on Fri 13/08/2010 11:02:56
Btw, wouldn't it work to simply call Add("    ");?

Yeah, this is something I tried, however only works if I wanted to add a gap after a line of text. (I can't call Add before I release the actual surface).
Title: Re: Enigmas of Space
Post by: Atelier on Mon 23/08/2010 20:16:42
I hate to double post, but my initial question is still unsolved.

Perhaps my explanation is the problem? :-\
Title: Re: Enigmas of Space
Post by: Khris on Mon 23/08/2010 22:19:54
I'm not sure I understand this.
Looking at the code, it doesn't seem to Add but rather replace what's there.

The original code does seem a bit messy so I didn't check it thoroughly before; I assumed it would scroll up some display sprite and add the line to the bottom.
Texties seems to never be used at all, and TextSpriteText's surface is cleared, then set as GUI background after the text is drawn to it.

Did you want to add space in order to display two lines simultaneously?

Right now, you could call Add() ten times one after another, and still only the last line would end up being visible.
Title: Re: Enigmas of Space
Post by: Atelier on Mon 23/08/2010 22:40:59
If I'm honest I only have a vague idea how it works myself. Another person wrote the code but never used it for their game, so with consultation they agreed to let me use it in my own. So I adapted it slightly for my own purposes (and it does the job perfectly, although I was never quite sure how efficient it is).

Perhaps I'll explain visually:

(http://img340.imageshack.us/img340/9654/45454019.png)

I want to find a way to add gaps (literally a space of blanks pixels) to separate chunks of unrelated text, as shown by the red rectangles. Only problem is, the way I'm currently doing it, there's a blank space added at the bottom where I don't want it (yellow rectangle). I'd like the last line to be flush with the top of the parser box.

It might be a long haul, but would it be simpler for me just to attempt re-writing the function, with all this in mind?

Thanks for all your help Khris.
Title: Re: Enigmas of Space
Post by: Khris on Mon 23/08/2010 23:20:19
I just saw the function does indeed scroll up the previous text.
I built the GUI and tested it.

Change the top of the function like this:

function Add(String Text, int Colour, int space)
{
     if (space > 0) Add(" ", 0, space-1);

     TextSpriteText =  ...


And import it like this:

import function Add(String Text, int Colour = 15, int space = 1);

Now call e.g.
  Add("Line one.");        // default: white text, single empty line before
  Add("Line two.", 15, 3);  // three empty lines before this line


I made color and spaces optional parameters. If you want another color, you can still skip the spaces parameter and it'll default to 1.
To get a different amount of spaces, state both color and spaces.
Title: Re: Enigmas of Space
Post by: Atelier on Tue 24/08/2010 09:46:33
Thanks Khris, problem all solved :=