/** Do palette animation and blit to the window. */ static void PaintWindow(HDC dc) { HDC dc2 = CreateCompatibleDC(dc); HBITMAP old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect); HPALETTE old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE); if (_cur_palette.count_dirty != 0) { Blitter *blitter = BlitterFactory::GetCurrentBlitter(); switch (blitter->UsePaletteAnimation()) { case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND: UpdatePalette(dc2, _local_palette.first_dirty, _local_palette.count_dirty); break; case Blitter::PALETTE_ANIMATION_BLITTER: blitter->PaletteAnimate(_local_palette); break; case Blitter::PALETTE_ANIMATION_NONE: break; default: NOT_REACHED(); } _cur_palette.count_dirty = 0; } BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY); SelectPalette(dc, old_palette, TRUE); SelectObject(dc2, old_bmp); DeleteDC(dc2); }
/** Draw the chat message-box */ void NetworkDrawChatMessage() { Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter(); if (!_chatmessage_dirty) return; /* First undraw if needed */ NetworkUndrawChatMessage(); if (_iconsole_mode == ICONSOLE_FULL) return; /* Check if we have anything to draw at all */ uint count = GetChatMessageCount(); if (count == 0) return; int x = _chatmsg_box.x; int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height; int width = _chatmsg_box.width; int height = _chatmsg_box.height; if (y < 0) { height = max(height + y, min(_chatmsg_box.height, _screen.height)); y = 0; } if (x + width >= _screen.width) { width = _screen.width - x; } if (width <= 0 || height <= 0) return; assert(blitter->BufferSize(width, height) <= (int)(_chatmsg_box.width * _chatmsg_box.height * blitter->GetBytesPerPixel())); /* Make a copy of the screen as it is before painting (for undraw) */ blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height); _cur_dpi = &_screen; // switch to _screen painting /* Paint a half-transparent box behind the chat messages */ GfxFillRect( _chatmsg_box.x, _screen.height - _chatmsg_box.y - count * (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING) - 2, _chatmsg_box.x + _chatmsg_box.width - 1, _screen.height - _chatmsg_box.y - 2, PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR // black, but with some alpha for background ); /* Paint the chat messages starting with the lowest at the bottom */ for (uint y = FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING; count-- != 0; y += (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING)) { DrawString(_chatmsg_box.x + 3, _chatmsg_box.x + _chatmsg_box.width - 1, _screen.height - _chatmsg_box.y - y + 1, _chatmsg_list[count].message, _chatmsg_list[count].colour); } /* Make sure the data is updated next flush */ _video_driver->MakeDirty(x, y, width, height); _chatmessage_visible = true; _chatmessage_dirty = false; }
void Sprite::render(BitmapCanvas& canvas) { // render the current frame at m_x,m_y if(m_visible) { int frame = m_animations[m_currentAnimation][m_currentFrame]; if( m_frames[frame]->m_imgMask != NULL ) { // we have a mask that we need to render first Blitter bltMask ( *(m_frames[frame]->m_imgMask) ); bltMask.SetMode (SRCPAINT); bltMask.SetDest (m_x, m_y); bltMask.BlitTo (canvas); Blitter blitFrame( *(m_frames[frame]->m_img) ); blitFrame.SetMode (SRCAND); blitFrame.SetDest (m_x, m_y); blitFrame.BlitTo(canvas); } else { // no mask, just render the frame Blitter bltMask ( *(m_frames[frame]->m_img) ); bltMask.SetMode (SRCPAINT); bltMask.SetDest (m_x, m_y); bltMask.BlitTo (canvas); } } }
/** Hide the chatbox */ void NetworkUndrawChatMessage() { /* Sometimes we also need to hide the cursor * This is because both textmessage and the cursor take a shot of the * screen before drawing. * Now the textmessage takes his shot and paints his data before the cursor * does, so in the shot of the cursor is the screen-data of the textmessage * included when the cursor hangs somewhere over the textmessage. To * avoid wrong repaints, we undraw the cursor in that case, and everything * looks nicely ;) * (and now hope this story above makes sense to you ;)) */ if (_cursor.visible && _cursor.draw_pos.x + _cursor.draw_size.x >= _chatmsg_box.x && _cursor.draw_pos.x <= _chatmsg_box.x + _chatmsg_box.width && _cursor.draw_pos.y + _cursor.draw_size.y >= _screen.height - _chatmsg_box.y - _chatmsg_box.height && _cursor.draw_pos.y <= _screen.height - _chatmsg_box.y) { UndrawMouseCursor(); } if (_chatmessage_visible) { Blitter *blitter = BlitterFactory::GetCurrentBlitter(); int x = _chatmsg_box.x; int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height; int width = _chatmsg_box.width; int height = _chatmsg_box.height; if (y < 0) { height = max(height + y, min(_chatmsg_box.height, _screen.height)); y = 0; } if (x + width >= _screen.width) { width = _screen.width - x; } if (width <= 0 || height <= 0) return; _chatmessage_visible = false; /* Put our 'shot' back to the screen */ blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height); /* And make sure it is updated next time */ _video_driver->MakeDirty(x, y, width, height); _chatmessage_dirty = true; } }
/** * Find the requested blitter and return his class. * @param name the blitter to select. * @post Sets the blitter so GetCurrentBlitter() returns it too. */ static Blitter *SelectBlitter(const char *name) { BlitterFactory *b = GetBlitterFactory(name); if (b == nullptr) return nullptr; Blitter *newb = b->CreateInstance(); delete *GetActiveBlitter(); *GetActiveBlitter() = newb; DEBUG(driver, 1, "Successfully %s blitter '%s'", StrEmpty(name) ? "probed" : "loaded", newb->GetName()); return newb; }
static void CheckPaletteAnim() { if (_cur_palette.count_dirty != 0) { Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter(); switch (blitter->UsePaletteAnimation()) { case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND: UpdatePalette(); break; case Blitter::PALETTE_ANIMATION_BLITTER: blitter->PaletteAnimate(_local_palette); break; case Blitter::PALETTE_ANIMATION_NONE: break; default: NOT_REACHED(); } _cur_palette.count_dirty = 0; } }
/** * Find the requested blitter and return his class. * @param name the blitter to select. * @post Sets the blitter so GetCurrentBlitter() returns it too. */ static Blitter *SelectBlitter(const char *name) { BlitterFactory *b = GetBlitterFactory(name); if (b == NULL) return NULL; //用工厂生成一个Blitter Blitter *newb = b->CreateInstance(); delete *GetActiveBlitter(); //得到的是双重地址,可以复制的. *GetActiveBlitter() = newb; DEBUG(driver, 1, "Successfully %s blitter '%s'", StrEmpty(name) ? "probed" : "loaded", newb->GetName()); return newb; }
bool VideoDriver_SDL::CreateMainSurface(uint w, uint h) { SDL_Surface *newscreen, *icon; char caption[50]; int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(); bool want_hwpalette; GetAvailableVideoMode(&w, &h); DEBUG(driver, 1, "SDL: using mode %ux%ux%d", w, h, bpp); if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals"); char icon_path[MAX_PATH]; if (FioFindFullPath(icon_path, lengthof(icon_path), BASESET_DIR, "openttd.32.bmp") != NULL) { /* Give the application an icon */ icon = SDL_CALL SDL_LoadBMP(icon_path); if (icon != NULL) { /* Get the colourkey, which will be magenta */ uint32 rgbmap = SDL_CALL SDL_MapRGB(icon->format, 255, 0, 255); SDL_CALL SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap); SDL_CALL SDL_WM_SetIcon(icon, NULL); SDL_CALL SDL_FreeSurface(icon); } } if (_use_hwpalette == 2) { /* Default is to autodetect when to use SDL_HWPALETTE. * In this case, SDL_HWPALETTE is only used for 8bpp * blitters in fullscreen. * * When using an 8bpp blitter on a 8bpp system in * windowed mode with SDL_HWPALETTE, OpenTTD will claim * the system palette, making all other applications * get the wrong colours. In this case, we're better of * trying to approximate the colors we need using system * colors, using a shadow surface (see below). * * On a 32bpp system, SDL_HWPALETTE is ignored, so it * doesn't matter what we do. * * When using a 32bpp blitter on a 8bpp system, setting * SDL_HWPALETTE messes up rendering (at least on X11), * so we don't do that. In this case, SDL takes care of * color approximation using its own shadow surface * (which we can't force in 8bpp on 8bpp mode, * unfortunately). */ want_hwpalette = (bpp == 8 && _fullscreen); } else { /* User specified a value manually */ want_hwpalette = _use_hwpalette; } if (want_hwpalette) DEBUG(driver, 1, "SDL: requesting hardware palete"); /* Free any previously allocated shadow surface */ if (_sdl_screen != NULL && _sdl_screen != _sdl_realscreen) SDL_CALL SDL_FreeSurface(_sdl_screen); if (_sdl_realscreen != NULL) { if (_requested_hwpalette != want_hwpalette) { /* SDL (at least the X11 driver), reuses the * same window and palette settings when the bpp * (and a few flags) are the same. Since we need * to hwpalette value to change (in particular * when switching between fullscreen and * windowed), we restart the entire video * subsystem to force creating a new window. */ DEBUG(driver, 0, "SDL: Restarting SDL video subsystem, to force hwpalette change"); SDL_CALL SDL_QuitSubSystem(SDL_INIT_VIDEO); SDL_CALL SDL_InitSubSystem(SDL_INIT_VIDEO); ClaimMousePointer(); SetupKeyboard(); } } /* Remember if we wanted a hwpalette. We can't reliably query * SDL for the SDL_HWPALETTE flag, since it might get set even * though we didn't ask for it (when SDL creates a shadow * surface, for example). */ _requested_hwpalette = want_hwpalette; /* DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK */ newscreen = SDL_CALL SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | (want_hwpalette ? SDL_HWPALETTE : 0) | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE)); if (newscreen == NULL) { DEBUG(driver, 0, "SDL: Couldn't allocate a window to draw on"); return false; } _sdl_realscreen = newscreen; if (bpp == 8 && (_sdl_realscreen->flags & SDL_HWPALETTE) != SDL_HWPALETTE) { /* Using an 8bpp blitter, if we didn't get a hardware * palette (most likely because we didn't request one, * see above), we'll have to set up a shadow surface to * render on. * * Our palette will be applied to this shadow surface, * while the real screen surface will use the shared * system palette (which will partly contain our colors, * but most likely will not have enough free color cells * for all of our colors). SDL can use these two * palettes at blit time to approximate colors used in * the shadow surface using system colors automatically. * * Note that when using an 8bpp blitter on a 32bpp * system, SDL will create an internal shadow surface. * This shadow surface will have SDL_HWPALLETE set, so * we won't create a second shadow surface in this case. */ DEBUG(driver, 1, "SDL: using shadow surface"); newscreen = SDL_CALL SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, bpp, 0, 0, 0, 0); if (newscreen == NULL) { DEBUG(driver, 0, "SDL: Couldn't allocate a shadow surface to draw on"); return false; } } /* Delay drawing for this cycle; the next cycle will redraw the whole screen */ _num_dirty_rects = 0; _screen.width = newscreen->w; _screen.height = newscreen->h; _screen.pitch = newscreen->pitch / (bpp / 8); _screen.dst_ptr = newscreen->pixels; _sdl_screen = newscreen; /* When in full screen, we will always have the mouse cursor * within the window, even though SDL does not give us the * appropriate event to know this. */ if (_fullscreen) _cursor.in_window = true; Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter(); blitter->PostResize(); InitPalette(); switch (blitter->UsePaletteAnimation()) { case Blitter::PALETTE_ANIMATION_NONE: case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND: UpdatePalette(); break; case Blitter::PALETTE_ANIMATION_BLITTER: if (_video_driver != NULL) blitter->PaletteAnimate(_local_palette); break; default: NOT_REACHED(); } snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision); SDL_CALL SDL_WM_SetCaption(caption, caption); GameSizeChanged(); return true; }
/** Draw the chat message-box */ void NetworkDrawChatMessage() { Blitter *blitter = BlitterFactory::GetCurrentBlitter(); if (!_chatmessage_dirty) return; /* First undraw if needed */ NetworkUndrawChatMessage(); if (_iconsole_mode == ICONSOLE_FULL) return; /* Check if we have anything to draw at all */ uint count = GetChatMessageCount(); if (count == 0) return; int x = _chatmsg_box.x; int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height; int width = _chatmsg_box.width; int height = _chatmsg_box.height; if (y < 0) { height = max(height + y, min(_chatmsg_box.height, _screen.height)); y = 0; } if (x + width >= _screen.width) { width = _screen.width - x; } if (width <= 0 || height <= 0) return; assert(blitter->BufferSize(width, height) <= (int)(_chatmsg_box.width * _chatmsg_box.height * blitter->GetBytesPerPixel())); /* Make a copy of the screen as it is before painting (for undraw) */ blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height); _cur_dpi = &_screen; // switch to _screen painting int string_height = 0; for (uint i = 0; i < count; i++) { SetDParamStr(0, _chatmsg_list[i].message); string_height += GetStringLineCount(STR_JUST_RAW_STRING, width - 1) * FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING; } string_height = min(string_height, MAX_CHAT_MESSAGES * (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING)); int top = _screen.height - _chatmsg_box.y - string_height - 2; int bottom = _screen.height - _chatmsg_box.y - 2; /* Paint a half-transparent box behind the chat messages */ GfxFillRect(_chatmsg_box.x, top - 2, _chatmsg_box.x + _chatmsg_box.width - 1, bottom, PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR // black, but with some alpha for background ); /* Paint the chat messages starting with the lowest at the bottom */ int ypos = bottom - 2; for (int i = count - 1; i >= 0; i--) { ypos = DrawStringMultiLine(_chatmsg_box.x + 3, _chatmsg_box.x + _chatmsg_box.width - 1, top, ypos, _chatmsg_list[i].message, _chatmsg_list[i].colour, SA_LEFT | SA_BOTTOM | SA_FORCE) - NETWORK_CHAT_LINE_SPACING; if (ypos < top) break; } /* Make sure the data is updated next flush */ _video_driver->MakeDirty(x, y, width, height); _chatmessage_visible = true; _chatmessage_dirty = false; }
/** * Callback of the screenshot generator that dumps the current video buffer. * @see ScreenshotCallback */ static void CurrentScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n) { Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter(); void *src = blitter->MoveTo(_screen.dst_ptr, 0, y); blitter->CopyImageToBuffer(src, buf, _screen.width, n, pitch); }