Code structure

Blitz3D Forums/Blitz3D Programming/Code structure

Wolfsong73(Posted 2016) [#1]
Hello

So, was wondering about this today at work, and thought I'd ask.

I know in programming, people tend to develop their own styles, and there's no one way to do anything.

I'm curious about how people structure their code?

For example, do you define all your images, functions and such at the beginning of the code? Do you put certain things at the end? Perhaps some things are kept in separate files which are then included, etc. etc?

It seems like, though I'm still very new to all this, that learning some good "housekeeping" habits would be a good idea, esp. as I start to create more complex programs. So, I'm looking for some ideas/advice/examples of how people structure/organize their code.

Thanks!


RemiD(Posted 2016) [#2]
personally the structure of my code (using Blitz3d) currently looks like this (the structure of my old codes may be slightly different, but most of them work well) :

;initialize constants, globals, lists (arrays and customtypes)
;functions/procedures, systems
;load premade components (meshes, skeletons (joints/bones), animations, textures, images, fonts, sounds)
;initialize graphics window
;initialize camera
;initialize listener
;build world
;mainloop
 ;getinput (mouse, keyboard)
 ;mode interaction 2D (update menu)
 ;mode interaction 3D (update world, entities)
 ;go to backbuffer
 ;render3d
 ;draw2d
 ;flip backbuffer to frontbuffer
 ;play/stop sounds
;exit


but this is just my structure, you can organize your code as you prefer (if it works well)


RemiD(Posted 2016) [#3]
here is a basic scene with a grid and first person view controls


(as you can see the structure of this code is different than what i have explained in my previous post, but it works well...)


Bobysait(Posted 2016) [#4]
one tip I always do :
All functions and extra stuff should always be defined before anything else
(In the example of RemiD, the Graphics3D and all the stuff until the first function should be after the last function)

It asserts everything is initialized before any call.
(you can have some initialization to do, like, defining globals, or some function calls to initialize some libraries)



This is just a small library that doesn't really do anything, but it's just for the purpose
Depending on how you include it in your code, it can generate an error

(the following will fail)


(while this one will work)


Or you can also save the library in an other bb file and "Include" it before your loop
[Codebox]
Include "MyLibrary.bb"

Graphics 800,600,0,2 ... etc ...
[/Codebox]


So, preserving SDk, Libraries (etc ...) above your main code is the best way to get everything working without missing some initializations
BUT ! (there is a BUT) if the library contains some initialization that create 3D objects (a global pivot for example) or load DirectX Objects (texture, images ...), it will fail if you don't create a graphics context.
So, you can make something slightly different like this :




A another tip (that beginners seems to forget and not having learnt at all) : Indent your code !
use tabulation to make your code clear and easy to read.


RemiD(Posted 2016) [#5]
I agree that it is better to write constants/globals/lists/functions before anything else because if you try to use a variable/reference before it has been created, then you will get an error (because it is empty or because it is already defined in another part of the program)...

But this structure works well too (similar structure than my code example):


in any case the variables/references must be created before being used by procedures/functions, to prevent any error (you don't want your program to act on something which does not exist...)...


Yue(Posted 2016) [#6]



Matty(Posted 2016) [#7]
I build a skeleton in code.

So most games follow a very similar pathway:

load()
..loop..
handleinput()
updateenvironment()
updatecreatures()
updatebullets()
cleardisplay()
drawstuff()
..endloop..
unload()

from there I write the headers of other functions I need and work inwards.