Wednesday 15 December 2010

Civ and Code

So, what have I been up to?  Well, since the yule tide is rolling over us I've been incredibly busy at work.

I have however been enjoying Civ5, I'm reasonably happy to play the game, though in DirectX 10 mode is does crash out sometimes.  Hopefully the up coming patch for it will sort somethings out.

In the world of code, especially at work though, I have been very busy.  I have a project to put together, but as an aside I've been looking at the boost C++ libraries, one little ditty I recently had to sort out was a hostname resolver for IPv4, here is that C++ for anyone interested in boost:

// Investigation into Boost - 14/12/2010
// (c)2010 - Jon Bond

///
/// Function to resolve a URL as an IP Address
///
std::string ResolveURLAsIPv4 (const std::string& URL, const int Port)
{
#ifdef _DEBUG
char* msg = Debugging::AssignMessage(128);
sprintf_s (msg, 128, "ResolveURLAsIP (%s : %i)", URL.c_str(), Port);
OutputDebugStringA (msg);
DWORD StartTick = GetTickCount();
#endif


std::string result = "127.0.0.1";

try 
{
// Construct the resolver service we're going to run
boost::asio::io_service IOService;
boost::asio::ip::tcp::resolver resolver(IOService);

// Set up the IP query we're going to run on the resolver service
boost::asio::ip::tcp::resolver::query query(
boost::asio::ip::tcp::v4(),
URL,
boost::lexical_cast(Port) );

// Set up the query we're going to iterate through
boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query);

// Iterate through all the end points returned by the DNS query
boost::asio::ip::tcp::resolver::iterator endIterator;
for ( ;
i != endIterator;
++i)
{
// Get the current end point indicated
boost::asio::ip::tcp::endpoint currentEndPoint = *i;

// Process the result
if ( currentEndPoint.address().is_v4() )
{
result = currentEndPoint.address().to_string();
// break out of the loop
break;
}
}
}
catch (std::exception e)
{
#ifdef _DEBUG
sprintf_s (msg, 128, "Error in ResolveURLAsIP.");
OutputDebugStringA (msg);
delete msg;
#endif

throw e;
}

#ifdef _DEBUG
DWORD Diff = GetTickCount() - StartTick;
sprintf_s (msg, 128, "ResolveURLAsIP Complete [%d] %s => %s", Diff, URL.c_str(), result.c_str() );
OutputDebugStringA (msg);
delete msg;
#endif

return result;
}
Please note, I know I could cut down the number of boost::asio:: whatever indirections by using 'using' statements, but this is fully written out so I could understand what was going on.

The other ditty I've been playing with is Direct2D as part of DirectX 10 on Vista & Windows 7... here's my class to initialise the Direct2D interface:

// Investigation into Direct2D - 3/12/2010
// (c)2010 - Jon Bond

#pragma once

// The program main
#include "..\Program\Program.h"

// Include the header
#include "D2D1.h"

// Include the library
#pragma comment (lib, "d2d1.lib")

namespace GuiMain
{

// The gui
class Gui
{
private:
// Shows whether there has been an error
bool m_Error;

// The window handle
HWND m_WindowHandle;

// The factory
ID2D1Factory *m_Factory;

// The render target
ID2D1HwndRenderTarget *m_Target;

// Factory handlers
void SetupFactory();
void CloseFactory();

// Create the resources
void CreateDeviceResources();
void ReleaseDeviceResources();

// Get the window size
D2D1_SIZE_U GetWindowSize ();

// A white solid brush
ID2D1SolidColorBrush* m_WhiteBrush;

public:

// The gui constructor
Gui (HWND WindowHandle);

// The gui destructor
~Gui ();

// Draw something
void Render ();

};


}

Code:

// Investigation into Direct2D - 3/12/2010
// (c)2010 - Jon Bond
#include "GuiMain.h"

namespace GuiMain
{

// Constructor
Gui::Gui(HWND WindowHandle)
{
m_Error = false;

// The window handle
m_WindowHandle = WindowHandle;

// Set up the factory
SetupFactory();
}

// Destructor
Gui::~Gui()
{
// Release the device drawing resources
ReleaseDeviceResources ();

// Close the factory
CloseFactory ();

m_WindowHandle = NULL;
}

// Set up the COM class factory for this interface
void Gui::SetupFactory()
{
HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_Factory);
if ( hr == S_OK )
{
CreateDeviceResources ();
}
}

// Close the factory
void Gui::CloseFactory()
{
// Release the factor, if it exists
if ( m_Factory != NULL )
{
m_Factory->Release();
m_Factory = NULL;
}
}

// The device resources
void Gui::CreateDeviceResources()
{
HRESULT hr ;

hr = m_Factory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(m_WindowHandle, GetWindowSize()),
&m_Target);
if ( hr != S_OK )
{
m_Error = true;
}
else
{
// Create the white brush
hr = this->m_Target->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&m_WhiteBrush);

if ( hr != S_OK )
{
m_Error = true;
}
}
}

// The release the device resources
void Gui::ReleaseDeviceResources()
{
m_WhiteBrush->Release();
// Dispose of the tar
if ( m_Target != NULL )
{
m_Target->Release();
m_Target = NULL;
}
}
// Return the screen size
D2D1_SIZE_U Gui::GetWindowSize ()
{
RECT rect;
GetClientRect (m_WindowHandle, &rect);
return D2D1::SizeU(rect.right-rect.left, rect.bottom-rect.top);
}
// Render something
void Gui::Render ()
{
m_Target->SetAntialiasMode(D2D1_ANTIALIAS_MODE::D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
m_Target->BeginDraw();
m_Target->Clear(D2D1::ColorF(D2D1::ColorF::Black));
m_Target->SetTransform(D2D1::Matrix3x2F::Identity());
D2D1_SIZE_U size = GetWindowSize();
m_Target->DrawLine (
D2D1::Point2F(0.0f, 0.0f),
D2D1::Point2F(size.width, size.height),
m_WhiteBrush,
0.5f
);
m_Target->EndDraw();
}
}

Please note, all my code here is copyright 2010 - Myself, but you may use it as you see fit.  So long as you leave my copyrght notices and name on the code.

1 comment:

  1. Oh god, such bad formatting, will fix this all later.

    ReplyDelete