Skip to content
Snippets Groups Projects
Commit 1e557ffa authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

More SDL cleanup

parent a194a157
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
Pipeline #3494 passed
...@@ -47,6 +47,26 @@ aspect_fix(int *x, int *y, int aspect_width, int aspect_height) ...@@ -47,6 +47,26 @@ aspect_fix(int *x, int *y, int aspect_width, int aspect_height)
*x = bestx; *x = bestx;
} }
/*
* Corrects width/height to have the specified aspect ratio
*/
void
aspect_fix_low(int *x, int *y, int aspect_width, int aspect_height)
{
int bestx, besty;
// Nothing we can do here...
if (aspect_width == 0 || aspect_height == 0)
return;
bestx = lround((double)*y * aspect_width / aspect_height);
besty = lround((double)*x * aspect_height / aspect_width);
if (bestx < *x && bestx > 0)
*x = bestx;
else
*y = besty;
}
/* /*
* Given a width/height of a source image, adjust it to match the current aspect * Given a width/height of a source image, adjust it to match the current aspect
* ratio. Will not reduce either number * ratio. Will not reduce either number
......
...@@ -19,3 +19,4 @@ struct graphics_buffer * do_scale(struct rectlist* rect, int xscale, int yscale, ...@@ -19,3 +19,4 @@ struct graphics_buffer * do_scale(struct rectlist* rect, int xscale, int yscale,
void aspect_correct(int *x, int *y, int aspect_width, int aspect_height); void aspect_correct(int *x, int *y, int aspect_width, int aspect_height);
void aspect_reverse(int *x, int *y, int scrnwidth, int scrnheight, int aspect_width, int aspect_height); void aspect_reverse(int *x, int *y, int scrnwidth, int scrnheight, int aspect_width, int aspect_height);
void aspect_fix(int *x, int *y, int aspect_width, int aspect_height); void aspect_fix(int *x, int *y, int aspect_width, int aspect_height);
void aspect_fix_low(int *x, int *y, int aspect_width, int aspect_height);
...@@ -46,6 +46,8 @@ SDL_Cursor *curs=NULL; ...@@ -46,6 +46,8 @@ SDL_Cursor *curs=NULL;
SDL_Renderer *renderer=NULL; SDL_Renderer *renderer=NULL;
SDL_Texture *texture=NULL; SDL_Texture *texture=NULL;
pthread_mutex_t win_mutex; pthread_mutex_t win_mutex;
pthread_mutex_t sdl_mode_mutex;
bool sdl_mode;
SDL_Surface *sdl_icon=NULL; SDL_Surface *sdl_icon=NULL;
sem_t sdl_ufunc_ret; sem_t sdl_ufunc_ret;
...@@ -187,6 +189,7 @@ const struct sdl_keyvals sdl_keyval[] = ...@@ -187,6 +189,7 @@ const struct sdl_keyvals sdl_keyval[] =
}; };
void sdl_video_event_thread(void *data); void sdl_video_event_thread(void *data);
static void setup_surfaces_locked(void);
static void sdl_user_func(int func, ...) static void sdl_user_func(int func, ...)
{ {
...@@ -348,7 +351,7 @@ window_can_scale_internally(int winwidth, int winheight) ...@@ -348,7 +351,7 @@ window_can_scale_internally(int winwidth, int winheight)
static void static void
internal_scaling_factors(int winwidth, int winheight, int *x, int *y) internal_scaling_factors(int winwidth, int winheight, int *x, int *y)
{ {
aspect_fix(&winwidth, &winheight, cvstat.aspect_width, cvstat.aspect_height); aspect_fix_low(&winwidth, &winheight, cvstat.aspect_width, cvstat.aspect_height);
aspect_reverse(&winwidth, &winheight, cvstat.scrnwidth, cvstat.scrnheight, cvstat.aspect_width, cvstat.aspect_height); aspect_reverse(&winwidth, &winheight, cvstat.scrnwidth, cvstat.scrnheight, cvstat.aspect_width, cvstat.aspect_height);
*x = winwidth / cvstat.scrnwidth; *x = winwidth / cvstat.scrnwidth;
*y = winheight / cvstat.scrnheight; *y = winheight / cvstat.scrnheight;
...@@ -410,6 +413,9 @@ static int sdl_init_mode(int mode) ...@@ -410,6 +413,9 @@ static int sdl_init_mode(int mode)
cvstat = vstat; cvstat = vstat;
internal_scaling = window_can_scale_internally(vstat.winwidth, vstat.winheight); internal_scaling = window_can_scale_internally(vstat.winwidth, vstat.winheight);
pthread_mutex_lock(&sdl_mode_mutex);
sdl_mode = true;
pthread_mutex_unlock(&sdl_mode_mutex);
pthread_mutex_unlock(&vstatlock); pthread_mutex_unlock(&vstatlock);
pthread_mutex_unlock(&blinker_lock); pthread_mutex_unlock(&blinker_lock);
...@@ -607,7 +613,6 @@ static void setup_surfaces_locked(void) ...@@ -607,7 +613,6 @@ static void setup_surfaces_locked(void)
flags |= SDL_WINDOW_ALLOW_HIGHDPI; flags |= SDL_WINDOW_ALLOW_HIGHDPI;
#endif #endif
pthread_mutex_lock(&win_mutex); pthread_mutex_lock(&win_mutex);
idealmw = cvstat.scrnwidth; idealmw = cvstat.scrnwidth;
idealmh = cvstat.scrnheight; idealmh = cvstat.scrnheight;
...@@ -620,6 +625,9 @@ static void setup_surfaces_locked(void) ...@@ -620,6 +625,9 @@ static void setup_surfaces_locked(void)
if (win == NULL) { if (win == NULL) {
// SDL2: This is slow sometimes... not sure why. // SDL2: This is slow sometimes... not sure why.
if (sdl.CreateWindowAndRenderer(cvstat.winwidth, cvstat.winheight, flags, &win, &renderer) == 0) { if (sdl.CreateWindowAndRenderer(cvstat.winwidth, cvstat.winheight, flags, &win, &renderer) == 0) {
sdl.GetWindowSize(win, &idealw, &idealh);
cvstat.winwidth = idealw;
cvstat.winheight = idealh;
sdl.RenderClear(renderer); sdl.RenderClear(renderer);
if (internal_scaling) if (internal_scaling)
newtexture = sdl.CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, idealw, idealh); newtexture = sdl.CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, idealw, idealh);
...@@ -638,6 +646,9 @@ static void setup_surfaces_locked(void) ...@@ -638,6 +646,9 @@ static void setup_surfaces_locked(void)
else { else {
sdl.SetWindowMinimumSize(win, idealmw, idealmh); sdl.SetWindowMinimumSize(win, idealmw, idealmh);
sdl.SetWindowSize(win, idealw, idealh); sdl.SetWindowSize(win, idealw, idealh);
sdl.GetWindowSize(win, &idealw, &idealh);
cvstat.winwidth = idealw;
cvstat.winheight = idealh;
if (internal_scaling) if (internal_scaling)
newtexture = sdl.CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, idealw, idealh); newtexture = sdl.CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, idealw, idealh);
else else
...@@ -1022,43 +1033,17 @@ void sdl_video_event_thread(void *data) ...@@ -1022,43 +1033,17 @@ void sdl_video_event_thread(void *data)
case SDL_WINDOWEVENT_SIZE_CHANGED: case SDL_WINDOWEVENT_SIZE_CHANGED:
// SDL2: User resized window // SDL2: User resized window
case SDL_WINDOWEVENT_RESIZED: case SDL_WINDOWEVENT_RESIZED:
{ pthread_mutex_lock(&sdl_mode_mutex);
// SDL2: Something resized window if (sdl_mode) {
const char *newh; pthread_mutex_unlock(&sdl_mode_mutex);
pthread_mutex_lock(&vstatlock);
pthread_mutex_lock(&win_mutex);
internal_scaling = window_can_scale_internally(ev.window.data1, ev.window.data2);
if (internal_scaling) {
newh = "0";
}
else {
newh = "2";
}
sdl.GetWindowSize(win, &cvstat.winwidth, &cvstat.winheight);
if (strcmp(newh, sdl.GetHint(SDL_HINT_RENDER_SCALE_QUALITY))) {
SDL_Texture *newtexture;
sdl.SetHint(SDL_HINT_RENDER_SCALE_QUALITY, newh);
if (internal_scaling) {
int idealw, idealh;
idealw = cvstat.winwidth;
idealh = cvstat.winheight;
aspect_fix(&idealw, &idealh, cvstat.aspect_width, cvstat.aspect_height);
newtexture = sdl.CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, idealw, idealh);
}
else
newtexture = sdl.CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, cvstat.scrnwidth, cvstat.scrnheight);
sdl.RenderClear(renderer);
if (texture)
sdl.DestroyTexture(texture);
texture = newtexture;
}
sdl.RenderClear(renderer);
bitmap_drv_request_pixels();
pthread_mutex_unlock(&win_mutex);
pthread_mutex_unlock(&vstatlock);
break; break;
} }
pthread_mutex_unlock(&sdl_mode_mutex);
pthread_mutex_lock(&vstatlock);
sdl_setwinsize_locked(ev.window.data1, ev.window.data2);
setup_surfaces_locked();
pthread_mutex_unlock(&vstatlock);
break;
case SDL_WINDOWEVENT_EXPOSED: case SDL_WINDOWEVENT_EXPOSED:
bitmap_drv_request_pixels(); bitmap_drv_request_pixels();
break; break;
...@@ -1078,20 +1063,25 @@ void sdl_video_event_thread(void *data) ...@@ -1078,20 +1063,25 @@ void sdl_video_event_thread(void *data)
pthread_mutex_lock(&win_mutex); pthread_mutex_lock(&win_mutex);
if (win != NULL) { if (win != NULL) {
pthread_mutex_lock(&sdl_headlock); pthread_mutex_lock(&sdl_headlock);
pthread_mutex_lock(&sdl_mode_mutex);
list = update_list; list = update_list;
update_list = update_list_tail = NULL; update_list = update_list_tail = NULL;
bool skipit = sdl_mode;
pthread_mutex_unlock(&sdl_mode_mutex);
pthread_mutex_unlock(&sdl_headlock); pthread_mutex_unlock(&sdl_headlock);
for (; list; list = old_next) { for (; list; list = old_next) {
SDL_Rect src; SDL_Rect src;
SDL_Rect dst; SDL_Rect dst;
old_next = list->next; old_next = list->next;
if (list->next == NULL) { if (list->next == NULL && !skipit) {
void *pixels; void *pixels;
int pitch; int pitch;
int row; int row;
int tw, th; int tw, th;
sdl.RenderClear(renderer);
pthread_mutex_lock(&vstatlock);
if (internal_scaling) { if (internal_scaling) {
struct graphics_buffer *gb; struct graphics_buffer *gb;
int xscale, yscale; int xscale, yscale;
...@@ -1102,8 +1092,10 @@ void sdl_video_event_thread(void *data) ...@@ -1102,8 +1092,10 @@ void sdl_video_event_thread(void *data)
src.y = 0; src.y = 0;
src.w = gb->w; src.w = gb->w;
src.h = gb->h; src.h = gb->h;
sdl.QueryTexture(texture, NULL, NULL, &tw, &th); if (sdl.QueryTexture(texture, NULL, NULL, &tw, &th) != 0)
sdl.LockTexture(texture, &src, &pixels, &pitch); fprintf(stderr, "Unable to query texture (%s)\n", sdl.GetError());
if (sdl.LockTexture(texture, &src, &pixels, &pitch) != 0)
fprintf(stderr, "Unable to lock texture (%s)\n", sdl.GetError());
if (pitch != gb->w * sizeof(gb->data[0])) { if (pitch != gb->w * sizeof(gb->data[0])) {
// If this happens, we need to copy a row at a time... // If this happens, we need to copy a row at a time...
for (row = 0; row < gb->h && row < th; row++) { for (row = 0; row < gb->h && row < th; row++) {
...@@ -1132,8 +1124,10 @@ void sdl_video_event_thread(void *data) ...@@ -1132,8 +1124,10 @@ void sdl_video_event_thread(void *data)
src.y = 0; src.y = 0;
src.w = list->rect.width; src.w = list->rect.width;
src.h = list->rect.height; src.h = list->rect.height;
sdl.QueryTexture(texture, NULL, NULL, &tw, &th); if (sdl.QueryTexture(texture, NULL, NULL, &tw, &th) != 0)
sdl.LockTexture(texture, &src, &pixels, &pitch); fprintf(stderr, "Unable to query texture (%s)\n", sdl.GetError());
if (sdl.LockTexture(texture, &src, &pixels, &pitch) != 0)
fprintf(stderr, "Unable to lock texture (%s)\n", sdl.GetError());
if (pitch != list->rect.width * sizeof(list->data[0])) { if (pitch != list->rect.width * sizeof(list->data[0])) {
// If this happens, we need to copy a row at a time... // If this happens, we need to copy a row at a time...
for (row = 0; row < list->rect.height && row < th; row++) { for (row = 0; row < list->rect.height && row < th; row++) {
...@@ -1158,6 +1152,7 @@ void sdl_video_event_thread(void *data) ...@@ -1158,6 +1152,7 @@ void sdl_video_event_thread(void *data)
dst.x = (cvstat.winwidth - dst.w) / 2; dst.x = (cvstat.winwidth - dst.w) / 2;
dst.y = (cvstat.winheight - dst.h) / 2; dst.y = (cvstat.winheight - dst.h) / 2;
} }
pthread_mutex_unlock(&vstatlock);
if (sdl.RenderCopy(renderer, texture, &src, &dst)) if (sdl.RenderCopy(renderer, texture, &src, &dst))
fprintf(stderr, "RenderCopy() failed! (%s)\n", sdl.GetError()); fprintf(stderr, "RenderCopy() failed! (%s)\n", sdl.GetError());
} }
...@@ -1165,7 +1160,7 @@ void sdl_video_event_thread(void *data) ...@@ -1165,7 +1160,7 @@ void sdl_video_event_thread(void *data)
} }
sdl.RenderPresent(renderer); sdl.RenderPresent(renderer);
} }
pthread_mutex_unlock(&win_mutex); pthread_mutex_unlock(&win_mutex);
break; break;
case SDL_USEREVENT_SETNAME: case SDL_USEREVENT_SETNAME:
...@@ -1199,9 +1194,12 @@ void sdl_video_event_thread(void *data) ...@@ -1199,9 +1194,12 @@ void sdl_video_event_thread(void *data)
free(ev.user.data1); free(ev.user.data1);
break; break;
case SDL_USEREVENT_SETVIDMODE: case SDL_USEREVENT_SETVIDMODE:
pthread_mutex_lock(&sdl_mode_mutex);
sdl_mode = false;
pthread_mutex_unlock(&sdl_mode_mutex);
pthread_mutex_lock(&vstatlock); pthread_mutex_lock(&vstatlock);
cvstat.winwidth = ev.user.data1 - NULL; sdl_setwinsize(ev.user.data1 - NULL, ev.user.data2 - NULL);
cvstat.winheight = ev.user.data2 - NULL;
setup_surfaces_locked(); setup_surfaces_locked();
pthread_mutex_unlock(&vstatlock); pthread_mutex_unlock(&vstatlock);
sdl_ufunc_retval=0; sdl_ufunc_retval=0;
...@@ -1290,6 +1288,7 @@ int sdl_initciolib(int mode) ...@@ -1290,6 +1288,7 @@ int sdl_initciolib(int mode)
pthread_mutex_init(&sdl_headlock, NULL); pthread_mutex_init(&sdl_headlock, NULL);
pthread_mutex_init(&win_mutex, NULL); pthread_mutex_init(&win_mutex, NULL);
pthread_mutex_init(&sdl_keylock, NULL); pthread_mutex_init(&sdl_keylock, NULL);
pthread_mutex_init(&sdl_mode_mutex, NULL);
return(sdl_init(mode)); return(sdl_init(mode));
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment