2021-04-29 14:15:24 +02:00
|
|
|
#include <assert.h>
|
2022-06-25 02:21:15 +02:00
|
|
|
#include <stdio.h>
|
2021-04-29 14:15:24 +02:00
|
|
|
#include "renwindow.h"
|
|
|
|
|
|
|
|
#ifdef LITE_USE_SDL_RENDERER
|
|
|
|
static int query_surface_scale(RenWindow *ren) {
|
|
|
|
int w_pixels, h_pixels;
|
|
|
|
int w_points, h_points;
|
|
|
|
SDL_GL_GetDrawableSize(ren->window, &w_pixels, &h_pixels);
|
|
|
|
SDL_GetWindowSize(ren->window, &w_points, &h_points);
|
|
|
|
/* We consider that the ratio pixel/point will always be an integer and
|
|
|
|
it is the same along the x and the y axis. */
|
|
|
|
assert(w_pixels % w_points == 0 && h_pixels % h_points == 0 && w_pixels / w_points == h_pixels / h_points);
|
|
|
|
return w_pixels / w_points;
|
|
|
|
}
|
2021-05-05 23:20:30 +02:00
|
|
|
|
|
|
|
static void setup_renderer(RenWindow *ren, int w, int h) {
|
|
|
|
/* Note that w and h here should always be in pixels and obtained from
|
|
|
|
a call to SDL_GL_GetDrawableSize(). */
|
2023-01-12 00:53:23 +01:00
|
|
|
if (!ren->renderer) {
|
|
|
|
ren->renderer = SDL_CreateRenderer(ren->window, -1, 0);
|
|
|
|
}
|
|
|
|
if (ren->texture) {
|
2021-05-05 23:20:30 +02:00
|
|
|
SDL_DestroyTexture(ren->texture);
|
|
|
|
}
|
|
|
|
ren->texture = SDL_CreateTexture(ren->renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_STREAMING, w, h);
|
2023-03-19 20:39:52 +01:00
|
|
|
ren->rensurface.scale = query_surface_scale(ren);
|
2021-05-05 23:20:30 +02:00
|
|
|
}
|
2021-04-29 14:15:24 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2022-04-15 17:34:46 +02:00
|
|
|
void renwin_init_surface(UNUSED RenWindow *ren) {
|
2021-04-29 14:15:24 +02:00
|
|
|
#ifdef LITE_USE_SDL_RENDERER
|
2023-03-19 20:39:52 +01:00
|
|
|
if (ren->rensurface.surface) {
|
|
|
|
SDL_FreeSurface(ren->rensurface.surface);
|
2021-04-29 14:15:24 +02:00
|
|
|
}
|
|
|
|
int w, h;
|
|
|
|
SDL_GL_GetDrawableSize(ren->window, &w, &h);
|
2023-03-19 20:39:52 +01:00
|
|
|
ren->rensurface.surface = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, SDL_PIXELFORMAT_BGRA32);
|
|
|
|
if (!ren->rensurface.surface) {
|
2022-05-31 20:57:26 +02:00
|
|
|
fprintf(stderr, "Error creating surface: %s", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-05-05 23:20:30 +02:00
|
|
|
setup_renderer(ren, w, h);
|
2021-04-29 14:15:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-16 16:19:52 +02:00
|
|
|
void renwin_init_command_buf(RenWindow *ren) {
|
|
|
|
ren->command_buf = NULL;
|
|
|
|
ren->command_buf_idx = 0;
|
|
|
|
ren->command_buf_size = 0;
|
|
|
|
}
|
|
|
|
|
2021-04-29 14:15:24 +02:00
|
|
|
|
|
|
|
static RenRect scaled_rect(const RenRect rect, const int scale) {
|
|
|
|
return (RenRect) {rect.x * scale, rect.y * scale, rect.width * scale, rect.height * scale};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void renwin_clip_to_surface(RenWindow *ren) {
|
2023-03-19 20:39:52 +01:00
|
|
|
SDL_SetClipRect(renwin_get_surface(ren).surface, NULL);
|
2021-04-29 14:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void renwin_set_clip_rect(RenWindow *ren, RenRect rect) {
|
2023-03-19 20:39:52 +01:00
|
|
|
RenSurface rs = renwin_get_surface(ren);
|
|
|
|
RenRect sr = scaled_rect(rect, rs.scale);
|
|
|
|
SDL_SetClipRect(rs.surface, &(SDL_Rect){.x = sr.x, .y = sr.y, .w = sr.width, .h = sr.height});
|
2021-04-29 14:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-19 20:39:52 +01:00
|
|
|
RenSurface renwin_get_surface(RenWindow *ren) {
|
2021-04-29 14:15:24 +02:00
|
|
|
#ifdef LITE_USE_SDL_RENDERER
|
2023-03-19 20:39:52 +01:00
|
|
|
return ren->rensurface;
|
2021-04-29 14:15:24 +02:00
|
|
|
#else
|
2022-05-31 20:57:26 +02:00
|
|
|
SDL_Surface *surface = SDL_GetWindowSurface(ren->window);
|
|
|
|
if (!surface) {
|
|
|
|
fprintf(stderr, "Error getting window surface: %s", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-03-19 20:39:52 +01:00
|
|
|
return (RenSurface){.surface = surface, .scale = 1};
|
2021-04-29 14:15:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:34:46 +02:00
|
|
|
void renwin_resize_surface(UNUSED RenWindow *ren) {
|
2021-04-29 14:15:24 +02:00
|
|
|
#ifdef LITE_USE_SDL_RENDERER
|
|
|
|
int new_w, new_h;
|
|
|
|
SDL_GL_GetDrawableSize(ren->window, &new_w, &new_h);
|
|
|
|
/* Note that (w, h) may differ from (new_w, new_h) on retina displays. */
|
2023-03-19 20:39:52 +01:00
|
|
|
if (new_w != ren->rensurface.surface->w || new_h != ren->rensurface.surface->h) {
|
2021-04-29 14:15:24 +02:00
|
|
|
renwin_init_surface(ren);
|
|
|
|
renwin_clip_to_surface(ren);
|
|
|
|
setup_renderer(ren, new_w, new_h);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void renwin_show_window(RenWindow *ren) {
|
|
|
|
SDL_ShowWindow(ren->window);
|
|
|
|
}
|
|
|
|
|
|
|
|
void renwin_update_rects(RenWindow *ren, RenRect *rects, int count) {
|
|
|
|
#ifdef LITE_USE_SDL_RENDERER
|
2023-03-19 20:39:52 +01:00
|
|
|
const int scale = ren->rensurface.scale;
|
2021-04-29 14:15:24 +02:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
const RenRect *r = &rects[i];
|
|
|
|
const int x = scale * r->x, y = scale * r->y;
|
|
|
|
const int w = scale * r->width, h = scale * r->height;
|
|
|
|
const SDL_Rect sr = {.x = x, .y = y, .w = w, .h = h};
|
2023-03-19 20:39:52 +01:00
|
|
|
int32_t *pixels = ((int32_t *) ren->rensurface.surface->pixels) + x + ren->rensurface.surface->w * y;
|
|
|
|
SDL_UpdateTexture(ren->texture, &sr, pixels, ren->rensurface.surface->w * 4);
|
2021-04-29 14:15:24 +02:00
|
|
|
}
|
|
|
|
SDL_RenderCopy(ren->renderer, ren->texture, NULL, NULL);
|
|
|
|
SDL_RenderPresent(ren->renderer);
|
|
|
|
#else
|
|
|
|
SDL_UpdateWindowSurfaceRects(ren->window, (SDL_Rect*) rects, count);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void renwin_free(RenWindow *ren) {
|
|
|
|
SDL_DestroyWindow(ren->window);
|
|
|
|
ren->window = NULL;
|
|
|
|
#ifdef LITE_USE_SDL_RENDERER
|
|
|
|
SDL_DestroyTexture(ren->texture);
|
2021-05-05 23:20:30 +02:00
|
|
|
SDL_DestroyRenderer(ren->renderer);
|
2023-03-19 20:39:52 +01:00
|
|
|
SDL_FreeSurface(ren->rensurface.surface);
|
2021-04-29 14:15:24 +02:00
|
|
|
#endif
|
|
|
|
}
|