Giter Club home page Giter Club logo

Comments (4)

AMDmi3 avatar AMDmi3 commented on June 14, 2024

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?

from libsdl2pp.

whoozle avatar whoozle commented on June 14, 2024

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 👍

from libsdl2pp.

AMDmi3 avatar AMDmi3 commented on June 14, 2024

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

from libsdl2pp.

whoozle avatar whoozle commented on June 14, 2024

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 :)

from libsdl2pp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.