bitblt function

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
webjeff
Posts: 66
Joined: Thu May 05, 2005 2:51 am

bitblt function

Post by webjeff »

Hello coders,

does bitblt function account for alpha? How does that work, my image is now loading correct but I can't get the alpha to work. it should be in the right format cause RGB is coming up correctly, so what needs to be done to get alpha working?

Thanks
Jeff.
Orion_
Posts: 69
Joined: Thu Jan 27, 2005 8:47 am

Post by Orion_ »

modify the function to support alpha :)
blackdroid
Posts: 564
Joined: Sat Jan 17, 2004 10:22 am
Location: Sweden
Contact:

Post by blackdroid »

since you are uploading it to vram directly and not doing it via gpu pipeline you have to do the alpha calculation yourself.
Kung VU
Tychom
Posts: 7
Joined: Sat May 14, 2005 10:22 pm

Post by Tychom »

well fwiw I knocked together a very quick and dirty alpha blending function last night for my game which is just an extension of the current - note this is bound to be terribly slow but it does at least work ;)

Code: Select all

#define MASK_RED	0x1F
#define MASK_GREEN	0x3E0
#define MASK_BLUE	0x7C00

#define GET_RED&#40;c&#41; &#40;&#40;unsigned char&#41;&#40;&#40;c & MASK_RED&#41; << 3&#41;&#41;
#define GET_GREEN&#40;c&#41; &#40;&#40;unsigned char&#41;&#40;&#40;c & MASK_GREEN&#41; >> 2&#41;&#41;
#define GET_BLUE&#40;c&#41; &#40;&#40;unsigned char&#41;&#40;&#40;c & MASK_BLUE&#41; >> 7&#41;&#41;

unsigned short alpha_blend&#40;unsigned short front, unsigned short back, unsigned char alpha&#41;
&#123;
	unsigned char beta = 255 - alpha;

	return rgb2col&#40;
		&#40;&#40;GET_RED&#40;front&#41; * alpha&#41; + &#40;GET_RED&#40;back&#41; * beta&#41;&#41; / 255,
		&#40;&#40;GET_GREEN&#40;front&#41; * alpha&#41; + &#40;GET_GREEN&#40;back&#41; * beta&#41;&#41; / 255,
		&#40;&#40;GET_BLUE&#40;front&#41; * alpha&#41; + &#40;GET_BLUE&#40;back&#41; * beta&#41;&#41; / 255&#41;;
&#125;

void BitBltA&#40;unsigned long x,unsigned long y,unsigned long w,unsigned long h,unsigned long mag,const unsigned short *d, const unsigned char * m&#41;
&#123;
	unsigned char *vptr0;		//pointer to vram
	unsigned char *vptr;		//pointer to vram
	unsigned long xx,yy,mx,my;
	const unsigned short *dd;
	const unsigned char * mm;

	vptr0=&#40;unsigned char*&#41;pgGetVramAddr&#40;x,y&#41;;
	for &#40;yy=0; yy<h; yy++&#41; &#123;
		for &#40;my=0; my<mag; my++&#41; &#123;
			vptr=vptr0;
			dd=d;
			mm=m;
			for &#40;xx=0; xx<w; xx++&#41; &#123;
				for &#40;mx=0; mx<mag; mx++&#41; &#123;
					*&#40;unsigned short *&#41;vptr = alpha_blend&#40;*dd,*&#40;unsigned short *&#41;vptr,*mm&#41;;
					vptr+=PIXELSIZE*2;
				&#125;
				dd++;
				mm++;
			&#125;
			vptr0+=LINESIZE*2;
		&#125;
		d+=w;
		m+=w;
	&#125;

&#125;
It requires a separate mask, in this case it's just 8bit so you'll need to have appropriate arrays looking something like this:

Code: Select all

//Image size&#58; &#40;8,8&#41;
const unsigned short bmp_shot_r&#91;&#93; =
&#123;
	0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
	0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
	0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
	0x8000,0xd120,0xedc3,0xee43,0xff29,0xfff1,0xfff3,0x8000,
	0x8000,0xd120,0xedc3,0xee43,0xff29,0xfff1,0xfff3,0x8000,
	0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
	0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,
	0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000
&#125;;

//Image size&#58; &#40;8,8&#41;
const unsigned char mask_bmp_shot_r&#91;&#93; =
&#123;
	0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
	0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
	0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
	0x0,0xff,0xff,0xff,0xff,0xff,0xff,0x0,
	0x0,0xff,0xff,0xff,0xff,0xff,0xff,0x0,
	0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
	0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
	0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
&#125;;


//call as such&#58; BitBltA&#40;x,y,8,8,2,bmp_shot_r,mask_bmp_shot_r&#41;;
I also have code now set up to take 24bit bitmaps and produce the 16bit sprite & 8bit masks as above if anyone needs them (along with some extra stuff to print fonts from a bitmap organised like those shown .here
Tychom
Posts: 7
Joined: Sat May 14, 2005 10:22 pm

Post by Tychom »

As an aside, has anyone idea about how much static data can be held (i.e. how big the stack can be), without malloc i'm storing already upwards of 300k in bitmap & mask data..
blackdroid
Posts: 564
Joined: Sat Jan 17, 2004 10:22 am
Location: Sweden
Contact:

Post by blackdroid »

well you know where userspace starts, and you have 24MB to play with, lots of pointers to use in case you worry about stack trashing.
Kung VU
webjeff
Posts: 66
Joined: Thu May 05, 2005 2:51 am

Post by webjeff »

OK,

im seein some wierd results from your bitbltA function.

Here is my image and mask:
const unsigned short image[]={
0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,
0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,
0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x7c00,0x6c04,0x6c04,0x6c04,0x6c04,0x6c04,0x6c04,0x6c04,0x6c04,
0x6c04,0x6c04,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,0x001f,
0x001f,0x001f,0x001f,0x001f,
};

const unsigned short mask[]={
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
};


This is a 16 bit image, with red on top and blue on the bottom, im just tryin to mask out either color and its coming up all messed up, it works fine if I use bitblt, but not right using bitbltA (the function you posted above) any ideas??

Thanks
Jeff
Tychom
Posts: 7
Joined: Sat May 14, 2005 10:22 pm

Post by Tychom »

the mask array should be of type "const unsigned char []", not short. Other than that, your mask array is split 40-60% so you'll still see a bit of blue (if that's what you wanted then all's good :) )
Post Reply