Transcript Document

Java2C#
Antonio Cisternino
Part V
Outline
Assembly & co.
 Visual Studio environment
 Example: using DirectX from C#

Outline
 Assembly
& co.
Visual Studio environment
 Example: using DirectX from C#

Code packaging in Java




Java classes are compiled into bytecode
Each class C definition resides into a separate
file named C.class
Inner classes are stored in files whose names
are formed by appending the class names
separated with $ and the extension .class
Jar files are ZIP files! Inside there are .class files
and possibly a manifest with information about
the package (i.e. the main class)
Code packaging in .NET





.NET adopts a coarser grain packagin model
w.r.t. Java
Code is deployed in assemblies
An assembly is a file whose format is based on
Windows PE
Inside an assembly there are one or more
modules containing code
Each code module may contain native code as
well as CIL
Assemblies




The special case for CLI files is defined within
the standard: it is platform independent
A CLI section of PE is formed by several tables
describing one or more types: data (code) and
metadata
An assembly could be signed and has a version
Signature and version are controlled using
assemblies custom attributes
Assembly’s metadata

General information:
[assembly:
[assembly:
[assembly:
[assembly:
[assembly:
[assembly:
[assembly:
[assembly:

AssemblyTitle("Direct3D Sample")]
AssemblyDescription("Example of using Direct3D 8 from C#")]
AssemblyConfiguration("")]
AssemblyCompany("Antonio Cisternino")]
AssemblyProduct("")]
AssemblyCopyright("")]
AssemblyTrademark("")]
AssemblyCulture("Neutral")]
Version information:
[assembly: AssemblyVersion("1.0.*")] // Major.Minor.Build.Revision

Assembly signing:
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyName("")]
CLR and Assemblies



Execution Engine relies on assemblies to find and load types
Assemblies can be precompiled to improve load time (ngen tool)
There is no CLASSPATH, assemblies are searched as follows:

Determines the correct assembly version by examining applicable configuration
files, including the application configuration file, publisher policy file, and machine
configuration file. If the configuration file is located on a remote machine, the
runtime must locate and download the application configuration file first.
 Checks whether the assembly name has been bound to beforeand, if so, uses
the previously loaded assembly.
 Checks the global assembly cache. If the assembly is found there, the runtime
uses this assembly.
 Probes for the assembly using the following steps:



If configuration and publisher policy do not affect the original reference and if the bind
request was created using the Assembly.LoadFrom method, the runtime checks for
location hints.
If a codebase is found in the configuration files, the runtime checks only this location. If
this probe fails, the runtime determines that the binding request failed and no other
probing occurs.
Probes for the assembly using the heuristics described in the probing section. If the
assembly is not found after probing, the runtime requests the Windows Installer to
provide the assembly. This acts as an install-on-demand feature.
Assembly cache




CLR relies on two caches: the global assembly cache
(GAC) and the native image cache that contains precompiled assemblies
.NET Framework ships with utilities to manage these
caches (gacutil and ngen)
Cache and assembly-level loading enable a wide range
of JIT optimization techniques
Tradeoffs are:



Disk space
Load time
Execution time
Outline

Assembly & co.
 Visual

Studio environment
Example: using DirectX from C#
Visual Studio 7





Visual Studio is becoming an open development
environment
The .NET version can be extended with a (near
to) public interface called Babel
Each language has a plugin that handle with
syntax checking, intellisense, etc.
It supports outlining: code becomes structured
and code navigation becomes easier
The debugger is integrated into the environment
and is multi-language
Demo
Outline
Assembly & co.
 Visual Studio environment

 Example:

using DirectX from C#
Example: Web services
DirectX




DirectX is an API that exposes direct access to
peripherals from programs
It is widely used to game programming due to a
significant improvement of performances
There are several modules to cope with 3D
graphics, 2D drawing, input, sound and so on
It is a C++ library accessible from VB and .NET
through a COM component
Accessing DirectX from C#





To access DirectX from C# the only way is to
use interoperability
The COM component is called
DIRECT.DirectX8.0 and has a type-library
Using tlbimp it is possible to get a .NET
assembly that exposes all the interfaces of that
component
The C# application uses the output as DirectX
library
Unsafe code is needed to be able to call certain
interface methods!
Initialization
DX8 = new DirectX8Class();
D3D = DX8.Direct3DCreate();
D3DDISPLAYMODE mode = new D3DDISPLAYMODE();
D3D.GetAdapterDisplayMode((int)CONST_D3DCONST.D3DADAPTER_D
EFAULT, ref mode);
D3DPRESENT_PARAMETERS d3dpp = new D3DPRESENT_PARAMETERS();
d3dpp.Windowed = 1;
d3dpp.SwapEffect =
CONST_D3DSWAPEFFECT.D3DSWAPEFFECT_COPY_VSYNC;
d3dpp.BackBufferFormat = mode.Format;
D3DDevice =
D3D.CreateDevice((int)CONST_D3DCONST.D3DADAPTER_DEFAULT,
CONST_D3DDEVTYPE.D3DDEVTYPE_HAL, this.Handle.ToInt32(),
CONST_D3DCREATEFLAGS.D3DCREATE_SOFTWARE_VERTEXPROCESSING
, ref d3dpp);
Custom vertices


Direct3D allows a flexible vertex format that can be controlled using
bitmasks
In our case:
struct CUSTOM_VERTEX {
public float x;
public float y;
public float z;
public float rhw;
public uint color;
public CUSTOM_VERTEX(float xx, float yy, float zz,
float r, uint c) {
x = xx; y = yy; z = zz; rhw = r; color = c;
}
}
Rendering of two triangles
protected override void OnPaint(PaintEventArgs e) {
unsafe {
CUSTOM_VERTEX[] vertices = new CUSTOM_VERTEX[]{
new CUSTOM_VERTEX(10, 10, 0.5f, 1, 0xffff0000), …};
int sizeOfVertices = sizeof(CUSTOM_VERTEX) * vertices.Length;
Direct3DVertexBuffer8 vb;
vb = D3DDevice.CreateVertexBuffer(sizeOfVertices, 0,
(int)(CONST_D3DFVFFLAGS.D3DFVF_XYZRHW | CONST_D3DFVFFLAGS.D3DFVF_DIFFUSE),
CONST_D3DPOOL.D3DPOOL_DEFAULT);
fixed (void* v = vertices) {
int pData;
byte* src = (byte*)v, dest;
vb.Lock(0, sizeOfVertices, out pData, 0);
dest = (byte*)pData;
for (int i = 0; i < sizeOfVertices; i++) *dest++ = *src++;
}
D3DDevice.Clear(0, new IntPtr(0), CONST_D3DCLEARFLAGS.D3DCLEAR_TARGET,
unchecked((int)0xff666699), 1.0f, 0);
D3DDevice.BeginScene();
D3DDevice.SetStreamSource(0, vb, sizeof(CUSTOM_VERTEX));
D3DDevice.SetVertexShader((int)(CONST_D3DFVFFLAGS.D3DFVF_XYZRHW |
CONST_D3DFVFFLAGS.D3DFVF_DIFFUSE));
D3DDevice.DrawPrimitive(CONST_D3DPRIMITIVETYPE.D3DPT_TRIANGLELIST, 0, 2);
D3DDevice.EndScene();
D3DDevice.Present(new IntPtr(0), new IntPtr(0), 0, new IntPtr(0));
}}
Demo
Next Lecture
Web services
 Framework SDK:

 Compilers
 Tools
…