problem with uint to IDirect3DSurface9*

Started by
5 comments, last by Evil Steve 14 years, 4 months ago
I am trying to render tiles in my 2D "game". But I cannot figure out this problem I am having at the moment and how to get by it so I can continue on with the programming. I have no clue how to fix this. Anyone that can help? :) preferably without flaming and cursing. I ain't sure this will work the way I want to but I won't find out if I can't fix this. Function
// draw the sprites. 
void DxManager::Render()
{
	int spriteWidth = 32, spriteHeight = 32, destx = 0, desty = 0;

	if(!pd3dDevice)
		return;
	pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);
	pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &BackBuffer);

	textures.push_back(this->getSurfaceFromBitmap("./gfx/sprite1.bmp"));
	textures.push_back(this->getSurfaceFromBitmap("./gfx/sprite1.bmp"));

	for(int x = 10; x >= 0; x--)
	{
		for(int y = 10; y >= 0; y--)
		{
			IDirect3DSurface9* pSurface;
			pSurface = groundMap[x][y];
			hResult = pd3dDevice->StretchRect(groundMap[x][y], NULL, BackBuffer, &destRect, D3DTEXF_NONE);
		}
	}
	if(hResult != D3D_OK)
	{
		MessageBox(NULL, "D3D is not OK!", "Error", MB_OK);
		return;
	}
	pd3dDevice->Present(NULL, NULL, NULL, NULL);
}
Class
#ifndef _DXMANAGER_H_
#define _DXMANAGER_H_

#include "gui.h"
#include "tiles.h"

#include <d3d9.h>
#include <d3dx9tex.h>
#include <string>

class DxManager : public Gui
{
public:
	DxManager();
	virtual ~DxManager();
	bool InitDirect3D();
	bool LoadBackground();
	bool InitSprites();
	
	void Render();
	void StartRender();
	void StopRender();
	void CleanUp();
	void BlitToSurface(IDirect3DSurface9* srcSurface, const RECT *srcRect, const RECT *destRect);

	std::vector<IDirect3DSurface9*> textures;

	LPDIRECT3D9			pd3d;
	LPDIRECT3DDEVICE9	pd3dDevice;	

protected:
	unsigned int construct();
	std::map<unsigned int, unsigned int> twidth;
	std::map<unsigned int, unsigned int> theight;
	Tile *tiles[10][10];

private:
	bool fullscreen;
	unsigned int tTile;
	D3DXIMAGE_INFO imageInfo;
	IDirect3DSurface9* surface;
	IDirect3DSurface9* GetBackBuffer();
	IDirect3DSurface9* getSurfaceFromBitmap(std::string filename);
	IDirect3DSurface9* getSurfaceFromBitmap(std::string filename, int width, int height, int alpha);
	IDirect3DSurface9* Background;
	IDirect3DSurface9* BackBuffer;
	IDirect3DSurface9* sprite;
	
};

#endif
groundMap[x][y]
unsigned int groundMap[10][10] = {
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}
Error(s)
1>------ Build started: Project: sample, Configuration: Debug Win32 ------
1>Compiling...
1>dxmanager.cpp
1>c:\development\dxmanager.cpp(106) : error C2440: '=' : cannot convert from 'int' to 'IDirect3DSurface9 *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\development\dxmanager.cpp(107) : error C2664: 'IDirect3DDevice9::StretchRect' : cannot convert parameter 1 from 'int' to 'IDirect3DSurface9 *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>Generating Code...
1>Compiling...
1>main.cpp
1>Generating Code...
1>Build log was saved at "file://c:\Development\BuildLog.htm"
1>sample - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Advertisement
Did you actually read the error message? And look at the code?
IDirect3DSurface9* pSurface;pSurface = groundMap[x][y];hResult = pd3dDevice->StretchRect(groundMap[x][y], NULL, BackBuffer, &destRect, D3DTEXF_NONE);

groundMap is an array of int's, and pSurface is a IDirect3DSurface9*. You can't assign one to the other, which is what the error tells you.
Quote:Original post by Promethium
Did you actually read the error message? And look at the code?
*** Source Snippet Removed ***
groundMap is an array of int's, and pSurface is a IDirect3DSurface9*. You can't assign one to the other, which is what the error tells you.


Yes I read errors and I also said in the head of the thread quoting myself
"But I cannot figure out this problem I am having at the moment" and "I have no clue how to fix this.". I have tried for a few hours to understand this and how to fix it, I have tried reinterpret_cast but it didn't go so well.

Not trying to be rude but if I didn't read the errors I would have no-clue on what part that was giving me the errors. I understand what the error say but not how to fix it. So instead of stating the obvious point me in the right direction instead of wasting your and my time.
Quote:Original post by FrozenSnake
But I cannot figure out this problem I am having at the moment


The problem, as your error message states, is that you cannot use an integer as if it were a surface. An integer is not a surface. It is a number.

Quote:I have no clue how to fix this.


You need to change your code so that you are not trying to use an integer as a surface. You cannot convert an integer into a surface using any sort of cast. An integer is not a surface. It is a number.

We can't tell you why you wrote the code that you did. You should know that. Unless of course you've copied this code in from some other place without understanding it, in which case you should probably go back to the source and have them explain it to you.
Mike Popoloski | Journal | SlimDX
See the documentation for IDirect3DDevice9::StretchRect. This function works by copying an image from one IDirect3DSurface9 to another. This is not just a type, but a D3D object, containing an image buffer. Your array is not an image buffer, it's an array of numbers.
From the rest of you code I assume you want that number to specify one of your textures, and you want to draw that texture. To do that you would use something like the following:
int index = groundMap[x][y];LPD3DSURFACE9 *pSurface = textures[index];pDevice->StretchRect(pSurface, ...);


And that should give you another error, if the code you posted is your actual code, as you don't define destRect before using it. You need to set the destination rectangle to the appropriate place the texture should be drawn.
Quote:Original post by Mike.Popoloski
We can't tell you why you wrote the code that you did. You should know that. Unless of course you've copied this code in from some other place without understanding it, in which case you should probably go back to the source and have them explain it to you.


No I didnt copy it, whats the point with doing that?

Quote:Original post by Erik Rufelt
See the documentation for IDirect3DDevice9::StretchRect. This function works by copying an image from one IDirect3DSurface9 to another. This is not just a type, but a D3D object, containing an image buffer. Your array is not an image buffer, it's an array of numbers.
From the rest of you code I assume you want that number to specify one of your textures, and you want to draw that texture. To do that you would use something like the following:
*** Source Snippet Removed ***

And that should give you another error, if the code you posted is your actual code, as you don't define destRect before using it. You need to set the destination rectangle to the appropriate place the texture should be drawn.


Thanks Erik, I will take a look! *EDIT* I found the destRect, it was deleted by accident from the code before posting it, I had some commented code so it was deleted by mistake together with that. It is a 'RECT destRect' *EDIT*

Note to the rest: This is a very helpful reply, learn from him! He didn't fix my code but pointed me in the direction so I can learn.
Quote:Original post by FrozenSnake
Note to the rest: This is a very helpful reply, learn from him! He didn't fix my code but pointed me in the direction so I can learn.


Huh?
Quote:Original post by Promethium
groundMap is an array of int's, and pSurface is a IDirect3DSurface9*. You can't assign one to the other, which is what the error tells you.

Quote:Original post by Mike.Popoloski
You need to change your code so that you are not trying to use an integer as a surface. You cannot convert an integer into a surface using any sort of cast. An integer is not a surface. It is a number.


To be perfectly honest, if you don't know the difference between an integer and a pointer to a struct / class, you really need to stop, take a step back and learn the basics. Jumping in head first is just going to cause you to run into more problems like this and will ultimately slow you down.

This topic is closed to new replies.

Advertisement