LoadPalette
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by turtle1776.
I am particularly intrigued by PB's ability to do graphics in 8 bit mode because you can write retro 2d games and maximize your graphics capability at the same time.
Several highly professional games (Starcraft, Diablo, Age of Empires) used 8 bit graphics so they could max-out their graphics capabilities. My laptop, with a minimal 8 MB of video memory, could run those games only because they were 8 bit. As far as I know, no other Basic language allows you to do 8 bit graphics besides PB.
Unfortunately, I am still trying to learn how to use the palettes, which are required for 8 bit. Specifically, I'd like to know how to use the following command:
LoadPalette() - Load an palette from a standard BMP file (256 colors pictures only). The palette is initialized with the value found inside the file.
Are there any sample bmp files out there that show how this works? Thanks!
I am particularly intrigued by PB's ability to do graphics in 8 bit mode because you can write retro 2d games and maximize your graphics capability at the same time.
Several highly professional games (Starcraft, Diablo, Age of Empires) used 8 bit graphics so they could max-out their graphics capabilities. My laptop, with a minimal 8 MB of video memory, could run those games only because they were 8 bit. As far as I know, no other Basic language allows you to do 8 bit graphics besides PB.
Unfortunately, I am still trying to learn how to use the palettes, which are required for 8 bit. Specifically, I'd like to know how to use the following command:
LoadPalette() - Load an palette from a standard BMP file (256 colors pictures only). The palette is initialized with the value found inside the file.
Are there any sample bmp files out there that show how this works? Thanks!
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by turtle1776.
Oh, and I'm not sure why, but 8 bit graphics don't load properly when using the following code:
Oh, and I'm not sure why, but 8 bit graphics don't load properly when using the following code:
Code: Select all
InitSprite() : InitKeyboard()
OpenScreen(640, 480, 8, "8 bit sprite example")
LoadSprite(0, "Data\your8bitsprite.bmp", 0)
Repeat
DisplaySprite(0, 100, 100)
FlipBuffers()
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
End
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by turtle1776.
Hmmm. It seems that my two separate questions were jumbled together in those answers. I'll try again:
(1) How do you use LoadPalette()? Can anyone email me a bmp that would work with this command? And perhaps some simple code? My email address is [url]mailto:pwlester@policyalmanac.org.[/url]
(2) Ok, now forget LoadPalette for a moment. This is a separate question. How do I get the following code to work?
Send me an email at [url]mailto:pwlester@policyalmanac.org[/url] and I will email you the 8 bit sprite.
Thanks!
Hmmm. It seems that my two separate questions were jumbled together in those answers. I'll try again:
(1) How do you use LoadPalette()? Can anyone email me a bmp that would work with this command? And perhaps some simple code? My email address is [url]mailto:pwlester@policyalmanac.org.[/url]
(2) Ok, now forget LoadPalette for a moment. This is a separate question. How do I get the following code to work?
Code: Select all
InitSprite() : InitKeyboard() : InitPalette(0)
OpenScreen(640, 480, 8, "8 bit sprite example")
LoadSprite(0, "Data\mage004.bmp", 0) ;see link for sprite
Repeat
DisplaySprite(0, 100, 100)
FlipBuffers()
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
End
Thanks!
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by turtle1776.
(1) The example in the PureBasic\Examples\Sources\ drawer works fine, but it does not use the LoadPalette command. I need a bmp that would work with the command, but I don't know what it should look like. Do you have one?
(2) I will send you my code + 8 bit image to see if it works for you.
Patrick
(1) The example in the PureBasic\Examples\Sources\ drawer works fine, but it does not use the LoadPalette command. I need a bmp that would work with the command, but I don't know what it should look like. Do you have one?
(2) I will send you my code + 8 bit image to see if it works for you.
Patrick
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Pupil.
turtle, use the DisplayPalette() command inside your Repeat:Until loop and you should get the correct colors!
code could look something like this:
turtle, use the DisplayPalette() command inside your Repeat:Until loop and you should get the correct colors!
code could look something like this:
Code: Select all
InitSprite() : InitKeyboard() : InitPalette(1)
OpenScreen(640, 480, 8, "8 bit sprite example")
LoadSprite(0, "Data\mage004.bmp", 0) ;see link for sprite
LoadPalette(0, "Data\mage004.bmp")
Repeat
FlipBuffers()
DisplayPalette(0)
DisplaySprite(0, 100, 100)
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
End
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by turtle1776.
Thanks pupil (and plouf too, I understand what you were suggesting now)! It works great now. Now I just need to figure out why.
It appears from your code that if you want to render anything in 8 bit mode, you must first use the loadPallete command, which I wasn't sure how to use. The PB documentation does not really describe how to use the command, and the palette.pb sample program that comes with PB does not use this command. Instead, it generates a palette using for/next loops. So thank you for the sample.
But now I have an additional question. What is loadPallete doing, exactly?
Let me describe my understanding of how a pallete works (this may be wrong). In 8 bit mode, you basically get 256 colors to work with at any given time. But these 256 colors can be any color you want if you create a pallete that assigns each of the 256 colors a specific rgb value. When the program is running, DirectX reads the incoming 8 bit value, checks it against your palette, then blits the proper color on the screen.
In the pallete.pb sample program that comes with PB, the pallete is generated automatically using for/next loops. This isn't really useful because that particular program assigns each of the 256 colors a variation of the color green.
To actually use 8 bit mode for real graphics, you need a different pallete. This is why I was interested in the loadPallete command. I thought that this command might be asking you to load a 16x16 pixel bitmap, which it would then use for look-up purposes.
Based on your example code, however, this is not what it does. Instead it seems to look at each of the colors used in your sprite, and assigns them to one or more of the 256 slots in your pallete. It appears to do this automatically. But what happens when you have many sprites to load, and none of them contains all 256 colors than you plan to use? Do you go back and create a single bitmap that contains all 256 of your colors and load that instead? Pure Basic then uses those colors when drawing all of your other graphics?
Any additional explanation that you could provide would be greatly appreciated. And thanks again for the code example!

[Edit]
Thanks for the tip, Fred. I just looked at that sample program. Here is the relevant code form that sample:
But the Back_3.bmp only contains about 5 different colors in it, and more colors are being used by the program than that. Now I am really confused.
Thanks pupil (and plouf too, I understand what you were suggesting now)! It works great now. Now I just need to figure out why.
It appears from your code that if you want to render anything in 8 bit mode, you must first use the loadPallete command, which I wasn't sure how to use. The PB documentation does not really describe how to use the command, and the palette.pb sample program that comes with PB does not use this command. Instead, it generates a palette using for/next loops. So thank you for the sample.
But now I have an additional question. What is loadPallete doing, exactly?
Let me describe my understanding of how a pallete works (this may be wrong). In 8 bit mode, you basically get 256 colors to work with at any given time. But these 256 colors can be any color you want if you create a pallete that assigns each of the 256 colors a specific rgb value. When the program is running, DirectX reads the incoming 8 bit value, checks it against your palette, then blits the proper color on the screen.
In the pallete.pb sample program that comes with PB, the pallete is generated automatically using for/next loops. This isn't really useful because that particular program assigns each of the 256 colors a variation of the color green.
To actually use 8 bit mode for real graphics, you need a different pallete. This is why I was interested in the loadPallete command. I thought that this command might be asking you to load a 16x16 pixel bitmap, which it would then use for look-up purposes.
Based on your example code, however, this is not what it does. Instead it seems to look at each of the colors used in your sprite, and assigns them to one or more of the 256 slots in your pallete. It appears to do this automatically. But what happens when you have many sprites to load, and none of them contains all 256 colors than you plan to use? Do you go back and create a single bitmap that contains all 256 of your colors and load that instead? Pure Basic then uses those colors when drawing all of your other graphics?
Any additional explanation that you could provide would be greatly appreciated. And thanks again for the code example!
[Edit]
Thanks for the tip, Fred. I just looked at that sample program. Here is the relevant code form that sample:
Code: Select all
; Get the palette information from Back_3.bmp and display it to the screen
LoadPalette(1, Path$+"Back_3.bmp")
DisplayPalette(1)
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Pupil.
In a BMP with 256 colors there is an array of colors(RGB values) in the header of the BMP file, the index of each of these colors is what the image is built from, each pixel represents an index in the color array.. This palette definition in the BMP is what LoadPalette() retrieves.
Using an 8 bit screen means that all the sprites displayed on the screen at a given time MUST share the same color palette to be displayed correctly. However it's not necessary for all the sprites to have the exact same colors defined, what's important is that for a given index the colors must be the same, if you try alternative you must load a palette that has the same colors for the union of the color indexes for the different sprites used. (i may have gotten that right or missed by a mile, or more)
If what i just wrote was pure gibberish, see alternative below;)
To not make things more complicated stick to the same color palette for each sprite you use.
In a BMP with 256 colors there is an array of colors(RGB values) in the header of the BMP file, the index of each of these colors is what the image is built from, each pixel represents an index in the color array.. This palette definition in the BMP is what LoadPalette() retrieves.
Using an 8 bit screen means that all the sprites displayed on the screen at a given time MUST share the same color palette to be displayed correctly. However it's not necessary for all the sprites to have the exact same colors defined, what's important is that for a given index the colors must be the same, if you try alternative you must load a palette that has the same colors for the union of the color indexes for the different sprites used. (i may have gotten that right or missed by a mile, or more)
If what i just wrote was pure gibberish, see alternative below;)
To not make things more complicated stick to the same color palette for each sprite you use.
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by turtle1776.
This code may illustrate my question from above. Save the following code in your PureBasic/Examples/Sources file and run it.
You should notice that the second sprite does not look right. Now remove the comment from second loadPallete command. It should now look normal.
But why?
This code may illustrate my question from above. Save the following code in your PureBasic/Examples/Sources file and run it.
Code: Select all
InitSprite() : InitKeyboard() : InitPalette(0)
OpenScreen(640, 480, 8, "8 bit sprite example")
LoadSprite(0, "..\Sources - Advanced\Waponez II\Data\Player_1.bmp", 0)
LoadSprite(1, "..\Sources - Advanced\Waponez II\Data\Back_3.bmp", 0)
LoadPalette(0, "..\Sources - Advanced\Waponez II\Data\Player_1.bmp")
; LoadPalette(0, "..\Sources - Advanced\Waponez II\Data\Back_3.bmp")
DisplayPalette(0)
Repeat
DisplaySprite(0, 100, 100)
DisplaySprite(1, 300, 100)
FlipBuffers()
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
End
But why?
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by turtle1776.
Thanks again, Pupil! You have been very helpful. I used what you wrote and did a Google search and came up with the following explanations on the web:
http://kbt.narod.ru/docs/lgfp/ch06/06-01.html
http://www.brackeen.com/home/vga/bitmaps.html
So, it sounds like what I should do is create a simple bmp file that contains all 256 colors that I want to use in my game. Use loadPalette() on that file. All other sprites loaded should only use the colors contained in the loaded palette file.
If I decide to use a new set of 256 colors, I simply load a new palette that contains the new colors.
Thanks!

Thanks again, Pupil! You have been very helpful. I used what you wrote and did a Google search and came up with the following explanations on the web:
http://kbt.narod.ru/docs/lgfp/ch06/06-01.html
http://www.brackeen.com/home/vga/bitmaps.html
So, it sounds like what I should do is create a simple bmp file that contains all 256 colors that I want to use in my game. Use loadPalette() on that file. All other sprites loaded should only use the colors contained in the loaded palette file.
If I decide to use a new set of 256 colors, I simply load a new palette that contains the new colors.
Thanks!