2D Drawing Commands, Wont Work With Random() ?

Just starting out? Need help? Post your questions and find answers here.
Raine3D
New User
New User
Posts: 8
Joined: Tue Sep 23, 2003 2:36 am

2D Drawing Commands, Wont Work With Random() ?

Post by Raine3D »

Whenever I try to use Random() in any 2D drawing command, nothing happens. However when I replace it with a normal number, it works fine. I dont think its my code, but I'll post a short test snippet anyways.

Code: Select all


#Window = 0
Width.w = GetSystemMetrics_(SM_CXFullScreen)
Height.w =  GetSystemMetrics_(SM_CYFullScreen)
Title.s = "Drawing Test"

Global c1.w, c2.w, c3.w
Global x1.w, x2.w
Global w1.w, w2.w

Declare RedrawScene()
Declare Destroy()
Declare Randomize()

WindowID = OpenWindow( #Window, 0, 0, Width, Height, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered, Title )

If WindowID = 0
  MessageBox_(0, "Could not create window!", "Error", MB_OK)
  End
EndIf

RedrawScene()
Randomize()

Repeat
  Randomize()
  EventID.l = WaitWindowEvent()
  
  Select EventID
  
  Case #PB_Event_CloseWindow
    Quit = 1
   Case #PB_Event_Repaint
     RedrawScene()
  
  EndSelect
Until Quit = 1

Destroy()
End

Procedure RedrawScene()
  StartDrawing(WindowOutput())
      FrontColor(c1,c2,c3)
      Box(x1,x2,w1,h1)
  StopDrawing()   
EndProcedure

Procedure Randomize()
  c1 = Random(255)
  c2 = Random(255)
  c3 = Random(255)
  x1 = Random(Width)
  y1 = Random(Height)
  w1 = Random(Width)
  h1 = Random(Height)
EndProcedure

Procedure Destroy()
  CloseWindow(#Window)
EndProcedure

freak
PureBasic Team
PureBasic Team
Posts: 5946
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You have mixed up the variable names. You have defined other's global than those you use.

The global ones are named: x1, x2, w1, w2
However, in the random procedure, you use: x1,y1,w1,h1
And in the draw procedure, you use: x1,x2,w1,h1

You see, you didn't use the same name everywhere.
Also the 'Width' and 'Height' are not global at all, so in your random
procedure, they always contain 0 !

Another notice: You have forgotten the '#' for the constants
in GetSystemMetrics_(), so you always pass a variable with 0 to these functions.
So you don't get the right values there either.

Here's a working version of your code:

Code: Select all

Global Width.w, Height.w

#Window = 0
Width.w = GetSystemMetrics_(#SM_CXFullScreen)
Height.w =  GetSystemMetrics_(#SM_CYFullScreen)
Title.s = "Drawing Test"

Debug Width
Debug Height

Global c1.w, c2.w, c3.w
Global x1.w, y1.w
Global w1.w, h1.w

Declare RedrawScene()
Declare Destroy()
Declare Randomize()

WindowID = OpenWindow( #Window, 0, 0, Width, Height, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered, Title )

If WindowID = 0
  MessageBox_(0, "Could not create window!", "Error", MB_OK)
  End
EndIf

RedrawScene()
Randomize()

Repeat
  Randomize()
  EventID.l = WaitWindowEvent()
 
  Select EventID
 
  Case #PB_Event_CloseWindow
    Quit = 1
   Case #PB_Event_Repaint
     RedrawScene()
 
  EndSelect
Until Quit = 1

Destroy()
End

Procedure RedrawScene()
  StartDrawing(WindowOutput())
      FrontColor(c1,c2,c3)
      Box(x1,y1,w1,h1)
  StopDrawing()   
EndProcedure

Procedure Randomize()
  c1 = Random(255)
  c2 = Random(255)
  c3 = Random(255)
  x1 = Random(Width)
  y1 = Random(Height)
  w1 = Random(Width)
  h1 = Random(Height)
EndProcedure

Procedure Destroy()
  CloseWindow(#Window)
EndProcedure 
As it is no bug, i have moved the topic..

Timo
quidquid Latine dictum sit altum videtur
Raine3D
New User
New User
Posts: 8
Joined: Tue Sep 23, 2003 2:36 am

Post by Raine3D »

Thank you.
Post Reply