📄 general.cpp
字号:
//// Copyright (c) 2005, Wei Mingzhi <whistler@openoffice.org>// All Rights Reserved.//// This program is free software; you can redistribute it and/or// modify it under the terms of the GNU General Public License as// published by the Free Software Foundation; either version 2 of// the License, or (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA// 02110-1301, USA//#include "main.h"CGeneral *gpGeneral = NULL;CGeneral::CGeneral():m_iPrevGirl(0),m_iPrevGirlColor(0),m_iCurMusic(-1){ LoadImages(); LoadFonts(); LoadMusic(); LoadSound(); InitCursor();}CGeneral::~CGeneral(){ DeleteImages(); DeleteFonts(); DeleteMusic(); DeleteSound(); DeleteCursor();}void CGeneral::DrawBG(int girlnum, int color){ if (girlnum < 0 || girlnum > 4 || color < 0 || color > 2) { TerminateOnError("CGeneral::DrawBG(): invalid parameter"); } SDL_Surface *bg = ((girlnum >= 4) ? m_imgElectron : m_imgGirls[girlnum][color]); assert(bg != NULL); // display the image SDL_BlitSurface(bg, NULL, gpScreen, NULL); m_iPrevGirl = girlnum; m_iPrevGirlColor = color;}void CGeneral::DrawBGFade(int girlnum, int color, int duration){ if (girlnum < 0 || girlnum > 4 || color < 0 || color > 2) { TerminateOnError("CGeneral::DrawBGFade(): invalid parameter"); } SDL_Surface *bg = ((girlnum >= 4) ? m_imgElectron : m_imgGirls[girlnum][color]); assert(bg != NULL); // display the image ScreenFade(duration, bg); m_iPrevGirl = girlnum; m_iPrevGirlColor = color;}void CGeneral::EraseArea(int x, int y, int w, int h, int girlnum, int color){ if (girlnum < 0 || girlnum > 3) { girlnum = m_iPrevGirl; color = m_iPrevGirlColor; } SDL_Rect dstrect; dstrect.x = x; dstrect.y = y; dstrect.w = w; dstrect.h = h; SDL_Surface *bg = m_imgGirls[girlnum][color]; assert(bg != NULL); // display the image SDL_BlitSurface(bg, &dstrect, gpScreen, &dstrect);}void CGeneral::ScreenFade(int duration, SDL_Surface *s){ SDL_Surface *pNewFadeSurface = SDL_CreateRGBSurface(gpScreen->flags & (~SDL_HWSURFACE), gpScreen->w, gpScreen->h, gpScreen->format->BitsPerPixel, gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask, gpScreen->format->Amask); if (!pNewFadeSurface) { // cannot create surface, just blit the surface to the screen if (s != NULL) { SDL_BlitSurface(s, NULL, gpScreen, NULL); SDL_UpdateRect(gpScreen, 0, 0, gpScreen->w, gpScreen->h); } return; } if (s == NULL) { // make black screen SDL_FillRect(pNewFadeSurface, NULL, SDL_MapRGB(pNewFadeSurface->format, 0, 0, 0)); } else { SDL_BlitSurface(s, NULL, pNewFadeSurface, NULL); } if (SDL_MUSTLOCK(gpScreen)) { if (SDL_LockSurface(gpScreen) < 0) { // cannot lock screen, just blit the surface to the screen if (s != NULL) { SDL_BlitSurface(s, NULL, gpScreen, NULL); SDL_UpdateRect(gpScreen, 0, 0, gpScreen->w, gpScreen->h); } return; } } const unsigned long size = gpScreen->pitch * gpScreen->h; unsigned char *fadeFromRGB = (unsigned char *)calloc(size, 1); unsigned char *fadeToRGB = (unsigned char *)calloc(size, 1); if (fadeFromRGB == NULL || fadeToRGB == NULL) { TerminateOnError("Memory allocation error !"); } memcpy(fadeFromRGB, gpScreen->pixels, size); memcpy(fadeToRGB, pNewFadeSurface->pixels, size); int first = SDL_GetTicks(), now = first; do { // The +50 is to allow first frame to show some change float ratio = (now - first + 50) / (float)duration; const unsigned char amount = (unsigned char)(ratio * 255); const unsigned char oldamount = 255 - amount; unsigned char *pw = (unsigned char *)gpScreen->pixels; unsigned char *stop = pw + size; unsigned char *from = fadeFromRGB; unsigned char *to = fadeToRGB; do { //dividing by 256 instead of 255 provides huge optimization *pw = (oldamount * *(from++) + amount * *(to++)) / 256; } while (++pw != stop); now = SDL_GetTicks(); SDL_UpdateRect(gpScreen, 0, 0, gpScreen->w, gpScreen->h); } while (now - first + 50 < duration); free(fadeFromRGB); free(fadeToRGB); SDL_BlitSurface(pNewFadeSurface, NULL, gpScreen, NULL); SDL_UpdateRect(gpScreen, 0, 0, gpScreen->w, gpScreen->h); if (SDL_MUSTLOCK(gpScreen)) SDL_UnlockSurface(gpScreen); SDL_FreeSurface(pNewFadeSurface);}void CGeneral::DrawGirl(int x, int y, int w, int h, int girlnum, int color){ if (girlnum < 0 || girlnum > 3) { TerminateOnError("CGeneral::DrawGirl(): invalid parameter"); } SDL_Rect dstrect, dstrect2; dstrect.x = dstrect.y = 0; dstrect.w = 640; dstrect.h = 480; dstrect2.x = x; dstrect2.y = y; dstrect2.w = w; dstrect2.h = h; SDL_Surface *bg = m_imgGirls[girlnum][color]; assert(bg != NULL); UTIL_ScaleBlit(bg, &dstrect, gpScreen, &dstrect2);}void CGeneral::WaitForAnyKey(){ SDL_Event event; while (SDL_PollEvent(&event)) { } while (1) { if (SDL_WaitEvent(&event)) { if (event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_ESCAPE) { // Quit the program immediately if user pressed ESC UserQuit(); } break; } else if (event.type == SDL_QUIT) { UserQuit(); } else if (event.type == SDL_MOUSEBUTTONDOWN) { break; } } }}SDLKey CGeneral::ReadKey(){ SDL_Event event; while (SDL_PollEvent(&event)) { } while (1) { if (SDL_WaitEvent(&event)) { if (event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_ESCAPE) { // Quit the program immediately if user pressed ESC UserQuit(); } break; } else if (event.type == SDL_QUIT) { UserQuit(); } else if (event.type == SDL_MOUSEBUTTONDOWN) { if (event.button.button == SDL_BUTTON_RIGHT) { return SDLK_RIGHT; } else if (event.button.button == SDL_BUTTON_LEFT) { if (m_iCurMusic == 4) { if (event.button.y < 240) { if (event.button.x < 320) { return SDLK_a; } else { return SDLK_b; } } else { if (event.button.x < 320) { return SDLK_c; } else { return SDLK_d; } } } if (event.button.y > (m_iCurMusic == 8 ? 260 : 380)) { SDLKey r = (SDLKey)(SDLK_a + (event.button.x - 20) / TILE_WIDTH); if (r > SDLK_n && r <= SDLK_z) { return SDLK_n; } return r; } return SDLK_RETURN; } } } } return event.key.keysym.sym;}void CGeneral::GameOver(){ int iCurSel = 0; SDL_Event event; // Draw the background SDL_BlitSurface(m_imgGameOver, NULL, gpScreen, NULL); // Draw the text DrawText("GAME OVER", 22, 22, 1, 255, 255, 0); DrawUTF8Text(va("%s?", msg("Continue")), 400, 300, 1, 255, 255, 0); DrawUTF8Text(msg("Yes"), 450, 350, 1, 255, 255, 0); DrawUTF8Text(msg("No"), 450, 390, 1, 255, 255, 0); // Play the "Game Over" music PlayMusic(m_musGameOver, 0); while (1) { // Erase the original arrow SDL_Rect dstrect; dstrect.x = 400; dstrect.y = 350; dstrect.w = 32; dstrect.h = 80; SDL_BlitSurface(m_imgGameOver, &dstrect, gpScreen, &dstrect); // Draw the new arrow DrawText(">", 400, iCurSel ? 390 : 350, 1, 255, 255, 0); UpdateScreen(); if (SDL_WaitEvent(&event)) { if (event.type == SDL_KEYDOWN) { switch (event.key.keysym.sym) { case SDLK_ESCAPE: case SDLK_n: // Quit the program immediately if user pressed ESC PlayMusic(NULL); UserQuit(); break; case SDLK_UP: case SDLK_DOWN: case SDLK_LEFT: case SDLK_RIGHT: iCurSel ^= 1; break; case SDLK_y: PlayMusic(NULL); PlaySound(SND_SOUND1); UTIL_Delay(2000); ScreenFade(300); return; case SDLK_KP_ENTER: case SDLK_RETURN: case SDLK_SPACE: PlayMusic(NULL); if (iCurSel == 0) { PlaySound(SND_SOUND1); UTIL_Delay(2000); ScreenFade(300); return; } else { UserQuit(); } break; } } else if (event.type == SDL_QUIT) { PlayMusic(NULL); UserQuit(); } } } PlayMusic(NULL); UserQuit();}void CGeneral::BonusGame(){ int i, j, k; char dat[] = {0xe9, 0x85, 0x8d, 0x00, 0xe7, 0x89, 0x8c, 0x00, 0xe5, 0x87, 0xba, 0x00, 0xe8, 0xa1, 0x80, 0x00, 0xe5, 0xa5, 0x96, 0xe5, 0x8a, 0xb1, 0xe6, 0xb8, 0xb8, 0xe6, 0x88, 0x8f, 0x00}; SDL_BlitSurface(m_imgBonusGame, NULL, gpScreen, NULL); UpdateScreen(); PlayMusic(m_musBGame, 0); UTIL_Delay(6000); bool locked = false; if (SDL_MUSTLOCK(gpScreen)) { SDL_LockSurface(gpScreen); locked = true; } for (i = 0; i < 640; i++) { for (j = 0; j < 480; j++) { unsigned char r, g, b; k = 0; UTIL_GetPixel(gpScreen, i, j, &r, &g, &b); k += r; k += g; k += b; k /= 3; UTIL_PutPixel(gpScreen, i, j, k, k, k); } } if (locked) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -