NOGDUS $1670.00 has been donated to NOGDUS!
May 22, 2017, 02:29:18 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Programming D3D9 Win32 on Linux!  (Read 3929 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« on: July 03, 2009, 04:15:46 PM »

Programming D3D9 Win32 on Linux!

You read that right!

I have discovered how to write, compile, and run Direct3D 9 Win32 executables without leaving my nice and comfortable Ubuntu Linux operating system! Grin


First of all, you will need to install the mingw32 package.

I'm using Ubuntu, so the command for installing is like so
Code:
$ sudo apt-get install mingw32

Next we will need the DirectX SDK 9.0c (November 2007).
Search microsoft.com for this. Remember you need the full SDK not just the DirectX9 redistributable.
I chose the November 2007 build of DirectX 9.0c because it works well for my purposes.

Once you have that downloaded, you will need to get the SDK files we need from it.
This is something that takes a little bit of work.
You can do this using Wine, but I used Crossover Office because its easy to create a temporary "bottle" and install the SDK, get the files and delete it.

With Wine, you will need to create a separate location for the SDK.

Anyway, I cannot offer the files that you need because microsoft will come and byte my arse.
If you cannot get the files, tell me and I'll give them to you directly (shhhh I didn't say that)

Ok, so now that the SDK is extracted in Wine or a Crossover Office bottle, you just need to copy the files we need.

There will be a directory called "Lib" and a directory called "Includes" in the location you installed the SDK.

We need to do a little work now.

Create a new directory in your home directory called "dx9sdk" in this copy the "Lib" and "Includes" folders and their contents.
Code:
$ mkdir ~/dx9sdk

Now, rename the "Lib" directory to "lib" (case sensitive) and "Includes" to "include".

Go into the "lib" directory, and you will see "x64" and "x86" folders.
We need the .lib files from the "x86" folder.

Copy them to the "lib" folder root.

Now we need to rename all the .lib files in the following manner.

d3d9.lib -> libd3d9.a

so all the .lib becomes .a
and all the files get prefixed with the letters "lib".

Got it?

Okay, once that is done, we can go create a test project.

Code:
$ mkdir -p ~/Projects/directx-on-linux/d3d9-window/
$ cd ~/Projects/directx-on-linux/d3d9-window/
$ touch Makefile winmain.cpp
$ gedit ./* &

You will now be staring at two blank files in gedit.

In the Makefile put the following:
Code:
all: app

app:
i586-mingw32msvc-gcc -I/home/username/dx9sdk/include/ -L/home/username/dx9sdk/lib/ -mwindows winmain.cpp -ldxguid -ld3d9 -o d3d9-window.exe

clean:
rm -f ./*.o
rm -f ./d3d9-window.exe

And in the winmain.cpp place this code

Code:

#include <windows.h>

#include <d3d9.h>

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddevice;

void init_direct3d(HWND hwnd);
void render_frame();
void kill_direct3d();

LRESULT CALLBACK main_window_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
default:break;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int nshowcmd)
{

WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = main_window_proc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = "RichardMarksDoesWin32OnLinux";

if (!RegisterClass(&wc))
{
MessageBox(0,"Unable to register the window class!", "Error", MB_OK | MB_ICONEXCLAMATION);
exit(1);
}

HWND window = CreateWindow("RichardMarksDoesWin32OnLinux", "Richard Marks Does Win32 On Linux!",
WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, 0, 0, hinstance, 0);
if (!window)
{
MessageBox(0,"Unable to create the window!", "Error", MB_OK | MB_ICONEXCLAMATION);
exit(1);
}

ShowWindow(window, nshowcmd);
UpdateWindow(window);

init_direct3d(window);

MSG message;
ZeroMemory(&message, sizeof(MSG));

while(true)
{
while(PeekMessage(&message, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}

if (message.message == WM_QUIT)
{
break;
}

render_frame();
}

kill_direct3d();

return (int)message.wParam;
}

void init_direct3d(HWND hwnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(d3dpp));

d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;

d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddevice);
}

void render_frame()
{
d3ddevice->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
d3ddevice->BeginScene();
d3ddevice->EndScene();
d3ddevice->Present(0, 0, 0, 0);
}

void kill_direct3d()
{
d3ddevice->Release();
d3d->Release();
}


Run make in the directory, and you should get a valid Win32 PE executable file.
Run it with Wine and it should run fine.
And that is that! Cool
Logged

Regretfulone
Guest
« Reply #1 on: July 11, 2009, 09:17:47 PM »

I have a question for you. I have directX installed on my windows and I have a pre-made game engine that I have been creating from a book I've been reading, do you think it would be possible for me to like combine the DirectX stuff into my game engine so that my game engine still handles all the dirty work of creating everything. I think I might find that a bit easier if I can actually do that, rather than jsut copy pasting the code over and over like the book says to do -.-
Logged
Richard Marks
Administrator
Offline Offline

Respect: 3425
« Reply #2 on: July 11, 2009, 10:39:36 PM »

I have a question for you. I have directX installed on my windows and I have a pre-made game engine that I have been creating from a book I've been reading, do you think it would be possible for me to like combine the DirectX stuff into my game engine so that my game engine still handles all the dirty work of creating everything. I think I might find that a bit easier if I can actually do that, rather than jsut copy pasting the code over and over like the book says to do -.-

I'm not fully sure what you are asking but I will answer as best as I can.

You have a book that contains code for a game library that implements a Win32 or Win32/DirectX game engine, and you want to use that instead of writing it over and over for your games?

I'm a little confused as to what you are talking about.

It is best to write some boiler plate code for creating the Win32 window, and initializing Direct 3D, and then you can use it as a starting point for your projects.

You really don't _need_ to worry about the difference between a game engine and a game right now while you are learning the APIs.

Its likely best for you to pick a simple game to work on and start figuring it out from there.
For instance, if you were trying to do a pac man clone game, you should first start with plain GDI and Win32 API, and create a window, then add the real-time loop code and the handler for the ESC key so you can exit the game demo quickly, and then learn how to load and display a bitmap image. (Which from what I gather from your messages, that is the point you are at now in the book..loading and displaying bitmaps with GDI)

Its not too difficult to do that, and I'll gladly help you with that.
Logged

Regretfulone
Guest
« Reply #3 on: July 12, 2009, 08:41:06 AM »

Yeah I have a game engine that I made from reading the book that isn't explaining how to load bitmaps too well. I like the way the engine initializes everything and takes care of creating the window and everything and then leaves you with functions to fill in like GameStart(), and GameInitialize(), GameEnd(), GameCycle(), and so forth. I just want to know if I could replace some of the code in the engine to make it DirectX compatible. I want to try and found out how to do it myself first haha cause I think I have an idea how to get the code into the engine so that everything runs smooth and everything still works. When I get home and work on it a bit ill post it up for you to see,
Logged
Richard Marks
Administrator
Offline Offline

Respect: 3425
« Reply #4 on: July 12, 2009, 01:10:02 PM »

Yeah I have a game engine that I made from reading the book that isn't explaining how to load bitmaps too well. I like the way the engine initializes everything and takes care of creating the window and everything and then leaves you with functions to fill in like GameStart(), and GameInitialize(), GameEnd(), GameCycle(), and so forth. I just want to know if I could replace some of the code in the engine to make it DirectX compatible. I want to try and found out how to do it myself first haha cause I think I have an idea how to get the code into the engine so that everything runs smooth and everything still works. When I get home and work on it a bit ill post it up for you to see,

Haha. Okay good luck.

Yes, you can add DirectX support to the Win32 game engine, its not going to be a simple matter, but its doable.
Logged

Tags:
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!