ImageClip

From SWWorkshop

Jump to: navigation, search

Sometimes working with lots of small image files cause performance and management problems. Because lots of small images will increase file access and OpenGL bind operations. The solution of this problem is combine images in one image file and clip when required. Clipping operations executes in GPU so it doesn't take extra cpu time.

Below example shows you how to clip iconset.

ScreenShot
Image:IconSet.jpg


Comments
Step1:Find count of images in X axis and Y axis

const static int XGRID_SIZE=20;
const static int YGRID_SIZE=14;

Step 2:Find the index of subImage and construct source rectangle. (Assumption:Grid size is same)

swMathSourceCalculate(&source,XGRID_SIZE,YGRID_SIZE,xIndex,yIndex);

Step 3: Selected cell/grid will be drawn to screen.

swGraphicsRenderImg2(imgID,&target,&source);

Api Doc
SwEngine, swGraphics, swMath, swRect, swApplication

Source Code

#include "../../include/SWEngine.h"
#pragma comment (lib,"../../lib/SWUtil.lib")    
#pragma comment (lib,"../../lib/SWTypes.lib")    
#pragma comment (lib,"../../lib/SWCore.lib")    
#pragma comment (lib,"../../lib/SWEngine.lib")    
 
swApplication imgRenderingApp;
int imgID;
 
//Icon Setting
swRect target={200,200,20,20};
swRect source={0,0,1,1};
int    xIndex=3;
int    yIndex=3;
int    timerID=-1;
 
const static int XGRID_SIZE=20;
const static int YGRID_SIZE=14;
 
//-------------------------------------------------------------------------------------------
void switchIcon(void *obj){ //This function triggered 0,2 seconds..
	xIndex++;
	if(xIndex==XGRID_SIZE){
		xIndex=0;
		yIndex++;
	}
 
	if(yIndex==YGRID_SIZE){
		yIndex=0;
	}
}
 
//-------------------------------------------------------------------------------------------
void GameLoop(){ 
 
	swGraphicsBeginScene();
 
		//Background
		swGraphicsSetBgColor0(0,0,0.6);
 
		//BlendingMode
		swGraphicsSetBlendingMode(SW_BLENDING_MODE_ADDITIVE);
		swMathSourceCalculate(&source,XGRID_SIZE,YGRID_SIZE,xIndex,yIndex);
 
		//Draw Image
		swGraphicsSetColor0(1,1,1,0.5);
		swGraphicsRenderImg2(imgID,&target,&source);
 
	swGraphicsEndScene();
 
}
 
//-------------------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{ 
 
 
	//Application Settings
	imgRenderingApp.hInstance=hInstance;
	imgRenderingApp.fullScreen=false;
	imgRenderingApp.cursor=true;
	imgRenderingApp.width=400;
	imgRenderingApp.height=300;
	imgRenderingApp.title="Image Clipping";
	imgRenderingApp.path="\\rsc\\IconSetAccess\\";
	imgRenderingApp.appRun=GameLoop;
 
	//Application Execution
	swEngineInit(&imgRenderingApp);
 
	//Init My Application
 	imgID=swGraphicsCreateImg("IconSet.tga");
	timerID=swTimerCreate(0.2f,NULL,switchIcon);
	swEngineRun();
	swEngineExit();
 
	return 0;
}
Personal tools