コード例 #1
0
ファイル: menu.c プロジェクト: MickaelSERENO/Tetris
void updateLabelColor(Menu *menu, SDL_Renderer *windowRenderer, SDL_Rect *mouseRect)
{
	/* The color for the labels. */
	SDL_Color inColor = {255, 255, 255};
	SDL_Color outColor = {200, 200, 200};

	if(rectCollision(*mouseRect, menu->quitRect))
	{
		if(menu->quitColor.r != 255)
		{
			menu->quitColor = inColor;
			SDL_DestroyTexture(menu->quitLabel);
			SDL_Surface* quitSurface = TTF_RenderText_Solid(menu->font, "Quit", menu->quitColor);
			menu->quitLabel = SDL_CreateTextureFromSurface(windowRenderer, quitSurface);
			SDL_FreeSurface(quitSurface);
		}
	}

	else
	{
		if(menu->quitColor.r != 200)
		{
			menu->quitColor = outColor;
			SDL_DestroyTexture(menu->quitLabel);
			SDL_Surface* quitSurface = TTF_RenderText_Solid(menu->font, "Quit", menu->quitColor);
			menu->quitLabel = SDL_CreateTextureFromSurface(windowRenderer, quitSurface);
			SDL_FreeSurface(quitSurface);
		}
	}

	if(rectCollision(*mouseRect, menu->startRect))
	{
		if(menu->startColor.r != 255)
		{
			menu->startColor = inColor;
			SDL_DestroyTexture(menu->startLabel);
			SDL_Surface *startSurface = TTF_RenderText_Solid(menu->font, "Start", menu->startColor);
			menu->startLabel = SDL_CreateTextureFromSurface(windowRenderer, startSurface);
			SDL_FreeSurface(startSurface);
		}
	}

	else
	{
		if(menu->startColor.r != 200)
		{
			menu->startColor = outColor;
			SDL_DestroyTexture(menu->startLabel);
			SDL_Surface *startSurface = TTF_RenderText_Solid(menu->font, "Start", menu->startColor);
			menu->startLabel = SDL_CreateTextureFromSurface(windowRenderer, startSurface);
			SDL_FreeSurface(startSurface);
		}
	}
}
コード例 #2
0
ファイル: menu.c プロジェクト: MickaelSERENO/Tetris
void firstMenu(Menu *menu, SDL_Renderer *windowRenderer, Context *context)
{
	/* Get the mouse position */
	SDL_Rect mouseRect;
	mouseRect.w = 0;
	mouseRect.h = 0;
	Uint32 mouseState = SDL_GetMouseState(&mouseRect.x, &mouseRect.y);

	/* Update the color of the labels */
	updateLabelColor(menu, windowRenderer, &mouseRect);

	/* Draw the label on the renderer */
	SDL_RenderCopy(windowRenderer, menu->quitLabel, NULL, &menu->quitRect);
	SDL_RenderCopy(windowRenderer, menu->startLabel, NULL, &menu->startRect);

	/* Test the click events. */
	if(rectCollision(mouseRect, menu->quitRect) && (mouseState & SDL_BUTTON(1)))
		*context = QUIT;
	else if(rectCollision(mouseRect, menu->startRect) && (mouseState & SDL_BUTTON(1)))
		*context = GAME;
}
コード例 #3
0
ファイル: map.cpp プロジェクト: notherkappa/tanks
void GameMap::removeObject(TRect point)
{
        TRect r;
        TStringList * l = map.GetList("objects");
        for (int i=0; i<l->Count; i++)
        {
                String name = l->Strings[i];
                r.left = StrToInt(map.Get("objects."+name+".left"));
                r.top  = StrToInt(map.Get("objects."+name+".top"));
                r.right = r.left+32;
                r.bottom = r.top +32;
                if (rectCollision(convertRect(r),convertRect(point)))
                        map.RemoveFromList("objects",l->Strings[i]);
        }
        delete l;
}
コード例 #4
0
ファイル: InGame.c プロジェクト: MickaelSERENO/ET3_Project_C
void InGame_updatePlayer(InGame* self)
{
	if(!self->map)
		return;

	//4 tiles / object / dynamic tiles are needed because the rect player has 4 points.
	Tile* topLeftTile         = NULL;
	Tile* topRightTile        = NULL;
	Tile* bottomLeftTile      = NULL;
	Tile* bottomRightTile     = NULL;

	Tile* topLeftDynamics     = NULL;
	Tile* topRightDynamics    = NULL;
	Tile* bottomLeftDynamics  = NULL;
	Tile* bottomRightDynamics = NULL;

	//Update the player gravity
	Player_updateGravity(self->player);

	//Then check collisions
	const SDL_Rect* pRect = Drawable_getRect((Drawable*)self->player);
	bottomLeftTile        = Map_getTileInfo(self->map, pRect->x, pRect->y + pRect->h + 1); //+1 are here to check if we will be on collision if we move the player by gravity of 1 pixel.
	bottomRightTile       = Map_getTileInfo(self->map, pRect->x + pRect->w, pRect->y + pRect->h + 1);
	topLeftTile           = Map_getTileInfo(self->map, pRect->x, pRect->y);
	topRightTile          = Map_getTileInfo(self->map, pRect->x + pRect->w, pRect->y);

	//If the tile on our foot is something SOLID, such as ground
	if(bottomLeftTile && (Tile_getInfo(bottomLeftTile) & SOLID) ||
	   bottomRightTile && (Tile_getInfo(bottomRightTile) & SOLID))
    {
		//Get the tile rect on where we entered in collision
		const SDL_Rect* tileRect;
		if(bottomRightTile)
		{
			bottomRightTile->updateCollision(bottomRightTile);
			tileRect = Drawable_getRect((Drawable*)bottomRightTile);
		}

		else if(bottomLeftTile)
		{
			bottomLeftTile->updateCollision(bottomLeftTile);
			tileRect = Drawable_getRect((Drawable*)bottomLeftTile);
		}
		int32_t y = tileRect->y - 1;
		((Drawable*)self->player)->setPosition((Drawable*)self->player, pRect->x, y - pRect->h); //The reposition the player correctly
		Player_setSpeedY(self->player, 0); //And set its gravity to 0
    }

	//The same for tile on our head
	else if(topLeftTile && (Tile_getInfo(topLeftTile) & SOLID) ||
	        topRightTile && (Tile_getInfo(topRightTile) & SOLID))
    {
		const SDL_Rect* tileRect;
		if(topRightTile)
		{
			topRightTile->updateCollision(topRightTile);
			tileRect = Drawable_getRect((Drawable*)topRightTile);
		}
		if(topLeftTile)
		{
			topLeftTile->updateCollision(topLeftTile);
			tileRect = Drawable_getRect((Drawable*)topLeftTile);
		}
		int32_t y = tileRect->y + tileRect->h + 1;
		((Drawable*)self->player)->setPosition((Drawable*)self->player, pRect->x, y);
		Player_setSpeedY(self->player, 0);
    }

	//We we aren't not on something, then we fall.
	else
		Player_setSpeedY(self->player, Player_getSpeedY(self->player) + GRAVITY);

    //Check on dynamic side
    bottomLeftDynamics  = Map_getDynamicTile(self->map, pRect->x, pRect->y + pRect->h);
    bottomRightDynamics = Map_getDynamicTile(self->map, pRect->x + pRect->w, pRect->y + pRect->h);

    uint32_t i;

	//Useful for not doing the work 4 times (we can use a loop)
	Tile* dynamicTile[4];
	dynamicTile[0] = bottomLeftDynamics;
	dynamicTile[1] = bottomRightDynamics;
	dynamicTile[2] = NULL;
	dynamicTile[3] = NULL;
	uint32_t dynamicTileID, sizeDynamicTile = 4;
	const SDL_Rect* r = Drawable_getRect((Drawable*)self->player);

	//Loop for checking collisions with all dynamic tiles
	for(dynamicTileID=0; dynamicTileID < sizeDynamicTile; dynamicTileID++)
	{
		//If we haven't a dynamic tile on this case
		if(!dynamicTile[dynamicTileID])
			continue;

		Tile* tile = dynamicTile[dynamicTileID];

		//Or if the dynamic tile is not an ennemy
		if(!(Tile_getInfo(tile) & ENNEMY))
			continue;

		//Or if the ennemy isn't destroyd yet (it lives)
		else if(tile && !tile->canDestroy)
		{
			//Look if indeed we are on collision. It is needed because dynamic tiles don't fit all the dynamic cases (definition of dynamic cases : all the tiles can be nowhere of any size.
			const SDL_Rect* tileRect = Drawable_getRect((Drawable*)tile);
			if(rectCollision(tileRect, pRect))
			{
				//If we fall, then the ennemy is killed
				if(self->player->speedY > 0)
				{
					InGame_addScore(self, 100);
					tile->canDestroy = true;
					Player_setSpeedY(self->player, JUMP_SPEED);
				}
			}
		}
	}

	//Check if something solid is on our side. We do the same thing that for falling.
	Player_updateMovement(self->player);
	pRect           = Drawable_getRect((Drawable*)self->player);
	bottomLeftTile  = Map_getTileInfo(self->map, pRect->x, pRect->y + pRect->h);
	bottomRightTile = Map_getTileInfo(self->map, pRect->x + pRect->w, pRect->y + pRect->h);
	topLeftTile     = Map_getTileInfo(self->map, pRect->x, pRect->y);
	topRightTile    = Map_getTileInfo(self->map, pRect->x + pRect->w, pRect->y);

	//on left
	if(bottomLeftTile && (Tile_getInfo(bottomLeftTile) & SOLID) ||
	   topLeftTile && (Tile_getInfo(topLeftTile) & SOLID))
    {
		const SDL_Rect* tileRect;
		if(topLeftTile)
		{
			topLeftTile->updateCollision(topLeftTile);
			tileRect = Drawable_getRect((Drawable*)topLeftTile);
		}
		if(bottomLeftTile)
		{
			bottomLeftTile->updateCollision(bottomLeftTile);
			tileRect = Drawable_getRect((Drawable*)bottomLeftTile);
		}

		uint32_t x = tileRect->x + tileRect->w + 1;
		((Drawable*)self->player)->setPosition((Drawable*)self->player, x, pRect->y);
    }

	//on right
	else if(topRightTile && (Tile_getInfo(topRightTile) & SOLID) ||
	        bottomRightTile && (Tile_getInfo(bottomRightTile) & SOLID))
    {
		const SDL_Rect* tileRect;
		if(topRightTile)
		{
			topRightTile->updateCollision(topRightTile);
			tileRect = Drawable_getRect((Drawable*)topRightTile);
		}
		if(bottomRightTile)
		{
			bottomRightTile->updateCollision(bottomRightTile);
			tileRect = Drawable_getRect((Drawable*)bottomRightTile);
		}

		int32_t x = tileRect->x - 1;
		((Drawable*)self->player)->setPosition((Drawable*)self->player, x - pRect->w, pRect->y);
    }

	//Reload tile information.
	pRect           = Drawable_getRect((Drawable*)self->player);
	bottomLeftTile  = Map_getTileInfo(self->map, pRect->x, pRect->y + pRect->h);
	bottomRightTile = Map_getTileInfo(self->map, pRect->x + pRect->w, pRect->y + pRect->h);
	topLeftTile     = Map_getTileInfo(self->map, pRect->x, pRect->y);
	topRightTile    = Map_getTileInfo(self->map, pRect->x + pRect->w, pRect->y);

	Object* bottomLeftObject  = Map_getObjectInfo(self->map, pRect->x, pRect->y + pRect->h);
	Object* bottomRightObject = Map_getObjectInfo(self->map, pRect->x + pRect->w, pRect->y + pRect->h);
	Object* topLeftObject     = Map_getObjectInfo(self->map, pRect->x, pRect->y);
	Object* topRightObject    = Map_getObjectInfo(self->map, pRect->x + pRect->w, pRect->y);

	//Then check if we are on a coin
	Tile* tiles[4];
	tiles[0] = bottomLeftTile;
	tiles[1] = bottomRightTile;
	tiles[2] = topRightTile;
	tiles[3] = topLeftTile;
	for(i=0; i < 4; i++)
	{
		if(tiles[i] && !tiles[i]->canDestroy && Tile_getInfo(tiles[i]) & SCORE)
		{
			tiles[i]->updateCollision(tiles[i]);
			MusicManager_playSound(globalVar_musics, COIN_SOUND);
			InGame_addScore(self, 100);
		}
	}

	Object* objects[4];
	objects[0] = bottomLeftObject;
	objects[1] = bottomRightObject;
	objects[2] = topLeftObject;
	objects[3] = topRightObject;

	for(i=0; i < 4; i++)
	{
		if(objects[i] && (Object_getInfo(objects[i]) & FINISH) && !self->hasWon)
		{
			objects[i]->updateCollision(objects[i]);
			self->hasWon = true;
		}
	}
	   
	//Then recheck on dynamic trace
    bottomLeftDynamics  = Map_getDynamicTile(self->map, pRect->x, pRect->y + pRect->h);
    bottomRightDynamics = Map_getDynamicTile(self->map, pRect->x + pRect->w, pRect->y + pRect->h);
    topLeftDynamics  = Map_getDynamicTile(self->map, pRect->x, pRect->y);
    topRightDynamics = Map_getDynamicTile(self->map, pRect->x + pRect->w, pRect->y);

	//Same thing that for falling
	dynamicTile[0] = bottomLeftDynamics;
	dynamicTile[1] = bottomRightDynamics;
	dynamicTile[2] = topRightDynamics;
	dynamicTile[3] = topLeftDynamics;
	for(dynamicTileID=0; dynamicTileID < sizeDynamicTile; dynamicTileID++)
	{
		if(!dynamicTile[dynamicTileID])
			continue;
		Tile* tile = dynamicTile[dynamicTileID];

		if(!(Tile_getInfo(tile) & ENNEMY))
			continue;

		else if(tile && !tile->canDestroy)
		{
			//Except that we die if we touch an ennemy
			const SDL_Rect* tileRect = Drawable_getRect((Drawable*)tile);
			if(rectCollision(tileRect, pRect))
			{
				self->hasDied = true;
				return;
			}
		}
	}
}