📄 demo8_6.cpp
字号:
{
error-=dy2;
// move to next line
vb_start+=x_inc;
} // end if error overflowed
// adjust the error term
error+=dx2;
// move to the next pixel
vb_start+=y_inc;
} // end for
} // end else |slope| > 1
// return success
return(1);
} // end Draw_Line
///////////////////////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
char buffer[80]; // used to print strings
// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
// return success
return(0);
} break;
case WM_PAINT:
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);
// end painting
EndPaint(hwnd,&ps);
// return success
return(0);
} break;
case WM_DESTROY:
{
// kill the application, this sends a WM_QUIT message
PostQuitMessage(0);
// return success
return(0);
} break;
default:break;
} // end switch
// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
///////////////////////////////////////////////////////////
int Game_Main(void *parms = NULL, int num_parms = 0)
{
// this is the main loop of the game, do all your processing
// here
// make sure this isn't executed again
if (window_closed)
return(0);
// for now test if user is hitting ESC and send WM_CLOSE
if (KEYDOWN(VK_ESCAPE))
{
PostMessage(main_window_handle,WM_CLOSE,0,0);
window_closed = 1;
} // end if
// clear out the back buffer
DDraw_Fill_Surface(lpddsback, 0);
// lock primary buffer
DDRAW_INIT_STRUCT(ddsd);
if (FAILED(lpddsback->Lock(NULL,&ddsd,
DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
NULL)))
return(0);
// do the graphics
Draw_Polygon2D(&ship, (UCHAR *)ddsd.lpSurface, ddsd.lPitch);
// test for scale
if (KEYDOWN('A')) // scale up
Scale_Polygon2D_Mat(&ship, 1.1, 1.1);
else
if (KEYDOWN('S')) // scale down
Scale_Polygon2D_Mat(&ship, 0.9, 0.9);
// test for rotate
if (KEYDOWN('Z')) // rotate left
Rotate_Polygon2D_Mat(&ship, -5);
else
if (KEYDOWN('X')) // rotate right
Rotate_Polygon2D_Mat(&ship, 5);
// test for translate
if (KEYDOWN(VK_RIGHT)) // translate right
Translate_Polygon2D_Mat(&ship, 5, 0);
else
if (KEYDOWN(VK_LEFT)) // translate left
Translate_Polygon2D_Mat(&ship, -5, 0);
// test for translate
if (KEYDOWN(VK_UP)) // translate up
Translate_Polygon2D_Mat(&ship, 0, -5);
else
if (KEYDOWN(VK_DOWN)) // translate left
Translate_Polygon2D_Mat(&ship, 0, 5);
// unlock primary buffer
if (FAILED(lpddsback->Unlock(NULL)))
return(0);
// draw some information
sprintf(buffer,"(x,y) = [%d, %d] ",ship.x0, ship.y0);
Draw_Text_GDI(buffer, 8,SCREEN_HEIGHT - 16,255, lpddsback);
Draw_Text_GDI("<A> and <S> - Scale <Z> and <X> - Rotate <Arrows> - Translate <ESC> - Exit", 8,8,255, lpddsback);
// perform the flip
while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
// wait a sec
Sleep(33);
// return success or failure or your own return code here
return(1);
} // end Game_Main
////////////////////////////////////////////////////////////
int Game_Init(void *parms = NULL, int num_parms = 0)
{
// this is called once after the initial window is created and
// before the main event loop is entered, do all your initialization
// here
LPDIRECTDRAW lpdd_temp = NULL; // used to get the IDIRECTDRAW4 interface
// seed random number generator
srand(GetTickCount());
// first create base IDirectDraw interface
if (FAILED(DirectDrawCreate(NULL, &lpdd_temp, NULL)))
return(0);
// now query for IDirectDraw4
if (FAILED(lpdd_temp->QueryInterface(IID_IDirectDraw4,
(LPVOID *)&lpdd)))
return(0);
// set cooperation to full screen
if (FAILED(lpdd->SetCooperativeLevel(main_window_handle,
DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
return(0);
// set display mode to 640x480x8
if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,0,0)))
return(0);
// clear ddsd and set size
DDRAW_INIT_STRUCT(ddsd);
// enable valid fields
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
// set the backbuffer count field to 1, use 2 for triple buffering
ddsd.dwBackBufferCount = 1;
// request a complex, flippable
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
// create the primary surface
if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL)))
return(0);
// now query for attached surface from the primary surface
// this line is needed by the call
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
// get the attached back buffer surface
if (FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsback)))
return(0);
// build up the palette data array
for (int color=1; color < 255; color++)
{
// fill with random RGB values
palette[color].peRed = rand()%256;
palette[color].peGreen = rand()%256;
palette[color].peBlue = rand()%256;
// set flags field to PC_NOCOLLAPSE
palette[color].peFlags = PC_NOCOLLAPSE;
} // end for color
// now fill in entry 0 and 255 with black and white
palette[0].peRed = 0;
palette[0].peGreen = 0;
palette[0].peBlue = 0;
palette[0].peFlags = PC_NOCOLLAPSE;
palette[255].peRed = 255;
palette[255].peGreen = 255;
palette[255].peBlue = 255;
palette[255].peFlags = PC_NOCOLLAPSE;
// make some primary colors
palette[1].peRed = 255;
palette[1].peGreen = 0;
palette[1].peBlue = 0;
palette[1].peFlags = PC_NOCOLLAPSE;
palette[2].peRed = 0;
palette[2].peGreen = 255;
palette[2].peBlue = 0;
palette[2].peFlags = PC_NOCOLLAPSE;
palette[3].peRed = 0;
palette[3].peGreen = 0;
palette[3].peBlue = 255;
palette[3].peFlags = PC_NOCOLLAPSE;
// create the palette object
if (FAILED(lpdd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 |
DDPCAPS_INITIALIZE,
palette,&lpddpal, NULL)))
return(0);
// finally attach the palette to the primary surface
if (FAILED(lpddsprimary->SetPalette(lpddpal)))
return(0);
// clear the surfaces out
DDraw_Fill_Surface(lpddsprimary, 0 );
DDraw_Fill_Surface(lpddsback, 0 );
// define points of ship
VERTEX2DF ship_vertices[24] =
{1,11,
2,8,
1,7,
1,-1,
3,-1,
3,-2,
11,-3,
11,-6,
3,-7,
2,-8,
1,-8,
1,-7,
-1,-7,
-1,-8,
-2,-8,
-3,-7,
-11,-6,
-11,-3,
-3,-2,
-3,-1,
-1,-1,
-1,7,
-2,8,
-1, 11};
// initialize ship
ship.state = 1; // turn it on
ship.num_verts = 24;
ship.x0 = SCREEN_WIDTH/2; // position it
ship.y0 = SCREEN_HEIGHT/2;
ship.xv = 0;
ship.yv = 0;
ship.color = 2; // green
ship.vlist = new VERTEX2DF [ship.num_verts];
for (int index = 0; index < ship.num_verts; index++)
ship.vlist[index] = ship_vertices[index];
// do a quick scale on the vertices, they are a bit too small
Scale_Polygon2D(&ship, 3.0, 3.0);
// create sin/cos lookup table
// generate the tables
for (int ang = 0; ang < 360; ang++)
{
// convert ang to radians
float theta = (float)ang*PI/(float)180;
// insert next entry into table
cos_look[ang] = cos(theta);
sin_look[ang] = sin(theta);
} // end for ang
// hide the cursor
ShowCursor(0);
// return success or failure or your own return code here
return(1);
} // end Game_Init
/////////////////////////////////////////////////////////////
int Game_Shutdown(void *parms = NULL, int num_parms = 0)
{
// this is called after the game is exited and the main event
// loop while is exited, do all you cleanup and shutdown here
// first the palette
if (lpddpal)
{
lpddpal->Release();
lpddpal = NULL;
} // end if
// now the back buffer surface
if (lpddsback)
{
lpddsback->Release();
lpddsback = NULL;
} // end if
// now the primary surface
if (lpddsprimary)
{
lpddsprimary->Release();
lpddsprimary = NULL;
} // end if
// now blow away the IDirectDraw4 interface
if (lpdd)
{
lpdd->Release();
lpdd = NULL;
} // end if
// show the cursor
ShowCursor(1);
// return success or failure or your own return code here
return(1);
} // end Game_Shutdown
// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
HDC hdc; // graphics device context
// first fill in the window class stucture
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// save hinstance in global
hinstance_app = hinstance;
// register the window class
if (!RegisterClassEx(&winclass))
return(0);
// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"DirectDraw 8-Bit Polygon Matrix Transformation Demo", // title
WS_POPUP | WS_VISIBLE,
0,0, // initial x,y
SCREEN_WIDTH,SCREEN_HEIGHT, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms
return(0);
// save main window handle
main_window_handle = hwnd;
// initialize game here
Game_Init();
// enter main event loop
while(TRUE)
{
// test if there is a message in queue, if so get it
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// test if this is a quit
if (msg.message == WM_QUIT)
break;
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
} // end if
// main game processing goes here
Game_Main();
} // end while
// closedown game here
Game_Shutdown();
// return to Windows like this
return(msg.wParam);
} // end WinMain
///////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -