Skip to content

Commit

Permalink
Clean up SDL_RenderTexture, SDL_FRect/SDL_Rect #ifdefs, misc.
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceLR committed Nov 5, 2024
1 parent 26217b9 commit a390c9b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 59 deletions.
34 changes: 16 additions & 18 deletions src/SDLmzx.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,14 @@ typedef struct { float x; float y; float w; float h; } SDL_FRect;

#if SDL_VERSION_ATLEAST(3,0,0)
typedef SDL_FRect SDL_Rect_mzx;
static inline SDL_Rect_mzx sdl_render_rect(int x, int y,
int w, int h, int full_w, int full_h)
static inline SDL_Rect_mzx sdl_render_rect(int x, int y, int w, int h)
{
SDL_FRect tmp = { (float)x, (float)y, (float)w, (float)h };
return tmp;
}
#elif SDL_VERSION_ATLEAST(2,0,0)
typedef SDL_Rect SDL_Rect_mzx;
static inline SDL_Rect_mzx sdl_render_rect(int x, int y,
int w, int h, int full_w, int full_h)
static inline SDL_Rect_mzx sdl_render_rect(int x, int y, int w, int h)
{
SDL_Rect tmp = { x, y, w, h };
return tmp;
Expand All @@ -374,29 +372,24 @@ typedef int SDL_RendererLogicalPresentation;
#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)
#define SDL_SetRenderClipRect(r, rect) (SDL_RenderSetClipRect((r), (rect)) == 0)

#if !SDL_VERSION_ATLEAST(2,0,12)
typedef int SDL_ScaleMode;
#endif

static inline int SDL_SetRenderLogicalPresentation(SDL_Renderer *render,
static inline SDL_bool SDL_SetRenderLogicalPresentation(SDL_Renderer *render,
int w, int h, SDL_RendererLogicalPresentation p, SDL_ScaleMode s)
{
return SDL_SetRenderLogicalSize(render, w, h);
// Return value 0/-1 -> true/false
return (SDL_bool)(SDL_RenderSetLogicalSize(render, w, h) == 0);
}
#endif

#if SDL_VERSION_ATLEAST(2,0,0)
static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *texture,
const SDL_Rect_mzx *src_rect, const SDL_Rect_mzx *dest_rect)
static inline SDL_bool SDL_RenderTexture(SDL_Renderer *renderer,
SDL_Texture *texture, const SDL_Rect_mzx *src_rect, const SDL_Rect_mzx *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
// Return value 0/-1 -> true/false
return (SDL_bool)(SDL_RenderCopy(renderer, texture, src_rect, dest_rect) == 0);
}
#endif

Expand All @@ -407,7 +400,12 @@ static inline int SDL_RenderTexture_mzx(SDL_Renderer *renderer, SDL_Texture *tex
#define SDL_SCALEMODE_NEAREST SDL_ScaleModeNearest
#define SDL_SCALEMODE_LINEAR SDL_ScaleModeLinear
#define SDL_DestroySurface(s) SDL_FreeSurface(s)
#define SDL_FillSurfaceRect(s,r,c) SDL_FillRect(s,r,c)

static inline SDL_bool SDL_FillSurfaceRect(SDL_Surface *surface, SDL_Rect *rect,
Uint32 color)
{
return (SDL_bool)(SDL_FillRect(surface, rect, color) == 0);
}

static inline SDL_bool SDL_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect)
{
Expand Down
4 changes: 2 additions & 2 deletions src/event_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1800,9 +1800,9 @@ void platform_init_event(void)
#endif

#if SDL_VERSION_ATLEAST(2,0,0)
SDL_SetGamepadEventsEnabled(true);
SDL_SetGamepadEventsEnabled(1);
load_gamecontrollerdb();
#endif

SDL_SetJoystickEventsEnabled(true);
SDL_SetJoystickEventsEnabled(1);
}
14 changes: 2 additions & 12 deletions src/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1488,22 +1488,12 @@ static boolean icon_w_h_constraint(png_uint_32 w, png_uint_32 h)
return (w == h) && ((w % 16) == 0) && ((h % 16) == 0);
}

#if SDL_VERSION_ATLEAST(2,0,0)
static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h,
png_uint_32 *stride, void **pixels)
{
#if SDL_VERSION_ATLEAST(2,0,0)
SDL_Surface *s = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_RGBA32);
if(!s)
return NULL;

*stride = s->pitch;
*pixels = s->pixels;
return s;
}
#else
static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h,
png_uint_32 *stride, void **pixels)
{
Uint32 rmask, gmask, bmask, amask;
SDL_Surface *s;

Expand All @@ -1520,14 +1510,14 @@ static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h,
#endif

s = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, rmask, gmask, bmask, amask);
#endif
if(!s)
return NULL;

*stride = s->pitch;
*pixels = s->pixels;
return s;
}
#endif

static SDL_Surface *png_read_icon(const char *name)
{
Expand Down
8 changes: 1 addition & 7 deletions src/render_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,6 @@ static void find_texture_format(struct graphics_data *graphics,
if(!strcmp(renderer_name, SDL_SOFTWARE_RENDERER))
is_software_renderer = true;

// thanks for the michael mouse API
formats = (const uint32_t *)SDL_GetPointerProperty(props,
SDL_PROP_RENDERER_TEXTURE_FORMATS_POINTER, NULL);
num_formats = 0;
Expand Down Expand Up @@ -1168,12 +1167,7 @@ void sdl_set_texture_scale_mode(struct graphics_data *graphics,
else
mode = SDL_SCALEMODE_LINEAR;

#if SDL_VERSION_ATLEAST(3,0,0)
if(!SDL_SetTextureScaleMode(render_data->texture[texture_id], mode))
#else
if(SDL_SetTextureScaleMode(render_data->texture[texture_id], mode) < 0)
#endif
warn("Failed to set texture %d scale mode: %s\n", texture_id, SDL_GetError());
SDL_SetTextureScaleMode(render_data->texture[texture_id], mode);
}
else
warn("Texture %d is null!\n", texture_id);
Expand Down
25 changes: 8 additions & 17 deletions src/render_sdlaccel.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,8 @@ 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];

SDL_Rect_mzx dest_bg =
sdl_render_rect(offx, offy, w * CHAR_W, h * CHAR_H, TEX_SCREEN_W, TEX_SCREEN_H);
SDL_Rect_mzx src_bg = sdl_render_rect(0, 0, w, h, TEX_BG_W, TEX_BG_H);
SDL_Rect_mzx dest_bg = sdl_render_rect(offx, offy, w * CHAR_W, h * CHAR_H);
SDL_Rect_mzx src_bg = sdl_render_rect(0, 0, w, h);

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

#ifdef RENDER_GEOMETRY

Expand Down Expand Up @@ -714,11 +713,8 @@ static void sdlaccel_render_cursor(struct graphics_data *graphics, unsigned x,
uint8_t *palette = render_data->palette;

// Input coordinates are on the character grid.
#if SDL_VERSION_ATLEAST(3,0,0)
SDL_FRect dest = { x * CHAR_W, y * CHAR_H + offset, CHAR_W, lines };
#else
SDL_Rect dest = { x * CHAR_W, y * CHAR_H + offset, CHAR_W, lines };
#endif
SDL_Rect_mzx dest =
sdl_render_rect(x * CHAR_W, y * CHAR_H + offset, CHAR_W, lines);

SDL_SetRenderDrawBlendMode(render_data->sdl.renderer, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(render_data->sdl.renderer,
Expand All @@ -734,11 +730,7 @@ static void sdlaccel_render_mouse(struct graphics_data *graphics, unsigned x,
struct sdlaccel_render_data *render_data = graphics->render_data;

// Input coordinates are pixel values.
#if SDL_VERSION_ATLEAST(3,0,0)
SDL_FRect dest = { x, y, w, h };
#else
SDL_Rect dest = { x, y, w, h };
#endif
SDL_Rect_mzx dest = sdl_render_rect(x, y, w, h);

/* There is no preset inversion blend mode, so make a custom blend mode.
* Lower SDL versions should simply fall back to drawing a white rectangle.
Expand Down Expand Up @@ -769,16 +761,15 @@ static void sdlaccel_sync_screen(struct graphics_data *graphics,
SDL_Rect_mzx src;
SDL_Rect_mzx dest;

src = sdl_render_rect(0, 0, SCREEN_PIX_W, SCREEN_PIX_H,
TEX_SCREEN_W, TEX_SCREEN_H);
src = sdl_render_rect(0, 0, SCREEN_PIX_W, SCREEN_PIX_H);

dest.x = window->viewport_x;
dest.y = window->viewport_y;
dest.w = window->viewport_width;
dest.h = window->viewport_height;

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

SDL_RenderPresent(renderer);

Expand Down
4 changes: 2 additions & 2 deletions src/render_softscale.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ static void softscale_sync_screen(struct graphics_data *graphics,
SDL_Rect_mzx dest_rect;

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

dest_rect.x = window->viewport_x;
dest_rect.y = window->viewport_y;
Expand All @@ -282,7 +282,7 @@ static void softscale_sync_screen(struct graphics_data *graphics,
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);

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

Expand Down
2 changes: 1 addition & 1 deletion unit/configure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#ifdef CONFIG_SDL
#include "../src/SDLmzx.h"
#undef IGNORE /* wtf Windows? */
#undef IGNORE /* Windows defines this for some reason... */
#endif

#ifdef CONFIG_EDITOR
Expand Down

0 comments on commit a390c9b

Please sign in to comment.