Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Johannes1991

Member Since 05 Jan 2009
Offline Last Active Sep 16 2014 12:55 PM

Topics I've Started

Win32 input crash

12 October 2013 - 05:07 PM

Hi

I've recently moved from directInput to using win32 for input and I've been experienced some hold ups in my program.
The window stops responding for a  split second and works again, And the longer the program runs the longer the stops gets.
If the program loses focus and gets it back then the program stops responding completely and has to be terminated.
I'm suspecting it's the game loop and/or use of peekmessage()! 

GameLoop
 

MSG msg;
bool gameRunning = true;
uint lastFrame = frameTimeStamp;

while(gameRunning)
{	
	lastFrameTimeStamp = frameTimeStamp;
	frameTimeStamp = timeGetTime();		

	if(frameTimeStamp - lastFrame > 17)
	{
		elapsedTime = (float)(frameTimeStamp - lastFrame) * 0.001f;

		input.Update();

		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				gameRunning = false;
				break;
			}

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		game.Update();
		game.Render();

		lastFrame = frameTimeStamp;
	}
}

input class:
 

void HInput::Update()
{
	if (!wndHandle)
		return;

	keyEventCount = 0;
	mouseEventCount = 0;
	MSG msg;

	// shift current keystates to last keystates
	memcpy(lastKeyStates, currentKeyStates, sizeof(bool) * INPUT_KEY_COUNT);

	// intercept input messages from the window
	while (PeekMessage(&msg, wndHandle, WM_KEYFIRST , WM_KEYLAST, PM_REMOVE))
	{
		TranslateMessage(&msg);

		INPUT_KEY key = TranslateWin32Key(msg.wParam);

		INPUT_KEY button;
		int x = GET_X_LPARAM(msg.lParam);
		int y = GET_Y_LPARAM(msg.lParam);
		
		switch(msg.message)
		{
		case WM_KEYDOWN:
			if (key == KEY_UNSUPPORTED)
				break;

			SET_KEY(key);
			AddKeyEvent(key);
			break;
		case WM_KEYUP:
			if (key == KEY_UNSUPPORTED)
				break;

			CLEAR_KEY(key);
			break;
		case WM_CHAR:
			AddCharEvent(msg.wParam);
			break;
		}
	}

	POINT pos;
	GetCursorPos(&pos);
	ScreenToClient(wndHandle, &pos);
	
	deltaX = pos.x - mouseX;
	deltaY = pos.y - mouseY;

	mouseX = pos.x;
	mouseY = pos.y;

	while (PeekMessage(&msg, wndHandle, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE))
	{
		INPUT_KEY button;
		int x = GET_X_LPARAM(msg.lParam);
		int y = GET_Y_LPARAM(msg.lParam);		

		switch(msg.message)
		{
		case WM_LBUTTONDOWN:
			SET_KEY(MOUSE_LMB);
			AddMouseEvent(IMET_CLICK, MOUSE_LMB, x, y);
			break;
		case WM_LBUTTONUP:
			CLEAR_KEY(MOUSE_LMB);
			AddMouseEvent(IMET_RELEASE, MOUSE_LMB, x, y);
			break;
		case WM_LBUTTONDBLCLK:
			AddMouseEvent(IMET_DOUBLECLICK, MOUSE_LMB, x, y);
			break;
		case WM_RBUTTONDOWN:
			SET_KEY(MOUSE_RMB);
			AddMouseEvent(IMET_CLICK, MOUSE_RMB, x, y);
			break;
		case WM_RBUTTONUP:
			CLEAR_KEY(MOUSE_RMB);
			AddMouseEvent(IMET_RELEASE, MOUSE_RMB, x, y);
			break;
		case WM_RBUTTONDBLCLK:
			AddMouseEvent(IMET_DOUBLECLICK, MOUSE_RMB, x, y);
			break;
		case WM_MBUTTONDOWN:
			SET_KEY(MOUSE_MMB);
			AddMouseEvent(IMET_CLICK, MOUSE_MMB, x, y);
			break;
		case WM_MBUTTONUP:
			CLEAR_KEY(MOUSE_MMB);
			AddMouseEvent(IMET_RELEASE, MOUSE_MMB, x, y);
			break;
		case WM_MBUTTONDBLCLK:
			AddMouseEvent(IMET_DOUBLECLICK, MOUSE_MMB, x, y);
			break;
		case WM_XBUTTONDOWN:	// for both x buttons
			if (HIWORD(msg.wParam) & XBUTTON1)
				button = MOUSE_M4B;
			else if (HIWORD(msg.wParam) & XBUTTON2)
				button = MOUSE_M5B;

			SET_KEY(button);
			AddMouseEvent(IMET_CLICK, button, x, y);
			break;
		case WM_XBUTTONUP:
			if (HIWORD(msg.wParam) & XBUTTON1)
				button = MOUSE_M4B;
			else if (HIWORD(msg.wParam) & XBUTTON2)
				button = MOUSE_M5B;

			CLEAR_KEY(button);
			AddMouseEvent(IMET_RELEASE, button, x, y);
			break;
		case WM_XBUTTONDBLCLK:
			if (HIWORD(msg.wParam) & XBUTTON1)
				button = MOUSE_M4B;
			else if (HIWORD(msg.wParam) & XBUTTON2)
				button = MOUSE_M5B;

			AddMouseEvent(IMET_DOUBLECLICK, button, x, y);
			break;
		}
	}
}

 


Compile Shader From Memory

28 February 2013 - 10:33 AM

I have started making my own shader loader and of course it's not working! When I read from the shader file i separate the hlsl code and my own code into different strings and when i try to compile the shader code with D3DX11CompileFromMemory() I get this error message:
"error X3000: syntax error: unexpected end of file"

I have put a null terminator in the end of the string but it doesn't work...
So anyone know the reason for this problem?

Code:

std::fstream file(path, std::ios::in);
std::string shaderCode = "", tempBuffer;

if(file.is_open())
{
        while(!file.eof())
        {
            //file.get(tempBuffer);
            file >> tempBuffer;

            if(tempBuffer == "ShaderInformation")
            {
                int i = 0;
                i += 0;
            }
            else
            {
                shaderCode += " " + tempBuffer;
            }
        }
}

shaderCode += '\0';

HRESULT hr;
ID3D10Blob *vsBlob, *psBlob, *errorBlob;
	
//Dummy hlsl code
const char temp[] = 
"Texture2D colormap;"
"Texture2D lightmap;"

"SamplerState fullscreenQuadSampler;"

"struct VertexInput"
"{"
	"float3 position : POSITION0;"
	"float2 uv : TEXCOORD0;"
"};"

"struct PixelInput"
"{"
	"float4 position : SV_Position;"
	"float2 uv : TEXCOORD0;"
"};"

"PixelInput VS(VertexInput input)"
"{"
	"PixelInput output;"

	"output.position = float4(input.position, 1.0f);"
	"float2 halfPixel = float2(0.5f / 800.0f, 0.5f / 600.0f);"
	"output.uv = input.uv;// + halfPixel;"

	"return output;"
"}"

"float4 PS(PixelInput input) : SV_Target"
"{"
	"float3 diffuseColor = colormap.Sample(fullscreenQuadSampler, input.uv).rgb;"
	"float4 lightData = lightmap.Sample(fullscreenQuadSampler, input.uv);"
   
	"float3 diffuseLight = lightData.rgb;"
	"float specularLight = lightData.a;"

	"return float4(diffuseColor * diffuseLight + specularLight, 1.0f);"
"}"
"\0";		
	
hr = D3DX11CompileFromMemory(temp,
		sizeof(temp),
		"FinalComposition.hlsl",
		NULL,
		NULL,
		"VS",
		"vs_4_0",
		D3D10_SHADER_PACK_MATRIX_ROW_MAJOR,
		NULL,
		NULL,
		&vsBlob,
		&errorBlob,
		NULL);


if(hr != S_OK)
{
	if(!errorBlob)
		return false;

	char msg[20000];
	strcpy_s(msg, sizeof(msg), (char*)errorBlob->GetBufferPointer());
	OutputDebugStringA(msg);
	MessageBoxA(GetDesktopWindow(), msg, "VertexShader Compilation Failed!", MB_OK | MB_ICONERROR);
	errorBlob->Release();

	return false;
}

 

 


My Server/Client lagging crazy

18 January 2012 - 09:53 AM

I've been playing around with winsock now from time to time and i decided to make a test server and client. I got the server up and going and the client works aswell. But there's a delay of 1 - 2 secs, just now i'm only sending the positions back and forth.

First when i used send() i set the len paramter with sizeof(package). But it made my client side unstable. But when i set a constant size it became very stable.

The code is in c++,

Server code,(process package is in the client too):
bool Server::Initialize_Server(UINT MaxClients)
{
this->MaxClients = MaxClients;
ClientAddresses = new sockaddr_in[MaxClients];
ClientSockets = new SOCKET[MaxClients];
AddressSize = sizeof(sockaddr_in);
NumberOfClients = 0;
if(WSAStartup(MAKEWORD(2,2), &Winsock) == WSAVERNOTSUPPORTED)
{
  cout << "\n\nCouldn't Start Winsock\n\n";
  WSACleanup();
  return false;
}

HostSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(HostSocket == INVALID_SOCKET)
{
  cout << "\n\nServer Socket Couldn't Be Created\n\n";
  WSACleanup();
  return false;
}
ZeroMemory(&HostAddress, sizeof(sockaddr_in));
HostAddress.sin_family = AF_INET;
HostAddress.sin_port = htons(PortNumber);
HostAddress.sin_addr.s_addr = INADDR_ANY;
if(bind(HostSocket, (sockaddr *) &HostAddress, sizeof(sockaddr_in)) != 0)
	{
  int i = WSAGetLastError();
  cout << "Couldn't Bind Socket";
  WSACleanup();
		return false;
	}
if(listen(HostSocket, MaxClients) != 0)
	{
  cout << "Couldn't Listen To Socket";
  WSACleanup();
		return false;
	}
u_long iMode = 1;
ioctlsocket(HostSocket, FIONBIO, &iMode);
MultiPlayer_1();
return true;
}
void Server::Process_Packages()
{
int PackageSize, PointerPos;
char* tempPackage;
for(int i = 0; i < NumberOfClients; i++)
{
  PointerPos = PackageSize = 0;
  ZeroMemory(Buffer, BufferSize);
  PackageSize = recv(ClientSockets[i], Buffer, BufferSize, NULL);
  while(PointerPos < PackageSize)
  {
   char PackageID = *((char *) (Buffer + PointerPos));
   switch(PackageID)
   {
   case PackageID_Player:
	{
	 //tempPackage = Network_Stuff.Check_Complete_Package(sizeof(GroupUnitInfos_Package), &PackageSize, 512, &PointerPos, (char *&) *Buffer);
	 if(!Check_Package_Whole(&ClientSockets[i], &PointerPos, sizeof(Player_Package), &PackageSize, tempPackage))
	 {
	  Player_Package* temp = (Player_Package *) tempPackage;
	  Player* tempPlayer = (Player *) GameData.ObjectList[temp->Player_ID];
	  tempPlayer->Set_Position(temp->Position);
	  tempPlayer->Set_Direction(temp->Direction);
	  delete[] tempPackage;
	 }else
	 {
	  Player_Package* temp = (Player_Package *) (Buffer + PointerPos);
	  PointerPos += sizeof(Player_Package);
	  Player* tempPlayer = (Player *) GameData.ObjectList[temp->Player_ID];
	  tempPlayer->Set_Position(temp->Position);
	  tempPlayer->Set_Direction(temp->Direction);
	 }
	}break;
   default:
	{
	
	 cout << "\n\nCouldn't Identify Package\nPackage Size: " << PackageSize * 0.001f << "kb\n\n";
	 PointerPos = PackageSize;
	}
	break;
   }
  }
}
}
bool Server::Check_Package_Whole(SOCKET* const Socket, int* PointerPos, const int SizeOfPackage, int* RevievedPackageSize, char *& tempBuffer)
{
if(*RevievedPackageSize - *PointerPos < sizeof(SizeOfPackage))
{
  int PartSizeInBuffer = *RevievedPackageSize - *PointerPos;
  tempBuffer = new char[sizeof(SizeOfPackage)];
  memcpy(tempBuffer, (char *)(Buffer + *PointerPos), PartSizeInBuffer);
  *RevievedPackageSize = recv(*Socket, Buffer, BufferSize, NULL);
  memcpy((char *) (tempBuffer + PartSizeInBuffer), Buffer, sizeof(SizeOfPackage) - PartSizeInBuffer);
  *PointerPos = sizeof(SizeOfPackage) - PartSizeInBuffer;
  return false;
}
else
  return true;
}
void Server::Update_Clients()
{
Player_Package Package;
for(int i = 0; i < NumberOfClients; i++)
{
  Package.Player_ID = i;
  Package.Position = GameData.ObjectList[i]->Get_Position();
  Package.Direction = GameData.ObjectList[i]->Get_Direction();
  for(int c = 0; c < NumberOfClients; c++)
  {
   if(c == i)
	continue;
   send(ClientSockets[c], (char *) &Package, BufferSize, NULL);
  }
}
}

Turn Object around Point

26 October 2011 - 07:06 AM

I've been trying to rotate a box around a certain point (for the purpose of angular velocity). I Know how to rotate the mesh around a point by changing the origo in mesh space, but the problem is that the i don't know how to rotate the Position variable too. Because if i change the rotation point(and the mesh origor) then the box "jumps" so the new origo in mesh space.

Here's a pic for further explanation:
Attached File  Pic.png   4.37KB   32 downloads
The Plus is the Box Mesh Origo and the red circle is the Rotation Point.

Thanx in advance

Turn Object around Point

26 October 2011 - 07:06 AM

I've been trying to rotate a box around a certain point (for the purpose of angular velocity). I Know how to rotate the mesh around a point by changing the origo in mesh space, but the problem is that the i don't know how to rotate the Position variable too. Because if i change the rotation point(and the mesh origor) then the box "jumps" so the new origo in mesh space.

Here's a pic for further explanation:
Attached File  Pic.png   4.37KB   32 downloads
The Plus is the Box Mesh Origo and the red circle is the Rotation Point.

Thanx in advance

PARTNERS