Palette effect using Allegro

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
aetherFox
Posts: 100
Joined: Jun 23, 2005 16:48

Palette effect using Allegro

Post by aetherFox »

A simple effect that demonstrates Allegro's palette handling.

This is from my book (right now it's only a document as such).

Code: Select all

'palette_effect.bas

option explicit

#include "allegro.bi"

'Simple variable to draw the shape
dim i as integer

'Create a color object using Allegro's RGB struct
dim color_rgb as RGB

'Initialise Allegro routines
allegro_init
install_keyboard

'Set colour depth to 8-bit
set_color_depth 8

'Set graphics mode to 320x240 with error checking
if set_gfx_mode(GFX_AUTODETECT_WINDOWED, 320, 240, 0, 0) < 0 then
      'If windowed fails, go win
      if set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 320, 240, 0, 0) < 0 then
         allegro_message "Unable to set any graphic mode" + chr(13) + allegro_error + chr(13)
         end 1
      end if
end if

'Set window title
set_window_title "Palette magic"

'Make the screen black
set_palette @black_palette

'Clear everything off the screen
clear_bitmap screen

for i = 160 to 0 step -1
   circlefill screen, SCREEN_W / 2, SCREEN_H / 2, i, i / 10
next i

while key(KEY_ESC) = 0
   'Randomly change the colors of the palette
   for i = 16 to 0 step -1
      color_rgb.r = int(rnd * 64)
      color_rgb.g = int(rnd * 64)
      color_rgb.b = int(rnd * 64)
      
      'Wait for vertical retrace
      vsync
      
      'Assin the random color to i
      set_color i, @color_rgb
   next i
wend

'De-initialise allegro
remove_keyboard
allegro_exit

end 0
dumbledore
Posts: 680
Joined: May 28, 2005 1:11
Contact:

Post by dumbledore »

neat, i had to change line 34 to

Code: Select all

set_palette @black_palette(0)
because fbc gave an error otherwise...
aetherFox
Posts: 100
Joined: Jun 23, 2005 16:48

Post by aetherFox »

Not for me. Using v0.13b
dumbledore
Posts: 680
Joined: May 28, 2005 1:11
Contact:

Post by dumbledore »

i'm using .14 / jun 20
aetherFox
Posts: 100
Joined: Jun 23, 2005 16:48

Post by aetherFox »

What's stranger is that @desktop_palette is the same thing as @black_palette.

Anyway, that line is redundant, I forgot to remove it.

Here is the new source:

Code: Select all

'palette_effect.bas

option explicit

#include "allegro.bi"

'Simple variable to draw the shape
dim i as integer

'Create a color object using Allegro's RGB struct
dim color_rgb as RGB

'Initialise Allegro routines
allegro_init
install_keyboard

'Set colour depth to 8-bit
set_color_depth 8

'Set graphics mode to 320x240 with error checking
if set_gfx_mode(GFX_AUTODETECT_WINDOWED, 320, 240, 0, 0) < 0 then
      'If windowed fails, go fullscreen
      if set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 320, 240, 0, 0) < 0 then
         allegro_message "Unable to set any graphic mode" + chr(13) + allegro_error + chr(13)
         end 1
      end if
end if

'Set window title
set_window_title "Palette magic"

'Clear everything off the screen
clear_bitmap screen

for i = 160 to 0 step -1
   circlefill screen, SCREEN_W / 2, SCREEN_H / 2, i, i / 10
next i

while key(KEY_ESC) = 0
   'Randomly change the colors of the palette
   for i = 16 to 0 step -1
      color_rgb.r = int(rnd * 64)
      color_rgb.g = int(rnd * 64)
      color_rgb.b = int(rnd * 64)
      
      'Wait for vertical retrace
      vsync
      
      'Assin the random color to i
      set_color i, @color_rgb
   next i
wend

'De-initialise allegro
remove_keyboard
allegro_exit

end 0
Post Reply