Kayıt: Oct 23, 2005 Mesajlar: 57 Nereden: kopenhagen
Tarih: Sat Dec 10, 2005 12:02 am Mesaj konusu: Cimg.h ilgilenenler icin
CImg.h
C++ Template Image Processing Library
diye bir header file la ugrasiyorum bu konuyla daha once ilgilenen oldumu.
Kullanmasini cok kolay buldum 2D oyunlar icin bir alternatif olusturabilir.
Kayıt: Nov 02, 2002 Mesajlar: 215 Nereden: Istanbool
Tarih: Sun Dec 11, 2005 12:46 am Mesaj konusu: Re: Cimg.h ilgilenenler icin
playmaker demiş ki:
CImg.h
C++ Template Image Processing Library
diye bir header file la ugrasiyorum bu konuyla daha once ilgilenen oldumu.
Kullanmasini cok kolay buldum 2D oyunlar icin bir alternatif olusturabilir.
Mentat Bey, aklima Mr. Leloglu'nun XImage class'i geldi, hatirladiniz mi, hani uzerinde hersey vardi, image.fft(), image.emboss(), image.zartzurt()...
Saka bi yana, image processing yapicaksan template en son istedigin sey. 8bit unsigned kullanicagin kesin cunku SIMD sart. Oyunlar icin ise image processing gerekmiyo denicek kadar az.
//-------------------------
int main(int argc,char **argv) {
// Read command line argument (if any)
cimg_usage("An implementation of the well known 'Tetris' game with CImg.");
unsigned int
blocdim = cimg_option("-blocdim",16,"Sprite bloc size"),
speed = cimg_option("-speed",20,"Initial speed"),
level = cimg_option("-level",0,"Level");
const char *geometry = cimg_option("-g","12x20","Size of the board");
unsigned int bwidth = 12,bheight = 20;
std::sscanf(geometry,"%u%*c%u",&bwidth,&bheight);
CImg<unsigned char> dlogo = CImg<unsigned char>("img/logo.ppm").resize(128,96);
if (cimg::dialog("CImg Tetris",
"Welcome to the CImg version of Tetris.\n"
"( by David Tschumperlé )\n\n"
"Press 'Start' when you are ready to play !","Start","Quit",0,0,0,0,dlogo)) std::exit(0);
CImg<unsigned char> board(bwidth,bheight,1,1,0), background(board.dimx()*blocdim,board.dimy()*blocdim,1,3);
(background.fill(0).noise(30).draw_plasma().noise(30).deriche(10,0,'y'))/=3;
if (level) (board.lineset(board.dimy()-level,board.dimy()-1,0,0).noise(100))%=pieces.size+1;
// Create a set of small gradient-colored blocs used to draw the pieces.
CImgl<unsigned char> blocs(pieces.size,blocdim,blocdim,1,3);
cimgl_map(blocs,l) {
CImg<unsigned char> color = CImg<unsigned char>(3).fill(0).noise(255,1).cut(100,255);
float val;
cimg_mapXYV(blocs[l],x,y,k) blocs[l](x,y,k) = (unsigned char)((val=(color[k]*0.7f*(x+y+5)/blocdim))>255?255:val);
blocs[l].draw_line(0,0,0,blocdim-1,(color/2).ptr()).draw_line(0,blocdim-1,blocdim-1,blocdim-1,(color/2).ptr());
color = (CImg<unsigned int>(color)*2).cut(0,255);
blocs[l].draw_line(0,0,blocdim-1,0,color.ptr()).draw_line(blocdim-1,0,blocdim-1,blocdim-1,color.ptr());
}
// Initialize window display and enter the main event loop
CImgDisplay disp(background,"CImg Tetris",0);
const unsigned char white[3]={255,255,255};
CImg<unsigned char> piece, next, next_mask;
int cx=-1,cy=-1,cn=-1,nn=rand()%pieces.size,time=0, score=0;
bool gameover = false, pause = false;
while (!gameover && !disp.closed && disp.key!=cimg::keyESC && disp.key!=cimg::keyQ) {
if (!pause) {
// Draw the board on the display window.
CImg<unsigned char> nboard(board), visu(background);
if (cx>=0 && cy>=0) cimg_mapXY(piece,x,y) if (piece(x,y)) nboard(cx-piece.dimx()/2+x,cy-piece.dimy()/2+y)=piece(x,y);
cimg_mapXY(board,xx,yy) if (nboard(xx,yy)) visu.draw_image(blocs[nboard(xx,yy)-1],xx*blocdim,yy*blocdim);
visu.draw_text(5,5,white,NULL,1,"Lines : %d",score,nn).draw_text(visu.dimx()-75,5,white,NULL,1,"Next :");
if (next.data) visu.draw_image(next,next_mask,visu.dimx()-next.dimx()-2,10-next.dimy()/2).display(disp).wait(20);
if (cn<0) {
// Introduce a new piece on the board (if necessary) and create representation of the next piece
board = nboard;
piece = pieces[cn=nn];
nn = rand()%pieces.size;
cx = board.dimx()/2;
cy = piece.dimy()/2;
next = CImg<unsigned char>(pieces[nn].dimx()*blocdim,pieces[nn].dimy()*blocdim,1,3,0);
cimg_mapXY(pieces[nn],xi,yi) if (pieces[nn](xi,yi)) next.draw_image(blocs[pieces[nn](xi,yi)-1],xi*blocdim,yi*blocdim);
next_mask = next.resize(-50,-50).get_norm_pointwise().threshold(0);
// Detect tetris lines and do line removal animation if found.
cimg_mapY(board,yyy) {
int Y = yyy*blocdim, line = 1;
cimg_mapX(board,xxx) if (!board(xxx,yyy)) line=0;
if (line) {
board.draw_image(board.get_crop(0,0,board.dimx()-1,yyy-1),0,1);
if (!((++score)%5)) speed=(unsigned int)(speed*0.9f);
for (float alpha=0; alpha<=1; alpha+=0.07f)
CImg<unsigned char>(visu).draw_image(background.get_crop(0,Y,visu.dimx()-1,Y+blocdim-1),0,Y,0,0,alpha).display(disp).wait(20);
visu.draw_image(background.get_crop(0,Y,visu.dimx()-1,Y+blocdim-1),0,Y,0,0);
}
}
}
// Handle motion & collisions
const int ox=cx, oy=cy;
bool rotated = false, collision;
switch (disp.key) {
case cimg::keyP: pause = true; break;
case cimg::keyARROWUP: piece.rotate(90); rotated = true; disp.key=0; break;
case cimg::keyARROWLEFT: cx--; disp.key=0; break;
case cimg::keyARROWRIGHT: cx++; disp.key=0; break;
}
if (cx-piece.dimx()/2<0) cx=piece.dimx()/2;
if (cy-piece.dimy()/2<0) cy=piece.dimy()/2;
if (cx+(piece.dimx()-1)/2>=board.dimx()) cx = board.dimx()-1-(piece.dimx()-1)/2;
// Detect collision along the X axis
collision = false; cimg_mapXY(piece,i,j) if (piece(i,j) && board(cx-piece.dimx()/2+i,cy-piece.dimy()/2+j)) collision = true;
if (collision) { cx=ox; if (rotated) piece.rotate(-90); }
if (disp.key==cimg::keyARROWDOWN || !((++time)%speed)) { cy++; disp.key=0; }
// Detect collisiong along the Y axis
collision = false; cimg_mapXY(piece,ii,jj) if (piece(ii,jj) && board(cx-piece.dimx()/2+ii,cy-piece.dimy()/2+jj)) collision = true;
if (collision || cy+(piece.dimy()-1)/2>=board.dimy()) { cy = oy; cn=-1; }
if (collision && cy==piece.dimy()/2) gameover=true;
} else {
// If game is paused (key 'P'), do a little text animation
float A = 0, B = 0;
CImg<float> pauselogo = CImg<unsigned char>(80,22,1,1,0).draw_text("Game Paused\nPress a key",0,0,white);
disp.key=0; while (!disp.key && !disp.closed) {
CImg<float> pauserotated = pauselogo.get_rotate((float)(30*std::sin(A)),5).
resize((int)(-150-80*std::sin(B)),(int)(-150-80*std::sin(B)));
A+=0.08f; B+=0.043f;
#if ( !defined(_MSC_VER) || _MSC_VER>1200 )
CImg<unsigned char>(background).
draw_image<float,float>(pauserotated.get_resize(-100,-100,1,3,2),pauserotated,
(background.dimx()-pauserotated.dimx())/2,(background.dimy()-pauserotated.dimy())/2,0,0,255).display(disp).wait(20);
#else
CImg<unsigned char>(background).
draw_image(pauserotated.get_resize(-100,-100,1,3,2),
(background.dimx()-pauserotated.dimx())/2,(background.dimy()-pauserotated.dimy())/2,0,0,255).display(disp).wait(20);
#endif
}
disp.key=0;
pause = false;
}
}
// End of game reached, display the score and do a 'game over' animation
char tmp[1024];
std::sprintf(tmp,"Game Over !\n\nYour score : %d",score);
cimg::dialog("CImg Tetris",tmp,"Quit");
return 0;
}
Bu forumda yeni konular açamazsınız Bu forumdaki mesajlara cevap veremezsiniz Bu forumdaki mesajlarınızı değiştiremezsiniz Bu forumdaki mesajlarınızı silemezsiniz Bu forumdaki anketlerde oy kullanamazsınız