Hello spongehammer,
IMHO to answer well your question, first you may have a look at Code Archive (
http://www.purearea.net/pb/CodeArchiv/CodeArchiv.html) where you will find hundreds of code advanced samples.
Most of the time, I noticed that PB users are pretty good coders doing a neat coding.
My own way to code, either using PB or any other language is to use easy understadable variables and constants names, and then using indentation for instructions editing.
Indentation means that i.e. a code between an IF / ENDIF Statement (may be For / NEXT or REPEAT / UNTIL etc) has a specific tab so that if you read it vertically you will find the corresponding end part of code at the same comuùn than start part of code.
Then be careful on one point I learnt 30 years ago ... Anything which seems to be repetitive should correspond to a procedure.
This makes the executable sometimes slower but lets you thik to higher level issues.
Learn about recursive programming, procedures benefits, and probalby later, if you feel really a newby to Win API stuf.
This is a fact that Fred PureBasic is certainly the best possible language for designing software.
This is also a fact that most PB coders think Windows. Just forget it first and go back on it later. Windows is the most used operating system and environment but has nothing to do with your coder's expectations ...
When using PB as a designing environment, try first to imagine what you want to do, and then how it may apply on the deisgn evironment you have got.
Have you got PureBasic, then be sure you can do anything you thought.
I do so.
Just it may takes some minutes, hours, or days to do. But PureBasuc alows to do to anything you thought first.
Are you experienced more or less in using computers, softwares, hardware, networks, any user experience / requirement has its reply coding with PureBasic.
This is the reason why I recommand first to take some hours downloading and reading Code archive samples.
On my own, wheb I post sample code, I try to be at the best possible level for teaching or answering any posted issue. I do not have always the right answer.
How I do code ...
Let's give an example :
Step 1
I wish to display square root of two on the console
Verbs are display and wish
Forget wish ... just think 'Do'
Step 2
What is Square_Of_Anything using PB ?
Have a look, even if it takes some hours to parse pages, at the PB help file.
A square root is sqr(n)
sqr(n) uses a function : Sqr and an argument n
Then you have to solve about the function and the argument. PB does assume the function Sqr which is not the case of any function / procedure you will see in Code Archive. Some functions are not PB functions but 'externals'.
Anyway Sqr is a PB function, then it just remind the argument to understand.
First understand that Fred (PB founder) made things working so that you may send any argument you wish without insulting you when the argument is not of the right kind.
Then you may try either Sqr(2) or Sqr(2.0) which reprensent respectivliy an integer and a float values.
Why try to be understanble in variables names ?
Code: Select all
lSquareRoot_Of_2.l = Sqr(2)
Debug lSquareRoot_Of_2
fSquareRoot_Of_2.f = Sqr(2.0)
Debug fSquareRoot_Of_2
Test it and undesrstand then ...
Before to have a meaning any computer objects, being a bit, a byte, a word a structure, a proceure or a softawre application, has it formats.
I can imagine, let(s hope I am not wrong, that you are human with 2 eyes, two arms, two legs, a noze and some other stuff, would you look without having defined what's your eyes are and feel ?
Then let's your mind imagine what you can do ...
Code: Select all
Structure eyes
x.l
y.l
EndStructure
If OpenWindow(0, 0, 0, 480, 360, #PB_Window_SystemMenu, "Eyes")
Repeat
StartDrawing(WindowOutput())
Box(0, 0, 480, 360, 0)
Plot(240, 180)
StopDrawing()
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End
Here you defined eyes as a something that do not need anything.
Then ...
Code: Select all
Structure eyes
x.l
y.l
EndStructure
If OpenWindow(0, 0, 0, 480, 360, #PB_Window_SystemMenu, "Eyes")
Repeat
StartDrawing(WindowOutput())
Box(0, 0, 480, 360, 0)
FrontColor(255, 255, 255)
Plot(240, 180)
StopDrawing()
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End
Here you have just defined that you are drawing a point somewhere ... but always at the same place.
Let's go further :
Code: Select all
Structure eyes
x.l
y.l
EndStructure
If OpenWindow(0, 0, 0, 480, 360, #PB_Window_SystemMenu, "Eyes")
Repeat
StartDrawing(WindowOutput())
GetCursorPos_(Where_I_Look.POINT)
Box(0, 0, 480, 360, 0)
FrontColor(255, 255, 255)
Plot(Where_I_Look\x, Where_I_Look\y)
StopDrawing()
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End
Here you have a point plotted close to the mouse position (just be careful to see this point).
May you want to look a better rendering about what eyes should be ?
Use this :
Code: Select all
Structure eyes
x.l
y.l
EndStructure
If OpenWindow(0, 0, 0, 480, 360, #PB_Window_SystemMenu, "Eyes")
Repeat
StartDrawing(WindowOutput())
GetCursorPos_(Where_I_Look.POINT)
Box(0, 0, 480, 360, 0)
FrontColor(255, 255, 255)
DrawingMode(1)
Circle(240, 180, 50, #Blue)
DrawingMode(1)
Ellipse(240, 180, 100, 50, #Blue)
Circle(240 + (Where_I_Look\x - 240) / 5, 180 - (180 - Where_I_Look\y) / 8, 25, #White)
StopDrawing()
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End
Then you have a code there which corresponds to a first 'higher level' but not exactly well coded :
Here you define better what eyes are undependantly of what your vision is large ...
Code: Select all
Structure eyes
x.l
y.l
EndStructure
WindowXSize.l = 480
WindowYSize.l = 360
Quit.l = #FALSE
If OpenWindow(0, 0, 0, WindowXSize, WindowYSize, #PB_Window_SystemMenu, "Eyes")
Repeat
StartDrawing(WindowOutput())
GetCursorPos_(Where_I_Look.POINT)
Box(0, 0, WindowXSize, WindowYSize, 0)
FrontColor(255, 255, 255)
DrawingMode(1)
Circle(WindowXSize / 2, WindowYSize / 2, 50, #Blue)
DrawingMode(1)
Ellipse(WindowXSize / 2, WindowYSize / 2, 100, 50, #Blue)
Circle(WindowXSize / 2 + (Where_I_Look\x - WindowXSize / 2) / 5, WindowYSize / 2- (WindowYSize / 2- Where_I_Look\y) / 8, 25, #White)
StopDrawing()
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End
Just one more trick to make the coding better :
Change where you are looking or not ?
Never execute instructions that are unnecessary ...
This is just for not consuming the CPU for nothing.
Code: Select all
Structure eyes
x.l
y.l
EndStructure
WindowXSize.l = 480
WindowYSize.l = 360
Where_I_Look.POINT
Quit.l = #FALSE
OldWhere_I_Look_x.l
OldWhere_I_Look_y.l
If OpenWindow(0, 0, 0, WindowXSize, WindowYSize, #PB_Window_SystemMenu, "Eyes")
SetCursorPos_(WindowXSize / 2, WindowYSize / 2)
Repeat
GetCursorPos_(Where_I_Look)
If Where_I_Look\x <> OldWhere_I_Look_y Or Where_I_Look\y <> Where_I_Look_y
StartDrawing(WindowOutput())
Box(0, 0, WindowXSize, WindowYSize, 0)
FrontColor(255, 255, 255)
DrawingMode(1)
Circle(WindowXSize / 2, WindowYSize / 2, 50, #Blue)
DrawingMode(1)
Ellipse(WindowXSize / 2, WindowYSize / 2, 100, 50, #Blue)
Circle(WindowXSize / 2 + (Where_I_Look\x - WindowXSize / 2) / 5, WindowYSize / 2- (WindowYSize / 2- Where_I_Look\y) / 8, 25, #White)
StopDrawing()
OldWhere_I_Look_x= Where_I_Look\x
OldWhere_I_Look_y = Where_I_Look\y
EndIf
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End
Does this help for a better understanding ?