Top-Down Design: Hierarchy and Composition

General discussion for topics related to the FreeBASIC project or its community.
Lachie Dazdarian
Posts: 2338
Joined: May 31, 2005 9:59
Location: Croatia
Contact:

Post by Lachie Dazdarian »

I'm not good on examples. Unless you want me to plug-in my Invatris source in here. Trust me, you don't want that.

To me, global variables simplify many things so much. Like I often use variables to control the rotation of sprites in various animations, usually named Frame(some_num). For example, I would have this in my main loop:
Frame1 = (Frame1 MOD 5) + 1
Frame2 = (Frame2 MOD 10) + 1
And if those variables are not global, every time I call some sub where my sprites are animated and drawn, I need to create an extra parameter for them where I would pass Frame1 or Frame2. Now I ask you, what's the benefit of this?

That's just one example. I don't know. I would just get a lot of unnecessary headaches and annoying value passing procedures only to get around the fact some variable was not declared globally. They are global, I see them everywhere. They are not, it’s like I’m blind on them and have to employ helpers guiding me toward them all the time.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

My coding style (I'm not sure if it starts at the top or bottom; maybe it starts in the middle and spreads out?) most likely is not good for big projects or team development, but it allows *very* fast development. Did I mention, I wrote Orb for the competition in three days? If you look at ParaChuters, BeGemmed, Zonaxtic, and Orb, you can tell they were written by the same person because they use the same coding style.

It's not messy, and I manage the variables well, but I do use shared variables, because they are used in several Subs. Here is some code from Orb:

Code: Select all

Dim Shared As Integer progress_level, progress_score
Dim Shared As Integer alien_total, bomb_total, bonus_total, mapblock_length, missile_total, star_total, spawner_total, text_total, ufo_total
Dim Shared As Integer game_won, game_quit, game_lost, game_display_offset, game_frame_total

Dim Shared As String menu_main(0 To 5), menu_ingame(0 To 2), menu_aftergame(0 To 8), menu_highscore(0 To highscore_max + 2), menu_rules(0 To 12)
Dim Shared As String game_level_title

Dim Shared As fb.image Ptr original_graphic(1 To 128)
Dim Shared As fb.image Ptr alien_graphic(1 To 3, 0 To 1)
Dim Shared As fb.image Ptr bomb_graphic(0 To 2)
Dim Shared As fb.image Ptr bonus_graphic(0 To 1)
Dim Shared As fb.image Ptr mapblock_graphic(1 To 3)
Dim Shared As fb.image Ptr player_graphic(0 To player_frame_max - 1)
Dim Shared As fb.image Ptr spawner_graphic
Dim Shared As fb.image Ptr ufo_graphic

Dim Shared As alien_type alien(1 To alien_max)
Dim Shared As bomb_type bomb(1 To bomb_max)
Dim Shared As bonus_type bonus(1 To bonus_max)
Dim Shared As missile_type missile(1 To missile_max)
Dim Shared As player_type player
Dim Shared As spawner_type spawner(1 To spawner_max)
Dim Shared As coord star(1 To star_max)
Dim Shared As text_type text(1 To text_max)
Dim Shared As ufo_type ufo(1 To ufo_max)

Dim Shared As highscore_type highscore(1 To highscore_max)
Dim Shared As mapblock_enum mapblock(mapblock_length_max, mapblock_height)
Dim Shared As sound_type sound(1 To sound_max)
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

KristopherWindsor

Well if I were to try and get rid of these globals, I would start by encapsulating related vars into UDTs.

Code: Select all

Type GameInfo
     progress_level  as integer
     progress_score as integer
     alien_total as integer
     bomb_total as integer
     bonus_total as integer
     mapblock_length as integer
     missile_total as integer
     star_total as integer
     spawner_total as integer
     text_total as integer
     ufo_total as integer
End Type

Type GameStates
     game_won as integer
     game_quit as integer
     game_lost as integer
     game_display_offset as integer
     game_frame_total as integer
end Type

Type GameText
     menu_main(0 To 5) As String
     menu_ingame(0 To 2) As String
     menu_aftergame(0 To 8) As String
     menu_highscore(0 To highscore_max + 2) As String
     menu_rules(0 To 12) As String
     game_level_title  As String
End Type

'I still deal with sprites as global but I use one shared array
'instead of multiple vars

Then I would pass these structures BYREF ONLY to the sub/functions that use them.

-Vince
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

And if those variables are not global, every time I call some sub where my sprites are animated and drawn, I need to create an extra parameter for them where I would pass Frame1 or Frame2. Now I ask you, what's the benefit of this?
Sounds like you need to use UDTs more and design better systems to handle these kinds of things... Your sprites, if animated, should contain the animation information somewhere. A simple call to updating your sprites would be good :-)

Global Variables also lock you into the design of how you're using libraries and procedures, rather than separating procedures from your data. This is pretty bad stuff when adding or removing features.

If a sub depends on that Frame1 that's in the global scope, and you remove it, or want to change the way Frame1 works, you have to change all the subs that rely on it. When programming with procedures in local scope, procedures only have to worry about the piece of data passed to it and nothing more.

@vdecampo:

Seeing the use of FSMs in engine code now that I'm wanting to add a menu and such to the game state... FSMs would make it a lot easier to switch from main engine code to pausing, menu, title screen, etc...
Post Reply