Skip to content

Commit

Permalink
More modern / correct SDL2 rendering. Fixes issue with dragging to
Browse files Browse the repository at this point in the history
alternate screen (see #61). sdltest needs work yet.
  • Loading branch information
wmcbrine committed Dec 11, 2019
1 parent 35bef88 commit 6e69627
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 127 deletions.
1 change: 0 additions & 1 deletion sdl2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ include pdcsdl.h, or just add the declarations you need in your code:
PDCEX SDL_Surface *pdc_screen, *pdc_font, *pdc_icon, *pdc_back;
PDCEX int pdc_sheight, pdc_swidth, pdc_yoffset, pdc_xoffset;

PDCEX void PDC_update_rects(void);
PDCEX void PDC_retile(void);

pdc_window is the main window, created by SDL_CreateWindow(), unless
Expand Down
91 changes: 5 additions & 86 deletions sdl2/pdcdisp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,10 @@
# include "../common/acs437.h"
#endif

#define MAXRECT 200 /* maximum number of rects to queue up before
an update is forced; the number was chosen
arbitrarily */

static SDL_Rect uprect[MAXRECT]; /* table of rects to update */
static chtype oldch = (chtype)(-1); /* current attribute */
static int rectcount = 0; /* index into uprect */
static short foregr = -2, backgr = -2; /* current foreground, background */
static bool blinked_off = FALSE;

/* do the real updates on a delay */

void PDC_update_rects(void)
{
int i;

if (rectcount)
{
/* if the maximum number of rects has been reached, we're
probably better off doing a full screen update */

if (rectcount == MAXRECT)
SDL_UpdateWindowSurface(pdc_window);
else
{
int w = pdc_screen->w;
int h = pdc_screen->h;

for (i = 0; i < rectcount; i++)
{
if (uprect[i].x > w ||
uprect[i].y > h ||
!uprect[i].w || !uprect[i].h)
{
if (i + 1 < rectcount)
{
memmove(uprect + i, uprect + i + 1,
(rectcount - i + 1) * sizeof(*uprect));
--i;
}
rectcount--;
continue;
}

if (uprect[i].x + uprect[i].w > w)
uprect[i].w = min(w, w - uprect[i].x);

if (uprect[i].y + uprect[i].h > h)
uprect[i].h = min(h, h - uprect[i].y);
}

if (rectcount > 0)
SDL_UpdateWindowSurfaceRects(pdc_window, uprect, rectcount);
}

rectcount = 0;
}
}

/* set the font colors to match the chtype's attribute */

static void _set_attr(chtype ch)
Expand Down Expand Up @@ -322,19 +267,11 @@ void PDC_gotoyx(int row, int col)

SDL_BlitSurface(pdc_font, &src, pdc_screen, &dest);
#endif

if (oldrow != row || oldcol != col)
{
if (rectcount == MAXRECT)
PDC_update_rects();

uprect[rectcount++] = dest;
}
}

void _new_packet(attr_t attr, int lineno, int x, int len, const chtype *srcp)
{
SDL_Rect src, dest, lastrect;
SDL_Rect src, dest;
int j;
#ifdef PDC_WIDE
Uint16 chstr[2] = {0, 0};
Expand All @@ -343,9 +280,6 @@ void _new_packet(attr_t attr, int lineno, int x, int len, const chtype *srcp)
short hcol = SP->line_color;
bool blink = blinked_off && (attr & A_BLINK) && (sysattrs & A_BLINK);

if (rectcount == MAXRECT)
PDC_update_rects();

#ifdef PDC_WIDE
src.x = 0;
src.y = 0;
Expand All @@ -358,24 +292,6 @@ void _new_packet(attr_t attr, int lineno, int x, int len, const chtype *srcp)
dest.h = pdc_fheight;
dest.w = pdc_fwidth * len;

/* if the previous rect was just above this one, with the same width
and horizontal position, then merge the new one with it instead
of adding a new entry */

if (rectcount)
lastrect = uprect[rectcount - 1];

if (rectcount && lastrect.x == dest.x && lastrect.w == dest.w)
{
if (lastrect.y + lastrect.h == dest.y)
uprect[rectcount - 1].h = lastrect.h + pdc_fheight;
else
if (lastrect.y != dest.y)
uprect[rectcount++] = dest;
}
else
uprect[rectcount++] = dest;

_set_attr(attr);

if (backgr == -1)
Expand Down Expand Up @@ -556,5 +472,8 @@ void PDC_blink_text(void)

void PDC_doupdate(void)
{
PDC_napms(1);
SDL_UpdateTexture(pdc_texture, NULL, pdc_screen->pixels, pdc_screen->pitch);
SDL_RenderClear(pdc_renderer);
SDL_RenderCopy(pdc_renderer, pdc_texture, NULL, NULL);
SDL_RenderPresent(pdc_renderer);
}
18 changes: 10 additions & 8 deletions sdl2/pdckbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ bool PDC_check_key(void)
{
int haveevent;

/**
* SDL_TEXTINPUT can return multiple chars from the IME
* which we should handle before polling for additional
* events.
*/
SDL_PumpEvents();

/* SDL_TEXTINPUT can return multiple chars from the IME which we
should handle before polling for additional events. */

if (event.type == SDL_TEXTINPUT && event.text.text[0])
haveevent = 1;
else haveevent = SDL_PollEvent(&event);
else
haveevent = SDL_PollEvent(&event);

return haveevent;
}
Expand Down Expand Up @@ -406,7 +407,7 @@ int PDC_event_filter(void *userdata, SDL_Event *event)
(SDL_WINDOWEVENT_RESTORED == event->window.event ||
SDL_WINDOWEVENT_EXPOSED == event->window.event))
{
SDL_UpdateWindowSurface(pdc_window);
PDC_doupdate();
return 0;
}

Expand All @@ -424,7 +425,8 @@ int PDC_get_key(void)
case SDL_WINDOWEVENT:
if (SDL_WINDOWEVENT_SIZE_CHANGED == event.window.event)
{
pdc_screen = SDL_GetWindowSurface(pdc_window);
PDC_oldscreen();
PDC_newscreen();
pdc_sheight = pdc_screen->h - pdc_xoffset;
pdc_swidth = pdc_screen->w - pdc_yoffset;
touchwin(curscr);
Expand Down
54 changes: 27 additions & 27 deletions sdl2/pdcscrn.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ int pdc_font_size =
#endif

SDL_Window *pdc_window = NULL;
SDL_Renderer *pdc_renderer = NULL;
SDL_Texture *pdc_texture = NULL;
SDL_Surface *pdc_screen = NULL, *pdc_font = NULL, *pdc_icon = NULL,
*pdc_back = NULL, *pdc_tileback = NULL;
int pdc_sheight = 0, pdc_swidth = 0, pdc_yoffset = 0, pdc_xoffset = 0;
Expand Down Expand Up @@ -130,6 +132,24 @@ static void _initialize_colors(void)
pdc_color[i].g, pdc_color[i].b);
}

void PDC_oldscreen(void)
{
SDL_FreeSurface(pdc_screen);
SDL_DestroyTexture(pdc_texture);
SDL_DestroyRenderer(pdc_renderer);
}

void PDC_newscreen(void)
{
SDL_GetWindowSize(pdc_window, &pdc_swidth, &pdc_sheight);
pdc_renderer = SDL_CreateRenderer(pdc_window, -1, 0);
pdc_texture = SDL_CreateTexture(pdc_renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
pdc_swidth, pdc_sheight);
pdc_screen = SDL_CreateRGBSurface(0, pdc_swidth, pdc_sheight, 32,
0, 0, 0, 0);
}

/* open the physical screen -- miscellaneous initialization */

int PDC_scr_open(void)
Expand Down Expand Up @@ -256,35 +276,14 @@ int PDC_scr_open(void)
}

SDL_SetWindowIcon(pdc_window, pdc_icon);

/* Events must be pumped before calling SDL_GetWindowSurface, or
initial modifiers (e.g. numlock) will be ignored and out-of-sync. */

SDL_PumpEvents();

pdc_screen = SDL_GetWindowSurface(pdc_window);

if (pdc_screen == NULL)
{
fprintf(stderr, "Could not open SDL window surface: %s\n",
SDL_GetError());
return ERR;
}

pdc_sheight = pdc_screen->h;
pdc_swidth = pdc_screen->w;
}
else
{
if (!pdc_screen)
pdc_screen = SDL_GetWindowSurface(pdc_window);

if (!pdc_sheight)
pdc_sheight = pdc_screen->h - pdc_yoffset;
SDL_PumpEvents();

if (!pdc_swidth)
pdc_swidth = pdc_screen->w - pdc_xoffset;
}
PDC_newscreen();

pdc_sheight -= pdc_yoffset;
pdc_swidth -= pdc_xoffset;

if (!pdc_screen)
{
Expand Down Expand Up @@ -343,7 +342,8 @@ int PDC_resize_screen(int nlines, int ncols)
pdc_swidth = ncols * pdc_fwidth;

SDL_SetWindowSize(pdc_window, pdc_swidth, pdc_sheight);
pdc_screen = SDL_GetWindowSurface(pdc_window);
PDC_oldscreen();
PDC_newscreen();
}

if (pdc_tileback)
Expand Down
5 changes: 4 additions & 1 deletion sdl2/pdcsdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ PDCEX TTF_Font *pdc_ttffont;
PDCEX int pdc_font_size;
#endif
PDCEX SDL_Window *pdc_window;
PDCEX SDL_Renderer *pdc_renderer;
PDCEX SDL_Texture *pdc_texture;
PDCEX SDL_Surface *pdc_screen, *pdc_font, *pdc_icon, *pdc_back;
PDCEX int pdc_sheight, pdc_swidth, pdc_yoffset, pdc_xoffset;

Expand All @@ -29,8 +31,9 @@ extern bool pdc_own_window; /* if pdc_window was not set
before initscr(), PDCurses is
responsible for (owns) it */

PDCEX void PDC_update_rects(void);
PDCEX void PDC_retile(void);

extern void PDC_oldscreen(void);
extern void PDC_newscreen(void);
extern int PDC_event_filter(void *, SDL_Event *);
extern void PDC_blink_text(void);
1 change: 0 additions & 1 deletion sdl2/pdcutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ void PDC_napms(int ms)
{
PDC_LOG(("PDC_napms() - called: ms=%d\n", ms));

PDC_update_rects();
while (ms > 50)
{
SDL_PumpEvents();
Expand Down
5 changes: 2 additions & 3 deletions sdl2/sdltest.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ int main(int argc, char **argv)

pdc_window = SDL_CreateWindow("PDCurses for SDL", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
pdc_screen = SDL_GetWindowSurface(pdc_window);

/* Initialize PDCurses */

pdc_yoffset = 416; /* 480 - 4 * 16 */

initscr();
initscr(); /* creates pdc_screen */
start_color();
scrollok(stdscr, TRUE);

Expand All @@ -62,7 +61,7 @@ int main(int argc, char **argv)
rand() % 256, rand() % 256));
}

SDL_UpdateWindowSurface(pdc_window);
doupdate(); /* updates pdc_screen */

/* Do some curses stuff */

Expand Down

0 comments on commit 6e69627

Please sign in to comment.