2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
29 Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
35 #include <mint/osbind.h>
36 #include <mint/cookie.h>
38 #include "SDL_endian.h"
39 #include "SDL_video.h"
40 #include "SDL_mouse.h"
41 #include "../SDL_sysvideo.h"
42 #include "../SDL_pixels_c.h"
43 #include "../../events/SDL_events_c.h"
44 #include "../SDL_cursor_c.h"
46 #include "../ataricommon/SDL_ataric2p_s.h"
47 #include "../ataricommon/SDL_atarieddi_s.h"
48 #include "../ataricommon/SDL_atarimxalloc_c.h"
49 #include "../ataricommon/SDL_atarigl_c.h"
51 #include "SDL_gemvideo.h"
52 #include "SDL_gemevents_c.h"
53 #include "SDL_gemmouse_c.h"
54 #include "SDL_gemwm_c.h"
55 #include "../ataricommon/SDL_xbiosevents_c.h"
56 #include "../ataricommon/SDL_ataridevmouse_c.h"
60 /*#define DEBUG_VIDEO_GEM 1*/
62 #define GEM_VID_DRIVER_NAME "gem"
65 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
67 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
71 static unsigned char vdi_index[256] = {
72 0, 2, 3, 6, 4, 7, 5, 8,
73 9, 10, 11, 14, 12, 15, 13, 255
76 static const unsigned char empty_name[]="";
78 /* Initialization/Query functions */
79 static int GEM_VideoInit(_THIS, SDL_PixelFormat *vformat);
80 static SDL_Rect **GEM_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
81 static SDL_Surface *GEM_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
82 static int GEM_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
83 static void GEM_VideoQuit(_THIS);
85 /* Hardware surface functions */
86 static int GEM_AllocHWSurface(_THIS, SDL_Surface *surface);
87 static int GEM_LockHWSurface(_THIS, SDL_Surface *surface);
88 static int GEM_FlipHWSurface(_THIS, SDL_Surface *surface);
89 static void GEM_UnlockHWSurface(_THIS, SDL_Surface *surface);
90 static void GEM_FreeHWSurface(_THIS, SDL_Surface *surface);
91 static void GEM_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
93 static int GEM_ToggleFullScreen(_THIS, int on);
96 /* Internal functions */
97 static void GEM_FreeBuffers(_THIS);
98 static void GEM_ClearScreen(_THIS);
99 static void GEM_ClearRect(_THIS, short *rect);
100 static void GEM_SetNewPalette(_THIS, Uint16 newpal[256][3]);
101 static void GEM_LockScreen(_THIS);
102 static void GEM_UnlockScreen(_THIS);
103 static void refresh_window(_THIS, int winhandle, short *rect);
106 /* OpenGL functions */
107 static void GEM_GL_SwapBuffers(_THIS);
110 /* GEM driver bootstrap functions */
112 static int GEM_Available(void)
114 /* Test if AES available */
115 if (appl_init() == -1)
122 static void GEM_DeleteDevice(SDL_VideoDevice *device)
124 SDL_free(device->hidden);
128 static SDL_VideoDevice *GEM_CreateDevice(int devindex)
130 SDL_VideoDevice *device;
134 /* Initialize all variables that we clean on shutdown */
135 device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
137 SDL_memset(device, 0, (sizeof *device));
138 device->hidden = (struct SDL_PrivateVideoData *)
139 SDL_malloc((sizeof *device->hidden));
140 device->gl_data = (struct SDL_PrivateGLData *)
141 SDL_malloc((sizeof *device->gl_data));
143 if ( (device == NULL) || (device->hidden == NULL) ) {
150 SDL_memset(device->hidden, 0, (sizeof *device->hidden));
151 SDL_memset(device->gl_data, 0, sizeof(*device->gl_data));
153 /* Set the function pointers */
154 device->VideoInit = GEM_VideoInit;
155 device->ListModes = GEM_ListModes;
156 device->SetVideoMode = GEM_SetVideoMode;
157 device->SetColors = GEM_SetColors;
158 device->UpdateRects = NULL /*GEM_UpdateRects*/;
159 device->VideoQuit = GEM_VideoQuit;
160 device->AllocHWSurface = GEM_AllocHWSurface;
161 device->LockHWSurface = GEM_LockHWSurface;
162 device->UnlockHWSurface = GEM_UnlockHWSurface;
163 device->FlipHWSurface = GEM_FlipHWSurface;
164 device->FreeHWSurface = GEM_FreeHWSurface;
165 device->ToggleFullScreen = NULL /*GEM_ToggleFullScreen*/;
168 device->SetCaption = GEM_SetCaption;
169 device->SetIcon = GEM_SetIcon;
170 device->IconifyWindow = GEM_IconifyWindow;
171 device->GrabInput = GEM_GrabInput;
174 device->InitOSKeymap = GEM_InitOSKeymap;
175 device->PumpEvents = GEM_PumpEvents;
178 device->FreeWMCursor = GEM_FreeWMCursor;
179 device->CreateWMCursor = GEM_CreateWMCursor;
180 device->ShowWMCursor = GEM_ShowWMCursor;
181 device->WarpWMCursor = NULL /*GEM_WarpWMCursor*/;
182 device->CheckMouseMode = GEM_CheckMouseMode;
185 /* OpenGL functions */
186 device->GL_LoadLibrary = SDL_AtariGL_LoadLibrary;
187 device->GL_GetProcAddress = SDL_AtariGL_GetProcAddress;
188 device->GL_GetAttribute = SDL_AtariGL_GetAttribute;
189 device->GL_MakeCurrent = SDL_AtariGL_MakeCurrent;
190 device->GL_SwapBuffers = GEM_GL_SwapBuffers;
193 device->hidden->use_dev_mouse =
194 (SDL_AtariDevMouse_Open()!=0) ? SDL_TRUE : SDL_FALSE;
196 vectors_mask = ATARI_XBIOS_JOYSTICKEVENTS; /* XBIOS joystick events */
197 if (!(device->hidden->use_dev_mouse)) {
198 vectors_mask |= ATARI_XBIOS_MOUSEEVENTS; /* XBIOS mouse events */
200 /* if (Getcookie(C_MiNT, &dummy)==C_FOUND) {
204 SDL_AtariXbios_InstallVectors(vectors_mask);
206 device->free = GEM_DeleteDevice;
211 VideoBootStrap GEM_bootstrap = {
212 GEM_VID_DRIVER_NAME, "Atari GEM video driver",
213 GEM_Available, GEM_CreateDevice
216 static void VDI_ReadExtInfo(_THIS, short *work_out)
218 unsigned long EdDI_version;
219 unsigned long cookie_EdDI;
221 Uint16 clut_type, num_bits;
223 /* Read EdDI informations */
224 if (Getcookie(C_EdDI, &cookie_EdDI) == C_NOTFOUND) {
228 EdDI_version = Atari_get_EdDI_version( (void *)cookie_EdDI);
230 vq_scrninfo(VDI_handle, work_out);
232 VDI_format = work_out[0];
233 clut_type = work_out[1];
234 num_bits = work_out[2];
235 num_colours = *((Uint32 *) &work_out[3]);
237 /* With EdDI>=1.1, we can have screen pitch, address and format
238 * so we can directly write to screen without using vro_cpyfm
240 if (EdDI_version >= EDDI_11) {
241 VDI_pitch = work_out[5];
242 VDI_screen = (void *) *((unsigned long *) &work_out[6]);
246 case VDI_CLUT_HARDWARE:
251 tmp_p = (Uint16 *)&work_out[16];
253 for (i=0;i<256;i++) {
254 vdi_index[*tmp_p++] = i;
258 case VDI_CLUT_SOFTWARE:
260 int component; /* red, green, blue, alpha, overlay */
262 unsigned short *tmp_p;
264 /* We can build masks with info here */
265 tmp_p = (unsigned short *) &work_out[16];
266 for (component=0;component<5;component++) {
267 for (num_bit=0;num_bit<16;num_bit++) {
268 unsigned short valeur;
272 if (valeur == 0xffff) {
278 VDI_redmask |= 1<< valeur;
281 VDI_greenmask |= 1<< valeur;
284 VDI_bluemask |= 1<< valeur;
287 VDI_alphamask |= 1<< valeur;
294 /* Remove lower green bits for Intel endian screen */
295 if ((VDI_greenmask == ((7<<13)|3)) || (VDI_greenmask == ((7<<13)|7))) {
296 VDI_greenmask &= ~(7<<13);
304 int GEM_VideoInit(_THIS, SDL_PixelFormat *vformat)
307 short work_in[12], work_out[272], dummy;
309 /* Open AES (Application Environment Services) */
310 if (appl_init() == -1) {
311 fprintf(stderr,"Can not open AES\n");
315 /* Read version and features */
316 GEM_version = aes_global[0];
317 if (GEM_version >= 0x0410) {
318 short ap_gout[4], errorcode;
321 errorcode=appl_getinfo(AES_WINDOW, &ap_gout[0], &ap_gout[1], &ap_gout[2], &ap_gout[3]);
324 GEM_wfeatures=ap_gout[0];
328 /* Ask VDI physical workstation handle opened by AES */
329 VDI_handle = graf_handle(&dummy, &dummy, &dummy, &dummy);
331 fprintf(stderr,"Wrong VDI handle %d returned by AES\n",VDI_handle);
335 /* Open virtual VDI workstation */
336 work_in[0]=Getrez()+2;
337 for(i = 1; i < 10; i++)
341 v_opnvwk(work_in, &VDI_handle, work_out);
342 if (VDI_handle == 0) {
343 fprintf(stderr,"Can not open VDI virtual workstation\n");
347 /* Read fullscreen size */
348 VDI_w = work_out[0] + 1;
349 VDI_h = work_out[1] + 1;
351 /* Read desktop size and position */
352 if (!wind_get(DESKTOP_HANDLE, WF_WORKXYWH, &GEM_desk_x, &GEM_desk_y, &GEM_desk_w, &GEM_desk_h)) {
353 fprintf(stderr,"Can not read desktop properties\n");
358 vq_extnd(VDI_handle, 1, work_out);
359 VDI_bpp = work_out[4];
377 fprintf(stderr,"%d bits colour depth not supported\n",VDI_bpp);
381 /* Setup hardware -> VDI palette mapping */
382 for(i = 16; i < 255; i++) {
387 /* Save current palette */
389 VDI_oldnumcolors=1<<8;
391 VDI_oldnumcolors=1<<VDI_bpp;
394 for(i = 0; i < VDI_oldnumcolors; i++) {
397 vq_color(VDI_handle, i, 0, rgb);
399 VDI_oldpalette[i][0] = rgb[0];
400 VDI_oldpalette[i][1] = rgb[1];
401 VDI_oldpalette[i][2] = rgb[2];
403 VDI_setpalette = GEM_SetNewPalette;
404 SDL_memcpy(VDI_curpalette,VDI_oldpalette,sizeof(VDI_curpalette));
406 /* Setup screen info */
407 GEM_title_name = empty_name;
408 GEM_icon_name = empty_name;
411 GEM_locked = SDL_FALSE;
412 GEM_win_fulled = SDL_FALSE;
413 GEM_fullscreen = SDL_FALSE;
414 GEM_lock_redraw = SDL_TRUE; /* Prevent redraw till buffers are setup */
417 VDI_pitch = VDI_w * VDI_pixelsize;
418 VDI_format = ( (VDI_bpp <= 8) ? VDI_FORMAT_INTER : VDI_FORMAT_PACK);
419 VDI_redmask = VDI_greenmask = VDI_bluemask = VDI_alphamask = 0;
420 VDI_ReadExtInfo(this, work_out);
422 #ifdef DEBUG_VIDEO_GEM
423 printf("sdl:video:gem: screen: address=0x%08x, pitch=%d\n", VDI_screen, VDI_pitch);
424 printf("sdl:video:gem: format=%d\n", VDI_format);
425 printf("sdl:video:gem: masks: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
426 VDI_alphamask, VDI_redmask, VDI_greenmask, VDI_bluemask
430 /* Setup destination mfdb */
431 VDI_dst_mfdb.fd_addr = NULL;
433 /* Determine the current screen size */
434 this->info.current_w = VDI_w;
435 this->info.current_h = VDI_h;
437 /* Determine the screen depth */
438 /* we change this during the SDL_SetVideoMode implementation... */
439 vformat->BitsPerPixel = VDI_bpp;
441 /* Set mouse cursor to arrow */
442 graf_mouse(ARROW, NULL);
445 /* Init chunky to planar routine */
446 SDL_Atari_C2pConvert = SDL_Atari_C2pConvert8;
448 /* Setup VDI fill functions */
449 vsf_color(VDI_handle,0);
450 vsf_interior(VDI_handle,1);
451 vsf_perimeter(VDI_handle,0);
453 /* Menu bar save buffer */
454 menubar_size = GEM_desk_w * GEM_desk_y * VDI_pixelsize;
455 GEM_menubar=Atari_SysMalloc(menubar_size,MX_PREFTTRAM);
457 /* Fill video modes list */
458 SDL_modelist[0] = SDL_malloc(sizeof(SDL_Rect));
459 SDL_modelist[0]->x = 0;
460 SDL_modelist[0]->y = 0;
461 SDL_modelist[0]->w = VDI_w;
462 SDL_modelist[0]->h = VDI_h;
464 SDL_modelist[1] = NULL;
467 SDL_AtariGL_InitPointers(this);
470 this->info.wm_available = 1;
476 SDL_Rect **GEM_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
478 if (format->BitsPerPixel != VDI_bpp) {
479 return ((SDL_Rect **)NULL);
482 if (flags & SDL_FULLSCREEN) {
483 return (SDL_modelist);
486 return((SDL_Rect **)-1);
489 static void GEM_FreeBuffers(_THIS)
493 Mfree( GEM_buffer2 );
498 Mfree( GEM_buffer1 );
503 static void GEM_ClearRect(_THIS, short *rect)
505 short oldrgb[3], rgb[3]={0,0,0};
507 vq_color(VDI_handle, vdi_index[0], 0, oldrgb);
508 vs_color(VDI_handle, vdi_index[0], rgb);
510 vsf_color(VDI_handle,0);
511 vsf_interior(VDI_handle,1);
512 vsf_perimeter(VDI_handle,0);
513 v_bar(VDI_handle, rect);
515 vs_color(VDI_handle, vdi_index[0], oldrgb);
518 static void GEM_ClearScreen(_THIS)
522 v_hide_c(VDI_handle);
527 GEM_ClearRect(this, pxy);
529 v_show_c(VDI_handle, 1);
532 static void GEM_SetNewPalette(_THIS, Uint16 newpal[256][3])
537 if (VDI_oldnumcolors==0)
540 for(i = 0; i < VDI_oldnumcolors; i++) {
541 rgb[0] = newpal[i][0];
542 rgb[1] = newpal[i][1];
543 rgb[2] = newpal[i][2];
545 vs_color(VDI_handle, i, rgb);
549 static void GEM_LockScreen(_THIS)
553 wind_update(BEG_UPDATE);
554 wind_update(BEG_MCTRL);
555 /* Reserve memory space, used to be sure of compatibility */
556 form_dial( FMD_START, 0,0,0,0, 0,0,VDI_w,VDI_h);
563 mfdb_src.fd_addr=GEM_menubar;
564 mfdb_src.fd_w=GEM_desk_w;
565 mfdb_src.fd_h=GEM_desk_y;
566 mfdb_src.fd_wdwidth=GEM_desk_w>>4;
567 mfdb_src.fd_nplanes=VDI_bpp;
573 blitcoords[0] = blitcoords[4] = 0;
574 blitcoords[1] = blitcoords[5] = 0;
575 blitcoords[2] = blitcoords[6] = GEM_desk_w-1;
576 blitcoords[3] = blitcoords[7] = GEM_desk_y-1;
578 vro_cpyfm(VDI_handle, S_ONLY, blitcoords, &VDI_dst_mfdb, &mfdb_src);
585 static void GEM_UnlockScreen(_THIS)
588 /* Restore menu bar */
593 mfdb_src.fd_addr=GEM_menubar;
594 mfdb_src.fd_w=GEM_desk_w;
595 mfdb_src.fd_h=GEM_desk_y;
596 mfdb_src.fd_wdwidth=GEM_desk_w>>4;
597 mfdb_src.fd_nplanes=VDI_bpp;
603 blitcoords[0] = blitcoords[4] = 0;
604 blitcoords[1] = blitcoords[5] = 0;
605 blitcoords[2] = blitcoords[6] = GEM_desk_w-1;
606 blitcoords[3] = blitcoords[7] = GEM_desk_y-1;
608 vro_cpyfm(VDI_handle, S_ONLY, blitcoords, &mfdb_src, &VDI_dst_mfdb);
611 /* Restore screen memory, and send REDRAW to all apps */
612 form_dial( FMD_FINISH, 0,0,0,0, 0,0,VDI_w,VDI_h);
614 wind_update(END_MCTRL);
615 wind_update(END_UPDATE);
617 GEM_locked=SDL_FALSE;
621 SDL_Surface *GEM_SetVideoMode(_THIS, SDL_Surface *current,
622 int width, int height, int bpp, Uint32 flags)
624 Uint32 modeflags, screensize;
625 SDL_bool use_shadow1, use_shadow2;
627 /* width must be multiple of 16, for vro_cpyfm() and c2p_convert() */
628 if ((width & 15) != 0) {
629 width = (width | 15) +1;
632 /*--- Verify if asked mode can be used ---*/
633 if (VDI_bpp != bpp) {
634 SDL_SetError("%d bpp mode not supported", bpp);
638 if (flags & SDL_FULLSCREEN) {
639 if ((VDI_w < width) || (VDI_h < height)) {
640 SDL_SetError("%dx%d mode is too large", width, height);
645 /*--- Allocate the new pixel format for the screen ---*/
646 if ( ! SDL_ReallocFormat(current, VDI_bpp, VDI_redmask, VDI_greenmask, VDI_bluemask, VDI_alphamask) ) {
647 SDL_SetError("Couldn't allocate new pixel format for requested mode");
651 screensize = width * height * VDI_pixelsize;
653 #ifdef DEBUG_VIDEO_GEM
654 printf("sdl:video:gem: setvideomode(): %dx%dx%d = %d\n", width, height, bpp, screensize);
657 /*--- Allocate shadow buffers if needed, and conversion operations ---*/
658 GEM_FreeBuffers(this);
661 use_shadow1=use_shadow2=SDL_FALSE;
662 if (VDI_screen && (flags & SDL_FULLSCREEN)) {
663 if (VDI_format==VDI_FORMAT_INTER) {
664 use_shadow1=SDL_TRUE;
665 GEM_bufops = B2S_C2P_1TOS;
668 use_shadow1=SDL_TRUE;
669 if (VDI_format==VDI_FORMAT_PACK) {
670 GEM_bufops = B2S_VROCPYFM_1TOS;
672 use_shadow2=SDL_TRUE;
673 GEM_bufops = B2S_C2P_1TO2|B2S_VROCPYFM_2TOS;
678 GEM_buffer1 = Atari_SysMalloc(screensize, MX_PREFTTRAM);
679 if (GEM_buffer1==NULL) {
680 SDL_SetError("Can not allocate %d KB for frame buffer", screensize>>10);
683 SDL_memset(GEM_buffer1, 0, screensize);
684 #ifdef DEBUG_VIDEO_GEM
685 printf("sdl:video:gem: setvideomode(): allocated buffer 1\n");
690 GEM_buffer2 = Atari_SysMalloc(screensize, MX_PREFTTRAM);
691 if (GEM_buffer2==NULL) {
692 SDL_SetError("Can not allocate %d KB for shadow buffer", screensize>>10);
695 SDL_memset(GEM_buffer2, 0, screensize);
696 #ifdef DEBUG_VIDEO_GEM
697 printf("sdl:video:gem: setvideomode(): allocated buffer 2\n");
701 /*--- Initialize screen ---*/
702 modeflags = SDL_PREALLOC;
704 modeflags |= SDL_HWPALETTE;
707 if (flags & SDL_FULLSCREEN) {
708 GEM_LockScreen(this);
710 GEM_ClearScreen(this);
712 modeflags |= SDL_FULLSCREEN;
713 if (VDI_screen && (VDI_format==VDI_FORMAT_PACK) && !use_shadow1) {
714 modeflags |= SDL_HWSURFACE;
716 modeflags |= SDL_SWSURFACE;
719 GEM_fullscreen = SDL_TRUE;
724 GEM_UnlockScreen(this);
726 /* Set window gadgets */
727 old_win_type = GEM_win_type;
728 if (!(flags & SDL_NOFRAME)) {
729 GEM_win_type=NAME|MOVER|CLOSER|SMALLER;
730 if (flags & SDL_RESIZABLE) {
731 GEM_win_type |= FULLER|SIZER;
732 modeflags |= SDL_RESIZABLE;
736 modeflags |= SDL_NOFRAME;
738 modeflags |= SDL_SWSURFACE;
740 /* Recreate window ? only for different widget or non-created window */
741 if ((old_win_type != GEM_win_type) || (GEM_handle < 0)) {
742 /* Calculate window size */
743 if (!wind_calc(WC_BORDER, GEM_win_type, 0,0,width,height, &x2,&y2,&w2,&h2)) {
744 GEM_FreeBuffers(this);
745 SDL_SetError("Can not calculate window attributes");
750 x2 = (GEM_desk_w-w2)>>1;
751 y2 = (GEM_desk_h-h2)>>1;
761 /* Destroy existing window */
762 if (GEM_handle >= 0) {
763 wind_close(GEM_handle);
764 wind_delete(GEM_handle);
768 GEM_handle=wind_create(GEM_win_type, x2,y2,w2,h2);
770 GEM_FreeBuffers(this);
771 SDL_SetError("Can not create window");
775 #ifdef DEBUG_VIDEO_GEM
776 printf("sdl:video:gem: handle=%d\n", GEM_handle);
779 /* Setup window name */
780 wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
781 GEM_refresh_name = SDL_FALSE;
783 /* Open the window */
784 wind_open(GEM_handle,x2,y2,w2,h2);
786 /* Resize window to fit asked video mode */
787 wind_get (GEM_handle, WF_WORKXYWH, &x2,&y2,&w2,&h2);
788 if (wind_calc(WC_BORDER, GEM_win_type, x2,y2,width,height, &x2,&y2,&w2,&h2)) {
789 wind_set (GEM_handle, WF_CURRXYWH, x2,y2,w2,h2);
793 GEM_fullscreen = SDL_FALSE;
796 /* Set up the new mode framebuffer */
800 current->pixels = GEM_buffer1;
801 current->pitch = width * VDI_pixelsize;
803 current->pixels = VDI_screen;
804 current->pitch = VDI_pitch;
808 if (flags & SDL_OPENGL) {
809 if (!SDL_AtariGL_Init(this, current)) {
810 GEM_FreeBuffers(this);
811 SDL_SetError("Can not create OpenGL context");
815 modeflags |= SDL_OPENGL;
819 current->flags = modeflags;
821 #ifdef DEBUG_VIDEO_GEM
822 printf("sdl:video:gem: surface: %dx%d\n", current->w, current->h);
825 this->UpdateRects = GEM_UpdateRects;
826 GEM_lock_redraw = SDL_FALSE; /* Enable redraw */
832 static int GEM_AllocHWSurface(_THIS, SDL_Surface *surface)
837 static void GEM_FreeHWSurface(_THIS, SDL_Surface *surface)
842 static int GEM_LockHWSurface(_THIS, SDL_Surface *surface)
847 static void GEM_UnlockHWSurface(_THIS, SDL_Surface *surface)
852 static void GEM_UpdateRectsFullscreen(_THIS, int numrects, SDL_Rect *rects)
854 SDL_Surface *surface;
857 surface = this->screen;
858 /* Need to be a multiple of 16 pixels */
859 surf_width=surface->w;
860 if ((surf_width & 15) != 0) {
861 surf_width = (surf_width | 15) + 1;
864 if (GEM_bufops & (B2S_C2P_1TO2|B2S_C2P_1TOS)) {
868 if (GEM_bufops & B2S_C2P_1TOS) {
869 destscr = VDI_screen;
870 destpitch = VDI_pitch;
872 destscr = GEM_buffer2;
873 destpitch = surface->pitch;
876 for (i=0;i<numrects;i++) {
877 void *source,*destination;
880 x1 = rects[i].x & ~15;
881 x2 = rects[i].x+rects[i].w;
886 source = surface->pixels;
887 source += surface->pitch * rects[i].y;
890 destination = destscr;
891 destination += destpitch * rects[i].y;
894 SDL_Atari_C2pConvert(
898 surface->pitch, destpitch
903 if (GEM_bufops & (B2S_VROCPYFM_1TOS|B2S_VROCPYFM_2TOS)) {
907 mfdb_src.fd_addr=surface->pixels;
908 mfdb_src.fd_w=surf_width;
909 mfdb_src.fd_h=surface->h;
910 mfdb_src.fd_wdwidth= (surface->pitch/VDI_pixelsize) >> 4;
911 mfdb_src.fd_nplanes=surface->format->BitsPerPixel;
916 if (GEM_bufops & B2S_VROCPYFM_2TOS) {
917 mfdb_src.fd_addr=GEM_buffer2;
920 for ( i=0; i<numrects; ++i ) {
921 blitcoords[0] = blitcoords[4] = rects[i].x;
922 blitcoords[1] = blitcoords[5] = rects[i].y;
923 blitcoords[2] = blitcoords[6] = rects[i].x + rects[i].w - 1;
924 blitcoords[3] = blitcoords[7] = rects[i].y + rects[i].h - 1;
926 vro_cpyfm(VDI_handle, S_ONLY, blitcoords, &mfdb_src, &VDI_dst_mfdb);
931 static void GEM_UpdateRectsWindowed(_THIS, int numrects, SDL_Rect *rects)
933 short pxy[4], wind_pxy[4];
936 if (wind_get(GEM_handle, WF_WORKXYWH, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3])==0) {
940 for ( i=0; i<numrects; ++i ) {
941 pxy[0] = wind_pxy[0] + rects[i].x;
942 pxy[1] = wind_pxy[1] + rects[i].y;
946 GEM_wind_redraw(this, GEM_handle, pxy);
950 static void GEM_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
952 SDL_Surface *surface;
954 if (GEM_lock_redraw) {
958 surface = this->screen;
960 if (surface->flags & SDL_FULLSCREEN) {
961 GEM_UpdateRectsFullscreen(this, numrects, rects);
963 GEM_UpdateRectsWindowed(this, numrects, rects);
967 static int GEM_FlipHWSurfaceFullscreen(_THIS, SDL_Surface *surface)
971 /* Need to be a multiple of 16 pixels */
972 surf_width=surface->w;
973 if ((surf_width & 15) != 0) {
974 surf_width = (surf_width | 15) + 1;
977 if (GEM_bufops & (B2S_C2P_1TO2|B2S_C2P_1TOS)) {
981 if (GEM_bufops & B2S_C2P_1TOS) {
982 destscr = VDI_screen;
983 destpitch = VDI_pitch;
985 destscr = GEM_buffer2;
986 destpitch = surface->pitch;
989 SDL_Atari_C2pConvert(
990 surface->pixels, destscr,
991 surf_width, surface->h,
993 surface->pitch, destpitch
997 if (GEM_bufops & (B2S_VROCPYFM_1TOS|B2S_VROCPYFM_2TOS)) {
1001 mfdb_src.fd_w=surf_width;
1002 mfdb_src.fd_h=surface->h;
1003 mfdb_src.fd_wdwidth=mfdb_src.fd_w >> 4;
1004 mfdb_src.fd_nplanes=surface->format->BitsPerPixel;
1009 if (GEM_bufops & B2S_VROCPYFM_1TOS) {
1010 mfdb_src.fd_addr=surface->pixels;
1012 mfdb_src.fd_addr=GEM_buffer2;
1015 blitcoords[0] = blitcoords[4] = 0;
1016 blitcoords[1] = blitcoords[5] = 0;
1017 blitcoords[2] = blitcoords[6] = surface->w - 1;
1018 blitcoords[3] = blitcoords[7] = surface->h - 1;
1020 vro_cpyfm(VDI_handle, S_ONLY, blitcoords, &mfdb_src, &VDI_dst_mfdb);
1026 static int GEM_FlipHWSurfaceWindowed(_THIS, SDL_Surface *surface)
1030 /* Update the whole window */
1031 wind_get(GEM_handle, WF_WORKXYWH, &pxy[0], &pxy[1], &pxy[2], &pxy[3]);
1033 GEM_wind_redraw(this, GEM_handle, pxy);
1038 static int GEM_FlipHWSurface(_THIS, SDL_Surface *surface)
1040 if (GEM_lock_redraw) {
1044 if (surface->flags & SDL_FULLSCREEN) {
1045 return GEM_FlipHWSurfaceFullscreen(this, surface);
1047 return GEM_FlipHWSurfaceWindowed(this, surface);
1051 static int GEM_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
1054 SDL_Surface *surface;
1056 #ifdef DEBUG_VIDEO_GEM
1057 printf("sdl:video:gem: setcolors()\n");
1060 /* Do not change palette in True Colour */
1061 surface = this->screen;
1062 if (surface->format->BitsPerPixel > 8) {
1066 for(i = 0; i < ncolors; i++)
1075 rgb[0] = VDI_curpalette[i][0] = (1000 * r) / 255;
1076 rgb[1] = VDI_curpalette[i][1] =(1000 * g) / 255;
1077 rgb[2] = VDI_curpalette[i][2] =(1000 * b) / 255;
1079 vs_color(VDI_handle, vdi_index[firstcolor+i], rgb);
1086 static int GEM_ToggleFullScreen(_THIS, int on)
1089 GEM_LockScreen(this);
1091 GEM_UnlockScreen(this);
1098 /* Note: If we are terminated, this could be called in the middle of
1099 another SDL video routine -- notably UpdateRects.
1101 void GEM_VideoQuit(_THIS)
1103 SDL_AtariXbios_RestoreVectors();
1104 if (GEM_usedevmouse) {
1105 SDL_AtariDevMouse_Close();
1108 GEM_FreeBuffers(this);
1110 #if SDL_VIDEO_OPENGL
1112 SDL_AtariGL_Quit(this, SDL_TRUE);
1116 /* Destroy window */
1117 if (GEM_handle>=0) {
1118 wind_close(GEM_handle);
1119 wind_delete(GEM_handle);
1123 GEM_UnlockScreen(this);
1131 GEM_SetNewPalette(this, VDI_oldpalette);
1133 /* Close VDI workstation */
1135 v_clsvwk(VDI_handle);
1138 /* Free mode list */
1139 if (SDL_modelist[0]) {
1140 SDL_free(SDL_modelist[0]);
1141 SDL_modelist[0]=NULL;
1144 this->screen->pixels = NULL;
1147 void GEM_wind_redraw(_THIS, int winhandle, short *inside)
1151 /* Tell AES we are going to update */
1152 wind_update(BEG_UPDATE);
1154 v_hide_c(VDI_handle);
1156 /* Browse the rectangle list to redraw */
1157 if (wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])!=0) {
1159 while (todo[2] && todo[3]) {
1161 if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
1162 todo[2] += todo[0]-1;
1163 todo[3] += todo[1]-1;
1164 refresh_window(this, winhandle, todo);
1167 if (wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])==0) {
1174 /* Update finished */
1175 wind_update(END_UPDATE);
1177 v_show_c(VDI_handle,1);
1180 static void refresh_window(_THIS, int winhandle, short *rect)
1183 short pxy[8],wind_pxy[8];
1184 SDL_Surface *surface;
1187 /* Is window iconified ? */
1189 /* if (GEM_wfeatures & (1<<WF_ICONIFY))*/ {
1190 if (wind_get(winhandle, WF_ICONIFY, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3])!=0) {
1191 iconified = wind_pxy[0];
1195 if (wind_get(winhandle, WF_WORKXYWH, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3])==0) {
1199 if (iconified && GEM_icon) {
1200 short icon_rect[4], dst_rect[4];
1205 GEM_ClearRect(this, rect);
1207 /* Calculate centered icon(x,y,w,h) relative to window */
1208 iconx = (wind_pxy[2]-surface->w)>>1;
1209 icony = (wind_pxy[3]-surface->h)>>1;
1211 icon_rect[0] = iconx;
1212 icon_rect[1] = icony;
1213 icon_rect[2] = surface->w;
1214 icon_rect[3] = surface->h;
1216 /* Calculate redraw rectangle(x,y,w,h) relative to window */
1217 dst_rect[0] = rect[0]-wind_pxy[0];
1218 dst_rect[1] = rect[1]-wind_pxy[1];
1219 dst_rect[2] = rect[2]-rect[0]+1;
1220 dst_rect[3] = rect[3]-rect[1]+1;
1222 /* Does the icon rectangle must be redrawn ? */
1223 if (!rc_intersect((GRECT *)icon_rect, (GRECT *)dst_rect)) {
1228 printf("sdl:video:gem: clip(0,0,%d,%d) to (%d,%d,%d,%d)\n",
1229 surface->w-1,surface->h-1, dst_rect[0],dst_rect[1],dst_rect[2],dst_rect[3]);
1230 printf("sdl:video:gem: icon(%d,%d,%d,%d)\n",
1231 icon_rect[0], icon_rect[1], icon_rect[2], icon_rect[3]);
1232 printf("sdl:video:gem: refresh_window(): draw icon\n");
1235 /* Calculate icon(x1,y1,x2,y2) relative to screen */
1236 icon_rect[0] += wind_pxy[0];
1237 icon_rect[1] += wind_pxy[1];
1238 icon_rect[2] += icon_rect[0]-1;
1239 icon_rect[3] += icon_rect[1]-1;
1241 /* Calculate intersection rectangle to redraw */
1242 pxy[4]=pxy[0]=MAX(icon_rect[0],rect[0]);
1243 pxy[5]=pxy[1]=MAX(icon_rect[1],rect[1]);
1244 pxy[6]=pxy[2]=MIN(icon_rect[2],rect[2]);
1245 pxy[7]=pxy[3]=MIN(icon_rect[3],rect[3]);
1247 /* Calculate icon source image pos relative to window */
1248 pxy[0] -= wind_pxy[0]+iconx;
1249 pxy[1] -= wind_pxy[1]+icony;
1250 pxy[2] -= wind_pxy[0]+iconx;
1251 pxy[3] -= wind_pxy[1]+icony;
1254 surface = this->screen;
1257 printf("sdl:video:gem: refresh_window(): draw frame buffer\n");
1260 /* Redraw all window content */
1261 pxy[0] = rect[0]-wind_pxy[0];
1262 pxy[1] = rect[1]-wind_pxy[1];
1263 pxy[2] = rect[2]-wind_pxy[0];
1264 pxy[3] = rect[3]-wind_pxy[1];
1272 if (GEM_bufops & B2S_C2P_1TO2) {
1276 x1 = (rect[0]-wind_pxy[0]) & ~15;
1277 x2 = rect[2]-wind_pxy[0];
1282 src = surface->pixels;
1283 src += surface->pitch * (rect[1]-wind_pxy[1]);
1287 dest += surface->pitch * (rect[1]-wind_pxy[1]);
1290 SDL_Atari_C2pConvert(
1292 x2-x1, rect[3]-rect[1]+1,
1294 surface->pitch, surface->pitch
1298 mfdb_src.fd_addr=surface->pixels;
1302 /* Need to be a multiple of 16 pixels */
1304 if ((width & 15) != 0) {
1305 width = (width | 15) + 1;
1307 mfdb_src.fd_w=width;
1309 mfdb_src.fd_h=surface->h;
1310 mfdb_src.fd_nplanes=surface->format->BitsPerPixel;
1311 mfdb_src.fd_wdwidth=mfdb_src.fd_w>>4;
1317 if (GEM_bufops & B2S_VROCPYFM_2TOS) {
1318 mfdb_src.fd_addr=GEM_buffer2;
1322 printf("sdl:video:gem: redraw %dx%d: (%d,%d,%d,%d) to (%d,%d,%d,%d)\n",
1323 surface->w, surface->h,
1324 pxy[0],pxy[1],pxy[2],pxy[3],
1325 pxy[4],pxy[5],pxy[6],pxy[7]
1329 vro_cpyfm( VDI_handle, S_ONLY, pxy, &mfdb_src, &VDI_dst_mfdb);
1332 #if SDL_VIDEO_OPENGL
1334 static void GEM_GL_SwapBuffers(_THIS)
1336 SDL_AtariGL_SwapBuffers(this);
1337 GEM_FlipHWSurface(this, this->screen);