Class failure in game

This is a discussion on Class failure in game within the Game Programming forums, part of the General Programming Boards category; Alright, during the course of programming the game i am working on, I have come to a major problem. For ...

  1. #1
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109

    Exclamation Class failure in game

    Alright, during the course of programming the game i am working on, I have come to a major problem. For whatever reason, it is no longer allowing me to initialize classes. It acts like it has no idea what the class tag is. I'm pretty sure it is not the headers, becuase i have used classes with all the prebuilt headers that are in this game except one, windowsx.h but i tested it and that is not the problem.

    i have tried to create the class in both the header i made and in the main source, and both come up with the same message.


    main source:

    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include "sidescrolling.h"
    
    static char g_szClassName[] = "MyWindowClass";
    static HINSTANCE g_hInst = NULL;
    const UINT idTimer1 = 1;
    UINT nTimerDelay = 10;
    
    HBITMAP hbmPlayer, hbmPlayerMask, hbmTerrorist, hbmTerroristMask, hbmBullet, hbmBulletMask;
    BITMAP bm, bmT, bmBullet;
    int playerx, playery, terrorist1x, terrorist1y, bulletx, bullety;
    int playerhealth = 100;
    int terroristhealth = 50;
    int Jumping = 0;
    HWND hwnd, JumpThreadID, TerroristThreadID;
    
    void ErasePlayer(HDC hdc)
    {
       RECT rc;
       rc.left = playerx + 11;
       rc.top = playery;
       rc.right = playerx + bm.bmWidth - 12;
       rc.bottom = playery + bm.bmHeight + 1;
       FillRect(hdc, &rc, (HBRUSH)(COLOR_BTNFACE+1));
    }
    
    void DrawPlayer(HDC hdc)
    {
       HDC hdcMemory;
       hdcMemory = CreateCompatibleDC(hdc);
       SelectObject(hdcMemory, hbmPlayerMask);
       BitBlt(hdc, playerx, playery, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCAND);
       SelectObject(hdcMemory, hbmPlayer);
       BitBlt(hdc, playerx, playery, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCPAINT);
       DeleteDC(hdcMemory);
    }
    
    void UpdatePlayer(HWND hwnd)
    {
       RECT rc;
       GetClientRect(hwnd, &rc);
       if(playerx < 0){
          playerx = 0;
       }
       else if(playerx + bm.bmWidth > rc.right){
          playerx = rc.right - bm.bmWidth;
       }
       if(playery < 0){
          playery = 0;
       }
       else if(playery + bm.bmHeight > rc.bottom){
          playery = rc.bottom - bm.bmHeight;
       }
    }
    
    void EraseTerrorist(HDC hdc)
    {
       RECT rc;
       rc.left = terrorist1x + 10;
       rc.top = terrorist1y + 8;
       rc.right = terrorist1x + bm.bmWidth - 11;
       rc.bottom = terrorist1y + bm.bmHeight - 6;
       FillRect(hdc, &rc, (HBRUSH)(COLOR_BTNFACE+1));
    }
    
    void DrawTerrorist(HDC hdc)
    {
       HDC hdcMemory;
       hdcMemory = CreateCompatibleDC(hdc);
       SelectObject(hdcMemory, hbmTerroristMask);
       BitBlt(hdc, terrorist1x, terrorist1y, bmT.bmWidth, bmT.bmHeight, hdcMemory, 0, 0, SRCAND);
       SelectObject(hdcMemory, hbmTerrorist);
       BitBlt(hdc, terrorist1x, terrorist1y, bmT.bmWidth, bmT.bmHeight, hdcMemory, 0, 0, SRCPAINT);
       DeleteDC(hdcMemory);
    }
    
    void UpdateTerrorist(HWND hwnd)
    {
       RECT rc;
       GetClientRect(hwnd, &rc);
       if(terrorist1x < 0){
          terrorist1x = 0;
       }
       else if(terrorist1x + bmT.bmWidth > rc.right){
          terrorist1x = rc.right - bmT.bmWidth;
       }
       if(terrorist1y < 0){
          terrorist1y = 0;
       }
       else if(terrorist1y + bmT.bmHeight > rc.bottom){
          terrorist1y = rc.bottom - bmT.bmHeight;
       }
    }
    
    void PlayerJump() {
        int oldy = playery;
        HDC hdcWindow = GetDC(hwnd);
        while(playery > oldy - 80) {
            playery -= 8;
            ErasePlayer(hdcWindow);
            UpdatePlayer(hwnd);
            DrawPlayer(hdcWindow);
            Sleep(16);
        }
        while(playery < oldy) {
            playery += 8;
            ErasePlayer(hdcWindow);
            UpdatePlayer(hwnd);
            DrawPlayer(hdcWindow);
            Sleep(8);
        }
        Jumping = 0;
        ReleaseDC(hwnd, hdcWindow);
        TerminateThread(JumpThreadID, 0);
        return;
    }
    
    void LoadTerrorist() {
        hbmTerrorist = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_TERRORIST));
        GetObject(hbmTerrorist, sizeof(bmT), &bmT);
        hbmTerroristMask = CreateBitmap(bmT.bmWidth, bmT.bmHeight, 1, 1, NULL);
        HDC hdcSrc = CreateCompatibleDC(NULL);
        HDC hdcDst = CreateCompatibleDC(NULL);
        HBITMAP hbmSrcT = SelectBitmap(hdcSrc, hbmTerrorist);
        HBITMAP hbmDstT = SelectBitmap(hdcDst, hbmTerroristMask);
        COLORREF clrTopLeft = GetPixel(hdcSrc, 0, 0);
        COLORREF clrSaveBk = SetBkColor(hdcSrc, clrTopLeft);
        BitBlt(hdcDst, 0, 0, bmT.bmWidth, bmT.bmHeight, hdcSrc, 0, 0, SRCCOPY);
        COLORREF clrSaveDstText = SetTextColor(hdcSrc, RGB(255,255,255));
        SetBkColor(hdcSrc, RGB(0,0,0));
        BitBlt(hdcSrc, 0, 0, bmT.bmWidth, bmT.bmHeight, hdcDst, 0, 0, SRCAND);
        SetTextColor(hdcDst, clrSaveDstText);
        SetBkColor(hdcSrc, clrSaveBk);
        SelectBitmap(hdcSrc, hbmSrcT);
        SelectBitmap(hdcDst, hbmDstT);
        DeleteDC(hdcSrc);
        DeleteDC(hdcDst);
    }
    
    void TerroristAI() {
        int attacking = 0;
        HDC hdc = GetDC(hwnd);
        for(;;) {
            if(terroristhealth <= 0) {
                break;
            }    
            if(terrorist1x - playerx <= 160) {
                attacking = 1;
            }
            if(attacking == 1) {
                if(terrorist1x > playerx) {
                    terrorist1x -= 5;
                }
                else if(terrorist1x < playerx) {
                    terrorist1x += 5;
                }
                Sleep(75);
                //Sleep(1000);
                EraseTerrorist(hdc);
                UpdateTerrorist(hwnd);
                DrawTerrorist(hdc);
            }
        }
        attacking = 0;
        ReleaseDC(hwnd, hdc);
        TerminateThread(TerroristThreadID, 0);
    }
    
    void LoadBullet() {
        hbmBullet = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BULLET));
        GetObject(hbmBullet, sizeof(bmBullet), &bmBullet);
        hbmBulletMask = CreateBitmap(bmBullet.bmWidth, bmBullet.bmHeight, 1, 1, NULL);
        HDC hdcSrc = CreateCompatibleDC(NULL);
        HDC hdcDst = CreateCompatibleDC(NULL);
        HBITMAP hbmSrcT = SelectBitmap(hdcSrc, hbmBullet);
        HBITMAP hbmDstT = SelectBitmap(hdcDst, hbmBulletMask);
        COLORREF clrTopLeft = GetPixel(hdcSrc, 0, 0);
        COLORREF clrSaveBk = SetBkColor(hdcSrc, clrTopLeft);
        BitBlt(hdcDst, 0, 0, bmBullet.bmWidth, bmBullet.bmHeight, hdcSrc, 0, 0, SRCCOPY);
        COLORREF clrSaveDstText = SetTextColor(hdcSrc, RGB(255,255,255));
        SetBkColor(hdcSrc, RGB(0,0,0));
        BitBlt(hdcSrc, 0, 0, bmBullet.bmWidth, bmBullet.bmHeight, hdcDst, 0, 0, SRCAND);
        SetTextColor(hdcDst, clrSaveDstText);
        SetBkColor(hdcSrc, clrSaveBk);
        SelectBitmap(hdcSrc, hbmSrcT);
        SelectBitmap(hdcDst, hbmDstT);
        DeleteDC(hdcSrc);
        DeleteDC(hdcDst);
    }
    
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT ps;
        HDC hdcWindow;
        switch(Message)
        {
            case WM_CREATE:
                /*
                hbmPlayer = LoadBitmap(g_hInst, "BALLBMP");
                hbmPlayerMask = LoadBitmap(g_hInst, "MASKBMP");
                if(!hbmPlayer || !hbmPlayerMask){
                    MessageBox(hwnd, "Load of resources failed.", "Error", MB_OK | MB_ICONEXCLAMATION);
                    return -1;
                }
                
                GetObject(hbmPlayer, sizeof(bm), &bm);
                */
                
                SetTimer(hwnd, idTimer1, nTimerDelay, NULL);      
                
                playerx = 32;
                playery = 230;
                terrorist1x = 240;
                terrorist1y = 230;
                bulletx = 0;
                bullety = 0;
                
                hbmPlayer = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_SEAL));
                GetObject(hbmPlayer, sizeof(bm), &bm);
                hbmPlayerMask = CreateBitmap(bm.bmWidth,bm.bmHeight,1,1,NULL);
                HDC hdcSrc = CreateCompatibleDC(NULL);
                HDC hdcDst = CreateCompatibleDC(NULL);
                HBITMAP hbmSrcT = SelectBitmap(hdcSrc,hbmPlayer);
                HBITMAP hbmDstT = SelectBitmap(hdcDst,hbmPlayerMask);
                COLORREF clrTopLeft = GetPixel(hdcSrc,0,0);
                COLORREF clrSaveBk = SetBkColor(hdcSrc,clrTopLeft);
                BitBlt(hdcDst,0,0,bm.bmWidth,bm.bmHeight,hdcSrc,0,0,SRCCOPY);
                COLORREF clrSaveDstText = SetTextColor(hdcSrc,RGB(255,255,255));
                SetBkColor(hdcSrc,RGB(0,0,0));
                BitBlt(hdcSrc,0,0,bm.bmWidth,bm.bmHeight,hdcDst,0,0,SRCAND);
                SetTextColor(hdcDst,clrSaveDstText);
                SetBkColor(hdcSrc,clrSaveBk);
                SelectBitmap(hdcSrc,hbmSrcT);
                SelectBitmap(hdcDst,hbmDstT);
                DeleteDC(hdcSrc);
                DeleteDC(hdcDst);
                
                LoadBullet();
                LoadTerrorist();
                TerroristThreadID = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TerroristAI, NULL, 0, NULL);
                break;
            /*
            case WM_TIMER:
                if(hbmPlayer && hbmPlayerMask)
                {
                    hdcWindow = GetDC(hwnd);
                    
                    ErasePlayer(hdcWindow);
                    UpdatePlayer(hwnd);
                    DrawPlayer(hdcWindow);
                    
                    ReleaseDC(hwnd, hdcWindow);
                }
                break;
            */
            case WM_PAINT:
                if(hbmPlayer && hbmPlayerMask)
                {
                    hdcWindow = BeginPaint(hwnd, &ps);
                    DrawPlayer(hdcWindow);
                    DrawTerrorist(hdcWindow);
                    EndPaint(hwnd, &ps);
                }
                break;
            case WM_KEYDOWN:
                switch(wParam) {
                    case VK_LEFT:
                        playerx -= 5;
                        hdcWindow = GetDC(hwnd);
                        ErasePlayer(hdcWindow);
                        UpdatePlayer(hwnd);
                        DrawPlayer(hdcWindow);
                        ReleaseDC(hwnd, hdcWindow);
                        break;
                    case VK_RIGHT:
                        playerx += 5;
                        hdcWindow = GetDC(hwnd);
                        ErasePlayer(hdcWindow);
                        UpdatePlayer(hwnd);
                        DrawPlayer(hdcWindow);
                        ReleaseDC(hwnd, hdcWindow);
                        break;
                    case VK_UP:
                        if(Jumping == 0) {
                            JumpThreadID = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlayerJump, NULL, 0, NULL);
                            Jumping = 1;
                        }    
                        break;
                    /*
                    case VK_DOWN:
                        playery += 5;
                        hdcWindow = GetDC(hwnd);
                        ErasePlayer(hdcWindow);
                        UpdatePlayer(hwnd);
                        DrawPlayer(hdcWindow);
                        ReleaseDC(hwnd, hdcWindow);
                        break;
                    */
                    case VK_SPACE:
                        break;
                }
                break;
            case WM_CLOSE:
                DestroyWindow(hwnd);
                break;
            case WM_DESTROY:
                KillTimer(hwnd, idTimer1);
                
                DeleteObject(hbmPlayer);
                DeleteObject(hbmPlayerMask);
                DeleteObject(hbmTerrorist);
                DeleteObject(hbmTerroristMask);
                DeleteObject(hbmBullet);
                DeleteObject(hbmBulletMask);
                PostQuitMessage(0);
                break;
            default:
                return DefWindowProc(hwnd, Message, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
       LPSTR lpCmdLine, int nCmdShow)
    {
       WNDCLASSEX WndClass;
       MSG Msg;
       g_hInst = hInstance;
       WndClass.cbSize        = sizeof(WNDCLASSEX);
       WndClass.style         = 0;
       WndClass.lpfnWndProc   = WndProc;
       WndClass.cbClsExtra    = 0;
       WndClass.cbWndExtra    = 0;
       WndClass.hInstance     = g_hInst;
       WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
       WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
       WndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
       WndClass.lpszMenuName  = NULL;
       WndClass.lpszClassName = g_szClassName;
       WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
       
       if(!RegisterClassEx(&WndClass))
       {
          MessageBox(0, "Window Registration Failed!", "Error!",
             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
          return 0;
       }
       
       hwnd = CreateWindowEx(
          WS_EX_CLIENTEDGE,
          g_szClassName,
          "Side Scrolling Shooter",
          WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, CW_USEDEFAULT, 640, 320,
          NULL, NULL, g_hInst, NULL);
       
       if(hwnd == NULL)
       {
          MessageBox(0, "Window Creation Failed!", "Error!",
             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
          return 0;
       }
       
       ShowWindow(hwnd, nCmdShow);
       UpdateWindow(hwnd);
       
       
       while(GetMessage(&Msg, NULL, 0, 0))
       {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
       }
       return Msg.wParam;
    }
    my header (which has the class in it right now):

    Code:
    #define IDB_SEAL                        103
    #define IDB_TERRORIST                   105
    #define IDB_BULLET                      107
    
    
    
    class Bullets {
        private:
            struct General {
                int x;
                int y;
                int inuse;
            };
        public:
            General B1, B2, B3, B4, B5, B6, B7, B8, B9, B10;
    } PlayerBullets, TerroristBullets;

    resource:

    Code:
    #include "sidescrolling.h"
    
    IDB_SEAL BITMAP "SEAL.bmp"
    IDB_TERRORIST BITMAP "Terrorist1.bmp"
    IDB_BULLET BITMAP "bullet.bmp"
    the error message:

    In file included from scrollingtest.c
    parser error before "Bullets"
    syntax error before '{' token
    parser error before '}' token
    [Warning] data definition has no type or storage class
    [Build Error] [sidescrollingtest.o] Error 1
    for some background, this is the start of an army side scrolling shooter.

    if any other info is needed, please ask.

    Thanks in advance.
    Last edited by Morgul; 02-12-2005 at 01:43 PM. Reason: "Fast typing spelling errors/other stuff"

  2. #2
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    I forgot to say that one time, i edited a ton of stuff and the class worked, but then when i went back in and tried to continue on and the class messed up again.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    May I recommend changing:
    Code:
    class Bullets {
        private:
            struct General {
                int x;
                int y;
                int inuse;
            };
        public:
            General B1, B2, B3, B4, B5, B6, B7, B8, B9, B10;
    } PlayerBullets, TerroristBullets;
    to:

    Code:
    struct BulletState
    {
      int x;
      int y;
      bool InUse;
      BulletState(void):x(0),y(0),InUse(false) {}
     
    };
    
    class BulletManager
    {
      protected:
        BulletState Bullets[10];
      public:
        //Functions to manage bullets
    };  //Don't instantiate globally
    Potentially a much better method would be to use a vector instead of a fixed array.
    ..
    std::vector<BulletState> BulletList;
    ..

    Then create a function to AddBullets

    Code:
    void BulletManager::AddBullet(int x,int y)
    {
       BulletState temp;
       temp.x=x;
       temp.y=y;
       temp.InUse=true;
       BulletList.push_back(temp);
    }
    Last edited by VirtualAce; 02-12-2005 at 02:55 PM.

  4. #4
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109

    Talking Fixed

    it excepted it! thanks.

    and yes I think I will use a vector to have more flexibility with the number of bullets I'm using.

    thanks alot.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Beware when using vectors. The temp object I created goes out of scope at the end of the function. However the temp that is pushed into the vector is copied and therefore it is still valid after the function goes out of scope.

    You should create a copy constructor so that your object is copied correctly at all times.

    But this also means that BulletState should not be a struct but rather a class of its own. BulletManager is simply a class that manages access to the list of Bullets. In this way your programmer or the person using the class never accesses the actual bullet objects. The manager class does all of it and is also responsible for object cleanup.

    A copy constructor for your Bullet class might look like this:

    Code:
    Bullet(const Bullet &bullet_object)
    {
       x=bullet_object.x;
       y=bullet_object.y;
       InUse=bullet_object.InUse;
    }
    Note that with your object members a copy constructor will not do much in this case and is not technically needed, but its a good practice to get into because eventually you will need them.

  6. #6
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    Thanks again.

    However, I am still having many problems getting it to recognize classes or structures. originally, it compiled fine when i first started using your method. But, i started adding another part to it and it screwed up. It doesnt understand the vector header.

    I think im going to just try to redo it, even though all my attempts to load graphics previously failed, it never loaded them.

    anyway, here is what i have for my header now, are there any errors you see?

    Code:
    #include <vector.h>
    
    #define IDB_SEAL                        103
    #define IDB_TERRORIST                   105
    #define IDB_BULLET                      107
    
    
    struct BulletState
    {
        int x;
        int y;
        bool InUse;
        BulletState(void):x(0),y(0),InUse(false) {}
    };
    
    class BulletManager {
        protected:
            vector<BulletState> BulletList;
        public:
            Addbullet(int x, int y);
            MoveBullet(int i);
            //Functions to manage bullets
    };     //Don't instantiate globally
    
    void BulletManager::AddBullet(int x,int y) {
        BulletState temp;
        temp.x=x;
        temp.y=y;
        temp.InUse=true;
        BulletList.push_back(temp);
    }
    
    void BulletManager::MoveBullet(int i) {
        POINT p;
        RECT rc;
        GetClientRect(hwnd, &rc);
        int x, y;
        GetCursorPos(&p);
        x = BulletList[i].x;
        y = BulletList[i].y;
        int xmovement = (p.x - x) / 8;
        int ymovement = (p.y - y) / 8;
        for(;;) {
            x += xmovement;
            y += ymovement;
            if(x < 0){
                x = 0;
            }
            else if(x + 4 > rc.right){
                x = rc.right - bmT.bmWidth;
            }
            if(y < 0){
                y = 0;
            }
            else if(y + 4 > rc.bottom){
                y = rc.bottom - bmT.bmHeight;
            }
        }
    }
    thanks again
    Sic vis pacum para bellum. If you want peace, prepare for war.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    //User vector header - not vector.h
    #include <vector>
    
    #define IDB_SEAL                        103
    #define IDB_TERRORIST                   105
    #define IDB_BULLET                      107
    
    
    struct BulletState
    {
        float x;
        float y;
        bool InUse;
        BulletState(void):x(0.0f),y(0.0f),InUse(false) {}
    };
    
    class BulletManager 
    {
        protected:
            vector<BulletState> BulletList;
        public:
            //Functions with no return type are void
            void Addbullet(float x, float y);
            void MoveBullet(int i);
            //Functions to manage bullets
    
            void MoveAllBullets(void);
    };     //Don't instantiate globally
    
    void BulletManager::AddBullet(float x,float y) 
    {
        BulletState temp;
        temp.x=x;
        temp.y=y;
        temp.InUse=true;
        BulletList.push_back(temp);
    }
    
    void BulletManager::MoveBullet(int i) 
    {
        POINT p;
        RECT rc;
        GetClientRect(hwnd, &rc);
        int x, y;
        GetCursorPos(&p);
        x = BulletList[i].x;
        y = BulletList[i].y;
        int xmovement = (p.x - x) / 8;
        int ymovement = (p.y - y) / 8;
        for(;;) {
            x += xmovement;
            y += ymovement;
            if(x < 0){
                x = 0;
            }
            else if(x + 4 > rc.right){
                x = rc.right - bmT.bmWidth;
            }
            if(y < 0){
                y = 0;
            }
            else if(y + 4 > rc.bottom){
                y = rc.bottom - bmT.bmHeight;
            }
        }
    }
    
    
    void BulletManager::MoveAllBullets(void)
    {
       POINT p;
       RECT rc;
       GetClientRect(hwnd, &rc);
       float x=0.0f, y=0.0f;
       GetCursorPos(&p);
    
        float xmovement = 0.0f;
        float ymovement = 0.0f;
    
        //Bullet list iterator
        std::vector<BulletState>::iterator iter=BulletList.begin();
    
        while (iter!=BulletList.end())
        {
           x=BulletList[iter].x;
           y=BulletList[iter].y;
               
           xmovement = ((float)p.x - x) / 8.0f;
           ymovement = ((float)p.y - y) / 8.0f;
    
           x+=xmovement;
           y+=ymovement;
    
           //clamp X and Y here 
           if(x < 0.0f)
           {
             x = 0.0f;
           }
           else if (x + 4.0f > (float)rc.right)
           {
              x = (float)rc.right - (float)bmT.bmWidth;
           }
            
            if(y < 0.0f)
            {
               y = 0.0f;
            }
            else if(y + 4.0f > (float)rc.bottom)
            {
               y = (float)rc.bottom - (float)bmT.bmHeight;
            } 
            BulletList[iter].x=x;
            BulletList[iter].y=y;
            ++iter;
         }
      }
    }
    I see no need to just move one bullet when you can update them all with one function.

    I changed your x and y to floats since using integers will probably yield some strange results.

  8. #8
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    thanks a lot.

    it tells me vector doesnt exist, even though i can see it when i go to the compiler's include folder.

    also yah floats probably would be better.


    oh and the thing doesnt seem to know what a bool is. it has a ton of errors, here is a list...


    3 C:\Dev-Cpp\Side Scrolling Shooter\sidescrollingtest.c In file included from sidescrollingtest.c
    1 C:\Dev-Cpp\Side Scrolling Shooter\sidescrolling.h vector: No such file or directory.

    3 C:\Dev-Cpp\Side Scrolling Shooter\sidescrollingtest.c In file included from sidescrollingtest.c

    12 C:\Dev-Cpp\Side Scrolling Shooter\sidescrolling.h parse error before "bool"

    12 C:\Dev-Cpp\Side Scrolling Shooter\sidescrolling.h no semicolon at end of struct or union

    13 C:\Dev-Cpp\Side Scrolling Shooter\sidescrolling.h parse error before ':' token

    14 C:\Dev-Cpp\Side Scrolling Shooter\sidescrolling.h ISO C does not allow extra `;' outside of a function

    16 C:\Dev-Cpp\Side Scrolling Shooter\sidescrolling.h parse error before "BulletManager"

    17 C:\Dev-Cpp\Side Scrolling Shooter\sidescrolling.h syntax error before '{' token

    24 C:\Dev-Cpp\Side Scrolling Shooter\sidescrolling.h parse error before '/' token

    27 C:\Dev-Cpp\Side Scrolling Shooter\sidescrolling.h ISO C does not allow extra `;' outside of a function

    13 C:\Dev-Cpp\Side Scrolling Shooter\sidescrolling.h:27 missing terminating ' character

    ...

    the list goes on for a while. all the problems are stupid things that arent problems at all.
    Last edited by Morgul; 02-14-2005 at 04:35 PM. Reason: bool
    Sic vis pacum para bellum. If you want peace, prepare for war.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    That's because you are using Dev Cpp. You must cater my code to fit your dev environment. I personally do no like Dev Cpp because that is the compiler on here that we get the most questions about.

  10. #10
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    i think that probably, when this was first started, some button was clicked that screwed it up. I dont know, I'm going to see if i can find whatever preference or whatever that did this.

    what free compilers do you recommend? I've tried Microsoft Visual Studio 2005 Beta Express Edition (or whatever it is called) and that doesnt seem to be very good, and I tried Borlands for a little while, though not that long really.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    The original error could be because you're compiling the file as C code, not C++.

    I recommend using std::list instead of std::vector for game objects, because you'll probably add/remove bullets a lot, which is cheaper with a list. Also, you won't need the random access of a vector, because you'll probably just iterate through the list from beginning to end.
    It doesn't matter that much what you choose for these kinds of games, though.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Nothing wrong with dev, bubba just is complaining .
    I use it all the time and I don't have trouble with it minus a few editor bugs .
    Woop?

  13. #13
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    This is making me really mad. I don't understand why the heck this could be happening.

    std::list sounds like it might be a good idea, thanks for the suggestion.




    The only thing is a I dont know why this could be screwing up, it makes no sense whate-so-ever.

    I checked and it all appears to be c++. The only thing that I can find that might be why it is messed up would be for it to have been started as an OpenGL project, but I doubt that is possible. I'm going to try to make a new project (with the same code) and see if it changes anything.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  14. #14
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    My main source code was a c source! Hey! I'm going to redo it as c++ and see if that changes anything (though all the class stuff was in the header which is c/c++)
    Sic vis pacum para bellum. If you want peace, prepare for war.

  15. #15
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109

    Getting somewhere

    I redid the source as c++ and it minimzed the errors of the header. However, i still have some that i'm trying to fix in the header. I get some of them (some are spelling mistakes and the like) but some make no sense.

    the header is the same as before (mostly, i had to move the declaration of some variables, which i was going to do anyway):

    Code:
    #include <vector>
    
    #define IDB_SEAL                        103
    #define IDB_TERRORIST                   105
    #define IDB_BULLET                      107
    
    
    HBITMAP hbmPlayer, hbmPlayerMask, hbmTerrorist, hbmTerroristMask, hbmBullet, hbmBulletMask;
    BITMAP bm, bmT, bmBullet;
    int playerx, playery, terrorist1x, terrorist1y, bulletx, bullety;
    int playerhealth = 100;
    int terroristhealth = 50;
    int Jumping = 0;
    HWND hwnd, main;
    HANDLE JumpThreadID, TerroristThreadID, BulletThreadID;
    
    struct BulletState
    {
        float x;
        float y;
        bool InUse;
        BulletState(void):x(0.0f),y(0.0f),InUse(false) {}
    };
    
    class BulletManager 
    {
        protected:
            std::vector<BulletState> BulletList;
        public:
            //Functions with no return type are void
            void AddBullet(float x, float y);
            void MoveBullet(int i);
            //Functions to manage bullets
    
            void MoveAllBullets(void);
    };     //Don't instantiate globally
    
    void BulletManager::AddBullet(float x,float y) 
    {
        BulletState temp;
        temp.x=x;
        temp.y=y;
        temp.InUse=true;
        BulletList.push_back(temp);
    }
    
    void BulletManager::MoveBullet(int i) 
    {
        POINT p;
        RECT rc;
        GetClientRect(hwnd, &rc);
        int x, y;
        GetCursorPos(&p);
        x = BulletList[i].x;
        y = BulletList[i].y;
        int xmovement = (p.x - x) / 8;
        int ymovement = (p.y - y) / 8;
        for(;;) {
            x += xmovement;
            y += ymovement;
            if(x < 0){
                x = 0;
            }
            else if(x + 4 > rc.right){
                x = rc.right - bmT.bmWidth;
            }
            if(y < 0){
                y = 0;
            }
            else if(y + 4 > rc.bottom){
                y = rc.bottom - bmT.bmHeight;
            }
        }
    }
    
    
    void BulletManager::MoveAllBullets(void)
    {
       POINT p;
       RECT rc;
       GetClientRect(hwnd, &rc);
       float x=0.0f, y=0.0f;
       GetCursorPos(&p);
    
        float xmovement = 0.0f;
        float ymovement = 0.0f;
    
        //Bullet list iterator
        std::vector<BulletState>::iterator iter=BulletList.begin();
    
        while (iter!=BulletList.end())
        {
           x=BulletList[iter].x;
           y=BulletList[iter].y;
               
           xmovement = ((float)p.x - x) / 8.0f;
           ymovement = ((float)p.y - y) / 8.0f;
    
           x+=xmovement;
           y+=ymovement;
    
           //clamp X and Y here 
           if(x < 0.0f)
           {
             x = 0.0f;
           }
           else if (x + 4.0f > (float)rc.right)
           {
              x = (float)rc.right - (float)bmT.bmWidth;
           }
            
            if(y < 0.0f)
            {
               y = 0.0f;
            }
            else if(y + 4.0f > (float)rc.bottom)
            {
               y = (float)rc.bottom - (float)bmT.bmHeight;
            } 
            BulletList[iter].x=x;
            BulletList[iter].y=y;
            ++iter;
         }
      }
    }
    3 C:\Dev-Cpp\Nuclear Ops\nuclearmain.cpp In file included from nuclearmain.cpp

    C:\Dev-Cpp\Nuclear Ops\nuclearheader.h In member function `void BulletManager::MoveBullet(int)':

    54 C:\Dev-Cpp\Nuclear Ops\nuclearheader.h [Warning] assignment to `int' from `float'

    54 C:\Dev-Cpp\Nuclear Ops\nuclearheader.h [Warning] argument to `int' from `float'

    55 C:\Dev-Cpp\Nuclear Ops\nuclearheader.h [Warning] assignment to `int' from `float'

    55 C:\Dev-Cpp\Nuclear Ops\nuclearheader.h [Warning] argument to `int' from `float'

    C:\Dev-Cpp\Nuclear Ops\nuclearheader.h In member function `void BulletManager::MoveAllBullets()':

    93 C:\Dev-Cpp\Nuclear Ops\nuclearheader.h no match for 'operator[]' in ' this->BulletManager::BulletList[iter]'

    error C:\Dev-Cpp\include\c++\3.3.1\bits\stl_vector.h:501 candidates are: _Tp& std::vector<_Tp, _Alloc>::operator[](unsigned int) [with _Tp = BulletState,

    515 C:\Dev-Cpp\include\c++\3.3.1\bits\stl_vector.h const _Tp& std::vector<_Tp, _Alloc>::operator[](unsigned int) const [with

    94 C:\Dev-Cpp\Nuclear Ops\nuclearheader.h no match for 'operator[]' in ' this->BulletManager::BulletList[iter]'

    error C:\Dev-Cpp\include\c++\3.3.1\bits\stl_vector.h:501 candidates are: _Tp& std::vector<_Tp, _Alloc>::operator[](unsigned int) [with _Tp = BulletState,

    515 C:\Dev-Cpp\include\c++\3.3.1\bits\stl_vector.h const _Tp& std::vector<_Tp, _Alloc>::operator[](unsigned int) const [with

    120 C:\Dev-Cpp\Nuclear Ops\nuclearheader.h no match for 'operator[]' in ' this->BulletManager::BulletList[iter]'

    error C:\Dev-Cpp\include\c++\3.3.1\bits\stl_vector.h:501 candidates are: _Tp& std::vector<_Tp, _Alloc>::operator[](unsigned int) [with _Tp = BulletState,

    515 C:\Dev-Cpp\include\c++\3.3.1\bits\stl_vector.h const _Tp& std::vector<_Tp, _Alloc>::operator[](unsigned int) const [with

    121 C:\Dev-Cpp\Nuclear Ops\nuclearheader.h no match for 'operator[]' in ' this->BulletManager::BulletList[iter]'

    error C:\Dev-Cpp\include\c++\3.3.1\bits\stl_vector.h:501 candidates are: _Tp& std::vector<_Tp, _Alloc>::operator[](unsigned int) [with _Tp = BulletState,

    515 C:\Dev-Cpp\include\c++\3.3.1\bits\stl_vector.h const _Tp& std::vector<_Tp, _Alloc>::operator[](unsigned int) const [with

    ...
    those bottom ones are some of the ones i dont get the most, it doesnt get the [] in things like

    Code:
    x = BulletList[i].x;
    y = BulletList[i].y;
    though it might have the fact that it is assigning and integer to a float, ill do the conversion right now to see if the problem is eliminated.

    some of the other things i have problems with is cases where i have a function that declares a ton of variables (the ones where i make the images transparent), i do that a couple times and it says that i have re-declared them (when actually, i havent at all, instead they are all in different functions, though i might have just figured out why, let me see)
    Sic vis pacum para bellum. If you want peace, prepare for war.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Question about engine design.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 01-29-2008, 10:53 AM
  3. Game Entities
    By DavidP in forum Game Programming
    Replies: 8
    Last Post: 05-31-2007, 09:07 PM
  4. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 01:48 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21