I'm trying to render text using SDL. Obviously SDL does not support rendering text by itself, so I went with this approach:
load font file
raster glyphs in the font to a bitmap
pack all bitmaps in a large texture, forming a spritesheet of glyphs
render text as a sequence of glyph-sprites: copy rectangles from the texture to the target
First steps are handled using FreeType library. It can generate bitmaps for many kinds of fonts and provide a lot of extra info about the glyphs. FreeType-generated bitmaps are (by default) alpha channel only. For every glyph I basically get a 2D array of A values in range 0 - 255. For simplicity reasons the MCVE below needs only SDL, I already embedded FreeType-generated bitmap in the source code.
Now, the question is: how should I manage the texture that consists of such bitmaps?
What blending mode should I use?
What modulation should I use?
What should the texture be filled with? FreeType provides alpha channel only, SDL generally wants a texture of RGBA pixels. What values should I use for RGB?
How do I draw text in specific color? I don't want to make a separate texture for each color.
FreeType documentation says: For optimal rendering on a screen the bitmap should be used as an alpha channel in linear blending with gamma correction. SDL blending mode documentation doesn't list anything named linear blending so I used a custom one but I'm not sure if I got it right.
I'm not sure if I got some of SDL calls right as some of them are poorly documented (I already know that locking with empty rectangles crashes on Direct3D), especially how to copy data using SDL_LockTexture.
#include <string>
#include <stdexcept>
#include <SDL.h>
constexpr unsigned char pixels[] = {
0, 0, 0, 0, 0, 0, 0, 30, 33, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 169, 255, 155, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 83, 255, 255, 229, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 189, 233, 255, 255, 60, 0, 0, 0, 0, 0,
0, 0, 0, 0, 33, 254, 83, 250, 255, 148, 0, 0, 0, 0, 0,
0, 0, 0, 0, 129, 227, 2, 181, 255, 232, 3, 0, 0, 0, 0,
0, 0, 0, 2, 224, 138, 0, 94, 255, 255, 66, 0, 0, 0, 0,
0, 0, 0, 68, 255, 48, 0, 15, 248, 255, 153, 0, 0, 0, 0,
0, 0, 0, 166, 213, 0, 0, 0, 175, 255, 235, 4, 0, 0, 0,
0, 0, 16, 247, 122, 0, 0, 0, 88, 255, 255, 71, 0, 0, 0,
0, 0, 105, 255, 192, 171, 171, 171, 182, 255, 255, 159, 0, 0, 0,
0, 0, 203, 215, 123, 123, 123, 123, 123, 196, 255, 239, 6, 0, 0,
0, 44, 255, 108, 0, 0, 0, 0, 0, 75, 255, 255, 77, 0, 0,
0, 142, 252, 22, 0, 0, 0, 0, 0, 5, 238, 255, 164, 0, 0,
5, 234, 184, 0, 0, 0, 0, 0, 0, 0, 156, 255, 242, 8, 0,
81, 255, 95, 0, 0, 0, 0, 0, 0, 0, 68, 255, 255, 86, 0,
179, 249, 14, 0, 0, 0, 0, 0, 0, 0, 3, 245, 255, 195, 0
};
[[noreturn]] inline void throw_error(const char* desc, const char* sdl_err)
{
throw std::runtime_error(std::string(desc) + sdl_err);
}
void update_pixels(
SDL_Texture& texture,
const SDL_Rect& texture_rect,
const unsigned char* src_alpha,
int src_size_x,
int src_size_y)
{
void* pixels;
int pitch;
if (SDL_LockTexture(&texture, &texture_rect, &pixels, &pitch))
throw_error("could not lock texture: ", SDL_GetError());
auto pixel_buffer = reinterpret_cast<unsigned char*>(pixels);
for (int y = 0; y < src_size_y; ++y) {
for (int x = 0; x < src_size_x; ++x) {
// this assumes SDL_PIXELFORMAT_RGBA8888
unsigned char* const rgba = pixel_buffer + x * 4;
unsigned char& r = rgba[0];
unsigned char& g = rgba[1];
unsigned char& b = rgba[2];
unsigned char& a = rgba[3];
r = 0xff;
g = 0xff;
b = 0xff;
a = src_alpha[x];
}
src_alpha += src_size_y;
pixel_buffer += pitch;
}
SDL_UnlockTexture(&texture);
}
int main(int /* argc */, char* /* argv */[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
throw_error("could not init SDL: ", SDL_GetError());
SDL_Window* window = SDL_CreateWindow("Hello World",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
1024, 768,
SDL_WINDOW_RESIZABLE);
if (!window)
throw_error("could not create window: ", SDL_GetError());
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer)
throw_error("could not create renderer: ", SDL_GetError());
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, 512, 512);
if (!texture)
throw_error("could not create texture: ", SDL_GetError());
SDL_SetTextureColorMod(texture, 255, 0, 0);
SDL_Rect src_rect;
src_rect.x = 0;
src_rect.y = 0;
src_rect.w = 15;
src_rect.h = 17;
update_pixels(*texture, src_rect, pixels, src_rect.w, src_rect.h);
/*
* FreeType documentation: For optimal rendering on a screen the bitmap should be used as
* an alpha channel in linear blending with gamma correction.
*
* The blending used is therefore:
* dstRGB = (srcRGB * srcA) + (dstRGB * (1 - srcA))
* dstA = (srcA * 0) + (dstA * 1) = dstA
*/
SDL_BlendMode blend_mode = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD);
if (SDL_SetTextureBlendMode(texture, blend_mode))
throw_error("could not set texture blending: ", SDL_GetError());
while (true) {
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
SDL_RenderClear(renderer);
SDL_Rect dst_rect;
dst_rect.x = 100;
dst_rect.y = 100;
dst_rect.w = src_rect.w;
dst_rect.h = src_rect.h;
SDL_RenderCopy(renderer, texture, &src_rect, &dst_rect);
SDL_RenderPresent(renderer);
SDL_Delay(16);
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
return 0;
}
break;
case SDL_QUIT:
return 0;
}
}
}
return 0;
}
Expected result: red letter "A" on yellow background.
Actual result: malformed red lines inside black square on yellow background.
I suspect that lines are broken because there is a bug within pointer arithmetics inside update_pixels but I have no idea what's causing the black square.
First of all, part of this stuff is already done in SDL_ttf library. You could use it to rasterise glyphs to surfaces or generate multichar text surface.
Your src_alpha += src_size_y; is incorrect - you copy row by row, but skip by column length, not row length. It should be src_size_x. That results in incorrect offset on each row and only first row of your copied image is correct.
Your colour packing when writing to texture is backwards. See https://wiki.libsdl.org/SDL_PixelFormatEnum#order - Packed component order (high bit -> low bit): SDL_PACKEDORDER_RGBA, meaning R is packed at highest bits while A is at lowest. So, when representing it with unsigned char*, First byte is A and fourth is R:
unsigned char& r = rgba[3];
unsigned char& g = rgba[2];
unsigned char& b = rgba[1];
unsigned char& a = rgba[0];
You don't need custom blending, use SDL_BLENDMODE_BLEND, that is 'standard' "src-alpha, one-minus-src-alpha" formula everyone uses (note that it does not blend dst alpha channel itself, nor uses it to any extent; when blending, we only care about src alpha).
Finally one more approach to this: you could put your glyph luminance value (alpha, whatever it is called, the point is it only have one channel) and put it into every channel. That way you could do additive blending without using alpha at all, don't even need RGBA texture. Glyph colour could still be multiplied with colour mod. SDL_ttf implements just that.
My Hard drive got corrupted and I had to set everything up again.
The Problem is that previously working code is now throwing errors.
QList<QColor> colors = {
QColor(0, 255, 255, 255),
QColor(0, 200, 255, 255),
QColor(0, 170, 255, 255),
QColor(0, 150, 255, 255),
QColor(0, 130, 255, 255),
};
Error:
D:\dev\est_tsd\tests\testgis.cpp:19: error: C2440: ‘initializing’: cannot convert from 'initializer-list' to 'QList'
No constructor could take the source type, or constructor overload resolution was ambiguous
I read that Qt supports initializer list with QList now
(Name of the kit: Desktop Qt 5.3 MSVC2013 OpenGL 64bit). What am I missing?
Help would be much appreciated.
You are using copy initialization semantic instead of direct list initialization. You should check if you have in .pro file:
CONFIG += c++11
and then use:
QList<QColor> colors{
QColor(0, 255, 255, 255),
QColor(0, 200, 255, 255),
QColor(0, 170, 255, 255),
QColor(0, 150, 255, 255),
QColor(0, 130, 255, 255)
};
Try removing the last comma.
It becomes:
QList<QColor> colors = {
QColor(0, 255, 255, 255),
QColor(0, 200, 255, 255),
QColor(0, 170, 255, 255),
QColor(0, 150, 255, 255),
QColor(0, 130, 255, 255)};
i.e. if I specify some cubic lines from example specified in Qt5 tutorial:
QPainterPath path;
path.addRect(20, 20, 60, 60);
path.moveTo(0, 0);
path.cubicTo(99, 0, 50, 50, 99, 99);
path.cubicTo(0, 99, 50, 50, 0, 0);
QPainter painter(this);
painter.fillRect(0, 0, 100, 100, Qt::white);
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt::SolidLine,
Qt::FlatCap, Qt::MiterJoin));
painter.setBrush(QColor(122, 163, 39));
painter.drawPath(path);
which constructs this set of curves
Now I'd like to render only a part of those curves on QImage specified by some region with starting point=[20px,50px] with width=80px and height=50px so resulting would look like this:
Or if it is possible, to render with 3x zoom, so resulting QImage would look the same but had size=[240px,150px]
I am new to Qt, so could someone please showed me a working code example?
You can transform the painter coordinate system:
QPainter painter(this);
painter.scale(3, 3); // zoom 3 times
painter.translate(-20, -50); // offset origin to 20x50
// ... render stuff
This has an advantage over the other answer, because it will be rendered as if you provided larger coordinates, instead of rendering it small and then enlarging the raster image, which will degrade image quality. Also, it is possible that Qt will optimize it to not render outside of the image, so it will render less, and you don't need to crop and throw results away.
Result:
Compare that to an upscaled raster:
I've got a code example for you. But it isn't that hard to find out how. You just have to read the documentation, all is easy to find. Qt's documentation is really great.
QApplication a(argc, argv);
QImage img(100, 100, QImage::Format_ARGB32);
QPainterPath path;
path.addRect(20, 20, 60, 60);
path.moveTo(0, 0);
path.cubicTo(99, 0, 50, 50, 99, 99);
path.cubicTo(0, 99, 50, 50, 0, 0);
QPainter painter(&img);
painter.fillRect(0, 0, 100, 100, Qt::white);
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt::SolidLine,
Qt::FlatCap, Qt::MiterJoin));
painter.setBrush(QColor(122, 163, 39));
painter.drawPath(path);
painter.end();
QPixmap pixmap( QPixmap::fromImage(img).copy(20, 50, 80, 50).scaled(240,150) );
// option 1, use a QLabel ( only for simple cases )
QLabel label;
label.setPixmap( pixmap );
label.show();
// option 2, use a QGraphicsScene ( far more flexible )
QGraphicsView view;
QGraphicsScene scene;
scene.addPixmap( pixmap );
scene.setSceneRect( img.rect() );
view.setScene(&scene);
view.show();
return a.exec();
This is what i Have so far.
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rect);
GetWindowRect(hwnd, &size);
width = size.right - size.left;
itoa(width, Swidth, 10);
height = size.bottom - size.top;
itoa(height, Sheight, 10);
itoa(rect.bottom, sBottom, 10);
itoa(rect.top, sTop, 10);
itoa(rect.left, sLeft, 10);
itoa(rect.right, sRight, 10);
TextOut(hdc, 0, 0, "Here is my width: ", 18);
TextOut(hdc, 125, 0, Swidth, 5);
TextOut(hdc, 175, 0, "Here is my height: ", 18);
TextOut(hdc, 300, 0, Sheight, 4);
TextOut(hdc, 0, 20, sBottom, strlen(sBottom));
TextOut(hdc, 50, 20, sTop, strlen(sTop));
TextOut(hdc, 100, 20, sRight, strlen(sRight));
TextOut(hdc, 150, 20, sLeft, strlen(sLeft));
TextOut(hdc, 0, 40, "Right Button Clicked: ", 23);
itoa(rightButtonClicked, SrightButtonClicked, 10);
TextOut(hdc, 150, 40, SrightButtonClicked, strlen(SrightButtonClicked));
if(rightButtonClicked > 20)
TextOut(hdc, 0, 60, SrightButtonClicked, strlen(SrightButtonClicked));
EndPaint (hwnd, &ps);
return 0;
case WM_LBUTTONDOWN:
return 0;
case WM_RBUTTONDOWN:
rightButtonClicked++;
return 0
Now I'm not sure what I"m doing wrong but I'm supposed to make it output the number of times I right click within the windows. The counter I have for rightButtonClicked incremetns but it won't display properly. Yes this is homework and I have researched the topic quite a bit so I'm looking for a little help.
I'm writing a simple program for testing mouse. It compiles fine, but doesn't work. When I launch it, the window freezes. What am I doing wrong?
#include <SDL/SDL.h>
#undef main
int main()
{
if (SDL_Init (SDL_INIT_EVERYTHING) != 0)
return 1;
SDL_Surface* Scr;
if ((Scr = SDL_SetVideoMode (300, 200, 32, 0)) == 0)
return 2;
SDL_Rect Mouse1 = {50, 50, 50, 100};
SDL_Rect Mouse3 = {150, 50, 50, 100};
SDL_Rect Mouse2 = {250, 50, 50, 100};
SDL_Surface Colors;
SDL_Rect Click = {0, 0, 50, 100};
SDL_Rect NoClick = {50, 0, 50, 100};
SDL_FillRect (Scr, 0, SDL_MapRGB (Scr->format, 255, 255, 255));
SDL_FillRect (&Colors, &Click, SDL_MapRGB (Colors.format, 255, 0, 0));
SDL_FillRect (&Colors, &NoClick, SDL_MapRGB (Colors.format, 0, 0, 255));
while (true)
{
if (SDL_GetMouseState (0, 0) & SDL_BUTTON(1))
SDL_BlitSurface (&Colors, &Click, Scr, &Mouse1);
else
SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse1);
if (SDL_GetMouseState (0, 0) & SDL_BUTTON(2))
SDL_BlitSurface (&Colors, &Click, Scr, &Mouse2);
else
SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse2);
if (SDL_GetMouseState (0, 0) & SDL_BUTTON(3))
SDL_BlitSurface (&Colors, &Click, Scr, &Mouse3);
else
SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse3);
if (SDL_GetKeyState (0) [SDLK_ESCAPE])
return 0;
SDL_Delay (17);
}
}
You are not processing any events. In your case, call SDL_PumpEvents to make SDL process them and update all its internal states:
while (true)
{
SDL_PumpEvents();
// The rest is the same ...
}