예제 #1
0
void Play(context_t *ctxt)
{
    SDL_bool won = SDL_TRUE;
    SDL_Event e;
    int i;
    GenBoard(ctxt);
    SDL_FreeSurface(ctxt->screen);
    ctxt->screen = SDL_SetVideoMode(ctxt->columns*WIDTH_CELL, ctxt->rows*HEIGHT_CELL, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    BlitAll(ctxt, SDL_TRUE);
    SDL_Flip(ctxt->screen);
    while(won && !isover(ctxt))
    {
        SDL_WaitEvent(&e);
        if(e.type == SDL_MOUSEBUTTONDOWN)
        {
            if(e.button.button == SDL_BUTTON_LEFT && !ctxt->board[(e.button.y/HEIGHT_CELL)*ctxt->columns + e.button.x/WIDTH_CELL].flagged)
            {
                if(ctxt->board[(e.button.y/HEIGHT_CELL)*ctxt->columns + e.button.x/WIDTH_CELL].value == MINE)
                    won = SDL_FALSE;
                else
                    ExploreCell(ctxt, e.button.x/WIDTH_CELL, e.button.y/HEIGHT_CELL);
            }
            else if(e.button.button == SDL_BUTTON_RIGHT)
            {
                if(!ctxt->board[(e.button.y/HEIGHT_CELL)*ctxt->columns + e.button.x/WIDTH_CELL].visited)
                    ctxt->board[(e.button.y/HEIGHT_CELL)*ctxt->columns + e.button.x/WIDTH_CELL].flagged =
                                    1 - ctxt->board[(e.button.y/HEIGHT_CELL)*ctxt->columns + e.button.x/WIDTH_CELL].flagged;;
            }
            else if(e.button.button == SDL_BUTTON_MIDDLE)
            {
                if(ctxt->board[(e.button.y/HEIGHT_CELL)*ctxt->columns + e.button.x/WIDTH_CELL].visited)
                    if(CountFlagsAround(ctxt, e.button.x/WIDTH_CELL, e.button.y/HEIGHT_CELL) ==
                       ctxt->board[(e.button.y/HEIGHT_CELL)*ctxt->columns + e.button.x/WIDTH_CELL].value)
                        for(i = 0; i < 8; ++i)
                            ExploreCell(ctxt, e.button.x/WIDTH_CELL + moveX[i], e.button.y/HEIGHT_CELL + moveY[i]);
            }
            BlitAll(ctxt, SDL_FALSE);
            SDL_Flip(ctxt->screen);
        }
    }
    if(won)
    {
        SDL_Delay(2000);
    }
    else
    {
        BlitAll(ctxt, SDL_TRUE);
        SDL_Flip(ctxt->screen);
        SDL_Delay(1000);
    }
}
예제 #2
0
파일: TGui.cpp 프로젝트: doremi/pxgui
void  TGui::RedrawAll()
{
    dirty.x = dirty.y = 0;
    dirty.w = DEFAULT_SCREEN_WIDTH;
    dirty.h = DEFAULT_SCREEN_HEIGHT;

    if (background)
        SDL_BlitSurface( background, &dirty, surface, &dirty );

    // draw all elements
    BlitAll();

    // update main screen area
    SDL_BlitSurface( surface, &dirty, screen, &dirty );

    // and flip it
    SDL_Flip(screen);

#ifdef USE_DOUBLE_BUFFER
    SDL_BlitSurface( surface, &dirty, screen, &dirty );
    SDL_Flip(screen);
#endif

}
예제 #3
0
파일: game.c 프로젝트: TeoJudes/Snake
int game(SDL_Surface* Screen, int score)
    {
        int keep=1;
        int timebg=0, time=0;
        int sizesnake = 1;
        int direction = RIGHT;
        int i=0, j=0;
        SDL_Surface *Wall = NULL, *Snake = NULL, *Empty = NULL, *Mush;
        SDL_Rect position;
            position.x;
            position.y;
        int map[BLOCK_WIDTH][BLOCK_HEIGHT]= {{0}};
        SDL_Event event;

        Snake = SDL_LoadBMP("BodySnake.bmp");
        Wall = SDL_LoadBMP("Wall.bmp");
        Empty = SDL_LoadBMP("MorceauFond.bmp");
        Mush = SDL_LoadBMP("mush.bmp");

        buildmap(map);

       while (keep)
            {
                SDL_PollEvent(&event);
                switch(event.type)
                {
                    case SDL_QUIT:
                    keep = 0;
                    break;

                    case SDL_KEYDOWN:
                        switch (event.key.keysym.sym)
                        {
                            case SDLK_DOWN:
                                        direction = BOT;
                                    break;

                            case SDLK_UP:
                                        direction = TOP;
                                    break;

                            case SDLK_LEFT:
                                        direction = LEFT;
                                    break;

                            case SDLK_RIGHT:
                                        direction = RIGHT;
                                break;

                            case SDLK_ESCAPE:
                                    keep = 0;
                                    break;
                        default:
                            break;

                            }
                        break;

                }

            time = SDL_GetTicks();

            if ( (time - timebg) > 80 )

                {
                    keep = movement(map , direction, sizesnake);
                    timebg=time;
                }
                BlitAll(map, Screen, Wall ,Empty, Snake, Mush);
                SDL_Flip(Screen);
            }
            
        SDL_Surface *Echec = NULL;//chargement de l'écran en cas d'échec
        SDL_Rect posEchec;
        posEchec.x = 0;
        posEchec.y = 0;
        Echec = SDL_LoadBMP("Echec.bmp");


        SDL_FillRect(Screen, NULL, SDL_MapRGB(Screen->format, 0, 0, 0));
        SDL_BlitSurface(Echec, NULL, Screen, &posEchec);
        SDL_Flip(Screen);

    return EXIT_SUCCESS;
    }
예제 #4
0
파일: TGui.cpp 프로젝트: doremi/pxgui
// Redraw iterates through elements list looking for bInvalidRect flag
// and computes one global dirty rectangle for updating changes 
void  TGui::Redraw()
{
	// find the smallest possible update rectangle
	//   [sx,sy]----+
	//    |         |
	//    |         |
	//    |         |
	//    +-----[dx,dy]

    if (!active) {
        SDL_Flip(screen);
        return;
    }

	int i = 0;
	int sx, sy, dx, dy, xl, xh, yl, yh;
	xl = xh = yl = yh = 0;
	sx = DEFAULT_SCREEN_WIDTH; // set dirty to max
	sy = DEFAULT_SCREEN_HEIGHT;
	dx = 0;
	dy = 0;

#if 1
	while(zList[i]) {
		if(zList[i]->bInvalidRect) {
			xl = zList[i]->x < zList[i]->lastx ? zList[i]->x : zList[i]->lastx;
			xh = zList[i]->x > zList[i]->lastx ? zList[i]->x+zList[i]->width : zList[i]->lastx+zList[i]->width;
			sx = sx > xl ? xl : sx;
			dx = dx < xh ? xh : dx;

			yl = zList[i]->y < zList[i]->lasty ? zList[i]->y : zList[i]->lasty;
			yh = zList[i]->y > zList[i]->lasty ? zList[i]->y+zList[i]->height : zList[i]->lasty+zList[i]->height;
			sy = sy > yl ? yl : sy;
			dy = dy < yh ? yh : dy;

			// add invalid rectangle into the dirty list
			zList[i]->bInvalidRect = false;
		}
		i++;
	}
	// compute the global dirty rectangle

    if (sx == DEFAULT_SCREEN_WIDTH && sy == DEFAULT_SCREEN_HEIGHT) {
        dirty.x = dirty.y = 0;
        dirty.w = DEFAULT_SCREEN_WIDTH;
        dirty.h = DEFAULT_SCREEN_HEIGHT;
    } else {
	dirty.x = sx;
	dirty.y = sy;
	dirty.w = dx-sx;
	dirty.h = dy-sy;
    }
#endif

	//cout << "DRCT: " << dirty.x << ", " << dirty.y << ", " << dirty.w << ", " << dirty.h << "\n";
//    printf("DRCT: %d, %d, %d, %d\n", dirty.x, dirty.y, dirty.w, dirty.h);

	// restore background
    if (background)
    	SDL_BlitSurface( background, &dirty, surface, &dirty );

	// draw all elements
	BlitAll();

	// update main screen area
	SDL_BlitSurface( surface, &dirty, screen, &dirty );

	// and flip it
    SDL_Flip(screen);
}