DirectX 9 target

Monkey Targets Forums/User Targets/DirectX 9 target

SLotman(Posted 2015) [#1]
Hope you guys have patience with me... ^_^

I'm here doing little by little a Direct X 9 target for Monkey (using VS2012, which is the one I have... I know VS2010 would be much better, so Windows XP SP1/SP2 could be targeted, but nothing I can do about it :/)

So, one of the first problems I encountered, seems to be about OpenGL Matrix vs Direct-X one. One uses the left hand system, while the other uses the right hand - and this causes rotation/scaling to be all different inside the used matrix.

I then come up with a quick "hack": swap the coordinates, so everything would be the same. It works really well with drawrect:



Rotation, scaling, everything is working with DrawRect. Then, I went ahead to loadimages, and to implement DrawSurface/DrawImage.

Using basically the same thing (just adding texture coordinates), I can't get rotation to work - it's rotating the opposite way.

Here's a image to show what's happening:


On the left, the GLFW target as it should be. On the right, my DX9 one. Look how the transparent PNG is rotated the other way around in the right.

This is the code to draw an image:


I been at this for hours, and can't find what's wrong. If the vertices positions are correct with DrawRect, why the same thing won't work with DrawSurface? O_o


Ferdi(Posted 2015) [#2]
In DrawRect you have:

	float x_ix = x * _jx;
	float x_jx = x * _ix;
	float y_iy = y * _jy + _tx;
	float y_jy = y * _iy + _ty;
	
	float w_ix = h * _jx;
	float w_jx = h * _ix;
	float h_iy = w * _jy + _tx;
	float h_jy = w * _iy + _ty;


But in Draw Surface you have:

	float x_ix = x * _ix;
	float x_jx = x * _jx;
	float y_iy = y * _iy + _tx;
	float y_jy = y * _jy + _ty;
	
	float w_ix = w * _ix;
	float w_jx = w * _jx;
	float h_iy = h * _iy + _tx;
	float h_jy = h * _jy + _ty;


Notice how in DrawRect x_ix = x * jx and in DrawSurface x_ix = x *ix ... etc etc

Could that be the problem?


SLotman(Posted 2015) [#3]
Unfortunately, no. I ended up copying something wrong when I posted - mostly because I've been trying to change things here and there for so long I'm seeing everything double now ^_^


SLotman(Posted 2015) [#4]
Heh! Thanks Ferdi!

After seeing that mistake, and how lost I was, I re-wrote the whole function from scratch - and voilá! it is working now ;)



Hmmm... ALMOST working. If I scale the image and rotate it at the same time, it gets distorted :/


Both images above are drawed with:
DrawImage image, 200,200, 45, 0.5,1

On the left, GLFW, correct. On the right, my own, still wrong :(
(on 0, 90 degrees and multiples it draws correctly, even if scaled)


Ferdi(Posted 2015) [#5]
The bit of code I question is this

	float w_ix = h * _jx;
	float w_jx = h * _ix;
	float h_iy = w * _jy + _tx;
	float h_jy = w * _iy + _ty;

...
	_Vertices[21] = w_ix + h_iy;
	_Vertices[22] = w_jx + h_jy;


Shouldn't w_ix = (x + w) * _jx etc etc?

This is the code for rotating and scaling in my popcap sexy framework. Not sure if you can just cut and paste and give it a whirl.

	else
	{
		float w=(float)surface->Width();
		float h=(float)surface->Height();
		float x0=x,x1=x+w,x2=x+w,x3=x;
		float y0=y,y1=y,y2=y+h,y3=y+h;
		float u0=0,u1=1.0; //*surf->uscale;
		float v0=0,v1=1.0; //*surf->vscale;

		if( tformed )
		{
			float tx0=x0,tx1=x1,tx2=x2,tx3=x3;
			x0=tx0 * ix + y0 * jx + tx;y0=tx0 * iy + y0 * jy + ty;
			x1=tx1 * ix + y1 * jx + tx;y1=tx1 * iy + y1 * jy + ty;
			x2=tx2 * ix + y2 * jx + tx;y2=tx2 * iy + y2 * jy + ty;
			x3=tx3 * ix + y3 * jx + tx;y3=tx3 * iy + y3 * jy + ty;
		}
		
		TriVertex vp1, vp2, vp3, vp4;
		
		vp1.x = x0; vp1.y = y0; vp1.u = u0; vp1.v = v0; vp1.color = colorARGB;
		vp2.x = x1; vp2.y = y1; vp2.u = u1; vp2.v = v0; vp2.color = colorARGB;
		vp3.x = x2; vp3.y = y2; vp3.u = u1; vp3.v = v1; vp3.color = colorARGB;
		vp4.x = x3; vp4.y = y3; vp4.u = u0; vp4.v = v1; vp4.color = colorARGB;
		
		if (gPopCapGraphics != NULL)
		{
			gPopCapGraphics->DrawTriangleTex(surface->data, vp1, vp2, vp3);
			gPopCapGraphics->DrawTriangleTex(surface->data, vp1, vp3, vp4);
		}
	}


Full source code: https://github.com/Ferddi/psf-monkey/blob/master/modules/mojo/native/mojo.sexy.cpp
Line: 615 is where you will be interested.

I gotta go to sleep it is nearly 4AM here =)


SLotman(Posted 2015) [#6]
Yay! I don't know what I did exactly, but now it is working :)


Next step... figure out how Monkey handles the image frames... with this working I may be able to make drawtext / drawimage (..., frame) work ^_^


SLotman(Posted 2015) [#7]
Little by little I'm making some progress ^_^


It's not as robust as glfw (doesn't even pause the app if the window loses focus or gets minimized) - but I can already run some demos, like the bouncy aliens above :)
Still have to factor in keyboard/mouse/joystick and sounds/music, but so far its going well :)


DruggedBunny(Posted 2015) [#8]
This is really cool, by the way!


Duke87(Posted 2015) [#9]
Nice work. I've never coded with DirectX. What would be the benefit by using DirectX over glfw? faster, better effects?


SLotman(Posted 2015) [#10]
The main benefit is compatibility on Windows. OpenGL drivers (specially from Intel) are flaky at best.
But the main reason I'm doing this is that some publishers (Like GameHouse) doesn't like OpenGL games on Windows, because it doesn't integrate well with their tools.


DruggedBunny(Posted 2015) [#11]
Good stuff, hope you can see it through!


SLotman(Posted 2015) [#12]
Small update: just got keydown/keyhit and getchar working :)

Now have still draw point, sphere and lines to work, and see why it locks up when I put my laptop on sleep mode, or if I do ALT+TAB on fullscreen - probably it loses the "dx9 device" - and I should handle that in some way :P

Still a long way to go, but most things are working quite nice so far :)


SLotman(Posted 2015) [#13]
Small update 2: got DrawCircle to work, and now you can ALT+TAB from fullscreen, and put the computer on hibernation - the game won't crash =)

Still have lots to do, including OnSuspend/OnResume, mouse input, audio... but little byt little Its progressing!




SLotman(Posted 2015) [#14]
Hmmm... just hit a snag. I tried to run the blobmonster sample (working on my machine, Windows 8) into a university computer, running windows 8.1 - and all I've got was a white window.

Then I set it to run in "compatible mode with windows 8" and it ran normally.

This is happening on every Monkey sample I tried (with my target). And I don't have Visual Studio here on the machine, so I can't try even to debug.

I have NO IDEA what's going on with Windows 8.1. Have anyone experienced anything similar?
(I'm also getting some corruption on the window title, showing some strange characters... which is weird, to say the least)


Raph(Posted 2015) [#15]
Windows 8.1 doesn't come with DX9. It comes with DX11, which is not the same DLL. You have to have DX9 explicitly installed.


Ferdi(Posted 2015) [#16]
I have Windows 8.1 + VS 2013 running in VirtualBox. It is for my Windows Phone build. If you like, you can email me the build directory of one of the monkey sample. (NOT your monkey code, the C++ translated monkey codes) I can see whether the white screen problem is happening here too, and may be debug it here? I can do this tonight. My email address is ferddi [at] gmail.com.


SLotman(Posted 2015) [#17]
Windows 8.1 doesn't come with DX9. It comes with DX11, which is not the same DLL. You have to have DX9 explicitly installed.

I don't think that's the issue, if it was, putting it on "windows 8 compatible mode" wouldn't make it work.


SLotman(Posted 2015) [#18]
I think I solved the problem, after I got the same thing on Windows 8... it was something to do on how I was updating the game logic and render. Tweaked it a little, now it seems to be working.

Monday I'll be at university again (and now I've put a small log file into the target) so I can see if it will work or not.

And now I have Mouse and drawpoint / drawline working. I can already run most of the samples in /bananas and even my own games ^_^

Need to implement setscissors, read/write pixels, and then figure out how to do music/sound :P

Don't know if the image will show but...



SLotman(Posted 2015) [#19]
Yup, confirmed - it had nothing to do with DX9. Today I could run everything on Windows 8.1, without any problems :)

Now, I was running "bananas/mak/mojotest" yesterday, and I hit a problem: with loading and releasing textures, the memory consuption was through the roof. Going up and up, even with me calling texture->Release() on the DX9 texture.

After breaking my head trying to find a memory leak that DOESN'T exist, I finally tried this:

#CFG_CPP_GC_TRIGGER = 1024

And the "leak" disappeared. Apparently there is something within Monkey's Garbage collector that doesn't like DX9 textures.
Since that was no good solution (you would have to write that on every program) - I hacked my way into the garbage collector, which fix things.

This is what I'm using now as gxtkSurface.Discard:


Would be nice to actually know what is going on, and have a real fix for this though :(
(Using Monkey 77b BTW - was there any changes to GC in newer releases?)


DruggedBunny(Posted 2015) [#20]

I can already run most of the samples in /bananas



Don't know the answer, but still... impressive!


SLotman(Posted 2015) [#21]
Nevermind! I was having trouble setting clipping planes... then I discovered about DX9 SetScissorRect command, and everything is fine once more :)


SLotman(Posted 2015) [#22]
Is there any good test for DrawPoly and DrawPoly2 in bananas?

I would like to test those functions on my target :)


Soap(Posted 2015) [#23]
If you need more people to test this I can with a few big projects.


SLotman(Posted 2015) [#24]
Sorry to curse but...

FFFFF####################CCCCKKKK YEAH!

Just got OpenAL integrated into my DX9 Target. A hell of a mess to make everything be recognized by VS2012 - but I'm now running my "Escape From Alcatraz", music (ogg), sounds (wav) and everything!




Phil7(Posted 2015) [#25]
Looks great. Your game also is a true inspiration for me to get my projects forward... :-)


SLotman(Posted 2015) [#26]
Thanks Phil7! That means a lot to me :)

And just by accident I discovered at least another advantage into this target, compared to glfw: it can load 16 and 256 colors PNGs!

Now I just have to fix the debug mode compilation (don't know why, but it is spitting errors - I probably forgot to change something I did in the release mode), and implement the createImage, read/writepixel... and see if everything still works in the latest monkey releases :P

Edit: another advantage - I'm now using "MapVirtualKey" to translate the VK_ Codes from windows, so it should now be compatible with just about every keyboard layout supported by Windows ^_^


SLotman(Posted 2016) [#27]
Just a small update... *just* got it working with latest Monkey, 87a.

Still loads of minor stuff to do on it, but its progress nonetheless :)