⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tmapdemo.cpp

📁 3D游戏开发领域专家撰写的经典游戏开发启迪性文章之一 到此为止所有的文章都已上传完毕,希望大家喜欢,以后会继续收集相关质料供大家分享
💻 CPP
📖 第 1 页 / 共 2 页
字号:
       {
       // extract RGB components (in BGR order), note the scaling
       UCHAR blue  = (temp_buffer[index*3 + 0] >> 3),
             green = (temp_buffer[index*3 + 1] >> 3),
             red   = (temp_buffer[index*3 + 2] >> 3);

       // build up 16 bit color word
       USHORT color = _RGB16BIT(red,green,blue);

       // write color to buffer
       ((USHORT *)bitmap->buffer)[index] = color;

       } // end for index

   // finally write out the correct number of bits
   bitmap->bitmapinfoheader.biBitCount=16;

   } // end if


// 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 upside down .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

///////////////////////////////////////////////////////////////////////////////

int Set_Palette(LPPALETTEENTRY set_palette)
{
// this function writes the sent palette

// first save the new palette in shadow
memcpy(palette, set_palette,256*sizeof(PALETTEENTRY));

// now set the new palette
lpddpal->SetEntries(0,0,256,palette);

// return success
return(1);
} // end Set_Palette


// WINDOWS CALLBACK FUNCTION //////////////////////////////////////////////////		             

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
// this is the main message handler of the system

HDC			hdc;		// handle to graphics context
PAINTSTRUCT ps;			// used to hold the paint info

// what is the message?

switch(msg)
	{	
	case WM_CREATE:
		{
		// do windows inits here
		return(0);
		} break;

	case WM_PAINT:
		{
		// this message occurs when your window needs repainting
		hdc = BeginPaint(hwnd,&ps);	 
		EndPaint(hdc,&ps);
		
		return(0);
   		} break;

	case WM_DESTROY:
		{
		// this message is sent when your window is destroyed
		PostQuitMessage(0);
		return(0);
		} break;

	default:break;

    } // end switch

// let windows process any messages that we didn't take care of 
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

// WINMAIN ////////////////////////////////////////////////////////////////////

int WINAPI WinMain(	HINSTANCE hinstance,
					HINSTANCE hprevinstance,
					LPSTR lpcmdline,
					int ncmdshow)
{
WNDCLASSEX		winclass;	// this holds the windows class info
HWND			hwnd;		// this holds the handle of our new window
MSG				msg;		// this holds a generic message

// 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.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground	= GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName	= NULL;
winclass.lpszClassName	= WINDOW_CLASS_NAME;

// register the window class
if (!RegisterClassEx(&winclass))
	return(0);

// create the window
if (!(hwnd = CreateWindowEx(0, 
							WINDOW_CLASS_NAME,					// class
							"Texture Mapping Demo!",			// title
							WS_OVERLAPPED | WS_VISIBLE,
					 		0,0,								// x,y
							320, // GetSystemMetrics(SM_CXSCREEN),
							240, // GetSystemMetrics(SM_CYSCREEN),				
							NULL,								// parent
							NULL,								// menu
							hinstance,							// instance
							NULL)))								// creation parms
return(0);

// hide the mouse cursor
ShowCursor(0);

// save the window handle
main_window_handle = hwnd;

// initialize direct draw
if (!DD_Init(hwnd))
   {
   DestroyWindow(hwnd);
   return(0);
   } // end if

// initialize game
Game_Init();

// enter main event loop
while(1)
	{
	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
	else
		{
		// do asynchronous processing here

		// acquire pointer to video ram, note it is always linear
		memset(&ddsd,0,sizeof(ddsd));
		ddsd.dwSize = sizeof(ddsd);
		lpddsprimary->Lock(NULL,&ddsd,DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,NULL);

        // get video pointer
		video_buffer = (UCHAR *)ddsd.lpSurface;

        // get video pitch, should be SCREEN_WIDTH, but possibly not!
        mem_pitch = (int)ddsd.lPitch;

		// call main logic module
		Game_Main();

		// release pointer to video ram
		lpddsprimary->Unlock(ddsd.lpSurface);
		
		} // end else

	} // end while

// shut down direct draw
DD_Shutdown();

// shutdown game
Game_Shutdown();

// return to Windows
return(msg.wParam);

} // end WinMain

// HERE ARE OUR GAME CONSOLE FUNCTIONS ///////////////////////////////////////////////////////

void Game_Init(void)
{
// do any initialization here

// load in the bitmap file here, 64x64 8 bit
Load_Bitmap_File(&bitmap, "texture.bmp");

// set new palette
Set_Palette(bitmap.palette);

} // end Game_Init

/////////////////////////////////////////////////////////////////////////////////////////////

void Game_Shutdown(void)
{
// cleanup and release all resources here

// release bitmap memory
Unload_Bitmap_File(&bitmap);

} // end Game_Shutdown

/////////////////////////////////////////////////////////////////////////////////////////////

void Game_Main(void)
{
// process your game logic and draw next frame
// this function must exit each frame and return back to windows
// also this function is responsible for controlling the frame rate
// therefore, if you want the game to run at 60fps, then the function should
// take 1/60th of a second before returning

// on entry video_buffer and mem_pitch are valid

// get input, note how keyboard is accessed with constants "VK_"
// these are defined in "winuser.h" are basically the scan codes
// for letters just use capital ASCII codes

FACE3D face; // the triangle object

// test for exit
if (KEY_DOWN(VK_ESCAPE))
	PostMessage(main_window_handle,WM_DESTROY,0,0); 

// draw 100 polys a frame
for (int polys=0; polys < 100; polys++)
{
// set up the vertices
face.tlist[0].x = rand()%SCREEN_WIDTH;
face.tlist[0].y = rand()%SCREEN_HEIGHT;
face.tlist[0].u = 0;
face.tlist[0].v = 0;

face.tlist[1].x = rand()%SCREEN_WIDTH;
face.tlist[1].y = rand()%SCREEN_HEIGHT;
face.tlist[1].u = 0;
face.tlist[1].v = 63;

face.tlist[2].x = rand()%SCREEN_WIDTH;
face.tlist[2].y = rand()%SCREEN_HEIGHT;
face.tlist[2].u = 63;
face.tlist[2].v = 63;

// assign the texture to your 64x64 texture map buffer
face.texture = bitmap.buffer;

// set the clipping coords of the desired 2D viewport
poly_clip_min_x = 0; 
poly_clip_max_x = SCREEN_WIDTH-1;
poly_clip_min_y = 0;
poly_clip_max_y = SCREEN_HEIGHT-1;

// then draw the triangle to the rendering buffer
Draw_Textured_Triangle((void *)&face, video_buffer, mem_pitch);

} // end for polys

// use sleep to slow system down to 60ish fps
Sleep(15);

// return and let main event loop work a bit

} // end Game_Main

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -