Problem mir Globalen Variablen!



  • Ich hab jetzt mal mit DirectX angefangen aber gleich am Anfang hab ich ein Problem!
    Und zwar will ich für die Fehler die passieren können Fehlermeldungen ausgeben lassen!

    Aber leider hab ich das Problem das die Globalen Variablen für den handler und die instanz nur Probleme machen 😞

    Hier mal mein jetztiger(mickriger) Code:

    Main.cpp

    #include <windows.h>   // include important windows stuff
    #include <windowsx.h> 
    #include <mmsystem.h>
    //#include <iostream.h> // include important C/C++ stuff
    #include <conio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <memory.h>
    #include <string.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <math.h>
    #include <io.h>
    #include <fcntl.h>  
    
    //Eigene Includes:
    #include "Engine.h"
    
    //TODO:Das hier entfernen und global einsetzen:
    HWND main_window_handle = NULL; // save the window handle
    HINSTANCE main_instance = NULL; // save the instance
    char buffer[80]; 
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = WINDOW_CLASS_NAME;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               WINDOW_CLASS_NAME,         /* Classname */
               WINDOW_CLASS_NAME,       /* Title Text */
               WS_POPUP,                /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               WINDOW_WIDTH,                 /* The programs width */
               WINDOW_HEIGHT,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
        UpdateWindow(hwnd);
    
        // save the window handle and instance in a global
        main_window_handle = hwnd;
        main_instance      = hThisInstance;
    
        //Spiel starten:
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
    
            //Hier läuft das Spiel:
    
        }
    
        //Oberfläche Löschen:
    
        //Hier wird das Spiel herrunter gefahren:
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
            HDC hdc;
            PAINTSTRUCT ps;
    
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
                 break;
    
            case WM_PAINT:
                 hdc=BeginPaint(hwnd,&ps);
    
                 EndPaint(hwnd, &ps);
                 break;
    
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
    
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    

    Engine.h

    #ifndef Engine_H
    #define Engine_H
    
    //Eigene Includes:
    #include "DirectX/ddraw.h"
    
    // DEFINES ////////////////////////////////////////////////
    
    // defines for windows 
    #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
    
    #define WINDOW_WIDTH  640              // size of window
    #define WINDOW_HEIGHT 480
    #define SCREEN_WIDTH  640              // size of screen
    #define SCREEN_HEIGHT 480
    #define SCREEN_BPP    16               // bits per pixel
    
    // MACROS /////////////////////////////////////////////////
    
    // these read the keyboard asynchronously
    #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
    #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
    
    // this builds a 16 bit color value in 5.5.5 format (1-bit alpha mode)
    #define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))
    
    // this builds a 16 bit color value in 5.6.5 format (green dominate mode)
    #define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
    
    // TYPES //////////////////////////////////////////////////
    
    typedef unsigned short USHORT;
    typedef unsigned short WORD;
    typedef unsigned char  UCHAR;
    typedef unsigned char  BYTE;
    
    class CEngine
    {
    public:
    
    //Konstruktor/Destruktor:
    CEngine();
    ~CEngine();
    
    //Grafik Stuff:
    void InitGame();
    void InitDirectDraw();
    void InitPrimarySurface();
    void InitSecondarySurface();
    
    //Tastatur(Eingaben)Stuff:
    void InitDirectInput();
    
    //SoundStuff:
    void InitDirectSound();
    
    //Grafik Stuff:
    void ShutdownGame();
    void ReleaseDirectDraw();
    void ReleasePrimarySurface();
    void ReleaseSecondarySurface();
    
    //Tastatur(Eingaben)Stuff:
    void ReleaseDirectInput();
    
    //SoundStuff:
    void ReleaseDirectSound();
    
    private:
    protected:
    };
    
    //Globales Engine Objekt:
    CEngine Engine;
    
    #endif //Engine_H
    

    Engine.cpp

    include "Engine.h"
    
    LPDIRECTDRAW7        lpdd         = NULL;  // dd object
    LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;  // dd primary surface
    LPDIRECTDRAWSURFACE7 lpddsback    = NULL;  // dd back surface
    LPDIRECTDRAWPALETTE  lpddpal      = NULL;  // a pointer to the created dd palette
    DDSURFACEDESC2       ddsd;                 // a direct draw surface description struct
    DDSCAPS2             ddscaps;              // a direct draw surface capabilities struct
    HRESULT              ddrval;               // result back from dd calls
    
    //Konstruktor/Destruktor:
    CEngine::CEngine()
    {
    }
    
    CEngine::~CEngine()
    {
    }
    
    //Grafik Stuff:
    void CEngine::InitGame()
    {
    }
    
    void CEngine::InitDirectDraw()
    {
    // this function is where you do all the initialization 
    // for your game
    
    // create object and test for error
    if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK)
    {
       //MessageBox(main_window_handle, "Das DirectX Objekt konnte nicht erstellt werden!", "Fehler beim erstellen des DX Objekts", MB_OK);
    }
    
    // set cooperation level to windowed mode normal
    if ((lpdd->SetCooperativeLevel(main_window_handle,
        DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | 
        DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))!=DD_OK)
    {
        //MessageBox(main_window_handle, "Die Kooperationsebene konnte nicht gesetzt werden!", "Kooperationsfehler", MB_OK);
    }
    
    // set the display mode
    if ((lpdd->SetDisplayMode(800,600,16,0,0))!=DD_OK)
    {
       //MessageBox(main_window_handle, "Die Bildschrimgröße konnte nicht angepasst werden!", "Display Fehler", MB_OK);
    }
    }
    
    void CEngine::InitPrimarySurface()
    {
    // Create the primary surface
    memset(&ddsd,0,sizeof(ddsd));
    ddsd.dwSize         = sizeof(ddsd);
    ddsd.dwFlags        = DDSD_CAPS;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
    
    if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
    {
     //MessageBox(main_window_handle,"Die Primäroberfäche konnte nicht erstellt werden!", "Fehler beim erstellen der Primär Oberfläche",MB_OK);
    }
    }
    
    void CEngine::InitSecondarySurface()
    {
    }
    
    //Tastatur(Eingaben)Stuff:
    void CEngine::InitDirectInput()
    {
    }
    
    //SoundStuff:
    void CEngine::InitDirectSound()
    {
    }
    
    //Grafik Stuff:
    void CEngine::ShutdownGame()
    {
    }
    
    void CEngine::ReleaseDirectDraw()
    {
     // this function is where you shutdown your game and
     // release all resources that you allocated
    
     // release the directdraw object
     if (lpdd!=NULL)
       lpdd->Release();
    }
    
    void CEngine::ReleasePrimarySurface()
    {
    // first release the primary surface
    if (lpddsprimary!=NULL)
       lpddsprimary->Release();
    }
    
    void CEngine::ReleaseSecondarySurface()
    {
    }
    
    //Tastatur(Eingaben)Stuff:
    void CEngine::ReleaseDirectInput()
    {
    }
    
    //SoundStuff:
    void CEngine::ReleaseDirectSound()
    {
    }
    

    Danke im voraus 🙂

    EDIT:
    Das sind die Globalen Probleme macher:

    //TODO:Das hier entfernen und global einsetzen:
    HWND main_window_handle = NULL; // save the window handle
    HINSTANCE main_instance = NULL; // save the instance
    char buffer[80];
    


  • Am schnellsten dürfte es gehen, wenn du folgende Zeilen

    //TODO:Das hier entfernen und global einsetzen:
    HWND main_window_handle = NULL; // save the window handle
    HINSTANCE main_instance = NULL; // save the instance
    

    aus Main.cpp entfernst und anstatt dessen in Engine.h einfügst.
    dann mußt du allerdings auch <windows.h> in Engine.h einbinden. (Wegen der Datentypen).

    Komm dann aber nicht auf die Idee die Dinger im Konstruktor der Klasse CEngine zu benutzen.



  • Okay hab ich aber ich hab diese Fehler:

    Compiler: Default compiler
    Building Makefile: "G:\Coding (Games)\Games\Erste Versuche\Game\Makefile.win"
    Finding dependencies for file: G:\Coding (Games)\Games\Erste Versuche\Game\main.cpp
    Finding dependencies for file: G:\Coding (Games)\Games\Erste Versuche\Game\Engine.cpp
    Führt  make... aus
    make.exe -f "G:\Coding (Games)\Games\Erste Versuche\Game\Makefile.win" all
    g++.exe main.o Engine.o  -o "Game.exe" -L"lib" -mwindows Libs/libddraw.a Libs/libdxguid.a  
    
    Engine.o(.bss+0x0):Engine.cpp: multiple definition of `main_window_handle'
    main.o(.bss+0x0):main.cpp: first defined here
    Engine.o(.bss+0x4):Engine.cpp: multiple definition of `main_instance'
    main.o(.bss+0x4):main.cpp: first defined here
    Engine.o(.bss+0x20):Engine.cpp: multiple definition of `buffer'
    main.o(.bss+0x20):main.cpp: first defined here
    Engine.o(.bss+0x70):Engine.cpp: multiple definition of `Engine'
    main.o(.bss+0x70):main.cpp: first defined here
    collect2: ld returned 1 exit status
    
    make.exe: *** [Game.exe] Error 1
    
    Ausführung beendet
    

    Als IDE nutze ich Dev-C++!!!



  • argh!

    Als erstes versuch mal Menu->Ausführen->Alles Erneuern.
    Denke daran, die Deklarationen von main_window_handle, main_instance, buffer aus Main.cpp zu entfernen!
    Falls das nichts hilft könntest du mal deine Engine Deklaration (CEngine Engine;) in Main.cpp verschieben (zumindest mal vorläufig). Wenns dann läuft mußt du halt weiter wursteln. Für ein Objekt, das immer für alle zur Verfügung steht würde sich ein Singleton besser eignen als diese Art der Deklaration!



  • Okay die Deklarationen hab ich ja entfernt aber ich hab das jetzt in die Engine.h gepackt aber selbst der Includeguard funkt nicht 😞

    Auch neuerstellen klappt nicht 😞

    Hab mir mal das XGame SDK gedownloaded. Werd mal gucken ob ich da was mit anfangen kann(später erst!!!) sollte ich mal ne andere IDE testen?

    EDIT:
    Auch Code::Blocks sagt diese Fehler 😢

    Project   : Win32 Application
    Compiler  : GNU GCC Compiler (called directly)
    Directory : G:\Coding (Games)\Games\Erste Versuche\CodeBlocks Game\
    --------------------------------------------------------------------------------
    Switching to target: default
    Compiling: main.cpp
    Compiling: Engine.cpp
    Linking executable: G:\Coding (Games)\Games\Erste Versuche\CodeBlocks Game\Game.exe
    .objs\Engine.o:Engine.cpp:(.bss+0x0): multiple definition of `main_window_handle'
    .objs\main.o:main.cpp:(.bss+0x0): first defined here
    .objs\Engine.o:Engine.cpp:(.bss+0x4): multiple definition of `main_instance'
    .objs\main.o:main.cpp:(.bss+0x4): first defined here
    .objs\Engine.o:Engine.cpp:(.bss+0x20): multiple definition of `buffer'
    .objs\main.o:main.cpp:(.bss+0x20): first defined here
    .objs\Engine.o:Engine.cpp:(.bss+0x70): multiple definition of `Engine'
    .objs\main.o:main.cpp:(.bss+0x70): first defined here
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 4 seconds)
    0 errors, 0 warnings
    


  • Du hast da ein paar Variablen mehrfach deklariert. (BTW: Kennst du Singletons?)

    Bye, TGGC (Das Eine, welches ist.)


Anmelden zum Antworten