SDLSurfacePtr GLVideoSystem::make_screenshot() { GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); const int& viewport_x = viewport[0]; const int& viewport_y = viewport[1]; const int& viewport_width = viewport[2]; const int& viewport_height = viewport[3]; SDLSurfacePtr surface = SDLSurface::create_rgb(viewport_width, viewport_height); std::vector<char> pixels(3 * viewport_width * viewport_height); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(viewport_x, viewport_y, viewport_width, viewport_height, GL_RGB, GL_UNSIGNED_BYTE, pixels.data()); SDL_LockSurface(surface.get()); for (int i = 0; i < viewport_height; i++) { char* src = &pixels[3 * viewport_width * (viewport_height - i - 1)]; char* dst = (static_cast<char*>(surface->pixels)) + i * surface->pitch; memcpy(dst, src, 3 * viewport_width); } SDL_UnlockSurface(surface.get()); return surface; }
void LoadWindowIcon() { SDLSurfacePtr surface = LoadSurfaceFromFile("icons/badge.png"); if (surface) { SDL_WM_SetIcon(surface.Get(), 0); } }
Face::Face(Context *context, Uint32 flags, Uint32 seed) : Single(context), m_preferredSize(INT_MAX) { if (!seed) seed = time(0); m_flags = flags; m_seed = seed; SDLSurfacePtr faceim = SDLSurfacePtr::WrapNew(SDL_CreateRGBSurface(SDL_SWSURFACE, FaceParts::FACE_WIDTH, FaceParts::FACE_HEIGHT, 24, 0xff, 0xff00, 0xff0000, 0)); FaceParts::FaceDescriptor face; switch (flags & GENDER_MASK) { case RAND: face.gender = -1; break; case MALE: face.gender = 0; break; case FEMALE: face.gender = 1; break; default: assert(0); break; } FaceParts::PickFaceParts(face, m_seed); FaceParts::BuildFaceImage(faceim.Get(), face, (flags & ARMOUR)); m_texture.reset(Graphics::TextureBuilder(faceim, Graphics::LINEAR_CLAMP, true, true).CreateTexture(GetContext()->GetRenderer())); if (!s_material) { Graphics::MaterialDescriptor matDesc; matDesc.textures = 1; s_material.Reset(GetContext()->GetRenderer()->CreateMaterial(matDesc)); } m_preferredSize = UI::Point(FaceParts::FACE_WIDTH, FaceParts::FACE_HEIGHT); SetSizeControlFlags(UI::Widget::PRESERVE_ASPECT); }
CTexture2DSharedPtr LoadFromDisk(const path &path)const { SDLSurfacePtr pSurface = LoadFileImage(path); const bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(pSurface->format->format); // Все изображения будем конвертировать в RGB или RGBA, // в зависимости от наличия альфа-канала в исходном изображении. const uint32_t requiredFormat = hasAlpha ? SDL_PIXELFORMAT_ABGR8888 : SDL_PIXELFORMAT_RGB24; if (pSurface->format->format != requiredFormat) { pSurface.reset(SDL_ConvertSurfaceFormat(pSurface.get(), requiredFormat, 0)); } // В системе координат OpenGL отсчёт идёт с нижней левой точки, // а не с верхней левой, поэтому переворачиваем изображение. CUtils::FlipSurfaceVertically(*pSurface); auto pTexture = std::make_shared<CTexture2D>(); pTexture->Bind(); pTexture->ApplyImageData(*pSurface); pTexture->ApplyTrilinearFilter(); pTexture->ApplyMaxAnisotropy(); pTexture->ApplyWrapMode(m_wrapS, m_wrapT); pTexture->GenerateMipmaps(); pTexture->Unbind(); return pTexture; }
void LoadWindowIcon() { // SDL doc says "Win32 icons must be 32x32". SDLSurfacePtr surface = LoadSurfaceFromFile("icons/badge32-8b.png"); if (surface) { SDL_WM_SetIcon(surface.Get(), 0); } }
static void _blit_image(SDL_Surface *s, const char *filename, int xoff, int yoff) { SDLSurfacePtr is = LoadSurfaceFromFile(filename); // XXX what should this do if the image couldn't be loaded? if (! is) { return; } SDL_Rect destrec = { 0, 0, 0, 0 }; destrec.x = ((FACE_WIDTH-is->w-1)/2)+xoff; destrec.y = yoff; SDL_BlitSurface(is.Get(), 0, s, &destrec); }
WindowSDL::WindowSDL(const Graphics::Settings &vs, const std::string &name) { // XXX horrible hack. if we don't want a renderer, we might be in an // environment that doesn't actually have graphics available. since we're // not going to draw anything anyway, there's not much point initialising a // window (which will fail in aforementioned headless environment) // // the "right" way would be to have a dummy window class as well, and move // a lot of this initialisation into the GL renderer. this is much easier // right now though if (vs.rendererType == Graphics::RENDERER_DUMMY) return; bool ok; // attempt sequence is: // 1- requested mode ok = CreateWindowAndContext(name.c_str(), vs.width, vs.height, vs.fullscreen, vs.hidden, vs.requestedSamples, 24); // 2- requested mode with no anti-aliasing (skipped if no AA was requested anyway) // (skipped if no AA was requested anyway) if (!ok && vs.requestedSamples) { Output("Failed to set video mode. (%s). Re-trying without multisampling.\n", SDL_GetError()); ok = CreateWindowAndContext(name.c_str(), vs.width, vs.height, vs.fullscreen, vs.hidden, 0, 24); } // 3- requested mode with 16 bit depth buffer if (!ok) { Output("Failed to set video mode. (%s). Re-trying with 16-bit depth buffer\n", SDL_GetError()); ok = CreateWindowAndContext(name.c_str(), vs.width, vs.height, vs.fullscreen, vs.hidden, vs.requestedSamples, 16); } // 4- requested mode with 16-bit depth buffer and no anti-aliasing // (skipped if no AA was requested anyway) if (!ok && vs.requestedSamples) { Output("Failed to set video mode. (%s). Re-trying with 16-bit depth buffer and no multisampling\n", SDL_GetError()); ok = CreateWindowAndContext(name.c_str(), vs.width, vs.height, vs.fullscreen, vs.hidden, 0, 16); } // 5- abort! if (!ok) { Error("Failed to set video mode: %s", SDL_GetError()); } SDLSurfacePtr surface = LoadSurfaceFromFile(vs.iconFile); if (surface) SDL_SetWindowIcon(m_window, surface.Get()); SDL_SetWindowTitle(m_window, vs.title); SDL_ShowCursor(0); SDL_GL_SetSwapInterval((vs.vsync!=0) ? 1 : 0); }
WindowSDL::WindowSDL(const Graphics::Settings &vs, const std::string &name) { bool ok; // attempt sequence is: // 1- requested mode ok = CreateWindowAndContext(name.c_str(), vs.width, vs.height, vs.fullscreen, vs.hidden, vs.requestedSamples, 24); // 2- requested mode with no anti-aliasing (skipped if no AA was requested anyway) // (skipped if no AA was requested anyway) if (!ok && vs.requestedSamples) { Output("Failed to set video mode. (%s). Re-trying without multisampling.\n", SDL_GetError()); ok = CreateWindowAndContext(name.c_str(), vs.width, vs.height, vs.fullscreen, vs.hidden, 0, 24); } // 3- requested mode with 16 bit depth buffer if (!ok) { Output("Failed to set video mode. (%s). Re-trying with 16-bit depth buffer\n", SDL_GetError()); ok = CreateWindowAndContext(name.c_str(), vs.width, vs.height, vs.fullscreen, vs.hidden, vs.requestedSamples, 16); } // 4- requested mode with 16-bit depth buffer and no anti-aliasing // (skipped if no AA was requested anyway) if (!ok && vs.requestedSamples) { Output("Failed to set video mode. (%s). Re-trying with 16-bit depth buffer and no multisampling\n", SDL_GetError()); ok = CreateWindowAndContext(name.c_str(), vs.width, vs.height, vs.fullscreen, vs.hidden, 0, 16); } // 5- abort! if (!ok) { Error("Failed to set video mode: %s", SDL_GetError()); } SDLSurfacePtr surface = LoadSurfaceFromFile(vs.iconFile); if (surface) SDL_SetWindowIcon(m_window, surface.Get()); SDL_SetWindowTitle(m_window, vs.title); SDL_ShowCursor(0); }
static void _blit_image(const SDLSurfacePtr &s, const char *filename, int xoff, int yoff) { _blit_image(s.Get(), filename, xoff, yoff); }
WindowSDL::WindowSDL(const Graphics::Settings &vs, const std::string &name) { Uint32 winFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; if (vs.fullscreen) winFlags |= SDL_WINDOW_FULLSCREEN; SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, vs.requestedSamples ? 1 : 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, vs.requestedSamples); // attempt sequence is: // 1- requested mode m_window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, vs.width, vs.height, winFlags); // 2- requested mode with no anti-aliasing (skipped if no AA was requested anyway) if (!m_window && vs.requestedSamples) { fprintf(stderr, "Failed to set video mode. (%s). Re-trying without multisampling.\n", SDL_GetError()); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); m_window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, vs.width, vs.height, winFlags); } // 3- requested mode with 16 bit depth buffer if (!m_window) { fprintf(stderr, "Failed to set video mode. (%s). Re-trying with 16-bit depth buffer\n", SDL_GetError()); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, vs.requestedSamples ? 1 : 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, vs.requestedSamples); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); m_window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, vs.width, vs.height, winFlags); } // 4- requested mode with 16-bit depth buffer and no anti-aliasing // (skipped if no AA was requested anyway) if (!m_window && vs.requestedSamples) { fprintf(stderr, "Failed to set video mode. (%s). Re-trying with 16-bit depth buffer and no multisampling\n", SDL_GetError()); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); m_window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, vs.width, vs.height, winFlags); } // 5- abort! if (!m_window) { OS::Error("Failed to set video mode: %s", SDL_GetError()); } m_glContext = SDL_GL_CreateContext(m_window); int bpp; Uint32 rmask, gmask, bmask, amask; SDL_PixelFormatEnumToMasks(SDL_GetWindowPixelFormat(m_window), &bpp, &rmask, &gmask, &bmask, &amask); switch (bpp) { case 16: SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); break; case 24: case 32: SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); break; default: fprintf(stderr, "Invalid pixel depth: %d bpp\n", bpp); // this valuable is not reliable if antialiasing vs are overridden by // nvidia/ati/whatever vs int actualSamples = 0; SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &actualSamples); if (vs.requestedSamples != actualSamples) fprintf(stderr, "Requested AA mode: %dx, actual: %dx\n", vs.requestedSamples, actualSamples); } SDLSurfacePtr surface = LoadSurfaceFromFile(vs.iconFile); if (surface) SDL_SetWindowIcon(m_window, surface.Get()); SDL_SetWindowTitle(m_window, vs.title); }