Skip to content

About

C++ wrapper over C bindings for the Serious Engine

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Serious Engine C++ API

This repository contains C++ wrappers for C bindings over the Engine.dll of the Serious Sam - The Second Encounter game.

It is intended to ease the usage of C API and offer smoother and more native usage of the orignal APIs.

Usage example

Minimal program that initializes the engine inside stream handling, prints a greeting to the console and exits:

#include <windows.h>

#include "Engine.h"

static void HelloWorld()
{
  SE_InitEngine(CTString("SeriousEngineCppAPI"));

  CPrintF("Hello %s!\n", "SeriousEngineCppAPI");

  SE_EndEngine();
}

int main()
{
  CTStream::ExecuteWithStreamHandling(HelloWorld);
  return 0;
}

Migrating from engine sources

If you have code written against the engine sources and want to compile it against the C++ API instead of linking the engine, most of your code does not change at all. The rules below cover the cases that do, the examples are taken from the SeriousModelerEX and SeriousEditorEX.

1. Primitive types: unchanged

INDEX, FLOAT, FLOAT3D, ANGLE, PIX, BOOL and friends are plain values in the C++ API, exactly as in the engine sources. Signatures using them compile as-is:

// WorldEditor - works unchanged
void CMainFrame::SetStatusBarMessage(CTString strMessage, INDEX iPane, FLOAT fTime);

2. Complex types: CTString, CPlacement3D, CModelObject, CWorld, ...

How you use the type What to do
Plain object you create yourself (CPlacement3D pl;) Works as-is
Reference, e.g. function argument (const CTFileName& fn) Works as-is
Pointer to an object (CEntity* pen) Use the pointer wrapper: CEntityPtr pen

Plain objects behave like the original classes. This block is identical in engine and C++ API code (Modeler):

CPlacement3D plCurrent;
plCurrent.pl_OrientationAngle(1) = AngleDeg(m_fHeading);
plCurrent.pl_PositionVector(1) = m_fXOffset;

For pointers, every wrapped class X declares a pointer wrapper XPtr (via DECL_PTR in Base/Types.h). Editor:

// before (engine sources)
CEntity* pen = pDoc->m_selEntitySelection.GetFirstInSelection();
CTFileName fnecl = pen->GetClass()->GetName();

// after (C++ API) - same code, only the type changes
CEntityPtr pen = pDoc->m_selEntitySelection.GetFirstInSelection();
CTFileName fnecl = pen->GetClass()->GetName();

Functions that returned pointers in the engine now return the wrapper, so updating the variable type is usually the whole fix (Modeler):

// before
CAttachmentModelObject* GetAttachmentModelObject(INDEX iAttachment);
// after
CAttachmentModelObjectPtr GetAttachmentModelObject(INDEX iAttachment);

The engine-style iteration macros are provided by the C++ API as well (FOREACHINDYNAMICARRAY in Templates/DynamicArray.h, FOREACHINDYNAMICCONTAINER in Templates/DynamicContainer.h etc):

// Modeler - writes every attaching position
FOREACHINDYNAMICARRAY(edm_md.md_aampAttachedPosition, CAttachedModelPosition, itamp)
{
  itamp->Write_t(&strmMappingFile);
}

// Editor - walks all entities of the world
FOREACHINDYNAMICCONTAINER(pDoc->m_woWorld.wo_cenEntities, CEntity, iten)
{
  if (iten->en_RenderType == CEntity::RT_BRUSH) { /* ... */ }
}

The loop variable is a container-specific pointer wrapper, not a TPointer directly, but can be used like a pointer (itamp as CAttachedModelPositionPtr, iten as CEntityPtr), so loop bodies carry over unchanged and can be tested, compared and passed around like the old pointers.

Two container-side changes go along with this:

  • operator[] on engine arrays (dynamic arrays, CList-style stocks) yields a pointer wrapper now, so use arr[i]->member instead of arr[i].member.
  • The old manual arr.Lock() / arr.Unlock() pair is gone - delete those calls.

3. Singletons: member calls become free functions

Engine singletons (_pModelStock, _pTextureStock, ...) are not exported as objects, their methods are exposed as free functions named _pSingleton_Method. Modeler/Editor:

// before                                          // after
_pModelStock->Obtain_t(fnm)                       _pModelStock_Obtain_t(fnm)
_pTextureStock->Obtain_t(fnt)                     _pTextureStock_Obtain_t(fnt)
_pModelStock->Release(pmd)                        _pModelStock_Release(*pmd)

Note the *: the free functions take the raw engine object, and *wrapper produces it from a pointer wrapper.

4. Ownership: who deletes what

  • Objects the engine gave you (from ..._Obtain_t, entity/sector pointers, container elements) are engine-owned. The C++ API wrapper classes hold the opaque engine handle and are non-owning by default - destroying a wrapper never touches the engine object. Keep them alive as long as the engine does, and return stock objects with the stock's Release free function when you are done.
  • Objects you create yourself (CModelObject, CPlacement3D, CTString, ...) are yours, like in engine code. Hold them by value or with std::unique_ptr - the wrapper itself does no refcounting. The Editor, for example, stores its gizmos as std::unique_ptr<CModelObject> m_axis_model;.

The pointer wrapper TPointer<TObject, TObjectCHandle> (Base/Pointer.h, typedef'd as XPtr) is just a nullable view onto an engine object, closer to a raw pointer than to std::shared_ptr:

CEntityPtr pen;                                            // null
pen = pDoc->m_selEntitySelection.GetFirstInSelection();    // copy/move copies the reference, never the object
if (pen) pen->GetName();   // operator bool is the null check
pen.Reset();               // forgets the object, engine object stays alive
pen == otherPen;           // comparisons and ordering compare engine handles

Because operator*/operator-> dereference the wrapper's internal storage, do not bind a reference through a temporary wrapper (CAttachedModel& am = *array[i]; dangles once the temporary dies). Store the wrapper itself, or dereference through a named wrapper that outlives the reference.

5. Warning: strings in varargs and printfs

Wrapper string types are class objects, vararg ...-style functions (CPrintF, WarningMessage, MFC CString constructors) need an explicit cast to a const char* (fix applied in SeriousEditorEX):

// before
CPrintF("Brush %s has no sectors\n", pen->GetName());
// after
CPrintF("Brush %s has no sectors\n", static_cast<const char*>(pen->GetName()));

About

C++ wrapper over C bindings for the Serious Engine

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages