Jump to content
  • Advertisement
Sign in to follow this  
Structural

C# - application wide constants, low level buffers and graphics

This topic is 4886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Three questions in one thread here. I started using C# recently, and there are a few issues I can't wrap my head around yet. 1) application wide constants I'm used to C++, and whenever I had a constant I wanted to use throughout the application I used a #define. C# doesn't allow this and I'm kind of lost on how to accomplish this now as I can't define anything inside the namespace either. 2) low level buffer access I'm making a network application and I receive my data in a byte buffer. In C++ I'd lay a structure over the buffer (see below) in order to access the seperate values.
struct Protocol
{
  int a;
  int b;
};

func()
{
  Protocol* p;
  int value1, value2;
  char* buffer = getNetworkData();
 
  p = (Protocol*) buffer; // or something alike

  value1 = p->a;
  value2 = p->b;
}
Now, I know you can use pointers, but that makes your code unmanaged or something. How would I do this in C# without making my code unmanaged? 3) Graphics The app I'm writing has next to a network connection a window that should draw some graphics next to the regular buttons and checkboxes. Nothing fancy really (a graph). I'm considering using GDI+ for this, but people are telling me it's slow. I haven't done any real tests so it might not be as worse as people are telling me. DirectX requires some installation of some extra framework functionality I have been told, which is not acceptable. I hope you can provide me with some answers. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Question 1:

public class Whatever
{
// Use as Whatever.One
public const int One = 1;
}

public class ThisIsASillyClass
{
public static int OnePlusX(int x)
{
// Example
return Whatever.One + x;
}
}



Question 2:
I'm not sure how to do this without using unmanaged memory either. Marshal.Copy() is the unmanaged solution though.

Question 3:
GDI+ will be more than fine to draw a graph. You can even make small 2D games with it at an acceptable rate. People rag on GDI because they're ignorant.

Share this post


Link to post
Share on other sites
Thank you for your input.
I can think of a bit of a work-around for the buffer/structure problem involving fetching every separate byte and multiplying them and constructing integers that way, but it won't be pretty.
A better solution would be very welcome.

Share this post


Link to post
Share on other sites
I also ran across problem #2 and I found no solution. I created my own serializer classes that simply convert between classes and byte arrays to solve it.

I guess Marshal.Copy or perhaps some unsafe mode pointer magic might be easier to do. I haven't really messed around with these two.

Share this post


Link to post
Share on other sites
Regarding your 2nd question:

Recently I had a similar problem: my solution was like this:


[StructLayout(LayoutKind.Sequential)]
public struct BSPTexture
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public char[] Name;
public int Flags;
public int Contents;
}

//here the code to load data into it:
textures = (BSPTexture) ReadRawData( binaryReader, textures.GetType() );

//which uses this function:
private static object ReadRawData( BinaryReader binaryReader, Type type )
{
int rawsize = Marshal.SizeOf( type );

byte[] rawdata = binaryReader.ReadBytes( Marshal.SizeOf( type ));

if( rawsize > rawdata.Length )
return null;

// allocate unmanaged memory
IntPtr buffer = Marshal.AllocHGlobal( rawsize );

// copy the data into the allocated unmanaged memory
Marshal.Copy( rawdata, 0, buffer, rawsize );

// create pointer to the memory
object obj = Marshal.PtrToStructure( buffer, type );

// free unmanged memory
Marshal.FreeHGlobal( buffer );

return obj;
}



Hope it helps!
Constantin

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!