Page 1 of 2

Needing a very basic example for drawing on a screen..

Posted: Tue Oct 09, 2007 8:54 am
by Irene
Hello!

Since last night I was tinkering with some code and couldn't even make a simple application to draw a white circle on the screen... Here's my current code, please direct me to any mistakes:

Code: Select all

If InitKeyboard() = 0 And InitMouse() = 0
 MessageRequester("ERROR", "DirectX/SDL could not be properly initialized. Please check your installation.")
 End
Else
 If OpenScreen(640, 480, 32, "SCREEN") = 0
 MessageRequester("ERROR", "An error occured while opening a 32-bit 640x480 screen. This should not happen.")
  End
 Else
  Repeat
   ExamineKeyboard()
   StartDrawing(ScreenOutput())
    Circle(200, 200, 100, RGB(255, 255, 255))
   StopDrawing()
  Until KeyboardPushed(#PB_Key_Escape)
 EndIf
EndIf
Thank you guys ^_^

Posted: Tue Oct 09, 2007 8:59 am
by hardfalcon
You forgot FlipBuffers() in the main loop... :wink:

Posted: Tue Oct 09, 2007 9:13 am
by Irene
hardfalcon wrote:You forgot FlipBuffers() in the main loop... :wink:
Oh this is a little embarrassing... I should have known that ^_^

Thanks now it works! ^o^

Posted: Tue Oct 09, 2007 12:19 pm
by Baldrick
A little mistake I notice in your code here is where you call the Init..
You are calling as AND, so all need to fail for an error, but in reality, if 1 fails you have a problem.

Code: Select all


; changed your inits to OR so only 1 needs to fail to end program, else you may end up with 
; app fall down, go BOOM  - InitMouse() is not required for this sample btw.. :)
If InitKeyboard() = 0 Or InitMouse() = 0 Or InitSprite()=0
 MessageRequester("ERROR", "DirectX/SDL could not be properly initialized. Please check your installation.") 
 End 
  Else  
   If OpenScreen(640, 480, 32, "SCREEN") = 0 
    MessageRequester("ERROR", "An error occured while opening a 32-bit 640x480 screen. This should not happen.") 
    End 
     Else 
      Repeat 
       a+b       
        If a>100 
         b=-1 
        EndIf 
        If a<1 
         color=Random(RGB(255, 255, 255)) 
         b=1
        EndIf    
       ; dont forget to clear your screen before drawing on it again
       ClearScreen(#Black) ;comment this out & you will see why
       ExamineKeyboard() 
       StartDrawing(ScreenOutput()) 
       Circle(200, 200, a, color) 
       StopDrawing() 
       FlipBuffers() 
      Until KeyboardPushed(#PB_Key_Escape) 
  EndIf 
EndIf

Posted: Tue Oct 09, 2007 1:33 pm
by Kaeru Gaman
personally I prefer closed short ifs, no long-range iffing around the complete code.

Code: Select all

;************************************
;*** INITS
;************************************

If Not InitSprite()
  MessageRequester("Error","No DX, man!")
  End
EndIf

If Not InitMouse()
  MessageRequester("Error","No Mouse, man!")
  End
EndIf

If Not InitKeyboard()
  MessageRequester("Error","No Keys, man!")
  End
EndIf

If Not OpenScreen(640, 480, 32, "SCREEN")
  MessageRequester("Error","No Screen, man!")
  End
EndIf

;************************************
;*** MAIN
;************************************
so, Irene, except the "And" your code is fine, just leave out the "Else" and put the "EndIf" right there...

Posted: Tue Oct 09, 2007 1:57 pm
by PB
@Kaeru Gaman: I do all my error-checking at the start too, but I compress
it all down into one small block. For your example, I'd just do it like this:

Code: Select all

If Not InitSprite() Or Not InitMouse() Or Not InitKeyboard() Or Not OpenScreen(640, 480, 32, "SCREEN")
  MessageRequester("Error","App couldn't start, please try again.")
  End
EndIf
I know this doesn't explain exactly WHY it couldn't start, but I don't care for specifics. :)

Posted: Tue Oct 09, 2007 2:25 pm
by Trond
PB wrote:@Kaeru Gaman: I do all my error-checking at the start too, but I compress
it all down into one small block. For your example, I'd just do it like this:

Code: Select all

If Not InitSprite() Or Not InitMouse() Or Not InitKeyboard() Or Not OpenScreen(640, 480, 32, "SCREEN")
  MessageRequester("Error","App couldn't start, please try again.")
  End
EndIf
I know this doesn't explain exactly WHY it couldn't start, but I don't care for specifics. :)
Why not

Code: Select all

If Not (InitSprite() And InitMouse() And InitKeyboard() And OpenScreen(640, 480, 32, "SCREEN"))
  MessageRequester("Error","App couldn't start, please try again.") 
  End 
EndIf

Posted: Tue Oct 09, 2007 3:21 pm
by Irene
Thank you guys, very helpful! PS: Trond isn't it better to use Or in your example? I'm a bit confused right now.. thanks! ^_^

Posted: Tue Oct 09, 2007 3:34 pm
by #NULL
PB uses Short-circuit evaluation

Posted: Tue Oct 09, 2007 3:36 pm
by Trond
Irene wrote:Thank you guys, very helpful! PS: Trond isn't it better to use Or in your example? I'm a bit confused right now.. thanks! ^_^
No, it should be And. Because all of them should return true. And if Not all of them returns true, display the message.

Posted: Tue Oct 09, 2007 3:42 pm
by hardfalcon
Irene wrote:Thank you guys, very helpful! PS: Trond isn't it better to use Or in your example? I'm a bit confused right now.. thanks! ^_^
Simply check it like this:

Code: Select all

If Not (0 And 0 And 1 And 1)
  Debug "Error"
Else
  Debug "No errors"
EndIf
You could test with any combination of 0 and 1.

Posted: Tue Oct 09, 2007 4:03 pm
by Kaeru Gaman
to make it easier:

Code: Select all

For n=0 To 15
  ;If Not ((n&1) And (n&2) And (n&4) And (n&8))
  If Not (n&1) Or  Not (n&2) Or Not (n&4) Or Not (n&8)
    Debug Str(n)+ "-> Error"
  Else
    Debug Str(n)+ "-> No errors"
  EndIf
Next
as you can see, the And version works properly, the OR version does not.

after all, I better like seperate iffing.
when InitSprite() fails, an OpenScreen() will not just attempted, wich clearly makes sense.

Posted: Tue Oct 09, 2007 5:30 pm
by Irene
It was PB who confused me... anyway thanks for the lesson Trond! ^_^

Posted: Tue Oct 09, 2007 6:13 pm
by Ollivier
There's the macro too to start and check...

Code: Select all

Macro Init(a, b = Init, c = ")

      If b#a() = 0
      
            MessageRequester("Message", "DirectX error !" + Chr(10) + c#a can't be initialized !")
            End
      
      EndIf

EndMacro



      Init(Sprite)
      Init(Mouse)
      Init(Keyboard)

Posted: Tue Oct 09, 2007 6:18 pm
by Fluid Byte
Hmmm...

I wish I would get this great amount of support for such a simple problem... Image