Jump to content
  • Advertisement
Sign in to follow this  
NLScotty

Movement and basis

This topic is 2347 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

Today I started coding my own little game for fun. I would like to start small and make it bigger and bigger. I've got experience in C++, but not with the Direct2D (I use this, since I guess most uses are now vista +). I've got a few questions. I use a singleton class for some classes which should always exist in my process -> CUpdate and CDirect2D.

I'll show those here:


#ifndef CSingleton_h__
#define CSingleton_h__
//direct2D
#include "CDirect2D.hpp"
//Server
//Game
#include "CUpdate.hpp"
class CSingleton
{
public:
//Direct2D
static CDirect2D* GetCDirect2DInstance();
//Server

//Game
static CUpdate* GetCUpdateInstance();
private:
CSingleton(); // Private constructor prevents instantiation

//Direct2D
static CDirect2D* CDirect2DInstance;
//Server
//Game
static CUpdate* CUpdateInstance;
};

#endif // CSingleton_h__



#ifndef CDirect2D_h__
#define CDirect2D_h__
#include <d2d1.h>
#include <d2d1helper.h>
#include <windows.h>
class CDirect2D
{
public:
CDirect2D();
HRESULT CreateDeviceIndependentResources();
HRESULT CreateRenderTarget();
ID2D1HwndRenderTarget* GetRenderTarget();
void StartDrawing();
void EndDrawing();
void SetHwnd(HWND hwnd);
HWND GetHwnd();
private:
ID2D1Factory* Direct2dFactory;
ID2D1HwndRenderTarget* RenderTarget;
HWND hWnd;
};
#endif // CDirect2D_h__



#ifndef CUpdate_h__
#define CUpdate_h__
#include <Windows.h>
class CUpdate
{
public:
HRESULT Update();
private:
};
#endif // CUpdate_h__


And the main file:


#include <windows.h>
#include "CSingleton.hpp"
// This file is coded without a class, cause of the speed issue.
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT result = 0;
switch (message)
{
case WM_PAINT:
CSingleton::GetCUpdateInstance()->Update();
break;
case WM_SIZE:
break;
case WM_DISPLAYCHANGE:
InvalidateRect(hwnd, NULL, FALSE);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;
default:
result = DefWindowProc(hwnd, message, wParam, lParam);
break;
}
return result;
}
void RunMessageLoop()
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
HRESULT InitWindow()
{
HRESULT hr = S_OK;

WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = GetModuleHandle(0);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
wcex.lpszClassName = L"NazGame";
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow(
L"NazGame",
L"Game",
WS_OVERLAPPEDWINDOW,
0, //x
0, //y
CW_USEDEFAULT, //width
CW_USEDEFAULT, //height
0,
0,
GetModuleHandle(0),
0
);
hr = hWnd ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
CSingleton::GetCDirect2DInstance()->SetHwnd(hWnd);
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
}
return hr;
}
int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine, __in int nShowCmd )
{
HeapSetInformation(0, HeapEnableTerminationOnCorruption, 0, 0);
//Init Window
InitWindow();

//Init Direct2D
if(!SUCCEEDED(CSingleton::GetCDirect2DInstance()->CreateDeviceIndependentResources()))
return 0;
if(!SUCCEEDED(CSingleton::GetCDirect2DInstance()->CreateRenderTarget()))
return 0;
RunMessageLoop();
return 0;
}


Everything works fine, but is this the way to go? The first game I would like to create is a game that connects to a server, login. It stands in a 2d 'map'. With platforms where you can stand. And that the game runs smooth and movement is fine. That's all and that the server updates my x,y of the char. I've got this for the update class so far:


#include "CUpdate.hpp"
#include "CSingleton.hpp"
HRESULT CUpdate::Update()
{
HRESULT hr = S_OK;

// do game logic here

CSingleton::GetCDirect2DInstance()->CreateRenderTarget();
CSingleton::GetCDirect2DInstance()->StartDrawing();
//CSingleton::GetCDirect2DInstance()->GetRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
CSingleton::GetCDirect2DInstance()->GetRenderTarget()->Clear(D2D1::ColorF(D2D1::ColorF::LightBlue));
// draw
// CChar ...
CSingleton::GetCDirect2DInstance()->EndDrawing();
return hr;
}


The background is just for testing purpose. So how should I go about movement of my character? Sync it to the vsync? Handle user input with DirectInput of with the win32 API? Also if I send the info to my server it wil send something back, same for login. Should I create a second thread for handling packets from the server (in the begin i could easily handle this in the game logic, but what if there will become more pakcets? ).

Also I will later add dubbel buffering for better performances. Is this a right start or do I things wrong? Wrong thinking?

Thanks for reading,

Grz

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!