void HelpPanel::draw(SDL_Surface *dest){ int x = dest->w/2 - width/2; int y = dest->h/2 - height/2; int line = 0; std::map<std::string, std::string>::iterator i; for(i = keymap.begin(); i != keymap.end(); ++i){ SDL_Surface *keyText = TTF_RenderText_Blended(font16, ((i->first).substr(1,i->first.length())).c_str(), white); SDL_Surface *keyVal = TTF_RenderText_Blended(font16, (i->second).c_str(), green); blitSurface(width/10, 3*titleText->h+line*keyText->h, keyText, panel); blitSurface(width-width/4, 3*titleText->h+line*keyText->h, keyVal, panel); line++; SDL_FreeSurface(keyText); SDL_FreeSurface(keyVal); } blitSurface(x, y, panel, dest); SDL_Flip(dest); }
void drawTextBox(SDL_Surface *dst, const string s[], int numLines, TTF_Font *font, int x, int y){ SDL_Color col = {0xDD, 0xDD, 0xDD, 0xDD}; int max_w = 0, pos = y, w, h; SDL_Surface **textImgs = new SDL_Surface *[numLines]; for(int i=0; i<numLines; i++){ TTF_SizeText(font, s[i].c_str(), &w, &h); textImgs[i] = TTF_RenderText_Blended(font, s[i].c_str(), col); if(w > max_w) max_w = w; } SDL_Surface *bg = createSurface(max_w + 8, h*numLines); SDL_FillRect(bg, NULL, 0); SDL_SetAlpha(bg, SDL_SRCALPHA, 128); x -= bg->w/2; blitSurface(bg, dst, x, y); SDL_FreeSurface(bg); for(int i=0; i<numLines; i++){ if(textImgs[i]){ blitSurface(textImgs[i], dst, x+4 ,y+h*i); SDL_FreeSurface(textImgs[i]); } } delete [] textImgs; }
void drawTextCentered(SDL_Surface *dst, const string &s, TTF_Font *font, int x, int y){ SDL_Color col = {0xFF, 0xFF, 0xFF, 0xFF}; SDL_Surface * textImg = TTF_RenderText_Blended(font, s.c_str(), col); x-= textImg->w / 2; blitSurface(textImg, dst, x,y); SDL_FreeSurface(textImg); }
void DirectXRender::blitRegionSurface(IND_Surface *pSu, int pX, int pY, int pWidth, int pHeight) { // If the region is the same as the image area, we blit normally // If the region is the same as the image area, we blit normally if (!pX && !pY && (pWidth == pSu->getWidth()) && (pHeight == pSu->getHeight())) { blitSurface(pSu); } else { bool correctParams = true; if (pSu->getNumTextures() > 1 || pX < 0 || pX + pWidth > pSu->getWidth() || pY < 0 || pY + pHeight > pSu->getHeight()) { correctParams = false; } if (correctParams) { // ----- Transform 4 vertices of the quad into world space coordinates ----- D3DXVECTOR4 mP1, mP2, mP3, mP4; Transform4Vertices(static_cast<float>(pWidth), 0.0f, static_cast<float>(pWidth), static_cast<float>(pHeight), 0.0f, 0.0f, 0.0f, static_cast<float>(pHeight), &mP1, &mP2, &mP3, &mP4); D3DXVECTOR3 mP1_f3(mP1); D3DXVECTOR3 mP2_f3(mP2); D3DXVECTOR3 mP3_f3(mP3); D3DXVECTOR3 mP4_f3(mP4); // Calculate the bounding rectangle that we are going to try to discard CalculateBoundingRectangle(&mP1_f3, &mP2_f3, &mP3_f3, &mP4_f3); // ---- Discard bounding rectangle using frustum culling if possible ---- if (!CullFrustumBox(mP1_f3, mP2_f3)) { _numDiscardedObjects++; return; } _numrenderedObjects++; // Prepare the quad that is going to be blitted // Calculates the position and mapping coords for that block fillVertex2d(&_vertices2d [0], static_cast<float>(pWidth), 0, (static_cast<float>(pX + pWidth) / pSu->getWidthBlock()), (1.0f - (static_cast<float>(pY + pSu->getSpareY()) / pSu->getHeightBlock()))); fillVertex2d(&_vertices2d [1], static_cast<float>(pWidth), static_cast<float>(pHeight), (static_cast<float>(pX + pWidth) / pSu->getWidthBlock()), (1.0f - (static_cast<float>(pY + pHeight + pSu->getSpareY()) / pSu->getHeightBlock()))); fillVertex2d(&_vertices2d [2], 0.0f, 0.0f, static_cast<float>(pX) / static_cast<float>(pSu->getWidthBlock()), (1.0f - (static_cast<float>(pY + pSu->getSpareY()) / pSu->getHeightBlock()))); fillVertex2d(&_vertices2d [3], 0.0f, static_cast<float>(pHeight), (static_cast<float>(pX)/ pSu->getWidthBlock()), (1.0f - (static_cast<float>(pY + pHeight + pSu->getSpareY()) / pSu->getHeightBlock()))); // Quad blitting _info._device->SetFVF(D3DFVF_CUSTOMVERTEX2D); _info._device->SetTexture(0, pSu->_surface->_texturesArray [0]._texture); _info._device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, &_vertices2d, sizeof(CUSTOMVERTEX2D)); } } }
//Draw theImage on the screen at (x,y) void ImageCache::render(SDL_Surface* theImage, const int x, const int y) { SDL_Rect position; position.x = static_cast<Sint16>(x); position.y = static_cast<Sint16>(y); blitSurface(theImage, &position); }
void TopAlien::paint(SDL_Surface *dest){ if(killedFrame && (frameStep - killedFrame) > 1){ active = false; return; } blitSurface(x, y, frame[frameStep % 2], dest); }
Panel::Panel(int w, int h, TTF_Font *f16, std::string title) : width(w), height(h), font16(f16) { int i; panel = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, BPP, screen->format->Rmask, \ screen->format->Gmask, screen->format->Bmask, screen->format->Amask); titleText = TTF_RenderText_Blended(font16, title.c_str(), green); spaceText = TTF_RenderText_Blended(font16, "Press <SPACE> to continue", green); for(i=0; i < panel->h; ++i){ drawPixel(panel, 0, i, 0xFF, 0xFF, 0xFF); drawPixel(panel, panel->w-1, i, 0xFF, 0xFF, 0xFF); } for(i=0; i < panel->w; ++i){ drawPixel(panel, i, 0, 0xFF, 0xFF, 0xFF); drawPixel(panel, i, panel->h-1, 0xFF, 0xFF, 0xFF); } for(i=3; i < panel->h-3; ++i){ drawPixel(panel, 3, i, 0xFF, 0xFF, 0xFF); drawPixel(panel, panel->w-4, i, 0xFF, 0xFF, 0xFF); } for(i=3; i < panel->w-3; ++i){ drawPixel(panel, i, 3, 0xFF, 0xFF, 0xFF); drawPixel(panel, i, panel->h-4, 0xFF, 0xFF, 0xFF); } blitSurface(width/2-titleText->w/2, 6, titleText, panel); blitSurface(width/2-spaceText->w/2, height-2*spaceText->h, spaceText, panel); }
void Credits::makePanel(void){ int base(0); creditsTitle = TTF_RenderText_Blended(font48, "Credits", green); codeText = TTF_RenderText_Blended(font18, "Coding and Design: Paul Slavin", white); musicText = TTF_RenderText_Blended(font18, "Title Music: VMK (8bc.org)", white); panel = SDL_CreateRGBSurface( SDL_HWSURFACE | SDL_SRCALPHA, codeText->w, 8*codeText->h, BPP, \ screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask); SDL_SetColorKey(panel, SDL_SRCCOLORKEY | SDL_RLEACCEL, 0); blitSurface((panel->w-creditsTitle->w)/2, base, creditsTitle, panel); base += creditsTitle->h + 20; blitSurface(0, base, codeText, panel); base += codeText->h + 20; blitSurface((panel->w - musicText->w)/2, base, musicText, panel); }
void GsScrollbar::drawScrollBar(const SDL_Rect &lRect) { SDL_Rect scrollRect = lRect; scrollRect.w = 10; // -> This can always stay at font size, the rest must be scaled to the parent control GsWeakSurface blitSurface( gVideoDriver.getBlitSurface() ); SDL_Rect bScUpRect = scrollRect; bScUpRect.w = scrollRect.w; bScUpRect.h = 10; SDL_Rect bScDownRect = bScUpRect; bScDownRect.y = (scrollRect.y+scrollRect.h) - (bScUpRect.h); blitSurface.fillRGB( scrollRect, 0xBF, 0xBF, 0xBF); // Now show the slider float relPos = float(mScrollPos) / float(mMaxScrollAmt); const int posSpace = int(relPos * float(scrollRect.h - (3*10))) + 1; SDL_Rect bSliderRect = bScDownRect; bSliderRect.x++; bSliderRect.y = (bScUpRect.y + bScUpRect.h) + posSpace; bSliderRect.w = bScUpRect.w - 2; bSliderRect.h = bScUpRect.h - 2; blitSurface.fillRGB( bSliderRect, 0x2F, 0x2F, 0x2F); // Set the up and down arrows //GsFont &Font = gGraphics.getFont(mFontID); blitSurface.fillRGB( bScUpRect, 0x7F, 0x7F, 0x7F); //Font.drawFontCentered(blitSurface.getSDLSurface(), "\017", bScUpRect.x, bScUpRect.w, bScUpRect.y, false ); blitSurface.fillRGB( bScDownRect, 0x7F, 0x7F, 0x7F); //Font.drawFontCentered(blitSurface.getSDLSurface(), "\023", bScDownRect.x, bScDownRect.w, bScDownRect.y, false ); mArrowHeight = 10.0f/float(lRect.h); mSliderHeight = 8.0f/float(lRect.h); }
void Credits::paint(SDL_Surface *dest, int yOffset){ blitSurface( (dest->w - panel->w)/2, yOffset, panel, dest); }
void Shot::paint(SDL_Surface *dest){ blitSurface(x, y, shotsurf, dest); }
int DirectXRender::blitAnimation(IND_Animation *pAn, int pSequence, int pX, int pY, int pWidth, int pHeight, bool pToggleWrap, float pUDisplace, float pVDisplace) { bool correctParams = true; int mFinish = 1; if (pSequence < 0 || pSequence > pAn->getNumSequences() - 1) { correctParams = false; } if (correctParams) { if (!pAn->getIsActive(pSequence)) { pAn->getSequenceTimer(pSequence)->start(); pAn->setIsActive(pSequence, 1); } // Current world matrix D3DXMATRIX mMatWorld, mTrans; _info._device->GetTransform(D3DTS_WORLD, &mMatWorld); // If the time of a frame have passed, go to the next frame if (pAn->getSequenceTimer(pSequence)->getTicks() > (unsigned long) pAn->getActualFrameTime(pSequence)) { // Put timer to zero pAn->getSequenceTimer(pSequence)->start(); // Point to the next frame increasing the counter int i = pAn->getActualFramePos(pSequence); pAn->setActualFramePos(pSequence, i + 1); // If the counter is higher than the number of frames of the sequence, we put it to zero if (pAn->getActualFramePos(pSequence) > pAn->getNumFrames(pSequence) - 1) { pAn->setActualFramePos(pSequence, pAn->getNumFrames(pSequence) - 1); pAn->setIsActive(pSequence, 0); mFinish = -1; } } // ----- OffsetX y OffsetY ----- D3DXMatrixTranslation(&mTrans, static_cast<float>(pAn->getActualOffsetX(pSequence)), static_cast<float>(pAn->getActualOffsetY(pSequence)), 0); D3DXMatrixMultiply(&mMatWorld, &mMatWorld, &mTrans); _info._device->SetTransform(D3DTS_WORLD, &mMatWorld); // ----- Blitting ----- // Blits all the IND_Surface (all the blocks) if (!pX && !pY && !pWidth && !pHeight) { blitSurface(pAn->getActualSurface(pSequence)); } else { if (!pToggleWrap) { // Blits a region of the IND_Surface if (pAn->getActualSurface(pSequence)->getNumTextures() > 1) return 0; blitRegionSurface(pAn->getActualSurface(pSequence), pX, pY, pWidth, pHeight); } else {// Blits a wrapping IND_Surface if (pAn->getActualSurface(pSequence)->getNumTextures() > 1) return 0; blitWrapSurface(pAn->getActualSurface(pSequence), pWidth, pHeight, pUDisplace, pVDisplace); } } } return mFinish; }
void KImage::blitSurface(KImage *dest, const KRect &dRect, const KRect &sRect) { if(dest != nullptr) { blitSurface(dest->surface, dRect, sRect); } }
void KImage::blitSurface(KImage *dest, int x, int y, const KRect &sRect) { if(dest != nullptr) { blitSurface(dest->surface, x, y, sRect); } }
void KImage::blitSurface(KImage *dest, int x, int y) { if(dest != nullptr) { blitSurface(dest->surface, x, y); } }
int main(int argc,char** argv) { SDL_Init(SDL_INIT_EVERYTHING); //init the SDL SDL_Surface* screen=SDL_SetVideoMode(BLOCK_SIZE*WIDTH+BLOCK_SIZE*10,BLOCK_SIZE*HEIGHT,32,SDL_SWSURFACE); //and our screen TTF_Init(); //the TTF as well, cus we want to write out stuff TTF_Font* font=TTF_OpenFont("air.ttf",12); Uint32 start; //the start time, to limit FPS bool running=true; //is the program still running? SDL_Event event; //What event has happened? srand(time(0)); //seed the random number generator int db=0; //how many blocks do we have (init 0)? int elements[8][4][4]; //we store all of the blocks in this 3D array int table[HEIGHT][WIDTH]; //This is our whole game-table, all of the blocks, which already put down is stored here int currentblock[4][4]; //our current falling block fillelement(elements,db); //load the blocks SDL_Surface* blocks=SDL_LoadBMP("blocks.bmp"); //load the image, which stores, the part of the images SDL_SetColorKey(blocks,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255)); //The purple color should be invisible int blockx; //the init position of the falling block int blocky; bool h; //if it's the 1. or 2. block, we need 4x4, else 3x3 Uint32 lastmove=SDL_GetTicks(); //how often should the block come down one Uint32 lastkey=SDL_GetTicks(); //how often should react the block, when the key is pressed (without release) bool keys[2]={0,0}; //if left arrow pressed, than keys[0]=1 if right arrow is pressed keys[1]=1 int points; int normalspeed; //how quick the tetris blocks fall (in this case once every 0.5 seconds) int speed=500; //the speed, the game currently running (the speed increase, when you press the down arrow) const int FPS=30; //how many FPS the game will run (this effect minimally, how quick the blocks fall) int nextblock; //what is the next block? int deletedlines; //how much lines we already deleted int movingspeed=400; //if we hold down the left/right arrow, how often (150ms) we want to move the block int quickmovingspeed=30; bool mousepointing=false; //do we pointing to the toplist text? int tmpx,tmpy; //the location of the mouse cursor //we initialize the game (set every parameter to default) initGame(&blockx,&blocky,&h,elements,table,currentblock,&normalspeed,&points,&deletedlines,&nextblock,&speed,db); int moved=0; while(running) //while the game running { start=SDL_GetTicks(); //get the current time (for FPS limitation) while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: //if escape is pressed, escape running=false; break; case SDLK_UP: //if up arrow is pressed rotateleft(currentblock,h); //rotate the block if(checkCollision(currentblock,blockx,blocky,table)) //and check if there is a collision for(int i=0;i<3;i++) rotateleft(currentblock,h); //if there was a collision, rotate back (rotate 4 times, is like if you haven't done anything) break; case SDLK_LEFT: keys[0]=1; break; case SDLK_RIGHT: keys[1]=1; break; case SDLK_DOWN: speed=10; //if down key is pressed, speed up a little bit break; } break; case SDL_KEYUP: switch(event.key.keysym.sym) { case SDLK_DOWN: speed=normalspeed; //if you released the down arrow, set back the speed break; case SDLK_LEFT: keys[0]=0; moved=0; break; case SDLK_RIGHT: keys[1]=0; moved=0; break; } break; case SDL_QUIT: running=false; break; case SDL_MOUSEMOTION: //if we moved the mouse tmpx=event.motion.x; //get the coordinates tmpy=event.motion.y; //if we are pointing to the square, which contain the toplist text if(tmpx>BLOCK_SIZE*WIDTH+2 && tmpx<BLOCK_SIZE*WIDTH+80 && tmpy>80+BLOCK_SIZE*5 && tmpy<80+BLOCK_SIZE*5+20) mousepointing=true; //make this boolean true else mousepointing=false; //else false break; case SDL_MOUSEBUTTONDOWN: //if we hit the mousebutton tmpx=event.button.x; tmpy=event.button.y; //on the toplist text if(tmpx>BLOCK_SIZE*WIDTH && tmpx<BLOCK_SIZE*WIDTH+80 && tmpy>80+BLOCK_SIZE*5 && tmpy<80+BLOCK_SIZE*5+20) displayToplist(font); //display it break; } } //LOGIC //if the collision happens, and the block is at the top of the screen, than game is over if(checkCollision(currentblock,blockx,blocky,table) && blocky==0) { if(addToplist(font,points)) //try to add out points to the toplist, if addtoplist returns 1, restart the game initGame(&blockx,&blocky,&h,elements,table,currentblock,&normalspeed,&points,&deletedlines,&nextblock,&speed,db); else running=false; //else we exit } //if we exceeded the time, how often should it go down, than move it down, and set back the time if(SDL_GetTicks()-lastmove>speed) { blocky++; lastmove=SDL_GetTicks(); } //if (left) key was pressed, and last time, when we moved the block is more than 200ms, than move it again if((keys[0] && moved>=2 && SDL_GetTicks()-lastkey>quickmovingspeed) || (keys[0] && moved==0) || (keys[0] && moved==1 && SDL_GetTicks()-lastkey>movingspeed)) { blockx--; //move moved++; lastkey=SDL_GetTicks(); //set back the time if(checkCollision(currentblock,blockx,blocky,table)==1)//if there is a collision blockx++; //move back }else if((keys[1] && moved>=2 && SDL_GetTicks()-lastkey>quickmovingspeed) || (keys[1] && moved==0) || (keys[1] && moved==1 && SDL_GetTicks()-lastkey>movingspeed)) //same with right arrow { blockx++; moved++; lastkey=SDL_GetTicks(); if(checkCollision(currentblock,blockx,blocky,table)==1) blockx--; } //RENDER SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0,0,0)); //clear the screen for(int i=0;i<24;i++) //render out the table for(int j=0;j<12;j++) { if(!table[i][j]) //if a value=0, than don't do anything, else draw the corresponding block (1 is the first block on the images, 2 is the 2...) continue; else blitSurface(blocks,(table[i][j]-1)*BLOCK_SIZE,0,BLOCK_SIZE,BLOCK_SIZE,screen,j*BLOCK_SIZE,i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE); } //render the falling block for(int i=0;i<4;i++) for(int j=0;j<4;j++) { if(currentblock[i][j]) //if not "empty", draw the corresponding block blitSurface(blocks,(currentblock[i][j]-1)*BLOCK_SIZE,0,BLOCK_SIZE,BLOCK_SIZE,screen,blockx*BLOCK_SIZE+j*BLOCK_SIZE,blocky*BLOCK_SIZE+i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE); } //here I check the collision, first I move the object down blocky++; if(checkCollision(currentblock,blockx,blocky,table))//and if there is a collision { blocky--; //move back for(int i=0;i<4;i++) for(int j=0;j<4;j++) { if(currentblock[i][j]!=0) table[blocky+i][blockx+j]=currentblock[i][j]; //and draw the block to the table matrix (except the 0), so we handle it as fallen block, it's not falling anymore } blocky=0; //and generate a new block the same way, as we did in the beginning of the main blockx=4; setCurrentblock(elements,currentblock,nextblock); h=(nextblock>2); nextblock=rand()%(db+1); }else blocky--; //if there was no collision, move the object back (else it will go every 2nd line) //this while loop will go as long, as we have full lines while(1) { int k=checkful(table); if(k==-1) break; deletedlines++; //if we have full lines for(int i=k;i>0;i--) for(int j=0;j<WIDTH;j++) table[i][j]=table[i-1][j]; //delete them, by move everything above it a line down for(int i=0;i<HEIGHT;i++) table[0][i]=0; //and 0 out the most top line (usually this has no effect, except, if there is a block-part in the top row) points+=(550-normalspeed)/10; if((points%50)==0 && normalspeed>0) //if the points are dividable by 50 (50,100,150...), speed up the game { if(normalspeed>50) normalspeed-=50; else if(normalspeed>0) normalspeed-=10; } } //render the menu SDL_Rect rec={BLOCK_SIZE*WIDTH,0,screen->clip_rect.w,screen->clip_rect.h}; //the background of the tetris is black (if you want SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,50,50,50)); //you can change it to an image) rec.x=BLOCK_SIZE*WIDTH+10; //this is the part next to the tetris (the menu) rec.w=4*BLOCK_SIZE; rec.y=20; rec.h=4*BLOCK_SIZE; SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,100,100,100)); //fill it with gray for(int i=0;i<4;i++) for(int j=0;j<4;j++) { if(elements[nextblock][i][j]) //if not "empty", draw the corresponding block { blitSurface(blocks,(elements[nextblock][i][j]-1)*BLOCK_SIZE,0,BLOCK_SIZE,BLOCK_SIZE,screen,(BLOCK_SIZE*WIDTH)+j*BLOCK_SIZE+20,20+i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE); } } //draw all of the menutext (huhh, it's good, that I have this function, else it would be a 4,5 times that amount of line) writeText(font,BLOCK_SIZE*WIDTH,0,"NEXT BLOCK",255,255,255); writeText(font,BLOCK_SIZE*WIDTH,20+BLOCK_SIZE*5,"POINTS: ",255,255,255); writeText(font,BLOCK_SIZE*WIDTH+150,20+BLOCK_SIZE*5,points,255,255,255); writeText(font,BLOCK_SIZE*WIDTH,40+BLOCK_SIZE*5,"CURRENT SPEED:",255,255,255); writeText(font,BLOCK_SIZE*WIDTH+150,40+BLOCK_SIZE*5,normalspeed,255,255,255); writeText(font,BLOCK_SIZE*WIDTH,60+BLOCK_SIZE*5,"DELETED LINES: ",255,255,255); writeText(font,BLOCK_SIZE*WIDTH+150,60+BLOCK_SIZE*5,deletedlines,255,255,255); rec.x=BLOCK_SIZE*WIDTH+2; //the toplist square rec.y=80+BLOCK_SIZE*5; rec.w=80; rec.h=20; if(!mousepointing) //if we are not pointing to the toplist square, fill it with { SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,0,0,0)); //black }else { SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,255,0,0)); //else red } writeText(font,BLOCK_SIZE*WIDTH+5,80+BLOCK_SIZE*5,"TOPLIST",255,255,255); //and write the toplist text SDL_Flip(screen); //show evrything o the real screen if(1000.0/FPS>SDL_GetTicks()-start) SDL_Delay(1000.0/FPS-(SDL_GetTicks()-start)); //regulate the FPS } SDL_FreeSurface(blocks); //delete and close everything TTF_CloseFont(font); TTF_Quit(); SDL_Quit(); return 0; }