Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Increment, *then* Decrement Loop in C# XNA
New Posts  All Forums:Forum Nav:

Increment, *then* Decrement Loop in C# XNA

post #1 of 11
Thread Starter 
I'm having trouble getting a gradually changing background in XNA. I have the RGB Values incrementing every time the Draw method is called (once every 60 seconds).

This makes for a nice change in color except when the value gets to 255 it resets back to 0 and so there is the noticeable reset in game. I want to have it increment to 255 then decrement back down to 0 and keep doing that in a loop.

However I cannot just put variables and call a method that loops through and then returns the value for that color (0-255) as the method that would call that is re-run every 1/60th of a second...

Right now I just have it as
Code:
graphics.GraphicsDevice.Clear(new Color(GetRed(Red++),(Blue++),(Green++));

which loops through nicely and increments them, but I have no way to decrement them once they get to 255...

Any help is appreciated.
OC Beast
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8500 @4GHZ Gigabyte P45 4850x2 2GB IT LIVES! 4GB 
Hard DriveOptical DriveOSMonitor
500AAKS+250external CD-DVD combo windows 7 ultimate x64 26" 
KeyboardPowerCaseMouse
MC$ wireless 650w corsair antec 900 Logitech battlefield 2142 G5 
  hide details  
Reply
OC Beast
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8500 @4GHZ Gigabyte P45 4850x2 2GB IT LIVES! 4GB 
Hard DriveOptical DriveOSMonitor
500AAKS+250external CD-DVD combo windows 7 ultimate x64 26" 
KeyboardPowerCaseMouse
MC$ wireless 650w corsair antec 900 Logitech battlefield 2142 G5 
  hide details  
Reply
post #2 of 11
What you need to do is create a boolean inc_dec_bool and have it increment/decrement based on that value.

So let's say inc_dec_bool = 0, then we make it so that we increment when this is 0.

Then we get 252, then 253, then 254, then 255.

At this point we change inc_dec_bool to 1.

While inc_dec_bool = 1, do your decrementing.

Then you have 3, then 2, then 1, then 0, at this point set inc_dec_bool to 0.

So you then just need four if statements(could even be done in just two)




// Once we reach 255, change inc_dec_bool so that we now decrement
if (GetRed(or whatever returns the RGB values) == 255){ inc_dec_bool 0;}
// Once we reach 0, change inc_dec_bool so that we now increment
if (GetRed(or whatever returns the RGB values) == 0){ inc_dec_bool 1;}

if (inc_dec_bool == 0){ graphics.GraphicsDevice.Clear(new Color(GetRed(Red--),(Blue--),(Green--)));}

if (inc_dec_bool == 1){ graphics.GraphicsDevice.Clear(new Color(GetRed(Red++),(Blue++),(Green++)));}
Edited by joemaniaci - 5/7/11 at 3:25pm
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
post #3 of 11
Thread Starter 
hmm thanks! I was going to try something like that but as I was thinking about it I didn't have it quite that way so it didn't work.
OC Beast
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8500 @4GHZ Gigabyte P45 4850x2 2GB IT LIVES! 4GB 
Hard DriveOptical DriveOSMonitor
500AAKS+250external CD-DVD combo windows 7 ultimate x64 26" 
KeyboardPowerCaseMouse
MC$ wireless 650w corsair antec 900 Logitech battlefield 2142 G5 
  hide details  
Reply
OC Beast
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8500 @4GHZ Gigabyte P45 4850x2 2GB IT LIVES! 4GB 
Hard DriveOptical DriveOSMonitor
500AAKS+250external CD-DVD combo windows 7 ultimate x64 26" 
KeyboardPowerCaseMouse
MC$ wireless 650w corsair antec 900 Logitech battlefield 2142 G5 
  hide details  
Reply
post #4 of 11
Code:

int direction = -1;
int currentValue = 255;

// Code below executes each frame
graphics.GraphicsDevice.Clear(new Color(currentValue, currentValue, currentValue);
currentValue += direction;

if ( currentValue == 0 || currentValue == 255 )
{
   direction *= -1;
}

Edited by lordikon - 5/9/11 at 9:31pm
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #5 of 11
Quote:
Originally Posted by lordikon;13436298 
Code:

bool direction = -1;
int currentValue = 255;

// Code below executes each frame
graphics.GraphicsDevice.Clear(new Color(currentValue, currentValue, currentValue);
currentValue += direction;

if ( currentValue == 0 || currentValue == 255 )
{
   direction *= -1;
}


I like it other then I would probably use an sByte instead of a bool although they are basically the same thing. In terms of memory usage unless you actually force it to store bits of bools in a single mem adress but that gets messy.
Edited by Midpipps - 5/9/11 at 12:21pm
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
Reply
post #6 of 11
Quote:
Originally Posted by Midpipps;13442559 
I like it other then I would probably use an sByte instead of a bool although they are basically the same thing. In terms of memory usage unless you actually force it to store bits of bools in a single mem adress but that gets messy.

Thanks, I changed the code halfway through and meant to change that to an integer type. I'm guessing the compiler would've at least given a warning for assigning a not boolean value to a bool.
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #7 of 11
Quote:
Originally Posted by lordikon;13448987 
Thanks, I changed the code halfway through and meant to change that to an integer type. I'm guessing the compiler would've at least given a warning for assigning a not boolean value to a bool.

Yeah it will give a compilation error in C# in C++ it will give compile warnings and will not keep any signs so -1 will always be 1 I totally spaced on that which is why your code seemed like it would work to me initially tongue.gif
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
Reply
post #8 of 11
Thread Starter 
Quote:
Originally Posted by lordikon;13436298 
Code:

int direction = -1;
int currentValue = 255;

// Code below executes each frame
graphics.GraphicsDevice.Clear(new Color(currentValue, currentValue, currentValue);
currentValue += direction;

if ( currentValue == 0 || currentValue == 255 )
{
   direction *= -1;
}

This doesn't change the color, it does the whole "sets it to one color then loops through and resets and you only see the reset when there is a black blip thing"
OC Beast
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8500 @4GHZ Gigabyte P45 4850x2 2GB IT LIVES! 4GB 
Hard DriveOptical DriveOSMonitor
500AAKS+250external CD-DVD combo windows 7 ultimate x64 26" 
KeyboardPowerCaseMouse
MC$ wireless 650w corsair antec 900 Logitech battlefield 2142 G5 
  hide details  
Reply
OC Beast
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8500 @4GHZ Gigabyte P45 4850x2 2GB IT LIVES! 4GB 
Hard DriveOptical DriveOSMonitor
500AAKS+250external CD-DVD combo windows 7 ultimate x64 26" 
KeyboardPowerCaseMouse
MC$ wireless 650w corsair antec 900 Logitech battlefield 2142 G5 
  hide details  
Reply
post #9 of 11
Quote:
Originally Posted by ZTR1760;13458710 
This doesn't change the color, it does the whole "sets it to one color then loops through and resets and you only see the reset when there is a black blip thing"

I'm not sure about your full implementation. I'm assuming you're clearing the graphics device each frame. It should clear it to a specific color, like 10, 10, 10, then it would increment or decrement immediately after that, and the next frame would be either 9,9,9 or 11,11,11.

Make sure you're not calling 'int currentValue = 255' each frame. Notice my comment in the code, only the code below my comment should be executed each frame.
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #10 of 11
Thread Starter 
Quote:
Originally Posted by lordikon;13458961 
I'm not sure about your full implementation. I'm assuming you're clearing the graphics device each frame. It should clear it to a specific color, like 10, 10, 10, then it would increment or decrement immediately after that, and the next frame would be either 9,9,9 or 11,11,11.

Make sure you're not calling 'int currentValue = 255' each frame. Notice my comment in the code, only the code below my comment should be executed each frame.

oh yea I know I put those at the class level variables section
OC Beast
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8500 @4GHZ Gigabyte P45 4850x2 2GB IT LIVES! 4GB 
Hard DriveOptical DriveOSMonitor
500AAKS+250external CD-DVD combo windows 7 ultimate x64 26" 
KeyboardPowerCaseMouse
MC$ wireless 650w corsair antec 900 Logitech battlefield 2142 G5 
  hide details  
Reply
OC Beast
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8500 @4GHZ Gigabyte P45 4850x2 2GB IT LIVES! 4GB 
Hard DriveOptical DriveOSMonitor
500AAKS+250external CD-DVD combo windows 7 ultimate x64 26" 
KeyboardPowerCaseMouse
MC$ wireless 650w corsair antec 900 Logitech battlefield 2142 G5 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Increment, *then* Decrement Loop in C# XNA