Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing SDL_Palette support #143

Open
whoozle opened this issue Aug 17, 2023 · 4 comments
Open

Missing SDL_Palette support #143

whoozle opened this issue Aug 17, 2023 · 4 comments

Comments

@whoozle
Copy link
Contributor

whoozle commented Aug 17, 2023

It would be really nice to have a wrapper around https://wiki.libsdl.org/SDL2/SDL_Palette

The only function that takes palette argument is SDL_SetSurfacePalette

Thanks

@AMDmi3
Copy link
Member

AMDmi3 commented Aug 22, 2023

What's your use case, if I may ask? https://wiki.libsdl.org/SDL2/SDL_Palette says one should never allocate palette manually (yet there are SDL_(Alloc|Free)Palette functions exactly for this case, hmm), so it doesn't look like there's need for SDL_Palette wrapper. May it be that only SetSurfacePalette(SDL_Palette*) method for Surface is actually needed?

@whoozle
Copy link
Contributor Author

whoozle commented Sep 3, 2023

My use case is rendering 8-bit surfaces with palette 🙈 (I'm rewriting one of the old DOS RPGs in modern c++)

DOS games often use palette animations etc, so I need to modify palettes and explicitly assign them to surfaces.

RAII palette class/setter you mention would be nice to have 👍

@AMDmi3
Copy link
Member

AMDmi3 commented Sep 5, 2023

Thanks for clarification, but that doesn't really answer the main question of whether you use AllocPalette. Code excerpt could also be useful

@whoozle
Copy link
Contributor Author

whoozle commented Sep 5, 2023

sorry, I use it like this:

Palette::Palette() : _id(0), _index(0) {
	common_palette = Resource::load(Resource::ResCommonPalette);
	if (common_palette.size() != 0x40 * 3)
		throw Exception("invalid palette.000 size: " + std::to_string(common_palette.size()));
	_palette = SDL_AllocPalette(256);
	if (!_palette)
		throw SDL2pp::Exception("SDL_AllocPalette");
}

void Palette::rotate() {
	++_index;
	SDL_Color colors[0x100];
	for(auto i = _ranges.begin(); i != _ranges.end(); ++i) {
		unsigned start = i->first, size = i->second;
		for(unsigned c = 0; c < size; ++c) {
			unsigned new_c = start + ((c - _index) % size);
			uint8_t *src = new_c >= 0xc0?
				common_palette.data() + (new_c - 0xc0) * 3:
				palette.data() + new_c * 3;
			SDL_Color &color = colors[c];
			color.r = *src++;
			color.g = *src++;
			color.b = *src++;
		}
		SDL_SetPaletteColors(_palette, colors, start, size);
	}
}


Palette::~Palette()
{ SDL_FreePalette(_palette); }   

later I use SetSurfacePalette to associate palette with surface and it's working fine :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants