Fast Rotate Image (Wheel of Fortune) [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Fast Rotate Image (Wheel of Fortune) [Windows]

Post 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
Last edited by RASHAD on Sun Jun 30, 2013 3:05 am, edited 4 times in total.
Egypt my love
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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.
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post by Fred »

It's because the event loop isn't correct.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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

Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

First post updated
Tested with PB 5.11 Win 7 x64 XP SP2 x86
Egypt my love
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

First post updated again
Egypt my love
Zach
Addict
Addict
Posts: 1676
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

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

Post by Zach »

You are too modest, you have provided outstanding code to members of this community on multiple occasions.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

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

Post 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
Post Reply