[App Release] Kraken's Awareness

Discussion in 'Razer Chroma' started by TheFranubian, Nov 22, 2015.

Thread Status:
Not open for further replies.
  1. TheFranubian

    TheFranubian Active Member

    [​IMG]

    Hello, with the help of the amazing WolfSpiritM and njbmartin I have managed to adapt a couple of lines of code, following their indications, to the original Razer Chroma SDK Sample Development executable.


    Download the .exe file in that link, and start it.

    Under the Headset tab, you will find a new option, called "Ambient Effect."

    Click on it, start it, and your Kraken 7.1 Chroma will now be able to adapt to the colors on screen, transitioning smoothly every second according to the average color of the screen, no matter the game or application (as long as it runs windowed or fullscreen windowed).

    Video demo:







    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////////////
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////////////

    • Experimental Instant/Blinking version. Forced the software to display an empty effect between refreshes to make the lights follow the screen way faster. Take into account that colors will also be darker to the human eye because of this blank frame being thrown every other refresh. Also, performance may be affected in demanding games.
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////////////
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////////////​



    Again, thank everybody and specially WolfSpiritM for making the coding so much more smoother and njbmartin for his support as well.
     
    Last edited: May 8, 2016
  2. TheFranubian

    TheFranubian Active Member

    Just checked such devices, they would be the Kraken 7.1 Chroma and the DeathAdder Chroma.
     
  3. njbmartin

    njbmartin Developer of Awesome

    Absolutely! Here's an example showing reactive

     
  4. TheFranubian

    TheFranubian Active Member

    Alright, thanks!

    I want to learn enough of the SDK to bring at least these two modes for the Kraken and DeathAdder. To get somewhat familiar with it, I have been trying to get the Ambient Effect in the SDK sample to work with single LED devices like the headset.

    My idea is to create a hBitmap value from the average color of each screen frame.

    I have added the option in the UI, and it does work when I click the Stop Ambient Effect, so I assume I have managed to make it stream data to the headset.

    "Start Ambient Effect" won't work, so I assume it is because I have not yet found the correct code to make the mode actually provide data other than the "stop" command above.

    Here's the original code of the Ambient Effect for the other devices, with my added, novice, pathetic attempt to add the effect to the Headset (device code = 4). How could I make it work with the STATIC or ALERT effects, or would it be necessary to create a custom one?

    I supposed that, by replacing COLUMN and ROW_MAX values with CHROMA_STATIC it would automatically reduce the hbitmap pool into a 1x1 color to use with the single LEDs.

    Sorry for any inconveniences, no need to answer to this particular issue since I think it would technically be off-topic.

    Code:
    DWORD WINAPI _CreateAmbientFx1(LPVOID lpParameter)
    {
        UINT DeiveType = (UINT)lpParameter;
    
        CChromaSDKImpl* pChromaSDK = (CChromaSDKImpl*)lpParameter;
    
        // Get the device context of the screen
        HDC hScreenDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
        // and a device context to put it in
        HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
    
        // Get width and height of primary display
        int width = GetDeviceCaps(hScreenDC, HORZRES);
        int height = GetDeviceCaps(hScreenDC, VERTRES);
    
        // Capture an image from the primary display
        HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
    
        while(TRUE)
        {
            HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
    
            // Specify the resize mode.
            ::SetStretchBltMode(hMemoryDC, HALFTONE);
    
            // Copy and resize the image to a memory buffer
            if(DeiveType == 1)
            {
                StretchBlt(hMemoryDC, 0, 0, ChromaSDK::Keyboard::MAX_COLUMN, ChromaSDK::Keyboard::MAX_ROW,
                        hScreenDC, 0, height, width, -height,
                        SRCCOPY);
            }
            else if(DeiveType == 5)
            {
                StretchBlt(hMemoryDC, 0, 0, ChromaSDK::Keypad::MAX_COLUMN, ChromaSDK::Keypad::MAX_ROW,
                        hScreenDC, 0, height, width, -height,
                        SRCCOPY);
            }
            else if(DeiveType == 4)
            {
                StretchBlt(hMemoryDC, 0, 0, ChromaSDK::Headset::CHROMA_STATIC, ChromaSDK::Headset::CHROMA_STATIC,
                    hScreenDC, 0, height, width, -height,
                    SRCCOPY);
            }
    
            // Select the bitmap into the device context.
            hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
    
            // Display the image
            pChromaSDK->ShowBitmap(DeiveType, hBitmap);
    
            Sleep(5);
        }
     
  5. WolfspiritM

    WolfspiritM Active Member

    Hi,

    I don't have a Kraken to try it myself nor am I able to test things right now but here are a few things you need to make sure if you're still struggling with that:

    I think CHROMA_STATIC is wrong in that context. CHROMA_STATIC represents 7 which would not be what you want. You might want to set it to 1. EDIT: Nevermind I looked at the wrong enum. CHROMA_STATIC really is 1 but I wouldn't use it here cause it's not the usecase for it.

    The CChromaSDKImpl::ShowBitmap that is called at the bottom also includes functions specific for device type. Actually it includes what the device will do with the image. There is nothing happening for Headsets there. You need to create an if case there, too.

    Take a look at CChromaSDKImpl::ShowColor to see how to send a command to the headset. You need to call CreateHeadsetEffect with ChromaSDK::Headset::CHROMA_STATIC and a STATIC_EFFECT_TYPE with the specific color you get from GetDIBits. In your case you don't need a for loop as the other (Keyboard and Keypad) cause you only want one row.

    Hope that helps at least a bit :)
     
    Last edited: Nov 26, 2015
  6. TheFranubian

    TheFranubian Active Member

    You sure have helped, thank you both!

    Okay, so I did the thing and I am able to stream the color I choose (in this case, I wrote "WHITE") through the AmbientEffect reference in the Headset page.

    Ok, so the only thing I'd need is to be able to replace "WHITE" with an expresion that relates to hBitmap instead, right?

    So that it receives a color derived from hBitmap instead of the color I have manually written.

    (I am refering to the very last lines at the bottom here, below //Display the Image section)

    Code:
    DWORD WINAPI _CreateAmbientFx1(LPVOID lpParameter)
    {
        UINT DeiveType = (UINT)lpParameter;
    
        CChromaSDKImpl* pChromaSDK = (CChromaSDKImpl*)lpParameter;
    
        // Get the device context of the screen
        HDC hScreenDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
        // and a device context to put it in
        HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
    
        // Get width and height of primary display
        int width = GetDeviceCaps(hScreenDC, HORZRES);
        int height = GetDeviceCaps(hScreenDC, VERTRES);
    
        // Capture an image from the primary display
        HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
    
        while(TRUE)
        {
            HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
    
            // Specify the resize mode.
            ::SetStretchBltMode(hMemoryDC, HALFTONE);
    
            // Copy and resize the image to a memory buffer
            if(DeiveType == 1)
            {
                StretchBlt(hMemoryDC, 0, 0, ChromaSDK::Keyboard::MAX_COLUMN, ChromaSDK::Keyboard::MAX_ROW,
                        hScreenDC, 0, height, width, -height,
                        SRCCOPY);
            }
            else if(DeiveType == 5)
            {
                StretchBlt(hMemoryDC, 0, 0, ChromaSDK::Keypad::MAX_COLUMN, ChromaSDK::Keypad::MAX_ROW,
                        hScreenDC, 0, height, width, -height,
                        SRCCOPY);
            }
            else if (DeiveType == 4)
            {
                StretchBlt(hMemoryDC, 0, 0, 1, 1,
                    hScreenDC, 0, height, width, -height,
                    SRCCOPY);
            }
    
            // Select the bitmap into the device context.
            hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
    
            // Display the image
            if (DeiveType == 4)
            {
                pChromaSDK->ShowColor(DeiveType, WHITE);
            }
            else
            {
                pChromaSDK->ShowBitmap(DeiveType, hBitmap);
            }
    
            Sleep(5);
        }
    If I am not mistaken, yeah, everything that remains is to replace "WHITE" with a referenced derived from hBitmap. For example, one that averages it?

    if (DeiveType == 4)
    {
    pChromaSDK->ShowColor(DeiveType, {thingie created from hBitmap});
    }

    Sorry for inconveniences, still learning my way across C#.
     
  7. WolfspiritM

    WolfspiritM Active Member

    Hi,

    I don't know if StretchBlt works (or produces the color you want) with 1x1 but if it does the following should work:

    I'd replace the if at the bottom with a single call to ShowBitmap as before cause to get the color you need to do a few more things which ShowBitmap already takes care of and do the switch there.

    Here is an (completly untested) example of the ShowBitmap method:
    http://pastebin.com/Xj7RRrQy

    :)
     
  8. TheFranubian

    TheFranubian Active Member

    I'm afraid this is outside my skill level.

    I'm gonna try to generate the code for a 2-color-breathing mode first, as training, and in the future, I may try again to provide Ambiental perception to the Kraken and Deathadder.

    Thanks all of you for the support os far, though!
     
  9. njbmartin

    njbmartin Developer of Awesome

    Just a note
    Haha yeah the environment stuff is pretty advanced.
     
  10. Spearman873

    Spearman873 Member

    use the sdk
     
  11. TheFranubian

    TheFranubian Active Member

    Everything that I need is a way to average the hBitmap into a single RGB color, that's not in the SDK because that's a very specific matter.
     
  12. WolfspiritM

    WolfspiritM Active Member

    I tried it yesterday with a Keyboard and didn't have any problem letting the Keyboard glow in a single avarage color and it works as expected. Did you try my posted code above? The only new lines are 29-33 and 84-93.
     
  13. TheFranubian

    TheFranubian Active Member

    Done with this app.

    Updated, delay between colors at a minimum, and fixed reds being too white as well as the whites being too purple-ish.
     
    njbmartin likes this.
  14. njbmartin

    njbmartin Developer of Awesome

    Nice work!
     
    TheFranubian likes this.
  15. TheFranubian

    TheFranubian Active Member

    Thank you!!
     
  16. TheFranubian

    TheFranubian Active Member

    Updated the thread, managed to make the headset display colors at half the fastest rate possible.

    Labelled as Instant Ambient Effect, if anybody wants to check it out. Keep in mind that I had to make the software throw in a blank frame every two color refreshes to make it work this way, so colors may seem to blink and will appear darker than in the normal version.
     
    njbmartin likes this.
  17. Sixie

    Sixie Member

    Razer has like created a Chroma Fever :stuck_out_tongue_winking_eye:
     
  18. njbmartin

    njbmartin Developer of Awesome

    Nice going! Curious, is the effect mirrored? ie. is it a reflection or does the top of the keyboard match the top of the screen?
     
  19. TheFranubian

    TheFranubian Active Member

    Having the chance at using the SDK to implement the Chroma lights as we see fit surely is welcome :3

    Thanks! And sorries, but this app is only to make the Ambient Effect work with the Kraken 7.1 Chroma headset; and I have been told there is another user that has released an Ambiental effect for the keyboards...

    And also, the Kraken is the only Chroma thing I have at the moment, sorry.
     
  20. TheFranubian

    TheFranubian Active Member

    Release version done!

    Added video showing the effect in the Witcher 3 and in Crysis 3.
     
Thread Status:
Not open for further replies.
Sign In with Razer ID >


Don't have a Razer ID yet?
Get Razer ID >