41#include <SDL3/SDL_clipboard.h>
42#include <SDL3/SDL_init.h>
44#define STB_IMAGE_WRITE_IMPLEMENTATION
47#define kOOLogUnconvertedNSLog @"unclassified.MyOpenGLView"
49extern int SaveEXRSnapshot(
const char* outfilename,
int width,
int height,
const float* rgb);
53@interface MyOpenGLView (OOPrivate)
61 SDL_DisplayID displayId = 0;
63 displayId = SDL_GetDisplayForWindow(
window);
66 SDL_DisplayID* displayIds = SDL_GetDisplays(&displayCount);
68 displayId = displayIds[0];
70 OOLog(
@"sdl.display_id",
@"Could not get list of displays. Error was: %s", SDL_GetError());
80 int nativeDisplayWidth = 1024;
81 int nativeDisplayHeight = 768;
85 if (displayId && SDL_GetDisplayUsableBounds(displayId, &boundsRect)) {
86 nativeDisplayWidth = boundsRect.w;
87 nativeDisplayHeight = boundsRect.h;
88 OOLog(
@"display.mode.list.native",
@"Native display resolution detected: %d x %d", nativeDisplayWidth, nativeDisplayHeight);
90 OOLog(
@"display.mode.list.native.failed",
@"%@",
@"SDL_GetWMInfo failed, defaulting to 1024x768 for native size");
93 [mode setValue:[
NSNumber numberWithInt:nativeDisplayWidth] forKey:kOODisplayWidth];
94 [mode setValue:[
NSNumber numberWithInt:nativeDisplayHeight] forKey:kOODisplayHeight];
95 [mode setValue:[
NSNumber numberWithInt:0] forKey:kOODisplayRefreshRate];
97 return [mode autorelease];
102 NSString* caption = [
NSString stringWithFormat:@"Oolite v%@ by %@ - %@", @OO_VERSION_FULL, @OO_BUILDER, @OO_BUILD_DATE];
103 return [[
caption retain] autorelease];
106- (void)createWindowWithSize:(NSSize)size
112 SDL_GL_SetAttribute(SDL_GL_FLOATBUFFERS, 1);
121 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
122 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
132 if ([prefs oo_boolForKey:
@"anti-aliasing" defaultValue:NO]) {
133 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
134 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
138 Uint32 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_HIGH_PIXEL_DENSITY;
142 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
146 SDL_PropertiesID props = SDL_CreateProperties();
147 SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, [windowCaption UTF8String]);
148 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER,
size.width);
149 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER,
size.height);
150 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER, windowFlags);
153 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_CENTERED);
154 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_CENTERED);
156 window = SDL_CreateWindowWithProperties(props);
158 OOLog(
@"display.initGL",
@"%@",
@"Trying 8-bpcc, 24-bit depth buffer");
159 SDL_GL_SetAttribute(SDL_GL_FLOATBUFFERS, 0);
160 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
161 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
162 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
163 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
164 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
165 window = SDL_CreateWindowWithProperties(props);
170 OOLog(
@"display.initGL",
@"%@",
@"Trying 5-bpcc, 16-bit depth buffer");
171 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
172 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
173 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
174 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
176 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
177 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
178 window = SDL_CreateWindowWithProperties(props);
182 SDL_DestroyProperties(props);
185 const char* errStr = SDL_GetError();
186 OOLogERR(
@"display.mode.error",
@"Could not create window: %s", errStr);
193 OOLog(
@"sdl.create_context",
@"Could not make OpenGL context current: %s", SDL_GetError());
200 OOLog(
@"sdl.create_context",
@"%@",
@"Could not create OpenGL context");
205 SDL_Surface* surface = SDL_GetWindowSurface(
window);
206 if (!SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB_LINEAR)) {
207 OOLogWARN(
@"sdl.use_edr_surface",
@"%@ %s",
@"Failed to set SDR linear surface - falling back to SDR. Error was:", SDL_GetError());
208 SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
215 OOLog(
@"sdl.create_context",
@"Could not make OpenGL context current: %s", SDL_GetError());
222 SDL_PropertiesID windowPropertiesId = SDL_GetWindowProperties(
window);
223 windowHandle = SDL_GetPointerProperty(windowPropertiesId, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
225 OOLog(
@"sdl.window_handle",
@"%@",
@"Failed to retrieve window handle");
229 atDesktopResolution = YES;
236 NSString* imagesDir = [[[
NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images"];
237 SDL_Surface* icon = SDL_LoadBMP([[imagesDir stringByAppendingPathComponent:
@"WMicon.bmp"] UTF8String]);
240 const SDL_PixelFormatDetails* pixelFormat = SDL_GetPixelFormatDetails(icon->format);
241 const SDL_Palette* palette = SDL_GetSurfacePalette(icon);
242 Uint32 colorkey = SDL_MapRGB(pixelFormat, palette, 128, 0, 128);
243 SDL_SetSurfaceColorKey(icon, YES, colorkey);
244 SDL_SetWindowIcon(
window, icon);
246 SDL_DestroySurface(icon);
252 _hdrMaxBrightness = [
prefs oo_floatForKey:@"hdr-max-brightness" defaultValue:1000.0f];
253 _hdrPaperWhiteBrightness = [
prefs oo_floatForKey:@"hdr-paperwhite-brightness" defaultValue:200.0f];
254 _hdrToneMapper =
OOHDRToneMapperFromString([prefs oo_stringForKey:
@"hdr-tone-mapper" defaultValue:
@"OOHDR_TONEMAPPER_ACES_APPROX"]);
267 OOLog(
@"display.initGL",
@"%@",
@"Achieved color / depth buffer sizes (bits):");
268 SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &testAttrib);
269 OOLog(
@"display.initGL",
@"Red: %d", testAttrib);
270 SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &testAttrib);
271 OOLog(
@"display.initGL",
@"Green: %d", testAttrib);
272 SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &testAttrib);
273 OOLog(
@"display.initGL",
@"Blue: %d", testAttrib);
274 SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &testAttrib);
275 OOLog(
@"display.initGL",
@"Alpha: %d", testAttrib);
276 SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &testAttrib);
277 OOLog(
@"display.initGL",
@"Depth Buffer: %d", testAttrib);
279 SDL_GL_GetAttribute(SDL_GL_FLOATBUFFERS, &testAttrib);
280 OOLog(
@"display.initGL",
@"Pixel type is float : %d", testAttrib);
282 SDL_PixelFormat format = SDL_GetWindowPixelFormat(
window);
283 OOLog(
@"display.initGL",
@"Window Pixel Format: %s", SDL_GetPixelFormatName(format));
289 if (
vSyncPreference && (!SDL_GL_GetSwapInterval(&hasVsync) || !hasVsync)) {
293 OOLogWARN(
@"display.initGL",
@"Could not enable V-Sync. Please check that your graphics driver supports the %@_swap_control extension.",
296 OOLog(
@"display.initGL",
@"%@",
@"V-Sync set");
300 SDL_GetWindowSizeInPixels(
window, &width, &height);
301 bounds.size.width = width;
302 bounds.size.height = height;
311 NSString* cmdLineArgsStr =
@"Startup command: ";
320 NSArray* arguments =
nil;
321 NSEnumerator* argEnum =
nil;
323 BOOL noSplashArgFound = NO;
334 for (argEnum = [arguments objectEnumerator]; (arg = [
argEnum nextObject]);) {
335 if ([arg isEqual:
@"-nosplash"] || [arg isEqual:
@"--nosplash"]) {
337 noSplashArgFound = YES;
338 }
else if (([arg isEqual:
@"-splash"] || [arg isEqual:
@"--splash"]) && !noSplashArgFound) {
343 if ([arg isEqual:
@"-novsync"] || [arg isEqual:
@"--novsync"])
346 if ([arg isEqual:
@"-hdr"])
350 cmdLineArgsStr = [
cmdLineArgsStr stringByAppendingFormat:@"%@ ", arg];
353 OOLog(
@"process.args",
@"%@", cmdLineArgsStr);
355#if OOLITE_SPEECH_SYNTH
357 if (!SDL_getenv(
"ESPEAK_DATA_PATH")) {
358 espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 100, [[
ResourceManager builtInPath] UTF8String], 0);
360 espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 100, NULL, 0);
369 OOLog(
@"sdl.init",
@"%@",
@"initialising SDL");
370 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD)) {
371 OOLog(
@"sdl.init.failed",
@"Unable to init SDL: %s\n", SDL_GetError());
393 OOLog(
@"sound.init",
@"%@",
@"Sound system disabled.");
397 OOLog(
@"display.mode.list",
@"%@",
@"CREATING MODE LIST");
431 if (MessageBox(NULL,
"No primary display in HDR mode was detected.\n\n"
432 "If you continue, graphics will not be rendered as intended.\n"
433 "Click OK to launch anyway, or Cancel to exit.",
434 "oolite.exe - HDR requested",
435 MB_OKCANCEL | MB_ICONWARNING)
443 if (!replaceSplashWindow) {
444 SDL_SetWindowResizable(
window,
true);
445 SDL_SetWindowBordered(
window,
true);
451 SDL_SetWindowFullscreen(
window,
true);
458 SDL_Window* previousWindow =
window;
461 SDL_DestroyWindow(previousWindow);
467 OOLog(
@"sdl.create_context",
@"Could not make OpenGL context current: %s", SDL_GetError());
480 SDL_Event dummyEvent;
481 while (SDL_PollEvent(&dummyEvent)) {
493- (void)updateGLSize:(NSSize)size
498 if (
bounds.size.width /
bounds.size.height > 4.0 / 3.0) {
520 OOGL(glClearColor(0.0, 0.0, 0.0, 0.0));
521 OOGL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
522 OOGL(glClearDepth(1.0));
523 OOGL(glDepthFunc(GL_LESS));
526 [UNIVERSE setLighting];
528 GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
529 GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
530 GLfloat stars_ambient[] = { 0.25, 0.2, 0.25, 1.0 };
532 OOGL(glLightfv(GL_LIGHT1, GL_AMBIENT, black));
533 OOGL(glLightfv(GL_LIGHT1, GL_SPECULAR, white));
534 OOGL(glLightfv(GL_LIGHT1, GL_DIFFUSE, white));
535 OOGL(glLightfv(GL_LIGHT1, GL_POSITION, black));
536 OOGL(glLightModelfv(GL_LIGHT_MODEL_AMBIENT, stars_ambient));
539 if ([extMgr usePointSmoothing]) {
540 OOGL(glEnable(GL_POINT_SMOOTH));
543 if ([extMgr useLineSmoothing]) {
544 OOGL(glEnable(GL_LINE_SMOOTH));
547 OOGL(glDisable(GL_NORMALIZE));
548 OOGL(glDisable(GL_RESCALE_NORMAL));
550 OOGL(glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR));
553- (void)initialiseGLWithSize:(NSSize)v_size
561 OOLog(
@"display.initGL",
@"Requested a new surface of %d x %d, %@.", (
int)
viewSize.width, (
int)
viewSize.height,
564 SDL_GL_SwapWindow(
window);
570 SDL_SetWindowBordered(
window, YES);
575 LONG currentWindowStyle = GetWindowLong(windowHandle, GWL_STYLE);
576 currentWindowStyle &= ~WS_POPUP;
577 SetWindowLong(windowHandle, GWL_STYLE, currentWindowStyle);
581 int pixelWidth, pixelHeight;
582 SDL_GetWindowSizeInPixels(
window, &pixelWidth, &pixelHeight);
584 [
self updateGLSize:NSMakeSize(pixelWidth, pixelHeight)];
586 OOLog(
@"display.initGL",
587 @"Created a new surface of %d x %d, %@.",
596 SDL_GL_SwapWindow(
window);
604 [typedString release];
607 [screenSizes release];
610 SDL_DestroyWindow(
window);
618 [keyMappings_normal release];
621 [keyMappings_shifted release];
678- (void)setFullScreenMode:(BOOL)fsm
683 [[NSUserDefaults standardUserDefaults]
685 forKey:@"fullscreen"];
686 [[NSUserDefaults standardUserDefaults] synchronize];
689- (void)toggleScreenMode
691 [
self setFullScreenMode:!fullScreen];
693 [
self initialiseGLWithSize:[
self modeAsSize:currentSize]];
695 [
self initialiseGLWithSize:currentWindowSize];
697 SDL_HideWindow(window);
698 SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
699 SDL_ShowWindow(window);
704- (void)setDisplayMode:(
int)mode fullScreen:(BOOL)fsm
706 [
self setFullScreenMode:fsm];
709 [
self initialiseGLWithSize:[
self modeAsSize:mode]];
712- (void)setScreenSize:(
int)sizeIndex
714 currentSize = sizeIndex;
716 [
self initialiseGLWithSize:[
self modeAsSize:currentSize]];
719- (NSMutableArray*)getScreenSizeArray
724- (NSSize)modeAsSize:(
int)sizeIndex
726 NSDictionary*
mode = [screenSizes objectAtIndex:sizeIndex];
736 [UNIVERSE drawUniverse];
738 glClearColor(0.0, 0.0, 0.0, 0.0);
739 glClear(GL_COLOR_BUFFER_BIT);
742 SDL_GL_SwapWindow(
window);
751 SDL_Surface* image = NULL;
756 image = SDL_LoadBMP([[imagesDir stringByAppendingPathComponent:
@"splash.bmp"] UTF8String]);
759 SDL_DestroySurface(image);
760 OOLogWARN(
@"sdl.gameStart",
@"%@",
@"image 'splash.bmp' not found!");
772 float pixelDensity = SDL_GetWindowPixelDensity(
window);
773 if (pixelDensity == 0.0f) {
777 glViewport(0, 0, dest.w * pixelDensity, dest.h * pixelDensity);
779 glEnable(GL_TEXTURE_2D);
780 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
781 glClear(GL_COLOR_BUFFER_BIT);
791 GLenum texture_format;
795 const SDL_PixelFormatDetails* pixelFormat = SDL_GetPixelFormatDetails(image->format);
796 nOfColors = pixelFormat->bytes_per_pixel;
799 if (pixelFormat->Rmask == 0x000000ff)
800 texture_format = GL_RGBA;
802 texture_format = GL_BGRA;
803 }
else if (nOfColors == 3)
805 if (pixelFormat->Rmask == 0x000000ff)
806 texture_format = GL_RGB;
808 texture_format = GL_BGR;
810 SDL_DestroySurface(image);
811 OOLog(
@"Sdl.GameStart",
@"%@",
@"----- Encoding error within image 'splash.bmp'");
816 glGenTextures(1, &texture);
817 glBindTexture(GL_TEXTURE_2D, texture);
820 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
821 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
824 glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, image->w, image->h, 0,
825 texture_format, GL_UNSIGNED_BYTE, image->pixels);
827 glBindTexture(GL_TEXTURE_2D, texture);
842 SDL_GL_SwapWindow(
window);
847 SDL_DestroySurface(image);
849 glDeleteTextures(1, &texture);
851 glDisable(GL_TEXTURE_2D);
854 SDL_DestroySurface(image);
858- (void)getDisplayDimensions:(
unsigned*)width height:(
unsigned*)height
860 SDL_DisplayID displayID = SDL_GetDisplayForWindow(
window);
862 SDL_Rect displaybounds;
863 if (SDL_GetDisplayBounds(displayID, &displaybounds)) {
865 *width = (unsigned)displaybounds.w;
867 *height = (unsigned)displaybounds.h;
877 SDL_DisplayID windowDisplayID = SDL_GetDisplayForWindow(
window);
878 SDL_DisplayID primaryDisplayID = SDL_GetPrimaryDisplay();
880 return (windowDisplayID != 0 && windowDisplayID == primaryDisplayID);
883- (void)grabMouseInsideGameWindow:(BOOL)value
885 SDL_SetWindowMouseGrab(
window, value);
889- (void)stringToClipboard:(NSString*)stringToCopy
893 if (!SDL_SetClipboardText(clipboardText)) {
894 OOLog(
@"stringToClipboard.failed",
@"Failed to copy string %@ to clipboard: %s", stringToCopy, SDL_GetError());
901- (BOOL)atDesktopResolution
903 return atDesktopResolution;
911- (BOOL)isOutputDisplayHDREnabled
914 SDL_DisplayID displayID = SDL_GetPrimaryDisplay();
915 if (displayID == 0) {
916 OOLog(
@"gameView.isOutputDisplayHDREnabled",
@"Error! Failed to retrieve primary display ID: %s", SDL_GetError());
919 SDL_PropertiesID props = SDL_GetDisplayProperties(displayID);
921 OOLog(
@"gameView.isOutputDisplayHDREnabled",
@"Error! Failed to get display properties: %s", SDL_GetError());
924 result = SDL_GetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN,
false);
925 OOLog(
@"gameView.isOutputDisplayHDREnabled",
@"HDR display output requested - checking availability: %@", result ?
@"YES" :
@"NO");
929- (float)hdrMaxBrightness
931 return _hdrMaxBrightness;
934- (void)setHDRMaxBrightness:(
float)newMaxBrightness
940 _hdrMaxBrightness = newMaxBrightness;
942 [[NSUserDefaults standardUserDefaults] setFloat:_hdrMaxBrightness forKey:@"hdr-max-brightness"];
945- (float)hdrPaperWhiteBrightness
947 return _hdrPaperWhiteBrightness;
950- (void)setHDRPaperWhiteBrightness:(
float)newPaperWhiteBrightness
956 _hdrPaperWhiteBrightness = newPaperWhiteBrightness;
958 [[NSUserDefaults standardUserDefaults] setFloat:_hdrPaperWhiteBrightness forKey:@"hdr-paperwhite-brightness"];
963 return _hdrToneMapper;
972 _hdrToneMapper = newToneMapper;
1008- (void)adjustColorSaturation:(
float)colorSaturationAdjustment;
1014- (BOOL)snapShot:(NSString*)filename
1016 BOOL snapShotOK = YES;
1017 SDL_Surface* tmpSurface;
1021 OOGL(glReadBuffer(GL_FRONT));
1025 NSString* originalDirectory = [[
NSFileManager defaultManager] currentDirectoryPath];
1030 static unsigned imageNo = 0;
1031 unsigned tmpImageNo = 0;
1032 NSString* pathToPic =
nil;
1033 NSString* baseName =
@"oolite";
1035#if SNAPSHOTS_PNG_FORMAT
1036 NSString* extension =
@".png";
1038 NSString* extension =
@".bmp";
1043 pathToPic = [filename stringByAppendingString:extension];
1045 tmpImageNo = imageNo;
1048 if (withFilename && [[NSFileManager defaultManager] fileExistsAtPath:pathToPic]) {
1049 OOLog(
@"screenshot.filenameExists",
@"Snapshot \"%@%@\
" already exists - adding numerical sequence.", pathToPic, extension);
1053 if (pathToPic ==
nil) {
1056 pathToPic = [
NSString stringWithFormat:@"%@-%03d%@", baseName, tmpImageNo, extension];
1057 }
while ([[NSFileManager defaultManager] fileExistsAtPath:pathToPic]);
1060 if (!withFilename) {
1061 imageNo = tmpImageNo;
1066 SDL_Surface* surface = NULL;
1068 int pixelWidth, pixelHeight;
1069 SDL_GetWindowSizeInPixels(
window, &pixelWidth, &pixelHeight);
1070 surface = SDL_CreateSurface(pixelWidth, pixelHeight, SDL_PIXELFORMAT_ARGB8888);
1073 SDL_Surface* surface = SDL_GetWindowSurface(
window);
1075 OOLog(
@"screenshot",
@"Saving screen shot \"%@\
" (%u x %u pixels).", pathToPic, surface->w, surface->h);
1077 int pitch = surface->pitch;
1078 unsigned char* pixls = malloc(pitch * surface->h);
1083 glPixelStorei(GL_PACK_ALIGNMENT, 1);
1085 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1086 for (
y = surface->h - 1, off = 0;
y >= 0;
y--, off += pitch) {
1087 glReadPixels(0,
y, surface->w, 1, GL_BGRA, GL_UNSIGNED_BYTE, pixls + off);
1090 tmpSurface = SDL_CreateSurfaceFrom(surface->w, surface->h, surface->format, pixls, surface->pitch);
1091#if SNAPSHOTS_PNG_FORMAT
1092 if (!SDL_SavePNG(tmpSurface, [pathToPic UTF8String])) {
1093 OOLog(
@"screenshotPNG",
@"Failed to save %@", pathToPic);
1097 if (!SDL_SaveBMP(tmpSurface, [pathToPic UTF8String])) {
1098 OOLog(
@"screenshotBMP",
@"Failed to save %@", pathToPic);
1102 SDL_DestroySurface(tmpSurface);
1107 NSString* fileExtension = [[
NSUserDefaults standardUserDefaults] oo_stringForKey:@"hdr-snapshot-format" defaultValue:SNAPSHOTHDR_EXTENSION_DEFAULT];
1110 if (![[fileExtension substringToIndex:1] isEqual:
@"."])
1118 NSString* pathToPicHDR = [
pathToPic stringByReplacingOccurrencesOfString:@".png" withString:fileExtension];
1119 OOLog(
@"screenshot",
@"Saving screen shot \"%@\
" (%u x %u pixels).", pathToPicHDR, surface->w, surface->h);
1120 GLfloat* pixlsf = (GLfloat*)malloc(pitch * surface->h *
sizeof(GLfloat));
1121 for (
y = surface->h - 1, off = 0;
y >= 0;
y--, off += pitch) {
1122 glReadPixels(0,
y, surface->w, 1, GL_RGB, GL_FLOAT, pixlsf + off);
1127 OOLog(
@"screenshotHDR",
@"Failed to save %@", pathToPicHDR);
1135 SDL_DestroySurface(surface);
1136 OOGL(glReadBuffer(GL_BACK));
1140 [[
NSFileManager defaultManager] changeCurrentDirectoryPath:originalDirectory];
1148 SDL_DisplayMode** modes;
1149 NSMutableDictionary*
mode;
1157 [screenSizes addObject:mode];
1159 int displayModeCount;
1160 modes = SDL_GetFullscreenDisplayModes(displayId, &displayModeCount);
1161 if (!displayModeCount) {
1162 OOLog(
@"display.mode.list.none",
@"%@",
@"SDL didn't return any screen modes");
1166 for (i = 0; i < displayModeCount; i++) {
1168 [mode setValue:[
NSNumber numberWithInt:(int)modes[
i]->w]
1169 forKey:kOODisplayWidth];
1170 [mode setValue:[
NSNumber numberWithInt:(int)modes[
i]->h]
1171 forKey:kOODisplayHeight];
1172 [mode setValue:[
NSNumber numberWithFloat:(int)modes[
i]->refresh_rate]
1173 forKey:kOODisplayRefreshRate];
1175 [screenSizes addObject:mode];
1176 OOLog(
@"display.mode.list",
@"Added res %d x %d", modes[i]->w, modes[i]->h);
1183- (void)saveWindowSize:(NSSize)windowSize
1185 NSUserDefaults* defaults = [
NSUserDefaults standardUserDefaults];
1186 [
defaults setInteger:(int)windowSize.width forKey:@"window_width"];
1187 [
defaults setInteger:(int)windowSize.height forKey:@"window_height"];
1194 NSUserDefaults* defaults = [
NSUserDefaults standardUserDefaults];
1195 if ([defaults objectForKey:
@"window_width"] && [defaults objectForKey:
@"window_height"]) {
1196 windowSize = NSMakeSize([defaults integerForKey:
@"window_width"],
1197 [defaults integerForKey:
@"window_height"]);
1208 int width = 0, height = 0, refresh = 0;
1211 NSArray* cmdline_arguments = [[
NSProcessInfo processInfo] arguments];
1213 NSUserDefaults* userDefaults = [
NSUserDefaults standardUserDefaults];
1214 if ([userDefaults objectForKey:
@"display_width"])
1216 if ([userDefaults objectForKey:
@"display_height"])
1217 height = [
userDefaults integerForKey:@"display_height"];
1218 if ([userDefaults objectForKey:
@"display_refresh"])
1219 refresh = [
userDefaults integerForKey:@"display_refresh"];
1220 if ([userDefaults objectForKey:
@"fullscreen"])
1227 if ([[cmdline_arguments objectAtIndex:i] isEqual:
@"-fullscreen"])
1229 if ([[cmdline_arguments objectAtIndex:i] isEqual:
@"-windowed"])
1233 if (width && height) {
1240- (
int)findDisplayModeForWidth:(
unsigned int)d_width Height:(
unsigned int)d_height Refresh:(
unsigned int)d_refresh
1244 unsigned int modeWidth, modeHeight, modeRefresh;
1246 modeCount = [screenSizes count];
1248 for (i = 0; i < modeCount; i++) {
1249 mode = [screenSizes objectAtIndex:i];
1250 modeWidth = [[mode objectForKey:kOODisplayWidth] intValue];
1251 modeHeight = [[mode objectForKey:kOODisplayHeight] intValue];
1252 modeRefresh = [[mode objectForKey:kOODisplayRefreshRate] intValue];
1253 if ((modeWidth == d_width) && (modeHeight == d_height) && (modeRefresh == d_refresh)) {
1254 OOLog(
@"display.mode.found",
@"Found mode %@",
mode);
1259 OOLog(
@"display.mode.found.failed",
@"Failed to find mode: width=%d height=%d refresh=%d", d_width, d_height, d_refresh);
1260 OOLog(
@"display.mode.found.failed.list",
@"Contents of list: %@",
screenSizes);
1266 NSDictionary*
mode = [screenSizes objectAtIndex:currentSize];
1272 OOLog(
@"display.mode.unknown",
@"%@",
@"Screen size unknown!");
1278 NSDictionary*
mode = [screenSizes objectAtIndex:currentSize];
1279 return [[mode retain] autorelease];
1282- (void)setFov:(
float)value fromFraction:(BOOL)fromFraction
1284 _fov = fromFraction ? value : tan((value / 2) *
M_PI / 180);
1287- (float)fov:(BOOL)inFraction
1289 return inFraction ?
_fov : 2 * atan(
_fov) * 180 /
M_PI;
1297- (void)setMsaa:(BOOL)newMsaa
1310 return 0 != (SDL_GetModState() & (SDL_KMOD_LSHIFT | SDL_KMOD_RSHIFT));
1314 return 0 != (GetKeyState(VK_SHIFT) & 0x100);
1319- (void)dumpRGBAToFileNamed:(NSString*)name
1320 bytes:(uint8_t*)bytes
1321 width:(NSUInteger)width
1322 height:(NSUInteger)height
1323 rowBytes:(NSUInteger)rowBytes
1325 if (name ==
nil || bytes == NULL || width == 0 || height == 0 || rowBytes < width * 4)
1329 NSString* dumpFile = [[
NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR] stringByAppendingPathComponent:@SNAPSHOTDIR];
1330 dumpFile = [
dumpFile stringByAppendingPathComponent:[
NSString stringWithFormat:@"%@.png", name]];
1332 SDL_Surface* tmpSurface = SDL_CreateSurfaceFrom(width, height, SDL_PIXELFORMAT_RGBA32, bytes, rowBytes);
1333 SDL_SavePNG(tmpSurface, [dumpFile UTF8String]);
1334 SDL_DestroySurface(tmpSurface);
1337- (void)dumpRGBToFileNamed:(NSString*)name
1338 bytes:(uint8_t*)bytes
1339 width:(NSUInteger)width
1340 height:(NSUInteger)height
1341 rowBytes:(NSUInteger)rowBytes
1343 if (name ==
nil || bytes == NULL || width == 0 || height == 0 || rowBytes < width * 3)
1347 NSString* dumpFile = [[
NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR] stringByAppendingPathComponent:@SNAPSHOTDIR];
1348 dumpFile = [
dumpFile stringByAppendingPathComponent:[
NSString stringWithFormat:@"%@.png", name]];
1350 SDL_Surface* tmpSurface = SDL_CreateSurfaceFrom(width, height, SDL_PIXELFORMAT_RGB24, bytes, rowBytes);
1351 SDL_SavePNG(tmpSurface, [dumpFile UTF8String]);
1352 SDL_DestroySurface(tmpSurface);
1355- (void)dumpGrayToFileNamed:(NSString*)name
1356 bytes:(uint8_t*)bytes
1357 width:(NSUInteger)width
1358 height:(NSUInteger)height
1359 rowBytes:(NSUInteger)rowBytes
1361 if (name ==
nil || bytes == NULL || width == 0 || height == 0 || rowBytes < width)
1365 NSString* dumpFile = [[
NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR] stringByAppendingPathComponent:@SNAPSHOTDIR];
1366 dumpFile = [
dumpFile stringByAppendingPathComponent:[
NSString stringWithFormat:@"%@.png", name]];
1368 SDL_Surface* tmpSurface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGBA32);
1369 for (
int y = 0;
y < height;
y++) {
1370 uint8_t* srcRow = bytes + rowBytes *
y;
1371 uint8_t* dstRow = (uint8_t*)tmpSurface->pixels +
y * tmpSurface->pitch;
1372 for (
int x = 0;
x < width;
x++) {
1373 dstRow[4 * x + 0] = srcRow[x];
1374 dstRow[4 * x + 1] = srcRow[x];
1375 dstRow[4 * x + 2] = srcRow[x];
1376 dstRow[4 * x + 3] =
'\xff';
1379 SDL_SavePNG(tmpSurface, [dumpFile UTF8String]);
1380 SDL_DestroySurface(tmpSurface);
1383- (void)dumpGrayAlphaToFileNamed:(NSString*)name
1384 bytes:(uint8_t*)bytes
1385 width:(NSUInteger)width
1386 height:(NSUInteger)height
1387 rowBytes:(NSUInteger)rowBytes
1389 if (name ==
nil || bytes == NULL || width == 0 || height == 0 || rowBytes < width * 2)
1393 NSString* dumpFile = [[
NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR] stringByAppendingPathComponent:@SNAPSHOTDIR];
1394 dumpFile = [
dumpFile stringByAppendingPathComponent:[
NSString stringWithFormat:@"%@.png", name]];
1396 SDL_Surface* tmpSurface = SDL_CreateSurfaceFrom(width, height, SDL_PIXELFORMAT_RGBA32, bytes, rowBytes);
1397 for (
int y = 0;
y < height;
y++) {
1398 uint8_t* srcRow = bytes + rowBytes *
y;
1399 uint8_t* dstRow = (uint8_t*)tmpSurface->pixels +
y * tmpSurface->pitch;
1400 for (
int x = 0;
x < width;
x++) {
1401 dstRow[4 * x + 0] = srcRow[2 * x];
1402 dstRow[4 * x + 1] = srcRow[2 * x];
1403 dstRow[4 * x + 2] = srcRow[2 * x];
1404 dstRow[4 * x + 3] = srcRow[2 * x + 1];
1407 SDL_SavePNG(tmpSurface, [dumpFile UTF8String]);
1408 SDL_DestroySurface(tmpSurface);
1411- (void)dumpRGBAToRGBFileNamed:(NSString*)rgbName
1412 andGrayFileNamed:(NSString*)grayName
1413 bytes:(uint8_t*)bytes
1414 width:(NSUInteger)width
1415 height:(NSUInteger)height
1416 rowBytes:(NSUInteger)rowBytes
1418 if ((rgbName ==
nil && grayName ==
nil) || bytes == NULL || width == 0 || height == 0 || rowBytes < width * 4)
1421 uint8_t *rgbBytes, *rgbPx, *grayBytes, *grayPx, *srcPx;
1423 BOOL trivalAlpha = YES;
1425 rgbPx = rgbBytes = malloc(width * height * 3);
1426 if (rgbBytes == NULL)
1429 grayPx = grayBytes = malloc(width * height);
1430 if (grayBytes == NULL) {
1435 for (
y = 0;
y < height;
y++) {
1436 srcPx = bytes + rowBytes *
y;
1438 for (
x = 0;
x < width;
x++) {
1439 *rgbPx++ = *srcPx++;
1440 *rgbPx++ = *srcPx++;
1441 *rgbPx++ = *srcPx++;
1442 trivalAlpha = trivalAlpha && ((*srcPx == 0xFF) || (*srcPx == 0x00));
1443 *grayPx++ = *srcPx++;
#define SNAPSHOTHDR_EXTENSION_HDR
@ OOHDR_TONEMAPPER_REINHARD
#define MAX_COLOR_SATURATION
@ OOSDR_TONEMAPPER_REINHARD
#define SNAPSHOTHDR_EXTENSION_DEFAULT
#define WINDOW_SIZE_DEFAULT_HEIGHT
#define MIN_HDR_MAXBRIGHTNESS
#define MIN_HDR_PAPERWHITE
#define WINDOW_SIZE_DEFAULT_WIDTH
#define SNAPSHOTHDR_EXTENSION_EXR
#define MAX_HDR_MAXBRIGHTNESS
#define MAX_HDR_PAPERWHITE
int SaveEXRSnapshot(const char *outfilename, int width, int height, const float *rgb)
OOSDRToneMapper OOSDRToneMapperFromString(NSString *string)
OOHDRToneMapper OOHDRToneMapperFromString(NSString *string)
OOINLINE float OOClamp_0_max_f(float value, float max) INLINE_CONST_FUNC
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define OOLogWARN(class, format,...)
#define OOLogERR(class, format,...)
#define OOLog(class, format,...)
void OOGLFrustum(double left, double right, double bottom, double top, double near, double far)
void OOGLResetProjection(void)
#define OOVerifyOpenGLState()
#define OOSetOpenGLState(STATE)
int SaveEXRSnapshot(const char *outfilename, int width, int height, const float *rgb)
void exitAppWithContext:(NSString *context)
void initialiseGLWithSize:(NSSize v_size)
void initKeyMappingData()
void createWindowWithSize:(NSSize size)
int bitsPerColorComponent
NSDictionary * keyMappings_shifted
BOOL isOutputDisplayHDREnabled()
NSDictionary * currentScreenMode()
int scancode2Unicode[NUM_KEYS]
enum StringInput allowingStringInput
int loadFullscreenSettings()
NSMutableString * typedString
NSMutableDictionary * getNativeSize()
float _mouseVirtualStickSensitivityFactor
void populateFullScreenModelist()
int findDisplayModeForWidth:Height:Refresh:(unsigned int d_width,[Height] unsigned int d_height,[Refresh] unsigned int d_refresh)
NSMutableArray * screenSizes
void updateGLSize:(NSSize size)
OOOpenGLMatrixManager * matrixManager
NSString * getWindowCaption()
NSPoint virtualJoystickPosition
BOOL isRunningOnPrimaryDisplayDevice()
NSDictionary * keyMappings_normal
OOOpenGLMatrixManager * getOpenGLMatrixManager()
NSTimeInterval timeSinceLastMouseWheel
NSTimeInterval timeIntervalAtLastClick
NSSize currentScreenSize()
void dumpGrayToFileNamed:bytes:width:height:rowBytes:(NSString *name,[bytes] uint8_t *bytes,[width] NSUInteger width,[height] NSUInteger height,[rowBytes] NSUInteger rowBytes)
OOSDRToneMapper sdrToneMapper()
SDL_DisplayID getDisplayId()
void dumpRGBToFileNamed:bytes:width:height:rowBytes:(NSString *name,[bytes] uint8_t *bytes,[width] NSUInteger width,[height] NSUInteger height,[rowBytes] NSUInteger rowBytes)
GameController * gameController
void setUpBasicOpenGLStateWithSize()
BOOL setStickHandlerClass:(Class aClass)
OOOpenGLExtensionManager * sharedManager()
void orthoLeft:right:bottom:top:near:far:(double l,[right] double r,[bottom] double b,[top] double t,[near] double n,[far] double f)
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data)