/** * \brief Calls the on_created() method of a Lua equipment item. * * Does nothing if the method is not defined. * * \param item An equipment item. */ void LuaContext::item_on_created(EquipmentItem& item) { if (!userdata_has_field(item, "on_created")) { return; } push_item(l, item); on_created(); lua_pop(l, 1); }
void WindowSDL2::create( const window_config &_config, AutoGCRoot* _on_created ) { //store these first created_handler = _on_created; //assign it now so we take a copy from the const config = _config; //then try init sdl video system int err = snow::window::init_sdl(); if (err == -1) { snow::log(1, "/ snow / could not initialize Video for SDL : %s\n", SDL_GetError()); on_created( false ); return; } //then create flags for the given config int request_flags = 0; int real_flags = 0; request_flags |= SDL_WINDOW_OPENGL; if(config.resizable) { request_flags |= SDL_WINDOW_RESIZABLE; } if(config.borderless) { request_flags |= SDL_WINDOW_BORDERLESS; } if(config.fullscreen) { request_flags |= SDL_WINDOW_FULLSCREEN; } //SDL_WINDOW_FULLSCREEN_DESKTOP; //opengl specifics, these all need to be set before create window SDL_GL_SetAttribute(SDL_GL_RED_SIZE, config.red_bits); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, config.green_bits); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, config.blue_bits); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, config.alpha_bits); if(config.depth_bits > 0) { SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, config.depth_bits ); } if(config.stencil_bits > 0) { SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, config.stencil_bits ); } SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); #ifdef SNOW_GLES SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); #endif if(config.antialiasing > 0) { SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1 ); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, config.antialiasing ); } //now actually try and create a window window = SDL_CreateWindow( config.title.c_str(), config.x, config.y, config.width, config.height, request_flags ); //#21 proves this false. need to adjust. //based on the code in SDL, the only reasons this can fail is // - no opengl at all (and we will always be requesting it so if that fails its game over) // - unable to load gl functions from library (same as no gl then) // - run out of memory to allocate a window (extremely unlikely but also game over) // - platform specific window failure, also unavoidable if( !window ) { snow::log(1, "/ snow / window failed to create for platform, cannot recover : %s\n", SDL_GetError()); on_created( false ); return; } //!window SDL_GetWindowPosition( window, &config.x, &config.y ); SDL_GetWindowSize( window, &config.width, &config.height ); id = SDL_GetWindowID(window); //now try creating the GL context, //if one doesn't already exist if(!snow_gl_context) { snow::log(2, "/ snow / attempting to create a GL context..."); snow_gl_context = SDL_GL_CreateContext(window); if( !snow_gl_context ) { snow::log(1, "/ snow / failed to create GL context for windowing (window id %d): %s\n", id, SDL_GetError() ); snow::log(1, "/ snow / trying again in a safer mode (no AA)"); //try without AA as this is a common cause of problems SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); //undo the local config for the aa, so it's sent back as off config.antialiasing = 0; //try again snow_gl_context = SDL_GL_CreateContext(window); if(!snow_gl_context) { snow::log(1, "/ snow / failed to create GL context without AA (window id %d): %s\n", id, SDL_GetError() ); //if that fails, we try and run a diagnostic test //against no stencil / depth buffer, so we can log more useful information SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0 ); snow_gl_context = SDL_GL_CreateContext(window); //if this succeeds we can at least log that there was a misconfigured depth/stencil buffer if(snow_gl_context) { snow::log(1, "/ snow / diagnostic test with no stencil/depth passed, meaning your stencil/depth bit combo is invalid (requested stencil:%d, depth:%d)\n", config.stencil_bits, config.depth_bits ); } else { snow::log(1, "/ snow / diagnostic test with no stencil/depth failed as well %s\n", SDL_GetError() ); } on_created( false ); return; } } //if we end up with a context if(snow_gl_context) { //update the window config flags to what //SDL has actually given us in return int actual_aa = config.antialiasing; int actual_depth = config.depth_bits; int actual_stencil = config.stencil_bits; int actual_red = config.red_bits; int actual_blue = config.blue_bits; int actual_green = config.green_bits; int actual_alpha = config.alpha_bits; SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &actual_aa); SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &actual_red); SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &actual_green); SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &actual_blue); SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &actual_alpha); SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &actual_depth); SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &actual_stencil); config.antialiasing = actual_aa; config.red_bits = actual_red; config.green_bits = actual_green; config.blue_bits = actual_blue; config.alpha_bits = actual_alpha; config.depth_bits = actual_depth; config.stencil_bits = actual_stencil; snow::render::set_context_attributes( actual_red, actual_green, actual_blue, actual_alpha, actual_depth, actual_stencil, actual_aa ); snow::log(2, "/ snow / success in creating GL context for window %d\n", id); } #ifdef NATIVE_TOOLKIT_GLEW int err = glewInit(); if(err != 0) { snow::log(1, "/ snow / failed to init glew?! %s\n", glewGetErrorString(err)); on_created( false ); return; } else { snow::log(2, "/ snow / GLEW init ok"); } #endif //NATIVE_TOOLKIT_GLEW } //!snow_gl_context //on iOS we need to intercept the loop #ifdef IPHONE snow::log(1, "/ snow / requesting main loop for iOS"); SDL_iPhoneSetAnimationCallback(window, 1, snow::core::loop, NULL); #endif //IPHONE #ifdef HX_WINDOWS snow::platform::window::load_icon( window ); #endif on_created( true ); } //WindowSDL2::create