Not logged inOpenClonk Forum
Up Topic Development / Developer's Corner / pretty new at programming -- could someone give me a hand?
- - By r4uz [nl] Date 2009-07-28 14:48
Hi all,

I've followed a course in university about C++. I made it all the way to pointers, dynamic arrays, linked lists, inheritance, overloading functions and operators. So I can basically follow a C++ file from start to end.

However the amount of .c and .h files is just overwhelming for me right now. Could someone point out the main flow of the program and what is all dependent of eachother? And the most important functions?
Parent - By Isilkor Date 2009-07-28 17:46
Currently, from a pure code perspective, everything depends on everything else. Which is quite a pain if you want to change something and have to wait 20 Minutes for the rebuild to finish ;)

The lowest barrier of entry has to be the C4Script functions, in my opinion. While C4Script.cpp is a huge file, the interface is rather clear, and if you already have some C4Script experience, you should have a rough idea of what every function does.
Reply
Parent - By Günther [de] Date 2009-07-28 18:09
Everything is important ;-) Apart from the script engine (C4Aul*), the GUI (C4GUI*, C4Startup*) the network (C4Network2*) and C4Object, C4ObjectCom and C4Movement, most files represent one or a few classes which collect the functions related to one piece of stuff. For example, C4SolidMask has functions which manage the impermeable parts of elevator cases, castles, etc., C4GamePadCon has functions for gamepad control, C4Command functions to make the Clonks do stuff themselves, and so on.

The main loop is mostly in C4Game::Execute. Ignore the macro obfuscation, and you can see what happens every frame.
Reply
Parent - - By MrBeast [de] Date 2009-07-29 14:21
It would be good if someone who knows the engine would create sort of graphic where is shown how the single pieces of the engine are connected together and in a short sentence what they do. I think this would help new ones to understand it and work with it. And it would be also good for clean up pourpose.
Reply
Parent - By r4uz [nl] Date 2009-07-29 18:49
Yeah that's a good idea! I'm currently just reading through all the header files...
Parent - By PeterW [de] Date 2009-07-30 17:08 Edited 2009-07-30 17:16
Anything concrete you're interested in? If you want something easy to understand, your best bet is to have a look at code that's pretty new. Old stuff was in many cases patched into oblivion and written at times where we didn't have much experience ourself (I shudder at the thought of my initial GWE works...).

Hm, maybe the structure of the "main loop" might be interesting for beginners? It's now stretched out across a lot of files, so it might not be obvious. And I always think the big picture is the most important. Quick walkthrough:
* StdScheduler maintains a list of StdSchulerProcs - which might by anything that needs doing inside the engine.
* StdScheduler::ScheduleProcs selects one of them that isn't sleeping and executes it.
* There might be two schedulers running:
  > The main one in C4Application (C4Application.h). It's a scheduler because it derives from CStdApp (see StdWindow.h), which in turn derives from StdScheduler. Note that there's exactly one global object of it named "Application".
  > Another one for parallel work - C4Application::InteractiveThread. This one actually only starts on-demand and is typically used for network stuff. The name doesn't make much sense nowadays, by the way.

Procs include:
* A proc waiting for messages/events from the OS (obviously platform specific, it's CStdMessageProc for Windows, in StdWindow.h)
* The high-accuracy game timer that governs both drawing and the game simulation, which is the C4ApplicationGameTimer defined in C4Application.h (further coverage below)
* Some low-accuracy timers doing background work like network timeouts and GUI refreshes, typically derive from C4ApplicationSec1Timer (C4Application.h)
* Low-level network procs that wait for data to receive or send and timeouts to pass (C4NetIO.h) [InteractiveThread]
* High-level network procs with more houskeeping an timeouts and statistics (C4Network2IO) [InteractiveThread]

When the C4ApplicationGameTimer is activated (C4ApplicationGameTimer::Execute, in C4Application.cpp), it draws the screen and in most cases also calls C4Application::GameTick, which depending on the game state either
a) shows the start GUI (C4Startup::Execute)
b) initializes a new game (C4Application::OpenGame, though the interesting parts are in C4Game::Init)
c) runs the game (C4Game::Execute)

Running the game consists of first "preparing" control (C4GameControl::Prepare), which in network mode makes sure our control is sent to other clients - and that we ourselves have all control needed to continue with the game. This might obviously fail, in which case the game tick is delayed. Otherwise, we execute the control (C4GameControl::Execute), which means collecting everything into C4GameControl::Control, saving it to a record (if recording) and then calling "Execute" on each individual control packet.

After that's done, we get back into C4Game::Execute to do the whole game simulation routine - meaning calling "Execute" on a number of subsystems.

I hope that helps somewhat...
Parent - - By Newton [es] Date 2009-08-01 12:36
You could put anything related in a more structured way here for the future - http://wiki.openclonk.org/index.php?title=DevelopersGuide
Parent - By MastroLindo Date 2009-08-19 23:02
looks promising...
I hope that after the summer, when hopefully the source code will be a bit clearer for newcomers, I will be able to contribute as well :)
Reply
Up Topic Development / Developer's Corner / pretty new at programming -- could someone give me a hand?

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill