256 Colour Tutorial Part 2: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
 
Line 35: Line 35:
[[Image:8bittut_dizzypal.png|center|Dizzy background palette]]
[[Image:8bittut_dizzypal.png|center|Dizzy background palette]]


You may have noticed that, I used the "rainbow" colours in slots #240 thru #253 (the first 14 slots in the last line of the above palette) to draw the circular "warping frame" of the background. If the colours of these slots are altered the colours of that "warping frame" will be changed accordingly (you can load the background into a graphics programme and play with the colours of these slots and see what will happen). To make an animated effect, you may try using the function '''CyclePalette()''' with #240 as the ''starting slot'' and #253 as the ''ending slot''. Here's a graphical illustration of what will happen if you do that:
You may have noticed that, I used the "rainbow" colours in slots #240 through #253 (the first 14 slots in the last line of the above palette) to draw the circular "warping frame" of the background. If the colours of these slots are altered the colours of that "warping frame" will be changed accordingly (you can load the background into a graphics programme and play with the colours of these slots and see what will happen). To make an animated effect, you may try using the function '''CyclePalette()''' with #240 as the ''starting slot'' and #253 as the ''ending slot''. Here's a graphical illustration of what will happen if you do that:


[[Image:8bittut_dizzypalcp.png|Diagram illustrating the effect of CyclePalette()]]
[[Image:8bittut_dizzypalcp.png|Diagram illustrating the effect of CyclePalette()]]
Line 41: Line 41:
Whenever the function CyclePalette(240, 253) is executed, the colours in these slots are shifted to the left by one slot, and the original colour in the first slot (#240) will be warped to the last slot (#253). Similarly, if we swap the two parameters of this function the direction of the shifting of the colours will be reversed.
Whenever the function CyclePalette(240, 253) is executed, the colours in these slots are shifted to the left by one slot, and the original colour in the first slot (#240) will be warped to the last slot (#253). Similarly, if we swap the two parameters of this function the direction of the shifting of the colours will be reversed.


Now, let's decide where to put the codes in, for example, I want the colours to cycle in the ''forward'' direction whenever I press the '''Space Bar''', and in the ''backwards'' direction when I press the '''Backspace''', so I type this into the room's script:
Now, let's decide where to put the codes in. For example, I want the colours to cycle in the ''forward'' direction whenever I press the '''Space Bar''', and in the ''backwards'' direction when I press the '''Backspace''', so I type this into the room's script:
   function on_key_press(eKeyCode key){
   function on_key_press(eKeyCode key){
     if (key==eKeySpace) CyclePalette(240, 253);
     if (key==eKeySpace) CyclePalette(240, 253);
Line 49: Line 49:
Save the script and compile the game to test it, you'll notice that whenever you press the '''Space Bar''', the colours of the "warping frame" changes. This is due to the shift in colours in the mentioned 14 colour slots. If you keep the '''Space Bar''' pressed you'll see a smooth colour rotation animation. Try it out also with the '''Backspace''' key.
Save the script and compile the game to test it, you'll notice that whenever you press the '''Space Bar''', the colours of the "warping frame" changes. This is due to the shift in colours in the mentioned 14 colour slots. If you keep the '''Space Bar''' pressed you'll see a smooth colour rotation animation. Try it out also with the '''Backspace''' key.


I'm not satisfied with this yet, now I want the water at the centre to fall ''continuously''. What can we do now? Again, we can use '''CyclePalette()'''. We can use this in the '''repeatedly_execute()''' or the '''repeatedly_execute _always()''' events for the continuous motion. Now, look at the background again. You'll see that the waterfall was carefully drawn using colours ''mostly'' from slot #208 through #215. I want the animation to play without ''any'' interruptions, so I add the following into the room's script:
I'm not satisfied with this yet, now I want the water at the centre to fall ''continuously''. What can we do now? Again, we can use '''CyclePalette()'''. We can use this in the '''repeatedly_execute()''' or the '''repeatedly_execute_always()''' events for the continuous motion. Now, look at the background again. You'll see that the waterfall was carefully drawn using colours ''mostly'' from slot #208 through #215. I want the animation to play without ''any'' interruptions, so I add the following into the room's script:
   function repeatedly_execute_always() {
   function repeatedly_execute_always() {
     CyclePalette(208, 215);
     CyclePalette(208, 215);
   }
   }


Again, save the script and test it... '''WOAH'''! The water moves! But it seems a bit too fast, isn't it? Just do some small tweakings to the above codes (I won't go into details about how that works, as you should already have enough scripting knowledge to read this tutorial):
Again, save the script and test it... '''WOAH'''! The water moves! But it seems a bit too fast, isn't it? Just do some small tweaking to the above codes (I won't go into details about how that works, as you should already have enough scripting knowledge to read this tutorial):


First, add this to the top of the room script:
First, add this to the top of the room's script:
   '''int waterflow=5;'''
   '''int waterflow=5;'''
Then, modify the '''repeatedly_execute_always()''' function like this:
Then, modify the '''repeatedly_execute_always()''' function like this:
Line 67: Line 67:
   }
   }


Test the game again, the animation is now slower and looks better. You may have noticed that some of teh white pixels on the water won't animate. This is because I drew them with a colour not using the slots included in the cycle. If you care, they're actually using colour slot #15 (instead of slot #215 in the cycle), which is a '''Gamewide''' "sprite" colour. From this you'll know that it may not be a bad practice to use duplicated colours in a 256 colour image, as there may be cases that you want the colour of a pixel to be changed somewhere in game (like in a palette rotation animation, say for example) while keeping the colour of another pixel unchanged. This is true as long as you manage your palette wisely in a systematic way.
Test the game again. The animation is now slower and looks better. You may have noticed that some of teh white pixels on the water won't animate. This is because I drew them with a colour not using the slots included in the cycle. If you care, they're actually using colour slot #15 (instead of slot #215 in the cycle), which is a "'''Gamewide'''" "sprite" colour. From this you'll know that it may not be a bad practice to use duplicated colours in a 256 colour image, as there may be cases that you want the colour of a pixel to be changed somewhere in-game (like in a palette rotation animation, say for example) while keeping the colour of another pixel unchanged. This is true as long as you manage your palette wisely in a systematic way.


===Changing an individual colour===
===Changing an individual colour===
Line 79: Line 79:
'''NOTE:''' This function will allow you to change the colours which are "locked" in the AGS Editor. However, you should not normally do this as it can cause strange colours in the game.
'''NOTE:''' This function will allow you to change the colours which are "locked" in the AGS Editor. However, you should not normally do this as it can cause strange colours in the game.
</blockquote>
</blockquote>
(By "locked" colours in the last sentence of the description it is actually referring to slots #0 thru #16, which are normally considered to be used by the system engine and are not advised to change.)
(By '''"locked" colours''' in the last sentence of the description it is actually referring to slots #0 through #16, which are normally considered to be used by the system engine and are not advised to change.)


Well, I won't bother explaining the above word by word. Basically what this function does, is to change the colour of a slot directly, and ''all'' pixels on screen which use this slot will be changed to the new colour immediately.
Well, I won't bother explaining the above word by word. Basically what this function does, is to change the colour of a slot directly, and ''all'' pixels on-screen which use this slot will be changed to the new colour immediately.
Let's illustrate this with an example, remember the [[media:8bittut_ss1.bmp|sprites]] we used in Part 1? Conside the girl sprite:
Let's illustrate this with an example, remember the [[media:8bittut_ss1.bmp|sprites]] we used in Part 1? Consider the girl sprite:


[[Image:8bittut_sprite0.png|center|68px|Girl sprite]]
[[Image:8bittut_sprite0.png|center|68px|Girl sprite]]


If you check, you'll see that the red tips of her antennae were drawn using colour slot #32. Suppose you want them to alter between black and red colours endlessly throughout the ''whole game'', what we can do is, in each game loop, if the colour of slot #32 is currently <font color='FF0000'>red</font>, change it to <font color='000000'>black</font> (where the (r, g, b) tuple equals (0, 0, 0) ), ''else'' change it to <font color='FF0000'>red</font> (where the (r, g, b) tuple equals (63, 0, 0) ). This logic can be visualised as follows:
If you check, you'll see that the red tips of her antennae were drawn using colour slot #32. Suppose you want them to alter between black and red colours endlessly throughout the ''whole game'', what we can do is, in each game loop, if the colour of slot #32 is currently <font color='FF0000'>red</font>, change it to <font color='000000'>black</font> (where the (R, G, B) tuple equals (0, 0, 0) ), ''else'' change it to <font color='FF0000'>red</font> (where the (R, G, B) tuple equals (63, 0, 0) ). This logic can be visualised as follows:


First, add on top of the ''Global Script'':  
First, add on top of the ''Global Script'':  
   int antennaecol=1; //variable to keep track of the colour change
   int antennaecol=1; //variable to keep track of the colour change


Then add the following to the ''Global script'':
Then, add the following to the ''Global Script'':
   function repeatedly_execute_always() {
   function repeatedly_execute_always() {
     if (antennaecol==0) {
     if (antennaecol==0) {
Line 104: Line 104:
Save your script and test the game, you may notice two things:
Save your script and test the game, you may notice two things:
# Though the girl's shoes were also painted in red, their colours won't flash like her antennae, since I didn't use slot #32 when painting her shoes. If you check this in the room with the [[media:8bittut_disco.bmp|"disco" background]], you'll see that the red "light" on the door lock won't flash either, because of the same reason.
# Though the girl's shoes were also painted in red, their colours won't flash like her antennae, since I didn't use slot #32 when painting her shoes. If you check this in the room with the [[media:8bittut_disco.bmp|"disco" background]], you'll see that the red "light" on the door lock won't flash either, because of the same reason.
# If you place also the alien sprite in a room, you may notice that his fingertip flashes also, this is because I also used colour slot #32 there. If you check the room with the [[media:8bittut_disco.bmp|"disco" background]], you'll find out that a pixel just above the disco ball also flashes.
# If you place also the alien sprite in a room, you may notice that his fingertip flashes also. This is because I also used colour slot #32 there. If you check the room with the [[media:8bittut_disco.bmp|"disco" background]], you'll find out that a pixel just above the disco ball also flashes.
   
   
Combining the above observations I hope you can get a hang on how to manage your palettes and images more wisely when you paint them.
Combining the above observations I hope you can get a hang on how to manage your palettes and images more wisely when you paint them.


Now, let's do a more interesting effect, suppose we don't want the girl's antenna tips to flash quickly, but want the colour to fade in and out gradually, what can we do? Well, we may make the following modifications to the above codes:
Now, let's do a more interesting effect. Suppose we don't want the girl's antenna tips to flash quickly, but want the colour to fade in and out gradually, what can we do? Well, we may make the following modifications to the above codes:
   int antennaecol='''63, antennaefade=-1''';
   int antennaecol='''63, antennaefade=-1''';


Line 121: Line 121:
Again, save your script and test the game. Got it already?
Again, save your script and test the game. Got it already?


Above are just some small examples on how to modify individual palette slots using '''SetPalRGB()'''. You may experiment yourself, like changing other slots, changing some of the slots into other crazy colours, etc. You may even think of some colour animations, with colour changes decided by some mathematical formulae. Everything is left to your imaginations. But always bear in mind that whenever you use '''SetPalRGB()''', the r-, g-, b- channel values ''must'' be within the allowed range of 0 through 63.
Above are just some small examples on how to modify individual palette slots using '''SetPalRGB()'''. You may experiment yourself, like changing other slots, changing some of the slots into other crazy colours, etc. You may even think of some colour animations, with colour changes decided by some mathematical formulae. Everything is left to your imaginations. But always bear in mind that whenever you use '''SetPalRGB()''', the R-, G-, B- channel values ''must'' be within the allowed range of 0 through 63.


===Conclusion===
===Conclusion===
Line 128: Line 128:
[[media:8bittut_paltut2_v3.zip|Download this file]]
[[media:8bittut_paltut2_v3.zip|Download this file]]


Which is merely just the above materials put together into an AGS "game". To test it just unzip it into an empty folder and load up '''Game.agf''' via the AGS editor and press F5 to test it. '''room1.crm''' is the "dizzy" room, where you can click to transport you to '''room2.crm''', which is the "disco" room, where you can click on the door(way) to toggle between opening and closing of the door. You'll notice also the light on the door lock will change colour according to the status of the door. You can read the room script for more info on how to do this. If you're having enough "fun" just press '''ctrl-Q''' to quit.
Which is merely just the above materials put together into an AGS "game". To test it just unzip it into an empty folder and load up '''Game.agf''' via the AGS editor and press F5 to test it. '''room1.crm''' is the "dizzy" room, where you can click to transport you to '''room2.crm''', which is the "disco" room, where you can click on the door(way) to toggle between opening and closing of the door. You'll notice also that the light on the door lock will change colour according to the status of the door. You can read the room script for more info on how to do this. If you're having enough "fun" just press '''ctrl-Q''' to quit.


===Downloadable files used in this part===
===Downloadable files used in this part===

Navigation menu