ecere: gfx/gui: add SDL 1.x support via new driver named SDL1.
[sdk] / ecere / src / gfx / drivers / SDLDisplayDriver.ec
1 namespace gfx::drivers;
2
3 #ifdef BUILDING_ECERE_COM
4 import "instance"
5 import "Display"
6 #else
7 #ifdef ECERE_STATIC
8 public import static "ecere"
9 #else
10 public import "ecere"
11 #endif
12 #endif
13
14 import "SDLInterface"
15
16 #if !defined(NO_SDL_DRIVERS) && !defined(NO_SDL1_DRIVER)
17
18 #include <stdio.h>
19
20 // source file line number printf (sflnprintf)
21 #define sflnprintf(format,...) printf("%s:% 5d: " format, __FILE__, __LINE__, ##__VA_ARGS__)
22
23 #define watch _watch
24 #include <SDL.h>
25 #undef watch
26
27 class SDLDisplay : LFBDisplay
28 {
29 };
30
31 class SDLDisplayDriver : DisplayDriver
32 {
33    class_property(name) = "SDL1";
34
35
36    // Constructor / Destructor
37
38    bool ::CreateDisplaySystem(DisplaySystem displaySystem)
39    {
40       bool result = false;
41       sflnprintf("class(SDLDisplayDriver) ::CreateDisplaySystem [WIP!]\n");
42       displaySystem.flags.memBackBuffer = true;
43       result = true;
44       return result;
45    }
46
47    void ::DestroyDisplaySystem(DisplaySystem displaySystem)
48    {
49       sflnprintf("class(SDLDisplayDriver) ::DestroyDisplaySystem [STUB!]\n");
50    }
51
52    bool ::CreateDisplay(Display display)
53    {
54       bool result = false;
55       SDLDisplay sdlDisplay = display.driverData = SDLDisplay { };
56       sflnprintf("class(SDLDisplayDriver) ::CreateDisplay [WIP!]\n");
57       if(sdlDisplay)
58       {
59          // To find out the format...
60          DisplaySize(display, 1, 1);
61          result = true;
62       }
63       return result;
64    }
65
66    void ::DestroyDisplay(Display display)
67    {
68       SDLDisplay sdlDisplay = display.driverData;
69       sflnprintf("class(SDLDisplayDriver) ::DestroyDisplay [STUB!]\n");
70
71       if(display)
72          ((subclass(DisplayDriver))class(LFBDisplayDriver)).DestroyDisplay(display);
73
74       // TODO: Implementation
75
76       delete sdlDisplay;
77       display.driverData = null;
78    }
79
80
81    // Display Position and Size
82
83    bool ::DisplaySize(Display display, int width, int height)
84    {
85       SDLDisplay sdlDisplay = display.driverData;
86       bool result = false;
87       sflnprintf("class(SDLDisplayDriver) ::DisplaySize [WIP!]\n");
88
89       // TODO: Implementation
90
91       /*
92       bool validFormat = true;
93       switch(backDesc.ddpfPixelFormat.dwRGBBitCount)
94       {
95          case 8: bitmap.pixelFormat = pixelFormat8; break;
96          case 15: bitmap.pixelFormat = pixelFormat555; break;
97          case 16:
98             if(backDesc.ddpfPixelFormat.dwGBitMask == 0x3E0)
99                bitmap.pixelFormat = pixelFormat555;
100             else
101                bitmap.pixelFormat = pixelFormat565;
102             break;
103          case 32:
104             bitmap.pixelFormat = pixelFormat888; break;
105          default:
106             validFormat = false;
107             break;
108       }
109       if(validFormat)
110       {
111          bitmap.picture = (byte *)backDesc.lpSurface;
112          bitmap.stride = backDesc.lPitch;
113          bitmap.stride >>= GetColorDepthShifts(bitmap.pixelFormat);
114          bitmap.size = bitmap.stride * bitmap.height;
115       }
116       */
117       display.displaySystem.pixelFormat = sdlDisplay.bitmap.pixelFormat;
118       result = true;
119
120       ((subclass(DisplayDriver))class(LFBDisplayDriver)).DisplaySize(display, width, height);
121
122       display.width = width;
123       display.height = height;
124
125       return result;
126    }
127
128    void ::DisplayPosition(Display display, int x, int y)
129    {
130       ((subclass(DisplayDriver))class(LFBDisplayDriver)).DisplayPosition(display, x, y);
131    }
132
133
134    // Palettes
135
136    void ::SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
137    {
138       SDLDisplay sdlDisplay = display.driverData;
139       sflnprintf("class(SDLDisplayDriver) ::SetPalette [WIP!]\n");
140       if(sdlDisplay.bitmap.pixelFormat == pixelFormat8)
141       {
142          // TODO: Implementation
143       }
144       else
145          ((subclass(DisplayDriver))class(LFBDisplayDriver)).SetPalette(display, palette, colorMatch);
146    }
147
148    void ::RestorePalette(Display display)
149    {
150       //SDLDisplay sdlDisplay = display.driverData;
151       sflnprintf("class(SDLDisplayDriver) ::RestorePalette [STUB!]\n");
152    }
153
154
155    // Display the back buffer content
156
157    void ::StartUpdate(Display display)
158    {
159       //SDLDisplay sdlDisplay = display.driverData;
160       sflnprintf("class(SDLDisplayDriver) ::StartUpdate [STUB!]\n");
161    }
162
163    void ::EndUpdate(Display display)
164    {
165       //SDLDisplay sdlDisplay = display.driverData;
166       sflnprintf("class(SDL2DisplayDriver) ::EndUpdate [STUB!]\n");
167       //--//SDL_RenderPresent();
168    }
169
170    void ::Scroll(Display display, Box scroll, int x, int y, Extent dirty)
171    {
172    }
173
174    void ::Update(Display display, Box updateBox)
175    {
176       //SDLDisplay sdlDisplay = display.driverData;
177       //--//Box * box = updateBox;
178       sflnprintf("class(SDLDisplayDriver) ::Update [STUB!]\n");
179       // SDL_RenderPresent();
180    }
181
182
183    // Allocate/free a bitmap
184
185    bool ::AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
186    {
187       return ((subclass(DisplayDriver))class(LFBDisplayDriver)).AllocateBitmap(displaySystem, bitmap, width, height, stride, format, allocatePalette);
188    }
189
190    void ::FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
191    {
192       ((subclass(DisplayDriver))class(LFBDisplayDriver)).FreeBitmap(displaySystem, bitmap);
193    }
194
195
196    // Lock
197
198    bool ::LockSystem(DisplaySystem displaySystem)
199    {
200       sflnprintf("class(SDLDisplayDriver) ::LockSystem [STUB!]\n");
201       return false;
202    }
203
204    void ::UnlockSystem(DisplaySystem displaySystem)
205    {
206       sflnprintf("class(SDLDisplayDriver) ::UnlockSystem [STUB!]\n");
207    }
208
209    bool ::Lock(Display display)
210    {
211       //SDLDisplay sdlDisplay = display.driverData;
212       sflnprintf("class(SDLDisplayDriver) ::Lock [STUB!]\n");
213       return true;
214    }
215
216    void ::Unlock(Display display)
217    {
218       //SDLDisplay sdlDisplay = display.driverData;
219       sflnprintf("class(SDLDisplayDriver) ::Unlock [STUB!]\n");
220    }
221
222
223    // Get/release a surface
224
225    bool ::GetSurface(Display display, Surface surface, int x,int y, Box clip)
226    {
227       //SDLDisplay sdlDisplay = display.driverData;
228       LFBSurface lfbSurface;
229       bool result = false;
230       sflnprintf("class(SDLDisplayDriver) ::GetSurface [WIP!]\n");
231       {
232          if((surface.driverData = lfbSurface = LFBSurface { }))
233          {
234             surface.offset.x = x;
235             surface.offset.y = y;
236             //surface.unclippedBox = surface.box = clip;
237             // TODO: Implementation
238             result = ((subclass(DisplayDriver))class(LFBDisplayDriver)).GetSurface(display, surface, x, y, clip);
239          }
240       }
241       return result;
242    }
243
244    bool ::GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
245    {
246       return ((subclass(DisplayDriver))class(LFBDisplayDriver)).GetBitmapSurface(displaySystem, surface, bitmap, x, y, clip);
247    }
248
249    void ::ReleaseSurface(Display display, Surface surface)
250    {
251       //SDLDisplay sdlDisplay = display.driverData;
252       sflnprintf("class(SDL2DisplayDriver) ::ReleaseSurface [WIP!]\n");
253       ((subclass(DisplayDriver))class(LFBDisplayDriver)).ReleaseSurface(display, surface);
254       // TODO: Implementation
255    }
256
257
258    // Clip a surface
259
260    void ::Clip(Display display, Surface surface, Box clip)
261    {
262       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Clip(display, surface, clip);
263    }
264
265
266    // Grab from the screen
267
268    bool ::GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
269    {
270       bool result = false;
271       //SDLDisplay sdlDisplay = display.driverData;
272       sflnprintf("class(SDL2DisplayDriver) ::GrabScreen [WIP!]\n");
273       // TODO: Implementation
274       result = ((subclass(DisplayDriver))class(LFBDisplayDriver)).GrabScreen(display, bitmap, x,y, w,h);
275       return result;
276    }
277
278
279    // Converts a bitmap format
280
281    bool ::ConvertBitmap(DisplaySystem displaySystem, Bitmap src, PixelFormat format, ColorAlpha * palette)
282    {
283       return ((subclass(DisplayDriver))class(LFBDisplayDriver)).ConvertBitmap(displaySystem, src, format, palette);
284    }
285
286
287    // Converts an LFB bitmap into an offscreen bitmap for this device
288
289    bool ::MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
290    {
291       return ((subclass(DisplayDriver))class(LFBDisplayDriver)).MakeDDBitmap(displaySystem, bitmap, mipMaps);
292    }
293
294
295    // Font loading
296
297    Font ::LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
298    {
299       Font font = (Font)((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
300       return font;
301    }
302
303    void ::UnloadFont(DisplaySystem displaySystem, Font font)
304    {
305       ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
306    }
307
308
309    // 2D Drawing
310
311    void ::SetForeground(Display display, Surface surface, ColorAlpha color)
312    {
313       ((subclass(DisplayDriver))class(LFBDisplayDriver)).SetForeground(display, surface, color);
314    }
315
316    void ::SetBackground(Display display, Surface surface, ColorAlpha color)
317    {
318       ((subclass(DisplayDriver))class(LFBDisplayDriver)).SetBackground(display, surface, color);
319    }
320
321    void ::SetBlitTint(Display display, Surface surface, ColorAlpha tint)
322    {
323       sflnprintf("class(SDLDisplayDriver) ::SetBlitTint [STUB!]\n");
324    }
325
326    void ::LineStipple(Display display, Surface surface, uint stipple)
327    {
328       ((subclass(DisplayDriver))class(LFBDisplayDriver)).LineStipple(display, surface, stipple);
329    }
330
331    ColorAlpha ::GetPixel(Display display, Surface surface, int x, int y)
332    {
333       sflnprintf("class(SDLDisplayDriver) ::GetPixel [STUB!]\n");
334       return 0;
335    }
336
337    void ::PutPixel(Display display, Surface surface, int x, int y)
338    {
339       ((subclass(DisplayDriver))class(LFBDisplayDriver)).PutPixel(display, surface, x,y);
340    }
341
342    void ::DrawLine(Display display, Surface surface, int x1, int y1, int x2, int y2)
343    {
344       ((subclass(DisplayDriver))class(LFBDisplayDriver)).DrawLine(display, surface, x1,y1,x2,y2);
345    }
346
347    void ::Rectangle(Display display, Surface surface, int x1, int y1, int x2, int y2)
348    {
349       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Rectangle(display, surface, x1,y1,x2,y2);
350    }
351
352    void ::Area(Display display, Surface surface,int x1, int y1, int x2, int y2)
353    {
354   /*    if(x1>x2) { int tmp = x2; x2 = x1; x1 = tmp; }
355
356       if(x1<surface.box.left)  x1=surface.box.left;
357       if(x2>surface.box.right) x2=surface.box.right;
358       if(y1<surface.box.top)   y1=surface.box.top;
359       if(y2>surface.box.bottom)  y2=surface.box.bottom;
360 */
361       x1 += surface.offset.x;
362       x2 += surface.offset.x;
363       y1 += surface.offset.y;
364       y2 += surface.offset.y;
365
366       {
367          //--//SDL_Rect rect = { (short)x1, (short)y1, (short)(x2-x1+1), (short)(y2-y1+1) };
368          //--//SDL_SetRenderDrawColor(surface.background.color.r, surface.background.color.g, surface.background.color.b, surface.background.a);
369          //--//SDL_RenderFill(&rect);
370          //SDL_FillRect()
371          // ((subclass(DisplayDriver))class(LFBDisplayDriver)).Area(display, surface, x1,y1,x2,y2);
372       }
373    }
374
375    void ::Clear(Display display, Surface surface, ClearType type)
376    {
377       sflnprintf("class(SDL2DisplayDriver) ::Clear [WIP!]\n");
378       //--//SDL_Rect rect = { (short)surface.box.left, (short)surface.box.top, (short)(surface.box.right-surface.box.left+1), (short)(surface.box.bottom-surface.box.top+1) };
379       //--//SDL_SetRenderDrawColor(surface.background.color.r, surface.background.color.g, surface.background.color.b, surface.background.a);
380       //--//SDL_RenderFill(&rect);
381       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Clear(display, surface, type);
382    }
383
384    void ::Blit(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h)
385    {
386       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Blit(display, surface, src, dx, dy, sx, sy, w, h);
387    }
388
389    void ::Stretch(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
390    {
391       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Stretch(display, surface, src, dx, dy, sx, sy, w, h, sw, sh);
392    }
393
394    void ::Filter(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
395    {
396       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Filter(display, surface, src, dx, dy, sx, sy, w, h, sw, sh);
397    }
398
399    void ::BlitDI(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h)
400    {
401       // Using Blit()
402       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Blit(display, surface, src, dx, dy, sx, sy, w, h);
403    }
404
405    void ::StretchDI(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
406    {
407       // Using Stretch()
408       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Stretch(display, surface, src, dx, dy, sx, sy, w, h, sw, sh);
409    }
410
411    void ::FilterDI(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
412    {
413       // Using Filter()
414       //Filter(display, surface, src, dx, dy, sx, sy, w, h, sw, sh);
415       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Filter(display, surface, src, dx, dy, sx, sy, w, h, sw, sh);
416    }
417
418    void ::TextFont(Display display, Surface surface, Font font)
419    {
420       ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
421       // why this?
422       SetForeground(display, surface, surface.foreground);
423    }
424
425    void ::TextOpacity(Display display, Surface surface, bool opaque)
426    {
427       ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextOpacity(display, surface, opaque);
428    }
429
430    void ::WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
431    {
432       if(surface.textOpacity)
433       {
434          int w, h;
435          ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
436          Area(display, surface, x, y, x+w-1, y+h-1);
437       }
438       ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x,y, text, len);
439    }
440
441    void ::TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
442    {
443       ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextExtent(display, surface, text, len, width, height);
444    }
445
446    void ::FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
447    {
448       ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
449    }
450
451    void ::DrawingChar(Display display, Surface surface, char character)
452    {
453       sflnprintf("class(SDLDisplayDriver) ::DrawingChar [STUB!]\n");
454    }
455
456    void ::NextPage(Display display)
457    {
458       sflnprintf("class(SDLDisplayDriver) ::NextPage [STUB!]\n");
459    }
460
461    // 3D Graphics
462
463 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
464    void ::SetRenderState(Display display, RenderState state, uint value)
465    {
466       sflnprintf("class(SDLDisplayDriver) ::SetRenderState [STUB!]\n");
467    }
468
469    void ::SetLight(Display display, int id, Light light)
470    {
471       sflnprintf("class(SDLDisplayDriver) ::SetLight [STUB!]\n");
472    }
473
474    void ::SetCamera(Display display, Surface surface, Camera camera)
475    {
476       sflnprintf("class(SDLDisplayDriver) ::SetCamera [STUB!]\n");
477    }
478
479    bool ::AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
480    {
481       sflnprintf("class(SDLDisplayDriver) ::AllocateMesh [STUB!]\n");
482       return false;
483    }
484
485    void ::FreeMesh(DisplaySystem displaySystem, Mesh mesh)
486    {
487       sflnprintf("class(SDLDisplayDriver) ::FreeMesh [STUB!]\n");
488    }
489
490    bool ::LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
491    {
492       sflnprintf("class(SDLDisplayDriver) ::LockMesh [STUB!]\n");
493       return false;
494    }
495
496    void ::UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
497    {
498       sflnprintf("class(SDLDisplayDriver) ::UnlockMesh [STUB!]\n");
499    }
500
501    void * ::AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
502    {
503       sflnprintf("class(SDLDisplayDriver) ::AllocateIndices [STUB!]\n");
504       return null;
505    }
506
507    void ::FreeIndices(DisplaySystem displaySystem, void * indices)
508    {
509       sflnprintf("class(SDLDisplayDriver) ::FreeIndices [STUB!]\n");
510    }
511
512    uint16 * ::LockIndices(DisplaySystem displaySystem, void * indices)
513    {
514       sflnprintf("class(SDLDisplayDriver) ::LockIndices [STUB!]\n");
515       return null;
516    }
517
518    void ::UnlockIndices(DisplaySystem displaySystem, void * indices, bool indices32bit, int nIndices)
519    {
520       sflnprintf("class(SDLDisplayDriver) ::UnlockIndices [STUB!]\n");
521    }
522
523    void ::SelectMesh(Display display, Mesh mesh)
524    {
525       sflnprintf("class(SDLDisplayDriver) ::SelectMesh [STUB!]\n");
526    }
527
528    void ::ApplyMaterial(Display display, Material material, Mesh mesh)
529    {
530       sflnprintf("class(SDLDisplayDriver) ::ApplyMaterial [STUB!]\n");
531    }
532
533    void ::DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
534    {
535       sflnprintf("class(SDLDisplayDriver) ::DrawPrimitives [STUB!]\n");
536    }
537
538    void ::PushMatrix(Display display)
539    {
540       sflnprintf("class(SDLDisplayDriver) ::PushMatrix [STUB!]\n");
541    }
542
543    void ::PopMatrix(Display display, bool setMatrix)
544    {
545       sflnprintf("class(SDLDisplayDriver) ::PopMatrix [STUB!]\n");
546    }
547
548    void ::SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
549    {
550       sflnprintf("class(SDLDisplayDriver) ::SetTransform [STUB!]\n");
551    }
552 #endif
553
554 }
555
556 #endif // !defined(NO_SDL_DRIVERS) && !defined(NO_SDL1_DRIVER)