shithub: npe

Download patch

ref: 227d52323d8f98c897a9269e9d4947cf1a28f4bd
parent: 13c0e712ae7d999f8df7f335cb6eaf7e48a3feb9
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Jun 16 03:57:21 EDT 2021

sdl2: simplify SDL_CreateRGBSurfaceFrom; add SDL_RenderReadPixels (no pitch used)

--- a/include/npe/SDL2/SDL.h
+++ b/include/npe/SDL2/SDL.h
@@ -73,7 +73,7 @@
 void SDL_FreeCursor(SDL_Cursor *cursor);
 SDL_Surface *SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int w, int h, int bpp, Uint32 fmt);
 SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 rm, Uint32 gm, Uint32 bm, Uint32 am);
-SDL_Surface *SDL_CreateRGBSurfaceFrom(Uint32 *pixels, int w, int h, int bpp, int pitch, Uint32 rm, Uint32 gm, Uint32 bm, Uint32 am);
+SDL_Surface *SDL_CreateRGBSurfaceFrom(void *pixels, int w, int h, int bpp, int pitch, Uint32 rm, Uint32 gm, Uint32 bm, Uint32 am);
 Uint32 SDL_MapRGB(SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b);
 int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key);
 int SDL_SetSurfaceRLE(SDL_Surface *surface, int flag);
@@ -89,6 +89,7 @@
 SDL_bool SDL_IsTextInputActive(void);
 void SDL_StartTextInput(void);
 void SDL_Delay(Uint32 ms);
+int SDL_RenderReadPixels(SDL_Renderer *rend, SDL_Rect *rect, Uint32 fmt, void *pixels, int pitch);
 int SDL_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect *srcrect, SDL_Rect *dstrect);
 void SDL_RenderPresent(SDL_Renderer *renderer);
 Uint32 SDL_GetWindowFlags(SDL_Window *window);
--- a/libnpe_sdl2/sdl2.c
+++ b/libnpe_sdl2/sdl2.c
@@ -325,9 +325,10 @@
 }
 
 SDL_Surface *
-SDL_CreateRGBSurfaceFrom(Uint32 *pixels, int w, int h, int bpp, int pitch, Uint32 rm, Uint32 gm, Uint32 bm, Uint32 am)
+SDL_CreateRGBSurfaceFrom(void *pixels, int w, int h, int bpp, int pitch, Uint32 rm, Uint32 gm, Uint32 bm, Uint32 am)
 {
 	SDL_Surface *s;
+	u8int *p;
 	int n, y;
 
 	if((s = SDL_CreateRGBSurface(0, w, h, bpp, rm, gm, bm, am)) == nil)
@@ -334,10 +335,8 @@
 		return nil;
 
 	n = w*bpp/8;
-	for(y = 0; y < h; y++){
-		memmove(s->pixels + y*n, pixels, n);
-		pixels += pitch / sizeof(Uint32);
-	}
+	for(y = 0, p = pixels; y < h; y++, p += pitch)
+		memmove(s->pixels + y*n, p, n);
 
 	return s;
 }
@@ -675,6 +674,28 @@
 	else
 		replclipr(screen, 0, screen->r);
 	npe_sdl.fullredraw = 0;
+}
+
+int
+SDL_RenderReadPixels(SDL_Renderer *rend, SDL_Rect *rect, Uint32 fmt, void *pixels, int pitch)
+{
+	Rectangle r;
+
+	USED(pitch); /* FIXME pitch & fmt */
+
+	if(fmt != SDL_PIXELFORMAT_ARGB8888){
+		werrstr("SDL_RenderReadPixels: FIXME non-argb8888");
+		return -1;
+	}
+
+	if(rect != nil)
+		r = Rect(rect->x, rect->y, rect->x+rect->w, rect->y+rect->h);
+	else
+		r = Rect(0, 0, rend->logiw, rend->logih);
+
+	unloadmemimage(back, r, pixels, Dx(r)*Dy(r)*4);
+
+	return 0;
 }
 
 Uint32