http://asawicki.info/ Programming, graphics, games, media, C++, Windows, Internet and more...
How to Run Windows Command with Given Working Directory?
The concept of "working directory" during program startup or "current directory" of the running process is an interesting topic in itself. Maybe I'll write another time about how to manipulate it in different programming languages. This time enough to say that some programs look for auxilliary files in working directory, some in the directory where own EXE file is located and some in other places like user's profile directory.
Problems begin when a program needs auxilliary files located in same directory as executable, but uses working directory to locate it. Apparently such program expects to always be ran with working directory equal to path of its EXE. It happened to me yesterday while using Windows port of Bison (parser generator). Error was:
win_bison: cannot open file `data/m4sugar/m4sugar.m4': No such file or directory
I can't just run the program with another working directory because I execute it from Visual C++, as a Custom Build Tool associated with my ".y" file. There is only place to enter a command in file property page, no place to change working directory, which is by default the directory of Visual C++ project I think.
The solution I found to be able to run a console command with given parameters and also with given working directory is to do it indirectly, using system "start" command, like this:
start /B /WAIT /D <WorkingDir> <ExePath> <Parameters>
Update 2012-11-29: I was informed that the problem in win_bison is now fixed so it can be used without such workaround.
Direct2D versus Direct3D
Direct2D library is not a completely new graphics API down to the driver level. It internally creates and uses Direct3D 10 device object. Of course it is a powerful library, not just a thin wrapper. But still everything ends up as a bunch of trianges uploaded to the buffer with Map/Unmap and sent to the GPU to be rendered using some vertex and pixel shader. PIX clearly shows all this:
As a result, Direct2D is able to exchange data with Direct3D 10. Unfortunately it's not the case with Direct3D 11. Topic How to use D2D with D3D11? at gamedev.net forum describes solution to this, full of problems and COM interface manipulation. Post DirectX Componentization and Interoperability by BenCon on DirectX Developer Blog nicely explains interop between different graphics API-s in Windows as a bigger picture. There is also a nasty hack to make D2D work (almost) directly with D3D11, described in post Hacking Direct2D to use directly Direct3D 11 instead of Direct3D 10.1 API on Code4k blog. But no clean and easy way exists.
So I give up with Direct2D for now. All in all, everything can be done by rendering triangles directly in Direct3D :P One thing I'll be missing is the perspective of easy high-quality text rendering with DirectWrite. Well, I'll go back to Bitmap Font Generator from AngelCode :P
Direct2D Class Diagram
I've made class diagram for Direct2D library. Enjoy :)
By the way, yEd is a free editor for all kinds of graphs, including UML diagrams like this. It may not allow you to enter list of fields and methods in classes like StarUML or Visio do. It may not allow to freely draw shapes like Inkscape or OpenOffice Draw do. But for creating, editing and arranging graphs of different kinds of nodes, interconnected with different kinds of links, it is really great. I especially like the way it snaps all positions and sizes to the neighbour nodes. Commands for automatic arranging of nodes, called Layout, also look impressive. Plus it supports different file formats, including XML-based, so you can easily generate such file and then open it in yEd to visualize some data.
Direct2D - Unboxing
Just as some bloggers record unboxing of products, I'd like to share my first impressions from playing around with Direct2D library. What is it? As you can guess from its name, it is a library from Microsoft for efficient, hardware-accelerated drawing of immediate-mode 2D graphics. It is successor of the old GDI, as well as GDI+. It is native library, object-oriented, based on COM. It works on Windows 7, as well as Windows Vista (after installing SP2 and Platform Update) and Windows 2008. The API seems modern, clean and very elegant, much like Direct3D 10/11. But of course it could be just a matter of taste. You can find its documentation, as well as headers and libraries in DirectX SDK. Docs say something about Windows 7 SDK, so probably it's there too.
What can it interop with? You can create a render target that will send the graphics to either a window (using HWND), a GDI canvas (HDC), DXGI surface (interop with Direct3D 10/11) or WIC Bitmap. Interop with GDI is also possible in the opposite way - you can obtain a HDC from D2D render target. Microsoft advices using WIC (Windows Imaging Component) for loading bitmap files. Another important library that cooperates with Direct2D is DirectWrite, which enables text rendering.
I didn't find any good tutorial about Direct2D, but I haven't look for it very hard either. I think the official documentation is good enough teaching all aspects of the library. After reviewing the documentation, I'm very impressed not only by the looks of the API, but also by the list of features is supports, like:
Things I don't like so far about the library:
My first test, drawn using following code:
using namespace D2D1; const D2D1_SIZE_F rtSize = m_RenderTarget->GetSize(); m_RenderTarget->BeginDraw(); m_RenderTarget->Clear(ColorF(ColorF::White)); m_Brush->SetColor(ColorF(ColorF::Silver)); const float gridStep = 24.f; for(float x = gridStep; x < rtSize.width; x += gridStep) m_RenderTarget->DrawLine(Point2F(x, 0.f), Point2F(x, rtSize.height), m_Brush, 1.f); for(float y = gridStep; y < rtSize.height; y += gridStep) m_RenderTarget->DrawLine(Point2F(0.f, y), Point2F(rtSize.width, y), m_Brush, 1.f); m_Brush->SetColor(ColorF(ColorF::Navy)); m_RenderTarget->DrawRectangle(RectF(200.f, 400.f, 400.f, 550.f), m_Brush); m_RenderTarget->DrawRectangle(RectF(240.f, 450.f, 280.f, 500.f), m_Brush); m_RenderTarget->DrawRectangle(RectF(320.f, 450.f, 360.f, 550.f), m_Brush)); m_RenderTarget->DrawLine(Point2F(200.f, 400.f), Point2F(300.f, 300.f), m_Brush, 1.f); m_RenderTarget->DrawLine(Point2F(300.f, 300.f), Point2F(400.f, 400.f), m_Brush, 1.f); m_RenderTarget->EndDraw();
Functional Programming is the New Trend
Some time ago I've written about data-oriented design as a popular trend among game developers, opposite to the belief that object-oriented programming is the silver bullet for all challenges and complexities of programming. Mike Acton, Engine Director at Insomniac Games and the creator of #AltDevBlogADay, is probably the biggest evangelist of this idea.
Now I can see that functional programming is a concept gaining popularity and it seems to follow similar mindset. Of course we won't start coding our games and programs in Lisp or Haskell tomorrow, but some of the ideas coming from functional languages can be applied to thinking about our everyday C++ code, instead of seeing design patterns, singletons and class inheritance everywhere. This can mean, for example, making data const (immutable) wherever possible and writing functions as pure - returning processed data that depend only on input data and not mutating or accessing any global state. This makes code simpler to read and understand, debug and unit-test, as well as to parallelize.
I've seen some voices in gamedev advocating that functional programming is the future even years ago. I can remember slides from some conference about it, I can't find it now though. But recently more and more programmers seem to be interested in learning some functional languages, writing about it, like explaining what the monad is etc. Some really recent, interesting blog posts about applying idea of functional programming in the real code:Life Without Objects by Chris Turner
By the way, it really impresses me how despite all his experience, fame and success, Carmack still tweets and blogs about programming, down to its dirtiest details, instead of writing about management, leadership, enterpreneurship, money, psychology, recruitment etc., like many other professionals do.