Lesson2: Variables and Functions

Lesson 2 teaches you how to use engine variables and functions - please see the Engine Plugins chapter for details. All engine variables from the script language are available, and also most engine functions except script-specific functions like wait. Under C/C++ you normally don't use multitasking and thus won't and can't use the script multitasking functions. Instead, use the Windows Sleep() function for waiting a certain time, or the EVENT_FRAME for triggering an entity function every frame cycle.

// Lesson2: Using engine variables and functions
#define WIN32_LEAN_AND_MEAN		
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include "adll.h"

int APIENTRY WinMain(HINSTANCE hInstance,	
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,	
                     int       nCmdShow)	
// The engine_open() function returns a pointer to a struct of 
// engine variables. We don't want to load a script or level here, 
// so we just pass NULL.
	ENGINE_VARS *ev = engine_open(NULL);
	if (!ev) return 1;	// acknex.dll not found

// Once we have the pointer to the ENGINE_VARS struct, we can use engine 
// functions for customizing the window, giving a level name to load, 
// assigning functions to buttons etc.
// In our case we're now again loading the arena.wmb level. 


// We want to limit our frame rate to 50 frames per second. For this we
// change an engine variable named fps_max:


// SETV() is a convenient macro to set engine variables.
// It is defined in the adll.h header.

// Now let's enter our main loop:

	while (engine_frame()) 

// The first engine frame opens the DirectX device and 
// switches the engine window to DirectX mode. Therefore,
// the time before the first frame is merely for setting 
// or defining variables - DirectX functions won't work yet.
// Also the the level is actually loaded in the first frame.
// The level_load() function just stored its name.
// If we loaded a script before, it's main and starter 
// functions are also executed in the first engine_frame().

// We didn't pass a level name to engine_open(), so we now have to 
// program our own camera movement. 
// By default, a VIEW named "camera" is created when opening a level,
// and can be moved by changing it's angle and position parameters.

		v(camera).pan += 3 * v(key_force).x;	// left/right cursor keys
		v(camera).tilt += 3 * v(key_force).y;	// up/down cursor keys

// The key_force vector returns a value of +/-1 dependent on which 
// cursor keys in x and y direction are pressed. 
// The v() is another simple macro to access engine variables.

// Now we want to move in the direction the camera is looking.
// For movement with the WASD keys, we use the key_w, key_a, 
// key_s, key_d variables and calculate a movement vector.

		VECTOR vMove;	// movement vector
		vMove.x = 6.0 * (v(key_w) - v(key_s));	// forward
		vMove.y = 6.0 * (v(key_a) - v(key_d));	// sideward
		vMove.z = 0;

// Finally, we use engine vector functions to rotate the movement vector 
// by the camera angle, then add it to the camera position. 
// We can typecast any object's x position to a VECTOR and it's pan 
// orientation to an Euler ANGLE.

		vec_rotate (&vMove, (ANGLE*)&v(camera).pan);
		vec_add ((VECTOR*)&v(camera).x, &vMove);

// This simple movement was not as smooth as the default one, 
// but we're going to change this in our next lesson.

	return 0;

► latest version online