So some code to share, might be useful.

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

So some code to share, might be useful.

Post by MrGodin » January 28th, 2014, 10:12 pm

Inspired by Chilli and arrays of pointers .. Thanks Chilli, been learning tonnes :)

Code: Select all


#pragma once
#include <windows.h>
// for RECT i believe its in WinDef.h
#include <windef.h>

// Used To grid out a texture based on rectangle values 
// use the constructor to create or reuse class via the Execute function
//which will flush data and reconstruct a new grid
// useful when placing text on image ect.. 
// or grid out your sprite sheets

// Some macros to work with this, un-comment to use
/*

#include "d3dx9.h"

typedef D3DXVECTOR2 Vec2;
// My macros .. 
/// **** USE ONLY WITH SPRITE TRANSFORMS
// Set Sprite Transform
#ifndef SET_TRANS 
#define SET_TRANS(p , m){(p)->SetTransform(&(m));}
#endif
// set sprite transform with a MatrixIdentity
#ifndef SET_TRANS_MI
#define SET_TRANS_MI(p , m){(p)->SetTransform(D3DXMatrixIdentity(&(m)));}
#endif
//~

// Thanks microsoft
#ifndef SAFE_DELETE
#define SAFE_DELETE(p)       { if (p) { delete (p);     (p)=NULL; } }
#endif    
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p);   (p)=NULL; } }
#endif    
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p)      { if (p) { (p)->Release(); (p)=NULL; } }
#endif

*/



class TImageGrid
{
private:	
	RECT** Grids;
	
	RECT  rcImageRectSize;
	float width;
	float height;
	int  grid_rows;
	int  grid_cols;
	int  size;
	float scale;
	Vec2 center;
	
	void FlushArray()
	{
		for(int c=0;c<grid_rows * grid_cols;c++)
		{
			SAFE_DELETE(Grids[c]);
		}
		SAFE_DELETE_ARRAY(Grids);

	}


public:
	TImageGrid()
	{
		Grids     = NULL;
		
		width     = height = 0;
		center    = Vec2(0,0);
		grid_rows = grid_cols = 0;
		rcImageRectSize.bottom = rcImageRectSize.left = rcImageRectSize.right = rcImageRectSize.top = 0;
		size      = 0;
		scale     = 0.0f;
	};
	//inImageRect = rectangle of image
	// in size is grid size eg: 32 = 32x32
	// inscale set to 1.0f for no scale
	TImageGrid(RECT inImageRect,int insize,float inscale)
		:
	rcImageRectSize(inImageRect),
	size(insize),
	scale(inscale)
	{
		width     = ((float)rcImageRectSize.right * scale) - ((float)rcImageRectSize.left*scale);
		height    = ((float)rcImageRectSize.bottom * scale) - ((float)rcImageRectSize.top * scale);
		center.x  = width / 2.0f;
		center.y  = height / 2.0f;
		grid_cols = width / size * scale;
		grid_rows = height / size * scale;

		
		
		Grids = new RECT*[grid_rows * grid_cols];
		

		
		
		for(int c=0;c<grid_rows;c++)
		{
			for(int x=0; x<grid_cols;x++)
			{
				 const int i = c * grid_cols + x;
				 Grids[i] = new RECT();
				

				 Grids[i]->left   = x * (size * scale);
				 Grids[i]->top    = c * (size * scale);
				 Grids[i]->right  = Grids[i]->left + (size*scale);
				 Grids[i]->bottom = Grids[i]->top  + (size*scale);
				
			}
		}
	}
	~TImageGrid()
	{
		for(int c=0;c<grid_rows * grid_cols;c++)
		{
			SAFE_DELETE(Grids[c]);
		}
		SAFE_DELETE_ARRAY(Grids);
		
	}
	Vec2 Center(){return center;}
	RECT* GetRect(int index)
          {
            if(index > grid_rows * grid_cols)
                 return NULL;

             return Grids[index];
          };
	RECT* GetRect(int row, int col)
	{
		int result = row * grid_cols + col;
                if(result > grid_rows * grid_cols)
                     return NULL;

		if(Grids[result])
			return Grids[result];

		return NULL;
	}


	RECT  GetRect2XWidth(int index)
	{
		RECT B;
                if(index > grid_rows*grid_cols)
                 {
                      B.left = B.right = B.top = B.bottom = 0;
                       //cheezeeeee
                       return B; 
                 }
               RECT* A = GetRect(index);
		
		B.left   = A->left;
		B.right  = A->right + (size*scale);
		B.top    = A->top;
		B.bottom = A->bottom;

		return B;

	}
	void Execute(RECT inImageRect,int size,float inscale)
	{
		FlushArray();
		size = size;
		scale = inscale;
		width     = ((float)rcImageRectSize.right * scale) - ((float)rcImageRectSize.left*scale);
		height    = ((float)rcImageRectSize.bottom * scale) - ((float)rcImageRectSize.top * scale);
		center.x  = width / 2.0f;
		center.y  = height / 2.0f;
    	grid_cols = (int)(width / (size * scale));
		grid_rows = (int)(height / (size * scale));
		
		

		Vec2 Position = Vec2(0.0f,0.0f);
		
				
		Grids = new RECT*[grid_rows * grid_cols];
		for(int c=0;c<grid_rows;c++)
		{
			for(int x=0; x<grid_cols;x++)
			{
				 const int i = c * grid_cols + x;
				

				 Grids[i] = new RECT();
				 Grids[i]->left   = x * (size*scale);
				 Grids[i]->top    = c * (size*scale);
				 Grids[i]->right  = Grids[i]->left + (size*scale);
				 Grids[i]->bottom = Grids[i]->top  + (size*scale);
				
			}
		}
	}
};
Curiosity killed the cat, satisfaction brought him back

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: So some code to share, might be useful.

Post by chili » January 29th, 2014, 10:43 am

Looks like a nice reusable class you made there :)
Chili

Post Reply