📄 t3dlib1.cpp
字号:
// get the dc from surface
if (FAILED(lpdds->GetDC(&xdc)))
return(0);
// set the colors for the text up
SetTextColor(xdc,RGB(palette[color].peRed,palette[color].peGreen,palette[color].peBlue) );
// set background mode to transparent so black isn't copied
SetBkMode(xdc, TRANSPARENT);
// draw the text a
TextOut(xdc,x,y,text,strlen(text));
// release the dc
lpdds->ReleaseDC(xdc);
// return success
return(1);
} // end Draw_Text_GDI
///////////////////////////////////////////////////////////
int Open_Error_File(char *filename)
{
// this function opens the output error file
FILE *fp_temp; // temporary file
// test if this file is valid
if ((fp_temp = fopen(filename,"w"))==NULL)
return(0);
// close old file if there was one
if (fp_error)
Close_Error_File();
// assign new file
fp_error = fp_temp;
// write out header
Write_Error("\nOpening Error Output File (%s):\n",filename);
// return success
return(1);
} // end Open_Error_File
///////////////////////////////////////////////////////////
int Close_Error_File(void)
{
// this function closes the error file
if (fp_error)
{
// write close file string
Write_Error("\nClosing Error Output File.");
// close the file handle
fclose(fp_error);
fp_error = NULL;
// return success
return(1);
} // end if
else
return(0);
} // end Close_Error_File
///////////////////////////////////////////////////////////
int Write_Error(char *string, ...)
{
// this function prints out the error string to the error file
char buffer[80]; // working buffer
va_list arglist; // variable argument list
// make sure both the error file and string are valid
if (!string || !fp_error)
return(0);
// print out the string using the variable number of arguments on stack
va_start(arglist,string);
vsprintf(buffer,string,arglist);
va_end(arglist);
// write string to file
fprintf(fp_error,buffer);
// return success
return(1);
} // end Write_Error
///////////////////////////////////////////////////////////////////////////////
int Create_Bitmap(BITMAP_IMAGE_PTR image, int x, int y, int width, int height)
{
// this function is used to intialize a bitmap
// allocate the memory
if (!(image->buffer = (UCHAR *)malloc(width*height)))
return(0);
// initialize variables
image->state = BITMAP_STATE_ALIVE;
image->attr = 0;
image->width = width;
image->height = height;
image->x = x;
image->y = y;
image->num_bytes = width*height;
// clear memory out
memset(image->buffer,0,width*height);
// return success
return(1);
} // end Create_Bitmap
///////////////////////////////////////////////////////////////////////////////
int Destroy_Bitmap(BITMAP_IMAGE_PTR image)
{
// this function releases the memory associated with a bitmap
if (image && image->buffer)
{
// free memory and reset vars
free(image->buffer);
// set all vars in structure to 0
memset(image,0,sizeof(BITMAP_IMAGE));
// return success
return(1);
} // end if
else // invalid entry pointer of the object wasn't initialized
return(0);
} // end Destroy_Bitmap
///////////////////////////////////////////////////////////
int Draw_Bitmap(BITMAP_IMAGE_PTR source_bitmap,UCHAR *dest_buffer, int lpitch, int transparent)
{
// this function draws the bitmap onto the destination memory surface
// if transparent is 1 then color 0 will be transparent
// note this function does NOT clip, so be carefull!!!
UCHAR *dest_addr, // starting address of bitmap in destination
*source_addr; // starting adddress of bitmap data in source
UCHAR pixel; // used to hold pixel value
int index, // looping vars
pixel_x;
// test if this bitmap is loaded
if (source_bitmap->attr & BITMAP_ATTR_LOADED)
{
// compute starting destination address
dest_addr = dest_buffer + source_bitmap->y*lpitch + source_bitmap->x;
// compute the starting source address
source_addr = source_bitmap->buffer;
// is this bitmap transparent
if (transparent)
{
// copy each line of bitmap into destination with transparency
for (index=0; index<source_bitmap->height; index++)
{
// copy the memory
for (pixel_x=0; pixel_x<source_bitmap->width; pixel_x++)
{
if ((pixel = source_addr[pixel_x])!=0)
dest_addr[pixel_x] = pixel;
} // end if
// advance all the pointers
dest_addr += lpitch;
source_addr += source_bitmap->width;
} // end for index
} // end if
else
{
// non-transparent version
// copy each line of bitmap into destination
for (index=0; index<source_bitmap->height; index++)
{
// copy the memory
memcpy(dest_addr, source_addr, source_bitmap->width);
// advance all the pointers
dest_addr += lpitch;
source_addr += source_bitmap->width;
} // end for index
} // end else
// return success
return(1);
} // end if
else
return(0);
} // end Draw_Bitmap
///////////////////////////////////////////////////////////////////////////////
int Load_Image_Bitmap(BITMAP_IMAGE_PTR image, // bitmap image to load with data
BITMAP_FILE_PTR bitmap, // bitmap to scan image data from
int cx,int cy, // cell or absolute pos. to scan image from
int mode) // if 0 then cx,cy is cell position, else
// cx,cy are absolute coords
{
// this function extracts a bitmap out of a bitmap file
UCHAR *source_ptr, // working pointers
*dest_ptr;
// is this a valid bob
if (!image)
return(0);
// test the mode of extraction, cell based or absolute
if (mode==BITMAP_EXTRACT_MODE_CELL)
{
// re-compute x,y
cx = cx*(image->width+1) + 1;
cy = cy*(image->height+1) + 1;
} // end if
// extract bitmap data
source_ptr = bitmap->buffer +
cy*bitmap->bitmapinfoheader.biWidth+cx;
// assign a pointer to the bimap image
dest_ptr = (UCHAR *)image->buffer;
// iterate thru each scanline and copy bitmap
for (int index_y=0; index_y<image->height; index_y++)
{
// copy next line of data to destination
memcpy(dest_ptr, source_ptr,image->width);
// advance pointers
dest_ptr += image->width;
source_ptr += bitmap->bitmapinfoheader.biWidth;
} // end for index_y
// set state to loaded
image->attr |= BITMAP_ATTR_LOADED;
// return success
return(1);
} // end Load_Image_Bitmap
///////////////////////////////////////////////////////////
int Scroll_Bitmap(void)
{
// this function scrolls a bitmap
// not implemented
// return success
return(1);
} // end Scroll_Bitmap
///////////////////////////////////////////////////////////
int Copy_Bitmap(void)
{
// this function copies a bitmap from one source to another
// not implemented
// return success
return(1);
} // end Copy_Bitmap
///////////////////////////////////////////////////////////
int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)
{
// this function opens a bitmap file and loads the data into bitmap
int file_handle, // the file handle
index; // looping index
UCHAR *temp_buffer = NULL; // used to convert 24 bit images to 16 bit
OFSTRUCT file_data; // the file data information
// open the file if it exists
if ((file_handle = OpenFile(filename,&file_data,OF_READ))==-1)
return(0);
// now load the bitmap file header
_lread(file_handle, &bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));
// test if this is a bitmap file
if (bitmap->bitmapfileheader.bfType!=BITMAP_ID)
{
// close the file
_lclose(file_handle);
// return error
return(0);
} // end if
// now we know this is a bitmap, so read in all the sections
// first the bitmap infoheader
// now load the bitmap file header
_lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));
// now load the color palette if there is one
if (bitmap->bitmapinfoheader.biBitCount == 8)
{
_lread(file_handle, &bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));
// now set all the flags in the palette correctly and fix the reversed
// BGR RGBQUAD data format
for (index=0; index < MAX_COLORS_PALETTE; index++)
{
// reverse the red and green fields
int temp_color = bitmap->palette[index].peRed;
bitmap->palette[index].peRed = bitmap->palette[index].peBlue;
bitmap->palette[index].peBlue = temp_color;
// always set the flags word to this
bitmap->palette[index].peFlags = PC_NOCOLLAPSE;
} // end for index
} // end if
// finally the image data itself
_lseek(file_handle,-(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);
// now read in the image
if (bitmap->bitmapinfoheader.biBitCount==8 || bitmap->bitmapinfoheader.biBitCount==16 ||
bitmap->bitmapinfoheader.biBitCount==24)
{
// delete the last image if there was one
if (bitmap->buffer)
free(bitmap->buffer);
// allocate the memory for the image
if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
{
// close the file
_lclose(file_handle);
// return error
return(0);
} // end if
// now read it in
_lread(file_handle,bitmap->buffer,bitmap->bitmapinfoheader.biSizeImage);
} // end if
else
{
// serious problem
return(0);
} // end else
#if 0
// write the file info out
printf("\nfilename:%s \nsize=%d \nwidth=%d \nheight=%d \nbitsperpixel=%d \ncolors=%d \nimpcolors=%d",
filename,
bitmap->bitmapinfoheader.biSizeImage,
bitmap->bitmapinfoheader.biWidth,
bitmap->bitmapinfoheader.biHeight,
bitmap->bitmapinfoheader.biBitCount,
bitmap->bitmapinfoheader.biClrUsed,
bitmap->bitmapinfoheader.biClrImportant);
#endif
// close the file
_lclose(file_handle);
// flip the bitmap
Flip_Bitmap(bitmap->buffer,
bitmap->bitmapinfoheader.biWidth*(bitmap->bitmapinfoheader.biBitCount/8),
bitmap->bitmapinfoheader.biHeight);
// return success
return(1);
} // end Load_Bitmap_File
///////////////////////////////////////////////////////////
int Unload_Bitmap_File(BITMAP_FILE_PTR bitmap)
{
// this function releases all memory associated with "bitmap"
if (bitmap->buffer)
{
// release memory
free(bitmap->buffer);
// reset pointer
bitmap->buffer = NULL;
} // end if
// return success
return(1);
} // end Unload_Bitmap_File
///////////////////////////////////////////////////////////
int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height)
{
// this function is used to flip bottom-up .BMP images
UCHAR *buffer; // used to perform the image processing
int index; // looping index
// allocate the temporary buffer
if (!(buffer = (UCHAR *)malloc(bytes_per_line*height)))
return(0);
// copy image to work area
memcpy(buffer,image,bytes_per_line*height);
// flip vertically
for (index=0; index < height; index++)
memcpy(&image[((height-1) - index)*bytes_per_line],
&buffer[index*bytes_per_line], bytes_per_line);
// release the memory
free(buffer);
// return success
return(1);
} // end Flip_Bitmap
///////////////////////////////////////////////////////////
void HLine(int x1,int x2,int y,int color, UCHAR *vbuffer, int lpitch)
{
// draw a horizontal line using the memset function
int temp; // used for temporary storage during endpoint swap
// perform trivial rejections
if (y > max_clip_y || y < min_clip_y)
return;
// sort x1 and x2, so that x2 > x1
if (x1>x2)
{
temp = x1;
x1 = x2;
x2 = temp;
} // end swap
// perform trivial rejections
if (x1 > max_clip_x || x2 < min_clip_x)
return;
// now clip
x1 = ((x1 < min_clip_x) ? min_clip_x : x1);
x2 = ((x2 > max_clip_x) ? max_clip_x : x2);
// draw the row of pixels
memset((UCHAR *)(vbuffer+(y*lpitch)+x1),
(UCHAR)color,x2-x1+1);
} // end HLine
//////////////////////////////////////////////////////////////////////////////
void VLine(int y1,int y2,int x,int color,UCHAR *vbuffer, int lpitch)
{
// draw a vertic
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -