strucktur für alle klassen definieren



  • Ich hab jetzt 2 Klassen, die den selben D3D-Device benutzen müssen und beide
    die CUSTOMVERTEX-Strucktur kennen sollen.
    Wie stelle ich jetzt an, dass alle Klassen die D3D Variablen und die CUSTOMVERTEX-Strucktur kennen?



  • INTEGER schrieb:

    Ich hab jetzt 2 Klassen, die den selben D3D-Device benutzen müssen und beide
    die CUSTOMVERTEX-Strucktur kennen sollen.
    Wie stelle ich jetzt an, dass alle Klassen die D3D Variablen und die CUSTOMVERTEX-Strucktur kennen?

    mach mal Singleton

    //
    //App.h

    class App
    {
    public:
      static App &getInstance()
      {
        static App theApp;
        return theApp;
      }
    
      const CUSTOMVERTEX &getPublicVertex() const
      {
        return vert;
      }
    
      const D3DDevice &get3DDevice() const
      {
        return device;
      }
    private:
      App(){}
      App(const A&){}  
    private:
      CUSTOMVERTEX vert;
      D3DDevice device;
    }
    

    //A.h

    #include "App.h"
    
    class A
    {
    public:
      A()
      {
        const App &app = App::getInstance();
        const D3DDevice &device = app.get3DDevice();
      }    
    };
    

    //B.h

    #include "App.h"
    
    class B
    {
    public:
      B()
      {
        const App &app = App::getInstance();
        const D3DDevice &device = app.get3DDevice();
        const CUSTOMVERTEX &vert = app.getPublicVertex();
      }
    };
    


  • Man hat mich auf http://www.c-plusplus.net/forum/viewtopic.php?t=39477 hingewiesen.
    Jetzt gehts auch.

    Sorry für die Arbeit, die du wegen mir hattest.



  • Jetzt taucht aber ein weiteres Problem auf, für welches ich aber keinen neuen
    Thread aufmachen will.

    Shape.h

    #pragma once
    #include <stdlib.h>
    #include <stdio.h>
    #include <d3d9.h>
    #include <d3dx9tex.h>
    #include <d3dx9core.h>
    #include "Globals.h"
    
    class Shape
    {
    public:
    	Shape(void);
    	~Shape(void);
    
    	bool LoadData(FILE * file);
    	CUSTOMVERTEX* vertex;
    	VECTOR* normals;
    	int iNormals;
    	WORD* wIndices;
    	int iIndices;
    	LPDIRECT3DVERTEXBUFFER9 pVB;
    	LPDIRECT3DINDEXBUFFER9 pInd;
    	int num_vertices; // um die Variable gehts
    };
    

    Shape.cpp

    bool Shape::LoadData(FILE * file)
    {
    	bool bError=false;
    	int *iPol = new int;
    	char szLine[256];
    
    	int i,nFlags;
    	int dump[10];
    
    	fgets (szLine, 256, file);	
    
    	sscanf (szLine2 "%d", &num_vertices);
    
    	for(i = 0; i < num_vertices;i++) /*Diese Zeile wird beim Debuggen als
                                                Fehlerhaft angezeit*/
    

    Jedes mal wenn ich der Variable einen Wert zuweise oder ihren Wert abfrage,
    bekomm ich einen Zugriffsverletztung-Fehler.



  • INTEGER schrieb:

    Jedes mal wenn ich der Variable einen Wert zuweise oder ihren Wert abfrage,
    bekomm ich einen Zugriffsverletztung-Fehler.

    du hast zu wenig gezeigt :p
    ich brauche mehr um etwas zu sagen



  • Anscheinen trifft das auf alle Variablen in der Klasse zu.
    Sobald sie aufgerufen werden, kommt dann beim Ausführen der Fehler.

    Hier die beiden Klassen:
    (in der main() wird dann nur noch LoadFromFile ausgeführt)

    Modell.h

    #pragma once
    #include <stdlib.h>
    #include <stdio.h>
    #include <d3d9.h>
    #include <d3dx9tex.h>
    #include <d3dx9core.h>
    #include "Shape.h"
    #include "Globals.h"
    
    class Modell
    {
    public:
    	Modell(void);
    	~Modell(void);
    	bool LoadFromFile(char* name, LPDIRECT3DDEVICE9 g_pd3dDevice);
    	bool Init3D(void);
    
    	int iShapes;
    
    	Shape* Shapes;
    };
    

    Modell.cpp

    #include ".\modell.h"
    
    Modell::Modell(void)
    : iShapes(0)
    , Shapes(NULL)
    {
    }
    
    Modell::~Modell(void)
    {
    }
    
    bool Modell::LoadFromFile(char* name, LPDIRECT3DDEVICE9 g_pd3dDevice)
    {
    	FILE *file;
    	char *sLine = new char;
    	int i=0;
    	bool bError=false;
    
    	file = fopen(name,"rt");
    	while (fgets (sLine, 256, file) != NULL  && !bError)
        {
            if (!strncmp (sLine, "//", 2))
                continue;
    		if (sscanf(sLine, "Meshes: %d", &iShapes) == 1)
    		{
    			for (i = 0; i < iShapes && !bError; i++)
    			{
    				fgets (sLine, 256, file);
    				Shapes[i].LoadData(file,g_pd3dDevice);
    			}
    		}
    	}
    	fclose(file);
    	return true;
    }
    
    bool Modell::Init3D(void)
    {
    
    	return true;
    }
    

    Shape.h

    #pragma once
    #include <stdlib.h>
    #include <stdio.h>
    #include <d3d9.h>
    #include <d3dx9tex.h>
    #include <d3dx9core.h>
    #include "Globals.h"
    
    class Shape
    {
    public:
    	Shape(void);
    	~Shape(void);
    
    	bool LoadData(FILE * file, LPDIRECT3DDEVICE9 g_pd3dDevice);
    	CUSTOMVERTEX* vertices;
    	VECTOR* normals;
    	int iNormalen;
    	WORD* wIndices;
    	int iIndices;
    	LPDIRECT3DVERTEXBUFFER9 pVB;
    	LPDIRECT3DINDEXBUFFER9 pInd;
    	int num_vertices;
    };
    

    Shape.cpp

    #include ".\shape.h"
    
    Shape::Shape(void)
    : vertices(NULL)
    , normals(NULL)
    , iNormalen(0)
    , wIndices(NULL)
    , iIndices(0)
    , num_vertices(0)
    {
    }
    
    Shape::~Shape(void)
    {
    }
    
    bool Shape::LoadData(FILE * file, LPDIRECT3DDEVICE9 g_pd3dDevice)
    {
    	bool bError=false;
    	char szLine[256];
    
    	int i,nFlags;
    	int dump[10];
    
    	fgets (szLine, 256, file);
    
    	sscanf (szLine, "%d", &num_vertices);
    
    	for(i = 0; i < num_vertices;i++)
    	{
    		fgets (szLine, 256, file);
    		sscanf (szLine, "%d %f %f %f %f %f %d",&nFlags,
    			&vertices[i].x, &vertices[i].y, &vertices[i].z,
                            &vertices[i].tu, &vertices[i].tv);
    		vertices[i].tv = 1.0f - vertices[i].tv;
    	}
    	fgets (szLine, 256, file);
    	sscanf (szLine, "%d", &iNormalen);
    	for(i = 0; i < iNormalen;i++)
    	{
    		fgets (szLine, 256, file);
    		sscanf (szLine, "%f %f %f", &normals[i].x, &normals[i].y, &normals[i].z);
    	}
    	fgets (szLine, 256, file);
    	sscanf (szLine, "%d", &iIndices);
    	iIndices = iIndices * 3;
    	for(i = 0; i < iIndices; i+=3)
    	{
    		fgets (szLine, 256, file);
    		sscanf (szLine, "%d %d %d %d %d %d %d %d", 
    			&dump[0],&wIndices[i], &wIndices[i+1], &wIndices[i+2],
    			&dump[1],&dump[2],&dump[3],&dump[4]);
    	}
    
    	if( FAILED( g_pd3dDevice->CreateVertexBuffer( num_vertices*sizeof(CUSTOMVERTEX),
                                                      0, D3DFVF_CUSTOMVERTEX,
                                                      D3DPOOL_DEFAULT, &pVB, NULL ) ) )
        {
            return false;
        }
    
    	CUSTOMVERTEX* pVertices;
        if( FAILED( pVB->Lock( 0, sizeof(pVertices), (void**)&pVertices, 0 ) ) )
            return false;
    	memcpy(pVertices,vertices,sizeof(vertices));
    	pVB->Unlock();
    
    	WORD *pIn;
    	if( FAILED( g_pd3dDevice->CreateIndexBuffer(iIndices*sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_DEFAULT,&pInd,NULL)))
    		return false;
    	if( FAILED(pInd->Lock(0,sizeof(pIn),(void**)&pIn,0)))
    		return false;
    	memcpy(pIn,wIndices,sizeof(wIndices)); 
    	pInd->Unlock();
    
    	return true;
    }
    


  • [cpp]Modell::~Modell(void)
    {
    delete [] Shapes;
    }

    bool Modell::LoadFromFile(char* name, LPDIRECT3DDEVICE9 g_pd3dDevice)
    {
    FILE *file;
    char *sLine = new char;
    int i=0;
    bool bError=false;

    file = fopen(name,"rt");
    while (fgets (sLine, 256, file) != NULL && !bError)
    {
    if (!strncmp (sLine, "//", 2))
    continue;
    if (sscanf(sLine, "Meshes: %d", &iShapes) == 1)
    {
    Shapes = new Shape[iShapes];
    for (i = 0; i < iShapes && !bError; i++)
    {
    fgets (sLine, 256, file);
    Shapes[i].LoadData(file,g_pd3dDevice);
    }
    }
    }
    fclose(file);
    return true;
    }[/cpp]



  • Habs überall geändert uns es funktioniert. Danke!
    Kann mich aber erinnern, dass man sowas bei VC++ 6.0 nicht machen musste.



  • INTEGER schrieb:

    Kann mich aber erinnern, dass man sowas bei VC++ 6.0 nicht machen musste.

    😃 doch!!!


Anmelden zum Antworten