📄 t3dlib1.cpp
字号:
// T3DLIB1.CPP - Game Engine Part I
// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
// has the GUID library been included?
//#ifndef INITGUID
//#define INITGUID
//#endif
#include <windows.h> // include important windows stuff
#include <windowsx.h>
#include <mmsystem.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <ddraw.h> // directX includes
#include "T3DLIB1.H"
// DEFINES ////////////////////////////////////////////////
// TYPES //////////////////////////////////////////////////
// PROTOTYPES /////////////////////////////////////////////
// EXTERNALS /////////////////////////////////////////////
extern HWND main_window_handle; // save the window handle
extern HINSTANCE main_instance; // save the instance
// GLOBALS ////////////////////////////////////////////////
FILE *fp_error = NULL; // general error file
// notice that interface 4.0 is used on a number of interfaces
LPDIRECTDRAW4 lpdd = NULL; // dd object
LPDIRECTDRAWSURFACE4 lpddsprimary = NULL; // dd primary surface
LPDIRECTDRAWSURFACE4 lpddsback = NULL; // dd back surface
LPDIRECTDRAWPALETTE lpddpal = NULL; // a pointer to the created dd palette
LPDIRECTDRAWCLIPPER lpddclipper = NULL; // dd clipper
PALETTEENTRY palette[MAX_COLORS_PALETTE]; // color palette
PALETTEENTRY save_palette[MAX_COLORS_PALETTE]; // used to save palettes
DDSURFACEDESC2 ddsd; // a direct draw surface description struct
DDBLTFX ddbltfx; // used to fill
DDSCAPS2 ddscaps; // a direct draw surface capabilities struct
HRESULT ddrval; // result back from dd calls
UCHAR *primary_buffer = NULL; // primary video buffer
UCHAR *back_buffer = NULL; // secondary back buffer
int primary_lpitch = 0; // memory line pitch for primary buffer
int back_lpitch = 0; // memory line pitch for back buffer
BITMAP_FILE bitmap8bit; // a 8 bit bitmap file
BITMAP_FILE bitmap16bit; // a 16 bit bitmap file
BITMAP_FILE bitmap24bit; // a 24 bit bitmap file
DWORD start_clock_count = 0; // used for timing
int windowed_mode = FALSE; // tracks if dd is windowed or not
// these defined the general clipping rectangle
int min_clip_x = 0, // clipping rectangle
max_clip_x = (SCREEN_WIDTH-1),
min_clip_y = 0,
max_clip_y = (SCREEN_HEIGHT-1);
// these are overwritten globally by DDraw_Init()
int screen_width = SCREEN_WIDTH, // width of screen
screen_height = SCREEN_HEIGHT, // height of screen
screen_bpp = SCREEN_BPP; // bits per pixel
int window_client_x0 = 0; // used to track the starting (x,y) client area for
int window_client_y0 = 0; // for windowed mode directdraw operations
// storage for our lookup tables
float cos_look[360];
float sin_look[360];
// FUNCTIONS //////////////////////////////////////////////
int Create_BOB(BOB_PTR bob, // the bob to create
int x, int y, // initial posiiton
int width, int height, // size of bob
int num_frames, // number of frames
int attr, // attrs
int mem_flags) // memory flags in DD format
{
// Create the BOB object, note that all BOBs
// are created as offscreen surfaces in VRAM as the
// default, if you want to use system memory then
// set flags equal to:
// DDSCAPS_SYSTEMMEMORY
// fir video memory you can create either local VRAM surfaces or AGP
// surfaces via the second set of constants shown below in the regular expression
// DDSCAPS_VIDEOMEMORY | (DDSCAPS_NONLOCALVIDMEM | DDSCAPS_LOCALVIDMEM )
DDSURFACEDESC2 ddsd; // used to create surface
int index; // looping var
// set state and attributes of BOB
bob->state = BOB_STATE_ALIVE;
bob->attr = attr;
bob->anim_state = 0;
bob->counter_1 = 0;
bob->counter_2 = 0;
bob->max_count_1 = 0;
bob->max_count_2 = 0;
bob->curr_frame = 0;
bob->num_frames = num_frames;
bob->curr_animation = 0;
bob->anim_counter = 0;
bob->anim_index = 0;
bob->anim_count_max = 0;
bob->x = x;
bob->y = y;
bob->xv = 0;
bob->yv = 0;
// set dimensions of the new bitmap surface
bob->width = width;
bob->height = height;
// set all images to null
for (index=0; index<MAX_BOB_FRAMES; index++)
bob->images[index] = NULL;
// set all animations to null
for (index=0; index<MAX_BOB_ANIMATIONS; index++)
bob->animations[index] = NULL;
// make sure surface width is a multiple of 8, dd likes that
bob->width_fill = ((width%8!=0) ? (8-width%8) : 0);
Write_Error("\nCreate BOB %d",bob->width_fill);
// now create each surface
for (index=0; index<bob->num_frames; index++)
{
// set to access caps, width, and height
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.dwWidth = bob->width + bob->width_fill;
ddsd.dwHeight = bob->height;
// set surface to offscreen plain
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | mem_flags;
// create the surfaces, return failure if problem
if (FAILED(lpdd->CreateSurface(&ddsd,&(bob->images[index]),NULL)))
return(0);
// set color key to color 0
DDCOLORKEY color_key; // used to set color key
color_key.dwColorSpaceLowValue = 0;
color_key.dwColorSpaceHighValue = 0;
// now set the color key for source blitting
(bob->images[index])->SetColorKey(DDCKEY_SRCBLT, &color_key);
} // end for index
// return success
return(1);
} // end Create_BOB
///////////////////////////////////////////////////////////
int Clone_BOB(BOB_PTR source, BOB_PTR dest)
{
// this function clones a BOB and updates the attr var to reflect that
// the BOB is a clone and not real, this is used later in the destroy
// function so a clone doesn't destroy the memory of a real bob
if ((source && dest) && (source!=dest))
{
// copy the bob data
memcpy(dest,source, sizeof(BOB));
// set the clone attribute
dest->attr |= BOB_ATTR_CLONE;
} // end if
else
return(0);
// return success
return(1);
} // end Clone_BOB
///////////////////////////////////////////////////////////
int Destroy_BOB(BOB_PTR bob)
{
// destroy the BOB, tests if this is a real bob or a clone
// if real then release all the memory, otherwise, just resets
// the pointers to null
int index; // looping var
// is this bob valid
if (!bob)
return(0);
// test if this is a clone
if (bob->attr && BOB_ATTR_CLONE)
{
// null link all surfaces
for (index=0; index<MAX_BOB_FRAMES; index++)
if (bob->images[index])
bob->images[index]=NULL;
// release memory for animation sequences
for (index=0; index<MAX_BOB_ANIMATIONS; index++)
if (bob->animations[index])
bob->animations[index]==NULL;
} // end if
else
{
// destroy each bitmap surface
for (index=0; index<MAX_BOB_FRAMES; index++)
if (bob->images[index])
(bob->images[index])->Release();
// release memory for animation sequences
for (index=0; index<MAX_BOB_ANIMATIONS; index++)
if (bob->animations[index])
free(bob->animations[index]);
} // end else not clone
// return success
return(1);
} // end Destroy_BOB
///////////////////////////////////////////////////////////
int Draw_BOB(BOB_PTR bob, // bob to draw
LPDIRECTDRAWSURFACE4 dest) // surface to draw the bob on
{
// draw a bob at the x,y defined in the BOB
// on the destination surface defined in dest
RECT dest_rect, // the destination rectangle
source_rect; // the source rectangle
// is this a valid bob
if (!bob)
return(0);
// is bob visible
if (!(bob->attr & BOB_ATTR_VISIBLE))
return(1);
// fill in the destination rect
dest_rect.left = bob->x;
dest_rect.top = bob->y;
dest_rect.right = bob->x+bob->width;
dest_rect.bottom = bob->y+bob->height;
// fill in the source rect
source_rect.left = 0;
source_rect.top = 0;
source_rect.right = bob->width;
source_rect.bottom = bob->height;
// blt to destination surface
if (FAILED(dest->Blt(&dest_rect, bob->images[bob->curr_frame],
&source_rect,(DDBLT_WAIT | DDBLT_KEYSRC),
NULL)))
return(0);
// return success
return(1);
} // end Draw_BOB
///////////////////////////////////////////////////////////
int Draw_Scaled_BOB(BOB_PTR bob, int swidth, int sheight, // bob and new dimensions
LPDIRECTDRAWSURFACE4 dest) // surface to draw the bob on)
{
// this function draws a scaled bob to the size swidth, sheight
RECT dest_rect, // the destination rectangle
source_rect; // the source rectangle
// is this a valid bob
if (!bob)
return(0);
// is bob visible
if (!(bob->attr & BOB_ATTR_VISIBLE))
return(1);
// fill in the destination rect
dest_rect.left = bob->x;
dest_rect.top = bob->y;
dest_rect.right = bob->x+swidth;
dest_rect.bottom = bob->y+sheight;
// fill in the source rect
source_rect.left = 0;
source_rect.top = 0;
source_rect.right = bob->width;
source_rect.bottom = bob->height;
// blt to destination surface
if (FAILED(dest->Blt(&dest_rect, bob->images[bob->curr_frame],
&source_rect,(DDBLT_WAIT | DDBLT_KEYSRC),
NULL)))
return(0);
// return success
return(1);
} // end Draw_Scaled_BOB
///////////////////////////////////////////////////////////
int Load_Frame_BOB(BOB_PTR bob, // bob to load with data
BITMAP_FILE_PTR bitmap, // bitmap to scan image data from
int frame, // frame to load
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;
DDSURFACEDESC2 ddsd; // direct draw surface description
// is this a valid bob
if (!bob)
return(0);
// test the mode of extraction, cell based or absolute
if (mode==BITMAP_EXTRACT_MODE_CELL)
{
// re-compute x,y
cx = cx*(bob->width+1) + 1;
cy = cy*(bob->height+1) + 1;
} // end if
// extract bitmap data
source_ptr = bitmap->buffer + cy*bitmap->bitmapinfoheader.biWidth+cx;
// get the addr to destination surface memory
// set size of the structure
ddsd.dwSize = sizeof(ddsd);
// lock the display surface
(bob->images[frame])->Lock(NULL,
&ddsd,
DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
NULL);
// assign a pointer to the memory surface for manipulation
dest_ptr = (UCHAR *)ddsd.lpSurface;
// iterate thru each scanline and copy bitmap
for (int index_y=0; index_y<bob->height; index_y++)
{
// copy next line of data to destination
memcpy(dest_ptr, source_ptr,bob->width);
// advance pointers
dest_ptr += (bob->width+bob->width_fill);
source_ptr += bitmap->bitmapinfoheader.biWidth;
} // end for index_y
// unlock the surface
(bob->images[frame])->Unlock(NULL);
// set state to loaded
bob->attr |= BOB_ATTR_LOADED;
// return success
return(1);
} // end Load_Frame_BOB
///////////////////////////////////////////////////////////
int Animate_BOB(BOB_PTR bob)
{
// this function animates a bob, basically it takes a look at
// the attributes of the bob and determines if the bob is
// a single frame, multiframe, or multi animation, updates
// the counters and frames appropriately
// is this a valid bob
if (!bob)
return(0);
// test the level of animation
if (bob->attr & BOB_ATTR_SINGLE_FRAME)
{
// current frame always = 0
bob->curr_frame = 0;
return(1);
} // end if
else
if (bob->attr & BOB_ATTR_MULTI_FRAME)
{
// update the counter and test if its time to increment frame
if (++bob->anim_counter >= bob->anim_count_max)
{
// reset counter
bob->anim_counter = 0;
// move to next frame
if (++bob->curr_frame >= bob->num_frames)
bob->curr_frame = 0;
} // end if
} // end elseif
else
if (bob->attr & BOB_ATTR_MULTI_ANIM)
{
// this is the most complex of the animations it must look up the
// next frame in the animation sequence
// first test if its time to animate
if (++bob->anim_counter >= bob->anim_count_max)
{
// reset counter
bob->anim_counter = 0;
// increment the animation frame index
bob->anim_index++;
// extract the next frame from animation list
bob->curr_frame = bob->animations[bob->curr_animation][bob->anim_index];
// is this and end sequence flag -1
if (bob->curr_frame == -1)
{
// test if this is a single shot animation
if (bob->attr & BOB_ATTR_ANIM_ONE_SHOT)
{
// set animation state message to done
bob->anim_state = BOB_STATE_ANIM_DONE;
// reset frame back one
bob->anim_index--;
// extract animation frame
bob->curr_frame = bob->animations[bob->curr_animation][bob->anim_index];
} // end if
else
{
// reset animation index
bob->anim_index = 0;
// extract first animation frame
bob->curr_frame = bob->animations[bob->curr_animation][bob->anim_index];
} // end else
} // end if
} // end if
} // end elseif
// return success
return(1);
} // end Amimate_BOB
///////////////////////////////////////////////////////////
int Scroll_BOB(void)
{
// this function scrolls a bob
// not implemented
// return success
return(1);
} // end Scroll_BOB
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -