C# C++ È¥ÇÕ ÇÁ·Î±×·¡¹Ö 4Àå

8. C#¿¡¼­ Native ´ÙÀÌ·ºÆ® X È£Ãâ dx_wrapping.zip

´ÙÀÌ·ºÆ® X µð¹ÙÀ̽º¸¦ »ý¼ºÇؼ­ ÆĶõ È­¸éÀ» º¸¿©ÁÖ´Â °£´ÜÇÑ ÇÁ·Î±×·¥À» ¸¸µé¾î º¸ÀÚ !!!

Native ´ÙÀÌ·ºÆ® X¸¦ È£Ãâ ÇÒ·Á¸é µð¹ÙÀ̽º³ª ÇÚµéÀÇ °ªÀ» C#°ú ¾î¶»°Ô Åë½Å ÇÒ°ÍÀÎÁö °í¹Î ÇÏ°Ô µÇ¾ú´Ù.
System.IntPtrÀ» ÀÌ¿ë Çϸé Åë½ÅÀÌ °¡´ÉÇÏ´Ù´Â °ÍÀ» ¾Ë°Ô µÇ¾ú´Ù.

Æ÷ÀÎÅͳª ÇÚµéÀº IntPtr ÀÌ¿ëÇØ ¹Þ±â

System.IntPtrÀ» »ç¿ëÇϸé Æ÷ÀÎÅͳª ÇÚµéÀ» ³×ÀÌƼºê·Î º¸³»°Å³ª ¹ÞÀ»¼ö ÀÖ´Ù.
C#¿¡¼­ DllImport¸¦ »ç¿ëÇؼ­ À©µµ¿ìÁî API ÇÔ¼ö¸¦ È£ÃâÇÒ ¶§, IntPtrÀ» º»ÀûÀÌ ÀÖÀ» °ÍÀÌ´Ù.

[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

IntPtr.ToPointer()¸¦ ÀÌ¿ëÇؼ­ C# FormÀÇ À©µµ¿ì ÇÚµéÀ» Native C++·Î ³Ñ°ÜÁÖ´Â ¿¹´Â ¸¹ÀÌ ÀÖÁö¸¸,
Nativ C++¿¡¼­ ,C#À¸·Î ³Ñ°ÜÁÖ´Â À©µµ¿ì ÇÚµéÀ̳ª Æ÷ÀÎÅÍÀÇ Á¤È®ÇÑ ¹®¼­´Â ãÀ»¼ö°¡ ¾ø¾ú´Ù.
DllImport °æ¿ì¸¦ »ý°¢Çؼ­ ¾Æ·¡ ¿¹Á¦¸¦ ¸¸µé¾î º¸¸é "hWnd", "IPtr", "window" 3°³ÀÇ °ªÀº ¸ðµÎ ¶È°°´Ù.

    HWND hWnd = ::FindWindow("Notepad", "Á¦¸ñ ¾øÀ½ - ¸Þ¸ðÀå");

    IntPtr iPtr = (IntPtr)hWnd;

    HWND window = (HWND)iPtr.ToPointer();

 

ÇÁ·Î±×·¥¿¡ »ç¿ëµÇ´Â ÆÄÀϵéÀº ´ÙÀ½°ú °°´Ù.

Unmanaged C++ graphics.cpp, graphics.h

CGraphics Ŭ·¡½º

´ÙÀÌ·ºÆ® X µð¹ÙÀ̽º¸¦ »ý¼ºÇؼ­ ºóÈ­¸éÀ» ·»´õ¸µ
Managed C++ mgraphics.cpp MGraphics Ŭ·¡½º CGraphics Ŭ·¡½º ·¦ÇÎ
C# dxmain.cs DXMain Ŭ·¡½º MGraphics Ŭ·¡½º¸¦ ÂüÁ¶ÇÏ¿© ·»´õ¸µÇÑ´Ù.

¾ÕÀå°ú´Â ´Ù¸£°Ô Unmanaged C++°ú Managed C++À» º°µµÀÇ DLL·Î ¸¸µéÁö ¾Ê°í graphics.dll Çϳª·Î ¸¸µç´Ù.

CGraphics Ŭ·¡½º´Â ¾ÐÃà ÆÄÀÏÀÇ ¼Ò½º¸¦ Âü°íÇÏ°í, MGraphics Ŭ·¡½º´Â ¸®½ºÆ®¸¸ º¸¿©ÁÙ°ÍÀÌ´Ù.
¼³¸íÀº C# ºÎºÐ¸¸ ÀÚ¼¼ÇÏ°Ô ÇÑ´Ù.

¾ÆÁ÷ C#¿¡ Àͼ÷ÇÏÁö ¾Ê±â ¶§¹®¿¡ Ʋ¸° ¼³¸íµµ ÀÖÀ» °ÍÀÌ´Ù.

MGraphics Ŭ·¡½º´Â ´ÙÀÌ·ºÆ® X µð¹ÙÀ̽º¸¦ »ý¼º, ·»´õ¸µ, ¼Ò¸êÀ» ·¦ÇÎ ÇÏ¿´´Ù.
»ý¼º, ·»´õ¸µ, ¼Ò¸êÀ» DXMain Ŭ·¡½º ¾îµð¼­ ÇØÁÙ°ÇÁö ¾Ë¸é ¹®Á¦´Â ½±°Ô Ç®¼ö ÀÖ´Ù.

»ý¼º: WinFormÀÌ È­¸é¿¡ Ãâ·ÂµÇ±â Àü¿¡ Load À̺¥Æ® ¹ß»ý

·»´õ¸µ: WinForm À̺¥Æ® ¾øÀ» ¶§ Idle À̺¥Æ® ¹ß»ý

¼Ò¸ê: WinFormÀ» ´ÝÀ» ¶§ Closed À̺¥Æ® ¹ß»ý

À̺¥Æ®¸¦ »ç¿ë ÇÒ·Á¸é À̺¥Æ®¸¦ »ý¼ºÀÚ¿¡ Ãß°¡ÇÑ´Ù.

À̺¥Æ® µî·Ï:

    public DXMain(string strText)

    {

        this.Text = strText;

        this.Load += new System.EventHandler(this.Form_Load);

        this.Closed += new System.EventHandler(this.Form_Closed);

        Application.Idle += new System.EventHandler(this.Form_Idle);

        //InitializeComponent();

        this.Show();

    }

»ý¼º :

    private void Form_Load(object sender, System.EventArgs e)

    {

        dxRender = new MGraphics();

        dxRender.Create(this.Handle);

        dxRender.SetupCamera();

    }

·»´õ¸µ :

    private void Form_Idle(object sender, System.EventArgs e)

    {

        dxRender.BeginScene();

        dxRender.EndScene();

    }

¼Ò¸ê :

    private void Form_Closed(object sender, System.EventArgs e)

    {

        dxRender.Cleanup();

    }

 

//mgraphis.cpp

#include <windows.h>

#using <mscorlib.dll>

using namespace System;

 

#include "graphics.h"

 

public __gc class MGraphics

{

public:

    MGraphics()

    {

        m_pUnmanagedGraphics = new CGraphics();

    }

    ~MGraphics()

    {

        delete m_pUnmanagedGraphics;

    }

 

public:

    bool Create(IntPtr windowHandle)

    {

        HWND hWnd = (HWND)windowHandle.ToPointer();

        RECT rect;

        GetClientRect(hWnd, &rect);

        return m_pUnmanagedGraphics->Create(hWnd, rect.right - rect.left, rect.bottom - rect.top, false);

    }

 

    void   Cleanup()

    {

        m_pUnmanagedGraphics->Cleanup();

    }

 

    void    SetupCamera()

    {

        m_pUnmanagedGraphics->SetupCamera();

    }

    void    BeginScene()

    {

        m_pUnmanagedGraphics->BeginScene();

    }

    IntPtr  EndScene()

    {

        return m_pUnmanagedGraphics->EndScene();

    }

 

private:

    CGraphics * m_pUnmanagedGraphics;

};

 

//dxmain.cs

using System;

using System.Windows.Forms;

 

 

public class DXMain : Form

{

    private MGraphics dxRender = null;

 

    public DXMain(string strText)

    {

        this.Text = strText;

        this.Load += new System.EventHandler(this.Form_Load);

        this.Closed += new System.EventHandler(this.Form_Closed);

        Application.Idle += new System.EventHandler(this.Form_Idle);

        //InitializeComponent();

        this.Show();

    }

 

    [STAThread]

    static void Main()

    {

        Application.Run(new DXMain("DirectX Test"));

    }

 

    private void Form_Load(object sender, System.EventArgs e)

    {

        dxRender = new MGraphics();

        dxRender.Create(this.Handle);

        dxRender.SetupCamera();

    }

 

    private void Form_Closed(object sender, System.EventArgs e)

    {

        dxRender.Cleanup();

    }

 

    private void Form_Idle(object sender, System.EventArgs e)

    {

        dxRender.BeginScene();

        dxRender.EndScene();

    }

/*

    protected override void OnPaint(PaintEventArgs e)

    {

        Form_Idle(this, e);

    }

 

    protected override void OnPaintBackground(PaintEventArgs e)

    {

        Form_Idle(this, e);

    }

*/   

}

[Æß]http://www.gamedev.net/community/forums/topic.asp?topic_id=474894

[Æß]Game Engine Toolset Development  (ÃâÆÇ»ç: Course Technology)
www.courseptr.com/downloads/bonus/009638_BonusCh02.pdf