Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with Shape object displaying black  (Read 7711 times)

0 Members and 1 Guest are viewing this topic.

Wibbs

  • Newbie
  • *
  • Posts: 40
    • View Profile
Problem with Shape object displaying black
« on: March 09, 2010, 05:05:22 pm »
Hey all,

I'm having a frustrating problem which I'm sure must be something really obvious, but I can't for the life of me see what I'm doing wrong.

I have a rectangle that is acting as a simple button.  When the mouse enters it, the colour of the rectangle changes.  This all works up to a point, but no matter what colour I set for displaying when the mouse is over the rectangle, it displays black.  I have searched my code to see if I'm accidentally setting it somewhere else, but couldn't find anything.  I've even stepped through the code to check what value the colour has just before and after rendering, and it appears correct....so what am I doing wrong?  Any help would be appreciated.

Thanks,

Phil

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Problem with Shape object displaying black
« Reply #1 on: March 09, 2010, 06:00:16 pm »
We need code in order to help you.

Wibbs

  • Newbie
  • *
  • Posts: 40
    • View Profile
Problem with Shape object displaying black
« Reply #2 on: March 10, 2010, 12:17:15 am »
Yeah sorry about that, I was at work so didn't have access to my code.  I've done a bit more digging since then, and have narrowed the problem down, but I am totally stumped.  I'm using version 1.5 of SFML.

I have a wrapper around the SFML.Shape object:
Code: [Select]

public class RectangleSprite
    {
        public RectangleSprite(int id, int x, int y, int width, int height, Color colour)
        {
            rectangle = Shape.Rectangle(new Vector2(0, 0), new Vector2(width, height), colour);
            rectangle.Position = new Vector2(x, y);
        }

        public bool OnRender(long time, long elapsedTime, RenderWindow window)
        {
            window.Draw(rectangle);
            return true;
        }

        public void SetColour(Color c)
        {
            rectangle.Color = c;
        }

        private Shape rectangle;
    }


I create 2 instances of this with:
Code: [Select]

test1 = new RectangleSprite(1, 0, 0, 50, 50, Color.Blue);
test2 = new RectangleSprite(1, 50, 0, 50, 50, Color.White);

which are rendered with:
Code: [Select]

public override bool OnRender(long time, long elapsedTime, RenderWindow window)
        {
            test1.OnRender(0, 0, window);
            test2.OnRender(0, 0, window);        
            return true;
        }

The colour of anobject changes when the mouse enters it with:
Code: [Select]

test1.SetColour(Color.Red);
test2.SetColour(Color.Red);

and when the mouse leaves again the colour is reset with:
Code: [Select]

test1.SetColour(Color.Blue);                    
test2.SetColour(Color.White);


The issue I have is that test2 correctly turns red when the mouse is over it as you would expect, but test1 turns black.  This seems to be to do with the specific colour chosen for when the mouse is out of the rectangle.  I haven't tested more than 3 or 4 of the predefined colours, but blue is the only one I've found that causes a problem so far (white, yellow and magenta work OK).  

I would appreciate any thoughts people have on this, because I've reduced the problem down as much as I can, and I can't see any reason for the behaviour I'm seeing.

Thanks in advance,

Phil

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Problem with Shape object displaying black
« Reply #3 on: March 10, 2010, 05:55:36 am »
You got me there. First thing that crossed my mind was that you might have some problems with your mouse picking code, but if you're having problems only with the blue color... A few things I'd try:
-Clear the window in a different color
-Try off all default colors, see if any other reproduce the problem
-Try using new Color(0, 0, 255) instead of Color.Blue
-Same thing but with new Color(0, 0, 230)
-Try updating to the SVN (SFML2) and see if that works out

But then again, just throwing stuff out there, I have absolutely no real idea of what could be the problem.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with Shape object displaying black
« Reply #4 on: March 10, 2010, 08:09:13 am »
You have to keep in mind that there are two different colors in shapes: the global one, which is inherited from SFML.Drawable, and the individual color or each points of the shape. These two colors are modulated (multiplicated) together to produce the final color.

Here is the problem: the color that you pass in Shape.Rectangle is assigned to the points, not to the global color. But then when you change the Color property, you change the global color of the shape. So you end up with both colors being used (modulated) instead of replacing the first one, which may produce black (red * blue == black).

If you only use the global color, then you should leave the points color white (when calling Shape.Rectangle) and just use the Color property.
Laurent Gomila - SFML developer

Wibbs

  • Newbie
  • *
  • Posts: 40
    • View Profile
Problem with Shape object displaying black
« Reply #5 on: March 10, 2010, 11:45:24 am »
Hi Laurent,

Thanks for the reply, and I understand what you are saying in theory.  However, I'm probably being dim here but how do I assign the global colour without setting the point colour?  Is it this colour that is set in the constructor for the object?  If this is the case, how do I change it after the object has been created?

Sorry for all the (probably obvious) questions.

Phil

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with Shape object displaying black
« Reply #6 on: March 10, 2010, 11:56:26 am »
Instead of
Code: [Select]
rectangle = Shape.Rectangle(new Vector2(0, 0), new Vector2(width, height), colour);
Write
Code: [Select]
rectangle = Shape.Rectangle(new Vector2(0, 0), new Vector2(width, height));
rectangle.Color = colour;
Laurent Gomila - SFML developer

Wibbs

  • Newbie
  • *
  • Posts: 40
    • View Profile
Problem with Shape object displaying black
« Reply #7 on: March 10, 2010, 06:53:47 pm »
Thanks for that, it's worked a treat.

On a sort of related subject, I've had a quick go at compiling the .net version of 2.0, but with no success.  I fully admit to the fact this is likely to be just me not understanding the process fully, and was wondering if there is anything written which takes you through the steps of doing it.  I've looked on the wiki with no success.

Thanks,

Phil

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with Shape object displaying black
« Reply #8 on: March 10, 2010, 08:00:53 pm »
No, there's no documentation about building the .Net building. I should definitely write one.

You have to :
1. Build SFML (C++) in release static mode
2. Build CSFML in release DLL mode
3. Build SFML.Net
4. Copy all the needed DLLs (CSFML DLLs and SFML.Net assemblies) to wherever they will be found by your application

That's it.

Note that you can use the batch builds to build SFML and CSFML easily. You should really use them if you use MinGW, it includes the extra step needed to properly compile the static C++ libraries.

If you still don't succeed, tell me where you're stuck in the process.
Laurent Gomila - SFML developer

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Problem with Shape object displaying black
« Reply #9 on: March 11, 2010, 12:35:38 am »
Can anyone explain to me why it wasn't working only with blue? I'm trying to understand what was the link to the color  :?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with Shape object displaying black
« Reply #10 on: March 11, 2010, 07:58:47 am »
blue * red = (0, 0, 255) * (255, 0, 0) component-wise = (0, 0, 0) = black
white * red = (255, 255, 255) * (255, 0, 0) component-wise = (255, 0, 0) = red
Laurent Gomila - SFML developer

Xeon06

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Problem with Shape object displaying black
« Reply #11 on: March 11, 2010, 12:47:53 pm »
Ahh, thanks  :lol: