help with mouse
hello
i just watched lesson 32 and i am trying to set 2 floats to take the mouse position but i aint success :i want somthing like
when mouse move right
x = x+0.1f;
when it move left
x = x-0.1f;
when it move up
y = y+0.1f;
and so on but i cant seem to get the mouse position can anyone please write here a code that i can use in my program for it?
thx in advance
are you using the WINAPI
if so i have no idea, and can only suggest you switch to SDL( www.libsdl.org )
because it makes these things easier...
if so i have no idea, and can only suggest you switch to SDL( www.libsdl.org )
because it makes these things easier...
Let's say your window is located at (200, 200), has a handle (my_hwnd) and its size is (320 x 240). When you load you app set the mouse position to the centre of the window:
Now whenever a WM_MOUSEMOVE event occurs, get the change in position (delta) from the centre then reset the mouse position to the centre:
Obviously these snippets assume WINAPI and C++ and I've rushed this code off the top of my head as I'm going out so you may have to invert signs and add some casts. Hope they help. ;)
// at startupfloat delta_x = 0.0f;float delta_y = 0.0f;RECT my_rect;GetWindowRect(my_hwnd, my_rect);SetCursorPos(my_rect.left + (window_width / 2), my_rect.top + (window_height / 2));
Now whenever a WM_MOUSEMOVE event occurs, get the change in position (delta) from the centre then reset the mouse position to the centre:
// in wndproc handlercase WM_MOUSEMOVE: // get change in mouse position delta_x += (window_width / 2) - LOWORD(lParam); delta_y += (window_height / 2) - HIWORD(lParam); // reset mouse position RECT my_rect; GetWindowRect(my_hwnd, my_rect); SetCursorPos(my_rect.left + (window_width / 2), my_rect.top + (window_height / 2));break;
Obviously these snippets assume WINAPI and C++ and I've rushed this code off the top of my head as I'm going out so you may have to invert signs and add some casts. Hope they help. ;)
--
Cheers,
Darren Clark
Cheers,
Darren Clark
umm thx but i still got 1 question how do i do that it will happen when
WM_MOUSEMOVE is occured?
what do i write
int WM_MOUSEMOVE()
{
}
?
O_o sorry for the noob question but i am still a newbie in opengl and this stuff
WM_MOUSEMOVE is occured?
what do i write
int WM_MOUSEMOVE()
{
}
?
O_o sorry for the noob question but i am still a newbie in opengl and this stuff
This is (part of) the wndproc I use for my windows implementation (I have abstracted the input/output layer of my programs).
Here Lib is a windows specific implemention of my abstract input/output class, it holds the members MX (current x axis position of the mouse), MY (current y axis position of the mouse), dMX (diffrence between the last position of MX and the current position of MX) and dMY (diffrence between the last position of MX and the current position of MX).
It holds other members of course.
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ //MessageBox(NULL, "Called WndProc", "Message pump working!", MB_OK); switch (uMsg) { case WM_MOUSEMOVE: { int nMX = LOWORD(lParam), nMY = HIWORD(lParam); Lib->dMX = Lib->MX - nMX; Lib->dMX = Lib->MY - nMY; Lib->MX = nMX; Lib->MY = nMY; break; } } return DefWindowProc(hWnd,uMsg,wParam,lParam);}
Here Lib is a windows specific implemention of my abstract input/output class, it holds the members MX (current x axis position of the mouse), MY (current y axis position of the mouse), dMX (diffrence between the last position of MX and the current position of MX) and dMY (diffrence between the last position of MX and the current position of MX).
It holds other members of course.
ok its not working here are the errors:
and here is my code:
//EDIT: Added source tags. You can learn how to use them in the FAQ. - Kaz.
[Edited by - Kazgoroth on September 17, 2005 12:30:21 PM]
Deleting intermediate files and output files for project 'lesson1 - Win32 Debug'.--------------------Configuration: lesson1 - Win32 Debug--------------------Compiling...Lesson1.cppc:\documents and settings\user\desktop\olddesktop\opengl\bigworld\lesson01\lesson1.cpp(28) : error C2501: 'GetWindowRect' : missing storage-class or type specifiersc:\documents and settings\user\desktop\olddesktop\opengl\bigworld\lesson01\lesson1.cpp(28) : error C2373: 'GetWindowRect' : redefinition; different type modifiersc:\program files\microsoft visual studio\vc98\include\winuser.h(6023) : see declaration of 'GetWindowRect'c:\documents and settings\user\desktop\olddesktop\opengl\bigworld\lesson01\lesson1.cpp(28) : error C2078: too many initializersc:\documents and settings\user\desktop\olddesktop\opengl\bigworld\lesson01\lesson1.cpp(28) : error C2440: 'initializing' : cannot convert from 'struct tagRECT' to 'int'No user-defined-conversion operator available that can perform this conversion, or the operator cannot be calledc:\documents and settings\user\desktop\olddesktop\opengl\bigworld\lesson01\lesson1.cpp(29) : error C2065: 'window_width' : undeclared identifierc:\documents and settings\user\desktop\olddesktop\opengl\bigworld\lesson01\lesson1.cpp(29) : error C2065: 'window_height' : undeclared identifierc:\documents and settings\user\desktop\olddesktop\opengl\bigworld\lesson01\lesson1.cpp(29) : error C2501: 'SetCursorPos' : missing storage-class or type specifiersc:\documents and settings\user\desktop\olddesktop\opengl\bigworld\lesson01\lesson1.cpp(29) : error C2373: 'SetCursorPos' : redefinition; different type modifiersc:\program files\microsoft visual studio\vc98\include\winuser.h(6248) : see declaration of 'SetCursorPos'c:\documents and settings\user\desktop\olddesktop\opengl\bigworld\lesson01\lesson1.cpp(29) : error C2078: too many initializersc:\documents and settings\user\desktop\olddesktop\opengl\bigworld\lesson01\lesson1.cpp(693) : fatal error C1004: unexpected end of file foundError executing cl.exe.lesson1.exe - 10 error(s), 0 warning(s)
and here is my code:
#include <windows.h> // Header File For Windows#include <stdio.h>#include <gl\gl.h> // Header File For The OpenGL32 Library#include <gl\glu.h> // Header File For The GLu32 Library#include <gl\glaux.h> // Header File For The Glaux Library#include <stdarg.h>#include <cstdlib> HDC hDC=NULL; // Private GDI Device ContextHGLRC hRC=NULL; // Permanent Rendering ContextHWND hWnd=NULL; // Holds Our Window HandleHINSTANCE hInstance; // Holds The Instance Of The Application#pragma comment( lib, "opengl32.lib" ) // Search For OpenGL32.lib While Linking#pragma comment( lib, "glu32.lib" ) // Search For GLu32.lib While Linking#pragma comment( lib, "winmm.lib" )bool bp;bool blend;bool keys[256]; // Array Used For The Keyboard Routinebool active=TRUE; // Window Active Flag Set To TRUE By Defaultbool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By DefaultGLfloat yy;GLfloat xx;GLuint texture[18];float delta_x = 0.0f;float delta_y = 0.0f;RECT my_rect;GetWindowRect(hWnd, my_rect);SetCursorPos(my_rect.left + (window_width / 2), my_rect.top + (window_height / 2));int life =16;GLfloat a =0;int rota = 0;float hj=100;float rott = 0;GLfloat b =0;GLfloat z =-1;int noob = 0;int lag = 0;GLfloat n =-1;LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProcGLfloat k = 0;GLfloat mm= 0.7f;GLfloat uu = n;GLfloat oo = 0;GLfloat floor;GLfloat abcd=0;GLfloat aslan = 0;GLfloat ik=1;int random; AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image{FILE *File=NULL;if (!Filename) // Make Sure A Filename Was Given{return NULL; // If Not Return NULL}File=fopen(Filename,"r");if (File) // Does The File Exist?{fclose(File); // Close The Handlereturn auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer}return NULL; // If Load Failed Return NULL}int LoadGLTextures() // Load Bitmaps And Convert To Textures{ int Status=FALSE; AUX_RGBImageRec *TextureImage[2]; memset(TextureImage,0,sizeof(void *)*2); // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit if ((TextureImage[0]=LoadBMP("Data/ground.bmp")) && (TextureImage[1]=LoadBMP("Data/sky.bmp"))) { Status=TRUE; glGenTextures(2, &texture[0]); // Create The Texture for (int i=0; i<2; i++) { // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); //glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage- >sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data); // glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage->sizeX, TextureImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data); } } for (int i=0; i<2; i++) { if (TextureImage) // If Texture Exists { if (TextureImage->data) // If Texture Image Exists { free(TextureImage->data); // Free The Texture Image Memory } free(TextureImage); // Free The Image Structure } } return Status; // Return The Status} GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window{if (height==0) // Prevent A Divide By Zero By{height=1; // Making Height Equal One}glViewport(0,0,width,height); // Reset The Current ViewportglMatrixMode(GL_PROJECTION); // Select The Projection MatrixglLoadIdentity(); // Reset The Projection Matrix// Calculate The Aspect Ratio Of The WindowgluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW); // Select The Modelview MatrixglLoadIdentity(); // Reset The Modelview Matrix}int InitGL(GLvoid) // All Setup For OpenGL Goes Here{if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW ){return FALSE; // If Texture Didn't Load Return FALSE ( NEW )}glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); // Enable Smooth ShadingglClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black BackgroundglClearDepth(1.0f); // Depth Buffer SetupglEnable(GL_DEPTH_TEST); // Enables Depth TestingglDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To DoglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations // Play Gun Shot Sound//glColor4f(1.0f,1.0f,1.0f,0.5f); // Full Brightness, 50% Alpha ( NEW ) //glBlendFunc(GL_SRC_ALPHA,GL_ONE);return TRUE; // Initialization Went OK}int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth BufferglLoadIdentity(); if (noob==-1){ z = z-0.1f; mm = mm-0.1f;noob=0;}if (noob==1){ z = z+0.1f; mm = mm+0.1f;noob=0;}if (lag==1){b=b-0.1f;oo = oo-0.1f;//hj = hj+0.5f;lag=0;}if (lag==-1){ b= b+0.1f;// hj = hj-0.5f; oo = oo+0.1f; lag =0;}if (rota ==1){rott = rott-0.2f;rota =0;}if (rota==-1){rott = rott+0.2f; rota=0;}yy=a;xx=b;glTranslatef(xx,n,z); glRotatef(180,0.0,1.0,0.0);glRotatef(delta_x,0,1,0);//glColor4f(1.0f,1.0f,1.0f,0.5f);//glRotatef(xx,0.0,1.0,0.0);//glRotatef(k,1.0,0.0,0.0);glBindTexture(GL_TEXTURE_2D, texture[0]);glBegin(GL_QUADS);//for (float af=50;af>50;af=af-2)//{//glTexCoord2f(0.0,1.0); glVertex3f(1.0,-1.0,af-4);//glTexCoord2f(1.0,1.0);glVertex3f(-1.0,-1.0,af-4);//glTexCoord2f(0.0,0.0);glVertex3f(-1.0,0.0,af);//glTexCoord2f(1.0,0.0);glVertex3f(1.0,0.0,af);//} /*glTexCoord2f(0.0,1.0); glVertex3f(5.0,0.0,-9);glTexCoord2f(1.0,1.0);glVertex3f(-5.0,0.0,-9);glTexCoord2f(0.0,0.0);glVertex3f(-5.0,0.0,0);glTexCoord2f(1.0,0.0);glVertex3f(5.0,0.0,0);glTexCoord2f(0.0,1.0); glVertex3f(5.0,0,0);glTexCoord2f(1.0,1.0);glVertex3f(-5.0,0.0,0);glTexCoord2f(0.0,0.0);glVertex3f(-5.0,0.0,9);glTexCoord2f(1.0,0.0);glVertex3f(5.0,0.0,9);*/glTexCoord2f(0.0,1.0); glVertex3f(100.0,0.0,200);glTexCoord2f(1.0,1.0);glVertex3f(-100.0,0.0,200);glTexCoord2f(1.0,0.0);glVertex3f(-100.0,0.0,-200.0);glTexCoord2f(0.0,0.0);glVertex3f(100.0,0.0,-200.0); glEnd();//glTranslatef(-xx,-n,-z);//glTranslatef(0,0,-6);glTranslatef(oo,aslan,mm); glBindTexture(GL_TEXTURE_2D, texture[1]);glBegin(GL_QUADS);glTexCoord2f(0.0,1.0); glVertex3f(hj,50.0,50);glTexCoord2f(1.0,1.0);glVertex3f(-hj,50.0,50);glTexCoord2f(1.0,0.0);glVertex3f(-hj,0.0,50);glTexCoord2f(0.0,0.0);glVertex3f(hj,0.0,50); glEnd();return TRUE; // Everything Went OK}GLvoid KillGLWindow(GLvoid) // Properly Kill The Window{if (fullscreen) // Are We In Fullscreen Mode?{ChangeDisplaySettings(NULL,0); // If So Switch Back To The DesktopShowCursor(TRUE); // Show Mouse Pointer}if (hRC) // Do We Have A Rendering Context?{if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?{MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);}if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?{MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);}hRC=NULL; // Set RC To NULL}if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC{MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);hDC=NULL; // Set DC To NULL}if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?{MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);hWnd=NULL; // Set hWnd To NULL}if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class{MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);hInstance=NULL; // Set hInstance To NULL}}/* This Code Creates Our OpenGL Window. Parameters Are: ** title - Title To Appear At The Top Of The Window ** width - Width Of The GL Window Or Fullscreen Mode ** height - Height Of The GL Window Or Fullscreen Mode ** bits - Number Of Bits To Use For Color (8/16/24/32) ** fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag){GLuint PixelFormat; // Holds The Results After Searching For A MatchWNDCLASS wc; // Windows Class StructureDWORD dwExstyle; // Window Extended styleDWORD dwstyle; // Window styleRECT WindowRect; // Grabs Rectangle Upper Left / Lower Right ValuesWindowRect.left=(long)0; // Set Left Value To 0WindowRect.right=(long)width; // Set Right Value To Requested WidthWindowRect.top=(long)0; // Set Top Value To 0WindowRect.bottom=(long)height; // Set Bottom Value To Requested Heightfullscreen=fullscreenflag; // Set The Global Fullscreen FlaghInstance = GetModuleHandle(NULL); // Grab An Instance For Our Windowwc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messageswc.cbClsExtra = 0; // No Extra Window Datawc.cbWndExtra = 0; // No Extra Window Datawc.hInstance = hInstance; // Set The Instancewc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Iconwc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointerwc.hbrBackground = NULL; // No Background Required For GLwc.lpszMenuName = NULL; // We Don't Want A Menuwc.lpszClassName = "OpenGL"; // Set The Class Nameif (!RegisterClass(&wc)) // Attempt To Register The Window Class{MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE; // Return FALSE}if (fullscreen) // Attempt Fullscreen Mode?{DEVMODE dmScreenSettings; // Device Modememset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's CleareddmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode StructuredmScreenSettings.dmPelsWidth = width; // Selected Screen WidthdmScreenSettings.dmPelsHeight = height; // Selected Screen HeightdmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per PixeldmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL){// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES){fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE}else{// Pop Up A Message Box Letting User Know The Program Is Closing.MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);return FALSE; // Return FALSE}}}if (fullscreen) // Are We Still In Fullscreen Mode?{dwExstyle=WS_EX_APPWINDOW; // Window Extended styledwstyle=WS_POPUP; // Windows styleShowCursor(FALSE); // Hide Mouse Pointer}else{dwExstyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended styledwstyle=WS_OVERLAPPEDWINDOW; // Windows style}AdjustWindowRectEx(&WindowRect, dwstyle, FALSE, dwExstyle); // Adjust Window To True Requested Size// Create The Windowif (!(hWnd=CreateWindowEx( dwExstyle, // Extended style For The Window"OpenGL", // Class Nametitle, // Window Titledwstyle | // Defined Window styleWS_CLIPSIBLINGS | // Required Window styleWS_CLIPCHILDREN, // Required Window style0, 0, // Window PositionWindowRect.right-WindowRect.left, // Calculate Window WidthWindowRect.bottom-WindowRect.top, // Calculate Window HeightNULL, // No Parent WindowNULL, // No MenuhInstance, // InstanceNULL))) // Dont Pass Anything To WM_CREATE{KillGLWindow(); // Reset The DisplayMessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE; // Return FALSE}static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be{sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor1, // Version NumberPFD_DRAW_TO_WINDOW | // Format Must Support WindowPFD_SUPPORT_OPENGL | // Format Must Support OpenGLPFD_DOUBLEBUFFER, // Must Support Double BufferingPFD_TYPE_RGBA, // Request An RGBA Formatbits, // Select Our Color Depth0, 0, 0, 0, 0, 0, // Color Bits Ignored0, // No Alpha Buffer0, // Shift Bit Ignored0, // No Accumulation Buffer0, 0, 0, 0, // Accumulation Bits Ignored16, // 16Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer0, // No Auxiliary BufferPFD_MAIN_PLANE, // Main Drawing Layer0, // Reserved0, 0, 0 // Layer Masks Ignored};if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?{KillGLWindow(); // Reset The DisplayMessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE; // Return FALSE}if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?{KillGLWindow(); // Reset The DisplayMessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE; // Return FALSE}if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?{KillGLWindow(); // Reset The DisplayMessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE; // Return FALSE}if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?{KillGLWindow(); // Reset The DisplayMessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE; // Return FALSE}if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context{KillGLWindow(); // Reset The DisplayMessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE; // Return FALSE}ShowWindow(hWnd,SW_SHOW); // Show The WindowSetForegroundWindow(hWnd); // Slightly Higher PrioritySetFocus(hWnd); // Sets Keyboard Focus To The WindowReSizeGLScene(width, height); // Set Up Our Perspective GL Screenif (!InitGL()) // Initialize Our Newly Created GL Window{KillGLWindow(); // Reset The DisplayMessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE; // Return FALSE}return TRUE; // Success}LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This WindowUINT uMsg, // Message For This WindowWPARAM wParam, // Additional Message InformationLPARAM lParam) // Additional Message Information{switch (uMsg) // Check For Windows Messages{case WM_ACTIVATE: // Watch For Window Activate Message{if (!HIWORD(wParam)) // Check Minimization State{active=TRUE; // Program Is Active}else{active=FALSE; // Program Is No Longer Active}return 0; // Return To The Message Loop}case WM_SYSCOMMAND: // Intercept System Commands{switch (wParam) // Check System Calls{case SC_SCREENSAVE: // Screensaver Trying To Start?case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?return 0; // Prevent From Happening}break; // Exit}case WM_CLOSE: // Did We Receive A Close Message?{PostQuitMessage(0); // Send A Quit Messagereturn 0; // Jump Back}case WM_KEYDOWN: // Is A Key Being Held Down?{keys[wParam] = TRUE; // If So, Mark It As TRUEreturn 0; // Jump Back}case WM_KEYUP: // Has A Key Been Released?{keys[wParam] = FALSE; // If So, Mark It As FALSEreturn 0; // Jump Back}case WM_SIZE: // Resize The OpenGL Window{ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Heightreturn 0; // Jump Back}}// Pass All Unhandled Messages To DefWindowProcreturn DefWindowProc(hWnd,uMsg,wParam,lParam);}int WINAPI WinMain( HINSTANCE hInstance, // InstanceHINSTANCE hPrevInstance, // Previous InstanceLPSTR lpCmdLine, // Command Line Parametersint nCmdShow) // Window Show State{MSG msg; // Windows Message StructureBOOL done=FALSE; // Bool Variable To Exit Loop// Ask The User Which Screen Mode They Preferif (MessageBox(NULL,"àúä îòåðééï áîñê îìà?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO){fullscreen=FALSE; // Windowed Mode}// Create Our OpenGL Windowif (!CreateGLWindow("yoni first program",640,480,16,fullscreen)){return 0; // Quit If Window Was Not Created}while(!done) // Loop That Runs While done=FALSE{if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?{if (msg.message==WM_QUIT) // Have We Received A Quit Message?{done=TRUE; // If So done=TRUE}else // If Not, Deal With Window Messages{TranslateMessage(&msg); // Translate The MessageDispatchMessage(&msg); // Dispatch The Message}}else // If There Are No Messages{// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()if (active) // Program Active?{if (keys[VK_ESCAPE]) // Was ESC Pressed?{done=TRUE; // ESC Signalled A Quit}else // Not Time To Quit, Update Screen{DrawGLScene(); // Draw The SceneSwapBuffers(hDC); // Swap Buffers (Double Buffering)}}if (keys['B'] && !bp) // Is B Key Pressed And bp FALSE? { bp=TRUE; // If So, bp Becomes TRUE blend = !blend; // Toggle blend TRUE / FALSE if(blend) // Is blend TRUE? { glEnable(GL_BLEND); // Turn Blending On glDisable(GL_DEPTH_TEST); // Turn Depth Testing Off } else // Otherwise { glDisable(GL_BLEND); // Turn Blending Off glEnable(GL_DEPTH_TEST); // Turn Depth Testing On } } if (!keys['B']) // Has B Key Been Released? { bp=FALSE; // If So, bp Becomes FALSE }if (keys['A']){lag = -1;}if (keys['D']){lag = 1;}//if (keys['W'])//{//a=a+0.1f;//}//if (keys['Z'])//{//z = z+0.1f;//}/*if (keys['K']){k = k+0.1;}if (keys['J']){k = k-0.1f;}*/if (keys['W']){noob = 1;}if (keys['N']){rota = 1;}if (keys['M']){rota = -1;}if (keys['S']){noob = -1;}if (keys[VK_F1]) // Is F1 Being Pressed?{keys[VK_F1]=FALSE; // If So Make Key FALSEKillGLWindow(); // Kill Our Current Windowfullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode// Recreate Our OpenGL Windowif (!CreateGLWindow("yoni's program",640,480,16,fullscreen)){return 0; // Quit If Window Was Not Created}}}}// ShutdownKillGLWindow(); // Kill The Windowreturn (msg.wParam); // Exit The Program}WindowProc(HWND hWnd2, UINT uMsg, WPARAM wParam, LPARAM lParam){ static PAINTSTRUCT ps; static GLboolean left = GL_FALSE; /* left button currently down? */ static GLboolean right = GL_FALSE; /* right button currently down? */ static GLuint state = 0; /* mouse state flag */ static int omx, omy, mx, my; switch(uMsg) { case WM_MOUSEMOVE: // get change in mouse position delta_x += (window_width / 2) - LOWORD(lParam); delta_y += (window_height / 2) - HIWORD(lParam); // reset mouse position RECT my_rect; GetWindowRect(hWnd, my_rect); SetCursorPos(my_rect.left + (window_width / 2), my_rect.top + (window_height / 2));break;}
//EDIT: Added source tags. You can learn how to use them in the FAQ. - Kaz.
[Edited by - Kazgoroth on September 17, 2005 12:30:21 PM]
The problem is on line 27 and 28. You currently have stated:
However, those are function calls, so they need to be inside of a function and not free floating like you have them now. Try and figure out where you meant to put them and move them there first.
After that you need to take a look at your declaration of the function:
WindowProc(HWND hWnd2, UINT uMsg, WPARAM wParam, LPARAM lParam)
When you use a WindoProc function, it must have a return type and be a callback:
LRESULT CALLBACK WindowProc(HWND hWnd2, UINT uMsg, WPARAM wParam, LPARAM lParam).
Lastly, you do not have a global variable for window_height and window_width, so they are undefined in the last function. You should make a global variable at the top and set them after you create your window.
GetWindowRect(hWnd, my_rect);SetCursorPos(my_rect.left + (window_width / 2), my_rect.top + (window_height / 2));
However, those are function calls, so they need to be inside of a function and not free floating like you have them now. Try and figure out where you meant to put them and move them there first.
After that you need to take a look at your declaration of the function:
WindowProc(HWND hWnd2, UINT uMsg, WPARAM wParam, LPARAM lParam)
When you use a WindoProc function, it must have a return type and be a callback:
LRESULT CALLBACK WindowProc(HWND hWnd2, UINT uMsg, WPARAM wParam, LPARAM lParam).
Lastly, you do not have a global variable for window_height and window_width, so they are undefined in the last function. You should make a global variable at the top and set them after you create your window.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement