Hi All!

I'm try to make a game in C++ with OpenGl. So far i've programmed the sprite and
the platform. the next thing is to create a AI bot who will try to kill you in the game.

but I've got the most bisarr problem with that: The AIBot class are making a
holy random big fat square in the platform despite I never programmed it to do so.

/**************************
 * Includes
 *
 **************************/

#include <windows.h>
#include <gl/gl.h>








/**************************
 * Function Declarations
 *
 **************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);


void drawSquare(float posx, float posy, float w, float h){
     glPushMatrix();
     glBegin(GL_POLYGON);
     
     glColor3f(1.0f, 0.0f, 0.0f);glVertex3f(posx, posy, 0.0f);
     glColor3f(0.0f, 1.0f, 0.0f);glVertex3f(posx+w, posy, 0.0f);
     glColor3f(0.0f, 0.0f, 1.0f);glVertex3f(posx+w, posy+h, 0.0f);
     glColor3f(1.0f, 0.0f, 1.0f);glVertex3f(posx, posy+h, 0.0f);
     
     glEnd();
     glPopMatrix();    
}








class Area{
      public:
             float x, y, w, h;
             Area(float Xx, float Yy, float Ww, float Hh){
                        x = Xx;
                        y = Yy;
                        w = Ww;
                        h = Hh;
             }
             
             bool isInSide(float mx, float my){
                  return (mx > x && my > y && mx < x+w && my < y+h);
             }
};


Area rutor[]={
     Area(-1.0f, -1.0f, 2.0f, 0.1f),
     Area(-1.0f, -1.0f, 0.1f, 2.0f),
     Area(0.9f, -1.0f, 0.1f, 2.0f),
     Area(-0.1f, -0.9f, 0.3f, 0.2f),
     Area(-0.4f, -0.499f, 0.3f, 0.2f),
     Area(-0.7f, -0.101f, 0.3f, 0.2f),
     Area(-0.1f, -0.101f, 0.3f, 0.2f),
     Area(0.2f, 0.3f, 0.3f, 0.2f)
};


float x=0.5f, y=0.5f, gravity = 0.000f;

bool left=false, right=false, jump=true, stay=true;

// here is the class that cause the problem
class comp{
      private:
              float robotl, roboty, gravity;
      public:
             comp(){
                    gravity=0.1f;
                    robotl=0.1f;
                    roboty=0.1f;
             }
             
};

comp Comp;

/**************************
 * WinMain
 *
 **************************/

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;

    /* register window class */
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

    /* create main window */
    hWnd = CreateWindow (
      "GLSample", "MyGame", 
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 800, 600,
      NULL, NULL, hInstance, NULL);

    /* enable OpenGL for the window */
    EnableOpenGL (hWnd, &hDC, &hRC);
    /* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else if (msg.message == WM_KEYDOWN){
                 if (msg.wParam == VK_UP){
                                if (jump){
                                          y+=0.01f;
                                          gravity = 0.01f;
                                          jump = false;
                                          stay = true;
                                }
                                
                 }else if (msg.wParam == VK_LEFT && jump){
                       left=true;
                       right=false;
                 }else if (msg.wParam == VK_RIGHT && jump) {
                       left=false;
                       right=true;
                 }
            }else if (msg.message == WM_KEYUP){
                  if (msg.wParam == VK_LEFT && jump){
                                 left=false;
                  }else if (msg.wParam == VK_RIGHT && jump){
                        right=false;
                  }
            }else{
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
            /* OpenGL animation code goes here */

            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            glClear (GL_COLOR_BUFFER_BIT);           
            
            for (int i=0; i < sizeof(rutor); i++){
                drawSquare(rutor[i].x, rutor[i].y, rutor[i].w, rutor[i].h);
                if (rutor[i].isInSide(x, y-0.1f)){
                                         gravity = 0.0f;
                                         jump=true;
                                         if (stay){
                                                   left=false;
                                                   right=false;
                                                   stay=false;
                                         }
                }
                
                if (rutor[i].isInSide(x-0.05f, y-0.05f)){
                                               x+=0.01f;
                                               left=false;
                                               right=false;
                                              
                }
                if (rutor[i].isInSide(x+0.05f, y-0.05f)){
                                               x-=0.01f;
                                               left=false;
                                               right=false;
                }
                
                
            }
            bool d=false;
            for (int i=0; i < sizeof(rutor); i++){
                if (rutor[i].isInSide(x, y-0.1)){
                                         y+=0.0000001;
                                          d=true;
                                          break;
                }
            }
            
            if (!d){
                       gravity-=0.0001f;
                       jump = false;
                       if (!stay && gravity<-0.0002f){
                                  stay = true;
                       }
            }
            
            
            y+=gravity;
            
            if (left){
                      x-=0.002f;
            }
            if (right){
                       x+=0.002f;
            }
            
            
            
            
            
            
            
            drawSquare(x-0.05f, y-0.1f, 0.1f, 0.2f);

            SwapBuffers (hDC);

            Sleep (3);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL (hWnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow (hWnd);

    return msg.wParam;
}


/********************
 * Window Procedure
 *
 ********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
               case VK_ESCAPE:
                    PostQuitMessage(0);
                    return 0;
        }
    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}


/*******************
 * Enable OpenGL
 *
 *******************/

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC (hWnd);

    /* set the pixel format for the DC */
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}


/******************
 * Disable OpenGL
 *
 ******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}

it seens like I can not store a X and Y float variable in the class comp.
If X and Y was an int, then the problem doesn't appears, but when X and Y
is float, then it draw that fat rectangle on the screen.
But, X and Y have to be float, because else I can't use the coordinates in OpenGL.

anyone with a solution? will be greatfull for answers.
if there were any bad english in this text, I'm sorry for that. I'm swedish.

Your problem is not related to the comp class or anything like that.

You have a memory corruption problem.

The problem is on the line 179 and 206:

for (int i=0; i < sizeof(rutor); i++){

The sizeof() operator returns the total size, in memory, of the variable or type that you give it, in this case "rutor". Because rutor is a static array of 8 Area objects, the value returned by sizeof(rutor) will by 8 * sizeof(Area) (8 times the size of an object of type Area). You are using the sizeof() operator as if it would return 8 in this case. In order to obtain the actual number of elements in a static array, you have to do:

for (int i=0; i < sizeof(rutor) / sizeof(Area); i++) {

The weird problems that you are getting is related to the fact that you write past the end of the array and overwrite other variables, causing weird stuff to happen. Replacing lines 179 and 206 with the above should solve your problems.

PS: Din engelska är inte dâligt als.

Edited 5 Years Ago by mike_2000_17: n/a

Your problem is not related to the comp class or anything like that.

You have a memory corruption problem.

The problem is on the line 179 and 206:

for (int i=0; i < sizeof(rutor); i++){

The sizeof() operator returns the total size, in memory, of the variable or type that you give it, in this case "rutor". Because rutor is a static array of 8 Area objects, the value returned by sizeof(rutor) will by 8 * sizeof(Area) (8 times the size of an object of type Area). You are using the sizeof() operator as if it would return 8 in this case. In order to obtain the actual number of elements in a static array, you have to do:

for (int i=0; i < sizeof(rutor) / sizeof(Area); i++) {

The weird problems that you are getting is related to the fact that you write past the end of the array and overwrite other variables, causing weird stuff to happen. Replacing lines 179 and 206 with the above should solve your problems.

PS: Din engelska är inte dâligt als.

thank you very much! :) I'm now done with the game. You can download it here

http://web.hjalmar.nu/~morgan/Projekt1.exe

This question has already been answered. Start a new discussion instead.