You don't need the Java SDK, just the [url=http://java.com/en/download/index.jsp]runtime environment[/url]. (The SDK includes the runtime environment plus other things needed for [i]writing[/i] Java programs, such as a compiler.)
Right, I have set-up a class to load in meshes and at the moment it only loads the same model but I want to be able to pass in a model name as a parameter and for it to load that parameter.
Here is the code I have for loading in the generic airplane model,
[code]
D3DXLoadMeshFromX(L"airplane 2.x", // file to load
D3DXMESH_SYSTEMMEM, // load mesh into system memory
d3ddev, // D3D device
NULL, // adjacency setting
&bufMaterial, // put materials here
NULL, // effects
&numMaterials, // number of materials in the model
&mesh); // the mesh
[/code]
Would passing over a string be the best method or is there another way I don't know of yet.
Just pass an STL string into your loading function (Pass it as a constant by-reference parameter.) and then call D3DXLoadMeshFromX() with the parameter FileName.c_str().
What's wrong with passing a string?
If you wanna avoid copying, just pass by reference.
Cosmos MS4 - Visual Studio 2008
System.exception: plug needed. System.Void System.Threading.Thread.Monitor.Enter(System object)
god, where do i need to change the code?!
[url]http://www.2shared.com/file/bov9pjVt/NOS.html[/url]
[QUOTE=yngndrw;25849264]Just pass an STL string into your loading function (Pass it as a constant by-reference parameter.) and then call D3DXLoadMeshFromX() with the parameter FileName.c_str().[/QUOTE]
Would you be able to explain it a bit more for someone who's an idiot? I'm still confused on how i would do it.
[QUOTE=Jimmylaw;25851257]Would you be able to explain it a bit more for someone who's an idiot? I'm still confused on how i would do it.[/QUOTE]
The D3DXLoadMeshFromX() function requires a C-style string as input. std::strings can be converted into C strings by using cppstring.c_str() . In your loading function, do something like this:
(No, your function should not be exactly like this, just get the modelname parameter declaration and it's subsequent conversion into a c string)
[cpp]
void LoadModel(const std::string &modelname) {
D3DXLoadMeshFromX(modelname.c_str(),
D3DXMESH_SYSTEMMEM,
d3ddev,
NULL,
&bufMaterial,
NULL,
&numMaterials,
&mesh);
}
[/cpp]
and call it like
[cpp]
std::string planemodel= "airplane 2.x";
...
LoadModel(planemodel);
[/cpp]
or
[cpp]
LoadModel("airplane 2.x");
[/cpp]
You are then passing only a reference to the model file string, instead of making a copy of it to send to the function. However, with so short a string, the performance difference between the two is negligible.
[QUOTE=BMCHa;25852353]The D3DXLoadMeshFromX() function requires a C-style string as input. std::strings can be converted into C strings by using cppstring.c_str() . In your loading function, do something like this:
(No, your function should not be exactly like this, just get the modelname parameter declaration and it's subsequent conversion into a c string)
[cpp]
void LoadModel(const std::string &modelname) {
D3DXLoadMeshFromX(modelname.c_str(),
D3DXMESH_SYSTEMMEM,
d3ddev,
NULL,
&bufMaterial,
NULL,
&numMaterials,
&mesh);
}
[/cpp]
and call it like
[cpp]
std::string planemodel= "airplane 2.x";
...
LoadModel(planemodel);
[/cpp]
or
[cpp]
LoadModel("airplane 2.x");
[/cpp]
You are then passing only a reference to the model file string, instead of making a copy of it to send to the function. However, with so short a string, the performance difference between the two is negligible.[/QUOTE]
Thanks very much, you explained it perfectly!
Could anyone pre compile the luainteface source for me for .net 4.0 ?
Source code :
[url]http://code.google.com/p/luainterface/[/url]
Trying to create a class for sprites and other things gui related.
It looks like it should work to me but when I run the program it comes up with memory errors.
[code]
#include "GuiObject.h"
guiObject::guiObject(void)
{
}
guiObject::guiObject(LPD3DXSPRITE D3Dspt,LPDIRECT3DDEVICE9 device)
{
d3ddev = device;
d3dspt = D3Dspt;
LPDIRECT3DTEXTURE9 sprite; // the pointer to the sprite
D3DXCreateSprite(d3ddev, &d3dspt); // create the Direct3D Sprite object
D3DXCreateTextureFromFile(d3ddev, "Panel1.png", &sprite);
}
void guiObject::Render()
{
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f); // center at the upper-left corner
D3DXVECTOR3 position(50.0f, 50.0f, 0.0f); // position at 50, 50 with no depth
d3dspt->Draw(sprite, NULL, ¢er, &position, D3DCOLOR_ARGB(127, 255, 255, 255));
}
LPDIRECT3DTEXTURE9 guiObject::getSprite(void){return sprite;}
LPD3DXSPRITE guiObject::getd3dspt(void){ return d3dspt;}
guiObject::~guiObject(void)
{
}
[/code]
The code creating the object in main.cpp looks to be fine also.
Here is a screenshot of the error.
[url]http://dl.dropbox.com/u/12592785/Screenshot.png[/url]
Where is d3dspt coming from in main? It seems to be null.
[QUOTE=shill le 2nd;25860172]Where is d3dspt coming from in main? It seems to be null.[/QUOTE]
Yea its defined at the top.
Here is my main.cpp
[code]
// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "Light.h"
#include "Mesh.h"
#include "GuiObject.h"
// define the screen resolution and keyboard macros
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
// global declarations
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPD3DXSPRITE d3dspt;
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPDIRECT3DSURFACE9 z_buffer = NULL; // the pointer to the z-buffer
LPDIRECT3DTEXTURE9 sprite; // the pointer to the sprite
// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory
void init_graphics(void); // 3D declarations
// Objects
Light myLight; // object of the light class
Mesh plane; // object of the mesh class
Mesh plane2; // object of the mesh class
guiObject hud;
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "WindowClass1";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
"WindowClass1",
"Our Direct3D Program",
WS_EX_TOPMOST | WS_POPUP,
0, 0,
SCREEN_WIDTH, SCREEN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
// set up and initialize Direct3D
initD3D(hWnd);
// enter the main loop:
MSG msg;
while(TRUE)
{
DWORD starting_point = GetTickCount();
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
render_frame();
// check the 'escape' key
if(KEY_DOWN(VK_ESCAPE))
PostMessage(hWnd, WM_DESTROY, 0, 0);
while ((GetTickCount() - starting_point) < 25);
}
// clean up DirectX and COM
cleanD3D();
return msg.wParam;
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.BackBufferWidth = 1280;
d3dpp.BackBufferHeight = 1024;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
// create the z-buffer
d3ddev->CreateDepthStencilSurface(SCREEN_WIDTH,
SCREEN_HEIGHT,
D3DFMT_D16,
D3DMULTISAMPLE_NONE,
0,
TRUE,
&z_buffer,
NULL);
init_graphics(); // call the function to initialize the triangle
myLight = Light(d3ddev, D3DXVECTOR3(-1.0f,-0.3f,-100.0f), D3DXVECTOR3(-1.0f, -0.3f, -1.0f)); // Creates a light with the parameters, d3ddev, position and direction
hud = guiObject(d3dspt,d3ddev);
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE); // turn on the 3D lighting
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50)); // ambient light
return;
}
// this is the function used to render a single frame
void render_frame(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
d3dspt->Begin(NULL); // // begin sprite drawing with transparency
hud.Render();
d3dspt->End();
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 8.0f, 16.0f), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
SCREEN_WIDTH / SCREEN_HEIGHT, // the aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
plane.Update();
d3ddev->SetTransform(D3DTS_WORLD, &(plane.GetMatrix())); // set the world transform
plane.GetMesh()->DrawSubset(0);
plane.Render();
plane2.Update();
d3ddev->SetTransform(D3DTS_WORLD, &(plane2.GetMatrix())); // set the world transform
plane2.GetMesh()->DrawSubset(0);
plane2.Render();
d3ddev->EndScene(); // ends the 3D scene
d3ddev->Present(NULL, NULL, NULL, NULL);
return;
}
// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
plane.GetMesh()->Release(); // close and release the spaceship mesh
plane2.GetMesh()->Release();
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D
return;
}
void init_graphics(void)
{
plane = Mesh(d3ddev,"airplane 2.x", 2);
plane2 = Mesh(d3ddev, "tiger.x", 10);
return;
}
[/code]
OK, well I don't actually know DirectX, just C++ in general, but you are definitely dereferencing a null pointer.
EDIT:
[QUOTE=shill le 2nd;25860259]remove the ampersand in this call: D3DXCreateSprite(d3ddev, &d3dspt);[/QUOTE]
I don't know DirectX either, but judging from the [url=http://msdn.microsoft.com/en-us/library/bb172797(VS.85).aspx]docs[/url], passing the address of the pointer is correct in this case. You're essentially passing a pointer by reference, so that the function can modify your pointer to point to the new sprite that it created.
Hmm, that makes sense. Ignore what I said.
Maybe check the return value on D3DXCreateSprite?
Right, I removed the ampersand and then got this error thrown in my face.
>c:\users\chris\documents\visual studio 2010\projects\renderengine\guiobject.cpp(14): error C2664: 'D3DXCreateSprite' : cannot convert parameter 2 from 'LPD3DXSPRITE' to 'LPD3DXSPRITE * Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-
Yeah, I was wrong, don't do that. Check the return value of D3DXCreateSprite.
I see what's happening. The guiObject::guiObject() constructor creates a sprite and stores its address in the [i]member variable[/i] d3dspt, which is only visible within methods of the guiObject class. The global d3dspt variable in main.cpp never gets modified, and remains null, and that's the one you're trying to call Begin() on.
Why are you doing the sprite's Begin() and End() calls in render_frame() anyway? Put them in guiObject::Render() so they see the correct member variable in the guiObject class, and get rid of that global sprite pointer.
[QUOTE=Wyzard;25860848]I see what's happening. The guiObject::guiObject() constructor creates a sprite and stores its address in the [i]member variable[/i] d3dspt, which is only visible within methods of the d3dspt class. The global d3dspt variable in main.cpp never gets modified, and remains null, and that's the one you're trying to call Begin() on.
Why are you doing the sprite's Begin() and End() calls in render_frame() anyway? Put them in guiObject::Render() so they see the correct member variable in the guiObject class, and get rid of that global sprite pointer.[/QUOTE]
Right, I have moved the begin and end code into guiObject::Render() along with the code to draw the sprite but unsure which global sprite pointer you are meaning?
Thanks for helping me anyway.
[QUOTE=Jimmylaw;25860968]...but unsure which global sprite pointer you are meaning?[/QUOTE]
The one on line 21 of your above-posted main.cpp
edit
So if i have understood this correctly, you want to to get rid of the parameters on the guiObject::guiObject method and use the d3ddev and d3dspt that are define in there. So its no longer using ones from main.cpp.
[QUOTE=Jimmylaw;25861053]So if i have understood this correctly, you want to to get rid of the parameters on the guiObject::guiObject method and use the d3ddev and d3dspt that are define in there. So its no longer using ones from main.cpp.[/QUOTE]
Actually you have two different issues here.
You should get rid of d3dspt outside the object and as a parameter, since it's never being initialized outside the object. You're passing it as a NULL pointer to your object, then your object is storing this pointer as a member and initializing it. This is exactly the same as simply declaring d3dspt inside the object constructor.
On the other hand, d3ddev is being created and initialized outside the object. If you want to use it inside and outside the object, you must pass it by reference to your object. Make your d3ddev class member a pointer to LPDIRECT3DDEVICE9, and pass the address of d3ddev to the constructor.
I didn't say anything about d3ddev. It makes sense for that to be supplied my main.cpp because it doesn't make sense for a guiObject to create its own D3D device.
But the D3Dspt parameter in guiObject::guiObject() doesn't actually do anything there. It's null when it's passed in, and it's used to assign a value to d3dspt (the member variable), but then d3dspt gets modified again by the call to D3DXCreateSprite(), overwriting the (null) value that was copied from D3Dspt. There's no point having the parameter because it doesn't influence anything.
Now that you've removed the d3dspt->Begin(NULL) and d3dspt->End() calls from render_frame(), the only thing the d3dspt variable is being used for in main.cpp is to pass to the guiObject constructor. Since it serves no purpose there and you can remove that parameter, you can remove the variable from main.cpp too.
[QUOTE=shill le 2nd;25861263]Actually you have two different issues here.
You should get rid of d3dspt outside the object and as a parameter, since it's never being initialized outside the object. You're passing it as a NULL pointer to your object, then your object is storing this pointer as a member and initializing it. This is exactly the same as simply declaring d3dspt inside the object constructor.
On the other hand, d3ddev is being created and initialized outside the object. If you want to use it inside and outside the object, you must pass it by reference to your object. Make your d3ddev class member a pointer to LPDIRECT3DDEVICE9, and pass the address of d3ddev to the constructor.[/QUOTE]
I'm not sure I understand you completely but I have have a d3ddev and a d3dspt in the object and outside the object on main.cp.
[QUOTE=Jimmylaw;25861351]I have have a d3ddev and a d3dspt in the object and outside the object on main.cp.[/QUOTE]
But you don't need both things in both places.
[list]
[*]In main.cpp, you don't need a sprite because the guiObject class takes care of that.
[*]In guiObject, the only place you need the D3D device is in the constructor, where it's passed in as a parameter. There's no need to store it in a member variable because it's not used for anything else.
[/list]
[QUOTE=Wyzard;25861462]But you don't need both things in both places.
[list]
[*]In main.cpp, you don't need a sprite because the guiObject class takes care of that.
[*]In guiObject, the only place you need the D3D device is in the constructor, where it's passed in as a parameter. There's no need to store it in a member variable because it's not used for anything else.
[/list][/QUOTE]
Right, I have removed the LPD3DXSPRITE d3dspt from the main.cpp and left LPDIRECT3DDEVICE9 d3ddev as a parameter.
The only reason I assign them member variables is because I couldn't access d3dspt from the parameters in the render method for the begins and end section, is guiobject even the correct place for the begin and end stuff?
Well, d3dspt needs to be a member variable in guiObject, specifically [i]so[/i] that you can set it in the constructor and then use it from Render(). But the device isn't used anywhere outside the constructor; there's no point saving it in a member variable to make it accessible in other methods, when it isn't [i]needed[/i] by other methods.
The Begin() and End() calls belong in whatever method is responsible for the overall task of "drawing the sprite". In your code, that's guiObject::Render().
[QUOTE=Wyzard;25861890]Well, d3dspt needs to be a member variable in guiObject, specifically [i]so[/i] that you can set it in the constructor and then use it from Render(). But the device isn't used anywhere outside the constructor; there's no point saving it in a member variable to make it accessible in other methods, when it isn't [i]needed[/i] by other methods.
The Begin() and End() calls belong in whatever method is responsible for the overall task of "drawing the sprite". In your code, that's guiObject::Render().[/QUOTE]
Right, I've done what you said to my code but I'm still getting what appear to be memory errors. There not the same ones but there still errors.
[url]http://dl.dropbox.com/u/12592785/error2.png[/url]
[QUOTE=Jimmylaw;25862134]Right, I've done what you said to my code but I'm still getting what appear to be memory errors.[/QUOTE]
Take the "LPD3DXSPRITE d3dspt;" line out of the guiObject constructor. That creates a local variable that hides the member variable with the same name, so your sprite is being stored into the local variable (which is thrown away at the end of the constructor) and the member variable used by Render() never gets set.
[editline]4th November 2010[/editline]
BTW, I've been reading about how sprites are used in D3D and it sounds like when multiple sprites are to be drawn, it's preferable (for performance reasons) to batch them together between a single pair of Begin() and End() calls, rather than doing a separate Begin() and End() for each.
If you're planning on using multiple guiObjects, it might be a good idea to move the sprite stuff back into main.cpp after all -- the d3dspt pointer, the Begin() and End() calls, and (in contrast to your original code) the D3DXCreateSprite() call too. That way you can call d3dspt->Begin() once, then call Render on all your guiObjects, then call d3dspt->End() once to finish the batch. You wouldn't even need to store the sprite as a member variable in the guiObject; just pass it into Render() as a parameter.
This is a different design than what we've been discussing, though. If you're confused, start simple and leave your Begin() and End() calls in guiObject::Render().
Sorry, you need to Log In to post a reply to this thread.