Problem displaying images.

Started by
6 comments, last by JDX_John 15 years, 4 months ago
Hi again. I am trying to show up 10 sprites taken of a "sprites.bmp" file, in the project folder, to the screen, but I get some errors. The code I think the errors come from is:

VOID Render()
{
	IDirect3DSurface9* surface		 ;
	IDirect3DSurface9* spriteSurface ;
	IDirect3DSurface9* backbuffer	 ;
	if (NULL==g_pd3dDevice)
		return;

	hResult=g_pd3dDevice->CreateOffscreenPlainSurface(640, 480, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
	if (FAILED(hResult))
		return;
	spriteSurface=getSurfaceFromBitmap("sprites.bmp");
	
	//Clear the backbuffer to a blue color
	g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(100, 200, 255), 1.0f, 0);

	//Get the backbuffer
	g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); //Here we get a pointer to the backbuffer surface
	//Copy the offscreen(surface) to the backbuffer
	for (int i = 10; i<10; i++)
	{
		RECT destRect; //Temporary destination rect
		//Fill the temporary rect with data from the current sprite structure
		destRect.left	=	spriteStruct.srcRect.left				 ;
		destRect.top	=	spriteStruct.srcRect.top					 ;
		destRect.bottom =	spriteStruct.srcRect.top  + SPRITE_HEIGHT ;
		destRect.right  =	spriteStruct.srcRect.left + SPRITE_WIDTH  ;

		//Draw the sprite to the backbuffer
		g_pd3dDevice->StretchRect(spriteSurface,
								  &spriteStruct.srcRect,
								  backbuffer,
								  &destRect,
								  D3DTEXF_NONE);
	}

//	if (SUCCEEDED(g_pd3dDevice->BeginScene()))
//	{
		//rendering of scene objects can happen here

		//End the scene
//		g_pd3dDevice->EndScene();
//	}

	//Present the backbuffer contents to the display
	g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}

IDirect3DSurface9* getSurfaceFromBitmap(std::string filename)
{
	HRESULT			   hResult = NULL;
	IDirect3DSurface9* surface = NULL;
	D3DXIMAGE_INFO	   imageInfo	 ; //holds details concerning this bitmap

	//Get the width and height info from this bitmap
	hResult=D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);

	//Make sure that the call to D3DXGetImageInfoFromFile was succeeded
	if (FAILED(hResult))
		return NULL;

	//Create the offscreen surface that will hold the bitmap
	hResult=g_pd3dDevice->CreateOffscreenPlainSurface(800, 600, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
	if (FAILED(hResult))
		return NULL;

	//Load the bitmap into the surface
	hResult=D3DXLoadSurfaceFromFile(surface,
									NULL,
									NULL,
									filename.c_str(),
									NULL,
									D3DX_DEFAULT,
									0,
									NULL);

	if (FAILED(hResult))
		return NULL;

	return surface;
}

bool initSprites(void)
{
	//Loop through 10 sprite structures and initialize them
	for (int i = 0; i < 10; i++)
	{
		spriteStruct.srcRect.top    = 0										     ;
		spriteStruct.srcRect.left   = i * SPRITE_WIDTH						     ;
		spriteStruct.srcRect.right  = spriteStruct.srcRect.left + SPRITE_WIDTH ;
		spriteStruct.srcRect.bottom = SPRITE_HEIGHT						         ;

		spriteStruct.posX		   = rand() % SCRN_WIDTH  - SPRITE_WIDTH		 ;
		spriteStruct.posY		   = rand() % SCRN_HEIGHT - SPRITE_HEIGHT		 ;
	}
	return true;
}
The errors say:

Sprites_Direct3D error LNK2020: simbol (token) without solving (0A00000A) spriteStruct

Sprites_Direct3D fatal error LNK1120: 1 extern without solving
Why do I get this errors? Is there any mistake in code? And yes, I included the link dependencies with lines:

#pragma comment(lib,"d3d9.lib");
#pragma comment(lib,"d3dx9.lib");
Thank you for attention. Ah! I almost forget it! Do you know any code colorizer, to make reading codes clearer and easier? Thank you! EDIT: Gave your thread a title, replaced large code tag with source tag. -jba. [Edited by - jbadams on December 8, 2008 9:33:26 AM]
Advertisement
What compiler are you using? Does it really not support colouring code?
Construct (Free open-source game creator)
I am using Microsoft Visual Studio .NET, and yes, it does colorizes the text, but at time of copying into post, colors dissapear.

What can I do whit the problem above?

Thank you,

Skinner
Quote:Original post by darkcube
I am using Microsoft Visual Studio .NET, and yes, it does colorizes the text, but at time of copying into post, colors dissapear.

What can I do whit the problem above?

Thank you,

Skinner


You should use "source" tag.
For more information see FAQ.

Sample:
#include <iostream>int main(int argc, char * argv[]){  std::cout << "Hello World!" << std::endl;  return 0;}


Quote:Original post by darkcube
I am using Microsoft Visual Studio .NET, and yes, it does colorizes the text, but at time of copying into post, colors dissapear.

What can I do whit the problem above?

Thank you,

Skinner
You haven't implemented spriteStruct anywhere, the linker doesn't know where to find it. You have a declaration, but no implementation.

As for the code-colouriser, just use [ source ] tags instead of
Quote:Original post by darkcube
Ah! I almost forget it! Do you know any code colorizer, to make reading codes clearer and easier?
Hi,

I edited your post to give the thread a descriptive title (rather than being listed as "untitled" as it was originally posted) and to replace the code tags around your largest segment of code with source tags as described in this section of the forum FAQ. You can hit the edit button on your post if you'd like to see the change I made (as a side-note, you can also do this to other user's posts to see how they've posted in a certain style, you just won't be able to submit changes).

Carry on folks! [smile]

- Jason Astle-Adams

Thank you jbadams :)
Quote:Original post by Evil Steve
Quote:Original post by darkcube
I am using Microsoft Visual Studio .NET, and yes, it does colorizes the text, but at time of copying into post, colors dissapear.

What can I do whit the problem above?

Thank you,

Skinner
You haven't implemented spriteStruct anywhere, the linker doesn't know where to find it. You have a declaration, but no implementation.

As for the code-colouriser, just use [ source ] tags instead of <!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE--><br><br>Hey Evil Steve. I did implemented the spriteStruct. Here is the whole code:<br><br><!--STARTSCRIPT--><!--source lang="cpp"--><div class="source"><pre><br><span class="cpp-comment">//To convert an aplication from WINDOWED mode, to FULL SCREEN mode:</span><br><span class="cpp-comment">//1. Change WS_OVERLAPPEDWINDOW to WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE</span><br><span class="cpp-comment">//2. d3dpp.backbufferformat=D3DFMT_X8R8G8B8</span><br><span class="cpp-comment">//3. d3dpp.windowed=FALSE</span><br><br><span class="cpp-comment">//In this example, we encompass all needed functions to another one, called getSurfaceFromBitmap</span><br><br><span class="cpp-directive">#include</span> &lt;windows.h&gt;<br><span class="cpp-directive">#include</span> &lt;string&gt;<br><span class="cpp-directive">#include</span> &lt;d3d9.h&gt;<br><span class="cpp-directive">#include</span> &lt;d3dx9.h&gt;<br><span class="cpp-directive">#include</span> <span class="cpp-literal">"resource.h"</span><br><br>#pragma comment(lib,<span class="cpp-literal">"d3d9.lib"</span>); <span class="cpp-comment">//Link dependencies: instead of Project -&gt; Properties -&gt; Linker</span><br>#pragma comment(lib,<span class="cpp-literal">"d3dx9.lib"</span>); <span class="cpp-comment">//Link dependencies: instead of Project -&gt; Properties -&gt; Linker</span><br><br><span class="cpp-comment">//GLOBAL VARIABLES</span><br>HINSTANCE g_hInstance = <span class="cpp-number">0</span>;<br>HWND g_hWnd = <span class="cpp-number">0</span>;<br>LPDIRECT3D9 g_pD3D = <span class="cpp-number">0</span>;<br>LPDIRECT3DDEVICE9 g_pd3dDevice = <span class="cpp-number">0</span>;<br>HRESULT hResult = <span class="cpp-number">0</span>;<br><span class="cpp-directive">#define</span> SPRITE_WIDTH <span class="cpp-number">48</span><br><span class="cpp-directive">#define</span> SPRITE_HEIGHT <span class="cpp-number">48</span><br><span class="cpp-directive">#define</span> SCRN_WIDTH <span class="cpp-number">640</span><br><span class="cpp-directive">#define</span> SCRN_HEIGHT <span class="cpp-number">480</span><br><span class="cpp-keyword">struct</span> {<br> RECT srcRect;<br> <span class="cpp-comment">//Position</span><br> <span class="cpp-keyword">int</span> posX;<br> <span class="cpp-keyword">int</span> posY;<br>} spriteStruct[];<br><br><span class="cpp-comment">//Function prototypes</span><br>LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);<br>HRESULT InitD3D( HWND hWnd );<br><span class="cpp-keyword">VOID</span> Cleanup();<br><span class="cpp-keyword">VOID</span> Render();<br>IDirect3DSurface9* getSurfaceFromBitmap(std::string filename);<br><span class="cpp-keyword">bool</span> initSprites(<span class="cpp-keyword">void</span>);<br><br><br><span class="cpp-comment">//WinMain</span><br><span class="cpp-keyword">INT</span> WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, <span class="cpp-keyword">int</span> showCmd)<br>{<br> <span class="cpp-comment">//Save handle</span><br> g_hInstance = hInstance;<br><br> <span class="cpp-comment">//Fill out a WNDCLASS structure</span><br> WNDCLASS wc;<br> wc.lpfnWndProc=WndProc;<br> wc.style=CS_HREDRAW|CS_VREDRAW;<br> wc.lpfnWndProc=WndProc;<br> wc.cbClsExtra=<span class="cpp-number">0</span>;<br> wc.cbWndExtra=<span class="cpp-number">0</span>;<br> wc.hbrBackground=(HBRUSH)::GetStockObject(BLACK_BRUSH);<br> wc.hIcon=::LoadIcon(<span class="cpp-number">0</span>, IDI_APPLICATION);<br> wc.hCursor=::LoadCursor(<span class="cpp-number">0</span>, IDC_ARROW);<br> wc.hInstance=g_hInstance;<br> wc.lpszMenuName=<span class="cpp-number">0</span>;<br> wc.lpszClassName=<span class="cpp-literal">"WindowA"</span>;<br> <br> <span class="cpp-comment">//Register our WNDCLASS structure (wc)</span><br> RegisterClass( &amp;wc );<br><br> <span class="cpp-comment">//Create the window</span><br> g_hWnd=::CreateWindow(<span class="cpp-literal">"WindowA"</span>, <span class="cpp-literal">"Title bar"</span>, WS_OVERLAPPEDWINDOW, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, <span class="cpp-number">800</span>, <span class="cpp-number">600</span>, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, g_hInstance, <span class="cpp-number">0</span>);<br> <span class="cpp-keyword">if</span> (g_hWnd == <span class="cpp-number">0</span>)<br> {<br> MessageBox(<span class="cpp-number">0</span>, <span class="cpp-literal">"Create window - FAILED"</span>, <span class="cpp-literal">"ERROR"</span>, MB_OK);<br> <span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>;<br> }<br> <span class="cpp-keyword">if</span> (SUCCEEDED(InitD3D(g_hWnd)))<br> {<br> <span class="cpp-comment">//Show and update the window</span><br> ShowWindow(g_hWnd, showCmd);<br> UpdateWindow(g_hWnd);<br><br> <span class="cpp-comment">//Enter the message loop</span><br> MSG msg;<br> ZeroMemory( &amp;msg, <span class="cpp-keyword">sizeof</span>(msg) );<br><br> <span class="cpp-keyword">while</span> (GetMessage(&amp;msg, NULL, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>))<br> {<br> TranslateMessage(&amp;msg);<br> DispatchMessage(&amp;msg);<br> }<br> <span class="cpp-keyword">return</span> (<span class="cpp-keyword">int</span>)msg.wParam;<br> }<br> <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br>}<br><br>LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)<br>{<br> <span class="cpp-keyword">switch</span>( msg )<br> {<br> <span class="cpp-keyword">case</span> WM_LBUTTONDOWN:<br> MessageBox (<span class="cpp-number">0</span>, <span class="cpp-literal">"You pressed the left button!"</span>, <span class="cpp-literal">"LEFT CLIC"</span>, MB_OK);<br> <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br><br> <span class="cpp-keyword">case</span> WM_KEYDOWN:<br> <span class="cpp-keyword">if</span> (wParam == VK_ESCAPE)<br> {<br> <span class="cpp-keyword">if</span> ((MessageBox(g_hWnd, <span class="cpp-literal">"Would you like to exit?"</span>, <span class="cpp-literal">"Exit"</span>, MB_YESNO + MB_DEFBUTTON2)==IDYES)) <br> DestroyWindow(g_hWnd);<br> }<br> <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br><br> <span class="cpp-keyword">case</span> WM_PAINT:<br> Render();<br> ValidateRect(g_hWnd, NULL);<br> <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br><br> <span class="cpp-keyword">case</span> WM_DESTROY:<br> Cleanup();<br> PostQuitMessage(<span class="cpp-number">0</span>);<br> <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br> }<br> <span class="cpp-keyword">return</span> DefWindowProc(hWnd, msg, wParam, lParam);<br>}<br><br>HRESULT InitD3D( HWND hWnd )<br>{<br> <span class="cpp-comment">//Create the D3D Object, needed to create the device.</span><br> <span class="cpp-keyword">if</span> (NULL ==(g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))<br> <span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>;<br> <br> D3DPRESENT_PARAMETERS d3dpp;<br> ZeroMemory(&amp;d3dpp, <span class="cpp-keyword">sizeof</span>(d3dpp));<br> d3dpp.Windowed = <span class="cpp-keyword">TRUE</span>;<br> d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;<br> d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;<br> d3dpp.BackBufferCount = <span class="cpp-number">1</span>;<br> d3dpp.BackBufferHeight = <span class="cpp-number">480</span>;<br> d3dpp.BackBufferWidth = <span class="cpp-number">640</span>;<br> d3dpp.hDeviceWindow = g_hWnd;<br><br> <span class="cpp-keyword">if</span> (FAILED(g_pD3D-&gt;CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &amp;d3dpp, &amp;g_pd3dDevice)))<br> <span class="cpp-keyword">return</span> E_FAIL;<br><br> <span class="cpp-keyword">return</span> S_OK;<br>}<br><br><span class="cpp-keyword">VOID</span> Cleanup()<br>{<br> <span class="cpp-keyword">if</span> (g_pd3dDevice != NULL)<br> g_pd3dDevice-&gt;Release();<br> <span class="cpp-keyword">if</span> (g_pD3D != NULL)<br> g_pD3D-&gt;Release();<br>}<br><br><span class="cpp-keyword">VOID</span> Render()<br>{<br> IDirect3DSurface9* surface ;<br> IDirect3DSurface9* spriteSurface ;<br> IDirect3DSurface9* backbuffer ;<br> <span class="cpp-keyword">if</span> (NULL==g_pd3dDevice)<br> <span class="cpp-keyword">return</span>;<br><br> hResult=g_pd3dDevice-&gt;CreateOffscreenPlainSurface(<span class="cpp-number">640</span>, <span class="cpp-number">480</span>, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &amp;surface, NULL);<br> <span class="cpp-keyword">if</span> (FAILED(hResult))<br> <span class="cpp-keyword">return</span>;<br> <span class="cpp-comment">//hResult=D3DXLoadSurfaceFromFile(surface, NULL, NULL, "test.bmp", NULL, D3DX_DEFAULT, 0, NULL);</span><br> <span class="cpp-comment">//if (FAILED(hResult))</span><br><span class="cpp-comment">// return;</span><br> spriteSurface=getSurfaceFromBitmap(<span class="cpp-literal">"sprites.bmp"</span>);<br> <br> <span class="cpp-comment">//Clear the backbuffer to a blue color</span><br> g_pd3dDevice-&gt;Clear(<span class="cpp-number">0</span>, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(<span class="cpp-number">100</span>, <span class="cpp-number">200</span>, <span class="cpp-number">255</span>), <span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span>);<br><br> <span class="cpp-comment">//Get the backbuffer</span><br> g_pd3dDevice-&gt;GetBackBuffer(<span class="cpp-number">0</span>, <span class="cpp-number">0</span>, D3DBACKBUFFER_TYPE_MONO, &amp;backbuffer); <span class="cpp-comment">//Here we get a pointer to the backbuffer surface</span><br> <span class="cpp-comment">//Copy the offscreen(surface) to the backbuffer</span><br> <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> i = <span class="cpp-number">10</span>; i&lt;<span class="cpp-number">10</span>; i++)<br> {<br> RECT destRect; <span class="cpp-comment">//Temporary destination rect</span><br> <span class="cpp-comment">//Fill the temporary rect with data from the current sprite structure</span><br> destRect.left = spriteStruct<span style="font-weight:bold;">.srcRect.left ;<br> destRect.top = spriteStruct<span style="font-weight:bold;">.srcRect.top ;<br> destRect.bottom = spriteStruct<span style="font-weight:bold;">.srcRect.top + SPRITE_HEIGHT ;<br> destRect.right = spriteStruct<span style="font-weight:bold;">.srcRect.left + SPRITE_WIDTH ;<br><br> <span class="cpp-comment">//Draw the sprite to the backbuffer</span><br> g_pd3dDevice-&gt;StretchRect(spriteSurface,<br> &amp;spriteStruct<span style="font-weight:bold;">.srcRect,<br> backbuffer,<br> &amp;destRect,<br> D3DTEXF_NONE);<br> }<br><br><span class="cpp-comment">// if (SUCCEEDED(g_pd3dDevice-&gt;BeginScene()))</span><br><span class="cpp-comment">// {</span><br> <span class="cpp-comment">//rendering of scene objects can happen here</span><br><br> <span class="cpp-comment">//End the scene</span><br><span class="cpp-comment">// g_pd3dDevice-&gt;EndScene();</span><br><span class="cpp-comment">// }</span><br><br> <span class="cpp-comment">//Present the backbuffer contents to the display</span><br> g_pd3dDevice-&gt;Present(NULL, NULL, NULL, NULL);<br>}<br><br>IDirect3DSurface9* getSurfaceFromBitmap(std::string filename)<br>{<br> HRESULT hResult = NULL;<br> IDirect3DSurface9* surface = NULL;<br> D3DXIMAGE_INFO imageInfo ; <span class="cpp-comment">//holds details concerning this bitmap</span><br><br> <span class="cpp-comment">//Get the width and height info from this bitmap</span><br> hResult=D3DXGetImageInfoFromFile(filename.c_str(), &amp;imageInfo);<br><br> <span class="cpp-comment">//Make sure that the call to D3DXGetImageInfoFromFile was succeeded</span><br> <span class="cpp-keyword">if</span> (FAILED(hResult))<br> <span class="cpp-keyword">return</span> NULL;<br><br> <span class="cpp-comment">//Create the offscreen surface that will hold the bitmap</span><br> hResult=g_pd3dDevice-&gt;CreateOffscreenPlainSurface(<span class="cpp-number">800</span>, <span class="cpp-number">600</span>, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &amp;surface, NULL);<br> <span class="cpp-keyword">if</span> (FAILED(hResult))<br> <span class="cpp-keyword">return</span> NULL;<br><br> <span class="cpp-comment">//Load the bitmap into the surface</span><br> hResult=D3DXLoadSurfaceFromFile(surface,<br> NULL,<br> NULL,<br> filename.c_str(),<br> NULL,<br> D3DX_DEFAULT,<br> <span class="cpp-number">0</span>,<br> NULL);<br><br> <span class="cpp-keyword">if</span> (FAILED(hResult))<br> <span class="cpp-keyword">return</span> NULL;<br><br> <span class="cpp-keyword">return</span> surface;<br>}<br><br><span class="cpp-keyword">bool</span> initSprites(<span class="cpp-keyword">void</span>)<br>{<br> <span class="cpp-comment">//Loop through 10 sprite structures and initialize them</span><br> <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> i = <span class="cpp-number">0</span>; i &lt; <span class="cpp-number">10</span>; i++)<br> {<br> spriteStruct<span style="font-weight:bold;">.srcRect.top = <span class="cpp-number">0</span> ;<br> spriteStruct<span style="font-weight:bold;">.srcRect.left = i * SPRITE_WIDTH ;<br> spriteStruct<span style="font-weight:bold;">.srcRect.right = spriteStruct<span style="font-weight:bold;">.srcRect.left + SPRITE_WIDTH ;<br> spriteStruct<span style="font-weight:bold;">.srcRect.bottom = SPRITE_HEIGHT ;<br><br> spriteStruct<span style="font-weight:bold;">.posX = rand() % SCRN_WIDTH - SPRITE_WIDTH ;<br> spriteStruct<span style="font-weight:bold;">.posY = rand() % SCRN_HEIGHT - SPRITE_HEIGHT ;<br> }<br> <span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;<br>}<br><br></pre></div><!--ENDSCRIPT--><br><br>What's wrong with the code?? :S<br><br>Thank you :)<br><br>Skinner<br><br><br><br>
struct {	RECT srcRect;	//Position	int posX;	int posY;} spriteStruct[];
This is wrong I am pretty sure. It looks like an ugly, illegal mix of C and C++ syntax. Look up how to do structs in a C++ reference.

www.simulatedmedicine.com - medical simulation software

Looking to find experienced Ogre & shader developers/artists. PM me or contact through website with a contact email address if interested.

This topic is closed to new replies.

Advertisement