Previous 15994 Revisions Next

r4071 Tuesday 6th January, 2009 at 02:47:40 UTC by R. Belmont
[SDLMESS] Sync with SDLMAME 0.129
[src/osd/sdl]blit13.h* debugwin.c draw13.c* drawogl.c drawsdl.c gl_shader_tool.h input.c osdsdl.h sdlmain.c video.c video.h window.c window.h

src/osd/sdl/drawogl.c
r4070r4071
3131
3232#include "gl_shader_mgr.h"
3333
34#ifdef SDLMAME_WIN32
34#if defined(SDLMAME_WIN32) && !(SDL_VERSION_ATLEAST(1,3,0))
3535typedef ptrdiff_t GLsizeiptr;
3636#endif
3737
r4070r4071
154154//  TYPES
155155//============================================================
156156
157typedef struct _texture_info texture_info;
158
159#if USE_OPENGL
160typedef void (*texture_copy_func)(texture_info *texture, const render_texinfo *texsource);
161#endif
162   
163/* texture_info holds information about a texture */
164struct _texture_info
165{
166   texture_info *      next;            // next texture in the list
167   HashT            hash;            // hash value for the texture (must be >= pointer size)
168   UINT32            flags;            // rendering flags
169   render_texinfo      texinfo;         // copy of the texture info
170   int               rawwidth, rawheight;   // raw width/height of the texture
171   int               rawwidth_create;   // raw width/height, pow2 compatible, if needed
172   int                 rawheight_create;   // (create and initial set the texture, not for copy!)
173   int               type;            // what type of texture are we?
174   int               format;            // texture format
175   int               borderpix;         // do we have a 1 pixel border?
176   int               xprescale;         // what is our X prescale factor?
177   int               yprescale;         // what is our Y prescale factor?
178   int               prescale_effect;   // which prescale effect (if any) to use
179   int               nocopy;            // must the texture date be copied?
180   
181   UINT32            texture;         // OpenGL texture "name"/ID
182
183   const GLint *      texProperties;      // texture properties
184   texture_copy_func   texCopyFn;         // texture copy function, !=NULL if !nocopy
185   GLenum            texTarget;         // OpenGL texture target
186   int               texpow2;         // Is this texture pow2
187   
188   UINT32            mpass_dest_idx;         // Multipass dest idx [0..1]
189   UINT32            mpass_textureunit[2];   // texture unit names for GLSL
190   
191   UINT32            mpass_texture_mamebm[2];// Multipass OpenGL texture "name"/ID for the shader
192   UINT32            mpass_fbo_mamebm[2];   // framebuffer object for this texture, multipass
193   UINT32            mpass_texture_scrn[2];   // Multipass OpenGL texture "name"/ID for the shader
194   UINT32            mpass_fbo_scrn[2];      // framebuffer object for this texture, multipass
195   
196   UINT32            lut_texture;         // LUT OpenGL texture "name"/ID for the shader
197   int               lut_table_width;      // LUT table width
198   int               lut_table_height;      // LUT table height
199   
200   UINT32            pbo;               // pixel buffer object for this texture (DYNAMIC only!)
201   UINT32            *data;               // pixels for the texture
202   int               data_own;            // do we own / allocated it ?
203   UINT32            *effectbuf;            // buffer for intermediate effect results or NULL
204   GLfloat            texCoord[8];
205   GLuint            texCoordBufferName;
206
207};
208
157209/* sdl_info is the information about SDL for the current screen */
158210typedef struct _sdl_info sdl_info;
159211struct _sdl_info
r4070r4071
476528      SDL_GetCurrentDisplayMode(&mode);
477529      mode.w = width;
478530      mode.h = height;
531      if (window->refresh)
532         mode.refresh_rate = window->refresh;
479533      SDL_SetFullscreenDisplayMode(&mode);   // Try to set mode
480534   }
481535   else
r4070r4071
484538   window->window_id = SDL_CreateWindow(window->title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
485539         width, height, sdl->extra_flags);
486540
487   if  (!(SDL_GetWindowFlags(window->window_id) & SDL_WINDOW_OPENGL) )
541   if  (!window->window_id )
488542   {
489      mame_printf_error("OpenGL not supported on this driver!\n");
543      mame_printf_error("OpenGL not supported on this driver: %s\n", SDL_GetError());
490544      return 1;
491545   }
492546
r4070r4071
496550   SDL_GetWindowSize(window->window_id, &window->width, &window->height);
497551   
498552   sdl->gl_context_id = SDL_GL_CreateContext(window->window_id);
553   if  (!sdl->gl_context_id)
554   {
555      mame_printf_error("OpenGL not supported on this driver: %s\n", SDL_GetError());
556      return 1;
557   }
499558
500   SDL_GL_SetSwapInterval(options_get_bool(mame_options(), SDLOPTION_WAITVSYNC) ? 2 : 0);
559   SDL_GL_SetSwapInterval(video_config.waitvsync ? 2 : 0);
501560   
502561#else
503562   sdl->extra_flags = (window->fullscreen ?  SDL_FULLSCREEN : SDL_RESIZABLE);
r4070r4071
505564
506565   SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
507566   #if (SDL_VERSION_ATLEAST(1,2,10))
508   SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,
509         options_get_bool(mame_options(), SDLOPTION_WAITVSYNC) ? 1 : 0);
567   SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, video_config.waitvsync ? 1 : 0);
510568   #endif
511569   
512570   load_gl_lib();
src/osd/sdl/input.c
r4070r4071
22//
33//  input.c - SDL implementation of MAME input routines
44//
5//  Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team.
5//  Copyright (c) 1996-2009, Nicola Salmoria and the MAME Team.
66//  Visit http://mamedev.org for licensing and usage restrictions.
77//
88//  SDLMAME by Olivier Galibert and R. Belmont
r4070r4071
4747};
4848
4949#define MAX_KEYS         256
50#define MAX_AXES         8
50#define MAX_AXES         32
5151#define MAX_BUTTONS         32
5252#define MAX_HATS         8
5353#define MAX_POV            4
r4070r4071
727727         sprintf(tempname, "A%d %s", axis, devinfo->name);
728728         input_device_item_add(devinfo->device, tempname, &devinfo->joystick.axes[axis], itemid, generic_axis_get_state);
729729      }
730
730                   
731731      // loop over all buttons
732732      for (button = 0; button < SDL_JoystickNumButtons(joy); button++)
733733      {
r4070r4071
12651265               {
12661266                  devinfo->joystick.axes[event.jaxis.axis] = (event.jaxis.value * 2);
12671267               }
1268               if ((axis == 12) || (axis == 13))
1268               else
12691269               {
1270                  devinfo->joystick.axes[event.jaxis.axis] = (event.jaxis.value / 2) + 16383;
1270                  int magic = (event.jaxis.value / 2) + 16384;
1271
1272                  devinfo->joystick.axes[event.jaxis.axis] = magic;
12711273               }
12721274            }
12731275            else
r4070r4071
14361438            app_has_mouse_focus = 0;
14371439            break;
14381440         }
1441         case SDL_WINDOWEVENT_MOVED:
1442            sdlwindow_clear(window);
1443            focus_window = window;
1444            break;
14391445         case SDL_WINDOWEVENT_RESIZED:
14401446            if (event.window.data1 != window->width || event.window.data2 != window->height)
14411447               sdlwindow_resize(window, event.window.data1, event.window.data2);
src/osd/sdl/drawsdl.c
r4070r4071
271271      sdl->yuv_bitmap = NULL;   
272272   }
273273
274   if (sdl_sm->is_scale)
274   if (sdl_sm->is_scale)
275   {
275276      render_target_get_minimum_size(window->target, &sdl->hw_scale_width, &sdl->hw_scale_height);
277      if (video_config.prescale)
278      {
279         sdl->hw_scale_width *= video_config.prescale;
280         sdl->hw_scale_height *= video_config.prescale;
281      }
282   }
276283
277284   if (sdl_sm->is_yuv)
278285      sdl->yuv_bitmap = malloc_or_die(sdl->hw_scale_width * sdl->hw_scale_height * sizeof(UINT16));
r4070r4071
280287   fmt = (sdl_sm->pixel_format ? sdl_sm->pixel_format : mode.format);
281288
282289   if (sdl_sm->is_scale)
283      sdl->texture_id = SDL_CreateTexture(fmt, SDL_TEXTUREACCESS_STREAMING,
284            sdl->hw_scale_width * sdl_sm->mult_w, sdl->hw_scale_height * sdl_sm->mult_h);
290   {
291      int w = sdl->hw_scale_width * sdl_sm->mult_w;
292      int h = sdl->hw_scale_height * sdl_sm->mult_h;
293     
294      sdl->texture_id = SDL_CreateTexture(fmt, SDL_TEXTUREACCESS_STREAMING, w, h);
295     
296   }
285297   else
286298   {
287299      sdl->texture_id = SDL_CreateTexture(fmt, SDL_TEXTUREACCESS_STREAMING,
r4070r4071
305317   int minimum_width, minimum_height;
306318
307319   render_target_get_minimum_size(window->target, &minimum_width, &minimum_height);
320
321   if (video_config.prescale)
322   {
323      minimum_width *= video_config.prescale;
324      minimum_height *= video_config.prescale;
325   }
326   
308327   if (sdl->yuvsurf != NULL)
309328   {
310329      SDL_FreeYUVOverlay(sdl->yuvsurf);
r4070r4071
401420      SDL_GetCurrentDisplayMode(&mode);
402421      mode.w = width;
403422      mode.h = height;
423      if (window->refresh)
424         mode.refresh_rate = window->refresh;
404425      SDL_SetFullscreenDisplayMode(&mode);   // Try to set mode
405426   }
406427   else
r4070r4071
420441
421442   // create a texture
422443   
423   if (options_get_bool(mame_options(), SDLOPTION_WAITVSYNC))   
444   if (video_config.waitvsync)   
424445      SDL_CreateRenderer(window->window_id, -1, SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC);
425446   else
426447      SDL_CreateRenderer(window->window_id, -1, SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTDISCARD);
r4070r4071
705726   if (sdl->blittimer > 0)
706727   {
707728      /* SDL Underlays need alpha = 0 ! */
708      SDL_RenderFill(0,0,0,0 /*255*/,NULL);
729      SDL_SetRenderDrawColor(0,0,0,0);
730      SDL_RenderFill(NULL);
731      //SDL_RenderFill(0,0,0,0 /*255*/,NULL);
709732      sdl->blittimer--;
710733   }
711734   
src/osd/sdl/draw13.c
r0r4071
1//============================================================
2// 
3//  drawogl.c - SDL software and OpenGL implementation
4//
5//  Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team.
6//  Visit http://mamedev.org for licensing and usage restrictions.
7//
8//  SDLMAME by Olivier Galibert and R. Belmont
9//
10//  Note: D3D9 goes to a lot of trouble to fiddle with MODULATE
11//        mode on textures.  That is the default in OpenGL so we
12//        don't have to touch it.
13//
14//============================================================
15
16// standard C headers
17#include <math.h>
18#include <stdio.h>
19
20// MAME headers
21#include "osdcomm.h"
22#include "render.h"
23#include "options.h"
24#include "driver.h"
25
26// standard SDL headers
27#include <SDL/SDL.h>
28
29// OSD headers
30#include "osdsdl.h"
31#include "window.h"
32
33//============================================================
34//  DEBUGGING
35//============================================================
36
37//============================================================
38//  CONSTANTS
39//============================================================
40
41#define STAT_PIXEL_THRESHOLD (150*150)
42
43enum
44{
45   TEXTURE_TYPE_NONE,
46   TEXTURE_TYPE_PLAIN,
47   TEXTURE_TYPE_SURFACE
48};
49
50
51//============================================================
52//  MACROS
53//============================================================
54
55#define IS_OPAQUE(a)      (a >= 1.0f)
56#define IS_TRANSPARENT(a)   (a <  0.0001f)
57
58#define MAX4(a, b, c, d) MAX(a, MAX(b, MAX(c, d)))
59#define MIN4(a, b, c, d) MIN(a, MIN(b, MIN(c, d)))
60
61
62//============================================================
63//  TYPES
64//============================================================
65
66
67typedef struct _quad_setup_data quad_setup_data;
68struct _quad_setup_data
69{
70   INT32         dudx, dvdx, dudy, dvdy;
71   INT32         startu, startv;
72   INT32         rotwidth, rotheight;
73};
74
75typedef struct _texture_info texture_info;
76
77typedef void (*texture_copy_func)(texture_info *texture, const render_texinfo *texsource);
78
79typedef struct _copy_info copy_info;
80struct _copy_info {
81   int             src_fmt;
82   Uint32            dst_fmt;
83   int               dst_bpp;
84   int               rotate;
85   texture_copy_func   func;
86   Uint32            bm_mask;
87   const char         *srcname;
88   const char         *dstname;
89   /* Statistics */
90   UINT64            pixel_count;
91   INT64            time;
92   int               samples;
93   int               perf;
94   /* list */
95   copy_info         *next;
96};
97
98/* texture_info holds information about a texture */
99struct _texture_info
100{
101   texture_info *      next;            // next texture in the list
102   
103   HashT            hash;            // hash value for the texture (must be >= pointer size)
104   UINT32            flags;            // rendering flags
105   render_texinfo      texinfo;         // copy of the texture info
106   
107   int               rawwidth, rawheight;// raw width/height of the texture
108
109   int               format;            // texture format
110   void            *pixels;         // pixels for the texture
111   int               pitch;
112   int               pixels_own;         // do we own / allocated it ?
113
114   SDL_TextureID      texture_id;
115   
116   copy_info         *copyinfo;
117   Uint32            sdl_access;
118   Uint32            sdl_blendmode;
119   quad_setup_data      setup;
120   int               is_rotated;
121   
122   osd_ticks_t         last_access;
123};
124
125/* sdl_info is the information about SDL for the current screen */
126typedef struct _sdl_info sdl_info;
127struct _sdl_info
128{
129   INT32          blittimer;
130   UINT32         extra_flags;
131
132   texture_info *   texlist;            // list of active textures
133   INT32            texture_max_width;        // texture maximum width
134   INT32            texture_max_height;       // texture maximum height
135
136   float         last_hofs;
137   float         last_vofs;
138   
139   // Stats
140   INT64         last_blit_time;
141   INT64         last_blit_pixels;
142};
143
144//============================================================
145//  PROTOTYPES
146//============================================================
147
148// core functions
149
150static void draw13_exit(void);
151static void draw13_attach(sdl_draw_info *info, sdl_window_info *window);
152static int draw13_window_create(sdl_window_info *window, int width, int height);
153static void draw13_window_resize(sdl_window_info *window, int width, int height);
154static void draw13_window_destroy(sdl_window_info *window);
155static int draw13_window_draw(sdl_window_info *window, UINT32 dc, int update);
156static const render_primitive_list *draw13_window_get_primitives(sdl_window_info *window);
157static void draw13_destroy_all_textures(sdl_window_info *window);
158static void draw13_window_clear(sdl_window_info *window);
159static int draw13_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt);
160static void draw13_destroy_texture(sdl_info *sdl, texture_info *texture);
161
162//============================================================
163//  Textures
164//============================================================
165
166static void texture_set_data(sdl_info *sdl, texture_info *texture, const render_texinfo *texsource, UINT32 flags);
167static texture_info *texture_create(sdl_window_info *window, const render_texinfo *texsource, quad_setup_data *setup, UINT32 flags);
168static texture_info *texture_find(sdl_info *sdl, const render_primitive *prim, quad_setup_data *setup);
169static texture_info * texture_update(sdl_window_info *window, const render_primitive *prim);
170
171
172//============================================================
173//  TEXCOPY FUNCS
174//============================================================
175
176#include "blit13.h"
177
178//============================================================
179//  STATIC VARIABLES
180//============================================================
181
182#define SDL_TEXFORMAT_LAST SDL_TEXFORMAT_PALETTE16A
183#define BM_ALL (-1)
184//( SDL_BLENDMODE_MASK | SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD)
185
186#define texcopy_NULL NULL
187#define ENTRY(a,b,c,d,f) { SDL_TEXFORMAT_ ## a, SDL_PIXELFORMAT_ ## b, c, d, texcopy_ ## f, BM_ALL, #a, #b, 0, 0, 0, 0}
188#define ENTRY_BM(a,b,c,d,f,bm) { SDL_TEXFORMAT_ ## a, SDL_PIXELFORMAT_ ## b, c, d, texcopy_ ## f, bm, #a, #b, 0, 0, 0, 0}
189#define ENTRY_LR(a,b,c,d,f) { SDL_TEXFORMAT_ ## a, SDL_PIXELFORMAT_ ## b, c, d, texcopy_ ## f, BM_ALL, #a, #b, 0, 0, 0, -1}
190
191static copy_info blit_info_default[] =
192{
193   /* no rotation */
194   ENTRY(ARGB32,          ARGB8888,    4, 0, NULL),
195   ENTRY_LR(ARGB32,       RGB888,    4, 0, argb32_rgb32),
196   /* Entry for primarily for directfb */
197   ENTRY_BM(ARGB32,       RGB888,    4, 0, argb32_rgb32, SDL_BLENDMODE_ADD),
198   ENTRY_BM(ARGB32,       RGB888,    4, 0, argb32_rgb32, SDL_BLENDMODE_MOD),
199   ENTRY_BM(ARGB32,       RGB888,    4, 0, argb32_rgb32, SDL_BLENDMODE_NONE),
200
201   ENTRY(RGB32,          ARGB8888,    4, 0, rgb32_argb32),
202   ENTRY(RGB32,          RGB888,    4, 0, NULL),
203
204   ENTRY(RGB32_PALETTED,   ARGB8888,    4, 0, rgb32pal_argb32),
205   ENTRY(RGB32_PALETTED,   RGB888,    4, 0, rgb32pal_argb32),
206
207   ENTRY(YUY16,          UYVY,       2, 0, NULL /* yuv16_uyvy*/),
208   ENTRY(YUY16,          YUY2,       2, 0, yuv16_yuy2),
209   ENTRY(YUY16,          YVYU,       2, 0, yuv16_yvyu),
210   ENTRY(YUY16,          ARGB8888,   4, 0, yuv16_argb32),
211   ENTRY(YUY16,          RGB888,      4, 0, yuv16pal_argb32),
212   
213   ENTRY(YUY16_PALETTED,    UYVY,       2, 0, yuv16pal_uyvy),
214   ENTRY(YUY16_PALETTED,    YUY2,       2, 0, yuv16pal_yuy2),
215   ENTRY(YUY16_PALETTED,    YVYU,       2, 0, yuv16pal_yvyu),
216   ENTRY(YUY16_PALETTED,    ARGB8888,   4, 0, yuv16pal_argb32),
217   ENTRY(YUY16_PALETTED,    RGB888,    4, 0, yuv16pal_argb32),
218
219   ENTRY(PALETTE16,       ARGB8888,    4, 0, pal16_argb32),
220   ENTRY(PALETTE16,       RGB888,    4, 0, pal16_argb32),
221
222   ENTRY(RGB15,          RGB555,    2, 0, NULL /* rgb15_argb1555 */),
223   ENTRY(RGB15,          ARGB1555,    2, 0, rgb15_argb1555),
224   ENTRY(RGB15,          ARGB8888,    4, 0, rgb15_argb32),
225   ENTRY(RGB15,          RGB888,    4, 0, rgb15_argb32),
226
227   ENTRY(RGB15_PALETTED,    ARGB8888,    4, 0, rgb15pal_argb32),
228   ENTRY(RGB15_PALETTED,    RGB888,    4, 0, rgb15pal_argb32),
229
230   ENTRY(PALETTE16A,       ARGB8888,    4, 0, pal16a_argb32),
231   ENTRY(PALETTE16A,       RGB888,    4, 0, pal16a_rgb32),
232
233   /* rotation */
234   ENTRY(ARGB32,          ARGB8888,    4, 1, rot_argb32_argb32),
235   ENTRY_LR(ARGB32,       RGB888,    4, 1, rot_argb32_rgb32),
236   /* Entry for primarily for directfb */
237   ENTRY_BM(ARGB32,       RGB888,    4, 1, rot_argb32_rgb32, SDL_BLENDMODE_ADD),
238   ENTRY_BM(ARGB32,       RGB888,    4, 1, rot_argb32_rgb32, SDL_BLENDMODE_MOD),
239   ENTRY_BM(ARGB32,       RGB888,    4, 1, rot_argb32_rgb32, SDL_BLENDMODE_NONE),
240
241   ENTRY(RGB32,          ARGB8888,    4, 1, rot_rgb32_argb32),
242   ENTRY(RGB32,          RGB888,    4, 1, rot_argb32_argb32),
243
244   ENTRY(RGB32_PALETTED,   ARGB8888,    4, 1, rot_rgb32pal_argb32),
245   ENTRY(RGB32_PALETTED,   RGB888,    4, 1, rot_rgb32pal_argb32),
246
247   ENTRY(YUY16,          ARGB8888,    4, 1, rot_yuv16_argb32),
248   ENTRY(YUY16,          RGB888,      4, 1, rot_yuv16_argb32),
249   
250   ENTRY(YUY16_PALETTED,    ARGB8888,    4, 1, rot_yuv16pal_argb32),
251   ENTRY(YUY16_PALETTED,    RGB888,       4, 1, rot_yuv16pal_argb32),
252
253   ENTRY(PALETTE16,       ARGB8888,    4, 1, rot_pal16_argb32),
254   ENTRY(PALETTE16,       RGB888,    4, 1, rot_pal16_argb32),
255
256   ENTRY(RGB15,          RGB555,    2, 1, rot_rgb15_argb1555),
257   ENTRY(RGB15,          ARGB1555,    2, 1, rot_rgb15_argb1555),
258   ENTRY(RGB15,          ARGB8888,    4, 1, rot_rgb15_argb32),
259   ENTRY(RGB15,          RGB888,    4, 1, rot_rgb15_argb32),
260
261   ENTRY(RGB15_PALETTED,    ARGB8888,    4, 1, rot_rgb15pal_argb32),
262   ENTRY(RGB15_PALETTED,    RGB888,    4, 1, rot_rgb15pal_argb32),
263
264   ENTRY(PALETTE16A,       ARGB8888,    4, 1, rot_pal16a_argb32),
265   ENTRY(PALETTE16A,       RGB888,    4, 1, rot_pal16a_rgb32),
266
267{ -1 },
268};
269
270static copy_info blit_info_16bpp[] =
271{
272   /* no rotation */
273   ENTRY(PALETTE16,       RGB555,    2, 0, pal16_argb1555),
274   ENTRY(PALETTE16,       ARGB1555,    2, 0, pal16_argb1555),
275
276   ENTRY(RGB15_PALETTED,    RGB555,    2, 0, rgb15pal_argb1555),
277   ENTRY(RGB15_PALETTED,    ARGB1555,    2, 0, rgb15pal_argb1555),
278
279   /* rotation */
280   ENTRY(PALETTE16,       RGB555,    2, 1, rot_pal16_argb1555),
281   ENTRY(PALETTE16,       ARGB1555,    2, 1, rot_pal16_argb1555),
282
283   ENTRY(RGB15_PALETTED,    RGB555,    2, 1, rot_rgb15pal_argb1555),
284   ENTRY(RGB15_PALETTED,    ARGB1555,    2, 1, rot_rgb15pal_argb1555),
285
286{ -1 },
287};
288
289static copy_info *blit_info[SDL_TEXFORMAT_LAST+1];
290
291static struct
292{
293   Uint32   format;
294   int      status;
295} fmt_support[30] = { { 0, 0 } };
296
297
298//============================================================
299//  INLINES
300//============================================================
301
302
303INLINE float round_nearest(float f)
304{
305   return floor(f + 0.5f);
306}
307
308INLINE HashT texture_compute_hash(const render_texinfo *texture, UINT32 flags)
309{
310   return (HashT)texture->base ^ (flags & (PRIMFLAG_BLENDMODE_MASK | PRIMFLAG_TEXFORMAT_MASK));
311}
312
313INLINE Uint32 map_blendmode(int blendmode)
314{
315   switch (blendmode)
316   {
317      case BLENDMODE_NONE:
318         return SDL_BLENDMODE_NONE;
319      case BLENDMODE_ALPHA:     
320         return SDL_BLENDMODE_BLEND;
321      case BLENDMODE_RGB_MULTIPLY:   
322         return SDL_BLENDMODE_MOD;
323      case BLENDMODE_ADD:     
324         return SDL_BLENDMODE_ADD;
325      default:
326         mame_printf_warning("Unknown Blendmode %d", blendmode);
327    }
328   return SDL_BLENDMODE_NONE;
329}
330
331INLINE void set_coloralphamode(SDL_TextureID texture_id, const render_color *color)
332{
333   UINT32 sr = (UINT32)(255.0f * color->r);
334   UINT32 sg = (UINT32)(255.0f * color->g);
335   UINT32 sb = (UINT32)(255.0f * color->b);
336   UINT32 sa = (UINT32)(255.0f * color->a);
337   
338   
339   if (color->r >= 1.0f && color->g >= 1.0f && color->b >= 1.0f && IS_OPAQUE(color->a))
340   {
341      SDL_SetTextureColorMod(texture_id, 0xFF, 0xFF, 0xFF);
342      SDL_SetTextureAlphaMod(texture_id, 0xFF);         
343   }
344   /* coloring-only case */
345   else if (IS_OPAQUE(color->a))
346   {
347      SDL_SetTextureColorMod(texture_id, sr, sg, sb);
348      SDL_SetTextureAlphaMod(texture_id, 0xFF);         
349   }
350   /* alpha and/or coloring case */
351   else if (!IS_TRANSPARENT(color->a))
352   {
353      SDL_SetTextureColorMod(texture_id, sr, sg, sb);
354      SDL_SetTextureAlphaMod(texture_id, sa);         
355   }
356   else
357   {
358      SDL_SetTextureColorMod(texture_id, 0xFF, 0xFF, 0xFF);
359      SDL_SetTextureAlphaMod(texture_id, 0xFF);         
360   }
361}
362
363INLINE void render_quad(sdl_info *sdl, texture_info *texture, render_primitive *prim, int x, int y)
364{
365   SDL_TextureID texture_id;
366   SDL_Rect target_rect;
367   
368   target_rect.x = x;
369   target_rect.y = y;
370   target_rect.w = round_nearest(prim->bounds.x1 - prim->bounds.x0);
371   target_rect.h = round_nearest(prim->bounds.y1 - prim->bounds.y0);
372   
373   if (texture)
374   {
375      texture_id = texture->texture_id;
376
377      texture->copyinfo->time -= osd_ticks();
378      if ((PRIMFLAG_GET_SCREENTEX(prim->flags)) && video_config.filter)
379      {
380         SDL_SetTextureScaleMode(texture->texture_id,  SDL_TEXTURESCALEMODE_BEST);
381      }
382      else
383      {
384         SDL_SetTextureScaleMode(texture->texture_id,  SDL_TEXTURESCALEMODE_FAST);
385      }
386      SDL_SetTextureBlendMode(texture_id, texture->sdl_blendmode);
387      set_coloralphamode(texture_id, &prim->color);
388      SDL_RenderCopy(texture_id, NULL, &target_rect);   
389      texture->copyinfo->time += osd_ticks();
390
391      texture->copyinfo->pixel_count += MAX(STAT_PIXEL_THRESHOLD , (texture->rawwidth * texture->rawheight));
392      if (sdl->last_blit_pixels)
393      {
394         texture->copyinfo->time += (sdl->last_blit_time * (INT64) (texture->rawwidth * texture->rawheight)) / (INT64) sdl->last_blit_pixels;
395      }
396      texture->copyinfo->samples++;
397      texture->copyinfo->perf = ( texture->copyinfo->pixel_count * (osd_ticks_per_second()/1000)) / texture->copyinfo->time;
398   }
399   else
400   {
401      UINT32 sr = (UINT32)(255.0f * prim->color.r);
402      UINT32 sg = (UINT32)(255.0f * prim->color.g);
403      UINT32 sb = (UINT32)(255.0f * prim->color.b);
404      UINT32 sa = (UINT32)(255.0f * prim->color.a);
405
406      SDL_SetRenderDrawBlendMode(map_blendmode(PRIMFLAG_GET_BLENDMODE(prim->flags)));
407      SDL_SetRenderDrawColor(sr, sg, sb, sa);
408      SDL_RenderFill(&target_rect);
409   }
410}
411
412#if 0
413int RendererSupportsFormat(Uint32 format)
414{
415    struct SDL_RendererInfo render_info;
416   int i;
417   
418    SDL_GetRendererInfo(&render_info);
419   
420   for (i=0; i < render_info.num_texture_formats; i++)
421   {
422      if (format == render_info.texture_formats[i])
423         return 1;
424   }
425   return 0;
426}   
427#endif
428
429int RendererSupportsFormat(Uint32 format, Uint32 access)
430{
431   int i;
432   SDL_TextureID texid;
433   for (i=0; fmt_support[i].format != 0; i++)
434   {
435      if (format == fmt_support[i].format)
436      {
437         return fmt_support[i].status;
438      }
439   }
440   /* not tested yet */
441   fmt_support[i].format = format;
442   fmt_support[i + 1].format = 0;
443   texid = SDL_CreateTexture(format, access, 10, 10);
444   if (texid)
445   {
446      fmt_support[i].status = 1;
447      SDL_DestroyTexture(texid);
448      return 1;
449   }
450   fmt_support[i].status = 0;
451   return 0;
452}   
453
454//============================================================
455//  draw13_init
456//============================================================
457
458static void add_list(copy_info **head, copy_info *element, Uint32 bm)
459{
460   copy_info *new = malloc(sizeof(copy_info));
461   *new = *element;
462
463   new->bm_mask = bm;
464   new->next = *head;
465   *head = new;
466}
467
468static void expand_copy_info(copy_info *list)
469{
470   copy_info   *bi;
471   
472   for (bi = list; bi->src_fmt != -1; bi++)
473   {
474      if (bi->bm_mask == BM_ALL)
475      {
476         add_list(&blit_info[bi->src_fmt], bi, SDL_BLENDMODE_NONE);
477         add_list(&blit_info[bi->src_fmt], bi, SDL_BLENDMODE_ADD);
478         add_list(&blit_info[bi->src_fmt], bi, SDL_BLENDMODE_MOD);
479         add_list(&blit_info[bi->src_fmt], bi, SDL_BLENDMODE_BLEND);
480      }
481      else
482         add_list(&blit_info[bi->src_fmt], bi, bi->bm_mask);
483   }
484}
485
486int draw13_init(sdl_draw_info *callbacks)
487{
488   
489   // fill in the callbacks
490   callbacks->exit = draw13_exit;
491   callbacks->attach = draw13_attach;
492
493   mame_printf_verbose("Using SDL native texturing driver (SDL 1.3+)\n");
494   
495   expand_copy_info(blit_info_default);
496   //FIXME: -opengl16 should be -opengl -prefer16bpp
497   //if (video_config.prefer16bpp_tex)
498      expand_copy_info(blit_info_16bpp);
499   
500   return 0;
501}
502
503
504//============================================================
505//  draw13_exit
506//============================================================
507
508static void draw13_exit(void)
509{
510   int i;
511   copy_info *bi;
512   for (i = 0; i <= SDL_TEXFORMAT_LAST; i++)
513      for (bi = blit_info[i]; bi != NULL; bi = bi->next)
514      {
515         if (bi->pixel_count)
516            mame_printf_verbose("%s -> %s %s blendmode 0x%02x, %d samples: %d KPixel/sec\n", bi->srcname, bi->dstname,
517                  bi->rotate ? "rot" : "norot", bi->bm_mask, bi->samples,
518                  (int) bi->perf);
519      }
520}
521
522//============================================================
523//  draw13_attach
524//============================================================
525
526static void draw13_attach(sdl_draw_info *info, sdl_window_info *window)
527{
528   // fill in the callbacks
529   window->create = draw13_window_create;
530   window->resize = draw13_window_resize;
531   window->get_primitives = draw13_window_get_primitives;
532   window->draw = draw13_window_draw;
533   window->destroy = draw13_window_destroy;
534   window->destroy_all_textures = draw13_destroy_all_textures;
535   window->clear = draw13_window_clear;
536   window->xy_to_render_target = draw13_xy_to_render_target;
537}
538
539//============================================================
540//  draw13_window_create
541//============================================================
542
543static int draw13_window_create(sdl_window_info *window, int width, int height)
544{
545   sdl_info *sdl = window->dxdata;
546
547   // allocate memory for our structures
548   sdl = malloc(sizeof(*sdl));
549   memset(sdl, 0, sizeof(*sdl));
550
551   window->dxdata = sdl;
552
553   sdl->extra_flags = (window->fullscreen ?
554         SDL_WINDOW_BORDERLESS | SDL_WINDOW_INPUT_FOCUS : SDL_WINDOW_RESIZABLE);
555
556   // create the SDL window
557   SDL_SelectVideoDisplay(window->monitor->handle);
558
559   if (window->fullscreen && video_config.switchres)
560   {
561      SDL_DisplayMode mode;
562      SDL_GetCurrentDisplayMode(&mode);
563      mode.w = width;
564      mode.h = height;
565      if (window->refresh)
566         mode.refresh_rate = window->refresh;
567      if (window->depth)
568      {
569         switch (window->depth)
570         {
571         case 15:
572            mode.format = SDL_PIXELFORMAT_RGB555;
573            break;
574         case 16:
575            mode.format = SDL_PIXELFORMAT_RGB565;
576            break;
577         case 24:
578            mode.format = SDL_PIXELFORMAT_RGB24;
579            break;
580         case 32:
581            mode.format = SDL_PIXELFORMAT_RGB888;
582            break;
583         default:
584            mame_printf_warning("Ignoring depth %d\n", window->depth);
585         }
586      }
587      SDL_SetFullscreenDisplayMode(&mode);   // Try to set mode
588   }
589   else
590      SDL_SetFullscreenDisplayMode(NULL);   // Use desktop
591   
592   window->window_id = SDL_CreateWindow(window->title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
593         width, height, sdl->extra_flags);
594
595   SDL_ShowWindow(window->window_id);
596   SDL_SetWindowFullscreen(window->window_id, window->fullscreen);
597   SDL_RaiseWindow(window->window_id);
598   SDL_GetWindowSize(window->window_id, &window->width, &window->height);
599   
600   // create renderer
601   
602   if (video_config.waitvsync)   
603      SDL_CreateRenderer(window->window_id, -1, SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC);
604   else
605      SDL_CreateRenderer(window->window_id, -1, SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTDISCARD);
606     
607    SDL_SelectRenderer(window->window_id);
608   
609   sdl->blittimer = 3;
610
611   // in case any textures try to come up before these are validated,
612   // OpenGL guarantees all implementations can handle something this size.
613   sdl->texture_max_width = 64;
614   sdl->texture_max_height = 64;
615
616   return 0;
617}
618
619//============================================================
620//  draw13_window_resize
621//============================================================
622
623static void draw13_window_resize(sdl_window_info *window, int width, int height)
624{
625   sdl_info *sdl = window->dxdata;
626
627   SDL_SetWindowSize(window->window_id, width, height);
628   SDL_GetWindowSize(window->window_id, &window->width, &window->height);
629   sdl->blittimer = 3;
630   
631}
632
633//============================================================
634//  drawsdl_xy_to_render_target
635//============================================================
636
637static int draw13_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt)
638{
639   sdl_info *sdl = window->dxdata;
640
641   *xt = x - sdl->last_hofs;
642   *yt = y - sdl->last_vofs;
643   if (*xt<0 || *xt >= window->blitwidth)
644      return 0;
645   if (*yt<0 || *yt >= window->blitheight)
646      return 0;
647   return 1;
648}
649
650//============================================================
651//  draw13_window_get_primitives
652//============================================================
653
654static const render_primitive_list *draw13_window_get_primitives(sdl_window_info *window)
655{
656   if ((!window->fullscreen) || (video_config.switchres))
657   {
658      sdlwindow_blit_surface_size(window, window->width, window->height);
659   }
660   else
661   {
662      sdlwindow_blit_surface_size(window, window->monitor->center_width, window->monitor->center_height);
663   }
664   render_target_set_bounds(window->target, window->blitwidth, window->blitheight, sdlvideo_monitor_get_aspect(window->monitor));
665   return render_target_get_primitives(window->target);
666}
667
668//============================================================
669//  draw13_window_draw
670//============================================================
671
672static int draw13_window_draw(sdl_window_info *window, UINT32 dc, int update)
673{
674   sdl_info *sdl = window->dxdata;
675   render_primitive *prim;
676   texture_info *texture=NULL;
677   float vofs, hofs;
678   int blit_pixels = 0;
679
680   if (video_config.novideo)
681   {
682      return 0;
683   }
684
685   SDL_SelectRenderer(window->window_id);
686
687   if (sdl->blittimer > 0)
688   {
689      /* SDL Underlays need alpha = 0 ! */
690      SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
691      SDL_SetRenderDrawColor(0,0,0,0 /*255*/);
692      SDL_RenderFill(NULL);
693      sdl->blittimer--;
694   }
695
696   // compute centering parameters
697   vofs = hofs = 0.0f;
698
699   if (video_config.centerv || video_config.centerh)
700   {
701      int ch, cw;
702
703      if ((window->fullscreen) && (!video_config.switchres))
704      {
705         ch = window->monitor->center_height;
706         cw = window->monitor->center_width;
707      }
708      else
709      {
710         ch = window->height;
711         cw = window->width;
712      }
713
714      if (video_config.centerv)
715      {
716         vofs = (ch - window->blitheight) / 2.0f;
717      }
718      if (video_config.centerh)
719      {
720         hofs = (cw - window->blitwidth) / 2.0f;
721      }
722   }
723
724   sdl->last_hofs = hofs;
725   sdl->last_vofs = vofs;
726   
727   osd_lock_acquire(window->primlist->lock);
728
729   // now draw
730   for (prim = window->primlist->head; prim != NULL; prim = prim->next)
731   {
732      Uint8 sr, sg, sb, sa;
733
734      switch (prim->type)
735      {
736         case RENDER_PRIMITIVE_LINE:
737            sr = (int)(255.0f * prim->color.r);
738            sg = (int)(255.0f * prim->color.g);
739            sb = (int)(255.0f * prim->color.b);
740            sa = (int)(255.0f * prim->color.a);
741
742            SDL_SetRenderDrawBlendMode(map_blendmode(PRIMFLAG_GET_BLENDMODE(prim->flags)));
743            SDL_SetRenderDrawColor(sr, sg, sb, sa);
744            SDL_RenderLine(prim->bounds.x0 + hofs, prim->bounds.y0 + vofs,
745                  prim->bounds.x1 + hofs, prim->bounds.y1 + vofs);
746            break;
747         case RENDER_PRIMITIVE_QUAD:
748            texture = texture_update(window, prim);
749            if (texture)
750               blit_pixels += (texture->rawheight * texture->rawwidth);
751            render_quad(sdl, texture, prim,
752                  round_nearest(hofs + prim->bounds.x0),
753                  round_nearest(vofs + prim->bounds.y0));
754            break;
755      }
756   }
757
758   osd_lock_release(window->primlist->lock);
759
760   sdl->last_blit_pixels = blit_pixels;
761   sdl->last_blit_time = -osd_ticks();
762   SDL_RenderPresent();
763   sdl->last_blit_time += osd_ticks();
764
765   return 0;
766}
767
768
769//============================================================
770//  draw13_window_clear
771//============================================================
772
773static void draw13_window_clear(sdl_window_info *window)
774{
775   sdl_info *sdl = window->dxdata;
776
777   sdl->blittimer = 2;
778}
779
780
781//============================================================
782//  draw13_window_destroy
783//============================================================
784
785static void draw13_window_destroy(sdl_window_info *window)
786{
787   sdl_info *sdl = window->dxdata;
788
789   // skip if nothing
790   if (sdl == NULL)
791      return;
792
793   // free the memory in the window
794
795   draw13_destroy_all_textures(window);
796
797   SDL_DestroyWindow(window->window_id);
798
799   free(sdl);
800   window->dxdata = NULL;
801}
802
803//============================================================
804//  texture handling
805//============================================================
806
807//============================================================
808//  texture_compute_size and type
809//============================================================
810
811static copy_info *texture_compute_size_type(const render_texinfo *texsource, texture_info *texture, UINT32 flags)
812{
813   copy_info *bi;
814   copy_info *result = NULL;
815   int maxperf = 0;
816   //int bm = PRIMFLAG_GET_BLENDMODE(flags);
817   
818   for (bi = blit_info[texture->format]; bi != NULL; bi = bi->next)
819   {
820      if ((texture->is_rotated == bi->rotate)
821            && (texture->sdl_blendmode == bi->bm_mask))
822      {
823         if (RendererSupportsFormat(bi->dst_fmt, texture->sdl_access))
824         {
825            if (bi->perf == 0)
826               return bi;
827            else if (bi->perf > (maxperf * 102) / 100)
828            {
829               result = bi;
830               maxperf = bi->perf;
831            }
832         }
833      }
834   }
835   if (result)
836      return result;
837   /* try last resort handlers */
838   for (bi = blit_info[texture->format]; bi != NULL; bi = bi->next)
839   {
840      if ((texture->is_rotated == bi->rotate)
841         && (texture->sdl_blendmode == bi->bm_mask))
842         if (RendererSupportsFormat(bi->dst_fmt, texture->sdl_access))
843            return bi;
844   }
845   //FIXME: crash implement a -do nothing handler */
846   return NULL;
847}
848
849//============================================================
850//  texture_create
851//============================================================
852
853static texture_info *texture_create(sdl_window_info *window, const render_texinfo *texsource, quad_setup_data *setup, UINT32 flags)
854{
855   sdl_info *sdl = window->dxdata;
856   texture_info *texture;
857   
858   // allocate a new texture
859   texture = malloc(sizeof(*texture));
860   memset(texture, 0, sizeof(*texture));
861
862   // fill in the core data
863   texture->hash = texture_compute_hash(texsource, flags);
864   texture->flags = flags;
865   texture->texinfo = *texsource;
866   texture->texinfo.seqid = -1; // force set data
867   texture->is_rotated = FALSE;
868   texture->setup = *setup;
869   texture->sdl_blendmode = map_blendmode(PRIMFLAG_GET_BLENDMODE(flags));
870   
871   switch (PRIMFLAG_GET_TEXFORMAT(flags))
872   {
873      case TEXFORMAT_ARGB32:
874         texture->format = SDL_TEXFORMAT_ARGB32;
875         break;
876      case TEXFORMAT_RGB32:
877            texture->format = texsource->palette ? SDL_TEXFORMAT_RGB32_PALETTED : SDL_TEXFORMAT_RGB32;
878         break;
879      case TEXFORMAT_PALETTE16:
880         texture->format = SDL_TEXFORMAT_PALETTE16;
881         break;
882      case TEXFORMAT_RGB15:
883         texture->format = texsource->palette ? SDL_TEXFORMAT_RGB15_PALETTED : SDL_TEXFORMAT_RGB15;
884         break;
885      case TEXFORMAT_PALETTEA16:
886         texture->format = SDL_TEXFORMAT_PALETTE16A;
887         break;
888      case TEXFORMAT_YUY16:
889         texture->format = texsource->palette ? SDL_TEXFORMAT_YUY16_PALETTED : SDL_TEXFORMAT_YUY16;
890         break;
891
892      default:
893         mame_printf_error("Unknown textureformat %d\n", PRIMFLAG_GET_TEXFORMAT(flags));
894   }
895
896   texture->rawwidth = texsource->width;
897   texture->rawheight = texsource->height;
898   if (setup->rotwidth != texture->rawwidth || setup->rotheight != texture->rawheight
899         || setup->dudx < 0 )
900      texture->is_rotated = TRUE;
901   else
902      texture->is_rotated = FALSE;
903   
904   //texture->sdl_access = SDL_TEXTUREACCESS_STATIC;
905   texture->sdl_access = SDL_TEXTUREACCESS_STREAMING;
906   
907   // compute the size
908   texture->copyinfo = texture_compute_size_type(texsource, texture, flags);
909
910   texture->texture_id = SDL_CreateTexture(texture->copyinfo->dst_fmt, texture->sdl_access,
911         texture->setup.rotwidth, texture->setup.rotheight);
912   
913    if ( (texture->copyinfo->func != NULL) && (texture->sdl_access == SDL_TEXTUREACCESS_STATIC))
914    {
915      texture->pixels = malloc(texture->setup.rotwidth * texture->setup.rotheight * texture->copyinfo->dst_bpp);
916        texture->pixels_own=TRUE;
917    }
918   /* add us to the texture list */
919   texture->next = sdl->texlist;
920   sdl->texlist = texture;
921
922   texture->last_access = osd_ticks();
923
924   return texture;
925}
926
927//============================================================
928//  texture_set_data
929//============================================================
930
931static void texture_set_data(sdl_info *sdl, texture_info *texture, const render_texinfo *texsource, UINT32 flags)
932{
933   texture->copyinfo->time -= osd_ticks();
934   if (texture->sdl_access == SDL_TEXTUREACCESS_STATIC)
935   {
936      if ( texture->copyinfo->func )
937       {
938         texture->pitch = texture->setup.rotwidth * texture->copyinfo->dst_bpp;
939         texture->copyinfo->func(texture, texsource);
940       }
941      else
942      {
943         texture->pixels = texsource->base;
944         texture->pitch = texture->texinfo.rowpixels * texture->copyinfo->dst_bpp;
945      }
946      SDL_UpdateTexture(texture->texture_id, NULL, texture->pixels, texture->pitch);
947   }
948   else
949   {
950      SDL_LockTexture(texture->texture_id, NULL, 1, (void **) &texture->pixels, &texture->pitch);
951      if ( texture->copyinfo->func )
952         texture->copyinfo->func(texture, texsource);
953      else
954      {
955         UINT8 *src = texsource->base;
956         UINT8 *dst = (UINT8 *) texture->pixels;
957         int spitch = texsource->rowpixels * texture->copyinfo->dst_bpp;
958         int num = texsource->width * texture->copyinfo->dst_bpp;
959         int h = texsource->height;
960         while (h--) {
961            memcpy(dst, src, num);
962            src += spitch;
963            dst += texture->pitch;
964         }
965      }
966      SDL_UnlockTexture(texture->texture_id);
967   }
968   texture->copyinfo->time += osd_ticks();
969}
970
971//============================================================
972//  compute rotation setup
973//============================================================
974
975static void compute_setup(const render_quad_texuv *texcoords, int texwidth, int texheight, quad_setup_data *setup, int flags)
976{
977   float fdudx, fdvdx, fdudy, fdvdy;
978   float x0, y0, x1, y1;
979   float width, height;
980   float fscale;
981   float prec;
982   
983   /* determine U/V deltas */
984   if ((PRIMFLAG_GET_SCREENTEX(flags)))
985      fscale = (float) video_config.prescale;
986   else
987      fscale = 1.0f;
988
989   x0 = MIN4(texcoords->tl.u, texcoords->tr.u, texcoords->br.u, texcoords->bl.u);
990   x1 = MAX4(texcoords->tl.u, texcoords->tr.u, texcoords->br.u, texcoords->bl.u);
991   y0 = MIN4(texcoords->tl.v, texcoords->tr.v, texcoords->br.v, texcoords->bl.v);
992   y1 = MAX4(texcoords->tl.v, texcoords->tr.v, texcoords->br.v, texcoords->bl.v);
993
994   
995   fdudx = (texcoords->tr.u - texcoords->tl.u) / (x1 - x0) / fscale;
996   fdvdx = (texcoords->tr.v - texcoords->tl.v) / (x1 - x0) / fscale;
997   fdudy = (texcoords->bl.u - texcoords->tl.u) / (y1 - y0) / fscale;
998   fdvdy = (texcoords->bl.v - texcoords->tl.v) / (y1 - y0) / fscale;
999
1000   /* for some reasons the core introduces distortions in the order
1001    * of 0.001 when scaling to fullscreen.
1002    * Get rid of them */
1003   prec = 100.0f * fscale;
1004   fdudx = round_nearest(fdudx * prec) / prec;
1005   fdudy = round_nearest(fdudy * prec) / prec;
1006   fdvdx = round_nearest(fdvdx * prec) / prec;
1007   fdvdy = round_nearest(fdvdy * prec) / prec;
1008   
1009   width = round_nearest(abs(fdudx * texwidth + fdvdx * texheight) * fscale * fscale);
1010   height = round_nearest(abs(fdudy * texwidth + fdvdy * texheight) * fscale * fscale);
1011
1012   /* clamp to integers */
1013   setup->rotwidth = width;
1014   setup->rotheight = height;
1015
1016   /* compute start and delta U,V coordinates now */
1017   setup->dudx = round_nearest(65536.0f * fdudx);
1018   setup->dvdx = round_nearest(65536.0f * fdvdx);
1019   setup->dudy = round_nearest(65536.0f * fdudy);
1020   setup->dvdy = round_nearest(65536.0f * fdvdy);
1021   setup->startu = round_nearest(65536.0f * texwidth * texcoords->tl.u);
1022   setup->startv = round_nearest(65536.0f * texheight * texcoords->tl.v);
1023
1024   //printf("%d %d %d %d \n", setup->dudx, setup->dvdx, setup->dudy, setup->dvdy);
1025   /* advance U/V to the middle of the first texel
1026    * the division by 2.1 is a workaround to prevent
1027    * crashes in certain cases */
1028   setup->startu += (float) (setup->dudx + setup->dudy) / 2.1f;
1029   setup->startv += (float) (setup->dvdx + setup->dvdy) / 2.1f;
1030}
1031
1032//============================================================
1033//  texture_find
1034//============================================================
1035
1036static texture_info *texture_find(sdl_info *sdl, const render_primitive *prim, quad_setup_data *setup)
1037{
1038   HashT texhash = texture_compute_hash(&prim->texture, prim->flags);
1039   texture_info *texture;
1040   osd_ticks_t now = osd_ticks();
1041
1042   // find a match
1043   for (texture = sdl->texlist; texture != NULL; )
1044      if (texture->hash == texhash &&
1045         texture->texinfo.base == prim->texture.base &&
1046         texture->texinfo.width == prim->texture.width &&
1047         texture->texinfo.height == prim->texture.height &&
1048         texture->texinfo.rowpixels == prim->texture.rowpixels &&
1049         texture->setup.dudx == setup->dudx &&
1050         texture->setup.dvdx == setup->dvdx &&
1051         texture->setup.dudy == setup->dudy &&
1052         texture->setup.dvdy == setup->dvdy &&
1053         ((texture->flags ^ prim->flags) & (PRIMFLAG_BLENDMODE_MASK | PRIMFLAG_TEXFORMAT_MASK)) == 0)
1054      {
1055         /* would we choose another blitter ? */
1056         if ((texture->copyinfo->samples & 0x1f) == 0x1f)
1057         {
1058            if (texture->copyinfo != texture_compute_size_type(&texture->texinfo, texture, prim->flags))
1059               return NULL;
1060#if 0
1061            else
1062            {
1063               /* reset stats */
1064               texture->copyinfo->samples = 0;
1065               texture->copyinfo->time = 0;
1066               texture->copyinfo->pixel_count = 0;
1067            }
1068#endif
1069         }
1070         texture->last_access = now;
1071         return texture;
1072      }
1073      else
1074      {
1075         /* free resources not needed any longer? */
1076         texture_info *expire = texture;
1077         texture = texture->next;
1078         if (now - expire->last_access > osd_ticks_per_second())
1079            draw13_destroy_texture(sdl, expire);
1080      }
1081
1082   // nothing found
1083   return NULL;
1084}
1085
1086//============================================================
1087//  texture_update
1088//============================================================
1089
1090static texture_info * texture_update(sdl_window_info *window, const render_primitive *prim)
1091{
1092   sdl_info *sdl = window->dxdata;
1093   quad_setup_data setup;
1094   texture_info *texture;
1095   
1096   compute_setup(&prim->texcoords, prim->texture.width, prim->texture.height, &setup, prim->flags);
1097
1098   texture = texture_find(sdl, prim, &setup);
1099
1100   // if we didn't find one, create a new texture
1101   if (texture == NULL && prim->texture.base != NULL)
1102    {
1103        texture = texture_create(window, &prim->texture, &setup, prim->flags);
1104    }
1105
1106    if (texture != NULL)
1107   {
1108      if (prim->texture.base != NULL && texture->texinfo.seqid != prim->texture.seqid)
1109      {
1110         texture->texinfo.seqid = prim->texture.seqid;
1111         // if we found it, but with a different seqid, copy the data
1112         texture_set_data(sdl, texture, &prim->texture, prim->flags);
1113      }
1114
1115   }
1116    return texture;
1117}
1118
1119static void draw13_destroy_texture(sdl_info *sdl, texture_info *texture)
1120{
1121   texture_info *p;
1122   
1123   SDL_DestroyTexture(texture->texture_id);
1124   if ( texture->pixels_own )
1125   {
1126      free(texture->pixels);
1127      texture->pixels=NULL;
1128      texture->pixels_own=FALSE;
1129   }
1130   
1131   for (p=sdl->texlist; p != NULL; p = p->next)
1132      if (p->next == texture)
1133         break;
1134   if (p == NULL)
1135       sdl->texlist = NULL;
1136   else
1137      p->next = texture->next;
1138   free(texture);
1139}
1140
1141static void draw13_destroy_all_textures(sdl_window_info *window)
1142{
1143   sdl_info *sdl = window->dxdata;
1144   texture_info *next_texture=NULL, *texture = NULL;
1145   int lock=FALSE;
1146
1147   if (sdl == NULL)
1148      return;
1149
1150   if(window->primlist && window->primlist->lock)
1151   {
1152      lock=TRUE;
1153      osd_lock_acquire(window->primlist->lock);
1154   }
1155
1156   texture = sdl->texlist;
1157
1158   while (texture)
1159   {
1160      next_texture = texture->next;
1161      draw13_destroy_texture(sdl, texture);
1162      texture = next_texture;
1163   }
1164
1165   if (lock)
1166      osd_lock_release(window->primlist->lock);
1167}
src/osd/sdl/blit13.h
r0r4071
1
2
3
4//============================================================
5//  INLINE
6//============================================================
7
8INLINE UINT32 premult32(UINT32 pixel)
9{
10   UINT8 a = (pixel >> 24) & 0xff;
11   UINT8 r = (pixel >> 16) & 0xff;
12   UINT8 g = (pixel >> 8) & 0xff;
13   UINT8 b = (pixel >> 0) & 0xff;
14   
15   return 0xFF000000 |
16      (((UINT16)r * (UINT16)a) / 255) << 16 |
17      (((UINT16)g * (UINT16)a) / 255) << 8 |
18      (((UINT16)b * (UINT16)a) / 255);
19}
20
21#define CLUL(x) ((int) (x) < 0 ? 0 : ((x) > 65535 ? 255 : (x)>>8))
22
23INLINE UINT32 ycc_to_rgb(UINT8 y, UINT8 cb, UINT8 cr)
24{
25   unsigned int r, g, b, common;
26
27   common = 298 * y - 56992;
28   r = (common +            409 * cr);
29   g = (common - 100 * cb - 208 * cr + 91776);
30   b = (common + 516 * cb - 13696);
31
32   return 0xff000000 | (CLUL(r)<<16) | (CLUL(g)<<8) | (CLUL(b));
33}
34
35INLINE UINT32 pixel_ycc_to_rgb(UINT16 *pixel)
36{
37   UINT32 p = *(UINT32 *)((FPTR) pixel & ~1);
38   return ycc_to_rgb((*pixel >> 8) & 0xff, (p) & 0xff, (p>>16) & 0xff);
39}
40
41INLINE UINT32 pixel_ycc_to_rgb_pal(UINT16 *pixel, const rgb_t *palette)
42{
43   UINT32 p = *(UINT32 *)((FPTR) pixel & ~1);
44   return ycc_to_rgb(palette[(*pixel >> 8) & 0xff], (p) & 0xff, (p>>16) & 0xff);
45}
46
47//============================================================
48//  Pixel conversions
49//============================================================
50
51#define OP_ARGB32_ARGB32(_src) (_src)
52
53#define OP_RGB32_ARGB32(_src) ((_src) | 0xff000000)
54
55#define OP_RGB32PAL_ARGB32(_src) \
56   (texsource->palette[0x200 + RGB_RED(_src)]   | \
57    texsource->palette[0x100 + RGB_GREEN(_src)] | \
58    texsource->palette[RGB_BLUE(_src)] | 0xff000000)
59
60#define OP_PAL16_ARGB32(_src) (0xff000000 | texsource->palette[_src])
61
62#define OP_PAL16A_ARGB32(_src) (texsource->palette[_src])
63
64#define OP_RGB15_ARGB32(_src) (0xff000000 | ((_src & 0x7c00) << 9) | ((_src & 0x03e0) << 6) | ((_src & 0x001f) << 3) | \
65   ((((_src & 0x7c00) << 9) | ((_src & 0x03e0) << 6) | ((_src & 0x001f) << 3) >> 5) & 0x070707))
66
67#define OP_RGB15PAL_ARGB32(_src) (0xff000000 | texsource->palette[0x40 + ((_src >> 10) & 0x1f)] | \
68      texsource->palette[0x20 + ((_src >> 5) & 0x1f)] | texsource->palette[0x00 + ((_src >> 0) & 0x1f)])
69
70#define OP_ARGB32_RGB32(_pixel) premult32(_pixel)
71
72#define OP_PAL16A_RGB32(_src) premult32(texsource->palette[_src])
73
74#define OP_PAL16_ARGB1555(_src) ((texsource->palette[_src]&0xf80000) >> 9 | \
75       (texsource->palette[_src]&0x00f800) >> 6 | \
76       (texsource->palette[_src]&0x0000f8) >> 3 | 0x8000)
77
78#define OP_RGB15_ARGB1555(_src) ((_src) | 0x8000)
79
80#define OP_RGB15PAL_ARGB1555(_src) ((texsource->palette[(_src) >> 10] & 0xf8) << 7 | \
81       (texsource->palette[((_src) >> 5) & 0x1f] & 0xf8) << 2 | \
82       (texsource->palette[(_src) & 0x1f] & 0xf8) >> 3 | 0x8000)
83
84#define OP_YUV16_UYVY(_src) (_src)
85
86#define OP_YUV16PAL_UYVY(_src) ((texsource->palette[((_src) >> 8) & 0xff] << 8) | ((_src) & 0x00ff))
87
88#define OP_YUV16PAL_YVYU(_src) ((texsource->palette[((_src) >> 8) & 0xff] & 0xff) | ((_src & 0xff) << 8))       
89
90#define OP_YUV16_YVYU(_src) ((((_src) >> 8) & 0xff) | ((_src & 0xff) << 8))       
91
92#define OP_YUV16_YUY2(_src) ( ((_src) & 0xff00ff00) | \
93   (((_src)>>16)&0xff) | (((_src)<<16)&0xff0000) )
94
95#define OP_YUV16PAL_YUY2(_src) ( (texsource->palette[((_src)>>8) & 0xff]) | \
96      (texsource->palette[((_src)>>24) & 0xff]<<16) | \
97   (((_src)<<8)&0xff00ff00) )
98
99#define OP_YUV16_ARGB32(_src) \
100      (UINT64) ycc_to_rgb(((_src) >>  8) & 0xff, (_src) & 0xff , ((_src)>>16) & 0xff) \
101   | ((UINT64)ycc_to_rgb(((_src) >> 24) & 0xff, (_src) & 0xff , ((_src)>>16) & 0xff) << 32)
102
103#define OP_YUV16PAL_ARGB32(_src) \
104      (UINT64)ycc_to_rgb(texsource->palette[((_src) >>  8) & 0xff], (_src) & 0xff , ((_src)>>16) & 0xff) \
105   | ((UINT64)ycc_to_rgb(texsource->palette[((_src) >> 24) & 0xff], (_src) & 0xff , ((_src)>>16) & 0xff) << 32)
106
107#define OP_YUV16_ARGB32ROT(_src) pixel_ycc_to_rgb(&(_src))
108
109#define OP_YUV16PAL_ARGB32ROT(_src) pixel_ycc_to_rgb_pal(&(_src), texsource->palette)
110
111//============================================================
112//  Copy and rotation
113//============================================================
114
115#define TEXCOPY_M( _name, _src_type, _dest_type,  _op, _len_div) \
116INLINE void texcopy_##_name (texture_info *texture, const render_texinfo *texsource) { \
117   int x, y; \
118   /* loop over Y */ \
119   for (y = 0; y < texsource->height; y++) { \
120      _src_type *src = (_src_type *)texsource->base + y * texsource->rowpixels / (_len_div); \
121      _dest_type *dst = (_dest_type *)((UINT8 *)texture->pixels + y * texture->pitch); \
122      for (x = 0; x < texsource->width / (_len_div); x++) { \
123         *dst++ = _op(*src); \
124         src++; \
125      } \
126   } \
127}
128
129#define TEXCOPY( _name, _src_type, _dest_type,  _op) \
130   TEXCOPY_M( _name, _src_type, _dest_type,  _op, 1)
131
132#define TEXROT( _name, _src_type, _dest_type, _op) \
133INLINE void texcopy_rot_##_name (texture_info *texture, const render_texinfo *texsource) { \
134   int x, y; \
135   quad_setup_data *setup = &texture->setup; \
136   int dudx = setup->dudx; \
137   int dvdx = setup->dvdx; \
138   /* loop over Y */ \
139   for (y = 0; y < setup->rotheight; y++) { \
140      INT32 curu = setup->startu + y * setup->dudy; \
141      INT32 curv = setup->startv + y * setup->dvdy; \
142      _dest_type *dst = (_dest_type *)((UINT8 *)texture->pixels + y * texture->pitch); \
143      for (x = 0; x < setup->rotwidth; x++) { \
144         _src_type *src = (_src_type *) texsource->base + (curv >> 16) * texsource->rowpixels + (curu >> 16); \
145         *dst++ = _op(*src); \
146         curu += dudx; \
147         curv += dvdx; \
148      } \
149   } \
150}
151
152//TEXCOPY(argb32_argb32, UINT32, UINT32, OP_ARGB32_ARGB32)
153
154TEXCOPY(rgb32_argb32,  UINT32, UINT32, OP_RGB32_ARGB32)
155TEXCOPY(rgb32pal_argb32,  UINT32, UINT32, OP_RGB32PAL_ARGB32)
156TEXCOPY(pal16_argb32,  UINT16, UINT32, OP_PAL16_ARGB32)
157TEXCOPY(pal16a_argb32,  UINT16, UINT32, OP_PAL16A_ARGB32)
158TEXCOPY(rgb15_argb32,  UINT16, UINT32, OP_RGB15_ARGB32)
159TEXCOPY(rgb15pal_argb32,  UINT16, UINT32, OP_RGB15PAL_ARGB32)
160
161TEXCOPY(pal16_argb1555,  UINT16, UINT16, OP_PAL16_ARGB1555)
162TEXCOPY(rgb15_argb1555,  UINT16, UINT16, OP_RGB15_ARGB1555)
163TEXCOPY(rgb15pal_argb1555,  UINT16, UINT16, OP_RGB15PAL_ARGB1555)
164
165TEXCOPY(argb32_rgb32, UINT32, UINT32, OP_ARGB32_RGB32)
166TEXCOPY(pal16a_rgb32,  UINT16, UINT32, OP_PAL16A_RGB32)
167
168TEXCOPY_M(yuv16_argb32, UINT32, UINT64, OP_YUV16_ARGB32, 2)
169TEXCOPY_M(yuv16pal_argb32, UINT32, UINT64, OP_YUV16PAL_ARGB32, 2)
170
171//TEXCOPY(yuv16_uyvy, UINT16, UINT16, OP_YUV16_UYVY)
172
173TEXCOPY(yuv16pal_uyvy, UINT16, UINT16, OP_YUV16PAL_UYVY)
174
175TEXCOPY(yuv16_yvyu, UINT16, UINT16, OP_YUV16_YVYU)
176TEXCOPY(yuv16pal_yvyu, UINT16, UINT16, OP_YUV16PAL_YVYU)
177
178TEXCOPY_M(yuv16_yuy2, UINT32, UINT32, OP_YUV16_YUY2, 2)
179TEXCOPY_M(yuv16pal_yuy2, UINT32, UINT32, OP_YUV16PAL_YUY2, 2)
180
181
182TEXROT(argb32_argb32, UINT32, UINT32, OP_ARGB32_ARGB32)
183TEXROT(rgb32_argb32,  UINT32, UINT32, OP_RGB32_ARGB32)
184TEXROT(rgb32pal_argb32,  UINT32, UINT32, OP_RGB32PAL_ARGB32)
185TEXROT(pal16_argb32,  UINT16, UINT32, OP_PAL16_ARGB32)
186TEXROT(pal16a_argb32,  UINT16, UINT32, OP_PAL16A_ARGB32)
187TEXROT(rgb15_argb32,  UINT16, UINT32, OP_RGB15_ARGB32)
188TEXROT(rgb15pal_argb32,  UINT16, UINT32, OP_RGB15PAL_ARGB32)
189
190TEXROT(pal16_argb1555,  UINT16, UINT16, OP_PAL16_ARGB1555)
191TEXROT(rgb15_argb1555,  UINT16, UINT16, OP_RGB15_ARGB1555)
192TEXROT(rgb15pal_argb1555,  UINT16, UINT16, OP_RGB15PAL_ARGB1555)
193
194TEXROT(argb32_rgb32, UINT32, UINT32, OP_ARGB32_RGB32)
195TEXROT(pal16a_rgb32,  UINT16, UINT32, OP_PAL16A_RGB32)
196
197TEXROT(yuv16_argb32, UINT16, UINT32, OP_YUV16_ARGB32ROT)
198TEXROT(yuv16pal_argb32, UINT16, UINT32, OP_YUV16PAL_ARGB32ROT)
src/osd/sdl/debugwin.c
r4070r4071
99//
1010//============================================================
1111
12#if !defined(SDLMAME_WIN32) && !defined(SDLMAME_NO_X11) && !defined(SDLMAME_SOLARIS) && !defined(SDLMAME_OS2)
12#if !defined(NO_DEBUGGER)
1313
1414#include <gdk/gdkkeysyms.h>
1515#include <gtk/gtk.h>
src/osd/sdl/video.c
r4070r4071
175175
176176
177177//============================================================
178//  get_modes - code take from SDL_Compat
179//============================================================
180
181#if (SDL_VERSION_ATLEAST(1,3,0))
182static SDL_Rect **get_current_display_modes(void)
183{
184   /* Get modes for current select display */
185    int i, nmodes;
186    SDL_Rect **modes;
187
188    /* Memory leak, but this is a compatibility function, who cares? */
189    nmodes = 0;
190    modes = NULL;
191    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
192        SDL_DisplayMode mode;
193        SDL_GetDisplayMode(i, &mode);
194        if (!mode.w || !mode.h) {
195            return (SDL_Rect **) (NULL);
196        }
197        // FIXME: refresh rates
198        if (nmodes > 0 && modes[nmodes - 1]->w == mode.w
199            && modes[nmodes - 1]->h == mode.h) {
200            continue;
201        }
202
203        modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
204        if (!modes) {
205            return NULL;
206        }
207        modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
208        if (!modes[nmodes]) {
209            return NULL;
210        }
211        modes[nmodes]->x = 0;
212        modes[nmodes]->y = 0;
213        modes[nmodes]->w = mode.w;
214        modes[nmodes]->h = mode.h;
215        mame_printf_verbose("Display mode #%d - %d x %d\n", nmodes, mode.w, mode.h);
216        ++nmodes;
217    }
218    if (modes) {
219        modes[nmodes] = NULL;
220    }
221    return modes;
222}
223#endif
224
225
226//============================================================
227178//  sdlvideo_monitor_refresh
228179//============================================================
229180
r4070r4071
448399//  monitor_enum_callback
449400//============================================================
450401
451#ifdef SDLMAME_WIN32
402#if defined(SDLMAME_WIN32) && !(SDL_VERSION_ATLEAST(1,3,0))
452403static BOOL CALLBACK monitor_enum_callback(HMONITOR handle, HDC dc, LPRECT rect, LPARAM data)
453404{
454405   sdl_monitor_info ***tailptr = (sdl_monitor_info ***)data;
r4070r4071
466417   memset(monitor, 0, sizeof(*monitor));
467418
468419   // copy in the data
469        monitor->handle = (UINT32)handle;
420    monitor->handle = (UINT32)handle;
470421   monitor->monitor_width = info.rcMonitor.right - info.rcMonitor.left;
471422   monitor->monitor_height = info.rcMonitor.bottom - info.rcMonitor.top;
472423   strcpy(monitor->monitor_device, info.szDevice);
r4070r4071
505456   sdl_monitor_list = NULL;
506457   tailptr = &sdl_monitor_list;
507458
508   #ifdef SDLMAME_WIN32
509   EnumDisplayMonitors(NULL, NULL, monitor_enum_callback, (LPARAM)&tailptr);
510   #elif (SDL_VERSION_ATLEAST(1,3,0))
459   #if (SDL_VERSION_ATLEAST(1,3,0))
511460   {
512461      int i, temp;
513462
r4070r4071
535484           monitor->aspect = (float)(dmode.w) / (float)(dmode.h);
536485           mame_printf_verbose("Adding monitor %s (%d x %d)\n", monitor->monitor_device, dmode.w, dmode.h);
537486
538           monitor->modes = get_current_display_modes();
539           
540487           // save the primary monitor handle
541488           if (i == 0)
542489              primary_monitor = monitor;
r4070r4071
547494      }
548495      SDL_SelectVideoDisplay(temp);
549496   }
497   #elif defined(SDLMAME_WIN32)
498   EnumDisplayMonitors(NULL, NULL, monitor_enum_callback, (LPARAM)&tailptr);
550499   #else
551500   add_primary_monitor((void *)&tailptr);
552501   sdl_monitor_list->modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_DOUBLEBUF);
r4070r4071
649598      ui_popup_time(1, "Keepaspect %s", video_config.keepaspect? "enabled":"disabled");
650599   }
651600   
652   if (USE_OPENGL)
601   if (USE_OPENGL || SDL_VERSION_ATLEAST(1,3,0))
653602   {
654603      //FIXME: on a per window basis
655604      if (ui_input_pressed(machine, IPT_OSD_5))
r4070r4071
731680       video_config.mode = VIDEO_MODE_OPENGL;
732681       video_config.prefer16bpp_tex = 1;
733682   }
683   else if (SDL_VERSION_ATLEAST(1,3,0) && (strcmp(stemp, SDLOPTVAL_SDL13) == 0))
684   {
685       video_config.mode = VIDEO_MODE_SDL13;
686       video_config.prefer16bpp_tex = 1;
687   }
734688   else
735689   {
736690      mame_printf_warning("Invalid video value %s; reverting to software\n", stemp);
r4070r4071
740694   video_config.switchres     = options_get_bool(mame_options(), SDLOPTION_SWITCHRES);
741695   video_config.centerh       = options_get_bool(mame_options(), SDLOPTION_CENTERH);
742696   video_config.centerv       = options_get_bool(mame_options(), SDLOPTION_CENTERV);
697   video_config.waitvsync     = options_get_bool(mame_options(), SDLOPTION_WAITVSYNC);
743698
699   if (USE_OPENGL || SDL_VERSION_ATLEAST(1,3,0))
700   {
701      video_config.filter        = options_get_bool(mame_options(), SDLOPTION_FILTER);
702   }
703
744704   if (USE_OPENGL)
745705   {
746706      video_config.prescale      = options_get_int(mame_options(), SDLOPTION_PRESCALE);
r4070r4071
751711      }
752712      // default to working video please
753713      video_config.prefer16bpp_tex = 0;
754      video_config.filter        = options_get_bool(mame_options(), SDLOPTION_FILTER);
755714      video_config.forcepow2texture = options_get_bool(mame_options(), SDLOPTION_GL_FORCEPOW2TEXTURE)==1;
756715      video_config.allowtexturerect = options_get_bool(mame_options(), SDLOPTION_GL_NOTEXTURERECT)==0;
757716      video_config.vbo         = options_get_bool(mame_options(), SDLOPTION_GL_VBO);
src/osd/sdl/window.c
r4070r4071
124124static OSDWORK_CALLBACK( sdlwindow_video_window_destroy_wt );
125125static OSDWORK_CALLBACK( sdlwindow_resize_wt );
126126static OSDWORK_CALLBACK( sdlwindow_toggle_full_screen_wt );
127static void sdlwindow_clear_surface(sdl_window_info *window);
128127static void sdlwindow_update_cursor_state(running_machine *machine, sdl_window_info *window);
129128static void sdlwindow_sync(void);
130129
r4070r4071
192191
193192   if (SDLMAME_INIT_IN_WORKER_THREAD)
194193   {
195      if (SDL_Init(SDL_INIT_TIMER|SDL_INIT_AUDIO| SDL_INIT_VIDEO| SDL_INIT_JOYSTICK|SDL_INIT_NOPARACHUTE))
194#if (SDL_VERSION_ATLEAST(1,3,0))
195      if (SDL_InitSubSystem(SDL_INIT_TIMER|SDL_INIT_AUDIO| SDL_INIT_VIDEO| SDL_INIT_JOYSTICK|SDL_INIT_NOPARACHUTE))
196#else
197      if (SDL_Init(SDL_INIT_TIMER|SDL_INIT_AUDIO| SDL_INIT_VIDEO| SDL_INIT_JOYSTICK|SDL_INIT_NOPARACHUTE))
198#endif
196199      {
197200         mame_printf_error("Could not initialize SDL: %s.\n", SDL_GetError());
198201         exit(-1);
r4070r4071
246249         video_config.mode = VIDEO_MODE_SOFT;
247250   }
248251#endif
252#if   SDL_VERSION_ATLEAST(1,3,0)
253   if (video_config.mode == VIDEO_MODE_SDL13)
254   {
255      if (draw13_init(&draw))
256         video_config.mode = VIDEO_MODE_SOFT;
257   }
258#endif
249259   if (video_config.mode == VIDEO_MODE_SOFT)
250260   {
251261      if (drawsdl_init(&draw))
r4070r4071
407417      newwidth = window_width;
408418
409419   if ((window->blitwidth != newwidth) || (window->blitheight != newheight))
410      sdlwindow_clear_surface(window);
420      sdlwindow_clear(window);
411421   
412422   window->blitwidth = newwidth;
413423   window->blitheight = newheight;
r4070r4071
431441
432442   sdlwindow_blit_surface_size(window, wp->resize_new_width, wp->resize_new_height);
433443   
434   sdlwindow_clear_surface(window);
444   sdlwindow_clear(window);
435445
436446   free(wp);
437447   return NULL;
r4070r4071
472482   return NULL;
473483}
474484
475static void sdlwindow_clear_surface(sdl_window_info *window)
485void sdlwindow_clear(sdl_window_info *window)
476486{
477487   worker_param *wp = malloc(sizeof(worker_param));
478488
r4070r4071
904914//  pick_best_mode
905915//============================================================
906916
917#if SDL_VERSION_ATLEAST(1,3,0)
907918static void pick_best_mode(sdl_window_info *window, int *fswidth, int *fsheight)
908919{
909920   int minimum_width, minimum_height, target_width, target_height;
910921   int i;
922   int num;
911923   float size_score, best_score = 0.0f;
924
925   // determine the minimum width/height for the selected target
926   render_target_get_minimum_size(window->target, &minimum_width, &minimum_height);
927
928   // use those as the target for now
929   target_width = minimum_width * MAX(1, window->prescale);
930   target_height = minimum_height * MAX(1, window->prescale);
931
932   // if we're not stretching, allow some slop on the minimum since we can handle it
933   {
934      minimum_width -= 4;
935      minimum_height -= 4;
936   }
937
938   num = SDL_GetNumDisplayModes();
939   
940   if (num == 0)
941   {
942      mame_printf_error("SDL: No modes available?!\n");
943      exit(-1);
944   }
945   else
946   {
947      for (i = 0; i < num; ++i)
948      {
949         SDL_DisplayMode mode;
950         SDL_GetDisplayMode(i, &mode);
951
952         // compute initial score based on difference between target and current
953         size_score = 1.0f / (1.0f + fabsf((INT32)mode.w - target_width) + fabsf((INT32)mode.h - target_height));
954
955         // if the mode is too small, give a big penalty
956         if (mode.w < minimum_width || mode.h < minimum_height)
957            size_score *= 0.01f;
958
959         // if mode is smaller than we'd like, it only scores up to 0.1
960         if (mode.w < target_width || mode.h < target_height)
961            size_score *= 0.1f;
962
963         // if we're looking for a particular mode, that's a winner
964         if (mode.w == window->maxwidth && mode.h == window->maxheight)
965            size_score = 2.0f;
966
967         // refresh adds some points
968         if (window->refresh)
969            size_score *= 1.0f / (1.0f + fabsf(window->refresh - mode.refresh_rate) / 10.0f);
970
971         mame_printf_verbose("%4dx%4d@%2d -> %f\n", (int)mode.w, (int)mode.h, (int) mode.refresh_rate, size_score);
972
973         // best so far?
974         if (size_score > best_score)
975         {
976            best_score = size_score;
977            *fswidth = mode.w;
978            *fsheight = mode.h;
979         }
980
981      }
982   }
983}
984#else
985static void pick_best_mode(sdl_window_info *window, int *fswidth, int *fsheight)
986{
987   int minimum_width, minimum_height, target_width, target_height;
988   int i;
989   float size_score, best_score = 0.0f;
912990   SDL_Rect **modes;
913991
914992   // determine the minimum width/height for the selected target
r4070r4071
9241002      minimum_height -= 4;
9251003   }
9261004
927#if defined(SDLMAME_WIN32) && !SDL_VERSION_ATLEAST(1,3,0)
1005#if defined(SDLMAME_WIN32)
9281006   /*
9291007    *  We need to do this here. If SDL_ListModes is
9301008    * called in init_monitors, the call will crash
r4070r4071
9771055      }
9781056   }
9791057}
1058#endif
9801059
981
9821060//============================================================
9831061//  sdlwindow_video_window_update
9841062//  (main thread)
r4070r4071
13911469   // constrain to fit
13921470   if (constrain)
13931471      constrain_to_aspect_ratio(window, &maxwidth, &maxheight, WMSZ_BOTTOMRIGHT);
1394   else
1472   //else
13951473   {
13961474      maxwidth -= WINDOW_DECORATION_WIDTH;
13971475      maxheight -= WINDOW_DECORATION_HEIGHT;
src/osd/sdl/video.h
r4070r4071
2020
2121#define MAX_VIDEO_WINDOWS         (4)
2222
23#define VIDEO_MODE_SOFT            (0)
24#define VIDEO_MODE_OPENGL         (1)
23enum {
24   VIDEO_MODE_SOFT   = 0,
25   VIDEO_MODE_OPENGL,
26   VIDEO_MODE_SDL13
27};
2528
2629#define VIDEO_SCALE_MODE_NONE      (0)
2730
r4070r4071
3639#define SDL_TEXFORMAT_PALETTE16         (5)
3740#define SDL_TEXFORMAT_RGB15            (6)
3841#define SDL_TEXFORMAT_RGB15_PALETTED   (7)
39#define SDL_TEXFORMAT_PALETTE16         (5)
40#define SDL_TEXFORMAT_RGB15            (6)
41#define SDL_TEXFORMAT_RGB15_PALETTED   (7)
4242#define SDL_TEXFORMAT_PALETTE16A      (8)
4343// special texture formats for 16bpp texture destination support, do not use
4444// to address the tex properties / tex functions arrays!
r4070r4071
6161   int               monitor_height;
6262   char            monitor_device[64];
6363   float            aspect;               // computed/configured aspect ratio of the physical device
64   int               reqwidth;            // requested width for this monitor
65   int               reqheight;            // requested height for this monitor
6664   int               center_width;         // width of first physical screen for centering
6765   int               center_height;         // height of first physical screen for centering
66#if !SDL_VERSION_ATLEAST(1,3,0)
6867   //FIXME: This should not be a SDL-type
6968   SDL_Rect **modes;                     // supported modes
69#endif
7070};
7171
7272
r4070r4071
9494   int               prescale;            // prescale factor (not currently supported)
9595   int               keepaspect;             // keep aspect ratio?
9696   int               numscreens;             // number of screens
97   int               layerconfig;            // default configuration of layers
9897   int               centerh;
9998   int               centerv;
10099
r4070r4071
153152
154153void sdlvideo_monitor_refresh(sdl_monitor_info *monitor);
155154float sdlvideo_monitor_get_aspect(sdl_monitor_info *monitor);
156sdl_monitor_info *sdlvideo_monitor_from_handle(UINT32 monitor);
155sdl_monitor_info *sdlvideo_monitor_from_handle(UINT32 monitor); //FIXME: Remove? not referenced
157156
158157#endif
src/osd/sdl/window.h
r4070r4071
3131//  TYPE DEFINITIONS
3232//============================================================
3333
34typedef struct _texture_info texture_info;
35
36#if USE_OPENGL
37typedef void (*texture_copy_func)(texture_info *texture, const render_texinfo *texsource);
38#endif
39   
40/* texture_info holds information about a texture */
41struct _texture_info
42{
43   texture_info *      next;            // next texture in the list
44   HashT            hash;            // hash value for the texture (must be >= pointer size)
45   UINT32            flags;            // rendering flags
46   render_texinfo      texinfo;         // copy of the texture info
47   int               rawwidth, rawheight;   // raw width/height of the texture
48   int               rawwidth_create;   // raw width/height, pow2 compatible, if needed
49   int                 rawheight_create;   // (create and initial set the texture, not for copy!)
50   int               type;            // what type of texture are we?
51   int               format;            // texture format
52   int               borderpix;         // do we have a 1 pixel border?
53   int               xprescale;         // what is our X prescale factor?
54   int               yprescale;         // what is our Y prescale factor?
55   int               prescale_effect;   // which prescale effect (if any) to use
56   int               nocopy;            // must the texture date be copied?
57   
58   UINT32            texture;         // OpenGL texture "name"/ID
59#if USE_OPENGL
60   const GLint *      texProperties;      // texture properties
61   texture_copy_func   texCopyFn;         // texture copy function, !=NULL if !nocopy
62   GLenum            texTarget;         // OpenGL texture target
63   int               texpow2;         // Is this texture pow2
64   
65   UINT32            mpass_dest_idx;         // Multipass dest idx [0..1]
66   UINT32            mpass_textureunit[2];   // texture unit names for GLSL
67   
68   UINT32            mpass_texture_mamebm[2];// Multipass OpenGL texture "name"/ID for the shader
69   UINT32            mpass_fbo_mamebm[2];   // framebuffer object for this texture, multipass
70   UINT32            mpass_texture_scrn[2];   // Multipass OpenGL texture "name"/ID for the shader
71   UINT32            mpass_fbo_scrn[2];      // framebuffer object for this texture, multipass
72   
73   UINT32            lut_texture;         // LUT OpenGL texture "name"/ID for the shader
74   int               lut_table_width;      // LUT table width
75   int               lut_table_height;      // LUT table height
76   
77   UINT32            pbo;               // pixel buffer object for this texture (DYNAMIC only!)
78   UINT32            *data;               // pixels for the texture
79   int               data_own;            // do we own / allocated it ?
80   UINT32            *effectbuf;            // buffer for intermediate effect results or NULL
81   GLfloat            texCoord[8];
82   GLuint            texCoordBufferName;
83#endif
84};
85
8634typedef struct _sdl_window_info sdl_window_info;
8735struct _sdl_window_info
8836{
r4070r4071
180128void sdlwindow_modify_effect(running_machine *machine, sdl_window_info *window, int dir);
181129void sdlwindow_toggle_draw(running_machine *machine, sdl_window_info *window);
182130void sdlwindow_resize(sdl_window_info *window, INT32 width, INT32 height);
131void sdlwindow_clear(sdl_window_info *window);
183132
133
184134//============================================================
185135// PROTOTYPES - drawsdl.c
186136//============================================================
r4070r4071
190140int drawsdl_scale_mode(const char *s);
191141
192142//============================================================
193// PROTOTYPES - drawsdl.c
143// PROTOTYPES - drawogl.c
194144//============================================================
195145
196146int drawogl_init(sdl_draw_info *callbacks);
197147
148//============================================================
149// PROTOTYPES - draw13.c
150//============================================================
151
152int draw13_init(sdl_draw_info *callbacks);
153
198154#endif /* __SDLWINDOW__ */
src/osd/sdl/sdlmain.c
r4070r4071
287287   fprintf(stderr, "%s: %s\n", title, message);
288288}
289289
290#ifdef SDLMAME_WIN32
290#if defined(SDLMAME_WIN32)
291291int SDL_main(int argc, char **argv);
292292
293293static BOOL OutOfMemory(void)
r4070r4071
300300{
301301   size_t n;
302302   char *bufp, *appname;
303
303304   int status;
304305
305306   /* Get the class name from argv[0] */
307
306308   appname = argv[0];
307309   if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
308310      appname = bufp+1;
r4070r4071
348350}
349351#endif
350352
351#ifndef SDLMAME_WIN32
353#if !defined(SDLMAME_WIN32)
352354int main(int argc, char **argv)
353355#else
354356int SDL_main(int argc, char **argv)
r4070r4071
584586   
585587   if (!SDLMAME_INIT_IN_WORKER_THREAD)
586588   {
589#if (SDL_VERSION_ATLEAST(1,3,0))
590      if (SDL_InitSubSystem(SDL_INIT_TIMER|SDL_INIT_AUDIO| SDL_INIT_VIDEO| SDL_INIT_JOYSTICK|SDL_INIT_NOPARACHUTE)) {
591#else
587592      if (SDL_Init(SDL_INIT_TIMER|SDL_INIT_AUDIO| SDL_INIT_VIDEO| SDL_INIT_JOYSTICK|SDL_INIT_NOPARACHUTE)) {
593#endif
588594         /* mame_printf_* not fully initialized yet */
589595         ShowError("Could not initialize SDL", SDL_GetError());
590596         exit(-1);
src/osd/sdl/gl_shader_tool.h
r4070r4071
138138
139139int gl_delete_shader( GLhandleARB *program, GLhandleARB *vertex_shader, GLhandleARB *fragment_shader );
140140
141#if defined(SDLMAME_MACOSX) || defined(SDLMAME_WIN32)
141#if defined(SDLMAME_MACOSX) || (defined(SDLMAME_WIN32) && !(SDL_VERSION_ATLEAST(1,3,0)))
142142#ifndef GL_ARB_shader_objects
143143    typedef char GLcharARB;
144144    typedef unsigned int GLhandleARB;
src/osd/sdl/osdsdl.h
r4070r4071
55
66   - Known bugs:
77   
8     * SDL1.3/X11: Crash on switch to fullscreen. Bug fix submitted to SDL team.
98     * SDL1.3/X11: Some compound keys, e.g. "'" are not supported by SDL driver
10     * SDL1.3/X11: No -switchres because SDL does not support it right now.
11     * SDL1.3/directfb: -switchres broken for directfb >= 1.2.0/works with 1.0.
129     * SDL1.3: sdlvideofps does not take -numscreens>1 into account.
10     * SDL1.3/WIN32: crashes with -rd d3d
11     * SDL1.3/WIN32: resizing does not work
1312
13   - fixed returning (w,h) = (0,0) in get_max_bounds
14   - new video driver "sdl13" utilitizing SDL texture and line
15     drawing support. Accelerated drivers like directfb now
16     may attain opengl speed. DirectFB does with a radeon card.
17     The driver determines which pixel formats perform best and
18     converts textures to these pixel formats.
19     Supported options:
20     -waitvsync
21     -filter
22     -prescale
23     Supported renderdrivers:
24     X11: opengl, software, x11 (no artwork!)
25     DirectFB: directfb, software, opengl (special setup, slow)
26     Windows: software, gdi (no artwork)
27   - SDL1.3: Clear bck when moving
28   - SDL1.3: now compiles on win32
29   - SDL1.3: Support screen refresh rates, provided the sdl video
30     layer supports them (e.g. X11).
31   - Use video_config.waitvsync (previously option was queried)
32   - -video soft / -sm  now supports -prescale
33   - removed reqwidth, reqheight from sdl_monitor_info; not used anywhere   
34   - removed layerconfig from sdl_video_config; not used anywhere   
35
1436   - fixed yuv issues (firefox) / this was a bug I introduced
1537   - removed deprecat.h from output.c
1638   - Separated keyboard and mouse initialization into separate functions
r4070r4071
263285#define SDLOPTVAL_OPENGL            "opengl"
264286#define SDLOPTVAL_OPENGL16            "opengl16"
265287#define SDLOPTVAL_SOFT               "soft"
288#define SDLOPTVAL_SDL13               "sdl13"
266289
267290#define SDLMAME_LED(x)               "led" #x
268291

Previous 15994 Revisions Next


© 1998-2018 The MESS Team