Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 945 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Most frequent mistakes in C++ programming #91069
09/22/06 10:14
09/22/06 10:14
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline OP

Chief Engineer
jcl  Offline OP

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
I've just thought I add this sticky thread because I see the same questions asked all over again.

Frequent Mistake 1: Creating an object.

This won't do:

STRING *mystring = new STRING; // will crash as soon as you attempt to use that string!

Script objects like STRING, ENTITY, BMAP etc. have to be created with their _create functions. Merely allocating a struct won't do because that struct must be initialized and linked to the object manager. So, this is the correct way:

STRING* mystring = str_create("");


Frequent Mistake 2: Creating STRINGs for calling functions.

You'll soon run into memory problems when you create a STRING every time you just want to pass a text or name to a function! All engine functions also accept normal char* arguments. You also do not need _str() or the like. Just use:

char* animation = "walk";
ent_animate(me,animation,VAR_(percent),VAR_(mode));

or

ent_animate(me,"walk",VAR_(percent),VAR_(mode));


Frequent Mistake 3: Forgetting the VAR_() when passing non-VAR values to engine functions.

The engine does not automatically convert int/float/...-values passed to the engine to var.

Last edited by TWO; 11/17/07 16:07.
Re: Most frequent mistakes in C++ programming [Re: jcl] #91070
09/22/06 19:02
09/22/06 19:02
Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
T
TWO Offline

Serious User
TWO  Offline

Serious User
T

Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
Frequent Mistake 4: The engine/plugin SDK is in your GS folder.

In all newer versions of the engine (>6.31) the SDK is located in the A6 folder.


Frequent Mistake 5: Using DirectX9 rendering features.

returns the active D3D device:

... = (LPDIRECT3DDEVICE9)draw_begin();

No need to close the scene after your code (via draw_end(); or present();), it´s handled by the engine!


Frequent Mistake 6a: Using type 'char' like STRING

One char is one letter (like 'A'). To use more than one char, define it like:

char* example; // This is a pointer to a group of chars

or

char example[] = "Hello"; // This is a char array ( in this case you have to provide a default string )

or

char example[5] = "Hello"; / /This is a normal char array
example = "bye!";


Frequent Mistake 6b: Accessing (A6-)string chars/letters

The string doesn´t just holds the chars, but some other informations like the index number in the engine. To directly access the chars use:

string example;
... = example->chars; // set the char pointer

Better use the _chr macro!

Re: Most frequent mistakes in C++ programming [Re: Nidhogg] #91072
02/14/07 17:09
02/14/07 17:09
Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
T
TWO Offline

Serious User
TWO  Offline

Serious User
T

Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
Frequent Mistake 7: error LNKxxxx: unresolved external symbol

If you get an unresolved symbol error without any other library than the A6 SDK, you forgot to add 'adll.lib' for a plugin or 'acknex.lib' for an engine project.

How-To-Add with Visual Studio 2005:

Open your project, Extras->Options, close the environment tab on the left side, open Projects and Project-Solutions (again on the left side), open VC++-Folders, then select 'Include directories' on the top left box. Now add the sdk_plugin or sdk_engine (e.g 'C:\Programme\Lite-C\sdk_plugin' or 'C:\Programme\Lite-C\sdk_engine') to the list. Select 'Lib directories' on the top left box and add the same folder again.

Note: Take a look at Frequent Mistake 10 for problems with the " __chr " symbol


Frequent Mistake 8: I´m new to C++, how to start?

First you need a compiler and an IDE. Popular compilers are Visual Studio, Dev C++, MinGW,... Search at Google how and where to get them, there are lot´s of freebies.

E.g
http://msdn.microsoft.com/vstudio/express/ (recommended)
http://www.bloodshed.net/devcpp.html

Now you need to get to know C++. It´s hard, but worth learning it. Buy a book (for good one´s search here at the forum / depending on your language) or google for online tutorials. I advise you to buy a book, event if it costs your last penny. The quality is mostly much better, you can hack everywhere, even in the dessert and it takes you away from the desktop for some minutes, sparing your eyes.

Now play with your gained experience. Yes, you could just start with the SDK, but thats not really senseful. If you think you´ve done it, go on to mistake 9.


Frequent Mistake 9: I know C++, but not the SDK

So, you know C++ but don´t know how to use the SDK. Then go ahead, if not, go back to mistake 8. The steps of creating a working project (not compiler specific):
1. Create a new project
2.1 In your compiler, add the sdk_engine or sdk_plugin folder (located in your A6 directory) to the locations for headers and libs; This will keep the SDK files in 1 central location, making updates easier and preventing lots of duplicated files
2.2 If you have no clue how to do 2.1, just copy the files from the sdk_engine/sdk_plugin to your project folder and add them then to the project
3. Add acknex.lib (for engine projects) or adll.lib (for engine plugins) to your linker options
4. Create a main file (e.g. main.cpp), depends on your compiler and your preferences
5. Fill it with that code:
 Code:
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include &lt;stdlib.h&gt;
#include &lt;malloc.h&gt;
#include &lt;memory.h&gt;
#include &lt;tchar.h&gt;
#include &lt;math.h.&gt;
// engine specific header file
#define DLL_USE	// always define before including adll.h
#include "adll.h"	

// DLL entry point
BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved )
{
// make sure the library is linked
    engine_bind();
    return TRUE;
}

6. Compile your stuff; For possible errors search this thread


Edit:
- Changed the way the SDK is linked


Re: Most frequent mistakes in C++ programming [Re: TWO] #91073
11/17/07 16:02
11/17/07 16:02
Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
T
TWO Offline

Serious User
TWO  Offline

Serious User
T

Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
Frequent Mistake 10: adll.lib(adll.obj) : error LNK2019: unresolved external symbol __chr

In one release, the adll.lib was outdated. Download the new one and overwrite the old file to fix it:
http://server.conitec.net/down/adll.zip


Frequent Mistake 11: Difference between 'adll.lib' and 'acknex.lib'

adll.lib is the library for building a plugin which provides functions to Lite-C/C-Script, acknex.lib is the one for using the engine with C++ without any Lite-C/C-Script code.

Re: Most frequent mistakes in C++ programming [Re: jcl] #256084
03/14/09 15:39
03/14/09 15:39
Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
T
TWO Offline

Serious User
TWO  Offline

Serious User
T

Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
People keep asking about a wait equivalent in C++

The hard way is to work with threads, but if you didn't know this yet you better just forget it again. For (simple) tasks found in gameplay that's not needed. A better way is to either call your function every frame from lite-c and to decide inside C++ code what to execute when. This decision can be based on a var (Cpp function was called N times?), on the engine timer (2 seconds passed since last execution?) or a condition (is the object in line of sight?). Or a more advanced way, especially if you want to write more code than some snippets in Cpp is to use a 'scheduler'. This scheduler is a singleton class inside your dll, to which tasks are added which then get executed at a requested time.

Code:
class ITask
{
public:
	virtual void onUpdate() = 0;
};

class Scheduler
{
public:
	static void addTask( ITask* task )
	{
		mTasks.push_back( task );
	}

	static void update()
	{
		TaskContainer::iterator itr, end = mTasks.end();

		for( itr = mTasks.begin(); itr != end; ++itr )
			(*itr)->onUpdate();
	}

private:
	typedef std::vector< ITask* > TaskContainer;
	static TaskContainer mTasks;
};

DLLFUNC void scheduler_update()
{
	Scheduler::update();
}


Instead of using a singleton class here I just made the methods static. Also, tasks are executed just every frame

Now, just derivate whatever from ITask and call scheduler_update() every frame.

Code:
class MoveTask : public ITask
{
public:
	MoveTask( ENTITY* entity )
		:	mEntity( entity )
	{
	}

	void onUpdate()
	{
		c_move( mEntity, ... );
	}

protected:
	ENTITY* mEntity;
};

...
Scheduler::addTask( new MoveTask( myEntityInTheWorld ) );



which corresponds to

Code:
while(1) {
	c_move( Entity, ... );
	wait(1);
}



Last edited by TWO; 07/27/10 13:04.

Moderated by  TWO 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1