click events?

Started by
8 comments, last by jordi_0071 19 years ago
Hello i am new here and i have a question. I am busy creating a sea battle game but how can i make a onclick event on the boat so you can see it on the field? if you need the code i'll post it for you!
Advertisement
We'll probably need to see some code to see where you are having a problem. What OS/API are you using?
i am using win32 here is the code(sorry for the comments its in Dutch):

#include <windows.h>#include <fstream.h>const char *ClsName = "Zeeslag"; // De class naamconst char *WndName = "Zeeslag"; // De window naam// De window procedureLRESULT CALLBACK WndProc (HWND hwnd, UINT Msg, WPARAM wparam, LPARAM lparam);// Window main///////////////// Craster class///////////////class CRaster {     public:         int Width,Height;        // Dimensies        int BPP;                // Bits Per Pixel.         char * Raster;            // bits van de plaatjes        RGBQUAD * Palette;        // RGB Palette voor de plaatjes        int BytesPerRow;                BITMAPINFO * pbmi;				int LoadBMP (char * szFile);         int GDIPaint (HDC hdc,int x,int y); };		        // Member functiesINT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 				   int nShowCmd){	MSG        Msg;	HWND       hwnd;	WNDCLASSEX WndClsEx;	// Maak de applicatie window	WndClsEx.cbSize        = sizeof (WNDCLASSEX);	WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;	WndClsEx.lpfnWndProc   = WndProc;	WndClsEx.cbClsExtra    = 0;	WndClsEx.cbWndExtra    = 0;	WndClsEx.hIcon         = LoadIcon (NULL, IDI_APPLICATION);	WndClsEx.hCursor       = LoadCursor (NULL, IDC_ARROW);	WndClsEx.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);	WndClsEx.lpszMenuName  = NULL;	WndClsEx.lpszClassName = ClsName;	WndClsEx.hInstance     = hInstance;	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);	// Regristreer de applicatie	RegisterClassEx(&WndClsEx);	// Maak de window Object	hwnd = CreateWindow(ClsName,             // De class naam						WndName,             // De window naam						WS_OVERLAPPEDWINDOW,                         100,                 // X as van de window						100,                 // Y as van de window 						800,                 // Breedte						600,                 // Hoogte						NULL,						NULL,						hInstance,						NULL);	// Kijk of de window is gemaakt	if( !hwnd ) // Als de window niet is gemaakt			return 0; //Stop de applicatie	// Laat de window zien aan de gebruiker	ShowWindow(hwnd, SW_SHOWNORMAL);	UpdateWindow(hwnd);	// Zolang de applicatie aanstaat	while ( GetMessage(&Msg, NULL, 0, 0) )	{		TranslateMessage(&Msg);		DispatchMessage(&Msg);	}	return Msg.wParam;}LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam){HDC          hdc;PAINTSTRUCT  ps;HBRUSH       HBH;static CRaster bmp; static CRaster bmp2;static CRaster bmp3;  static CRaster bmp4; COLORREF clrRed   = RGB(255, 25, 2);COLORREF clrbrown = RGB(70 ,20 ,20);COLORREF clrgreen = RGB(800, 110 ,2);{	switch(msg)	{		  case WM_CREATE:             bmp.LoadBMP ("mijnenveger.bmp"); 			bmp2.LoadBMP ("fregrat.bmp");			bmp3.LoadBMP ("fregrat.bmp");			bmp4.LoadBMP ("kruiser.bmp");             return 0; 		// Als de gebruiker de window wilt sluiten	case WM_DESTROY:		// Sluit de window		PostQuitMessage(WM_QUIT);				break;		case WM_PAINT: 		hdc = BeginPaint(hwnd, &ps);		// bmp paint		bmp.GDIPaint (hdc,380,95); 		bmp2.GDIPaint (hdc,380,145			); 		bmp3.GDIPaint (hdc,380,180); 		bmp4.GDIPaint (hdc,380,215); 				HBH = CreateSolidBrush(RGB(8, 80, 200));				// Blauwe veld 1		SelectObject(hdc, HBH);        Rectangle(hdc, 50, 50, 301, 251);        DeleteObject(HBH);        // Blauwe veld 2		SelectObject(hdc, HBH);		Rectangle(hdc, 50, 320, 301, 521);		DeleteObject(HBH);		// Lijnen van veld 1 horizontaal		// Lijn 1		MoveToEx(hdc, 50, 50, NULL);		LineTo(hdc, 300, 50);		// Lijn 2        MoveToEx(hdc, 50, 70, NULL);		LineTo(hdc, 300, 70);		// Lijn 3		MoveToEx(hdc, 50, 90, NULL);		LineTo(hdc, 300, 90);		// Lijn 4		MoveToEx(hdc, 50, 110, NULL);		LineTo(hdc, 300, 110);		// Lijn 5        MoveToEx(hdc, 50, 130, NULL);		LineTo(hdc, 300, 130);		// Lijn 6        MoveToEx(hdc, 50, 150, NULL);        LineTo(hdc, 300, 150);		// Lijn 7        MoveToEx(hdc, 50, 170, NULL);        LineTo(hdc, 300, 170);		// Lijn 8        MoveToEx(hdc, 50, 190, NULL);        LineTo(hdc, 300, 190);		// Lijn 9        MoveToEx(hdc, 50, 210, NULL);        LineTo(hdc, 300, 210);		// Lijn 10		MoveToEx(hdc, 50, 230, NULL);		LineTo(hdc, 300, 230);        // Lijnen van veld 1 verticaal		// Lijn 1		MoveToEx(hdc, 50, 50, NULL);		LineTo(hdc, 50, 250);		// Lijn 2		MoveToEx(hdc, 75, 50, NULL);        LineTo(hdc, 75, 250);		// Lijn 3        MoveToEx(hdc, 100, 50, NULL);        LineTo(hdc, 100, 250);		// Lijn 4        MoveToEx(hdc, 125, 50, NULL);        LineTo(hdc, 125, 250);		// Lijn 5        MoveToEx(hdc, 150, 50, NULL);        LineTo(hdc, 150, 250);		// Lijn 6		MoveToEx(hdc, 175, 50, NULL);        LineTo(hdc, 175, 250);        // Lijn 7        MoveToEx(hdc, 200, 50, NULL);        LineTo(hdc, 200, 250);        // Lijn 8		MoveToEx(hdc, 225, 50, NULL);        LineTo(hdc, 225, 250);        // Lijn 9		MoveToEx(hdc, 250, 50, NULL);        LineTo(hdc, 250, 250);        // Lijn 10		MoveToEx(hdc, 275, 50, NULL);        LineTo(hdc, 275, 250);		// lijnen van veld 2 horizontaal     // Lijn 1	MoveToEx(hdc, 50, 320,NULL);	LineTo(hdc, 300, 320);    // Lijn 2	MoveToEx(hdc, 50, 340,NULL);	LineTo(hdc, 300, 340);	// Lijn 3	MoveToEx(hdc, 50, 360,NULL);	LineTo(hdc, 300, 360);	// Lijn 4	MoveToEx(hdc, 50, 380,NULL);	LineTo(hdc, 300, 380);	// Lijn 5	MoveToEx(hdc, 50, 400,NULL);	LineTo(hdc, 300, 400);	// Lijn 6	MoveToEx(hdc, 50, 420,NULL);	LineTo(hdc, 300, 420);    // Lijn 7	MoveToEx(hdc, 50, 440,NULL);	LineTo(hdc, 300, 440);    // Lijn 8	MoveToEx(hdc, 50, 460,NULL);	LineTo(hdc, 300, 460);	 // Lijn 9	MoveToEx(hdc, 50, 480,NULL);	LineTo(hdc, 300, 480);	 // Lijn 10	MoveToEx(hdc, 50, 500,NULL);	LineTo(hdc, 300, 500);    // lijn 11	MoveToEx(hdc, 50, 520,NULL);	LineTo(hdc, 300, 520);	// Lijnen van veld 2 verticaal	// Lijn 1	MoveToEx(hdc, 50, 320,NULL); 	LineTo(hdc, 50, 520); 	// Lijn 2	MoveToEx(hdc, 75, 320,NULL); 	LineTo(hdc, 75, 520); 	// Lijn 3	MoveToEx(hdc, 100, 320,NULL); 	LineTo(hdc, 100, 520); 	// Lijn 4	MoveToEx(hdc, 125, 320,NULL); 	LineTo(hdc, 125, 520); 	// Lijn 5	MoveToEx(hdc, 150, 320,NULL); 	LineTo(hdc, 150, 520); 	// Lijn 6	MoveToEx(hdc, 175, 320,NULL); 	LineTo(hdc, 175, 520); 	// Lijn 7	MoveToEx(hdc, 200, 320,NULL); 	LineTo(hdc, 200, 520); 	// Lijn 8	MoveToEx(hdc, 225, 320,NULL); 	LineTo(hdc, 225, 520); 	// Lijn 9	MoveToEx(hdc, 250, 320,NULL); 	LineTo(hdc, 250, 520); 	// Lijn 10	MoveToEx(hdc, 275, 320,NULL); 	LineTo(hdc, 275, 520);     // Lijn 11	MoveToEx(hdc, 300, 320,NULL); 	LineTo(hdc, 300, 520); // de cijfers van de veld 1	// cijfer 1	SetTextColor(hdc, clrRed);	TextOut(hdc, 40, 233, "1", 1);	// cijfer 2	SetTextColor(hdc, clrRed);	TextOut(hdc, 40, 213, "2", 1);	// cijfer 3	SetTextColor(hdc, clrRed);	TextOut(hdc, 40, 193, "3", 1);	// cijfer 4	SetTextColor(hdc, clrRed);	TextOut(hdc, 40, 173, "4", 1);	// cijfer 5	SetTextColor(hdc, clrRed);	TextOut(hdc, 40, 153, "5", 1);	// cijfer 6	SetTextColor(hdc, clrRed);	TextOut(hdc, 40, 133, "6", 1);	// cijfer 7	SetTextColor(hdc, clrRed);	TextOut(hdc, 40, 113, "7", 1);	// cijfer 8	SetTextColor(hdc, clrRed);	TextOut(hdc, 40, 93, "8", 1);	// cijfer 9	SetTextColor(hdc, clrRed);	TextOut(hdc, 40, 73, "9", 1);	// cijfer 10	SetTextColor(hdc, clrRed);	TextOut(hdc, 33, 53, "10", 2);// de cijfers van de veld 2	// cijfer 1		SetTextColor(hdc, clrRed);		TextOut(hdc, 40, 502, "1", 1);	// cijfer 2		SetTextColor(hdc, clrRed);		TextOut(hdc, 40, 482, "2", 1);	// cijfer 3		SetTextColor(hdc, clrRed);		TextOut(hdc, 40, 462, "3", 1);	// cijfer 4		SetTextColor(hdc, clrRed);		TextOut(hdc, 40, 442, "4", 1);	// cijfer 5		SetTextColor(hdc, clrRed);		TextOut(hdc, 40, 422, "5", 1);	// cijfer 6		SetTextColor(hdc, clrRed);		TextOut(hdc, 40, 402, "6", 1);	// cijfer 7		SetTextColor(hdc, clrRed);		TextOut(hdc, 40, 382, "7", 1);	// cijfer 8		SetTextColor(hdc, clrRed);		TextOut(hdc, 40, 362, "8", 1);	// cijfer 9		SetTextColor(hdc, clrRed);		TextOut(hdc, 40, 342, "9", 1);	// cijfer 10		SetTextColor(hdc, clrRed);		TextOut(hdc, 33, 322, "10", 2);// de letters van veld 1				// letter a		SetTextColor(hdc, clrRed);		TextOut(hdc, 59, 251, "a", 1);		// letter b		SetTextColor(hdc, clrRed);		TextOut(hdc, 85, 251, "b", 1);		// letter c		SetTextColor(hdc, clrRed);		TextOut(hdc, 110, 251, "c", 1);		// letter d		SetTextColor(hdc, clrRed);		TextOut(hdc, 135, 251, "d", 1);				// letter e		SetTextColor(hdc, clrRed);		TextOut(hdc, 159, 251, "e", 1);				// letter f		SetTextColor(hdc, clrRed);		TextOut(hdc, 185, 251, "f", 1);				// letter g		SetTextColor(hdc, clrRed);		TextOut(hdc, 211, 251, "g", 1);				// letter h		SetTextColor(hdc, clrRed);		TextOut(hdc, 235, 251, "h", 1);				// letter i		SetTextColor(hdc, clrRed);		TextOut(hdc, 259, 251, "i", 1);					// letter j		SetTextColor(hdc, clrRed);		TextOut(hdc, 285, 251, "j", 1);// de letters van veld 2:				// letter a		SetTextColor(hdc, clrRed);		TextOut(hdc, 59, 521, "a", 1);		// letter b		SetTextColor(hdc, clrRed);		TextOut(hdc, 85, 521, "b", 1);		// letter c		SetTextColor(hdc, clrRed);		TextOut(hdc, 110, 521, "c", 1);		// letter d		SetTextColor(hdc, clrRed);		TextOut(hdc, 135, 521, "d", 1);		// letter e		SetTextColor(hdc, clrRed);		TextOut(hdc, 159, 521, "e", 1);		// letter f		SetTextColor(hdc, clrRed);		TextOut(hdc, 185, 521, "f", 1);		// letter g		SetTextColor(hdc, clrRed);		TextOut(hdc, 211, 521, "g", 1);		// letter h		SetTextColor(hdc, clrRed);		TextOut(hdc, 235, 521, "h", 1);		// letter i		SetTextColor(hdc, clrRed);		TextOut(hdc, 259, 521, "i", 1);		// letter j		SetTextColor(hdc, clrRed);		TextOut(hdc, 285, 521, "j", 1);// Maak een box voor de boten	// Links verticaal van de boten	MoveToEx(hdc, 350, 50,NULL); 	LineTo(hdc, 350, 520);    // Rechts verticaal van de boten	MoveToEx(hdc, 500, 50,NULL); 	LineTo(hdc, 500, 520);	// Horizontaal boven lijn	MoveToEx(hdc, 350, 50,NULL); 	LineTo(hdc, 500, 50);	MoveToEx(hdc, 350, 51,NULL); 	LineTo(hdc, 500, 51);	MoveToEx(hdc, 350, 52,NULL); 	LineTo(hdc, 500, 52);	MoveToEx(hdc, 350, 53,NULL); 	LineTo(hdc, 500, 53);	// Horizontaal box lijn	MoveToEx(hdc, 350, 519,NULL); 	LineTo(hdc, 500, 519);	MoveToEx(hdc, 350, 518,NULL); 	LineTo(hdc, 500, 518);	MoveToEx(hdc, 350, 517,NULL); 	LineTo(hdc, 500, 517);	MoveToEx(hdc, 350, 516,NULL); 	LineTo(hdc, 500, 516);// Teksten// boten tekst		SetTextColor(hdc, clrRed);		TextOut(hdc, 355, 55, "Boten:", 6);				SetTextColor(hdc, clrRed);		TextOut(hdc, 355, 75, "Mijnenveger", 11);				SetTextColor(hdc, clrRed);		TextOut(hdc, 355, 125, "Fregrat", 7);						SetTextColor(hdc, clrRed);		TextOut(hdc, 355, 165, "Fregrat", 7);								SetTextColor(hdc, clrRed);		TextOut(hdc, 355, 205, "kruiser", 7);// De computer tekst		SetTextColor(hdc, clrRed);		TextOut(hdc, 140, 10, "Speler:", 7);				SetTextColor(hdc, clrRed);		TextOut(hdc, 220, 10, "Gewonnen", 8);				SetTextColor(hdc, clrRed);		TextOut(hdc, 234, 30, "Verloren", 8);// De speler tekst		SetTextColor(hdc, clrRed);		TextOut(hdc, 120, 280, "Computer:", 9);		SetTextColor(hdc, clrRed);		TextOut(hdc, 220, 280, "Gewonnen", 8);		SetTextColor(hdc, clrRed);		TextOut(hdc, 234, 300, "Verloren", 8);// Menu tekst			SetTextColor(hdc, clrRed);		TextOut(hdc, 650, 40, "       Menu", 11);		SetTextColor(hdc, clrgreen);		TextOut(hdc, 650, 70,  "  Nieuw spel", 12);		TextOut(hdc, 650, 90,  "Online spelen (n/a)", 19);		TextOut(hdc, 650, 110, "    Options", 11);		TextOut(hdc, 650, 130, "    Afsluiten", 13);	EndPaint(hwnd, &ps);	default:		return DefWindowProc(hwnd, msg, wparam, lparam);	}	return 0;}}int CRaster::LoadBMP (char * szFile) {     BITMAPFILEHEADER bmfh;     BITMAPINFOHEADER bmih;     // Open file.     ifstream bmpfile (szFile , ios::in | ios::binary);     if (! bmpfile.is_open()) return 1;        // Error opening file     // Load bitmap fileheader & infoheader     bmpfile.read ((char*)&bmfh,sizeof (BITMAPFILEHEADER));     bmpfile.read ((char*)&bmih,sizeof (BITMAPINFOHEADER));     // Check filetype signature     if (bmfh.bfType!='MB') return 2;        // File is not BMP     // Assign some short variables:     BPP=bmih.biBitCount;     Width=bmih.biWidth;     Height= (bmih.biHeight>0) ? bmih.biHeight : -bmih.biHeight; // absoulte value     BytesPerRow = Width * BPP / 8;     BytesPerRow += (4-BytesPerRow%4) % 4;    // int alignment     // If BPP aren't 24, load Palette:     if (BPP==24) pbmi=(BITMAPINFO*)new char [sizeof(BITMAPINFO)];     else     {         pbmi=(BITMAPINFO*) new char[sizeof(BITMAPINFOHEADER)+(1<<BPP)*sizeof(RGBQUAD)];         Palette=(RGBQUAD*)((char*)pbmi+sizeof(BITMAPINFOHEADER));         bmpfile.read ((char*)Palette,sizeof (RGBQUAD) * (1<<BPP));     }     pbmi->bmiHeader=bmih;     // Load Raster     bmpfile.seekg (bmfh.bfOffBits,ios::beg);     Raster= new char[BytesPerRow*Height];     // (if height is positive the bmp is bottom-up, read it reversed)     if (bmih.biHeight>0)         for (int n=Height-1;n>=0;n--)             bmpfile.read (Raster+BytesPerRow*n,BytesPerRow);     else         bmpfile.read (Raster,BytesPerRow*Height);     // so, we always have a up-bottom raster (that is negative height for windows):     pbmi->bmiHeader.biHeight=-Height;     bmpfile.close();     return 0; } // ********** // CRaster::GDIPaint (hdc,x,y); // * Paints Raster to a Windows DC. int CRaster::GDIPaint (HDC hdc,int x=0,int y=0) {     // Paint the image to the device.     return SetDIBitsToDevice (hdc,x,y,Width,Height,0,0,         0,Height,(LPVOID)Raster,pbmi,0); }


you can make the boat's for you selfe because i cant upload them at this moment!
You should handle the WM_LBUTTONDOWN event in your WndProc.
do i have to make a case for the left mouse button? en where do i have to place WM_LBUTTONDOWN?
you will probably end up with something like this:
...EndPaint(hwnd, &ps);case WM_LBUTTONDOWN:	HandleButtonClick(lParam 0x0000FFFF, (lParam 0xFFFF0000) >> 16);	break;default:	return DefWindowProc(hwnd, msg, wparam, lparam);}


The lParam 0x0000FFFF and (lParam 0xFFFF0000) >> 16 extract the mouse coords from the lParam var.
i get 3 errors:

D:\Microsoft Visual Studio2\MyProjects\zeeslagv1\Main.cpp(598) : error C2065: 'HandleButtonClick' : undeclared identifier
D:\Microsoft Visual Studio2\MyProjects\zeeslagv1\Main.cpp(598) : error C2143: syntax error : missing ')' before 'constant'
D:\Microsoft Visual Studio2\MyProjects\zeeslagv1\Main.cpp(598) : error C2059: syntax error : ')'


this is what i placed:


case WM_LBUTTONDOWN:
HandleButtonClick(lparam 0x0000FFFF, (lparam 0xFFFF0000) >> 16);
break;
I was using HandleButtonClick to stand in for whatever code you are going to use, just to show you where to put it and how to get the x, y coords. You have to write the function yourself [smile]

EDIT:
The point is that WM_LBUTTONDOWN, tells you when and where the left mouse button was clicked. It up to you then to respond approprietly. You can put whatever you like into HandleButtonClick, or whatever you want to call the function, the point is that it just holds the appropriete response to the message.
do you know a example game like this with the code?
guess not!

This topic is closed to new replies.

Advertisement