347 {
348
350
352
353 assert(this->font);
354
356
357 if (this->font->scale != scale) {
358 this->font->scale = scale;
360 this->texture = release(this->texture);
361 this->textureSize = MakeSize(0, 0);
362 }
363
364 if (this->text) {
365
367
368 if (this->texture == NULL) {
369 SDL_Surface *surface;
370
371 if (this->colorEscapes) {
373 } else {
375 this->text,
376 this->color,
377 this->lineWrap ? frame.w : 0);
378 }
379
380 assert(surface);
381
382 const float textureWidth = roundf(surface->w / scale);
383 const float textureHeight = roundf(surface->h / scale);
384
385 this->textureSize.w = (int) textureWidth;
386 this->textureSize.h = (int) textureHeight;
387
388 SDL_Surface *upload = surface;
389 SDL_Surface *converted = NULL;
390
391 if (SDL_BYTESPERPIXEL(surface->format) == 1) {
392 converted = SDL_CreateSurface(surface->w, surface->h, SDL_PIXELFORMAT_RGBA32);
393 assert(converted);
394 const SDL_PixelFormatDetails *details = SDL_GetPixelFormatDetails(SDL_PIXELFORMAT_RGBA32);
395 assert(details);
396 const Uint8 *src = (const Uint8 *) surface->pixels;
397 Uint32 *dst = (Uint32 *) converted->pixels;
398 for (int y = 0; y < surface->h; y++) {
399 for (int x = 0; x < surface->w; x++) {
400 const Uint8 a = src[y * surface->pitch + x];
401 dst[y * surface->w + x] = SDL_MapRGBA(details, NULL, 255, 255, 255, a);
402 }
403 }
404 upload = converted;
405 } else if (surface->format != SDL_PIXELFORMAT_RGBA32) {
406 converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
407 assert(converted);
408 upload = converted;
409 }
410
411 const SDL_GPUTextureCreateInfo texInfo = {
412 .type = SDL_GPU_TEXTURETYPE_2D,
413 .format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM,
414 .usage = SDL_GPU_TEXTUREUSAGE_SAMPLER,
415 .width = (Uint32) upload->w,
416 .height = (Uint32) upload->h,
417 .layer_count_or_depth = 1,
418 .num_levels = 1,
419 };
420
421 this->texture = $(renderer->
device, createTexture, &texInfo, upload->pixels);
422
423 if (converted) {
424 SDL_DestroySurface(converted);
425 }
426
427 SDL_DestroySurface(surface);
428 }
429
430 assert(this->texture);
431
432 const SDL_Rect draw_rect = { frame.x, frame.y, this->textureSize.w, this->textureSize.h };
434 }
435}
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.