Page 1 of 1

Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sat Jun 29, 2013 2:39 pm
by RASHAD
Hi

Code: Select all

; Image Rotating Code
; by RASHAD
; Compiler: PB 5.11

UsePNGImageDecoder()
UsePNGImageEncoder()
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UseJPEG2000ImageEncoder()
UseJPEG2000ImageDecoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

If FileSize(GetTemporaryDirectory()+"wheel.png") = -1
    InitNetwork()
    If Not ReceiveHTTPFile("http://img6.imageshack.us/img6/4648/513pxwheeloffortuneseas.png", GetTemporaryDirectory()+"wheel.png")
      MessageRequester("Error","Downloading error....",#MB_ICONERROR) 
    EndIf
EndIf

LoadImage(0,GetTemporaryDirectory()+"wheel.png")

Global imgw
Global Dim p.Point(2)

Procedure Image_Rotate(Image,Angle)
  
  Sin.f = Sin(Radian(-Angle))
  Cos.f = Cos(Radian(-Angle))
  height=513
  width=513
  imgw = Abs(height*Sin(Radian(-45)))+Abs(width*Cos(Radian(-45)))
  Center = imgw / 2
     
   p(0)\x=Center-0.5*Width*Cos-0.5*Height*Sin
   p(0)\y=Center+0.5*Width*Sin-0.5*Height*Cos
   p(1)\x=Center+0.5*Width*Cos-0.5*Height*Sin
   p(1)\y=Center-0.5*Width*Sin-0.5*Height*Cos
   p(2)\x=Center-0.5*Width*Cos+0.5*Height*Sin
   p(2)\y=Center+0.5*Width*Sin+0.5*Height*Cos

  temp=CreateImage(#PB_Any,imgw,imgw)

  ImageDc = CreateCompatibleDC_(0)
  SelectObject_(ImageDc,ImageID(Image))
  dc=StartDrawing(ImageOutput(temp))
    PlgBlt_(dc,@p(),ImageDc,0,0,width,height,0,0,0)
  StopDrawing()
  CopyImage(temp,image)
  DeleteDC_(ImageDc)
  FreeImage(temp)
EndProcedure


If OpenWindow(0,0,0,800,840,"Wheel of Fortune",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  ImageGadget(1,40,40,780,780,0)
  ButtonGadget(2,305,800,80,22,"Spin")
  ButtonGadget(3,395,800,80,22,"Stop")
  
  CopyImage(0,1)
  Image_Rotate(1,0)
  SetGadgetState(1,ImageID(1))
  Y = 10
  
  AddWindowTimer(0, 100, 10)
Repeat
    Delay(1)
    Select WaitWindowEvent()
     Case #PB_Event_CloseWindow
          Quit = 1
     Case #PB_Event_Timer
          If Y < 10
             x+1+y
             CopyImage(0,1)
             Image_Rotate(1,x)
             SetGadgetState(1,ImageID(1))    
             ;SetGadgetAttribute(1, #PB_Button_Image, ImageID(1))
            If x >= 360
               x = 0
               y + 1
            EndIf
          EndIf
     Case #PB_Event_Gadget
           Select EventGadget()
             Case 2
                   x = 0
                   y = 0

             Case 3
                  y = 10

           EndSelect
    EndSelect

Until Quit = 1
 
EndIf

Edit :Tested with XP SP2 x86
Bugs fixed

Edit 2 : Much better

Edit 3 : Much better

Re: Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sat Jun 29, 2013 3:19 pm
by Demivec
When I run the code, wait five seconds and then click anywhere in the window or on the title bar the window turns white and the program doesn't seem to respond at all. I can't close the program by clicking the close button either.

I have to terminate the program another way. I am using Windows XP x86.

Nice wheel image. The drawing is fast but I get a lot of flickering in addition to the problem already described.

Re: Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sat Jun 29, 2013 3:21 pm
by Fred
It's because the event loop isn't correct.

Re: Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sat Jun 29, 2013 3:28 pm
by Demivec
Fred wrote:It's because the event loop isn't correct.
True, but I couldn't find a variation that solved the problem.

I tried removing WindowEvent() because there was already a WaitWindowEvent() at the end of the loop also.
I then tried specifying 10ms in the WaitWindowEvent() call.

I then tried:

Code: Select all

Repeat
  
    For Turn = 1 To 50
      For x = 0 To 360 Step 10
         CopyImage(0,1)
         Image_Rotate(1,x)
         SetGadgetAttribute(1, #PB_Button_Image, ImageID(1))
      Next
    Next
    Repeat
      event = WindowEvent()
    Until event = 0 Or event = #PB_Event_CloseWindow
    Delay(10)
Until event=#PB_Event_CloseWindow
It still didn't help or change the way the program reacted.

Re: Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sat Jun 29, 2013 3:47 pm
by RASHAD
Sorry guys :mrgreen:
I know that the loop not OK even with me here Win 7 x64
I did the code to rotate any image at any Angle
In the last minute I decided to do it for The Wheel of Fortune (I was bored at that time wanted to get rid of it) :P
I will try to correct it soon(Unless one of you did it first) :P
@Demivec look at the bright part you can rotate from slow to fast to very fast :)
Sorry mate

The original code :

Code: Select all

; Image Rotating Code
; by RASHAD
; Compiler: PB 5.11

UsePNGImageDecoder()
UsePNGImageEncoder()
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UseJPEG2000ImageEncoder()
UseJPEG2000ImageDecoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

Global imgw,Center

Procedure Image_Rotate(Image,Angle)
  Protected Cos.f = Cos(Radian(-Angle))
  Protected Sin.f = Sin(Radian(-Angle))
  height=ImageHeight(Image)
  width=ImageWidth(Image)
  imgw = Abs(height*Sin(Radian(-45)))+Abs(width*Cos(Radian(-45)))
  ;imgh = Abs(height*Cos(Radian(-Angle)))+Abs(width*Sin(Radian(-Angle)))

  ;If imgw > imgh
     Center = imgw/2
  ;Else
     ;Center = imgh/2
  ;EndIf

     Dim p.Point(2)
     p(0)\x=Center-0.5*Width*Cos-0.5*Height*Sin
     p(0)\y=Center+0.5*Width*Sin-0.5*Height*Cos
     p(1)\x=Center+0.5*Width*Cos-0.5*Height*Sin
     p(1)\y=Center-0.5*Width*Sin-0.5*Height*Cos
     p(2)\x=Center-0.5*Width*Cos+0.5*Height*Sin
     p(2)\y=Center+0.5*Width*Sin+0.5*Height*Cos

  temp=CreateImage(#PB_Any,imgw,imgw)

  ImageDc = CreateCompatibleDC_(0)
  SelectObject_(ImageDc,ImageID(Image))
  dc=StartDrawing(ImageOutput(temp))
    ;Box(0,0,Center*2,Center*2,#gray)
    PlgBlt_(dc,@p(),ImageDc,0,0,width,height,0,0,0)
  StopDrawing()
  CopyImage(temp,image)
  FreeImage(temp)
EndProcedure


LoadImage(1,"g:\Image12.bmp")

Image_Rotate(1,60)

;w = Center*2

If OpenWindow(0,0,0,imgw+20,imgw+20,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ContainerGadget(0,10,10,imgw,imgw)
  ButtonImageGadget(1,0,0,imgw,imgw,ImageID(1))
  SetWindowTheme_(GadgetID(1), @null.w, @null.w)
CloseGadgetList()
DisableGadget(0,1)
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf


Re: Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sat Jun 29, 2013 3:55 pm
by RASHAD
First post updated
Tested with PB 5.11 Win 7 x64 XP SP2 x86

Re: Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sat Jun 29, 2013 4:52 pm
by Demivec
RASHAD wrote:@Demivec look at the bright part you can rotate from slow to fast to very fast
Sorry mate
I know you are knowledgeable of many of window's in's and out's and I was just hoping to see what you had come up with. Instead I only got to see a hint of the upcoming show. :wink:

I have poked and prodded the code a little but haven't come up with anything fruitful yet.

Re: Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sat Jun 29, 2013 5:05 pm
by RASHAD
Hi Demevic
About knowledgeable of many of window's in's and out's that is not true
I only have my own share which is not much
Anyway thanks mate
I tried hard to post the image as data until i got mad without any success
I will try to run the code on a real XP to see whats going on
Be tuned

Re: Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sat Jun 29, 2013 6:41 pm
by RASHAD
First post updated again

Re: Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sat Jun 29, 2013 7:59 pm
by Zach
You are too modest, you have provided outstanding code to members of this community on multiple occasions.

Re: Fast Rotate Image (Wheel of Fortune) [Windows]

Posted: Sun Jun 30, 2013 12:45 pm
by yrreti
post by Zach
You are too modest, you have provided outstanding code to members of this community on multiple occasions.
I sure second that! :wink:

yrreti