Drawing function query

BlitzPlus Forums/BlitzPlus Beginners Area/Drawing function query

Siopses(Posted 2007) [#1]
I made this "Dibujo(guibank)" function for use in a paint program I am making. My question is to why it won't work.
Please only explain what is wrong, not how to fix it... because
it will be a vital part in a program I'm making for the Blitz Showcase, and I want it to be ENTIRELY my coding.
Thanks,
Siopses
;GUI

;Windows
window=CreateWindow("MICHAELSOFT DIBUJO",300,400,555,555,window,15)

;Canvases
canvas=CreateCanvas(0,0,430,450,window)

;Menu
menu=WindowMenu(window)
File=CreateMenu("FILE",1,menu)
CreateMenu("END",2,File)
CreateMenu("Clear",3,File)

UpdateWindowMenu window

;Buttons
draw=CreateButton("DRAW",450,150,50,25,window,2)

canvasx=430
canvasy=450
guibank=CreateBank(24)
PokeInt guibank,0,window
PokeInt guibank,4,canvas
PokeInt guibank,8,File
PokeInt guibank,12,draw
PokeInt guibank,16,canvasx
PokeInt guibank,20,canvasy

.begin

Repeat
	WaitEvent()
	Dibujo(guibank)
Until EventID()=$803
AppTitle("Quit?")
blah=Proceed("Are you sure"+Chr(10)+"you want to quit?")
	If blah=1 Then
		End
			Else
				If blah<>1 Then
					Goto begin
				EndIf
	EndIf

Function Dibujo(guibank)
	canvas=PeekInt(guibank,4)
	File=PeekInt(guibank,8)
	draw=PeekInt(guibank,12)
	canvasx=PeekInt(guibank,16)
	canvasy=PeekInt(guibank,20)
		SetBuffer CanvasBuffer(canvas)
			Select EventID()
				Case $203
					If MouseX>canvasx 
						If MouseY>canvasy
							If ButtonState(draw)=True Then
								Plot MouseX,MouseY
									FlipCanvas canvas
							EndIf
						EndIf 
					EndIf 
			End Select
End Function 



Matty(Posted 2007) [#2]
It should be MouseX() and MouseY() not just mousex and mousey. Can't tell what else is wrong off the top of my head, without blitzplus directly in front of me.


H&K(Posted 2007) [#3]
(Not whats wrong but)
whats this?
If blah=1 Then
	End
Else
        If blah<>1 Then....
If the blah=1 fails then why test for blah<>1?

Repeat

        Repeat
	        WaitEvent()
	        Dibujo(guibank)
        Until EventID()=$803

        AppTitle("Quit?")
        blah=Proceed("Are you sure"+Chr(10)+"you want to quit?")

        If blah=1 Then End
Forever


1) You are still doing the indentation wrong, the elses should be below the ifs they belong to , (as should for/next repeat/until do/loop and while/wend)

2)If you can avoid useing Goto, then avoid it. (Im not sure if it matters but you were jumping out of an if statment, and I dont know what that would endup making the program think)

Just indentation correction
Function Dibujo(guibank)

	canvas=PeekInt(guibank,4)
	File=PeekInt(guibank,8)
	draw=PeekInt(guibank,12)
	canvasx=PeekInt(guibank,16)
	canvasy=PeekInt(guibank,20)
	SetBuffer CanvasBuffer(canvas)

	Select EventID()
	Case $203
		If MouseX>canvasx And MouseY>canvasy and ButtonState(draw)=True 
			Plot MouseX,MouseY
			FlipCanvas canvas
		EndIf 
	End Select
End Function
As you can see there is a commad AND which means If X is true AND If y is True AND if z is true, can be written as If X is true AND y is True AND z is true, without the need for Ifs all over the place

(Ps I dont know whats wrong, cos I dont have BPlus ;)


CS_TBL(Posted 2007) [#4]
I've told the thing about the indenting zillions of times, and I've given an elegant one-line 'areyousure'-end routine, but I think siopses keeps c/p'ing old code without improving, therefor each time the same mistakes pop up. I think siopses doesn't *know* what to indent, he roughly *estimates* what to indent.


Rob Farley(Posted 2007) [#5]
http://www.blitzbasic.com/codearcs/codearcs.php?code=1229


Siopses(Posted 2007) [#6]
What's the matter with my indenting? I'm connecting the
scope's right? For example:
AppTitle("Quit?")
blah=Proceed("Are you sure"+Chr(10)+"you want to quit?")
	If blah=1 Then
	:	End
	:		Else
	:			If blah<>1 Then
	:			:	Goto begin
	:			EndIf
	EndIf

I'm still curious as to why it won't draw, I'm not going
to copy one of the thing's in the Samples section of
Blitz Plus.


CS_TBL(Posted 2007) [#7]
"Else" is not a scope, but an additional command of the IF statement.

So, correct indenting is:
If
    [..]
ElseIf
    [..]
Else
    [..]
EndIf


Incorrect indenting is:
If
    [..]
    ElseIf
        [..]
    Else
        [..]
EndIf


Not to mention:

If
    [..]
        ElseIf
           [..]
        Else
            [..]
EndIf



CS_TBL(Posted 2007) [#8]
additionally: -just as I mentioned in the doubleclick dilemma thread:

Put this in your main loop, and replace the "Until EventID()=$803" with "Forever":

	If EventID()=$803
		If Proceed("Are you sure"+Chr(10)+"you want to quit?") End
	EndIf



CS_TBL(Posted 2007) [#9]
btw, it's bad practice to close a mainloop/program purely on one simple event ID. Therefor I recommend against these constructions like "Until EventID()=$803". I recommend a normal repeatforever-loop and check inside the loop whether one should exit the loop. I know it's sometimes mentioned like
"Until EventID()=$803" in the manual, but that manual isn't good on *all* points. Its main flaw is lack of consistency.


Siopses(Posted 2007) [#10]
Ok so now, that that's solved then do you know what's wrong with my drawing function?


CS_TBL(Posted 2007) [#11]
Function Dibujo(guibank)
	canvas=PeekInt(guibank,4)
	draw=PeekInt(guibank,12)
	
	Select EventID()
	
		Case $203
			If ButtonState(draw)
				SetBuffer CanvasBuffer(canvas)
					Plot EventX(),EventY()
				FlipCanvas canvas
			EndIf
			
	End Select
End Function 


As you see, I wiped half the function contents. Also, I replaced MouseX and MouseY with EventX and EventY. And I narrowed down the opened canvasbuffer to strictly the area where it's needed.

I wouldn't base a designtool on this structure tho. Correct way is to draw into an image buffer and draw this imagebuffer onto a canvas.
Additionally, you want to use the events $201 and $202 for mousedown and mouseup rather than a draw checkbox.


Siopses(Posted 2007) [#12]
Thanks, but I only wanted to know the problem... Not how
to fix it. Looks like I won't be posting anything in Blitz Showcase now-Project terminated.

Please only explain what is wrong, not how to fix it... because
it will be a vital part in a program I'm making for the Blitz Showcase, and I want it to be ENTIRELY my coding.


I probably could have figured it out with a few more tests,
but it doesn't matter now, anymore.


CS_TBL(Posted 2007) [#13]
Ok. But sometimes you do need a few examples of how things are done, otherwise you'll learn it the wrong way.


Siopses(Posted 2007) [#14]
I'm not as bad as I was before I'm a GUI conneiseur, but GUI
is not really that much of a way to do game stuff, correct. To
think of it I'm going to make another program... Without the
forum's help. I don't want this to happen again by anyone,
so its nessecary to learn on my own- even if it takes longer.


CS_TBL(Posted 2007) [#15]
Don't be afraid to learn something from someone tho. In the end everyone's using everyone's code. Even if you learn from the manual, you're still learning from the ones who wrote those examples.


Siopses(Posted 2007) [#16]
I know but if you experiment on your own, and learn from
trial and error you'll learn the best. Experience is the best
teacher.


Who was John Galt?(Posted 2007) [#17]
Come on Siopses... that's no reason to throw the towel in on your showcase. He was only trying to help. Besides, this is only a minor bit of your code. It's the final functionality that counts.


H&K(Posted 2007) [#18]
I know but if you experiment on your own, and learn from trial and error you'll learn the best. Experience is the best teacher
Wrong.
Someone experianced who is willing to teach, is the best teacher.

I learnt, by reading the listings in Magazines back in the 80's. And so didnt make the mistakes you are makeing, because they didnt accor to me. Sometimes there are lots of right ways to do something, but equally there are lots and lots and lots of wrong ways to do something.

Without the corrections that we are giving, you would still be quite able to learn to program, but you almost certainly would not have learnt to program in one of the "correct" ways.

What I would suddject if you really want to learn by yourself, is to print out several of the example programs that come with B3d. And the type them in again, but when ever you get to something you dont understand, either look it up, (Or ask if you must), either way dont continue until you understand it.

Then as you do this, save in a seperate place any functions that you like the look of, and make your own "Libary" of functions. Then when you start to program your own program you should concider these functions "Yours".

@cs_tbl
Im in two minds about the indent for CASE, when there was an endcase, then yes indent it. ATM tho without and end, I tend not to Indent case, and keepit at the smae level as select. (Probably not right, but I do it anyway)


Siopses(Posted 2007) [#19]
To Nomen luni I will If you still understand that MOST of the
coding is mine.
{EDIT}
I already removed the code from the memory, so I will have
to start from scratch. But I'll start it over.


CS_TBL(Posted 2007) [#20]
H&K: you mean:

Select action
  Case what
    Singasong()
  Case who
    You()
End Select


vs

Select action
Case what
  Singasong()
Case who
  You()
End Select

?


CS_TBL(Posted 2007) [#21]
btw, siopses could spend some while on games. We'll see him back here once he needs to create a tailormade mapeditor or whatever game-editor.. :-)


Siopses(Posted 2007) [#22]
I'm not going to do that though. I already had a project going but it was shot down by you, so now I'm making a
different one- without this forums help.


CS_TBL(Posted 2007) [#23]
Well, suit yourself I'd say. As long as you keep in mind that even the best coders here on this forum dare to ask questions if they're stuck.

What's wisdom.. ask the road to someone and go on, or wander around for days while spoiling fuel and energy..?


Amon(Posted 2007) [#24]
Just thought I'd pop in and say that if I hadn't spammed the beginner forums with questions and 'How do I do's' I would be struggling more.

People are helpfull here so it's best to just ask if you're stuck.

8 accounts ago (my first account) I spammed the forums with questions on how to make platform games. People were more than happy to help. Plus when you see how code works you begin to understand why it works; instead of this trail and error ways you want to code.


H&K(Posted 2007) [#25]
@Cs
Select action

Case what
    Singasong()

Case who
    You()

End Select
With the NLs, I can see why ppl would say it was wrong, but I dont like the double backTab fo EndSelect

To Nomen luni I will If you still understand that MOST of the
coding is mine.
{EDIT}
I already removed the code from the memory, so I will have
to start from scratch. But I'll start it over.
If you are really really going to be this small about help, then I will refrain from helping you from now on. And I would recomend that everyone else does the same.
If on the other hand you are open to changing your mind, pick someone you think knows what they are doing, and look at there early posts (Click on there name in any of their posts)


CS_TBL(Posted 2007) [#26]
H&K, taste I think.. I usually tend to mix-up those a bit anyway, depending on the current day of the week.. :-)


Siopses(Posted 2007) [#27]
To H&K... Come on are you saying that youv'e never wanted
to code something entirely by yourself.


H&K(Posted 2007) [#28]
No Im not, Im saying that when someone (after asking), told me the answer to a problem I had.
I didnt go
"nananananan nanananan nananannananannana not listenin' nanananannanannanana nanannanananan nananananannananananan"
I said thankyou, and either used or ignored their advice


CS_TBL(Posted 2007) [#29]
In this particular case there was so much wrong with that function that fixing the code would've been less work than writing an essay on all the details.

Function Dibujo(guibank)
	canvas=PeekInt(guibank,4)
	File=PeekInt(guibank,8)   <- not needed
	draw=PeekInt(guibank,12)
	canvasx=PeekInt(guibank,16) <- not needed
	canvasy=PeekInt(guibank,20) <- not needed
		SetBuffer CanvasBuffer(canvas) <- wrong place, tad hard to explain now, but you opened a drawingbuffer while you weren't sure you'd be closing it (flipcanvas was in an IF statement) and in B+ you should *never* leave a canvasbuffer open upon quit.
			Select EventID()
				Case $203
					If MouseX>canvasx  <- not needed
						If MouseY>canvasy  <- not needed
							If ButtonState(draw)=True Then
								Plot MouseX,MouseY  <- had to be eventx() and eventy()
									FlipCanvas canvas
							EndIf
						EndIf  <- not needed
					EndIf  <- not needed
			End Select
End Function 


So, as you see, there was really a lot to fix in there. Most of it could just be wiped away. In this case it was the easiest way to just present a finished function rather than a whole story related to each line which is now trailed by a <-

As was said before: it's just a tiny function that does nothing special or highly advanced, there enough to find out yourself. It's the functionality, not the implementation.


Siopses(Posted 2007) [#30]
Ok fine, CS_TBL is right anyway he's helped me despite my
ignorance on a wide variety of subjects:
Indenting
Code Boxs
GUI
Misc. Coding
Banks (owe him lot's for this one)
etc.
So I'm going to throw in the towel, bury the hatchet, whatever you want. I'm just saying I made it clear, I really
wanted this project to be all my coding... I'm depressed
and I'm sorry for being a jackass