Newbie needs Help! Flipping!

BlitzPlus Forums/BlitzPlus Beginners Area/Newbie needs Help! Flipping!

diceman(Posted 2005) [#1]
Hey guys.
Just started to mess a little with the BlitzPlus Editor, and I really dig the concept. However, I've got a slight problem with the whole Buffer-Stuff. I want to draw in black color on a white background by clicking the left mouse-button, but instead the screen flips always between black and white with each mouse-click. Anyone care looking at my code and help me fixing it? It's simple, just meant to be a test to grasp the concept of buffering, but am I to program more complex stuff, if I fail even at a "simple" thing like this? ;)
Thanks in advance.

Graphics 800,600
ClsColor 255,255,255
Cls
Flip
Color 0,0,0
While Not KeyHit(1)
button=GetMouse()
If button=1
Plot MouseX(),MouseY()
Flip
EndIf
Wend


CS_TBL(Posted 2005) [#2]
add this:

SetBuffer BackBuffer()


after the Graphics command..

hmm.. on a second view:

when you want to make a paint-toolie, rather paint on an image-buffer.. and DrawBlock that image-buffer to the Backbuffer.. then flip.

so, your loop is like (pseudo :)

repeat
setbuffer imagebuffer(MyPainting)
; do your drawcommands/mouse here
setbuffer backbuffer()
drawblock MyPainting,0,0
flip
until you pressed escape


Do try to learn about event-style programming.. you WANT that!


CS_TBL(Posted 2005) [#3]
I'll get back on it with a nice tut. need to reboot because I forgot a "setbuffer desktopbuffer()" upon which blitz starts to complain about everything, and refuses to run any blitz app.. (compiled .exe's included!)

so stick this in your head: "after you're done with imagebuffers, use desktopbuffers!"


CS_TBL(Posted 2005) [#4]
observe, study, learn, practice, gain!
app=CreateWindow("designer",0,0,640,480,0,1) ; create the window

canvas=CreateCanvas(32,32,512,320,app) ; create the canvas on the window

img=CreateImage(512,320) ; create an image


Repeat

	WaitEvent() ; wait for events
	If EventID()=$803 quit=True ; alt-f4 pressed? make 'quit' true
	
	
	If EventSource()=canvas ; we doin' something on the canvas?
	
		If EventID()=$201 ; mouse-down?
			lmd=True
		EndIf
		
		If EventID()=$202 ; mouse-up?
			lmd=False
		EndIf
		
		If lmd ; while the mouse is down
			ex=EventX() ; x+y coords from the canvas! (not the screen!)
			ey=EventY() ;
			
			SetBuffer ImageBuffer(img) ; make sure we draw to the imagebuffer
				Color Rnd(255),Rnd(255),Rnd(255)
				Text ex,ey,"mouse!"
			SetBuffer CanvasBuffer(canvas) ; and draw that image to the canvas
				Cls
				DrawBlock img,0,0  				;DrawBlock img,-ex/2,-ey/2     ; <--- try this for some creepy effect
			FlipCanvas canvas ; flip the canvas (or else you'll be watching @ nothing)
		EndIf

	EndIf
	
	
Until quit ; so, repeat until quit = true


; make sure you add this, since you could press alt-f4 before the current buffer was set to the
; desktopbuffer
SetBuffer DesktopBuffer()
                           


; place to clean-up things,
FreeImage img ; like this..

End


*update*
- got rid of the setbuffers within the $201 and $202 checks, they were useless .. :)
- indented the stuff within a buffer-scope .. =personal style .. but I recommend it to everyone..


Grey Alien(Posted 2005) [#5]
Diceman, in Blitz you use double buffering (i.e. there are two buffers and whilst one is showing you draw on the other one.) Your code first fills the screen (BackBuffer) with white and then calls flip. This shows the white screen. The current drawing buffer is now a blank black one. You wait for a mouse click and draw a black pixel on a black background and call flip again. This shows the black screen and now the white screen becomes your drawing buffer. Then when you click the mouse you get a black dot on the white background and flip will show this frame and so on.

A quick solution is to draw everything twice (once on each buffer) but this is a bit lame (I have added 2 new lines to your code).

Graphics 800,600 
ClsColor 255,255,255
Cls
Flip
Cls ;new code
Color 0,0,0
While Not KeyHit(1) 
button=GetMouse()
If button=1
Plot MouseX(),MouseY() 
Flip
Plot MouseX(),MouseY() ;new code
EndIf
Wend 


Really you should be drawing to an Image (in the memory) instead and then draw this image onto the current buffer before flipping it (as per CS_TBL's code). Then you only have to draw enverything once and it will be easy to overlay a mouse cursor. Hope you get it.


Berserker [swe](Posted 2005) [#6]
am I to program more complex stuff, if I fail even at a "simple" thing like this? ;)



Diceman you should never feel like that! The fact that you want to learn is more then many people can say for them selfs.

I have wanted to learn programming for a couple of years back and i learn something new everytime i browse theese forums. I wouldnt say that im any good i would still say im a newbie after two years of Blitz3D and 4years total with Blitz* BUT im proud to say that "i want to learn".
And in programming only youre fantasi is the limit. And sometimes the money =)

Just keep on the good spirit and dont give up and you'll have youre own game in notime.

And read some of the contents on this site for further encouragement (dont know the word)
http://www-cs-students.stanford.edu/~amitp/gameprog.html


diceman(Posted 2005) [#7]
Thanks for all the backup and useful tips. It seems like I haven't grasped the concept of buffering completely, but for now it's great to know that there's something like DesktopBuffering, too.

Now, can anyone help me, how to post Blitz-Code up here, that the formatting isn't all screwed up and how to use colors in the forum? Thanks.


CS_TBL(Posted 2005) [#8]
for small sources:

<code>
</code>

or for bigger sources:

<codebox>
</codebox>

and replace all the <> with []