Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 908191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:40:38+00:00 2026-05-15T16:40:38+00:00

time for another off the wall question. I am writing an MD2 loader for

  • 0

time for another off the wall question. I am writing an MD2 loader for my small 3D engine project. In my old language (C) I could define a structure and then read() from an open file directly into the structure. I have a structure to hold the header information from the MD2 file, as follows:

[StructLayout(LayoutKind.Sequential)]
public struct MD2_Header
{
    public int FourCC;
    public int Version;
    public int TextureWidth;
    public int TextureHeight;
    public int FrameSizeInBytes;
    public int NbrTextures;
    public int NbrVertices;
    public int NbrTextureCoords;
    public int NbrTriangles;
    public int NbrOpenGLCmds;
    public int NbrFrames;
    public int TextureOffset;
    public int TexCoordOffset;
    public int TriangleOffset;
    public int FrameOffset;
    public int OpenGLCmdOffset;
    public int EndOffset;
}

In my reader code, I would like to do something like:

// Suck the MD2 header into a structure, it is 68 bytes long.
Classic.Util.MD2_Header md2hdr = new Classic.Util.MD2_Header();
md2hdr = reader.ReadBytes(sizeof(Classic.Util.MD2_Header));

I realize this is not correct, as it breaks type safety somewhat oddly, but you get the idea of what I want to accomplish. I could do this with separate calls to reader.ReadInt32(), but I am curious if there is anyway to get this to work the way I am wanting using normal library calls.

I have looked a little into the Marshal.Copy() method, but it seems to be for going between managed and unmanaged memory, which is not really what I am doing here.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:40:39+00:00Added an answer on May 15, 2026 at 4:40 pm

    A structure in C and a structure in C# are two completely different things. A structure in C is used both for value types and reference types, while a structure in C# is only used for value types.

    A value type should represent a single value, but what you have is plenty of values, so you should use a class instead. The recommended maximum size for a structure in .NET is 16 bytes, and you have more than four times as much data.

    A class with properties and a constructor that takes a byte array would look like this:

    public class MD2_Header {
    
      public int FourCC { get; set; }
      public int Version { get; set; };
      public int TextureWidth { get; set; };
      public int TextureHeight { get; set; };
      public int FrameSizeInBytes { get; set; };
      public int NbrTextures { get; set; };
      public int NbrVertices { get; set; };
      public int NbrTextureCoords { get; set; };
      public int NbrTriangles { get; set; };
      public int NbrOpenGLCmds { get; set; };
      public int NbrFrames { get; set; };
      public int TextureOffset { get; set; };
      public int TexCoordOffset { get; set; };
      public int TriangleOffset { get; set; };
      public int FrameOffset { get; set; };
      public int OpenGLCmdOffset { get; set; };
      public int EndOffset { get; set; };
    
      public MD2_Header(byte[] values) {
        FourCC = BitConverter.ToInt32(values, 0);
        Version = BitConverter.ToInt32(values, 4);
        TextureWidth = BitConverter.ToInt32(values, 8);
        TextureHeight = BitConverter.ToInt32(values, 12);
        FrameSizeInBytes = BitConverter.ToInt32(values, 16);
        NbrTextures = BitConverter.ToInt32(values, 20);
        NbrVertices = BitConverter.ToInt32(values, 24);
        NbrTextureCoords = BitConverter.ToInt32(values, 28);
        NbrTriangels = BitConverter.ToInt32(values, 32);
        NbrOpenGLCmds = BitConverter.ToInt32(values, 36);
        NbrFrames = BitConverter.ToInt32(values, 40);
        TextureOffset = BitConverter.ToInt32(values, 44);
        TexCoordOffset = BitConverter.ToInt32(values, 48);
        TriangleOffset = BitConverter.ToInt32(values, 52);
        FrameOffset = BitConverter.ToInt32(values, 56);
        OpenGLCmdOffset = BitConverter.ToInt32(values, 60);
        EndOffset = BitConverter.ToInt32(values, 64);
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Piggy backing off another question I posted , I have a complex find() that
At run time I want to dynamically build grid columns (or another display layout)
Every time I create a new project I copy the last project's ant file
Every time I have to estimate time for a project (or review someone else's
Every time I turn on my company-owned development machine, I have to kill 10+
From time to time I see an enum like the following: [Flags] public enum
Every time I create an object that has a collection property I go back
Every time I need to work with date and/or timstamps in Java I always
From time to time I get a System.Threading.ThreadStateException when attempting to restart a thread.
Every time a user posts something containing < or > in a page in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.