How to draw solid text using sdl_ttf? - sdl

Here's an example:
SDL_Color textcol = {255, 255, 255};
SDL_Color backtextcol = {0, 0, 0};
SDL_Surface *mes = TTF_RenderText_Shaded(font, "The game has begun", textcol, backtextcol);
apply_surface(40, 40, mes, screen);
SDL_Flip(screen);
Thanks to that example, we draw a black rectangle with white letters. Is it possible to draw just white letters with black inside without the whole black rectangle?

Yes it is possible. There are 3 main text-drawing functions in SDL_TTF:
TTF_RenderText_Solid - this one renders basic text(whitout background) and its fast, but its low-quality, and will look pixellated.
TTF_RenderText_Shaded - this one draws nice blended characters, but to a pre-defined background color
TTF_RenderText_Blended - this one draws a nice blended text, but it uses alpha-blending and its slow, but gives very nice results

Related

How I can add a shadow to a texture on SDL2?

So, I'm making a 2D game and I have some textures. I will like some of them to drop a shadow, there is something like drop-shadow in css for SDL2?
Render the texture first, then render a slightly larger semi-transparent gray square slightly behind it. If you want rounded corners, use a shader that increases alpha as you get further from the corners.
Since noone mentionned it yet, here it is:
int SDL_SetTextureColorMod(SDL_Texture* texture,
Uint8 r,
Uint8 g,
Uint8 b)
https://wiki.libsdl.org/SDL_SetTextureColorMod
This function multiplies a texture color channels when copying it to the SDL_Renderer*. Using it with 0 as r, g and b arguments would make your texture pitch black but not affect the alpha, so the texture would keep its shape (like in the case of a transparent PNG). You just have to copy that shadow before (= behind) your texture, with a slight offset. You can also change the overall transparency of the shadow, with SDL_SetTextureAlphaMod(SDL_Texture* texture, Uint8 a)
Just don't forget to set the values back to 255 when you're done.
Code example:
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// [...] draw background, etc... here
SDL_Rect characterRect;
// [...] fill the rect however you want
SDL_Rect shadowRect(characterRect);
shadowRect.x += 30; // offsets the shadow
shadowRect.y += 30;
SDL_SetTextureColorMod(characterTexture, 0, 0, 0); // makes the texture black
SDL_SetTextureAlphaMod(characterTexture, 191); // makes the texture 3/4 of it's original opacity
SDL_RenderCopy(renderer, characterTexture, NULL, &shadowRect); // draws the shadow
SDL_SetTextureColorMod(characterTexture, 255, 255, 255); // sets the values back to normal
SDL_SetTextureAlphaMod(characterTexture, 255);
SDL_RenderCopy(renderer, characterTexture, NULL, &characterRect); // draws the character
// [...] draw UI, additionnal elements, etc... here
SDL_RenderPresent(renderer);

How to draw single-colour Ellipse (no black border) with QPainter

Code for the beginning:
QColor yellow("#f0d048");
Qt::BrushStyle style = Qt::SolidPattern;
QBrush brush(yellow, style);
painter.setBrush(brush);
painter.drawEllipse(10,10,10,10);
Everytime I do this, I get a yellow circle surrounded by a black 1-pixel-sized border. In total the circle will have the same size like if I draw with black colour, so what shall I do to just get a single-coloured yellow circle without black border?
Best regards
Set a pen on painter
painter.setPen(Qt::NoPen);
Qt has 'brush' for filling figures, and 'pen' for drawing lines and outlines.

CAIRO_OPERATOR_CLEAR not working as expected

I want to remove parts of a previously filled shape with Cairo and C++.
Consider the following MWE:
void test(cairo_t *cr){
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr); //background
cairo_rectangle(cr, 50, 50, 150, 150);
cairo_set_source_rgb(cr, 0, 0, 1);
cairo_fill(cr); //first rect
cairo_set_operator(cr,CAIRO_OPERATOR_CLEAR);
cairo_arc(cr, 100, 100, 100, 0, M_PI * 2);
cairo_fill(cr); //circle that show remove a part of the rect
}
It results in the following picture:
According to the documentation I would have expected no black color at all, and all parts of the blue rectangle, that are under the circle to become removed (and therefore white as the background).
Did I misunderstand the operator? Did I make any mistake?
How would cairo know what you consider the background?
The documentation that you link to mentions that the alpha channels and all color channels are set to 0. This is fully transparent black.
The example in the documentation is an image with an alpha channel and thus the cleared parts become transparent.
You are using an image without an alpha channel and thus the cleared parts become black.

Cocos2D - Change color of a sprite to what it was

I am changing the color of my sprite using the following code
sprite.color = ccc3(255, 0, 0);
it changes the color to red..
How can I change the color to what it was??
Thanks..
You can return to original color by using
sprite.color = ccc3(255, 255, 255);
the original color was not lost. the tint methods (ccc3 in this case) are not adding-color, but darkening the individual RGB channels. That's the reason you cant tint a black image to any other color.
In your example, you didn't paint your sprite Red. You've just cut all channels except the red one

Allegro 4.2.1, removing bmp background color

I have been building a game in allegro 4.2.1 and need help to remove a specific color to make invisible. The background color is, (255, 0, 255). I have been at the following sites, but they have not helped me much:
http://www.allegro.cc/forums/thread/599210,
http://www.cpp-home.com/tutorials/258_1.htm
If someone could provide me with an example, I would be very glad.
You need to do the following things to enable transparent pixels:
Call set_color_depth(32) before calling set_gfx_mode()
Load your images after calling set_gfx_mode()
Use masked_blit() or draw_sprite() to draw the image.
If you do the above, all "magic pink" pixels (100% red, 0% green, 100% blue) will be treated as transparent.
BITMAP *bmp;
allegro_init();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
clear_to_color(screen, makecol(0,0,0));
// once the video mode has been set, it is safe to create/load images.
// this bitmap will be 640x480 with pure pink.
bmp = create_bitmap(640, 480);
clear_to_color(bmp, makecol(255,0,255));
rectfill(bmp, 100,100, 200,200, makecol(255,255,255));
draw_sprite(screen, bmp, 0, 0);
// or
// masked_blit(bmp, screen, 0,0, 0,0, 640,480);