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

📄 matsysapp.cpp

📁 hl2 source code. Do not use it illegal.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*

  glapp.c - Simple OpenGL shell
  
	There are several options allowed on the command line.  They are:
	-height : what window/screen height do you want to use?
	-width  : what window/screen width do you want to use?
	-bpp    : what color depth do you want to use?
	-window : create a rendering window rather than full-screen
	-fov    : use a field of view other than 90 degrees
*/

#include "stdafx.h"

#pragma warning(disable:4305)
#pragma warning(disable:4244)
#include <windows.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <mmsystem.h>
#include "matsysapp.h"
#include "cmdlib.h"
#include "imaterialproxyfactory.h"
#include "filesystem.h"
#include "imaterialproxy.h"
#include "MaterialSystem_Config.h"
#include "vstdlib/icommandline.h"

static int g_nCapture = 0;


#define OSR2_BUILD_NUMBER 1111
#define M_PI		3.14159265358979323846	// matches value in gcc v2 math.h


SpewRetval_t MatSysAppSpewFunc( SpewType_t type, char const *pMsg )
{
	printf( "%s", pMsg );
	OutputDebugString( pMsg );

	if( type == SPEW_ASSERT )
		return SPEW_DEBUGGER;
	else if( type == SPEW_ERROR )
		return SPEW_ABORT;
	else
		return SPEW_CONTINUE;
}


class MatSysAppMaterialProxyFactory : public IMaterialProxyFactory
{
public:
	virtual IMaterialProxy *CreateProxy( const char *proxyName )
	{
		CreateInterfaceFn clientFactory = Sys_GetFactoryThis();
		if( !clientFactory )
		{
			return NULL;
		}
		// allocate exactly enough memory for the versioned name on the stack.
		char proxyVersionedName[1024];
		strcpy( proxyVersionedName, proxyName );
		strcat( proxyVersionedName, IMATERIAL_PROXY_INTERFACE_VERSION );

		IMaterialProxy *materialProxy;
		materialProxy = ( IMaterialProxy * )clientFactory( proxyVersionedName, NULL );
		if( !materialProxy )
		{
			return NULL;
		}
		
		return materialProxy;
	}

	virtual void DeleteProxy( IMaterialProxy *pProxy )
	{
		if( pProxy )
		{
			pProxy->Release();
		}
	}
};
MatSysAppMaterialProxyFactory	g_MatSysAppMaterialProxyFactory;


MaterialSystemApp	g_MaterialSystemApp;

float       fAngle = 0.0f;

char       *szFSDesc[] = { "Windowed", "Full Screen" };

extern "C" unsigned int g_Time;
unsigned int g_Time = 0;



int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	return g_MaterialSystemApp.WinMain(hInstance, hPrevInstance, szCmdLine, iCmdShow);
}

  

BOOL isdigits( char *s )
{
   int i;
   
   for (i = 0; s[i]; i++)
   {
	   if ((s[i] > '9') || (s[i] < '0'))
       {
		   return FALSE;
       }
   }
   return TRUE;
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	return g_MaterialSystemApp.WndProc(hwnd, iMsg, wParam, lParam);
}

// This function builds a list the screen resolutions supported by the display driver
static void BuildModeList(screen_res_t* &pResolutions, int &iResCount)
{
   DEVMODE  dm;
   int      mode;
   
   mode = 0;
   while(EnumDisplaySettings(NULL, mode, &dm))
   {
	   mode++;
   }
   
   pResolutions = (screen_res_t *)malloc(sizeof(screen_res_t)*mode);
   mode = 0;
   while(EnumDisplaySettings(NULL, mode, &dm))
   {
	   pResolutions[mode].width = dm.dmPelsWidth;
	   pResolutions[mode].height = dm.dmPelsHeight;
	   pResolutions[mode].bpp = dm.dmBitsPerPel;
	   pResolutions[mode].flags = dm.dmDisplayFlags;
	   pResolutions[mode].frequency = dm.dmDisplayFrequency;
	   mode++;
   }
   iResCount = mode;
}


bool Sys_Error(const char *pMsg, ...)
{
	va_list marker;
	char msg[4096];
	
	va_start(marker, pMsg);
	vsprintf(msg, pMsg, marker);
	va_end(marker);

	MessageBox(NULL, msg, "FATAL ERROR", MB_OK);

	g_MaterialSystemApp.Term();
	exit(1);
	return false;
}


void con_Printf(const char *pMsg, ...)
{
	char msg[2048];
	va_list marker;

	va_start(marker, pMsg);
	vsprintf(msg, pMsg, marker);
	va_end(marker);

	OutputDebugString(msg);
}


bool MSA_IsKeyDown(char key)
{
	return !!(GetAsyncKeyState(key) & 0x8000);
}


bool MSA_IsMouseButtonDown( int button )
{
	if( button == MSA_BUTTON_LEFT )
		return !!(GetAsyncKeyState(VK_LBUTTON) & 0x8000);
	else
		return !!(GetAsyncKeyState(VK_RBUTTON) & 0x8000);
}


void MSA_Sleep(unsigned long count)
{
	if(count > 0)
		Sleep(count);
}


static void MaterialSystem_Error( char *fmt, ... )
{
	char str[4096];
	va_list marker;
	
	va_start(marker, fmt);
	vsprintf(str, fmt, marker);
	va_end(marker);
	
	Sys_Error(str);
}


static void MaterialSystem_Warning( char *fmt, ... )
{
}


void InitMaterialSystemConfig(MaterialSystem_Config_t *pConfig, const char *matPath)
{
	memset( pConfig, 0, sizeof(*pConfig) );
	pConfig->screenGamma = 2.2f;
	pConfig->texGamma = 2.2;
	pConfig->overbright = 2;
	pConfig->bAllowCheats = false;
	pConfig->bLinearFrameBuffer = false;
	pConfig->polyOffset = 4;
	pConfig->skipMipLevels = 0;
	pConfig->lightScale = 1.0f;
	pConfig->bFilterLightmaps = true;
	pConfig->bFilterTextures = true;
	pConfig->bMipMapTextures = true;
	pConfig->bShowMipLevels = false;
	pConfig->bReverseDepth = false;
	pConfig->bCompressedTextures = false;
	pConfig->bBumpmap = false;
	pConfig->bLightingOnly = false;
	pConfig->errorFunc = MaterialSystem_Error;
	pConfig->warningFunc = MaterialSystem_Warning;
	pConfig->bUseGraphics = true;
	pConfig->bEditMode = false;	// No, we're not in WorldCraft.
}


/*
====================
CalcFov
====================
*/
float CalcFov (float fov_x, float width, float height)
{
	float	a;
	float	x;

	if (fov_x < 1 || fov_x > 179)
		fov_x = 90;	// error, set to 90

	x = width/tan(fov_x/360*M_PI);

	a = atan (height/x);

	a = a*360/M_PI;

	return a;
}


MaterialSystemApp::MaterialSystemApp()
{
	Clear();
}

MaterialSystemApp::~MaterialSystemApp()
{
	Term();
}

void MaterialSystemApp::Term()
{
   int i;

	if( m_pFileSystemDLL )
	{
		Sys_UnloadModule( m_pFileSystemDLL );
		m_pFileSystemDLL = NULL;
	}
   
   // Free the command line holder memory
   if (m_argc > 0)
   {
	   // Free in reverse order of allocation
	   for (i = (m_argc-1); i >= 0; i--)
       {
		   free(m_argv[i]);
       }
	   // Free the parameter "pockets"
	   free(m_argv);
   }
	
  
   // Free the memory that holds the video resolution list
   if (m_pResolutions)
	   free(m_pResolutions);

   if (m_hDC)
   {
	   if (!ReleaseDC((HWND)m_hWnd, (HDC)m_hDC))
       {
		   MessageBox(NULL, "ShutdownOpenGL - ReleaseDC failed\n", "ERROR", MB_OK);
       }
	   m_hDC   = NULL;
   }
   if (m_bFullScreen)
   {
	   ChangeDisplaySettings( 0, 0 );
   }

	Clear();
}

void MaterialSystemApp::Clear()
{
	m_pFileSystemDLL = NULL;
	m_pMaterialSystem = NULL;
	m_hMaterialSystemInst = 0;
	m_hInstance = 0;
	m_iCmdShow = 0;
	m_hWnd = 0;
	m_hDC = 0;
	m_bActive = false;
	m_bFullScreen = false;
	m_width = m_height = 0;
	m_centerx = m_centery = 0;
	m_bpp = 0;
	m_bChangeBPP = false;
	m_bAllowSoft = 0;
	g_nCapture = 0;
	m_szCmdLine = 0;
	m_argc = 0;
	m_argv = 0;
	m_glnWidth = 0;
	m_glnHeight = 0;
	m_gldAspect = 0;
	m_NearClip = m_FarClip = 0;
	m_fov = 90;
	m_pResolutions = 0;
	m_iResCount = 0;
	m_iVidMode = 0;
}

int MaterialSystemApp::WinMain(void *hInstance, void *hPrevInstance, char *szCmdLine, int iCmdShow)
{
	MSG         msg;
    HDC         hdc;
	
	CommandLine()->CreateCmdLine( szCmdLine );

    // Not changable by user
    m_hInstance    = hInstance;
    m_iCmdShow     = iCmdShow;
    m_pResolutions = 0;
    m_NearClip     = 8.0f;
    m_FarClip      = 28400.0f;
	
    // User definable
    m_fov          = 90.0f;
    m_bAllowSoft   = FALSE;
    m_bFullScreen  = TRUE;
	
    // Get the current display device info
    hdc = GetDC( NULL );
    m_DevInfo.bpp    = GetDeviceCaps(hdc, BITSPIXEL);
    m_DevInfo.width  = GetSystemMetrics(SM_CXSCREEN);
    m_DevInfo.height = GetSystemMetrics(SM_CYSCREEN);
    ReleaseDC(NULL, hdc);
	
    // Parse the command line if there is one
    m_argc = 0;
    if (strlen(szCmdLine) > 0)
	{
        m_szCmdLine = szCmdLine;
        GetParameters();
	}
	
    // Default to 640 pixels wide
    m_width  = FindNumParameter("-width", 640);
    m_height = FindNumParameter("-height", 480);
    m_bpp    = FindNumParameter("-bpp", 32);
    m_fov    = FindNumParameter("-fov", 90);
	
    // Check for windowed rendering
    m_bFullScreen = FALSE;
    if (FindParameter("-fullscreen"))
	{
        m_bFullScreen = TRUE;
	}
	
    // Build up the video mode list
    BuildModeList(m_pResolutions, m_iResCount);
	
    // Create the main program window, start up OpenGL and create our viewport
    if (CreateMainWindow( m_width, m_height, m_bpp, m_bFullScreen) != TRUE)
	{
        ChangeDisplaySettings(0, 0);
        MessageBox(NULL, "Unable to create main window.\nProgram will now end.", "FATAL ERROR", MB_OK);
        Term();
        return 0;
	}

    // Turn the cursor off for full-screen mode
    if (m_bFullScreen == TRUE)
	{ 
		// Probably want to do this all the time anyway
        ShowCursor(FALSE);
	}

    // We're live now
    m_bActive = TRUE;
	
	// Define this funciton to init your app
	AppInit();


	RECT rect;
	GetWindowRect( (HWND)m_hWnd, &rect );

	m_centerx = ( rect.left + rect.right ) / 2;
	m_centery = ( rect.top + rect.bottom ) / 2;


    // Begin the main program loop
    while (m_bActive == TRUE)
	{
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
            TranslateMessage (&msg);
            DispatchMessage (&msg);
		}
        
		if (m_pMaterialSystem)
		{
            RenderScene();
		}
	}
    if (m_bFullScreen == TRUE)
	{
        ShowCursor(TRUE);
	}
    
	// Release the parameter and video resolution lists
	Term();
	
	// Tell the app to cleanup.
	AppExit();
    return msg.wParam;
}


long MaterialSystemApp::WndProc(void *inhwnd, long iMsg, long wParam, long lParam)
{
	if(inhwnd != m_hWnd)
	{
		return DefWindowProc((HWND)inhwnd, iMsg, wParam, lParam);
	}

	HWND hwnd = (HWND)inhwnd;
	
	switch (iMsg)
	{
		case WM_CHAR:
			switch(wParam)
			{
			case VK_ESCAPE:
				SendMessage(hwnd, WM_CLOSE, 0, 0);
				break;
			}
			AppChar( wParam );
		break;

⌨️ 快捷键说明

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