We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Is the main Sketch an instance of PGraphics
Page Index Toggle Pages: 1
Is the main Sketch an instance of PGraphics? (Read 3185 times)
Is the main Sketch an instance of PGraphics?
May 19th, 2010, 3:33pm
 
I'm trying to set up a system which allows me to use the same routines to draw to the main graphics scope or to a sub-window within it. It's working pretty well (I can use an hDraw(PGraphics tScope) routine to refer to whichever scope I need), but I can't find a way to refer to the original scope. Is there a hidden variable (an instance of PGraphics) that I can use? At the moment I'm setting up a first PGraphics object which is going over the whole screen, but it suffers from all the limitations of PGraphics, particularly bad text and other antialiasing issues.
Re: Is the main Sketch an instance of PGraphics?
Reply #1 - May 19th, 2010, 4:12pm
 
the object 'g' is the PGraphics object of the sketch.
Re: Is the main Sketch an instance of PGraphics?
Reply #2 - May 20th, 2010, 1:05am
 
Danny Kodicek wrote on May 19th, 2010, 3:33pm:
it suffers from all the limitations of PGraphics, particularly bad text and other antialiasing issues.

You shouldn't have more problems with an off-screen PGraphics than the sketch's one.
Such issues are usually visible when you overlay text on each frame without erasing the frame first: the natural anti-aliasing of text becomes blocky as half-tone pixels cumulate.
Re: Is the main Sketch an instance of PGraphics?
Reply #3 - May 23rd, 2010, 2:04pm
 
PhiLho  wrote on May 20th, 2010, 1:05am:
Danny Kodicek wrote on May 19th, 2010, 3:33pm:
it suffers from all the limitations of PGraphics, particularly bad text and other antialiasing issues.

You shouldn't have more problems with an off-screen PGraphics than the sketch's one.
Such issues are usually visible when you overlay text on each frame without erasing the frame first: the natural anti-aliasing of text becomes blocky as half-tone pixels cumulate.


That's not what I'm finding - and it's in line with the Help, which says 'Note that transparency levels are binary: pixels are either complete opaque or transparent. For the time being (as of release 0127), this means that text characters will be opaque blocks. This will be fixed in a future release (Bug 641).' I've found that alpha channels aren't being obeyed at all.

Thanks for the g suggestion, JohnG, I'll try that.
Re: Is the main Sketch an instance of PGraphics?
Reply #4 - May 23rd, 2010, 10:27pm
 
Danny Kodicek wrote on May 23rd, 2010, 2:04pm:
it's in line with the Help, which say [...]

At the risk of shocking you, the reference isn't always up to date, I have posted a number of change requests asking for updates: after all, that's part of the open source process.
The best is to do a simple test:
Code:
PFont fontT;
void setup()
{
 size(512, 512);
 background(255);
 fontT = loadFont("TimesNewRomanPSMT-48.vlw");
 textFont(fontT);
 frameRate(10);
}
void draw()
{
 background(#FF8800);
 fill(#0088FF);
 text("This is a test", 10, 100);
}

Look at the output with a zoom utility like the excellent Gipsysoft's Zoom+ (for Windows), ideally one, like this one, able to update zoomed view in real time.
If you change the background color, you will see the text still blends with it.
Now, comment out the background call and look at the borders of the text. You can see pixels going from half tone to full color. The text becomes aliased.
That's a problem many people complain about (ie. they just forgot to erase before redrawing the text), that's why I supposed it could be your issue. (Can be different, I haven't seen your source.)
Re: Is the main Sketch an instance of PGraphics?
Reply #5 - May 24th, 2010, 4:04pm
 
No, that's not my issue. Here's the code I was using (this is a method of a TextFormat class I've created):

void hDrawText(PGraphics tScope, String tString, int tX, int tY) {
   tScope.pushStyle();
   tScope.textFont(pFont, pSize);
     tScope.textAlign(LEFT,CENTER);
   tScope.fill(pCol);
   tScope.text(tString, tX, tY);
   tScope.popStyle();
 }

This was part of a general draw loop, which went something like this:
void draw()  {
 if (prog.hUpdate()) {
 pMain.beginDraw();
 prog.hDraw(pMain);
 pMain.endDraw();
 image(pMain,0,0);
 }
}
where pMain was an instance of PGraphics. Now I've replaced pMain with g and removed the beginDraw, endDraw and image lines.
When I used a buffer, the text looked horrible, with bits of antialiasing missing. As soon as I replaced it, it looked great. So there's definitely as issue there.

Completely off the point, holy crapola, Java's a horrible language when you're used to something as flexible as Lingo. Arrays in particular are just unpleasant. Irrelevant, I know, but I had to get it off my chest - I've been struggling with getting a HashMap from Strings to floats for the last hour.
Re: Is the main Sketch an instance of PGraphics?
Reply #6 - May 25th, 2010, 2:37am
 
Not sure why you think that's not your issue. I don't see the background of the text erased in your hDrawText() routine (might be done elsewhere) and, beside, I don't see hDrawText() called in the draw() routine you show.
Re: Is the main Sketch an instance of PGraphics?
Reply #7 - May 25th, 2010, 11:59am
 
I was just simplifying. There's an object heirarchy that leads to the drawText function. But yes, I do draw the background. And besides anything else, as I say, the real proof is that when I switched to using g instead of my own PGraphics object, with no other changes, the problem went away.
Re: Is the main Sketch an instance of PGraphics?
Reply #8 - May 25th, 2010, 12:03pm
 
And just to make it absolutely clear - here's the code in the prog object:


 void hDraw(PGraphics tScope) {
   tScope.background(pBGCol);
   super.hDraw(tScope);
 }
Looking for "clarification"
Reply #9 - May 27th, 2010, 9:31pm
 
Get it?  Clarification?  OK, that was a bad pun.   Smiley

I wanted to jump in on this thread because I feel like using an offscreen buffer does have aliasing issues, and not just with text.  However, text provides a convenient testbed.  Here is a test I wrote, and the result.

Code:

// Test to compare:
// * Rendering text directly to screen
// * Rendering text to an offscreen buffer and placing on screen

PGraphics buffer;
PFont myFont;

void setup() {
 size(150, 100);
 buffer = createGraphics(150, 50, P2D);
 myFont = createFont("Verdana", 14);
}

void draw() {
 renderText(g, "Direct to Screen");
 renderText(buffer, "First to Buffer");
 image(buffer, 0, 50);
}

void renderText(PGraphics target, String string) {
 target.beginDraw();
 target.fill(0);
 target.rect(0, 0, width, 50);
 target.fill(255);
 target.textAlign(CENTER);
 target.textFont(myFont);
 target.text(string, width/2, 25);
 target.endDraw();
}



...

It's pretty obvious to me that one thing is not like the other.  But it's not clear why!

If I understand what's being said (and trust me, it's a good chance I don't!  Grin), then I think I'm doing it right by drawing a "background" of black behind the text.  If it's not a bug, then could you help me/us by fixing the code above?

Thank you!
Re: Is the main Sketch an instance of PGraphics?
Reply #10 - May 28th, 2010, 1:16am
 
Mmm, for some reason (I use v.0184) the off-screen rendering is even uglier for me!
Anyway, there is a simple reason for the difference you see: default rendering of a sketch (size with 2 params) is JAVA2D. You create a P2D buffer. If you do: buffer = createGraphics(150, 50, JAVA2D); you will see no difference.
JAVA2D is better suited to fonts created with createFont (as it uses native font, not a rendered one) although the difference stands out more when you change the size in the textFont() call (vector font rendering vs. bitmap font scaling).
Re: Is the main Sketch an instance of PGraphics?
Reply #11 - May 28th, 2010, 8:28pm
 
I verified that using JAVA2D fixed the issue I was seeing.  Thank you!
Page Index Toggle Pages: 1