src/video/SDL_video.c
changeset 1336 3692456e7b0f
parent 1330 450721ad5436
child 1338 604d73db6802
equal deleted inserted replaced
1335:c39265384763 1336:3692456e7b0f
   173 	/* Select the proper video driver */
   173 	/* Select the proper video driver */
   174 	index = 0;
   174 	index = 0;
   175 	video = NULL;
   175 	video = NULL;
   176 	if ( driver_name != NULL ) {
   176 	if ( driver_name != NULL ) {
   177 #if 0	/* This will be replaced with a better driver selection API */
   177 #if 0	/* This will be replaced with a better driver selection API */
   178 		if ( strrchr(driver_name, ':') != NULL ) {
   178 		if ( SDL_strrchr(driver_name, ':') != NULL ) {
   179 			index = atoi(strrchr(driver_name, ':')+1);
   179 			index = atoi(SDL_strrchr(driver_name, ':')+1);
   180 		}
   180 		}
   181 #endif
   181 #endif
   182 		for ( i=0; bootstrap[i]; ++i ) {
   182 		for ( i=0; bootstrap[i]; ++i ) {
   183 			if ( strncmp(bootstrap[i]->name, driver_name,
   183 			if ( SDL_strncmp(bootstrap[i]->name, driver_name,
   184 			             strlen(bootstrap[i]->name)) == 0 ) {
   184 			             SDL_strlen(bootstrap[i]->name)) == 0 ) {
   185 				if ( bootstrap[i]->available() ) {
   185 				if ( bootstrap[i]->available() ) {
   186 					video = bootstrap[i]->create(index);
   186 					video = bootstrap[i]->create(index);
   187 					break;
   187 					break;
   188 				}
   188 				}
   189 			}
   189 			}
   214 	video->gamma = NULL;
   214 	video->gamma = NULL;
   215 	video->wm_title = NULL;
   215 	video->wm_title = NULL;
   216 	video->wm_icon  = NULL;
   216 	video->wm_icon  = NULL;
   217 	video->offset_x = 0;
   217 	video->offset_x = 0;
   218 	video->offset_y = 0;
   218 	video->offset_y = 0;
   219 	memset(&video->info, 0, (sizeof video->info));
   219 	SDL_memset(&video->info, 0, (sizeof video->info));
   220 	
   220 	
   221 	video->displayformatalphapixel = NULL;
   221 	video->displayformatalphapixel = NULL;
   222 
   222 
   223 	/* Set some very sane GL defaults */
   223 	/* Set some very sane GL defaults */
   224 	video->gl_config.driver_loaded = 0;
   224 	video->gl_config.driver_loaded = 0;
   242 	video->gl_config.stereo = 0;
   242 	video->gl_config.stereo = 0;
   243 	video->gl_config.multisamplebuffers = 0;
   243 	video->gl_config.multisamplebuffers = 0;
   244 	video->gl_config.multisamplesamples = 0;
   244 	video->gl_config.multisamplesamples = 0;
   245 	
   245 	
   246 	/* Initialize the video subsystem */
   246 	/* Initialize the video subsystem */
   247 	memset(&vformat, 0, sizeof(vformat));
   247 	SDL_memset(&vformat, 0, sizeof(vformat));
   248 	if ( video->VideoInit(video, &vformat) < 0 ) {
   248 	if ( video->VideoInit(video, &vformat) < 0 ) {
   249 		SDL_VideoQuit();
   249 		SDL_VideoQuit();
   250 		return(-1);
   250 		return(-1);
   251 	}
   251 	}
   252 
   252 
   287 }
   287 }
   288 
   288 
   289 char *SDL_VideoDriverName(char *namebuf, int maxlen)
   289 char *SDL_VideoDriverName(char *namebuf, int maxlen)
   290 {
   290 {
   291 	if ( current_video != NULL ) {
   291 	if ( current_video != NULL ) {
   292 		strncpy(namebuf, current_video->name, maxlen-1);
   292 		SDL_strncpy(namebuf, current_video->name, maxlen-1);
   293 		namebuf[maxlen-1] = '\0';
   293 		namebuf[maxlen-1] = '\0';
   294 		return(namebuf);
   294 		return(namebuf);
   295 	}
   295 	}
   296 	return(NULL);
   296 	return(NULL);
   297 }
   297 }
   387 	if ( (width <= 0) || (height <= 0) ) {
   387 	if ( (width <= 0) || (height <= 0) ) {
   388 		return(0);
   388 		return(0);
   389 	}
   389 	}
   390 
   390 
   391 	/* Search through the list valid of modes */
   391 	/* Search through the list valid of modes */
   392 	memset(&format, 0, sizeof(format));
   392 	SDL_memset(&format, 0, sizeof(format));
   393 	supported = 0;
   393 	supported = 0;
   394 	table = ((bpp+7)/8)-1;
   394 	table = ((bpp+7)/8)-1;
   395 	SDL_closest_depths[table][0] = bpp;
   395 	SDL_closest_depths[table][0] = bpp;
   396 	SDL_closest_depths[table][7] = 0;
   396 	SDL_closest_depths[table][7] = 0;
   397 	for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
   397 	for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
   459 		*BitsPerPixel = native_bpp;
   459 		*BitsPerPixel = native_bpp;
   460 		return(1);
   460 		return(1);
   461 	}
   461 	}
   462 
   462 
   463 	/* No exact size match at any depth, look for closest match */
   463 	/* No exact size match at any depth, look for closest match */
   464 	memset(&format, 0, sizeof(format));
   464 	SDL_memset(&format, 0, sizeof(format));
   465 	supported = 0;
   465 	supported = 0;
   466 	table = ((*BitsPerPixel+7)/8)-1;
   466 	table = ((*BitsPerPixel+7)/8)-1;
   467 	SDL_closest_depths[table][0] = *BitsPerPixel;
   467 	SDL_closest_depths[table][0] = *BitsPerPixel;
   468 	SDL_closest_depths[table][7] = SDL_VideoSurface->format->BitsPerPixel;
   468 	SDL_closest_depths[table][7] = SDL_VideoSurface->format->BitsPerPixel;
   469 	for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
   469 	for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
   539 
   539 
   540 	/* 8-bit shadow surfaces report that they have exclusive palette */
   540 	/* 8-bit shadow surfaces report that they have exclusive palette */
   541 	if ( SDL_ShadowSurface->format->palette ) {
   541 	if ( SDL_ShadowSurface->format->palette ) {
   542 		SDL_ShadowSurface->flags |= SDL_HWPALETTE;
   542 		SDL_ShadowSurface->flags |= SDL_HWPALETTE;
   543 		if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
   543 		if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
   544 			memcpy(SDL_ShadowSurface->format->palette->colors,
   544 			SDL_memcpy(SDL_ShadowSurface->format->palette->colors,
   545 				SDL_VideoSurface->format->palette->colors,
   545 				SDL_VideoSurface->format->palette->colors,
   546 				SDL_VideoSurface->format->palette->ncolors*
   546 				SDL_VideoSurface->format->palette->ncolors*
   547 							sizeof(SDL_Color));
   547 							sizeof(SDL_Color));
   548 		} else {
   548 		} else {
   549 			SDL_DitherColors(
   549 			SDL_DitherColors(
   647 		ready_to_go = SDL_ShadowSurface;
   647 		ready_to_go = SDL_ShadowSurface;
   648 		SDL_ShadowSurface = NULL;
   648 		SDL_ShadowSurface = NULL;
   649 		SDL_FreeSurface(ready_to_go);
   649 		SDL_FreeSurface(ready_to_go);
   650 	}
   650 	}
   651 	if ( video->physpal ) {
   651 	if ( video->physpal ) {
   652 		free(video->physpal->colors);
   652 		SDL_free(video->physpal->colors);
   653 		free(video->physpal);
   653 		SDL_free(video->physpal);
   654 		video->physpal = NULL;
   654 		video->physpal = NULL;
   655 	}
   655 	}
   656 	if( video->gammacols) {
   656 	if( video->gammacols) {
   657 		free(video->gammacols);
   657 		SDL_free(video->gammacols);
   658 		video->gammacols = NULL;
   658 		video->gammacols = NULL;
   659 	}
   659 	}
   660 
   660 
   661 	/* Save the previous grab state and turn off grab for mode switch */
   661 	/* Save the previous grab state and turn off grab for mode switch */
   662 	saved_grab = SDL_WM_GrabInputOff();
   662 	saved_grab = SDL_WM_GrabInputOff();
   795 		/* If the implementation either supports the packed pixels
   795 		/* If the implementation either supports the packed pixels
   796 		   extension, or implements the core OpenGL 1.2 API, it will
   796 		   extension, or implements the core OpenGL 1.2 API, it will
   797 		   support the GL_UNSIGNED_SHORT_5_6_5 texture format.
   797 		   support the GL_UNSIGNED_SHORT_5_6_5 texture format.
   798 		 */
   798 		 */
   799 		if ( (bpp == 16) &&
   799 		if ( (bpp == 16) &&
   800 		     (strstr((const char *)video->glGetString(GL_EXTENSIONS), "GL_EXT_packed_pixels") ||
   800 		     (SDL_strstr((const char *)video->glGetString(GL_EXTENSIONS), "GL_EXT_packed_pixels") ||
   801 		     (atof((const char *)video->glGetString(GL_VERSION)) >= 1.2f))
   801 		     (atof((const char *)video->glGetString(GL_VERSION)) >= 1.2f))
   802 		   ) {
   802 		   ) {
   803 			video->is_32bit = 0;
   803 			video->is_32bit = 0;
   804 			SDL_VideoSurface = SDL_CreateRGBSurface(
   804 			SDL_VideoSurface = SDL_CreateRGBSurface(
   805 				flags, 
   805 				flags, 
   841 
   841 
   842 		/* Free the original video mode surface (is this safe?) */
   842 		/* Free the original video mode surface (is this safe?) */
   843 		SDL_FreeSurface(mode);
   843 		SDL_FreeSurface(mode);
   844 
   844 
   845 		/* Set the surface completely opaque & white by default */
   845 		/* Set the surface completely opaque & white by default */
   846 		memset( SDL_VideoSurface->pixels, 255, SDL_VideoSurface->h * SDL_VideoSurface->pitch );
   846 		SDL_memset( SDL_VideoSurface->pixels, 255, SDL_VideoSurface->h * SDL_VideoSurface->pitch );
   847 		video->glGenTextures( 1, &video->texture );
   847 		video->glGenTextures( 1, &video->texture );
   848 		video->glBindTexture( GL_TEXTURE_2D, video->texture );
   848 		video->glBindTexture( GL_TEXTURE_2D, video->texture );
   849 		video->glTexImage2D(
   849 		video->glTexImage2D(
   850 			GL_TEXTURE_2D,
   850 			GL_TEXTURE_2D,
   851 			0,
   851 			0,
  1132 {
  1132 {
  1133 	SDL_Palette *pal = screen->format->palette;
  1133 	SDL_Palette *pal = screen->format->palette;
  1134 	SDL_Palette *vidpal;
  1134 	SDL_Palette *vidpal;
  1135 
  1135 
  1136 	if ( colors != (pal->colors + firstcolor) ) {
  1136 	if ( colors != (pal->colors + firstcolor) ) {
  1137 		memcpy(pal->colors + firstcolor, colors,
  1137 		SDL_memcpy(pal->colors + firstcolor, colors,
  1138 		       ncolors * sizeof(*colors));
  1138 		       ncolors * sizeof(*colors));
  1139 	}
  1139 	}
  1140 
  1140 
  1141 	vidpal = SDL_VideoSurface->format->palette;
  1141 	vidpal = SDL_VideoSurface->format->palette;
  1142 	if ( (screen == SDL_ShadowSurface) && vidpal ) {
  1142 	if ( (screen == SDL_ShadowSurface) && vidpal ) {
  1144 		 * This is a shadow surface, and the physical
  1144 		 * This is a shadow surface, and the physical
  1145 		 * framebuffer is also indexed. Propagate the
  1145 		 * framebuffer is also indexed. Propagate the
  1146 		 * changes to its logical palette so that
  1146 		 * changes to its logical palette so that
  1147 		 * updates are always identity blits
  1147 		 * updates are always identity blits
  1148 		 */
  1148 		 */
  1149 		memcpy(vidpal->colors + firstcolor, colors,
  1149 		SDL_memcpy(vidpal->colors + firstcolor, colors,
  1150 		       ncolors * sizeof(*colors));
  1150 		       ncolors * sizeof(*colors));
  1151 	}
  1151 	}
  1152 	SDL_FormatChanged(screen);
  1152 	SDL_FormatChanged(screen);
  1153 }
  1153 }
  1154 
  1154 
  1160 
  1160 
  1161 	if ( video->physpal ) {
  1161 	if ( video->physpal ) {
  1162 		/* We need to copy the new colors, since we haven't
  1162 		/* We need to copy the new colors, since we haven't
  1163 		 * already done the copy in the logical set above.
  1163 		 * already done the copy in the logical set above.
  1164 		 */
  1164 		 */
  1165 		memcpy(video->physpal->colors + firstcolor,
  1165 		SDL_memcpy(video->physpal->colors + firstcolor,
  1166 		       colors, ncolors * sizeof(*colors));
  1166 		       colors, ncolors * sizeof(*colors));
  1167 	}
  1167 	}
  1168 	if ( screen == SDL_ShadowSurface ) {
  1168 	if ( screen == SDL_ShadowSurface ) {
  1169 		if ( SDL_VideoSurface->flags & SDL_HWPALETTE ) {
  1169 		if ( SDL_VideoSurface->flags & SDL_HWPALETTE ) {
  1170 			/*
  1170 			/*
  1185 			if ( video->gamma ) {
  1185 			if ( video->gamma ) {
  1186 				if( ! video->gammacols ) {
  1186 				if( ! video->gammacols ) {
  1187 					SDL_Palette *pp = video->physpal;
  1187 					SDL_Palette *pp = video->physpal;
  1188 					if(!pp)
  1188 					if(!pp)
  1189 						pp = screen->format->palette;
  1189 						pp = screen->format->palette;
  1190 					video->gammacols = malloc(pp->ncolors
  1190 					video->gammacols = SDL_malloc(pp->ncolors
  1191 							  * sizeof(SDL_Color));
  1191 							  * sizeof(SDL_Color));
  1192 					SDL_ApplyGamma(video->gamma,
  1192 					SDL_ApplyGamma(video->gamma,
  1193 						       pp->colors,
  1193 						       pp->colors,
  1194 						       video->gammacols,
  1194 						       video->gammacols,
  1195 						       pp->ncolors);
  1195 						       pp->ncolors);
  1282 		if(!video)
  1282 		if(!video)
  1283 			return gotall;	/* video not yet initialized */
  1283 			return gotall;	/* video not yet initialized */
  1284 		if(!video->physpal && !(which & SDL_LOGPAL) ) {
  1284 		if(!video->physpal && !(which & SDL_LOGPAL) ) {
  1285 			/* Lazy physical palette allocation */
  1285 			/* Lazy physical palette allocation */
  1286 			int size;
  1286 			int size;
  1287 			SDL_Palette *pp = malloc(sizeof(*pp));
  1287 			SDL_Palette *pp = SDL_malloc(sizeof(*pp));
  1288 			if ( !pp ) {
  1288 			if ( !pp ) {
  1289 				return 0;
  1289 				return 0;
  1290 			}
  1290 			}
  1291 			current_video->physpal = pp;
  1291 			current_video->physpal = pp;
  1292 			pp->ncolors = pal->ncolors;
  1292 			pp->ncolors = pal->ncolors;
  1293 			size = pp->ncolors * sizeof(SDL_Color);
  1293 			size = pp->ncolors * sizeof(SDL_Color);
  1294 			pp->colors = malloc(size);
  1294 			pp->colors = SDL_malloc(size);
  1295 			if ( !pp->colors ) {
  1295 			if ( !pp->colors ) {
  1296 				return 0;
  1296 				return 0;
  1297 			}
  1297 			}
  1298 			memcpy(pp->colors, pal->colors, size);
  1298 			SDL_memcpy(pp->colors, pal->colors, size);
  1299 		}
  1299 		}
  1300 		if ( ! SetPalette_physical(screen,
  1300 		if ( ! SetPalette_physical(screen,
  1301 		                           colors, firstcolor, ncolors) ) {
  1301 		                           colors, firstcolor, ncolors) ) {
  1302 			gotall = 0;
  1302 			gotall = 0;
  1303 		}
  1303 		}
  1349 		}
  1349 		}
  1350 		SDL_PublicSurface = NULL;
  1350 		SDL_PublicSurface = NULL;
  1351 
  1351 
  1352 		/* Clean up miscellaneous memory */
  1352 		/* Clean up miscellaneous memory */
  1353 		if ( video->physpal ) {
  1353 		if ( video->physpal ) {
  1354 			free(video->physpal->colors);
  1354 			SDL_free(video->physpal->colors);
  1355 			free(video->physpal);
  1355 			SDL_free(video->physpal);
  1356 			video->physpal = NULL;
  1356 			video->physpal = NULL;
  1357 		}
  1357 		}
  1358 		if ( video->gammacols ) {
  1358 		if ( video->gammacols ) {
  1359 			free(video->gammacols);
  1359 			SDL_free(video->gammacols);
  1360 			video->gammacols = NULL;
  1360 			video->gammacols = NULL;
  1361 		}
  1361 		}
  1362 		if ( video->gamma ) {
  1362 		if ( video->gamma ) {
  1363 			free(video->gamma);
  1363 			SDL_free(video->gamma);
  1364 			video->gamma = NULL;
  1364 			video->gamma = NULL;
  1365 		}
  1365 		}
  1366 		if ( video->wm_title != NULL ) {
  1366 		if ( video->wm_title != NULL ) {
  1367 			free(video->wm_title);
  1367 			SDL_free(video->wm_title);
  1368 			video->wm_title = NULL;
  1368 			video->wm_title = NULL;
  1369 		}
  1369 		}
  1370 		if ( video->wm_icon != NULL ) {
  1370 		if ( video->wm_icon != NULL ) {
  1371 			free(video->wm_icon);
  1371 			SDL_free(video->wm_icon);
  1372 			video->wm_icon = NULL;
  1372 			video->wm_icon = NULL;
  1373 		}
  1373 		}
  1374 
  1374 
  1375 		/* Finish cleaning up video subsystem */
  1375 		/* Finish cleaning up video subsystem */
  1376 		video->free(this);
  1376 		video->free(this);
  1666 	SDL_VideoDevice *this  = current_video;
  1666 	SDL_VideoDevice *this  = current_video;
  1667 
  1667 
  1668 	if ( video ) {
  1668 	if ( video ) {
  1669 		if ( title ) {
  1669 		if ( title ) {
  1670 			if ( video->wm_title ) {
  1670 			if ( video->wm_title ) {
  1671 				free(video->wm_title);
  1671 				SDL_free(video->wm_title);
  1672 			}
  1672 			}
  1673 			video->wm_title = (char *)malloc(strlen(title)+1);
  1673 			video->wm_title = (char *)SDL_malloc(SDL_strlen(title)+1);
  1674 			if ( video->wm_title != NULL ) {
  1674 			if ( video->wm_title != NULL ) {
  1675 				strcpy(video->wm_title, title);
  1675 				SDL_strcpy(video->wm_title, title);
  1676 			}
  1676 			}
  1677 		}
  1677 		}
  1678 		if ( icon ) {
  1678 		if ( icon ) {
  1679 			if ( video->wm_icon ) {
  1679 			if ( video->wm_icon ) {
  1680 				free(video->wm_icon);
  1680 				SDL_free(video->wm_icon);
  1681 			}
  1681 			}
  1682 			video->wm_icon = (char *)malloc(strlen(icon)+1);
  1682 			video->wm_icon = (char *)SDL_malloc(SDL_strlen(icon)+1);
  1683 			if ( video->wm_icon != NULL ) {
  1683 			if ( video->wm_icon != NULL ) {
  1684 				strcpy(video->wm_icon, icon);
  1684 				SDL_strcpy(video->wm_icon, icon);
  1685 			}
  1685 			}
  1686 		}
  1686 		}
  1687 		if ( (title || icon) && (video->SetCaption != NULL) ) {
  1687 		if ( (title || icon) && (video->SetCaption != NULL) ) {
  1688 			video->SetCaption(this, video->wm_title,video->wm_icon);
  1688 			video->SetCaption(this, video->wm_title,video->wm_icon);
  1689 		}
  1689 		}
  1771 	if ( icon && video->SetIcon ) {
  1771 	if ( icon && video->SetIcon ) {
  1772 		/* Generate a mask if necessary, and create the icon! */
  1772 		/* Generate a mask if necessary, and create the icon! */
  1773 		if ( mask == NULL ) {
  1773 		if ( mask == NULL ) {
  1774 			int mask_len = icon->h*(icon->w+7)/8;
  1774 			int mask_len = icon->h*(icon->w+7)/8;
  1775 			int flags = 0;
  1775 			int flags = 0;
  1776 			mask = (Uint8 *)malloc(mask_len);
  1776 			mask = (Uint8 *)SDL_malloc(mask_len);
  1777 			if ( mask == NULL ) {
  1777 			if ( mask == NULL ) {
  1778 				return;
  1778 				return;
  1779 			}
  1779 			}
  1780 			memset(mask, ~0, mask_len);
  1780 			SDL_memset(mask, ~0, mask_len);
  1781 			if ( icon->flags & SDL_SRCCOLORKEY ) flags |= 1;
  1781 			if ( icon->flags & SDL_SRCCOLORKEY ) flags |= 1;
  1782 			if ( icon->flags & SDL_SRCALPHA ) flags |= 2;
  1782 			if ( icon->flags & SDL_SRCALPHA ) flags |= 2;
  1783 			if( flags ) {
  1783 			if( flags ) {
  1784 				CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  1784 				CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
  1785 			}
  1785 			}
  1786 			video->SetIcon(video, icon, mask);
  1786 			video->SetIcon(video, icon, mask);
  1787 			free(mask);
  1787 			SDL_free(mask);
  1788 		} else {
  1788 		} else {
  1789 			video->SetIcon(this, icon, mask);
  1789 			video->SetIcon(this, icon, mask);
  1790 		}
  1790 		}
  1791 	}
  1791 	}
  1792 }
  1792 }