Project layout

General FreeBASIC programming questions.
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Project layout

Postby Gonzo » May 27, 2012 21:57

How do you lay out your project?
How do you connect separate parts of a project?
How do you use different .bas files, and access its data
Can you share the same variables between systems, can you do it on an as-needed basis?

Lets take a classic example - renderer functions, configuration options, physics, networking, sound etc. etc.

Im asking this in general, because I dont know, and maybe others need advice too =)
So, if you have any insight to share.. please!
dafhi
Posts: 1298
Joined: Jun 04, 2005 9:51

Re: Project layout

Postby dafhi » May 28, 2012 0:15

One thing I've found helpful is the offloading of a developing section into a "dev" file. It's based loosely around the Test-Driven Development programming method
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Project layout

Postby petan » May 28, 2012 0:58

Hi Gonzo.
How do you lay out your project?

Maybe wrong question.My project - by my needs and knowledges. So your project - I don't know,;what it is, how big and hard it will be, how many modules, etc. etc.Put right question on known problem .Universal solution on unknown problem does not exists.
See this for small&mid projects
http://www.freebasic.net/forum/viewtopic.php?f=2&t=16373
How do you connect separate parts of a project?

Again.What is "separate part "?
If possible, in no way.Not using separate parts. If you think OOP programming, as I know FB is not fully OOP featured yet.
How do you use different .bas files, and access its data

Exclusively used

Code: Select all

#include
Shared

command for building dereferenced modules and global data (including global multidim helpers), not one big gig source file.That would be self-overkill scenario.
#include used only in main prog module in beginning for connecting all parts/source modules into the one big continuous source, if possible.
Last question is about cross-programming/compiling ? Again, be very exact in defining of your problem.
My sand beach is data analysis on only one system/language/app.No networking/exchange data/using untested&unsafe features.Using the simplest things for getting results.

So don't worry and put exact question about concrete thing/your new project, if it's not secret.

Pete
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: Project layout

Postby Gonzo » May 28, 2012 14:46

of course, its not really a secret
im building a game engine (again), and its probably going to be huge as usual...
i need to connect several parts, and last time i did it with dim shared structs for global data, and includes of .bi files for separate parts of the project
in the end this meant a single .asm file for the most part (i did have some OO, such as matrices and vectors)

i just need to know a better way to layout the project
because i dont fully understand how to use different .bas files with my project
back in the qbasic days 12 years ago, it was easy to connect different .bas files... now not so much
and in the visual basic days i used .cls files alot, and bas files had private and public stuff
does that even work with freebasic?
one of my biggest headaches of all time is the way freebasic and C works top-down, so i have to define something before i can use it
does this mean, just like in C, i need to define structs in every file?
how??
is there an example project with several .bas files somewhere, because i really dont want another 35 #include *.bi lines

for those who wonder, yes my blockworld game is on hold, it was just too complicated for a one-man team to do =)
even though there were several people on it, we have decided to make a 2d world instead
its not about time, its a 50/50 "coding must be fun" (i spent 6 months optimizing only, no progress), and i have to know i can pull it off
i did learn alot, so im looking forward to going back to 2D :) ill just have to make the gameplay awesome..

i hope i have explained what i mean by project layout
i know full well how to design a project, i just dont know how to do it in freebasic.. and i have to admit, not in C either
i do know you can redefine structs in C, but i need to know the best way to do it in FB =)
dkl
Site Admin
Posts: 3210
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Project layout

Postby dkl » May 28, 2012 16:07

I like splitting projects up into multiple .bas modules and the compiling them together into one program via:
fbc main.bas parser.bas lexer.bas foo.bas other.bas ...


It also means that common declarations (such as procedures or UDTs used in multiple modules) are put into some .bi file(s) which are #included by the modules that need these declarations. This way the declarations are not duplicated in code but can be used everywhere they're needed.

For example, each module can see the declare sub foo( ) line by #including the appropriate #include containing that line, in order to be able to use foo( ). But only one of these modules contains the actual body/implementation of that sub. It's like a small library.

Here's an example of project layout I like at the moment:
https://github.com/dkl/fbfrog
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: Project layout

Postby Gonzo » May 28, 2012 16:19

thanks, that helps a ton =)
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Project layout

Postby petan » May 28, 2012 18:54

Very good, skills in any language is big plus for reworking code.I did the same thing, but not game.Don't worry about reworking.
Your game is singleplayer or multiplayer ? How much megabytes in size ?? /Old code to conversion../
For networking-multiplayering I don't know FB status and stability, as it is not my sand.Ask it on Game thread directly.
Also, you don't need 35x #include command to join BAS files ;D ,
In jerky mode is enough one, and every included file has next #include.Method "Matryoschka"...
But seriously, here is no rule for numbers or size BAS module.Just collect to module thematically neared SUB's and if you have feeling of too long browsing the one in editor, then close module and open new.

Pete
marcov
Posts: 2814
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Project layout

Postby marcov » May 28, 2012 20:33

petan wrote:
Again.What is "separate part "?
If possible, in no way.Not using separate parts. If you think OOP programming, as I know FB is not fully OOP featured yet.


That's modular thinking, and has nothing to do with OOP. OOP later built a bit on top off modular, but is not a prerequisite.

Basically the first step is to have some form of separate compilation, (like C has)

Then the next step is to make that automatable, so that a piece of program (typically a part of the compiler) can determine the order to compile. Usually that is meant with a modular or module- system. Just like Java, C# and Pascal variants, point the compiler to the main file, add some include directories (Classpath, unitpath), press compile, and it will compile everything recursively, without manually setting up compilation order, or even which files to link(*)

FB has some module system, but I'm not sure what exactly it is capable of.

(*) files outside the reach of the unit system, like separate .a of .o files from other languages must be explicitely included.
Last edited by marcov on May 29, 2012 8:50, edited 1 time in total.
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

Re: Project layout

Postby TESLACOIL » May 29, 2012 1:00

playing devils advocate here " to what problem is your game engine the solution "


as in do you really want to make a game engine for others to ACTUALLY use

or is it just a project to skill up with and have fun ?

If you are attempting the former then i think you will quickly find that you will have to take off your fun hat and put on your semi pro hat.....that might not be a place you want to go....but you will get dragged there nonetheless

as you mentioned earlier, the 'fun aspects' where starting to leak away with your previous huge build. Lachies words ' dont bite of more than you can chew..cos u aint a zillion dollar developer ' started bouncing around, and its not a comfortable feeling, but hes right.....persistent enthusiasm can quickly turn into a nasty case of 'trench foot'





My AI project is about as big as it gets for 1 man and his dog...it sucks almost up all my free time, and empties my wallet to. Yes the project is interesting but fun went out the window a long time ago......i even feel guilty if ive not coded for a couple of days

I am also slowly working through a big multiplayer space game...which though i dont realy have the time for, im doing it to brush up my code skills and as an excuse to do something sat in front of the computer that's NOT related to my all consuming AI project....though im beginning to wander if i should just take a 3month break instead. lol


The problem ive always found with coding, is that is just sucks you in a very big way...yet when you step back out and look at what you have achieved in terms of real world results AND compared that output to all the man hours invested ( & quality man hours at that ) is it really, truly worthwhile ? ....you can kill time by putting your feet up and swigging beer, spending that same time sat in front of a computer 'keeping busy doing nothing' is in some real way, a tragic waste

just because you can , doesn't mean you must






....as i said at the start, im just playing devils advocate here....

but i do think its wise to step back for a bit and consider what you 'really want to achieve'....and importantly, will it actually be achievable on the terms that you value ?
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Re: Project layout

Postby pestery » May 29, 2012 8:39

I divide my code into 2 modules (2 .asm outputs), one for the bulk of my program and the other containing functions that use operating system specific commands. This helps to separate most of my code from things like the Window API functions. I also divide my files by sub-topic and group them in folders by topic. It works well for me, but then my projects haven't become as large as your block world project yet.

Regarding the general layout of a project, I think the trick is to have a look at a number of different methods and pick the bits that you like, that work for your situation or for your coding style.

A quick example of my general project layout.

Code: Select all

' --- General file/folder layout ---
' project/main.bas
' project/os-spec.bas
' project/header.bi
' project/src/gui/gui.bi
' project/src/gui/gui.bas
' project/src/gui/user-input.bi
' project/src/gui/user-input.bas
' project/src/network/network.bi
' project/src/network/network.bas
' project/src/network/net-send.bi
' project/src/network/net-send.bas
' project/src/os-spec/clipboard.bi
' project/src/os-spec/clipboard.bas

' Compile as:
' fbc main.bas os-spec.bas

' --- project/main.bas ---
#Include "header.bi" ' Include the master header file
#Include "src/gui/gui.bas" ' General high-level GUI functions that are used by other modules/topics
#Include "src/gui/user-input.bas" ' User-input function and data
#Include "src/network/network.bas" 'General high-level network functions
gui_init()
network_init()
' Main loop here
network_terminate()
gui_terminate()
End

' --- project/os-spec.bas ---
#If Defined(__FB_WIN32__)
' Include Windows specific libraries here
#Include "windows.bi"
#ElseIf Defined(__FB_LINUX__)
' Include Linux specific libraries here
#Else
#Error "Platform not supported"
#EndIf
#Include "header.bi" ' Includes other header files
#Include "src/network/net-send.bas" ' Network send data functions which use windows or linux functions
#Include "src/os-spec/clipboard.bas" ' OS clipboard interaction functions

' --- project/header.bi ---
#Include "src/gui/gui.bi" ' Includes function declarations and common datatypes for gui.bas
#Include "src/gui/user-input.bi"
#Include "src/network/network.bi"
#Include "src/network/net-send.bi"
#Include "src/os-spec/clipboard.bi"

petan wrote:Also, you don't need 35x #include command to join BAS files ;D
The master header file in my current revision has 26 #include's ;-)
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Project layout

Postby petan » May 29, 2012 17:02

pestery wrote:The master header file in my current revision has 26 #include's ;-)

So, 26 #include's only and nice structure..
No worries, Gonzo, and go in. Unworking code post on the forum or simplify it, if you feel one as sensitive data .

Pete
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: Project layout

Postby Gonzo » May 29, 2012 17:34

um... some variable in config.bi is empty when accessed from another module than the one that set it
why? whats the point of shared if its not global

i have now spent hours setting my project up with .bi files with declare's
and turned .bas files into modules, and i expected shared variables in .bi files to be resident throughout project as long as its #include'd

05-29-2012 19:38:39:: modpath: C:\FbEdit1068\Projects\dm2\data\mods\LttP\
05-29-2012 19:38:40:: Generating GL textures...
05-29-2012 19:38:40:: modpath:

config.bi:

Code: Select all

Type modconfig
   
   modname As String
   modpath As String
   
End Type
Dim Shared modconf As modconfig


config.bi is #included from main and textures .bas files

edit: tried extern, got fake reference
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Project layout

Postby petan » May 29, 2012 18:42

Show your module structure, if not complicated+compiler directive - compiled main module or all ?
Show order of #includes in main prog module.
Or simply trace visibility of your variable in modules with using block commenting marks

Code: Select all

/'
code
code
code
'/
for understanding what is where visible./And learning commands like Extern, Common etc./
Fill it with value and print it in every module>>finding where it is lost.
modconf is global variable for all modules by this, so ..
Clear ? Comment everything but modconf.All modules joined and compiled.Unnecessary contents commented.Scan only modconf visibility/accessibility.
Running only skeleton routine of proggy.Base example here:
http://www.freebasic.net/forum/viewtopic.php?f=2&t=19931

Pete

Note: I forgot to say - block commenting marks is main command at reworking code to different language.
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: Project layout

Postby Gonzo » May 29, 2012 19:01

alright, i figured it out.. apparently the extern's are supposed to be in the .bi that goes with the .bas
who woulda thunk :(
codeFoil
Posts: 255
Joined: Dec 22, 2011 4:45
Location: United States
Contact:

Re: Project layout

Postby codeFoil » May 29, 2012 21:11

pestery's method is also my general approach, but to reach that point you need a good road map of your project first. Once you have a directory tree setup, making changes later can be a chore, which is the exact opposite effect a well planned layout should have.

The benefits of modularizing in the first place are two fold. One, it help isolate different parts of your program which minimizes side effects when making changes. Two, it helps you find code in the first place. It's easier to search through a few hundred line than it is a few thousand.

When starting a new project, I give it a few days to roll around in my mind. I doodle data structures and imagine them as machines with moving parts. Each part needs to do just one thing or travel along one dimension.

I generally try to start with a monolithic piece of psuedo code that contains just enough information to identify the major sections of a system. That gives me my top level break down. From there, I can start decided where I need to dip into external libraries or the Windows API. Each of these gets spun off into a module to isolate it.

With that ground work in place, I'm ready for a prototype, and the purpose here is to gauge whether or not my design feels good. If the overall layout feels tedious or messy in the first day of coding, I scrap it and start over, because when you finally get it right, the actually coding just pours like concrete from a hopper. (No wheel barrows required)

The end result is never as pretty as I'd like it be, so the next time around, I try something slightly different.

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest