comparison render_sdl.c @ 2429:da3dc881d3f0

Initial implementation of storbook artwork display
author Michael Pavone <pavone@retrodev.com>
date Sun, 04 Feb 2024 20:11:39 -0800
parents b50fa7602e39
children bed4d3db8a3f
comparison
equal deleted inserted replaced
2428:65c2e4d990cc 2429:da3dc881d3f0
1526 1526
1527 extra_renderers[win_idx] = NULL; 1527 extra_renderers[win_idx] = NULL;
1528 extra_windows[win_idx] = NULL; 1528 extra_windows[win_idx] = NULL;
1529 } 1529 }
1530 1530
1531 SDL_Texture **static_images;
1532 uint8_t num_static;
1533 uint8_t render_static_image(uint8_t window, char *path)
1534 {
1535 uint32_t fsize;
1536 uint8_t *buffer;
1537 if (is_absolute_path(path)) {
1538 FILE *f = fopen(path, "rb");
1539 if (!f) {
1540 return 0xFF;
1541 }
1542 fsize = file_size(f);
1543 buffer = calloc(1, fsize);
1544 if (fread(buffer, 1, fsize, f) != fsize) {
1545 free(buffer);
1546 fclose(f);
1547 return 0xFF;
1548 }
1549 fclose(f);
1550 } else {
1551 buffer = (uint8_t *)read_bundled_file(path, &fsize);
1552 }
1553 if (!buffer) {
1554 return 0xFF;
1555 }
1556
1557 uint32_t width, height;
1558 uint32_t *pixels = load_png(buffer, fsize, &width, &height);
1559 free(buffer);
1560 if (!pixels) {
1561 return 0xFF;
1562 }
1563 uint8_t img_index = 0;
1564 if (!num_static) {
1565 num_static = 8;
1566 static_images = calloc(num_static, sizeof(SDL_Texture*));
1567 }
1568 for (; img_index < num_static; img_index++)
1569 {
1570 if (!static_images[img_index]) {
1571 break;
1572 }
1573 }
1574 if (img_index == num_static) {
1575 num_static *= 2;
1576 static_images = realloc(static_images, num_static * sizeof(SDL_Texture*));
1577 }
1578 static_images[img_index] = SDL_CreateTexture(extra_renderers[window - FRAMEBUFFER_USER_START], SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, width, height);
1579 SDL_UpdateTexture(static_images[img_index], NULL, pixels, sizeof(uint32_t) * width);
1580 free(pixels);
1581 return img_index;
1582 }
1583
1584 void render_draw_image(uint8_t window, uint8_t image, int x, int y, int width, int height)
1585 {
1586 SDL_Rect dst = {
1587 .x = x,
1588 .y = y,
1589 .w = width,
1590 .h = height
1591 };
1592 SDL_RenderCopy(extra_renderers[window - FRAMEBUFFER_USER_START], static_images[image], NULL, &dst);
1593 }
1594
1595 void render_clear_window(uint8_t window, uint8_t r, uint8_t g, uint8_t b)
1596 {
1597 SDL_SetRenderDrawColor(extra_renderers[window - FRAMEBUFFER_USER_START], r, g, b, 255);
1598 SDL_RenderClear(extra_renderers[window - FRAMEBUFFER_USER_START]);
1599 }
1600
1601 void render_fill_rect(uint8_t window, uint8_t r, uint8_t g, uint8_t b, int x, int y, int width, int height)
1602 {
1603 SDL_SetRenderDrawColor(extra_renderers[window - FRAMEBUFFER_USER_START], r, g, b, 255);
1604 SDL_Rect dst = {
1605 .x = x,
1606 .y = y,
1607 .w = width,
1608 .h = height
1609 };
1610 SDL_RenderFillRect(extra_renderers[window - FRAMEBUFFER_USER_START], &dst);
1611 }
1612
1613 void render_window_refresh(uint8_t window)
1614 {
1615 SDL_RenderPresent(extra_renderers[window - FRAMEBUFFER_USER_START]);
1616 }
1617
1531 uint32_t *locked_pixels; 1618 uint32_t *locked_pixels;
1532 uint32_t locked_pitch; 1619 uint32_t locked_pitch;
1533 uint32_t *render_get_framebuffer(uint8_t which, int *pitch) 1620 uint32_t *render_get_framebuffer(uint8_t which, int *pitch)
1534 { 1621 {
1535 if (sync_src == SYNC_AUDIO_THREAD || sync_src == SYNC_EXTERNAL) { 1622 if (sync_src == SYNC_AUDIO_THREAD || sync_src == SYNC_EXTERNAL) {