int x=0,y=0,width=100,height=100;
int index_x, index_y;
UCHAR red,green,blue;
USHORT *temp_buffer;
temp_buffer = (USHORT*)malloc(width*height);
//copy into temporary buffer
for (index_y = y; index_y < height+y; index_y++)
{
for (index_x = x; index_x < width+x; index_x++)
{
temp_buffer[(index_x - x) + (index_y - y)] = buffer[index_x + (index_y*lPitch16)];
}
}
// now, write data back to buffer
for (index_y=0; index_y < height; index_y++)
{
for (index_x=0; index_x < width; index_x++)
{
red = ((temp_buffer[index_x + (index_y)] & 0xF800) >> 11);
green = ((temp_buffer[index_x + (index_y)] & 0x7E0) >> 6);
blue = ((temp_buffer[index_x + (index_y)] & 0x1F));
buffer[(index_x + x) + ((index_y + y) * lPitch16)] = temp_buffer[index_x + index_y];
}
}
}
free(temp_buffer);
When I run this code, it should not have any effect, but it writes garbage. Could someone please tell me what I am doing wrong?
P.S. The reason why I''m writing this code is to do a "spotlight" type of effect, so when I get this to work I will transform the information before writing it back to the buffer. I''m not quite sure how to do this either, so if someone could help me out with that too, that would be great.
DirectDraw Surface to unsigned short?
I am having trouble with the following code (I am in 640x480 16bit mode):
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment
Visit our web site:Asylum Entertainment
Yo,
I think the problem is in the line
temp_buffer = (USHORT*)malloc(width*height);
you aren''t allocating enough bytes. If your depth is 16bit, it should be
temp_buffer = (USHORT*)malloc(width*height*2);//16bit=2bytes
the way you''re doing, you''re plotting the pixels in the wrong location of the memory.
Good Luck, hope this helps,
Nicodemus.
I think the problem is in the line
temp_buffer = (USHORT*)malloc(width*height);
you aren''t allocating enough bytes. If your depth is 16bit, it should be
temp_buffer = (USHORT*)malloc(width*height*2);//16bit=2bytes
the way you''re doing, you''re plotting the pixels in the wrong location of the memory.
Good Luck, hope this helps,
Nicodemus.
Nicodemus.----"When everything goes well, something will go wrong." - Murphy
I didn''t work. I think you don''t need to multiply by 2 because it is an unsigned short. Have any other ideas?
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment
Visit our web site:Asylum Entertainment
yes you do have to multiply by two. malloc allocates x number of bytes not x number of unsigned short int (or likewise).
Yo,
Are you sure that the RGB format you're using is correct?
I mean, if you're using RGB 5-6-5 and the format that the hardware uses is RGB 5-5-5,
it won't work. Have you checked this?
Edited by - Nicodemus on 1/30/00 11:08:28 PM
Are you sure that the RGB format you're using is correct?
I mean, if you're using RGB 5-6-5 and the format that the hardware uses is RGB 5-5-5,
it won't work. Have you checked this?
Edited by - Nicodemus on 1/30/00 11:08:28 PM
Nicodemus.----"When everything goes well, something will go wrong." - Murphy
Here''s your problem. In the second loop, you have
the following line:
buffer[(index_x + x) + ((index_y + y) * lPitch16)] = temp_buffer[index_x + index_y];
The index you pass to temp_buffer isn''t correct.
Shouldn''t you have (index_x + x) + (index_y + y)
for the index?
The index you use for temp_buffer in the first loop
also doesn''t look right.
You could make this code much faster if you''d eliminate
the need to perform a multiply for each pixel.
I''ve noticed at least two major issues with your code: The malloc one, wich you do have to multiply by two,
And in the following code:
buffer[index_x + (index_y*lPitch16)]
I haven''t found what type buffer is, but it''s either 16 bit * or 8 bit *. the problem is, lPitch is the width of the surface in bytes(8 bit); as such, if buffer is a 16 bit *, then you have to divide lPitch by two, if it''s a 8 bit pointer you have to multiply index_x by two. Same goes for buffer[(index_x + x) + ((index_y + y) * lPitch16)]; you have to either divide lPitch by two if it/s 16 bit, or multiply (index_x + x) by two if it''s 8bit.
As for the spotlight, I''ve never done this before, but i think there are two issues for it:
determining the area where you want the spotlight to shine (get an algorithm that for the shape you want your spotligh to be, for each value in Y gets the leftmost and rightmost values the shape will get, then between those values do the following process)
Transforming the pixels. If you want a white light spotlight, it''s probably just a case of multipling the r, g,and b values (read: shifting) by the same amount (each shift to the left will yield a *2 multiplication, making the picture twice as brilliant) If you want the spotlight to have other colors, I think it''s a question of multiplying(here you may have to actually multiply) each component by the proportion of that component that the color you want has. (don''t forget to limit you components to 255 before you write them to thesurface)
NOTE: the malloc issue should either make you program crash, or -maybe- allow other programs to write over your surface, producing garbage on half your screen(not likely)
The 5-6-5 -> 5-5-5 issue is one to be considered. Check for the format, and write specific code for each case.
One final though: if you use the light manipulations above, remember that if you dim a picture to cast a spotlight you''ll lose information and the result under the spotlight may produce banding(visible lines of separation between colors) in that case you may want to dim the whole picture except for your spotlight. Good luck
And in the following code:
buffer[index_x + (index_y*lPitch16)]
I haven''t found what type buffer is, but it''s either 16 bit * or 8 bit *. the problem is, lPitch is the width of the surface in bytes(8 bit); as such, if buffer is a 16 bit *, then you have to divide lPitch by two, if it''s a 8 bit pointer you have to multiply index_x by two. Same goes for buffer[(index_x + x) + ((index_y + y) * lPitch16)]; you have to either divide lPitch by two if it/s 16 bit, or multiply (index_x + x) by two if it''s 8bit.
As for the spotlight, I''ve never done this before, but i think there are two issues for it:
determining the area where you want the spotlight to shine (get an algorithm that for the shape you want your spotligh to be, for each value in Y gets the leftmost and rightmost values the shape will get, then between those values do the following process)
Transforming the pixels. If you want a white light spotlight, it''s probably just a case of multipling the r, g,and b values (read: shifting) by the same amount (each shift to the left will yield a *2 multiplication, making the picture twice as brilliant) If you want the spotlight to have other colors, I think it''s a question of multiplying(here you may have to actually multiply) each component by the proportion of that component that the color you want has. (don''t forget to limit you components to 255 before you write them to thesurface)
NOTE: the malloc issue should either make you program crash, or -maybe- allow other programs to write over your surface, producing garbage on half your screen(not likely)
The 5-6-5 -> 5-5-5 issue is one to be considered. Check for the format, and write specific code for each case.
One final though: if you use the light manipulations above, remember that if you dim a picture to cast a spotlight you''ll lose information and the result under the spotlight may produce banding(visible lines of separation between colors) in that case you may want to dim the whole picture except for your spotlight. Good luck
Sorry for the long lines, I''ll try to remember to use the return key more often

This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement