I did everything that is required but I can't make the code work

I did everything that is required but I can't make the code work.
Here is the requirements:

In the windows main.cpp (in the while loop of Win Main and right before the first if statement), add something similar to the following to detect a right arrow key press:

if (KEY_DOWN(VK_RIGHT)) {



ObjVelX =. . .; //Add you own code to update the object’s horizontal velocity
//so that when the right key is pressed the velocity increases
//i.e. it goes faster and faster
}


Add similar code for the left arrow key.

Add the following code after the ShowWindow in WinMain to remove keyboard focus so the arrow keys will not control the slider controls:
SetFocus(NULL);



1
2
3
4
5
6
7
8
//Here is what I got for that in the main.cpp//

if (KEY_DOWN(VK_RIGHT))  
{ 
ObjVelX += (50*20/1000);} 
if (KEY_DOWN(VK_LEFT)) 
{  
ObjVelX += (50*20/1000);}


Here is step 2: Create a timer event
The program should calculate the new position of the object for the subsequent frames and update the OpenGL output. Use the windows timer so that each frame is about 20/1000= 1/50th of a second.

A windows timer can be created in WinMain just before the while message loop using the SetTimer function:
SetTimer(HDialog, 1, 20, NULL); //20 milliseconds


This will generate a WM_TIMER message that can be handled by the WndProc function. Each time the timer goes off, update the object's position.
Add a case for the timer event in the DialogProc() function, in main.cpp:



case WM_TIMER:

ObjPosX =. . . //add code to position based on formula
//xnew = xold + dt*velocity
//Be sure to use the same time as set up in SetTimer:
//dt=20.0/1000.0

break;



1
2
3
4
5
6
7
8
9
10
///Here is what I have for that for the main.cpp///

case WM_TIMER: 
 
ObjPosX = ObjPosX + 20.0/1000.0 * ObjVelX; 
char str[32]; 
sprintf(str, "%s", <%.2f %.2f %.2f>", "Name goes here", ObjPosX, 0.0F, 0.0F); 
SetWindowText(HDialog, str); 
SetFocus(NULL); 
break; 


Here is the last step in the CGfxOpenGL.cpp :

Modify the sphere’s glTranslatef function in CGfxOpenGL.cpp to draw the single sphere in the proper new (updated) position
For example,
glTranslatef(ObjPosX, 0, 0);[/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Here is the main.cpp part of the code with arrow keys//

 while (!Exiting) 
    { 
        // render opengl stuff 
                g_glRender->Prepare(0.0f); 
        g_glRender->Render(); 
        SwapBuffers(hDC); 
 
 
                if (KEY_DOWN(VK_RIGHT))  ////step1//
                { 
                ObjVelX += (50*20/1000);}        
 
                        if (KEY_DOWN(VK_LEFT))  
                        { 
                                ObjVelX += (50*20/1000);} 
                         
                                 
         
                 
        // handle window message stuff 
        if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) 
        { 
            if (!GetMessage (&msg, NULL, 0, 0)) 
            { 
                Exiting = true; 
                break; 
            } 
 
                        // avoid processing messages for the dialog 
                        if (!IsDialogMessage (HDialog, & msg)) 
                        { 
                                TranslateMessage ( & msg ); 
                                DispatchMessage ( & msg ); 
                        } 
        } 
 
 
         
        } 
 
    return (int)(msg.wParam);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Here is the main.cpp with timer event//

case WM_TIMER: /////Problem #2 
 
                 
                ObjPosX = ObjPosX + 20.0/1000.0 * ObjVelX; 
                char str[32]; 
                sprintf(str, "%s", <%.2f %.2f %.2f>", "Name goes here", ObjPosX, 0.0F, 0.0F); 
                SetWindowText(HDialog, str); 
                SetFocus(NULL); 
                break; 
     
        case WM_DESTROY:            // window destroy 
    case WM_QUIT: 
    case WM_CLOSE:                  // windows is closing 
 
        // deselect rendering context and delete it 
        wglMakeCurrent(hDC, NULL); 
        wglDeleteContext(hRC); 
 
        // send WM_QUIT to message queue 
        PostQuitMessage(0); 
        break;  
1
2
3
4
5
6
7
8
9
10
11
12
//Here is the CGfxOpenGL.cpp for teh last step//

// undo the previous floor transform 
     
        glTranslatef(ObjPosX, 0, 0);////last step//////////////////////////////////////// 
 
    // draw some spheres 
         
         
        glColor3f(1, 0, 0);                 //color blue 
        gluSphere(quadric, 1.0f, 20, 20);  // has radius 1 
        
Topic archived. No new replies allowed.