Skip to content

Commit

Permalink
Fix SDL2 builds and add SDL_FRect workaround.
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceLR committed Apr 15, 2024
1 parent 6545b61 commit 958a830
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 58 deletions.
64 changes: 55 additions & 9 deletions src/SDLmzx.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ __M_BEGIN_DECLS
#endif
#endif

#include <limits.h>

/* SDL1 backwards compatibility for SDL2 ************************************/

#if !SDL_VERSION_ATLEAST(2,0,0)
Expand Down Expand Up @@ -242,8 +244,8 @@ typedef SDL_GameControllerButton SDL_GamepadButton;
#define SDL_GAMEPAD_BUTTON_BACK SDL_CONTROLLER_BUTTON_BACK
#define SDL_GAMEPAD_BUTTON_GUIDE SDL_CONTROLLER_BUTTON_GUIDE
#define SDL_GAMEPAD_BUTTON_START SDL_CONTROLLER_BUTTON_START
#define SDL_GAMEPAD_BUTTON_LEFT_STICK SDL_CONTROLLER_BUTTON_LEFT_STICK
#define SDL_GAMEPAD_BUTTON_RIGHT_STICK SDL_CONTROLLER_BUTTON_RIGHT_STICK
#define SDL_GAMEPAD_BUTTON_LEFT_STICK SDL_CONTROLLER_BUTTON_LEFTSTICK
#define SDL_GAMEPAD_BUTTON_RIGHT_STICK SDL_CONTROLLER_BUTTON_RIGHTSTICK
#define SDL_GAMEPAD_BUTTON_LEFT_SHOULDER SDL_CONTROLLER_BUTTON_LEFTSHOULDER
#define SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
#define SDL_GAMEPAD_BUTTON_DPAD_UP SDL_CONTROLLER_BUTTON_DPAD_UP
Expand Down Expand Up @@ -308,25 +310,69 @@ static inline void SDL_SetJoystickEventsEnabled(SDL_bool enabled)
#define SDL_DestroyPalette(p) SDL_FreePalette(p)
#define SDL_CreatePixelFormat(f) SDL_AllocFormat(f)
#define SDL_DestroyPixelFormat(pf) SDL_FreeFormat(pf)
#define SDL_GetMasksForPixelFormatEnum(f,b,r,g,b,a) SDL_PixelFormatEnumToMasks(f,b,r,g,b,a)
#define SDL_GetMasksForPixelFormatEnum(f,b,R,G,B,A) SDL_PixelFormatEnumToMasks(f,b,R,G,B,A)
#endif

/**
* SDL_rect.h
*/
#if !SDL_VERSION_ATLEAST(2,0,10)
typedef struct { float x; float y; float w; float h; } SDL_FRect;
#endif

/* SDL_render.h */
#if SDL_VERSION_ATLEAST(3,0,0)
typedef SDL_FRect SDL_RenderRect;
static inline SDL_RenderRect sdl_render_rect(int x, int y,
int w, int h, int full_w, int full_h)
{
SDL_FRect tmp =
{
(float)x / full_w,
(float)y / full_h,
(float)w / full_w,
(float)h / full_h
};
return tmp;
}
#else
typedef SDL_Rect SDL_RenderRect;
static inline SDL_RenderRect sdl_render_rect(int x, int y,
int w, int h, int full_w, int full_h)
{
SDL_Rect tmp = { x, y, w, h };
return tmp;
}
#endif

/**
* SDL_render.h
*/
#if !SDL_VERSION_ATLEAST(3,0,0) && SDL_VERSION_ATLEAST(2,0,0)
typedef int SDL_RendererLogicalPresentation;
#define SDL_LOGICAL_PRESENTATION_DISABLED 0
#define SDL_SCALEMODE_BEST SDL_ScaleModeBest
#define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear
#define SDL_SCALEMODE_NEAREST SDL_ScaleModeNearest
#define SDL_SetRenderClipRect(r, rect) SDL_RenderSetClipRect(r, rect)
#define SDL_SCALEMODE_BEST SDL_ScaleModeBest
#define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear
#define SDL_SCALEMODE_NEAREST SDL_ScaleModeNearest
#define SDL_SetRenderClipRect(r, rect) SDL_RenderSetClipRect(r, rect)
#define SDL_SetRenderLogicalSize(r, w, h) SDL_RenderSetLogicalSize(r, w, h)

static inline int SDL_SetRenderLogicalPresentation(SDL_Renderer *render,
int w, int h, SDL_RendererLogicalPresentiation p, SDL_ScaleMode s)
int w, int h, SDL_RendererLogicalPresentation p, SDL_ScaleMode s)
{
return SDL_SetRenderLogicalSize(render, w, h);
}
#endif

static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_RenderRect *src_rect, const SDL_RenderRect *dest_rect)
{
#if SDL_VERSION_ATLEAST(3,0,0)
return SDL_RenderTexture(renderer, texture, src_rect, dest_rect);
#else
return SDL_RenderCopy(renderer, texture, src_rect, dest_rect);
#endif
}

/* SDL_surface.h */
#if !SDL_VERSION_ATLEAST(3,0,0) && SDL_VERSION_ATLEAST(2,0,0)
#define SDL_DestroySurface(s) SDL_FreeSurface(s)
Expand Down
2 changes: 1 addition & 1 deletion src/render_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ boolean sdlrender_set_video_mode(struct graphics_data *graphics,
// Flag removed in SDL3; all drivers support it and its presence needs to be
// tested by trying to create a target texture instead.
if(requires_blend_ops)
sdl_renderer_flags |= SDL_RENDERER_TARGETTEXTURE;
sdl_rendererflags |= SDL_RENDERER_TARGETTEXTURE;
#endif

if(graphics->sdl_render_driver[0])
Expand Down
50 changes: 19 additions & 31 deletions src/render_sdlaccel.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

#include "SDLmzx.h"
#include "graphics.h"
#include "platform.h"
#include "render.h"
#include "renderers.h"
#include "render_sdl.h"
Expand Down Expand Up @@ -551,13 +550,10 @@ static void sdlaccel_render_layer(struct graphics_data *graphics,
SDL_Texture *chars_tex = render_data->sdl.texture[TEX_CHARS];
SDL_Texture *bg_tex = render_data->sdl.texture[TEX_BACKGROUND];

#if SDL_VERSION_ATLEAST(3,0,0)
SDL_FRect dest_bg = { offx, offy, w * CHAR_W, h * CHAR_H };
SDL_FRect src_bg = { 0, 0, w, h };
#else
SDL_Rect dest_bg = { offx, offy, w * CHAR_W, h * CHAR_H };
SDL_Rect src_bg = { 0, 0, w, h };
#endif
SDL_RenderRect dest_bg =
sdl_render_rect(offx, offy, w * CHAR_W, h * CHAR_H, TEX_SCREEN_W, TEX_SCREEN_H);
SDL_RenderRect src_bg = sdl_render_rect(0, 0, w, h, TEX_BG_W, TEX_BG_H);

void *_bg;
int bg_pitch;
uint32_t *bg;
Expand Down Expand Up @@ -642,7 +638,7 @@ static void sdlaccel_render_layer(struct graphics_data *graphics,
}
}
SDL_UnlockTexture(bg_tex);
SDL_RenderTexture(renderer, bg_tex, &src_bg, &dest_bg);
SDL_RenderTexture_mzx(renderer, bg_tex, &src_bg, &dest_bg);

#ifdef RENDER_GEOMETRY

Expand All @@ -651,13 +647,8 @@ static void sdlaccel_render_layer(struct graphics_data *graphics,
#else /* !RENDER_GEOMETRY */
/* Render foreground and partially transparent chars. */
{
#if SDL_VERSION_ATLEAST(3,0,0)
SDL_FRect dest_rect = { 0, 0, CHAR_W, CHAR_H };
SDL_FRect src_rect = { 0, 0, CHAR_W, CHAR_H };
#else
SDL_Rect dest_rect = { 0, 0, CHAR_W, CHAR_H };
SDL_Rect src_rect = { 0, 0, CHAR_W, CHAR_H };
#endif
next = layer->data;
for(y = 0; y < h; y++)
{
Expand Down Expand Up @@ -686,7 +677,7 @@ static void sdlaccel_render_layer(struct graphics_data *graphics,
else
continue;

SDL_RenderTexture(renderer, chars_tex, &src_rect, &dest_rect);
SDL_RenderCopy(renderer, chars_tex, &src_rect, &dest_rect);
}
}
}
Expand Down Expand Up @@ -756,30 +747,27 @@ static void sdlaccel_sync_screen(struct graphics_data *graphics)
struct sdlaccel_render_data *render_data = graphics->render_data;
SDL_Renderer *renderer = render_data->sdl.renderer;
SDL_Texture *screen_tex = render_data->sdl.texture[TEX_SCREEN];
#if SDL_VERSION_ATLEAST(3,0,0)
SDL_FRect src;
SDL_FRect dest;
#else
SDL_Rect src;
SDL_Rect dest;
#endif
SDL_RenderRect src;
SDL_RenderRect dest;
int width = render_data->w;
int height = render_data->h;
int v_width, v_height;

src.x = 0;
src.y = 0;
src.w = SCREEN_PIX_W;
src.h = SCREEN_PIX_H;
src = sdl_render_rect(0, 0, SCREEN_PIX_W, SCREEN_PIX_H,
TEX_SCREEN_W, TEX_SCREEN_H);

fix_viewport_ratio(width, height, &v_width, &v_height, graphics->ratio);
dest.x = (width - v_width) / 2;
dest.y = (height - v_height) / 2;
dest.w = v_width;
dest.h = v_height;
dest = sdl_render_rect(
(width - v_width) / 2,
(height - v_height) / 2,
v_width,
v_height,
width,
height
);

SDL_SetRenderTarget(renderer, NULL);
SDL_RenderTexture(renderer, screen_tex, &src, &dest);
SDL_RenderTexture_mzx(renderer, screen_tex, &src, &dest);

SDL_RenderPresent(renderer);

Expand Down
30 changes: 13 additions & 17 deletions src/render_softscale.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "render_sdl.h"
#include "renderers.h"
#include "util.h"
#include "yuv.h"

struct softscale_render_data
{
Expand Down Expand Up @@ -266,34 +265,31 @@ static void softscale_sync_screen(struct graphics_data *graphics)
SDL_Renderer *renderer = render_data->sdl.renderer;
SDL_Texture *texture = render_data->sdl.texture[0];
SDL_Rect *texture_rect = &(render_data->texture_rect);
#if SDL_VERSION_ATLEAST(3,0,0)
SDL_FRect src_rect;
SDL_FRect dest_rect;
#else
SDL_Rect src_rect;
SDL_Rect dest_rect;
#endif
SDL_RenderRect src_rect;
SDL_RenderRect dest_rect;
int width = render_data->w;
int height = render_data->h;
int v_width, v_height;

src_rect.x = texture_rect->x;
src_rect.y = texture_rect->y;
src_rect.w = texture_rect->w;
src_rect.x = texture_rect->h;
src_rect = sdl_render_rect(texture_rect->x, texture_rect->y,
texture_rect->w, texture_rect->h, render_data->texture_width, SCREEN_PIX_H);

fix_viewport_ratio(width, height, &v_width, &v_height, graphics->ratio);
dest_rect.x = (width - v_width) / 2;
dest_rect.y = (height - v_height) / 2;
dest_rect.w = v_width;
dest_rect.h = v_height;
dest_rect = sdl_render_rect(
(width - v_width) / 2,
(height - v_height) / 2,
v_width,
v_height,
width,
height
);

softscale_unlock_texture(render_data);

SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);

SDL_RenderTexture(renderer, texture, &src_rect, &dest_rect);
SDL_RenderTexture_mzx(renderer, texture, &src_rect, &dest_rect);
SDL_RenderPresent(renderer);
}

Expand Down

0 comments on commit 958a830

Please sign in to comment.