Jump to content

  • Log In with Google      Sign In   
  • Create Account


falcon93

Member Since 17 Jun 2011
Offline Last Active May 01 2013 07:31 PM

#4838355 C# Calcuator Help

Posted by falcon93 on 21 July 2011 - 02:58 AM

Haha, thanks for the code though :)

I was adding some new button like the actual numbers 0 through 9, and I also renamed some of the button. I also made sure to rename them inside my class file.
After doing this the button aren't doing what they're suppose to, in fact they do nothing at all.

If I add buttons and don't define what they do in my class file will that cause my whole program to break?


I havn't read through all replies, but I assume you use Visual Studio?

In properties window, click on the yellow thunder icon. On the right of the field "Click", check if the method name for your button click is correct. I'm not sure if it will solve your problem, but it's worth checking out.

For your second question; If you add a button on your form without creating a click event method for it, your program will not crash, and it will neither crash if you leave the click event method empety. However, it's pointless to have a button that doesn't do anything, right? Posted Image


#4835622 Menu's in XNA

Posted by falcon93 on 15 July 2011 - 03:57 AM

Sure, I havn't said there's anything wrong with the code you use. I just think it's unnecessary advanced for creating a menu with three buttons. Also, he was not just wondering how to capture mouse clicks, he was also asking about the buttons themselves. For the buttons click action, the two most common solutions is either mine, or the more advanced one which includes events.

So no offence, lets leave both our solutions standing here, and let him choose the one that fits him best Posted Image


#4834453 Drawing a Texture

Posted by falcon93 on 12 July 2011 - 01:15 PM

Yes, I tried scaling my image to 512x512 and it worked just fine. I also noticed, using D3DX_DEFAULT_NONPOW2 for the width and height, will not scale the image. Thanks very much for the help :)


May I also ask you; You game me these render settings before, what are they actually doing?

d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); 
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); 
d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); 
d3ddev->SetRenderState(D3DRS_SRCBLENDALPHA, D3DRS_DESTBLENDALPHA);
d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); 
d3ddev->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );

I noticed they didn't do any difference on just an ordinary image, but maybe they can give some cool results with other types of textures?

Thanks for helping :)


#4833968 Drawing a Texture

Posted by falcon93 on 11 July 2011 - 02:28 PM

I'll take a look at it tonight, I'll reply again as soon I get it working >hopefully< :wink:

Thanks a lot for helping.


#4832866 Drawing a Texture

Posted by falcon93 on 08 July 2011 - 11:50 AM

The errors on my desktop computer has been solved, stupid me :wink: My laptop runs x86 Windows 7, while my desktop runs x64 Windows 7. Due to this, I thought that I could use the x64 library file on my desktop, instead for the x86 library file I used on my laptop. That was the problem. I changed the Library Directories path to:
Microsoft DirectX SDK (June 2012)\Lib\x86 and now it works.

Ok, so back to the programming. I've followed a tutorial (DirectX 9) and managed to initialize DirectX, creating a triangle from 3 vertices and color it. However the tutorial then justs goes stright into 3D programming instead of telling how to create a quad and give it a texture. I've pasted my new code below, and I think that I've now completed most of the steps you gave me Yewbie. How will I now create a quad and assign a texture to it?


#include <Windows.h>
#include <d3d9.h>


#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600


#pragma comment (lib, "d3d9.lib")


LPDIRECT3D9    d3d;
LPDIRECT3DDEVICE9  d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer;


void initD3D(HWND hWnd);
void render_frame(void);
void cleanD3D(void);
void init_graphics(void);


struct CUSTOMVERTEX { float X, Y, Z, RHW; DWORD COLOR; };
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND  hWnd;
MSG   msg;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize   = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor   = LoadCursor(NULL, IDC_ARROW);
wc.hIcon   = LoadIcon(NULL, IDI_WINLOGO);
wc.hInstance  = hInstance;
wc.lpfnWndProc  = WindowProc;
wc.lpszClassName = L"MFGCLASS";
wc.style   = CS_VREDRAW | CS_HREDRAW;

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL, L"MFGCLASS", L"MyFirstGame", WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);

ShowWindow(hWnd, nCmdShow);

initD3D(hWnd);

while(true)
{
  while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }

  if (msg.message == WM_QUIT) break;

  render_frame();
}

cleanD3D();

return msg.wParam;
}


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);
}


void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));

d3dpp.Windowed   = true;
d3dpp.SwapEffect  = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow  = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;

d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

init_graphics();
}


void render_frame(void)
{
d3ddev->Clear(9, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);

d3ddev->BeginScene();

d3ddev->SetFVF(CUSTOMFVF);

d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));

d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);
}


void cleanD3D(void)
{
v_buffer->Release();
d3ddev->Release();
d3d->Release();
}


void init_graphics(void)
{
CUSTOMVERTEX vertices[] = 
{
  { 400.0f, 62.5f,  0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
  { 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
  { 150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
};

d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);

VOID* pVoid;

v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
}



#4825881 Menu's in XNA

Posted by falcon93 on 21 June 2011 - 02:46 AM

This is actually pretty easy to do if you are familiar with object-orientated programming.

First of all, you want to be able to hide/show your menu (collection of buttons), and it would also be good to have a place to check button clicks. Therefore, it can be a good idea to do a MainMenu class. In your main menu class, you can do like this to get ability to show/hide your menu:

public void Update()
{
	if (visible)
	{
		button_play.Update();
		button_credits.Update();
		button_quit.Update();
	}
}

public void Draw()
{
	if (visible)
	{
		button_play.Draw();
		button_credits.Draw();
		button_quit.Draw();
	}
}


If you want to show you menu, simply set the the bool visible to true, and if you want to hide it, change it to false.
Ex:
main_menu.visible = true;

When you want to check button click, put this code in your main_menu class:
public void Update()
{
	Point x = new Point(Mouse.GetState().X, Mouse.GetState().Y);

	if (visible)
	{
		button_play.Update();    			
		button_credits.Update();
	button_quit.Update();

		if (Mouse.GetState().LeftButton == ButtonState.Pressed)
		{
			if (button_play.rectangle.Contains(x))
			{
				// What happens when the button was clicked?
			}

			if (button_credits.rectangle.Contains(x))
			{
				// What happens when the button was clicked?
			}

			if (button_quit.rectangle.Contains(x))
			{
		// What happens when the button was clicked?
			}
		}

	}
}


Hope that helps. Maybe I missunderstood what you searched help for, if so just shout Posted Image


PARTNERS