Welcome, Guest. Please login or register.

Author Topic: Pixel isn't drawn  (Read 2008 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Omega Space ProtonsTopic starter

  • Newbie
  • *
  • Join Date: Oct 2010
  • Posts: 41
    • Show only replies by Omega Space Protons
Pixel isn't drawn
« on: October 25, 2010, 02:42:42 PM »
I have started coding my first game for the Amiga and am a bit stuck. I am trying to draw a single pixel on the screen using the WritePixel() function. However for some reason the pixel is not being drawn. The two line below sum it up:

Code: [Select]
/* set the pen color to blue */
pen = ObtainBestPenA(ViewAddress()->ViewPort->ColorMap, 0x0, 0x0, 0xFFFFFFFF, NULL);

 /* Draw the ball */
WritePixel(GameWindow->RPort, 100, 100);
Any assistance in this matter would be greatly appreciated.

Omega Space Protons

Update: see updated code below.
« Last Edit: October 25, 2010, 06:04:30 PM by Omega Space Protons »
 

Offline Thomas

Re: Pixel isn't drawn
« Reply #1 on: October 25, 2010, 03:19:37 PM »
1. you didn't check for failure. ObtainBestPen will return -1 if no matching pen was found, unless you specify OBP_FailIfBad,FALSE in the tag list

2. you got the pen number, but you forgot to tell the drawing system about it. Add SetAPen(GameWindow->RPort,pen); before WritePixel

3. you placed these commands after the Wait() command in your game loop procedure. That means, the commands will only be executed when some input message arrives. You should draw the pixel before the game loop is entered, then it will be drawn first and then the program will wait for input.

4. You never release the pen you obtained. And the obtain is done in a loop, so sooner or later all pens will be occupied. You should call ObtainBestPen once for each needed color in the initialisation routine and call ReleasePen once for each obtained pen in the cleanup routine. Do not call ObtainBestPen again in your game loop.

5. Not sure about the ViewAddress() function, never used it myself. I don't know if it is valid in an Intuition environment. I'd rather use GameWindow->WScreen->ViewPort.ColorMap. This way you get exactly the viewport which is used by your window and not the one which is used by the frontmost screen. Your game's screen could be put into the back by the user, then ViewAddress()->ViewPort will point to another screen.

Offline Omega Space ProtonsTopic starter

  • Newbie
  • *
  • Join Date: Oct 2010
  • Posts: 41
    • Show only replies by Omega Space Protons
Re: Pixel isn't drawn
« Reply #2 on: October 25, 2010, 03:37:51 PM »
is this better for setting the color:
Code: [Select]
[FONT=monospace]
int game_init(void)
{
 
    int result = 0;
 
    /* First open the graphics.library */
    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L);
 
    /* was the graphics library opened? */
    if(GfxBase != NULL)
    {
 
        /* Second open the intuition.library. */
        IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0L);
 
        /* was the library opened? */
        if (IntuitionBase != NULL)
        {
 
            /* since the was opened, we can open a window. */
            GameWindow = (struct Window *)OpenWindowTags(NULL,
                         WA_Left, 20,
                         WA_Top, 20,
                         WA_Width, 200,
                         WA_Height, 200,
                         WA_Title, (ULONG)"Omega Space Protons",
                         WA_DepthGadget, TRUE,
                         WA_CloseGadget, TRUE,
                         WA_SizeGadget, TRUE,
                         WA_DragBar, TRUE,
                         WA_GimmeZeroZero, TRUE,
                         WA_ReportMouse, TRUE,
                         WA_IDCMP, IDCMP_CLOSEWINDOW,
                         TAG_END);
 
            /* was the window opened? */
            if (GameWindow == NULL)
            {
                /* The window was not opened so display a message. */
                printf("Unable to open the window!\n");
                result = -1;
            }
        }
        else
        {
            /* the library was not opened, so display a message */
            printf("Unable to open the intuition.library!\n");
            result = -2;
        }
    }
    else
    {
        /* The graphics.library was not opened so display amessage. */
        printf("Unable to open the graphics.library!\n");
    }
[/FONT]
     blue = ObtainBestPenA(GameWindow->WScreen->ViewPort.ColorMap, 0x0, 0x0, 0xFFFFFFFF, NULL);

    if(blue = -1)
   {
   printf("Unable to obtain pen for the color blue!\n");
   }
[FONT=monospace]
    return result;
}[/FONT]
then inside my game loop:

and in the cleanup function:
Code: [Select]
void clean_up(void)
{
 
    /* If thw window is open, close it */
    if(GameWindow != NULL)
    {
        CloseWindow(GameWindow);
    }
 
    /* If the intuition.library is open, close it */
    if(IntuitionBase != NULL)
    {
        CloseLibrary((struct Library *)IntuitionBase);
    }
 
    /* If the graphics.library is open, close it */
    if(GfxBase != NULL)
    {
        CloseLibrary((struct Library *)GfxBase);
    }

    ReleasePen(GameWindow->WScreen->ViewPort.ColorMap, blue);
 
    return;
}
finally in the game loop:
Code: [Select]
void game_loop(void)
{
 
    ULONG signals;
    ULONG window_signal;
    struct IntuiMessage *message;
    UWORD msg_code;
    ULONG msg_class;
    BOOL end = FALSE;
    LONG pen;
 
    /* define the window signal */
    window_signal = 1L << GameWindow->UserPort->mp_SigBit;
 
    /* Main game loop */
 
    /* Wait until the close button is pressed */
    while (!end && GameWindow)
    {
        signals = Wait(window_signal);
 
        /* check the signal bit forour message port. Will be trueif there is a message */
        if (signals & ( 1L << GameWindow->UserPort->mp_SigBit))
        {
 
            /* there may be more then one message, so keep processing messages until there are no more. */
            while (message = (struct IntuiMessage *)GetMsg(GameWindow->UserPort))
            {
 
                /* Copy the necessary infomation from the message. */
                msg_class = message->Class;
                msg_code = message->Code;
 
                /* Reply as soon as possible. */
                ReplyMsg( (struct Message *)message) ;
 
                /* Take the proper action in response to the message */
                switch(msg_class)
                {
                    /* User pressed the close window gadget */
                case IDCMP_CLOSEWINDOW:
                    end = TRUE;
                    break;
 
                    /* default action */
                default:
                    break;
                }
            }
        }
 
    /* set the pen color to blue */
    SetPen(GameWindow->RPort, blue);
 
    /* Draw the ball */
    WritePixel(GameWindow->RPort, 100, 100);
    }
 
    return;
}
« Last Edit: October 25, 2010, 03:45:27 PM by Omega Space Protons »
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16882
  • Country: gb
  • Thanked: 6 times
    • Show only replies by Karlos
Re: Pixel isn't drawn
« Reply #3 on: October 25, 2010, 04:09:41 PM »
Your problem is as Thomas describes. You are setting your pen and drawing your pixel after your main event loop has finished. Assuming your window closes after the main loop function returns, you probably don't even get a chance to see it rendered.

Move this:

    /* set the pen color to blue */
    SetAPen(GameWindow->RPort, blue);
 
    /* Draw the ball */
    WritePixel(GameWindow->RPort, 100, 100);

before the IDCMP event handling loop (more specifically, before the Wait() call on the IDCMO port).
« Last Edit: October 25, 2010, 04:14:28 PM by Karlos »
int p; // A
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Pixel isn't drawn
« Reply #4 on: October 25, 2010, 04:14:36 PM »
Quote from: Omega Space Protons;586945
and in the cleanup function:
Code: [Select]
void clean_up(void)
{
 
    /* If thw window is open, close it */
    if(GameWindow != NULL)
    {
        CloseWindow(GameWindow);
    }
 
    /* If the intuition.library is open, close it */
    if(IntuitionBase != NULL)
    {
        CloseLibrary((struct Library *)IntuitionBase);
    }
 
    /* If the graphics.library is open, close it */
    if(GfxBase != NULL)
    {
        CloseLibrary((struct Library *)GfxBase);
    }

    ReleasePen(GameWindow->WScreen->ViewPort.ColorMap, blue);
 
    return;
}
You're referencing a dangling pointer here. GameWindow is a pointer to released memory (CloseWindow() has already released the memory at this point). Thus you will get a random WScreen pointer and further pain.

Depending on your luck it might crash only occasionally. It much depends on how busy your system is.
 

Offline Thomas

Re: Pixel isn't drawn
« Reply #5 on: October 25, 2010, 04:36:35 PM »
In addition to what Piru said, you may not call ReleasePen after you have closed graphics.library.

Put ReleasePen to the beginning of your cleanup routine.

Smilarly ObtainBestPenA will crash if the window couldn't be opened. You should put ObtainBestPenA into the else branch of the "if (GameWindow == NULL)".

Offline Omega Space ProtonsTopic starter

  • Newbie
  • *
  • Join Date: Oct 2010
  • Posts: 41
    • Show only replies by Omega Space Protons
Re: Pixel isn't drawn
« Reply #6 on: October 25, 2010, 04:42:13 PM »
Ok, I think I have it now, i made the necessary adjustments and would like to know if this code is correct before i finalize it. Attached is the modified code with the the suggested modifications awaiting your approval.

Omega Space Protons

Update: see updated code below.
« Last Edit: October 25, 2010, 06:04:08 PM by Omega Space Protons »
 

Offline Thomas

Re: Pixel isn't drawn
« Reply #7 on: October 25, 2010, 05:24:48 PM »
It should be SetAPen, not SetPen. There is also a B pen and an OL pen.

Also the = in if (blue = -1) should be ==. = is always an assignment and == is always a comparison. If you mix them up, strange things will happen.

Edit: you should also set result to an error code if the pen could not be obtained.

Offline Omega Space ProtonsTopic starter

  • Newbie
  • *
  • Join Date: Oct 2010
  • Posts: 41
    • Show only replies by Omega Space Protons
Re: Pixel isn't drawn
« Reply #8 on: October 25, 2010, 06:02:36 PM »
I think this is it, as soon as get the ok, I will finalize it into the current code branch. Thank you everyone for helping me with this problem, you have saved me a lot of time.

Omega Space Protons
« Last Edit: October 25, 2010, 06:10:27 PM by Omega Space Protons »