Author Topic: [c#] How to get an array with stuct to work ?  (Read 3959 times)

0 Members and 1 Guest are viewing this topic.

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
[c#] How to get an array with stuct to work ?
« on: February 26, 2011 »
I think its must be possible, but i am not really sure how to use/write it in C#. What i need is an array where i can read/write RGBA values and just direct access to RED, GREEN, BLUE, ALPHA.

Code: [Select]
        // Init

        public struct ARGB  // RGBA or ARGB ^^
        {
            public byte A;
            public byte R;
            public byte G;
            public byte B;
        }

        public uint[] arTexture;

        // Methode

        public void TestStructArray()
        {
            arTexture = new uint[64 * 64];
        }

Later something like following should be possible:

Code: [Select]
// Set (byte) value for any color channel
arTexture[ position ].A = 255
arTexture[ position ].R = 255
arTexture[ position ].G = 255
arTexture[ position ].B = 255

// Set (uint) value for ARGB pixel value
arTexture[ position ].ARGB = 0xFF224466

// Get (byte) value of any color channel
valueA = arTexture[ position ].A
valueR = arTexture[ position ].R
valueG = arTexture[ position ].G
valueB = arTexture[ position ].B

ValueARGB = arTexture[ position ].RGBA

Any idea how this can be done (array with struct)?  And yes, i know this can be done by using C#'s COLOR... But i want to use my own for some reasons ;)

[Edited - Added:]
Atm i am using an uint[] where i read/write the full pixelcolor as ARGB and using SHL/SHR operators to get/set the value of the wanted color channel! This works but i want to have a cleaner code and being more flexible.
« Last Edit: February 26, 2011 by va!n »
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [c#] How to get an array with stuct to work ?
« Reply #1 on: February 26, 2011 »
This technique can't work in C#.  You are not allowed to know how a struct is represented, and you can't overlay two data structures upon each other.  You can get the semantics you want, like Color, but then you can never turn that into an array efficiently.

Your best bet is to work directly with your array of uints.  And it will be interesting to see how efficient it is to get them on a bitmapped display.  I've tried pixel stuff in C# and have some tips, but my results were 15fps-ish for 640x480.

Jim
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: [c#] How to get an array with stuct to work ?
« Reply #2 on: February 26, 2011 »
Hmmm... so its not possible in C#? I have to think about... Using uint for my array and shifting around to set/get a  channel color works well..

Yes drawing in C# using GDI/GDI+ is really slow. However i tried from the first second to do all stuff in a big array and bringing the result very fast to a bitmap/on screen! I found a way to draw the array result very fast - a lot faster as C# drawings! From performance issue i have no problem yet. Just only thought how to have cleaner code and....

thanks for your info...


[Edited - Added:]
In one of my very firsts tests i tried to draw and generate 5 different simple textures (each 256x256)... Needed time:  ~ 20 ms (i think this is great)
« Last Edit: February 26, 2011 by va!n »
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won: