DispMechanism

From SWWorkshop

Jump to: navigation, search
  • First tecnique. Render objects front to back order. For example we make Breakout game. We write code similar below.
void GameLoop(){
        displayBricks()
        displayBall()
        displayPlayer()
    }
  • Second tecnique. We give display responsibilities to one component and this component controls layer order. But why we need this component. Why don't we use first tecnique. In small application first tecnique can be used. But in a big game project lots object exist and these object rendering order is serious work. Displaying mechanism component provides this abstraction so you don't worry about ordering. You only set layer then do'nt think anything about displaying.

ScreenShot
Image:DisplayManager.JPG

Source Code
Define Xen Structure.

typedef struct _Xen{
	boolean  bVisible;
	int      layer;
	swRect   target;
	int      animatorID;
        int      displayID;
}Xen;


Define Xen Functions.

int spriteID=-1;
 
//-------------------------------------------------------------------------------------------
void xenInit(){
	spriteID=swGraphicsCreateSprite("XenRunning\\");
}
 
 
//-------------------------------------------------------------------------------------------
void xenDeInit(){
	swGraphicsDestroySprite(spriteID);
}
 
//-------------------------------------------------------------------------------------------
void xenDisplay(void *obj){
	Xen *xen=(Xen*)obj;
	//BlendingMode
	swGraphicsSetBlendingMode(SW_BLENDING_MODE_SOLID);
 
	//Draw Image
	swGraphicsSetColor0(1,1,1,1);
	swGraphicsRenderSprite0(spriteID,swAnimatorGetIndex(xen->animatorID),&xen->target);
}
 
 
//-------------------------------------------------------------------------------------------
Xen* xenCreate(){
	Xen* xen=(Xen*)malloc(sizeof(Xen));
	xen->animatorID=swAnimatorCreate(swGraphicsGetCountOfImgInSprite(spriteID),0.03f);
	swAnimatorSetExecutionMode(xen->animatorID,SW_ANIMATOR_EXEC_FORWARD_LOOP);
	swRectSet(&xen->target,200,200,124,150);
	xen->displayID=swDispManagerAdd(xenDisplay,xen,NULL,NULL,NULL);
	return xen;
}
//-------------------------------------------------------------------------------------------
void  xenDestroy(Xen *xen){
	swAnimatorDestroy(xen->animatorID);
	swDispManagerDel(xen->displayID);
	free(xen);	
}


Integrate Xen and Application

#include "DispMechanism.h"
 
swApplication dispMechanismApp;
 
//-------------------------------------------------------------------------------------------
void GameLoop(){ 
	swGraphicsBeginScene();
			swGraphicsSetBgColor2(&SWCOLOR_GRAY);
			swDispManagerExecute();
	swGraphicsEndScene();
}
 
 
 
//-------------------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{ 
 
 
	//Application Settings
	dispMechanismApp.hInstance=hInstance;
	dispMechanismApp.fullScreen=false;
	dispMechanismApp.cursor=true;
	dispMechanismApp.width=800;
	dispMechanismApp.height=600;
	dispMechanismApp.title="Display Mechanism";
	dispMechanismApp.path="\\rsc\\DisplayMechanism\\";
	dispMechanismApp.appRun=GameLoop;
 
	//Application Execution
	swEngineInit(&dispMechanismApp);
 
	//Init My Application
	xenInit();
	xenCreate();
 
 
	swEngineRun();
	swEngineExit();
 
	return 0;
}
Personal tools