Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Dzielenie kodu na kilka plików

Ostatnio zmodyfikowano 2009-09-05 15:50
Autor Wiadomość
steckel
Temat założony przez niniejszego użytkownika
Dzielenie kodu na kilka plików
» 2009-09-05 10:30:38
Mam takie pliki:

ShowTriangle.hpp:
C/C++
#ifndef SHOWTRIANGLE_HPP
#define SHOWTRIANGLE_HPP

#include <DirectX.hpp>
#include <Triangle.hpp>

#endif
ShowTriangle.cpp:
C/C++
#include "ShowTriangle.hpp"

bool Run = true;


void Rendering() {
    pDev->Clear( 0, 0, D3DCLEAR_TARGET, 0xff000000, 1, 0 );
    Triangle triangle1( 0, 0, 0, 1, 0xffff0000, 300, 0, 0, 1, 0xffff0000, 150, 200, 0, 1, 0xffff0000 );
    Triangle triangle2( 0, 200, 0, 1, 0xff0000ff, 150, 0, 0, 1, 0xff0000ff, 300, 200, 0, 1, 0xff0000ff );
    triangle1.Print();
    triangle2.Print();
    pDev->Present( 0, 0, 0, 0 );
}

int __stdcall WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
{
    InitWindow( 800, 600, "Okno" );
    InitDirect( true );
    MSG msg;
    while( Run )
    {
        if( PeekMessage( & msg, 0, 0U, 0U, PM_REMOVE ) )
        {
            if( msg.message == WM_DESTROY ) break;
           
            TranslateMessage( & msg );
            DispatchMessage( & msg );
        }
        else
        {
            Rendering();
            if( GetKeyState( VK_ESCAPE ) & 0x0800 )
            {
                Run = false;
            }
        }
    }
    pDev->Release();
    pD3D->Release();
}
DirectX.hpp:
C/C++
#ifndef DIRECTX_HPP
#define DIRECTX_HPP

#include <d3dx9.h>
#include <string>

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

const DWORD OURVERT_FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;

extern IDirect3D9 * pD3D;
extern IDirect3DDevice9 * pDev;
extern HWND hWnd;
extern IDirect3DVertexBuffer9 * pVB;

void InitWindow( int width, int height, std::string name );
void InitDirect( bool windowed );


struct OurVertex
{
    float x, y, z;
    float rhw;
    D3DCOLOR color;
};

#endif
DirectX.cpp:
C/C++
#include <DirectX.hpp>

using namespace std;

IDirect3D9 * pD3D;
IDirect3DDevice9 * pDev;
HWND hWnd;
IDirect3DVertexBuffer9 * pVB;

void InitWindow( int width, int height, string name )
{
    HINSTANCE hProg = GetModuleHandle( 0 );
    WNDCLASS wc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hProg;
    wc.lpfnWndProc = DefWindowProc;
    wc.lpszClassName = "WndClass";
    wc.lpszMenuName = 0;
    wc.hbrBackground =( HBRUSH ) GetStockObject( BLACK_BRUSH );
    wc.hIcon = LoadIcon( hProg, IDI_WINLOGO );
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.hCursor = LoadCursor( hProg, IDC_ARROW );
    RegisterClass( & wc );
    hWnd = CreateWindowEx( 0, "WndClass", name.c_str(), WS_OVERLAPPEDWINDOW,
    0, 0, width, height, 0, 0, hProg, 0 );
    ShowWindow( hWnd, 5 );
}

void InitDirect( bool windowed ) {
    pD3D = Direct3DCreate9( D3D_SDK_VERSION );
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( & d3dpp, sizeof( d3dpp ) );
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.Windowed = windowed;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, & d3dpp, & pDev );
}
Triangle.hpp:
C/C++
#ifndef TRIANGLE_HPP
#define TRAINGLE_HPP

#include <DirectX.hpp>

class Triangle {
public:
    OurVertex verts[ 3 ];
    Triangle( float x1, float y1, float z1, float rhw1, D3DCOLOR color1, float x2, float y2, float z2, float rhw2, D3DCOLOR color2, float x3, float y3, float z3, float rhw3, D3DCOLOR color3 );
    void Print();
};

#endif
Triangle.cpp:
C/C++
#include <Triangle.hpp>

Triangle::Triangle( float x1, float y1, float z1, float rhw1, D3DCOLOR color1, float x2, float y2, float z2, float rhw2, D3DCOLOR color2, float x3, float y3, float z3, float rhw3, D3DCOLOR color3 ) {
    verts[ 0 ].x = x1;
    verts[ 0 ].y = y1;
    verts[ 0 ].z = z1;
    verts[ 0 ].rhw = rhw1;
    verts[ 0 ].color = color1;
    verts[ 1 ].x = x2;
    verts[ 1 ].y = y2;
    verts[ 1 ].z = z2;
    verts[ 1 ].rhw = rhw2;
    verts[ 1 ].color = color2;
    verts[ 2 ].x = x3;
    verts[ 2 ].y = y3;
    verts[ 2 ].z = z3;
    verts[ 2 ].rhw = rhw3;
    verts[ 2 ].color = color3;
}

void Triangle::Print() {
    pDev->CreateVertexBuffer( sizeof( verts ), D3DUSAGE_DYNAMIC, OURVERT_FVF, D3DPOOL_DEFAULT, & pVB, 0 );
    void * data;
    pVB->Lock( 0, 3 * sizeof( OurVertex ), & data, D3DLOCK_DISCARD );
    memcpy( data,( void * ) verts, sizeof( verts ) );
    pVB->Unlock();
    pDev->BeginScene();
    pDev->SetFVF( OURVERT_FVF );
    pDev->SetStreamSource( 0, pVB, 0, sizeof( OurVertex ) );
    pDev->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
    pDev->EndScene();
}

Pliki ShowTriangle są we folderze z projektem, a DirectX i Triangle we folderze Include. Nie wiem dlaczego pojawiają się błędy:
C/C++
1 >------Build started: Project: ShowTriangle, Configuration: Debug Win32------
1 > Compiling...
1 > ShowTriangle.cpp
1 > Linking...
1 > ShowTriangle.obj: error LNK2019: unresolved external symbol "public: void __thiscall Triangle::Print(void)"( ? Print @ Triangle @ @ QAEXXZ ) referenced in function "void __cdecl Rendering(void)"( ? Rendering @ @ YAXXZ )
1 > ShowTriangle.obj
    : error LNK2019
    : unresolved external symbol "public: __thiscall Triangle::Triangle(float,float,float,float,unsigned long,float,float,float,float,unsigned long,float,float,float,float,unsigned long)"( ?? 0Triangle @ @ QAE @ MMMMKMMMMKMMMMK @ Z ) referenced in function "void __cdecl Rendering(void)"( ? Rendering @ @ YAXXZ ) 1 > ShowTriangle.obj
    : error LNK2001
    : unresolved external symbol "struct IDirect3DDevice9 * pDev"( ? pDev @ @ 3PAUIDirect3DDevice9 @ @ A ) 1 > ShowTriangle.obj
    : error LNK2001
    : unresolved external symbol "struct IDirect3D9 * pD3D"( ? pD3D @ @ 3PAUIDirect3D9 @ @ A ) 1 > ShowTriangle.obj
    : error LNK2019
    : unresolved external symbol "void __cdecl InitDirect(bool)"( ? InitDirect @ @ YAX_N @ Z ) referenced in function _WinMain @ 16 1 > ShowTriangle.obj
    : error LNK2019
    : unresolved external symbol "void __cdecl InitWindow(int,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)"( ? InitWindow @ @ YAXHHV ? $ basic_string @ DU ? $ char_traits @ D @ std @ @ V ? $ allocator @ D @ 2 @ @ std @ @ @ Z ) referenced in function _WinMain @ 16 1 > C:\D ocuments and Settings\User\Moje dokumenty\Visual Studio 2008\Projects\ShowTriangle\Debug\ShowTriangle.exe
    : fatal error LNK1120
    : 6 unresolved externals 1 > Build log was saved at "file://c:\Documents and Settings\User\Moje dokumenty\Visual Studio 2008\Projects\ShowTriangle\ShowTriangle\Debug\BuildLog.htm" 1 > ShowTriangle - 7 error( s )
    , 0 warning( s ) ========== Build
    : 0 succeeded
    , 1 failed
    , 0 up - to - date
    , 0 skipped ==========
P-10065
steckel
Temat założony przez niniejszego użytkownika
» 2009-09-05 15:46:26
Ale gdy dodaję pliki np. iostream lub string to nie muszę ich dodawać do projektu, aby wszystko działało. Co zrobić, aby tak samo działały pliki Triangle i DirectX?
P-10078
steckel
Temat założony przez niniejszego użytkownika
» 2009-09-05 16:08:31
Dzięki :)
P-10079
manfred
» 2009-09-05 11:15:28
Triangle.cpp i DirectX.cpp też musisz skompilować. Ciężko, żeby z jednego pliku kompilator miał wyczarować definicje z innych plików, nie?
P-19275
manfred
» 2009-09-05 15:50:25
Bo tamte cholerstwa są header-only (albo, jako że to nagłówki standardowe, stosowana jest do nich jakaś magia). Twoje nie są, więc dodaj je do projektu, albo zrób z nich header-only.
P-19279
« 1 »
  Strona 1 z 1