History

sdl2: refactor image channel code, more palette function coverage

Jacob Moody committed 3 years ago

Commit b699be11 parent d8590258 files · +393 −82Browse files

Unified · Split

8 files
  1. include/npe/SDL2/SDL.h +23 −0
  2. include/npe/SDL2/SDL_events.h +6 −0
  3. include/npe/string.h +1 −0
  4. libnpe/mkfile +1 −0
  5. libnpe/strlcpy.c +19 −0
  6. libnpe_sdl2/_sdl.h +1 −0
  7. libnpe_sdl2/events.c +59 −14
  8. libnpe_sdl2/sdl2.c +283 −68
include/npe/SDL2/SDL.h+23−0
@@ -46,7 +46,18 @@ typedef struct SDL_mutex SDL_mutex; · 45 unmodified lines
4646
4747 #define SDL_zero(x) do{ memset(&(x), 0, sizeof(x)); }while(0)
4848 #define SDL_memset memset
49+#define SDL_malloc malloc
4950 #define SDL_realloc realloc
51+#define SDL_calloc calloc
52+#define SDL_memcpy memcpy
53+#define SDL_memmove memmove
54+#define SDL_memcmp memcmp
55+#define SDL_printf printf
56+#define SDL_snprintf snprintf
57+#define SDL_strlen strlen
58+#define SDL_strlcpy strlcpy
59+#define SDL_strstr strstr
60+#define SDL_strncmp strncmp
5061
5162 #include <SDL2/SDL_keycode.h>
5263 #include <SDL2/SDL_scancode.h>
@@ -81,6 +92,7 @@ void SDL_RestoreWindow(SDL_Window *window); · 28 unmodified lines
8192 void SDL_RaiseWindow(SDL_Window *window);
8293 int SDL_UpdateTexture(SDL_Texture *texture, SDL_Rect *rect, void *pixels, int pitch);
8394 int SDL_RenderClear(SDL_Renderer *renderer);
95+int SDL_RenderFillRect(SDL_Renderer *r, SDL_Rect *rect);
8496 int SDL_GetWindowDisplayIndex(SDL_Window *window);
8597 void SDL_FreeSurface(SDL_Surface *surface);
8698 Uint32 SDL_GetGlobalMouseState(int *x, int *y);
@@ -92,6 +104,7 @@ void SDL_FreeCursor(SDL_Cursor *cursor); · 5 unmodified lines
92104 SDL_Surface *SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int w, int h, int bpp, Uint32 fmt);
93105 SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 rm, Uint32 gm, Uint32 bm, Uint32 am);
94106 SDL_Surface *SDL_CreateRGBSurfaceFrom(void *pixels, int w, int h, int bpp, int pitch, Uint32 rm, Uint32 gm, Uint32 bm, Uint32 am);
107+void SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat *format, Uint8 *r, Uint8 *g, Uint8 *b);
95108 Uint32 SDL_MapRGB(SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b);
96109 int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key);
97110 int SDL_SetSurfaceRLE(SDL_Surface *surface, int flag);
@@ -121,18 +134,21 @@ void SDL_SetWindowPosition(SDL_Window *window, int x, int y); · 23 unmodified lines
121134 void SDL_DestroyTexture(SDL_Texture *texture);
122135 void SDL_DestroyRenderer(SDL_Renderer *renderer);
123136 void SDL_DestroyWindow(SDL_Window *window);
137+int SDL_GetDisplayUsableBounds(int displayIndex, SDL_Rect *rect);
124138 int SDL_GetDesktopDisplayMode(int displayIndex, SDL_DisplayMode *mode);
125139 void SDL_SetWindowTitle(SDL_Window *window, char *title);
126140 int SDL_SetTextureBlendMode(SDL_Texture *texture, SDL_BlendMode blendMode);
127141 SDL_bool SDL_SetHint(char *name, char *value);
128142 SDL_Window *SDL_CreateWindow(char *title, int x, int y, int w, int h, Uint32 flags);
129143 SDL_Renderer *SDL_CreateRenderer(SDL_Window *window, int index, Uint32 flags);
144+int SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags, SDL_Window **window, SDL_Renderer **renderer);
130145 int SDL_SetRenderDrawBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode);
131146 char *SDL_GetCurrentVideoDriver(void);
132147 SDL_Texture *SDL_CreateTexture(SDL_Renderer *renderer, Uint32 format, int access, int w, int h);
133148 void SDL_EnableScreenSaver(void);
134149 Uint32 SDL_GetTicks(void);
135150 int SDL_GetRendererOutputSize(SDL_Renderer *renderer, int *w, int *h);
151+void SDL_RenderGetViewport(SDL_Renderer *rebderer, SDL_Rect *rect);
136152 int SDL_SaveBMP(SDL_Surface *s, const char *file);
137153 void SDL_ClearError(void);
138154 int SDL_SetTextureAlphaMod(SDL_Texture *texture, Uint8 alpha);
@@ -156,7 +172,9 @@ int SDL_UnlockMutex(SDL_mutex*); · 17 unmodified lines
156172 #define SDL_mutexV(x) SDL_UnlockMutex(x)
157173 #define SDL_mutexP(x) SDL_LockMutex(x)
158174 int SDL_FillRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color);
175+SDL_Palette *SDL_AllocPalette(int ncolors);
159176 int SDL_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors, int firstcolor, int ncolors);
177+int SDL_SetSurfacePalette(SDL_Surface *s, SDL_Palette *palette);
160178 int SDL_BlitSurface(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
161179 int SDL_SoftStretch(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect);
162180 int SDL_LockTexture(SDL_Texture *texture, const SDL_Rect *rect, void **pixels, int *pitch);
@@ -197,7 +215,10 @@ enum { · 34 unmodified lines
197215 SDL_PIXELFORMAT_INDEX8 = 0x13000801,
198216 SDL_PIXELFORMAT_RGB24 = 0x17101803,
199217 SDL_PIXELFORMAT_ABGR8888 = 0x16762004,
218+ SDL_PIXELFORMAT_XBGR8888 = 0x16561804,
219+ SDL_PIXELFORMAT_BGR24 = 0x17401803,
200220 SDL_PIXELFORMAT_RGB888 = SDL_PIXELFORMAT_XRGB8888,
221+ SDL_PIXELFORMAT_BGR888 = SDL_PIXELFORMAT_XBGR8888,
201222
202223 /* shit no one cares about */
203224 SDL_TEXTUREACCESS_STREAMING = 0,
@@ -245,6 +266,7 @@ enum { · 41 unmodified lines
245266 #define SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4 "SDL_WINDOWS_NO_CLOSE_ON_ALT_F4"
246267 #define SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH "SDL_MOUSE_FOCUS_CLICKTHROUGH"
247268 #define SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING "SDL_WINDOWS_DISABLE_THREAD_NAMING"
269+#define SDL_HINT_RENDER_VSYNC "SDL_RENDER_VSYNC"
248270
249271 struct SDL_Point {
250272 int x, y;
@@ -288,6 +310,7 @@ struct SDL_Palette { · 37 unmodified lines
288310 struct SDL_PixelFormat {
289311 SDL_Palette *palette;
290312 int format;
313+ int BytesPerPixel;
291314 };
292315
293316 struct SDL_RendererInfo {
include/npe/SDL2/SDL_events.h+6−0
@@ -58,6 +58,7 @@ typedef struct SDL_MouseButtonEvent SDL_MouseButtonEvent; · 57 unmodified lines
5858 typedef struct SDL_Keysym SDL_Keysym;
5959 typedef struct SDL_Event SDL_Event;
6060 typedef int SDL_eventaction;
61+typedef int (*SDL_EventFilter)(void *userdata, SDL_Event *event);
6162
6263 struct SDL_WindowEvent {
6364 Uint32 type;
@@ -127,6 +128,7 @@ struct SDL_Event { · 63 unmodified lines
127128 struct {
128129 int x, y;
129130 int xrel, yrel;
131+ int state;
130132 }motion;
131133 struct {
132134 char text[SDL_TEXTINPUTEVENT_TEXT_SIZE+1];
@@ -145,5 +147,9 @@ int SDL_RegisterEvents(int); · 12 unmodified lines
145147 int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action, Uint32 minType, Uint32 maxType);
146148 Uint32 SDL_GetWindowID(SDL_Window *window);
147149 void SDL_PumpEvents(void);
150+void SDL_SetEventFilter(SDL_EventFilter filter, void *userdata);
151+SDL_Scancode SDL_GetScancodeFromKey(SDL_Keycode key);
152+Uint8* SDL_GetKeyboardState(int *numkeys);
153+char* SDL_GetKeyName(SDL_Keycode key);
148154
149155 #endif
include/npe/string.h+1−0
@@ -5,5 +5,6 @@ · 4 unmodified lines
55
66 char *strerror(int errnum);
77 int strerror_r(int errnum, char *buf, size_t buflen);
8+size_t strlcpy(char *dst, char *src, size_t sz);
89
910 #endif
libnpe/mkfile+1−0
@@ -62,6 +62,7 @@ OFILES=\ · 61 unmodified lines
6262 strerror.$O\
6363 strerror_r.$O\
6464 strftime.$O\
65+ strlcpy.$O\
6566 trunc.$O\
6667 unlink.$O\
6768 usleep.$O\
libnpe/strlcpy.c+19−0
@@ -0,0 +1,19 @@
1+#include <string.h>
2+
3+size_t
4+strlcpy(char *dst, char *src, size_t sz)
5+{
6+ char *start;
7+
8+ start = src;
9+ if(sz == 0)
10+ return strlen(start);
11+
12+ while(sz-- > 0 && (*dst++ = *src++) != '\0')
13+ ;
14+ if(dst[-1] != '\0')
15+ dst[-1] = '\0';
16+ while(*src++)
17+ ;
18+ return src - start - 1;
19+}
libnpe_sdl2/_sdl.h+1−0
@@ -1,6 +1,7 @@
11 #include <SDL2/SDL.h>
22 #include <sys/stat.h>
33 #include <stdint.h>
4+#include <string.h>
45 #include <tos.h>
56 #include <thread.h>
67 #include <draw.h>
libnpe_sdl2/events.c+59−14
@@ -26,7 +26,6 @@ static Alt salt[Numchan+1] = { · 25 unmodified lines
2626 [Numchan] = { nil, nil, CHANNOBLK },
2727 };
2828
29−static int rune2scancode(Rune r);
3029 static void kbdproc(void *);
3130 static void mouseproc(void *);
3231
@@ -91,6 +90,21 @@ SDL_QuitRequested(void) · 58 unmodified lines
9190 return quitreq;
9291 }
9392
93+struct {
94+ SDL_EventFilter f;
95+ void *aux;
96+} filter = {
97+ nil,
98+ nil,
99+};
100+
101+void
102+SDL_SetEventFilter(SDL_EventFilter f, void *userdata)
103+{
104+ filter.f = f;
105+ filter.aux = userdata;
106+}
107+
94108 void
95109 SDL_PumpEvents(void)
96110 {
@@ -116,20 +130,20 @@ SDL_PollEvent(SDL_Event *e) · 19 unmodified lines
116130 break;
117131 e->type = SDL_TEXTINPUT;
118132 e->text.text[runetochar(e->text.text, &rune)] = 0;
119− return 1;
133+ goto Filter;
120134 }else if((npe_sdl.hints & Altf4noclose) == 0 && (kmod & KMOD_LALT) != 0 && rune == (KF|4)){
121135 e->type = SDL_QUIT;
122136 quitreq = 1;
123− return 1;
137+ goto Filter;
124138 }else{
125139 e->type = (t == Rup) ? SDL_KEYUP : SDL_KEYDOWN;
126140 e->key.repeat = t == Rrepeat;
127− e->key.keysym.scancode = rune2scancode(rune);
141+ e->key.keysym.scancode = SDL_GetScancodeFromKey(rune);
128142 if(rune == '\n')
129143 rune = SDLK_RETURN;
130144 e->key.keysym.sym = rune;
131145 e->key.state = e->type;
132− return 1;
146+ goto Filter;
133147 }
134148 break;
135149
@@ -153,7 +167,8 @@ SDL_PollEvent(SDL_Event *e) · 17 unmodified lines
153167 npe_sdl.mredraw = 1;
154168 if(npe_sdl.m.buttons == npe_sdl.om.buttons){
155169 e->type = SDL_MOUSEMOTION;
156− return 1;
170+ e->motion.state = npe_sdl.m.buttons;
171+ goto Filter;
157172 }
158173 }
159174 if(npe_sdl.m.buttons == npe_sdl.om.buttons)
@@ -163,25 +178,25 @@ SDL_PollEvent(SDL_Event *e) · 3 unmodified lines
163178 e->type = down ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
164179 e->button.button = SDL_BUTTON_LEFT;
165180 npe_sdl.om.buttons = (npe_sdl.om.buttons & ~1) | (npe_sdl.m.buttons & 1);
166− return 1;
181+ goto Filter;
167182 }
168183 if((down = (npe_sdl.m.buttons & 2)) != (npe_sdl.om.buttons & 2)){ /* middle */
169184 e->type = down ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
170185 e->button.button = SDL_BUTTON_MIDDLE;
171186 npe_sdl.om.buttons = (npe_sdl.om.buttons & ~2) | (npe_sdl.m.buttons & 2);
172− return 1;
187+ goto Filter;
173188 }
174189 if((down = (npe_sdl.m.buttons & 4)) != (npe_sdl.om.buttons & 4)){ /* right */
175190 e->type = down ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
176191 e->button.button = SDL_BUTTON_RIGHT;
177192 npe_sdl.om.buttons = (npe_sdl.om.buttons & ~4) | (npe_sdl.m.buttons & 4);
178− return 1;
193+ goto Filter;
179194 }
180195 if(npe_sdl.m.buttons & (8|16)){
181196 e->type = SDL_MOUSEWHEEL;
182197 e->wheel.x = 0;
183198 e->wheel.y = (npe_sdl.m.buttons & 8) ? 1 : -1;
184− return 1;
199+ goto Filter;
185200 }
186201 break;
187202
@@ -193,10 +208,15 @@ SDL_PollEvent(SDL_Event *e) · 5 unmodified lines
193208 e->type = SDL_WINDOWEVENT;
194209 e->window.event = SDL_WINDOWEVENT_EXPOSED;
195210 e->window.windowID = 1; //TODO more then one?
196− return 1;
211+ goto Filter;
197212 }
198213
199214 return 0;
215+
216+Filter:
217+ if(filter.f != nil && filter.f(filter.aux, e) == 0)
218+ return 0;
219+ return 1;
200220 }
201221
202222 int
@@ -211,8 +231,8 @@ SDL_WaitEvent(SDL_Event *e) · 8 unmodified lines
211231 return r;
212232 }
213233
214−static int
215−rune2scancode(Rune r)
234+SDL_Scancode
235+SDL_GetScancodeFromKey(SDL_Keycode r)
216236 {
217237 if(r >= 'a' && r <= 'z')
218238 return r - 'a' + SDL_SCANCODE_A;
@@ -282,12 +302,29 @@ rune2scancode(Rune r) · 63 unmodified lines
282302 return r;
283303 }
284304
305+static Uint8 kbdstate[SDL_NUM_SCANCODES];
306+
307+Uint8*
308+SDL_GetKeyboardState(int *numkeys)
309+{
310+ if(numkeys != nil)
311+ *numkeys = SDL_NUM_SCANCODES;
312+ return kbdstate;
313+}
314+
315+char*
316+SDL_GetKeyName(SDL_Keycode key)
317+{
318+ USED(key);
319+ return ""; /* FIXME */
320+}
321+
285322 static void
286323 kbdproc(void *)
287324 {
288325 char buf[128], buf2[128], *s;
289326 int kfd, n, kbin, t;
290− Rune r, o;
327+ Rune r, scan, o;
291328
292329 threadsetname("kbdproc");
293330 if((kfd = open("/dev/kbd", OREAD|OCEXEC)) < 0)
@@ -325,8 +362,12 @@ kbdproc(void *) · 31 unmodified lines
325362
326363 case 'k':
327364 s = buf+1;
365+ memset(kbdstate, 0, sizeof kbdstate);
328366 while(*s){
329367 s += chartorune(&r, s);
368+ scan = SDL_GetScancodeFromKey(r);
369+ if(scan < nelem(kbdstate))
370+ kbdstate[scan] = 1;
330371 if(utfrune(buf2+1, r) == nil){
331372 t = Rdown;
332373 if(r == Kalt){
@@ -356,8 +397,12 @@ kbdproc(void *) · 23 unmodified lines
356397
357398 case 'K':
358399 s = buf2+1;
400+ memset(kbdstate, 0, sizeof kbdstate);
359401 while(*s){
360402 s += chartorune(&r, s);
403+ scan = SDL_GetScancodeFromKey(r);
404+ if(scan < nelem(kbdstate))
405+ kbdstate[scan] = 1;
361406 if(utfrune(buf+1, r) == nil){
362407 if(r == Kalt)
363408 kmod &= ~KMOD_LALT;
libnpe_sdl2/sdl2.c+283−68
@@ -21,22 +21,6 @@ struct SDL_Cursor { · 20 unmodified lines
2121 Point hot;
2222 };
2323
24−static SDL_PixelFormat argb8888 = {
25− .format = SDL_PIXELFORMAT_ARGB8888,
26−};
27−
28−static SDL_PixelFormat xrgb8888 = {
29− .format = SDL_PIXELFORMAT_XRGB8888,
30−};
31−
32−static SDL_PixelFormat abgr8888 = {
33− .format = SDL_PIXELFORMAT_ABGR8888,
34−};
35−
36−static SDL_PixelFormat rgb24 = {
37− .format = SDL_PIXELFORMAT_RGB24,
38−};
39−
4024 static SDL_Window onewin;
4125 static SDL_Renderer oneren;
4226 static Memimage *back;
@@ -47,6 +31,9 @@ static SDL_Cursor *oldcursor, *cursor; · 4 unmodified lines
4731 static int showcursor = SDL_ENABLE;
4832 static char basepath[PATH_MAX];
4933 static u32int renddrawcol = DBlack;
34+struct {
35+ Uint32 r, g, b, a;
36+} defmask;
5037
5138 struct npe_sdl npe_sdl = {0};
5239
@@ -92,6 +79,114 @@ SDL_SetWindowBordered(SDL_Window *w, SDL_bool flag) · 39 unmodified lines
9279 USED(w); USED(flag);
9380 }
9481
82+static int
83+chan2mask(Uint32 chan, Uint32 *rm, Uint32 *gm, Uint32 *bm, Uint32 *am)
84+{
85+ switch(chan){
86+ case ARGB32:
87+ *am = 0xFF000000;
88+ if(0){
89+ case XRGB32:
90+ *am = 0x00000000;
91+ }
92+ case RGB24:
93+ *rm = 0x00FF0000;
94+ *gm = 0x0000FF00;
95+ *bm = 0x000000FF;
96+ break;
97+ case ABGR32:
98+ *am = 0xFF000000;
99+ if(0){
100+ case XBGR32:
101+ *am = 0x00000000;
102+ }
103+ case BGR24:
104+ *bm = 0x00FF0000;
105+ *gm = 0x0000FF00;
106+ *rm = 0x000000FF;
107+ break;
108+ default:
109+ assert(0);
110+ }
111+ return 0;
112+}
113+
114+static ulong
115+mask2chan(int bpp, Uint32 rm, Uint32 gm, Uint32 bm, Uint32 am)
116+{
117+ USED(gm, bm);
118+
119+ switch(bpp){
120+ case 8:
121+ return CMAP8;
122+ case 24:
123+ if(rm & 0xFF0000)
124+ return RGB24;
125+ else
126+ return BGR24;
127+ case 32:
128+ if(am == 0){
129+ if(rm & 0xFF0000)
130+ return XRGB32;
131+ else
132+ return XBGR32;
133+ } else {
134+ if(rm & 0xFF0000)
135+ return ARGB32;
136+ else
137+ return XBGR32;
138+ }
139+ }
140+ assert(0);
141+ return 0;
142+}
143+
144+static Uint32
145+chan2pixel(ulong chan)
146+{
147+ switch(chan){
148+ case ARGB32:
149+ return SDL_PIXELFORMAT_ARGB8888;
150+ case XRGB32:
151+ return SDL_PIXELFORMAT_XRGB8888;
152+ case RGB24:
153+ return SDL_PIXELFORMAT_RGB24;
154+ case ABGR32:
155+ return SDL_PIXELFORMAT_ABGR8888;
156+ case XBGR32:
157+ return SDL_PIXELFORMAT_XBGR8888;
158+ case BGR24:
159+ return SDL_PIXELFORMAT_BGR24;
160+ case CMAP8:
161+ return SDL_PIXELFORMAT_INDEX8;
162+ }
163+ assert(0);
164+ return 0;
165+}
166+
167+static ulong
168+pixel2chan(Uint32 format)
169+{
170+ switch(format){
171+ case SDL_PIXELFORMAT_ARGB8888:
172+ return ARGB32;
173+ case SDL_PIXELFORMAT_XRGB8888:
174+ return XRGB32;
175+ case SDL_PIXELFORMAT_RGB24:
176+ return RGB24;
177+ case SDL_PIXELFORMAT_ABGR8888:
178+ return ABGR32;
179+ case SDL_PIXELFORMAT_XBGR8888:
180+ return XBGR32;
181+ case SDL_PIXELFORMAT_BGR24:
182+ return BGR24;
183+ case SDL_PIXELFORMAT_INDEX8:
184+ return CMAP8;
185+ }
186+ assert(0);
187+ return 0;
188+}
189+
95190 int
96191 SDL_Init(int mask)
97192 {
@@ -113,7 +208,10 @@ SDL_Init(int mask) · 15 unmodified lines
113208 npe_sdl.scale = 1;
114209 physw = Dx(screen->r);
115210 physh = Dy(screen->r);
116−
211+ if(chan2mask(screen->chan, &defmask.r, &defmask.g, &defmask.b, &defmask.a) < 0){
212+ werrstr("SDL_Init: unsupported screen channel");
213+ return -1;
214+ }
117215 return 0;
118216 err:
119217 werrstr("SDL_Init: %r");
@@ -223,17 +321,7 @@ SDL_CreateTexture(SDL_Renderer *, Uint32 format, int, int w, int h) · 103 unmodified lines
223321 SDL_Texture *t;
224322 int dformat;
225323
226− switch(format){
227− case SDL_PIXELFORMAT_ARGB8888:
228− dformat = ARGB32;
229− break;
230− case SDL_PIXELFORMAT_XRGB8888:
231− dformat = XRGB32;
232− break;
233− case SDL_PIXELFORMAT_ABGR8888:
234− dformat = ABGR32;
235− break;
236− default:
324+ if((dformat = pixel2chan(format)) == 0){
237325 werrstr("SDL_CreateTexture: format is not supported");
238326 goto err;
239327 }
@@ -341,6 +429,14 @@ SDL_RenderClear(SDL_Renderer *) · 101 unmodified lines
341429 return 0;
342430 }
343431
432+int
433+SDL_RenderFillRect(SDL_Renderer *r, SDL_Rect *rect)
434+{
435+ /* FIXME */
436+ assert(rect == nil);
437+ return SDL_RenderClear(r);
438+}
439+
344440 int
345441 SDL_SetRenderDrawColor(SDL_Renderer *, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
346442 {
@@ -368,40 +464,37 @@ SDL_free(void *p) · 21 unmodified lines
368464 }
369465
370466 SDL_Surface *
371−SDL_CreateRGBSurface(Uint32 flags, int w, int h, int bpp, Uint32 rm, Uint32 gm, Uint32 bm, Uint32 am)
467+SDL_CreateRGBSurface(Uint32, int w, int h, int bpp, Uint32 rm, Uint32 gm, Uint32 bm, Uint32 am)
372468 {
373469 SDL_Surface *s;
374470 int n;
375− Memimage *i;
376−
377− USED(flags, rm, gm, bm, am); /* FIXME flags & masks */
471+ ulong chan;
378472
473+ rm = rm ? rm : defmask.r;
474+ gm = gm ? gm : defmask.g;
475+ bm = bm ? bm : defmask.b;
476+ if((chan = mask2chan(bpp, rm, gm, bm, am)) == 0){
477+ werrstr("bad bpp and/or mask");
478+ return nil;
479+ }
379480 n = w*h*bpp/8;
380481 if((s = calloc(1, sizeof(*s))) == nil){
381482 werrstr("SDL_CreateRGBSurface: memory");
382483 return nil;
383484 }
384− s->i = i = allocmemimage(Rect(0,0,w,h), screen->chan);
485+ s->i = allocmemimage(Rect(0,0,w,h), chan == CMAP8 ? screen->chan : chan);
385486 s->format = calloc(1, sizeof(SDL_PixelFormat));
386− switch(bpp){
387− case 32:
388− s->format->format = SDL_PIXELFORMAT_ARGB8888;
389− s->pixels = i->data->bdata;
390− break;
391− case 24:
392− s->format->format = SDL_PIXELFORMAT_RGB24;
393− s->pixels = calloc(1, n);
394− break;
395− case 8:
396− s->format->format = SDL_PIXELFORMAT_INDEX8;
487+ s->format->BytesPerPixel = bpp/8;
488+ s->format->format = chan2pixel(chan);
489+ if(chan == CMAP8){
490+ s->i = allocmemimage(Rect(0,0,w,h), screen->chan);
397491 s->format->palette = calloc(1, sizeof(SDL_Palette));
398492 s->format->palette->ncolors = 256;
399493 s->format->palette->colors = calloc(1, sizeof(SDL_Color) * 256);
400494 s->pixels = calloc(1, n);
401− break;
402− default:
403− werrstr("non supported bpp");
404− return nil;
495+ }else{
496+ s->i = allocmemimage(Rect(0,0,w,h), chan);
497+ s->pixels = ((Memimage*)s->i)->data->bdata;
405498 }
406499
407500 s->w = w;
@@ -436,27 +529,17 @@ SDL_CreateRGBSurfaceFrom(void *pixels, int w, int h, int bpp, int pitch, Uint32 · 28 unmodified lines
436529 SDL_Surface *
437530 SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int w, int h, int bpp, Uint32 fmt)
438531 {
439− SDL_PixelFormat *f;
440532 SDL_Surface *s;
533+ ulong chan;
534+ Uint32 rm, gm, bm, am;
441535
442− if(fmt == SDL_PIXELFORMAT_ARGB8888)
443− f = &argb8888;
444− else if(fmt == SDL_PIXELFORMAT_XRGB8888)
445− f = &xrgb8888;
446− else if(fmt == SDL_PIXELFORMAT_RGB24)
447− f = &rgb24;
448− else if(fmt == SDL_PIXELFORMAT_ABGR8888)
449− f = &abgr8888;
450− else{
536+ if((chan = pixel2chan(fmt)) == 0){
451537 werrstr("SDL_CreateRGBSurfaceWithFormat: FIXME format %8ux not implemented", fmt);
452538 return nil;
453539 }
454−
455− if((s = SDL_CreateRGBSurface(flags, w, h, bpp, 0, 0, 0, 0)) == nil)
540+ chan2mask(chan, &rm, &gm, &bm, &am);
541+ if((s = SDL_CreateRGBSurface(flags, w, h, bpp, rm, bm, gm, am)) == nil)
456542 return nil;
457−
458− s->format = f;
459−
460543 return s;
461544 }
462545
@@ -470,6 +553,7 @@ SDL_FillRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color) · 7 unmodified lines
470553 switch(dst->format->format){
471554 case SDL_PIXELFORMAT_XRGB8888:
472555 case SDL_PIXELFORMAT_ARGB8888:
556+ case SDL_PIXELFORMAT_XBGR8888:
473557 case SDL_PIXELFORMAT_ABGR8888:
474558 p = (Uint32*)dst->pixels;
475559 for(i = 0; i < dst->n / sizeof(*p); i++)
@@ -491,6 +575,17 @@ SDL_FillRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color) · 15 unmodified lines
491575 return 0;
492576 }
493577
578+SDL_Palette*
579+SDL_AllocPalette(int ncolors)
580+{
581+ SDL_Palette *p;
582+
583+ p = malloc(sizeof(*p));
584+ p->ncolors = ncolors;
585+ p->colors = mallocz(sizeof(SDL_Color)*ncolors, 1);
586+ return p;
587+}
588+
494589 int
495590 SDL_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors, int firstcolor, int ncolors)
496591 {
@@ -502,6 +597,13 @@ SDL_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors, int firstcol · 5 unmodified lines
502597 return 0;
503598 }
504599
600+int
601+SDL_SetSurfacePalette(SDL_Surface *s, SDL_Palette *palette)
602+{
603+ s->format->palette = palette;
604+ return 0;
605+}
606+
505607 static void
506608 syncpalette(SDL_Surface *s)
507609 {
@@ -519,6 +621,32 @@ syncpalette(SDL_Surface *s) · 11 unmodified lines
519621 }
520622 }
521623
624+static void
625+synctopalette(SDL_Surface *s)
626+{
627+ SDL_Color c, *f;
628+ Uint32 *from;
629+ int j, k;
630+ Memimage *i;
631+ SDL_PixelFormat fmt;
632+
633+ i = s->i;
634+ fmt.format = chan2pixel(screen->chan);
635+ from = (void*)i->data->bdata;
636+ for(j = 0; j < s->n; j++){
637+ SDL_GetRGB(from[j], &fmt, &c.r, &c.g, &c.b);
638+ for(k = 0; k < s->format->palette->ncolors; k++){
639+ f = s->format->palette->colors + k;
640+ if(c.r == f->r && c.g == f->g && c.b == f->b)
641+ break;
642+ }
643+ if(k == s->format->palette->ncolors)
644+ s->pixels[j] = 0; /* FIXME */
645+ else
646+ s->pixels[j] = k;
647+ }
648+}
649+
522650 int
523651 SDL_BlitSurface(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
524652 {
@@ -530,9 +658,10 @@ SDL_BlitSurface(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL · 5 unmodified lines
530658 if(src->format->format == SDL_PIXELFORMAT_INDEX8)
531659 syncpalette(src);
532660
533− /* FIXME */
534− assert(dst->format->format != SDL_PIXELFORMAT_INDEX8);
535661 memimagedraw(dst->i, r2, src->i, ZP, nil, ZP, S);
662+
663+ if(dst->format->format == SDL_PIXELFORMAT_INDEX8)
664+ synctopalette(dst);
536665 return 0;
537666 }
538667
@@ -615,10 +744,63 @@ SDL_FreeSurface(SDL_Surface *surface) · 76 unmodified lines
615744 free(surface);
616745 }
617746
747+void
748+SDL_GetRGB(Uint32 pixel, SDL_PixelFormat *fmt, Uint8 *r, Uint8 *g, Uint8 *b)
749+{
750+ SDL_Color *c;
751+
752+ switch(fmt->format){
753+ case SDL_PIXELFORMAT_ARGB8888:
754+ case SDL_PIXELFORMAT_XRGB8888:
755+ case SDL_PIXELFORMAT_RGB24:
756+ *r = pixel>>16;
757+ *g = pixel>>8;
758+ *b = pixel;
759+ break;
760+ case SDL_PIXELFORMAT_ABGR8888:
761+ case SDL_PIXELFORMAT_XBGR8888:
762+ case SDL_PIXELFORMAT_BGR24:
763+ *b = pixel>>16;
764+ *g = pixel>>8;
765+ *r = pixel;
766+ break;
767+ case SDL_PIXELFORMAT_INDEX8:
768+ assert(fmt->palette);
769+ assert(pixel < fmt->palette->ncolors);
770+ c = fmt->palette->colors + pixel;
771+ *r = c->r;
772+ *g = c->g;
773+ *b = c->b;
774+ break;
775+ default:
776+ assert(0);
777+ }
778+}
779+
618780 Uint32
619−SDL_MapRGB(SDL_PixelFormat *, Uint8 r, Uint8 g, Uint8 b)
781+SDL_MapRGB(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b)
620782 {
621− return r<<24 | g<<16 | b<<8 | 0xff;
783+ SDL_Color *c;
784+
785+ switch(fmt->format){
786+ case SDL_PIXELFORMAT_ARGB8888:
787+ case SDL_PIXELFORMAT_XRGB8888:
788+ case SDL_PIXELFORMAT_RGB24:
789+ return 0xff<<24 | r<<16 | g<<8 | b;
790+ case SDL_PIXELFORMAT_ABGR8888:
791+ case SDL_PIXELFORMAT_XBGR8888:
792+ case SDL_PIXELFORMAT_BGR24:
793+ return 0xff<<24 | b<<16 | g<<8 | r;
794+ case SDL_PIXELFORMAT_INDEX8:
795+ assert(fmt->palette);
796+ for(c = fmt->palette->colors; c < fmt->palette->colors + fmt->palette->ncolors; c++){
797+ if(c->r == r && c->g == g && c->b == b)
798+ return c - fmt->palette->colors;
799+ }
800+ default:
801+ assert(0);
802+ return 0;
803+ }
622804 }
623805
624806 int
@@ -1027,6 +1209,14 @@ SDL_GetRendererOutputSize(SDL_Renderer *r, int *w, int *h) · 402 unmodified lines
10271209 return 0;
10281210 }
10291211
1212+void
1213+SDL_RenderGetViewport(SDL_Renderer *r, SDL_Rect *rect)
1214+{
1215+ rect->x = rect->y = 0;
1216+ rect->w = r->logiw;
1217+ rect->h = r->logih;
1218+}
1219+
10301220 void
10311221 SDL_SetWindowSize(SDL_Window *, int w, int h)
10321222 {
@@ -1094,6 +1284,22 @@ SDL_SetWindowPosition(SDL_Window *, int x, int y) · 61 unmodified lines
10941284 }
10951285 }
10961286
1287+int
1288+SDL_GetDisplayUsableBounds(int displayIndex, SDL_Rect *r)
1289+{
1290+ if(displayIndex != 0)
1291+ return -1;
1292+
1293+ if(r == nil)
1294+ return 0;
1295+
1296+ r->x = display->image->r.min.x;
1297+ r->y = display->image->r.min.y;
1298+ r->w = Dx(display->image->r);
1299+ r->h = Dy(display->image->r);
1300+ return 0;
1301+}
1302+
10971303 int
10981304 SDL_GetDesktopDisplayMode(int displayIndex, SDL_DisplayMode *mode)
10991305 {
@@ -1202,6 +1408,15 @@ SDL_CreateRenderer(SDL_Window *, int, Uint32) · 102 unmodified lines
12021408 return &oneren;
12031409 }
12041410
1411+int
1412+SDL_CreateWindowAndRenderer(int w, int h, Uint32, SDL_Window **win, SDL_Renderer **rend)
1413+{
1414+ SDL_SetWindowSize(&onewin, w, h);
1415+ *win = &onewin;
1416+ *rend = &oneren;
1417+ return 0;
1418+}
1419+
12051420 int
12061421 SDL_SetRenderDrawBlendMode(SDL_Renderer *, SDL_BlendMode blendMode)
12071422 {
@@ -1533,7 +1748,7 @@ SDL_CreateMutex(void) · 325 unmodified lines
15331748 {
15341749 SDL_mutex *m;
15351750
1536− m = mallocz(sizeof *m, 1);
1751+ m = mallocz(sizeof(*m), 1);
15371752 return m;
15381753 }
15391754

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t