Thread: Spliting in group of two

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Question Spliting in group of two

    Hello. I have this function written in C, and I have problems with it. Do not look at what you do not understand, it's for the PSP but I will explain what it does.
    In the current state yhe function makes starting at 0x00000000 ABGR color format and reach 0x00000FF, then repeat the whole process. Like:

    0x00000000 -> 0x000000FF, black -> full red

    then it will go from:
    0x00000100 -> 0x000001FF, black with a hint of green -> red with a hint of green

    then:
    0x00000200 -> 0x000002FF and so on.

    Code:
        void _ColorRaiseByOne()
        {   
           int ABGR = 0x00000000;
           for(;;ABGR++)
           {
           SceCtrlData pad;
           sceCtrlReadBufferPositive(&pad,1);
           pspDebugScreenClear();
           pspDebugScreenSetBackColor(ABGR);
           if (pad.Buttons & PSP_CTRL_TRIANGLE)
              {
              sceKernelExitGame();                
              }   
           }   
        }
    I want to make it to go like;
    0x00000000 ->0x000000FF
    then

    0x000000FF ->0x0000FFFF
    then

    0x0000FFFF ->0x00FFFFFF
    then

    0x00FFFFFF ->0xFFFFFFFF.

    I've tried with no success in this mode:
    Code:
    void _ColorRaiseByOne()
    {
        int ABGR = 0x00000000;
        for (int A=0x00; A<=0xFF; A++)
        {
            for (int B=0x00; B<=0xFF; B++)
            {
                for (int G=0x00; G<=0xFF; G++)
                {
                    for (int R=0x00; R<=0xFF; R++)
                    {
                        ABGR = A+B+G+R;
                    }
                }
            }
        }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    All you are doing is simply incrementing ABGR. Skip your quadruply nested for loop and do:

    Code:
    int ABGR;
    
    for (ABGR = 0x00000000; ABGR <= 0xFFFFFFFF; ABGR++) {
        ...
    }

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    I already did this thing, in first example, by writing
    Code:
    int ABGR = 0x00000000;
           for(;;ABGR++)
           {
                ....
           }
    The problem is that I want it not just to increment by one. I want a special increment. Like I said before, I want that when it has reached 0x000000FF, to block that last 2 numbers to FF and to increment just the other ones, until two by two reach FF.
    It is like splitting up ABGR in A, B, G and R. When R has reached FF it must remain at FF, and after increment G from 00 to FF. Now, G must remain to FF and B must be incremented until it reaches FF. And finaly, after G has reached FF, A must be incremented from 00 to FF.
    At the end It will become 0xFFFFFFFF. I don't know if you understand what I'm trying...
    Last edited by RisingACK; 11-10-2010 at 05:19 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I take it you have a dword where you wish to increment individual bytes within the dword.

    One possible way of doing this is to use a union...
    Code:
    // define union as global type
    typedef union tColorControl
      { uint_32t Color;
         struct tByte
           { uint8_t A;         // note: these may not be in the
             uint8_t B;         //           correct order
             uint8_t G;
             uint8_t R; }
            Byte; }
       ColorControl, *ColorControl;
    
    
    // inside your procedure
    ColorControl abgr; 
    StartValue = 0;  // or whatever you need
    
    // assign initial value...
    abgr.Color  = StartValue;
    
    // manipulate by bytes
    While (abgr.Byte.R < 0xFF)
      { abgr.Byte.R++;
          SetColor(abgr.Color);  // external color setting call
         delay(1000); }
    
    While (abgr.Byte.G < 0xFF)
      { abgr.Byte.G++;
          SetColor(abgr.Color); 
         delay(1000); }
    
    While (abgr.Byte.B < 0xFF)
      { abgr.Byte.B++;
          SetColor(abgr.Color);  
         delay(1000); }
    
    While (abgr.Byte.A < 0xFF)
      { abgr.Byte.A++;
          SetColor(abgr.Color); 
         delay(1000); }
    Last edited by CommonTater; 11-10-2010 at 06:45 AM.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I think I get what you're trying to do. Try adapting this code to your use.
    Code:
    #include <stdio.h>
    
    
    struct RGBA
    {
        unsigned int R : 8;
        unsigned int G : 8;
        unsigned int B : 8;
        unsigned int A : 8;
    };
    
    
    unsigned
    to_unsigned( const struct RGBA* color );
    
    
    int
    main( void )
    {
        struct RGBA color = {0, 0xFF, 0, 0xFF};       // R = 0x00, G = 0xFF, B = 0x00, A = 0xFF
    
        unsigned final = to_unsigned( &color );     // Pack into a single uint.
        fprintf( stdout, "%#0X\n", final );     // Output: 0XFF00FF00
    
        return 0;
    }
    
    // Pack it ABGR; e.g. A in MSB, R in LSB.
    unsigned
    to_unsigned( const struct RGBA* color )
    {
        unsigned packed_rgba = 0;
        packed_rgba |= color->A;
        packed_rgba <<= 8;
        packed_rgba |= color->B;
        packed_rgba <<= 8;
        packed_rgba |= color->G;
        packed_rgba <<= 8;
        packed_rgba |= color->R;
        
        return packed_rgba;
    }
    You can increment and decrement individual RGBA fields as needed. There is likely a better way to implement this, but this is what I came up.

    If you have questions, feel free to ask them. If anyone feels like correcting my quick hack, please do. I will be grateful.

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I should have used exact-width types, like CommonTater did. Oh well.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    For the second pattern in the OP, this is a simple way to do it :

    Code:
    void ColorRaiseByOne()
    {
        unsigned ABGR = 0;
        
        for (int shift = 0; shift < 32; shift += 8)
            for (int i = 0; i < 0xFF; i++)
                ABGR += 1 << shift;
    }
    Drop in calls to PSP...whatever function as needed once per iteration of the inner loop.

    For various obscure reasons, I'd also avoid starting a function name with an underscore.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting whit MPI
    By isato in forum C Programming
    Replies: 0
    Last Post: 03-03-2009, 10:38 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. ray casting
    By lambs4 in forum Game Programming
    Replies: 62
    Last Post: 01-09-2003, 06:57 PM
  4. Group
    By knight543 in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2002, 02:16 PM