r-lyeh-archived / unifont

Embeddable console 1bpp font that supports many european/eastern unicode codepoints. Aimed to gamedev (C++11).
zlib License
13 stars 3 forks source link

How could I copy/render this in SDL2? #3

Open tony opened 8 years ago

tony commented 8 years ago

How would one go about using this with SDL?

r-lyeh-archived commented 8 years ago

Hey tony, you better want to use SDL_ttf instead, which is the proper SDL way. However, if you need to print into textures for some reason you would need to lock the texture surface, get the texture pointer, pass it to unifont and draw the string, then unlock the texture surface.

Some pseudocode following

SDL_PixelFormat pixelFormat; // <-- global var
Uint32* pixels = nullptr;
int format, pitch = 0;

// Get the size of the texture.
int w, h;
SDL_QueryTexture(texture, &format, nullptr, &w, &h);

// Now let's make our "pixels" pointer point to the texture data.
if (SDL_LockTexture(texture, nullptr, (void**)&pixels, &pitch)) {
    // If the locking fails, you might want to handle it somehow. SDL_GetError(); or something here.
}

pixelFormat.format = format;

// Now you want to format the color to a correct format that SDL can use.
// Basically we setup up our buffer and RGB to SDL color conversion.
unifont<Uint32> font( pixels, w, [&](uint8_t R, uint8_t G, uint8_t B, uint8_t A ) {
     return SDL_MapRGB(&pixelFormat, R, G, B);
} );

// Now we can print the lines we want
int ox = 100, oy = 100;
oy += font.render_string( ox, oy, 0|2, "gr: Χάρηκα για την γνωρημία. Η αθασιά της Αϊσές αν έχει αθάσια, ας έχει" ).h;
oy += font.render_string( ox, oy, 0|2, "hr: Cvrči cvrči cvrčak na čvoru crne smrče" ).h;
oy += font.render_string( ox, oy, 0|2, "is: Ég get etið gler án þess að meiða mig" ).h;

// Also don't forget to unlock your texture once you're done.
SDL_UnlockTexture(texture);