Help with problem on keyboard input

Started by
19 comments, last by Watever 16 years ago
can someone plz help me with my code.....im still a noob to opengl so dont call me an idiot plz.... ok i am trying to make a little game where an enemy shoots at you and you dodge the bullets until the end of a level.... except when i try to make the player move the enemy moves too!! and that wouldnt work... so can someone plz help solve this problem??? and i dont know how to make the enemy shoot at the player here is code...

#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <gl.h>
#include <glu.h>    //OpenGL Extension Header File
#include <glaux.h>  //OpenGL Image and Texture Code

HGLRC hRC = NULL;   //Rendering Context
HDC hDC = NULL;     //Device Context Connects to the Graphic Interface
HWND hWnd = NULL;   //Handles Everything
HINSTANCE hInstance; //Actually Creates Everything Visibally

bool keys[256];         //Used to Handle KeyBoard Activity
bool active = true;     //is the gl active?
bool fullscreen = true; //is the screen full?

bool masking=true;
bool scene;
bool mp;
bool sp;

GLuint texture[9];
GLuint loop;

int level = 1;
int dlevel = level;

GLfloat roll;

struct		object								// Create A Structure For Our Player
{
    float	fx, fy,fz,spin;
    float	x, y,z;
    char name;
    int life;
    int lives;
    bool bs;
    bool right;
    bool left;
};

struct	object	player;
struct	object	enemy[8];
struct	object	bullet;
struct  object  boss;

LRESULT CALLBACK WndProc(HWND , UINT , WPARAM , LPARAM);

AUX_RGBImageRec *LoadBMP(char *Filename)
{
    FILE *File=NULL;
    if (!Filename)
    {
        return NULL;
    }
    File=fopen(Filename,"r");
    if (File)
    {
        fclose(File);
        return auxDIBImageLoad(Filename);
    }
    return NULL;
}

int LoadGLTextures()
{
    int Status=false;
    AUX_RGBImageRec *TextureImage[9];
    memset(TextureImage,0,sizeof(void *)*9);

    if ((TextureImage[0] = LoadBMP("Data/mask2.bmp")) &&
            (TextureImage[1] = LoadBMP("Data/mask1.bmp")) &&
            (TextureImage[2] = LoadBMP("Data/image1.bmp")) &&
            (TextureImage[3] = LoadBMP("Data/Back.bmp")) &&
            (TextureImage[4] = LoadBMP("Data/image2.bmp")) &&
            (TextureImage[5] = LoadBMP("Data/enemy.bmp")) &&
            (TextureImage[6] = LoadBMP("Data/eForce.bmp")) &&
            (TextureImage[7] = LoadBMP("Data/eBullet.bmp")) &&
            (TextureImage[8] = LoadBMP("Data/mask3.bmp")))
    {
        Status = true;
        glGenTextures(9,&texture[0]);

        for (loop=0;loop<9;loop++)
        {
            glBindTexture(GL_TEXTURE_2D,texture[loop]);
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
            glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage[loop]->sizeX,TextureImage[loop]->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[loop]->data);
        }
    }
    for (loop=0;loop<9;loop++)
    {
        if (TextureImage[loop])
        {
            if (TextureImage[loop]->data)
            {
                free(TextureImage[loop]->data);
            }
            free(TextureImage[loop]);
        }
    }
    return Status;
}
/****************************
    RESIZING THE SCENE
****************************/

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
    if (height == 0) //height can never be 0
    {
        height = 1;
    }

    glViewport(0,0,width,height); //make window the size of width and height

    glMatrixMode(GL_PROJECTION); //select the projection matrix
    glLoadIdentity(); //reset the view

    //Set Perspective View
    gluPerspective(45.0f,(GLfloat) width / (GLfloat) height,0.1f,100.0f);
    /// 45.0f is starting Angle ; width,height converted to GLfloat ; d = 0.1f<x<100.0f

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

/****************************************
    INIT GL VARIABLE AND ENVIROMENT
****************************************/

int InitGL(GLvoid)
{
    if (!LoadGLTextures())
    {
        return false;
    }

    glClearColor(0.0f,0.0f,0.0f,1.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_TEXTURE_2D);

    player.x = -0.2f;
    player.y = -0.2f;
    player.z = 0.1f;

    player.fx = 0.1f;
    player.fy = 0.1f;
    player.fz = 0.1f;

    player.bs = false;

    for(int loop=0;loop<(level*dlevel);loop++)
    {
        enemy[loop].x = 0.5f;
        enemy[loop].y = 0.0f;
        enemy[loop].z = 0.1f;

        enemy[loop].fx = 0.0f;
        enemy[loop].fy = 0.0f;
        enemy[loop].fy = 0.0f;

        bullet.x = enemy[loop].x -= 0.2f;
        bullet.y = enemy[loop].y;
    }

    return true;
}

void DrawPlayer()
{
    glBlendFunc(GL_DST_COLOR,GL_ONE_MINUS_DST_COLOR);
    glTranslatef(player.x,player.y,player.z);
    glScalef(player.fx,player.fy,player.fz);

    if(player.left){
    glBindTexture(GL_TEXTURE_2D,texture[0]);}
    else{glBindTexture(GL_TEXTURE_2D,texture[8]);}
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f,0.0f);
    glVertex3f(-1.0f,-1.0f,0.0f);
    glTexCoord2f(+1.0f,0.0f);
    glVertex3f( 1.0f,-1.0f,0.0f);
    glTexCoord2f(1.0f,1.0f);
    glVertex3f( 1.0f, 1.0f,0.0f);
    glTexCoord2f(+0.0f,1.0f);
    glVertex3f(-1.0f, 1.0f,0.0f);
    glEnd();

    glPushMatrix();

}

void DrawLevel1()
{
    glTranslatef(0.0f,1.6f,-3.0f);

    glDisable(GL_DEPTH);

    //Background
    glBindTexture(GL_TEXTURE_2D,texture[2]);
    glBegin(GL_QUADS);
    glTexCoord2f(-roll+0.0f,0.0f);
    glVertex3f(-1.1f,-1.1f,0.0f);
    glTexCoord2f(-roll+3.0f,0.0f);
    glVertex3f( 1.1f,-1.1f,0.0f);
    glTexCoord2f(-roll+3.0f,3.0f);
    glVertex3f( 1.1f, 1.1f,0.0f);
    glTexCoord2f(-roll+0.0f,3.0f);
    glVertex3f(-1.1f, 1.1f,0.0f);
    glEnd();

    glTranslatef(0.0f,-0.4f,0.0f);
    glBindTexture(GL_TEXTURE_2D,texture[4]);
    glBegin(GL_QUADS);
    glTexCoord2f(-roll+0.0f,0.0f);
    glVertex3f(-1.1f,-1.1f,0.0f);
    glTexCoord2f(-roll+3.0f,0.0f);
    glVertex3f( 1.1f,-1.1f,0.0f);
    glTexCoord2f(-roll+3.0f,3.0f);
    glVertex3f( 1.1f, 1.1f,0.0f);
    glTexCoord2f(-roll+0.0f,3.0f);
    glVertex3f(-1.1f, 1.1f,0.0f);
    glEnd();

    glTranslatef(0.0f,-0.5f,0.0f);
    glBindTexture(GL_TEXTURE_2D,texture[3]);
    glBegin(GL_QUADS);
    glTexCoord2f(-roll+0.0f,0.0f);
    glVertex3f(-1.1f,-1.1f,0.0f);
    glTexCoord2f(-roll+3.0f,0.0f);
    glVertex3f( 1.1f,-1.1f,0.0f);
    glTexCoord2f(-roll+3.0f,3.0f);
    glVertex3f( 1.1f, 1.1f,0.0f);
    glTexCoord2f(-roll+0.0f,3.0f);
    glVertex3f(-1.1f, 1.1f,0.0f);
    glEnd();

    glTranslatef(0.0f,-0.4f,0.0f);
    glBindTexture(GL_TEXTURE_2D,texture[1]);
    glBegin(GL_QUADS);
    glTexCoord2f(-roll+0.0f,0.0f);
    glVertex3f(-1.1f,-1.1f,0.0f);
    glTexCoord2f(-roll+3.0f,0.0f);
    glVertex3f( 1.1f,-1.1f,0.0f);
    glTexCoord2f(-roll+3.0f,3.0f);
    glVertex3f( 1.1f, 1.1f,0.0f);
    glTexCoord2f(-roll+0.0f,3.0f);
    glVertex3f(-1.1f, 1.1f,0.0f);
    glEnd();

    glEnable(GL_DEPTH);
}

void EnemyShoot()
{
    for(int w=0;w<(dlevel*level);w++)
    {
        glTranslatef(bullet.x,enemy[w].y,0.0f);
        glScalef(0.1f,0.1f,0.1f);
        glBegin(GL_QUADS);
            glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f,-1.0f,0.0f);
            glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0f,-1.0f,0.0f);
            glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0f, 1.0f,0.0f);
            glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f, 1.0f,0.0f);
        glEnd();
        glScalef(1.0f,1.0f,1.0f);

        bullet.x -= 0.5f;
    }
}
/**************************
    DRAWING CODE IN GL
***************************/

int DrawGLScene(GLvoid)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear screen color
    glLoadIdentity();

    DrawLevel1();
    DrawPlayer();

    glEnable(GL_BLEND);
    for (int loop=0; loop<(dlevel*level); loop++)				// Loop To Draw Enemies
	{
        glTranslatef(enemy[loop].x,enemy[loop].y,enemy[loop].z);

		glColor3f(1.0f,1.0f,1.0f);					// Make Enemy Body Pink
		glBindTexture(GL_TEXTURE_2D,texture[5]);
		glBegin(GL_QUADS);
            glTexCoord2f(0.0f,0.0f); glVertex3f(-1.1f,-1.1f,0.0f);
            glTexCoord2f(1.0f,0.0f); glVertex3f( 1.1f,-1.1f,0.0f);
            glTexCoord2f(1.0f,1.0f); glVertex3f( 1.1f, 1.1f,0.0f);
            glTexCoord2f(0.0f,1.0f); glVertex3f(-1.1f, 1.1f,0.0f);
		glEnd();							// Done Drawing Enemy Blade

		if(player.x >= 0.0f || player.y > 0.0f)
        {
             if((enemy[loop].x<player.x) && (enemy[loop].y == player.y))
                {
                    enemy[loop].x+= 1.0f;
                    EnemyShoot();
                }
                if((enemy[loop].x>player.x) && (enemy[loop].y == player.y))
                {
                    enemy[loop].x-= 1.0f;
                    EnemyShoot();
                }
                if((enemy[loop].y<player.y) && (enemy[loop].x == player.x))
                {
                    enemy[loop].y+= 1.0f;
                    EnemyShoot();
                }
                if((enemy[loop].y>player.y) && (enemy[loop].x == player.x))
                {
                    enemy[loop].y-= 1.0f;
                    EnemyShoot();
                }
                if((enemy[loop].x == player.x) && (enemy[loop].y == player.y))
                {
                    player.lives--;
                }
        }

        if(player.x<= -0.8f)
        {
            player.x+=0.1f;
        }
        if(player.x>= 0.8f)
        {
            player.x-=0.1f;
        }
        if(player.y<= -0.5f)
        {
            player.y+=0.1f;
        }
        if(player.y>= 0.08f)
        {
            player.y-=0.1f;
        }

	}

	glPopMatrix();

    glDisable(GL_BLEND);

    roll+=0.01f;
    if (roll>1.0f)
    {
        roll-=1.0f;
    }

    return true;
}

/*******************************
    PROPERLY CLOSE THE WINDOW
*******************************/

void KeyBoard()
{
    if (keys[VK_DOWN]){player.y-= 0.1f;}
    if (keys[VK_UP]){player.y+= 0.1f;}
    if (keys[VK_LEFT]){player.x-= 0.1f; player.left = true; player.right = false;}
    if (keys[VK_RIGHT]){player.x+= 0.1f; player.left = false; player.right = true;}
    for(int loop=0;loop<(dlevel*level);loop++){
    if (keys['W'])enemy[loop].y+=0.1f;
    if (keys['S'])enemy[loop].y-=0.1f;
    if (keys['A'])enemy[loop].x-=0.1f;
    if (keys['D'])enemy[loop].x+=0.1f;}
}

GLvoid KillGLWindow(GLvoid)
{
    if (fullscreen) //is the screen in fullscreen mode?
    {
        ChangeDisplaySettings(NULL,0); //go back to desktop if this not used then comp become corrupt and have to restart
        ShowCursor(true);
    }

    if (hRC) //is rendering context on?
    {
        if (!wglMakeCurrent(NULL,NULL)) //are we able to release it from device context(graphic interface)?
        {
            MessageBox(NULL,"Couldnt Release DC and RC!!","ERROR",MB_OK | MB_ICONEXCLAMATION);
        }

        if (!wglDeleteContext(hRC)) //can we release RC?
        {
            MessageBox(NULL,"Couldnt Release RC","ERROR",MB_OK | MB_ICONEXCLAMATION);
        }
        hRC = NULL;
    }

    if (hDC && !ReleaseDC(hWnd,hDC)) //can we release DC?
    {
        MessageBox(NULL,"Device Context Dont Work!!","ERROR",MB_OK | MB_ICONSTOP);
        hDC = NULL;
    }

    if (hWnd && !DestroyWindow(hWnd)) //can we destroy window?
    {
        MessageBox(NULL,"Couldnt Kill Window!!","ERROR", MB_OK | MB_ICONEXCLAMATION);
        hWnd = NULL;
    }

    if (!UnregisterClass("OpenGL",hInstance))
    {
        MessageBox(NULL,"Couldnt Unregister Class","ERROR",MB_OK | MB_ICONSTOP);
        hInstance = NULL;
    }
}

/****************************************
            CREATE THE WINDOW
***************************************/

bool CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
    GLuint		PixelFormat;			// Holds The Results After Searching For A Match
    WNDCLASS	wc;						// Windows Class Structure
    DWORD		dwExstyle;				// Window Extended style
    DWORD		dwstyle;				// Window style
    RECT		WindowRect;				// Grabs Rectangle Upper Left / Lower Right Values
    WindowRect.left=(long)0;			// Set Left Value To 0
    WindowRect.right=(long)width;		// Set Right Value To Requested Width
    WindowRect.top=(long)0;				// Set Top Value To 0
    WindowRect.bottom=(long)height;		// Set Bottom Value To Requested Height

    fullscreen=fullscreenflag;			// Set The Global Fullscreen Flag

    hInstance			= GetModuleHandle(NULL);				// Grab An Instance For Our Window
    wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw On Size, And Own DC For Window.
    wc.lpfnWndProc		= (WNDPROC) WndProc;					// WndProc Handles Messages
    wc.cbClsExtra		= 0;									// No Extra Window Data
    wc.cbWndExtra		= 0;									// No Extra Window Data
    wc.hInstance		= hInstance;							// Set The Instance
    wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);			// Load The Default Icon
    wc.hCursor			= LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer
    wc.hbrBackground	= NULL;									// No Background Required For GL
    wc.lpszMenuName		= NULL;									// We Don't Want A Menu
    wc.lpszClassName	= "OpenGL";								// Set The Class Name

    if (!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 Mode
        memset(&dmScreenSettings,0,sizeof(dmScreenSettings));	// Makes Sure Memory's Cleared
        dmScreenSettings.dmSize=sizeof(dmScreenSettings);		// Size Of The Devmode Structure
        dmScreenSettings.dmPelsWidth	= width;				// Selected Screen Width
        dmScreenSettings.dmPelsHeight	= height;				// Selected Screen Height
        dmScreenSettings.dmBitsPerPel	= bits;					// Selected Bits Per Pixel
        dmScreenSettings.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 style
        dwstyle=WS_POPUP;										// Windows style
        ShowCursor(FALSE);										// Hide Mouse Pointer
    }
    else
    {
        dwExstyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			// Window Extended style
        dwstyle=WS_OVERLAPPEDWINDOW;							// Windows style
    }

    AdjustWindowRectEx(&WindowRect, dwstyle, FALSE, dwExstyle);		// Adjust Window To True Requested Size

    // Create The Window
    if (!(hWnd=CreateWindowEx(	dwExstyle,							// Extended style For The Window
                               "OpenGL",							// Class Name
                               title,								// Window Title
                               dwstyle |							// Defined Window style
                               WS_CLIPSIBLINGS |					// Required Window style
                               WS_CLIPCHILDREN,					// Required Window style
                               0, 0,								// Window Position
                               WindowRect.right-WindowRect.left,	// Calculate Window Width
                               WindowRect.bottom-WindowRect.top,	// Calculate Window Height
                               NULL,								// No Parent Window
                               NULL,								// No Menu
                               hInstance,							// Instance
                               NULL)))								// Dont Pass Anything To WM_CREATE
    {
        KillGLWindow();								// Reset The Display
        MessageBox(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 Descriptor
        1,											// Version Number
        PFD_DRAW_TO_WINDOW |						// Format Must Support Window
        PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
        PFD_DOUBLEBUFFER,							// Must Support Double Buffering
        PFD_TYPE_RGBA,								// Request An RGBA Format
        bits,										// Select Our Color Depth
        0, 0, 0, 0, 0, 0,							// Color Bits Ignored
        0,											// No Alpha Buffer
        0,											// Shift Bit Ignored
        0,											// No Accumulation Buffer
        0, 0, 0, 0,									// Accumulation Bits Ignored
        16,											// 16Bit Z-Buffer (Depth Buffer)
        0,											// No Stencil Buffer
        0,											// No Auxiliary Buffer
        PFD_MAIN_PLANE,								// Main Drawing Layer
        0,											// Reserved
        0, 0, 0										// Layer Masks Ignored
    };

    if (!(hDC=GetDC(hWnd)))							// Did We Get A Device Context?
    {
        KillGLWindow();								// Reset The Display
        MessageBox(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 Display
        MessageBox(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 Display
        MessageBox(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 Display
        MessageBox(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 Display
        MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return FALSE;								// Return FALSE
    }

    ShowWindow(hWnd,SW_SHOW);						// Show The Window
    SetForegroundWindow(hWnd);						// Slightly Higher Priority
    SetFocus(hWnd);									// Sets Keyboard Focus To The Window
    ReSizeGLScene(width, height);					// Set Up Our Perspective GL Screen

    if (!InitGL())									// Initialize Our Newly Created GL Window
    {
        KillGLWindow();								// Reset The Display
        MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return FALSE;								// Return FALSE
    }

    return TRUE;									// Success
}

LRESULT CALLBACK WndProc(	HWND	hWnd,			// Handle For This Window
                          UINT	uMsg,			// Message For This Window
                          WPARAM	wParam,			// Additional Message Information
                          LPARAM	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 Message
        return 0;								// Jump Back
    }

    case WM_KEYDOWN:							// Is A Key Being Held Down?
    {
        keys[wParam] = TRUE;					// If So, Mark It As TRUE
        return 0;								// Jump Back
    }

    case WM_KEYUP:								// Has A Key Been Released?
    {
        keys[wParam] = FALSE;					// If So, Mark It As FALSE
        return 0;								// Jump Back
    }

    case WM_SIZE:								// Resize The OpenGL Window
    {
        ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
        return 0;								// Jump Back
    }
    }

    // Pass All Unhandled Messages To DefWindowProc
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain(	HINSTANCE	hInstance,			// Instance
                    HINSTANCE	hPrevInstance,		// Previous Instance
                    LPSTR		lpCmdLine,			// Command Line Parameters
                    int			nCmdShow)			// Window Show State
{
    MSG		msg;									// Windows Message Structure
    BOOL	done=FALSE;								// Bool Variable To Exit Loop

    // Ask The User Which Screen Mode They Prefer
    if (MessageBox(NULL,"Do you want fullscreen?", "Computer Virus",MB_YESNO|MB_ICONQUESTION)==IDNO)
    {
        fullscreen=FALSE;							// Windowed Mode
    }

    // Create Our OpenGL Window
    if (!CreateGLWindow("Watevers OpenGL",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 Message
                DispatchMessage(&msg);				// Dispatch The Message
            }
        }
        else										// If There Are No Messages
        {
            // Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
            if ((active && !DrawGLScene()) || keys[VK_ESCAPE])	// Active?  Was There A Quit Received?
            {
                done=TRUE;							// ESC or DrawGLScene Signalled A Quit
            }
            else									// Not Time To Quit, Update Screen
            {
                SwapBuffers(hDC);					// Swap Buffers (Double Buffering)
                KeyBoard();
                if (keys[VK_F1])
                    // Is F1 Being Pressed?
                {
                    keys[VK_F1]=FALSE;					// If So Make Key FALSE
                    KillGLWindow();						// Kill Our Current Window
                    fullscreen=!fullscreen;				// Toggle Fullscreen / Windowed Mode

                    // Recreate Our OpenGL Window
                    if (!CreateGLWindow("Watevers OpenGL",640,480,16,fullscreen))
                    {
                        return 0;						// Quit If Window Was Not Created
                    }
                }
            }
        }
    }

    // Shutdown
    KillGLWindow();									// Kill The Window
    return (msg.wParam);							// Exit The Program
}

Advertisement
As far as I can see your enemy movement problem isn't input, but display.

Try using either glPushMatrix/glPopMatrix or adding glLoadIdentity(); at the beginning of DrawLevel1(), DrawPlayer(), EnemyShoot() and the beginning of your enemy draw loop, just before the glTranslatef. Otherwise the translations will accumulate.
blah :)
ok now i cant see the player or the enemy???
i tried to edit the player position to make it visible but it didnt work...what now?

ima noob sorry.....
in function "int DrawGLScene(GLvoid)" you move the enemies towards the player's position with a constant every frame.
Make an UpdateAI function instead to manage your enemies, and don't forget to multiply the movement speed with previous frame elapsed time to make them move FPS independent.

And a tip is to separate the movement and firing mechanism of the AI to make it easier to manage.

Quote:
if((enemy[loop].x<player.x) && (enemy[loop].y == player.y)){    enemy[loop].x+= 1.0f;    EnemyShoot();}if((enemy[loop].x>player.x) && (enemy[loop].y == player.y)){    enemy[loop].x-= 1.0f;    EnemyShoot();}if((enemy[loop].y<player.y) && (enemy[loop].x == player.x)){    enemy[loop].y+= 1.0f;    EnemyShoot();}if((enemy[loop].y>player.y) && (enemy[loop].x == player.x)){    enemy[loop].y-= 1.0f;    EnemyShoot();}if((enemy[loop].x == player.x) && (enemy[loop].y == player.y)){    player.lives--;}


[Edited by - Molle85 on April 2, 2008 4:44:13 PM]
ok i get it....
i still cant see the player or the enemy....because of the glLoadIdentity i added to DrawLevel1 and DrawPlayer.........
I don't use OpenGL myself so i can't help you there, but if your draw calls worked before then revert the code as you had it before
ok but now the enemy moves with the players movement again....
and if i put glLoadIdentity() at the beginning of DrawPlayer() the player and the enemy doesnt draw or if i put glLoadIdentity() at the end then the enemy doesnt show up......
add glLoadIdentity() before the translation in the loop that draws the enemies
Quote:Original post by kolrabi
As far as I can see your enemy movement problem isn't input, but display.

Try using either glPushMatrix/glPopMatrix or adding glLoadIdentity(); at the beginning of DrawLevel1(), DrawPlayer(), EnemyShoot() and the beginning of your enemy draw loop, just before the glTranslatef. Otherwise the translations will accumulate.


He has the right idea...

glPushMatrix();
DrawPlayer();
glPopMatrix();


glPushMatrix();
DrawLevel1();
glPopMatrix();

a.s.o. for every draw call
so DrawLevel1 should look this....

<code>
void DrawLevel1()
{
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0f,1.6f,-3.0f);

glDisable(GL_DEPTH);

//Background
glBindTexture(GL_TEXTURE_2D,texture[2]);
glBegin(GL_QUADS);
glTexCoord2f(-roll+0.0f,0.0f);
glVertex3f(-1.1f,-1.1f,0.0f);
glTexCoord2f(-roll+3.0f,0.0f);
glVertex3f( 1.1f,-1.1f,0.0f);
glTexCoord2f(-roll+3.0f,3.0f);
glVertex3f( 1.1f, 1.1f,0.0f);
glTexCoord2f(-roll+0.0f,3.0f);
glVertex3f(-1.1f, 1.1f,0.0f);
glEnd();

glTranslatef(0.0f,-0.4f,0.0f);
glBindTexture(GL_TEXTURE_2D,texture[4]);
glBegin(GL_QUADS);
glTexCoord2f(-roll+0.0f,0.0f);
glVertex3f(-1.1f,-1.1f,0.0f);
glTexCoord2f(-roll+3.0f,0.0f);
glVertex3f( 1.1f,-1.1f,0.0f);
glTexCoord2f(-roll+3.0f,3.0f);
glVertex3f( 1.1f, 1.1f,0.0f);
glTexCoord2f(-roll+0.0f,3.0f);
glVertex3f(-1.1f, 1.1f,0.0f);
glEnd();

glTranslatef(0.0f,-0.5f,0.0f);
glBindTexture(GL_TEXTURE_2D,texture[3]);
glBegin(GL_QUADS);
glTexCoord2f(-roll+0.0f,0.0f);
glVertex3f(-1.1f,-1.1f,0.0f);
glTexCoord2f(-roll+3.0f,0.0f);
glVertex3f( 1.1f,-1.1f,0.0f);
glTexCoord2f(-roll+3.0f,3.0f);
glVertex3f( 1.1f, 1.1f,0.0f);
glTexCoord2f(-roll+0.0f,3.0f);
glVertex3f(-1.1f, 1.1f,0.0f);
glEnd();

glTranslatef(0.0f,-0.4f,0.0f);
glBindTexture(GL_TEXTURE_2D,texture[1]);
glBegin(GL_QUADS);
glTexCoord2f(-roll+0.0f,0.0f);
glVertex3f(-1.1f,-1.1f,0.0f);
glTexCoord2f(-roll+3.0f,0.0f);
glVertex3f( 1.1f,-1.1f,0.0f);
glTexCoord2f(-roll+3.0f,3.0f);
glVertex3f( 1.1f, 1.1f,0.0f);
glTexCoord2f(-roll+0.0f,3.0f);
glVertex3f(-1.1f, 1.1f,0.0f);
glEnd();

glEnable(GL_DEPTH);
glPopMatrix();
}
</code>

This topic is closed to new replies.

Advertisement