Enigmas of Space [SOLVED]

Started by Atelier, Thu 12/08/2010 19:50:40

Previous topic - Next topic

Atelier

I hope you can help me with something which has been puzzling me for a long time. Before the problem, here's the code:

Code: ags

//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:

Code: ags

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

Joe

Copinstar © Oficial Site

Khris

Like this:

Code: ags
  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);


?

Atelier

Thanks Joe and Khris for the replies. Your code works Khris, however something odd happens. Sometimes I call Add twice, ie:

Code: ags

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:

Code: ags

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

Khris

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("    ");?

Atelier

This is the code (there's one extra parameter, Colour, which I omitted earlier for clarity):

Code: ags

//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:

Code: ags

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).

Atelier

I hate to double post, but my initial question is still unsolved.

Perhaps my explanation is the problem? :-\

Khris

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.

Atelier

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:



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.

Khris

#9
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:

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

     TextSpriteText =  ...


And import it like this:

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


Now call e.g.
Code: ags
  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.

Atelier

Thanks Khris, problem all solved :=

SMF spam blocked by CleanTalk