1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL_image.h>
#include "constantes.h"
#include "formes.h"
#include "event.h"
int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL, *carreBlanc = NULL, *carreBleu = NULL, *carreJaune = NULL, *carreRouge = NULL, *carreVert = NULL, *carreViolet = NULL, *carreOrange = NULL, *temoin = NULL;
SDL_Event event;
SDL_Rect positionCarre[4], positionForme, positionCarte, positionTemoin;
Input in;
int continuer = 1, carte[LARGEUR_ECRAN][HAUTEUR_ECRAN] = {0}, carteForme[TAILLE_FORME][TAILLE_FORME] = {0}, i = 0, j = 0, z = 0;
int tempsActuel = 0, tempsPrecedent = 0, gagne = 0, collision = 0;
SDL_Init(SDL_INIT_VIDEO);
ecran = SDL_SetVideoMode(LARGEUR_ECRAN, LARGEUR_ECRAN, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE);
SDL_WM_SetCaption("Tetriiiiis !", NULL);
carreBlanc = IMG_Load("images\\blanc.jpg");
carreBleu = IMG_Load("images\\bleu.jpg");
carreJaune = IMG_Load("images\\jaune.jpg");
carreRouge = IMG_Load("images\\rouge.jpg");
carreVert = IMG_Load("images\\vert.jpg");
carreViolet = IMG_Load("images\\violet.jpg");
carreOrange = IMG_Load("images\\orange.jpg");
temoin = IMG_Load("images\\temoin.jpg");
// Pour tester les collisions je place quelques blocs dans la carte
carte[NB_BLOCS_LARGEUR / 2][NB_BLOCS_HAUTEUR - 1] = 1;
carte[(NB_BLOCS_LARGEUR / 2 )- 1][NB_BLOCS_HAUTEUR - 2] = 1;
carte[(NB_BLOCS_LARGEUR / 2) - 1][NB_BLOCS_HAUTEUR - 1] = 1;
carte[(NB_BLOCS_LARGEUR / 2) - 1][NB_BLOCS_HAUTEUR - 3] = 1;
// On initalise la position de la forme
positionForme.x = (NB_BLOCS_LARGEUR / 2) - 2 ;
positionForme.y = -2;
// on génére une forme
creerForme(carteForme);
SDL_EnableKeyRepeat(100, 100);
memset(&in, 0, sizeof(in));
while(!in.key[SDLK_ESCAPE] && !in.quit)
{
UpdateEvents(&in);
tempsActuel = SDL_GetTicks();
if((tempsActuel - tempsPrecedent) > 900)
{
if(verifierCollisionCarte(positionForme, carte, carteForme))
{
positionForme.y++;
tempsPrecedent = tempsActuel;
}
}
else
{
SDL_Delay(1);
}
if(in.key[SDLK_UP])
{
tournerForme(carteForme, positionForme);
in.key[SDLK_UP] = 0;
}
if(in.key[SDLK_DOWN] && verifierCollisionCarte(positionForme, carte, carteForme))
{
positionForme.y++;
in.key[SDLK_DOWN] = 0;
}
if(in.key[SDLK_LEFT])
{
positionForme.x--;
in.key[SDLK_LEFT] = 0;
}
if(in.key[SDLK_RIGHT])
{
positionForme.x++;
in.key[SDLK_RIGHT] = 0;
}
if(in.key[SDLK_SPACE])
{
tournerForme(carteForme, positionForme);
in.key[SDLK_SPACE] = 0;
}
if(!gagne)
{
// positionTemoin.x = positionForme.x * TAILLE_BLOC;
// positionTemoin.y = positionForme.y * TAILLE_BLOC;
// SDL_BlitSurface(temoin, NULL, ecran, &positionTemoin);
z =0;
for (i = 0; i < TAILLE_FORME; i++)
{
for (j = 0; j < TAILLE_FORME; j++)
{
if (carteForme[i][j])
{
positionCarre[z].x = (positionForme.x + i) * TAILLE_BLOC;
positionCarre[z].y = (positionForme.y + j) * TAILLE_BLOC;
// On blite le carrés selon la couleur
switch(carteForme[i][j])
{
case BLANC:
SDL_BlitSurface(carreBlanc, NULL, ecran, &positionCarre[z]);
break;
case BLEU:
SDL_BlitSurface(carreBleu, NULL, ecran, &positionCarre[z]);
break;
case JAUNE:
SDL_BlitSurface(carreJaune, NULL, ecran, &positionCarre[z]);
break;
case VERT:
SDL_BlitSurface(carreVert, NULL, ecran, &positionCarre[z]);
break;
case VIOLET:
SDL_BlitSurface(carreViolet, NULL, ecran, &positionCarre[z]);
break;
case ROUGE:
SDL_BlitSurface(carreRouge, NULL, ecran, &positionCarre[z]);
break;
case ORANGE:
SDL_BlitSurface(carreOrange, NULL, ecran, &positionCarre[z]);
break;
}
}
}
}
for (i = 0; i < NB_BLOCS_LARGEUR; i++)
{
for (j = 0; j < NB_BLOCS_HAUTEUR; j++)
{
if(carte[i][j])
{
positionCarte.x = i * TAILLE_BLOC;
positionCarte.y = j * TAILLE_BLOC;
SDL_BlitSurface(carreOrange, NULL, ecran, &positionCarte);
}
}
}
}
SDL_Flip(ecran);
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255,255,255));
}
return 0;
} |
Partager