Game Development Community

Using T2D Functions from VS C++ Code?

by BadManiac · in Torque 2D Beginner · 08/07/2014 (2:44 am) · 2 replies

I've been working on a 2D library of my own written in C and ASM (yes Assembler), but I've gotten to a point where NIH Syndrome and feature creep basically means I work exclusively on the library instead of actual games. Looking to change that and T2D looks really promising, but, uhm, I'm oldschool :p

Is it possible to set up T2D so that you can access it's functions from a C++ program in Visual Studio, so that the majority of the code is in actual C++ rather than Torque script.

Basically, can you #include <Torque2d.h> and then t2d.dostuff(); in the code?

Is there a good source of example source code for using T2D from within a C++ Project?

About the author

Recent Threads


#1
08/07/2014 (6:13 am)
It's doable, but without some specific examples I can provide direction or pitfalls. There are some critical systems you might need to be familiar with, like the event, serialization, and batch rendering. T2D is easily packaged into a lib, but it also has dependencies (image libs) you need to carry over unless you replace them.
#2
08/07/2014 (7:17 am)
That sounds promising. I'm basically looking for a dropin replacement for my own software lib that allows hardware scaling and rotation alongside the usual windows setup and destruction and so on. I already have image loading code that should be fairly easy to reuse, as well and many other basic functions.

I need windows management, initialization and shutdown, textures/surface management, blitting with scaling, rotation and blending. And... that's pretty much it really.

Here's some code from a simple demo project using my library. Init, Render and Shotdown.

// a helper function for creating the viewport
static void CreateViewport(jBOOL fullscreen)
{
	const jVIEWPORT_MODE mode =
	{
		"JRA::Library Basics",
		DISPLAY_XRES,
		DISPLAY_YRES,
		REGION_XRES,
		REGION_YRES,
		fullscreen,
		THREADED
	};

	static jVIEWPORT_HOOK hook =
	{
		WindowProcHook,
		NULL
	};

	// try a GDI renderer first
	if(jViewport_Create(&viewport, jViewportGDI, &mode, &hook))
	{
		return;
	}
	// otherwise fall back on direct draw
	if(jViewport_Create(&viewport, jViewportDirectDrawGDI, &mode, &hook))
	{
		return;
	}
	// try again in windowed mode
	if(fullscreen)
	{
		CreateViewport(jFALSE);
	}
	// give up and terminate the application
	jUtils_Message(jTRUE, "Viewport creation failed");
}

// render full frame of game objects
// this function gets called once for each viewport region
static void Render(jSURFACE_AREA *viewport, void *context, unsigned thread)
{
	// create a surface area from the background image surface
	//jSURFACE_AREA fnt = jSurface_Area(&battlefield->sfc);
	jSURFACE_AREA bg = jSurface_Area(&background);
	
	// blit the background image to the viewport
	//jBlit.Opaque(viewport, &bg, 0, 0);
	jBlit_ScaleNearest(viewport, &bg, 0, 0, DISPLAY_XRES, DISPLAY_YRES);

	// display the all important frame rate
	jFont_Print(viewport, verdana, 0, 0, jFONT_TRANSPARENT, 0, "FPS:%i", (int)timer.freq);

	if(toggle_text)
	{
		// some round text to demonstrate AA
		jFont_Print(viewport, verdana, 160, 32, jFONT_TRANSPARENT, 0, "Monospace 8pt font no AA");
	
		// display some text showcasing a variable width font
		jFont_Print(viewport, comic, 8, 48, jFONT_TRANSPARENT, 0, "Comic Sans 72px");
		jFont_Print(viewport, battlefield, 128, 256, jFONT_TRANSPARENT, 0, "::JRA::");
		jFont_Print(viewport, battlefield, 64, 256 + battlefield->height, jFONT_TRANSPARENT, 0, "Library");
	}
}

// set display mode, initialize lib systems and load graphics
static void Startup(void)
{
	const char *error;
	// initialize library
	if(error = jStartup(NULL, NULL), error)
	{
		jUtils_Message(jTRUE, "Library initialization failed: %s", error);
	}
	// create viewport
	CreateViewport(FULLSCREEN);

	// create the timer
	jTimer_Reset(&timer);

	//seed RNG
	srand((unsigned)jTimer_Poll());

	// now we can load images since we have established a pixel format
	verdana = jFont_LoadMonospace("../../Resources/font8pt.jmg");
	//verdana = jFont_LoadFont("../../Resources/arial12.fnt");
	battlefield = jFont_LoadFont("../../Resources/battlefield80.fnt");
	comic = jFont_CreateFont("Comic Sans MS", 72, 700, jTRUE, jTRUE);

	//jSurface_Create(&background, 640, 480);
	jImage_LoadJmg(&background, "../../Resources/background.jmg");
	if(!LoadBackground())
	{
		jUtils_Message(jTRUE, "Failed to load background");
	}
}

// release game objects
static void Shutdown(void)
{
	jSurface_Destroy(&background);
	jFont_Destroy(verdana);
	jFont_Destroy(comic);
	jFont_Destroy(battlefield);
	jViewport_Destroy(&viewport);
	jShutdown();
}

// update the game
static jBOOL DoFrame(void)
{
	// process requests for toggling between windowed mode and fullscreen
	if(toggle_fullscreen)
	{
		jBOOL mode = !viewport.mode.fullscreen;

		// recreate the viewport in the new mode
		jViewport_Destroy(&viewport);
		CreateViewport(mode);

		// clear the flag
		toggle_fullscreen = jFALSE;
	}

	// rendering a new frame
	switch(jViewport_Update(&viewport, Render, NULL))
	{
	// suspend thread if window is minimized or hidden
	case jVIEWPORT_INVISIBLE:
		jTimer_Pause(&timer);
		jTimer_Sleep(0.050);
		return jTRUE;

	// exit application if the window was closed
	case jVIEWPORT_CLOSED:
		return jFALSE;
	}

	// update timer
	jTimer_Update(&timer);

	return jTRUE;
}