Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

C# to C++

Started by rms Nov 17, 2004 at 4:45 AM 4 replies 650+ views
Original Post
rms
rms
Hello, I'm converting my C# Managed DirectX 2D game to C++ DirectX (DirectX 9.0c) and I've run into some problems with sprites. I just want to load a "texture" and render it to the scene. I'm not going to use any 3D anywhere in this game, so I guess/hope I can use screen coordinates and don't have to set up a camera? This works in my C# project.

//Initialization
Sprite sprite;
Texture spriteTexture;
Rectangle textureSize;
spriteTexture = TextureLoader.FromFile(device, "sprite.tga");
Surface s = spriteTexture.GetSurfaceLevel(0);
SurfaceDescription sd = s.Description;
textureSize = new Rectangle(0, 0, sd.Width, sd.Height);
sprite = new Sprite(device);
//Rendering
sprite.Begin(SpriteFlags.AlphaBlend);
sprite.Draw(spriteTexture, textureSize, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(100.0f, 150.0f, 1.0f), Color.White);
sprite.End();




My C++ code compiles but doesn't render anything, I only see the "clear color".

//Initialization
ID3DXSprite* sprite = NULL;
IDirect3DTexture9* spriteTexture = NULL;
D3DXCreateTextureFromFile(shared.g_device, "sprite.tga", &spriteTexture);
RECT textureSize = {0, 0, 256, 256};
D3DXCreateSprite(shared.g_device, &sprite);
//Rendering
sprite->Begin(D3DXSPRITE_ALPHABLEND);
sprite->Draw(spriteTexture, &textureSize, &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(100.0f, 150.0f, 1.0f), 0xFFFFFFFF);
sprite->End();




What do I have to do to make this work? I've tried alot of the suggestions found in other posts about this problem but I can't seem to find the right solution for me. Or I'm just doing that wrong too =) I hope there's nothing special about the image-format? I saved it as a 24 bits/pixel targa. Thanks in advance, Mattias [Edited by - rms on November 19, 2004 1:19:16 PM]
rms
rms
While I'm at it. If I want to print out a variable (float fps) with ID3DXFont->DrawText(), how?
Neither
DrawText(NULL, (fps+" fps"), -1, ▭, DT_LEFT|DT_NOCLIP, 0xFF000000);
nor
DrawText(NULL, (LPCSTR)fps, -1, ▭, DT_LEFT|DT_NOCLIP, 0xFF000000);
works with C++.
I use this in C#.
DrawText(null, fps+" fps", new Rectangle(screenWidth-50, 5, 0, 0), DrawTextFormat.NoClip, Color.Black);
I haven't found any "good" resources for string-"formatting" in C++.

[Edited by - rms on November 19, 2004 1:24:44 PM]
sirob
sirob
About the fps part, you need to do this:

TCHAR the-string[50];
sprintf(the-string, TEXT("FPS: %f"), fps);

then use the "the-string" variable in the actual DrawText call.
Sirob Yes.» - status: Work-O-Rama.
Syranide
Syranide
String formatting in C++ is done using "sprintf" or the new "StringCbPrintf" for Windows.

Regarding the rest I don't know, I expect you have "BeginScene" and "EndScene" somewhere else?

Apart from that it seems great (if you don't BeginScene and EndScene I believe nothing is drawn) and, uhm, you need to "Present" too.


rms
rms
Wow, quick reply. Thanks for the fps-problem fix.

And yes, of course I have Clear, BeginScene, EndScene and Present in my rendering function. Everything below the "Rendering" comment is in there and the other stuff goes in Init-function.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.