349 {
350
352
354
355 assert(this->font);
356
358
359 if (this->font->scale != scale) {
360 this->font->scale = scale;
362 this->texture = release(this->texture);
363 this->textureSize = MakeSize(0, 0);
364 }
365
366 if (this->text) {
367
369
370 if (this->texture == NULL) {
371 SDL_Surface *surface;
372
373 if (this->colorEscapes) {
375 } else {
377 this->text,
378 this->color,
379 this->lineWrap ? frame.w : 0);
380 }
381
382 assert(surface);
383
384 const float textureWidth = roundf(surface->w / scale);
385 const float textureHeight = roundf(surface->h / scale);
386
387 this->textureSize.w = (int) textureWidth;
388 this->textureSize.h = (int) textureHeight;
389
390 SDL_Surface *upload = surface;
391 SDL_Surface *converted = NULL;
392
393 if (SDL_BYTESPERPIXEL(surface->format) == 1) {
394 converted = SDL_CreateSurface(surface->w, surface->h, SDL_PIXELFORMAT_RGBA32);
395 assert(converted);
396 const SDL_PixelFormatDetails *details = SDL_GetPixelFormatDetails(SDL_PIXELFORMAT_RGBA32);
397 assert(details);
398 const Uint8 *src = (const Uint8 *) surface->pixels;
399 Uint32 *dst = (Uint32 *) converted->pixels;
400 for (int y = 0; y < surface->h; y++) {
401 for (int x = 0; x < surface->w; x++) {
402 const Uint8 a = src[y * surface->pitch + x];
403 dst[y * surface->w + x] = SDL_MapRGBA(details, NULL, 255, 255, 255, a);
404 }
405 }
406 upload = converted;
407 } else if (surface->format != SDL_PIXELFORMAT_RGBA32) {
408 converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
409 assert(converted);
410 upload = converted;
411 }
412
413 const SDL_GPUTextureCreateInfo texInfo = {
414 .type = SDL_GPU_TEXTURETYPE_2D,
415 .format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM,
416 .usage = SDL_GPU_TEXTUREUSAGE_SAMPLER,
417 .width = (Uint32) upload->w,
418 .height = (Uint32) upload->h,
419 .layer_count_or_depth = 1,
420 .num_levels = 1,
421 };
422
423 this->texture = $(renderer->
device, createTexture, &texInfo, upload->pixels);
424
425 if (converted) {
426 SDL_DestroySurface(converted);
427 }
428
429 SDL_DestroySurface(surface);
430 }
431
432 assert(this->texture);
433
434 const SDL_Rect draw_rect = { frame.x, frame.y, this->textureSize.w, this->textureSize.h };
436 }
437}
static SDL_Surface * renderCharacters(const Font *self, const char *chars, SDL_Color color, int wrapWidth)
static void drawTexture(const Renderer *self, Texture *texture, const SDL_Rect *rect, const SDL_Color *color)
static SDL_Surface * renderWithColorEscapes(const Text *self, int wrapWidth)
Renders text with color escape sequences applied to an SDL_Surface.
static float view_pixel_density(SDL_Window *window)
static SDL_Rect renderFrame(const View *self)
RenderDevice * device
The backing RenderDevice.
SDL_Window * window
The window.