Hard Challenge - Moving images

Discussion in 'Programming General' started by lordzsolt, Apr 10, 2012.

Hard Challenge - Moving images
  1. Unread #1 - Apr 10, 2012 at 6:33 PM
  2. lordzsolt
    Joined:
    Jul 1, 2008
    Posts:
    2,059
    Referrals:
    2
    Sythe Gold:
    2
    Two Factor Authentication User Halloween 2013

    lordzsolt Grand Master
    $5 USD Donor

    Hard Challenge - Moving images

    I've come up to possibly my hardest hard challenge.

    Basically, I have a background and I have an image that follows the cursor.

    I managed to do this by constantly printing the background image to the screen, then printing the image that's following the cursor on it. But this causes the image to keep blinking (obviously).

    Is there any other way to do this?
     
  3. Unread #2 - Apr 11, 2012 at 6:58 AM
  4. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    Hard Challenge - Moving images

    How are you doing the drawing?

    Are you using double buffering? Are you sure the images are being drawn every frame?
     
  5. Unread #3 - Apr 13, 2012 at 3:27 PM
  6. lordzsolt
    Joined:
    Jul 1, 2008
    Posts:
    2,059
    Referrals:
    2
    Sythe Gold:
    2
    Two Factor Authentication User Halloween 2013

    lordzsolt Grand Master
    $5 USD Donor

    Hard Challenge - Moving images

    I did it with the readimagefile command.
     
  7. Unread #4 - Apr 14, 2012 at 8:24 AM
  8. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    Hard Challenge - Moving images

    If you post your code up I might be able to help.
     
  9. Unread #5 - Apr 16, 2012 at 1:20 PM
  10. lordzsolt
    Joined:
    Jul 1, 2008
    Posts:
    2,059
    Referrals:
    2
    Sythe Gold:
    2
    Two Factor Authentication User Halloween 2013

    lordzsolt Grand Master
    $5 USD Donor

    Hard Challenge - Moving images

    Code:
    readimagefile("Kepek/Battle/BattleBG_Deploy.JPG",0,0,x,y);
        //Saving the background
        int size=imagesize (0,0,x,y);
        void* buffer1=malloc(size); //Alapkepernyo repulok nelkul
        void* buffer2=malloc(size);
        getimage(0,0,x,y,buffer1);
        //Showing the airplanes
        readimagefile("Kepek/Battle/Troops/Command_Center.GIF",535,318,659,442);
        readimagefile("Kepek/Battle/Troops/Scorpion.GIF",535,463,659,587);
        readimagefile("Kepek/Battle/Troops/Hellion.GIF",535,610,659,734);
        readimagefile("Kepek/Battle/Troops/Cannon.GIF",535,755,659,879);
        int place=0; // Number of planes deployed.
        cx=-1;
        cy=-1;
        bool run=true;
        do
        {
            getmouseclick(WM_LBUTTONDOWN,cx,cy);
            if (cx>535 && cy>318 && cx<659 && cy<442 && place!=4) // Clicked command center
            {
                putimage(0,0,buffer1,0);
                readimagefile("Kepek/Battle/Troops/Scorpion.GIF",535,463,659,587);
                readimagefile("Kepek/Battle/Troops/Hellion.GIF",535,610,659,734);
                readimagefile("Kepek/Battle/Troops/Cannon.GIF",535,755,659,879);
                getimage(0,0,x,y,buffer2);
                mx2=-1;
                my2=-1;
                do
                {
                    mx=mousex();
                    my=mousey();
                    if (mx2!=mx || my2!=my)
                    {
                        putimage(0,0,buffer2,0);
                        readimagefile("Kepek/Battle/Troops/Command_Center.GIF",mx-62,my-62,mx+62,my+62);
                    }
    
    This is the fragment that interests us:
    putimage(0,0,buffer2,0);
    readimagefile("Kepek/Battle/Troops/Command_Center.GIF",mx-62,my-62,mx+62,my+62)

    As you can see, every time I move the cursor, I redraw the the background and draw the airplane on it. This causes the pixels where the airplane is located to keep flashing (obviously, since it can't paint fast enough). I'd need a whole new way of doing this.
     
  11. Unread #6 - Apr 16, 2012 at 3:46 PM
  12. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    Hard Challenge - Moving images

    Copy the background image into a single buffer, then overwrite parts of that buffer with the airplane images. That way you will only have to call putimage on that buffer, and avoid the flashing problem.
     
  13. Unread #7 - Apr 17, 2012 at 6:13 AM
  14. lordzsolt
    Joined:
    Jul 1, 2008
    Posts:
    2,059
    Referrals:
    2
    Sythe Gold:
    2
    Two Factor Authentication User Halloween 2013

    lordzsolt Grand Master
    $5 USD Donor

    Hard Challenge - Moving images

    What what? Yeah, that sounds quite smart, though what command do you use to write on the buffer? Could you write that part of the code that does this?
     
  15. Unread #8 - Apr 17, 2012 at 6:24 PM
  16. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    Hard Challenge - Moving images

    Sorry I don't want to write code. However if it's WinBGIm you're using, then this might help:

    http://www.cs.colorado.edu/~main/bgi/doc/

    Alternatively you could try enabling double buffering by passing true for the dbflag parameter in your initwindow call, and calling swapbuffers() at the end of your main loop... I think that would also fix the problem.
     
  17. Unread #9 - Apr 17, 2012 at 7:00 PM
  18. lordzsolt
    Joined:
    Jul 1, 2008
    Posts:
    2,059
    Referrals:
    2
    Sythe Gold:
    2
    Two Factor Authentication User Halloween 2013

    lordzsolt Grand Master
    $5 USD Donor

    Hard Challenge - Moving images

    Ok, I've tried digging as deep as I can, but I still can't find which function to use for writing on the buffer.

    Correct me if I'm wrong, but even if I was to do double buffering, I'd still need to write on the second buffer.
     
  19. Unread #10 - Apr 17, 2012 at 8:53 PM
  20. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    Hard Challenge - Moving images

    I've never used these old graphics functions before, so I can't say for sure whether you can do what I was describing back there. However...

    If you enable double buffering, then I think the window will set up two internal buffers (these are unrelated to buffer1 and buffer2 in your own code).

    One of these internal buffers will be the one you draw everything onto. The other buffer is what is shown on the screen.

    If you look at the initwindow function in the documentation, you'll see that there are extra optional parameters.

    So, see this here, call it like this to enable double buffering:

    Code:
    initwindow(yourwidth, yourheight, "title", 0, 0, true, true)
    And call swapwindow() when you've done the drawing and want to display whatever on the screen.
     
  21. Unread #11 - Apr 18, 2012 at 11:21 AM
  22. lordzsolt
    Joined:
    Jul 1, 2008
    Posts:
    2,059
    Referrals:
    2
    Sythe Gold:
    2
    Two Factor Authentication User Halloween 2013

    lordzsolt Grand Master
    $5 USD Donor

    Hard Challenge - Moving images

    OMG YOU'RE AWESOME!!!

    It's working really well!

    Is there any way to turn off double buffering? Since I only need it a part of the program.

    I've though about doing it somewhat like this. swapbuffers () can be switched with
    Code:
    int oldv = getvisualpage( );
    int olda = getactivepage( ); 
    setvisualpage(olda); 
    setactivepage(oldv);
    So once I'm done with double buffering, I'll merge the visualpage and activepage. Then when I need to turn double buffering off, I'll split them. Do you think this could work? I'll be testing it in a few hours.
     
  23. Unread #12 - Apr 18, 2012 at 1:14 PM
  24. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    Hard Challenge - Moving images

    Awesome, so the flashing is gone? Glad to help :D

    I'd recommend using double buffering for all the drawing unless there's a reason not to. But see what works for you.
     
  25. Unread #13 - Apr 18, 2012 at 1:28 PM
  26. lordzsolt
    Joined:
    Jul 1, 2008
    Posts:
    2,059
    Referrals:
    2
    Sythe Gold:
    2
    Two Factor Authentication User Halloween 2013

    lordzsolt Grand Master
    $5 USD Donor

    Hard Challenge - Moving images


    OMG OMG OMG YOU'RE AWESOME!!!!!!!!!!!

    I managed to get it done and it works perfectly and it looks amazing!!!!!



    As for why I'd like to turn off double buffering, is that it turns to be useless after I'm done with this part, plus when I'm done with placing all the ships, I need to save the contects of a portion of the screen and print it to the new one (background when placing ships is different then when you're playing), and for some reason it messed up, and I felt like it's simpler to me this way (when I need double buffering, I just use two buffers, and when I don't, I just merg the active and visual pages while saving one of them for later use). But this doesn't make any difference in the way the viewer sees the game.


    Once again, you're mega awesome and thanks for sticking to me for nearly a week now.
     
  27. Unread #14 - Apr 18, 2012 at 3:42 PM
  28. lordzsolt
    Joined:
    Jul 1, 2008
    Posts:
    2,059
    Referrals:
    2
    Sythe Gold:
    2
    Two Factor Authentication User Halloween 2013

    lordzsolt Grand Master
    $5 USD Donor

    Hard Challenge - Moving images

    Updated question: Is there any way to clear those buffers?
     
  29. Unread #15 - Apr 20, 2012 at 3:23 PM
  30. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    Hard Challenge - Moving images

    No problem, this old graphics stuff is interesting.

    Looking at the documentation, I think either cleardevice() or clearviewport() will do that. You might need to set the colour to clear to by using setbkcolor() first.

    You would clear the offscreen buffer at the start of every new frame, before you begin drawing.
     
  31. Unread #16 - Apr 24, 2012 at 4:20 AM
  32. lordzsolt
    Joined:
    Jul 1, 2008
    Posts:
    2,059
    Referrals:
    2
    Sythe Gold:
    2
    Two Factor Authentication User Halloween 2013

    lordzsolt Grand Master
    $5 USD Donor

    Hard Challenge - Moving images

    Ok, decided to follow you advice and use two buffers during the whole program since I've found what the problem was.
     
< [source] Breakout(non-applet) in Java! 100% coded by me. | Free copy of Windows 8 for developers. >

Users viewing this thread
1 guest


 
 
Adblock breaks this site