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

Just starting out? Need help? Post your questions and find answers here.
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

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

Post 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 ^_^
hardfalcon
User
User
Posts: 89
Joined: Fri Apr 29, 2005 3:03 pm
Location: Luxembourg
Contact:

Post by hardfalcon »

You forgot FlipBuffers() in the main loop... :wink:
"And God caused a deep sleep to fall upon Adam, and he slept: and he took one of his ribs, and closed up the flesh instead thereof; And the spare rib, which God had taken from man, made he a woman, and brought her unto the man"
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

Post 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^
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Post 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
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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...
oh... and have a nice day.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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. :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

Post 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! ^_^
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post by #NULL »

PB uses Short-circuit evaluation
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
hardfalcon
User
User
Posts: 89
Joined: Fri Apr 29, 2005 3:03 pm
Location: Luxembourg
Contact:

Post 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.
"And God caused a deep sleep to fall upon Adam, and he slept: and he took one of his ribs, and closed up the flesh instead thereof; And the spare rib, which God had taken from man, made he a woman, and brought her unto the man"
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

Post by Irene »

It was PB who confused me... anyway thanks for the lesson Trond! ^_^
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post 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)
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Hmmm...

I wish I would get this great amount of support for such a simple problem... Image
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Post Reply