Skip to main content
GameDev.net gamedev.net
🔒 Locked

Changing background color during runtime

Started by Roberts91 Jan 5, 2009 at 5:23 AM 16 replies 6.5k views
Original Post
Roberts91
Roberts91
I'm trying to change the background color during runtime with my DirectX program. From blue to black to blue to black and so on. I know how to change the color with this:
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
to black or whatever color I want and I've tried using that function and change colors under a certain condition but I can't get it to work. It stays the same color during runtime. Can anyone help me get this to work? thanks!
Evil Steve
Evil Steve
That should work fine. Are you sure you're Clear()ing every frame with the new colour? Any errors from the Debug Runtimes?
Roberts91
Roberts91
Nothing works
CableGuy
CableGuy
Saying nothing works won't help. At least give us some code...
Roberts91
Roberts91
// include the basic windows header files and the Direct3D header file#include <windows.h>#include <windowsx.h>#include <d3d9.h>// include the Direct3D Library file#pragma comment (lib, "d3d9.lib")// global declarationsLPDIRECT3D9 d3d;    // the pointer to our Direct3D interfaceLPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class// function prototypesvoid initD3D(HWND hWnd);    // sets up and initializes Direct3Dvoid render_frame(void);    // renders a single framevoid cleanD3D(void);    // closes Direct3D and releases memory// the WindowProc function prototypeLRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);// the entry point for any Windows programint WINAPI WinMain(HINSTANCE hInstance,                   HINSTANCE hPrevInstance,                   LPSTR lpCmdLine,                   int nCmdShow){    HWND hWnd;    WNDCLASSEX wc;    ZeroMemory(&wc, sizeof(WNDCLASSEX));    wc.cbSize = sizeof(WNDCLASSEX);    wc.style = CS_HREDRAW | CS_VREDRAW;    wc.lpfnWndProc = (WNDPROC)WindowProc;    wc.hInstance = hInstance;    wc.hCursor = LoadCursor(NULL, IDC_ARROW);    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;    wc.lpszClassName = L"WindowClass";    RegisterClassEx(&wc);    hWnd = CreateWindowEx(NULL,                          L"WindowClass",                          L"Our First Direct3D Program",                          WS_OVERLAPPEDWINDOW,                          300, 300,                          640, 480,                          NULL,                          NULL,                          hInstance,                          NULL);    ShowWindow(hWnd, nCmdShow);    // set up and initialize Direct3D    initD3D(hWnd);    // enter the main loop:    MSG msg;    while(TRUE)    {        DWORD starting_point = GetTickCount();        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))        {            if (msg.message == WM_QUIT)                break;            TranslateMessage(&msg);            DispatchMessage(&msg);        }        render_frame();        while ((GetTickCount() - starting_point) < 25);    }    // clean up DirectX and COM    cleanD3D();    return msg.wParam;}// this is the main message handler for the programLRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    switch(message)    {        case WM_DESTROY:            {                PostQuitMessage(0);                return 0;            } break;    }    return DefWindowProc (hWnd, message, wParam, lParam);}// this function initializes and prepares Direct3D for usevoid initD3D(HWND hWnd){    d3d = Direct3DCreate9(D3D_SDK_VERSION);    // create the Direct3D interface    D3DPRESENT_PARAMETERS d3dpp;    // create a struct to hold various device information    ZeroMemory(&d3dpp, sizeof(d3dpp));    // clear out the struct for use    d3dpp.Windowed = TRUE;    // program windowed, not fullscreen    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // discard old frames    d3dpp.hDeviceWindow = hWnd;    // set the window to be used by Direct3D    // create a device class using this information and the info from the d3dpp stuct    d3d->CreateDevice(D3DADAPTER_DEFAULT,                      D3DDEVTYPE_HAL,                      hWnd,                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,                      &d3dpp,                      &d3ddev);    return;}// this is the function used to render a single framevoid render_frame(void){    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);    d3ddev->BeginScene();    // begins the 3D scene		// do 3D rendering here    d3ddev->EndScene();    // ends the 3D scene    d3ddev->Present(NULL, NULL, NULL, NULL);   // displays the created frame on the screen    return;}// this is the function that cleans up Direct3D and COMvoid cleanD3D(void){    d3ddev->Release();    // close and release the 3D device    d3d->Release();    // close and release Direct3D    return;}
Codeka
Codeka
Quote:
Original post by Roberts91
// this is the function used to render a single framevoid render_frame(void){    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);    d3ddev->BeginScene();    // begins the 3D scene		// do 3D rendering here    d3ddev->EndScene();    // ends the 3D scene    d3ddev->Present(NULL, NULL, NULL, NULL);   // displays the created frame on the screen    return;}


That'll clear the back buffer to a blue-ish colour, but you haven't shown us how you're planning to switch between colours. It looks like you've just cut'n'pasted the sample code from directxtutorial.com and are asking us to implement this new feature for you. Have you even tried to do it yourself?
Roberts91
Roberts91
Well your too late to the party for me to show you what I had. I usually delete code that doesn't work because it doesnt "work". I made this thread in hoping you can show me what code I can add that will not work. Not for me to show you code that doesnt work.
Evil Steve
Evil Steve
Quote:
Original post by Roberts91
Well your too late to the party for me to show you what I had. I usually delete code that doesn't work because it doesnt "work". I made this thread in hoping you can show me what code I can add that will not work. Not for me to show you code that doesnt work.
The code you've given us works just fine - it clears the backbuffer to a blue-ish colour. However that's not what you're trying to do, so the code you've shown us is irrelevant.

We need to see your current code; which doesn't work, not the last code you had that did work - It's much easier for someone to correct broken code than to write code for you. You haven't even said what you've tried to do, you just said it "doesn't work".

Ultimately, the code will look like:
if(some_condition_that_makes_clearing_blue)   d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);else   d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
Roberts91
Roberts91
There you go and thats what I had. But what condition can we do to trigger that function?
Evil Steve
Evil Steve
Quote:
Original post by Roberts91
I've tried using that function and change colors under a certain condition


Quote:
Original post by Roberts91
There you go and thats what I had. But what condition can we do to trigger that function?
Under whatever your "certain condition" is.

What exactly do you want to trigger it? Pressing / holding a key? Alternate every second? Only render black at certain times of the day? Only render black for certain phases of the moon?
Toolmaker
Toolmaker
Quote:
Original post by Roberts91
There you go and thats what I had. But what condition can we do to trigger that function?


I would use the current saline level of the cup of soup that I'm currently having. You should write an API to read the saline levels in my cup of soup, and based on the maximum amount of salt that's possible to dissolve into soup, you can calculate the background color. Of course, there are more salt levels possible than colors, but I bet you can come up with a clever algorythm to round the values up to the nearest usable color.


In all seriousness: It can be *any*. You're a programmer, so you can use any condition you want. Wether the fact you have diarrhea or a keypress. You can think up any condition. Hell, you could just just do this: d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256), 1.0f, 0); *Note: Prepare for ultimate seizure time*

Toolmaker
 
Roberts91
Roberts91
Well I was wanting it to alternate every second so I was using my starting_point variable to compare times but that didn't work. Unless it was happening so fast that I couldn't see it.
Evil Steve
Evil Steve
Quote:
Original post by Roberts91
Well I was wanting it to alternate every second so I was using my starting_point variable to compare times but that didn't work. Unless it was happening so fast that I couldn't see it.
This should work (Untested):
if(((GetTickCount()/1000) % 2) == 0)   d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);else   d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
Basically that gets the current time in milliseconds, divides it by 1000 (to get into seconds), and then checks if the number is even (Divide by 2 gives a remainder of 0). If it is, then it clears to blue, otherwise it clears to black.
Roberts91
Roberts91
Oooh okay so yea I was doing it horribly wrong. I don't really understand the math you just presented but okay yea it works.
Evil Steve
Evil Steve
Quote:
Original post by Roberts91
Oooh okay so yea I was doing it horribly wrong. I don't really understand the math you just presented but okay yea it works.
You can break it down a bit differently:
DWORD dwCurrentTimeInSeconds = GetTickCount()/1000;DWORD dwRemainder = dwCurrentTimeInSeconds % 2;if(dwRemainder == 0)//Etc

The modulo operator (%) is the remainder of a division, in the above case it gives the the remainder of dividing the current time in seconds by 2, which will be 0 (the number is divisible by 2 and therefore even), or 2 (the number isn't divisible by 2 and is therefore odd).

It's much more important to understand why something works than it is for it to "just work" - otherwise you can't change it without just guessing what you're doing.
Roberts91
Roberts91
I gotcha now. I understand everything but the GetTickCount I'm guessing that it returns how many milliseconds have passed? And since theres 1000 milliseconds in a second you divide it by 1000 which in turns gives 1 (just pretend 1 = 1 second). And then 1 % 2 = 1. So it will draw black. And then once 2 seconds have passed which will be 2 % 2 = 0 it will draw blue. Gotcha gotcha. Thanks man!
Toolmaker
Toolmaker
Quote:
Original post by Roberts91
I gotcha now. I understand everything but the GetTickCount I'm guessing that it returns how many milliseconds have passed? And since theres 1000 milliseconds in a second you divide it by 1000 which in turns gives 1 (just pretend 1 = 1 second). And then 1 % 2 = 1. So it will draw black. And then once 2 seconds have passed which will be 2 % 2 = 0 it will draw blue. Gotcha gotcha. Thanks man!


Actually, GetTickCount() returns the number of milliseconds that have passed since the system booted. By dividing it by 1000, you get the number of seconds that passed since system boot. And you then basically tell it to do modulus 2(Which trickles down to dividing the number by 2, and determining the amount left. So if you modulus 501 by 2, the result will be 1). Explanation of this.

Toolmaker
 

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.