First commit, compiles without sound for now. Need to change controls, levels, remove some menus, and make sound work

This commit is contained in:
Vincent-FK
2021-06-04 09:37:35 +02:00
parent 26217d9cf0
commit 9c6fec17a2
188 changed files with 7104 additions and 0 deletions

117
src/bomb.c Normal file
View File

@@ -0,0 +1,117 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "SDL.h"
#include "SDL_image.h"
#include "../include/game.h"
struct s_bomb {
int x, y, portee;
int joueur;
SDL_Surface * time_image[4];
int current_time; //contient 1 2 3 ou 4 selon l'etat de la bombe
t_bomb nxt;
};
static void bomb_load_img(t_bomb bomb,int time, const char *filename) {
bomb->time_image[time] = IMG_Load(filename);
if (!bomb->time_image[time])
error("IMG_Load: %s\n", IMG_GetError());
}
t_bomb bomb_init(t_bomb list_bombs, int x, int y, int portee, int joueur) {
t_bomb bomb = malloc(sizeof(*bomb));
if (!bomb)
error("Memory error");
bomb_load_img(bomb, 0, IMG_BOMB_TTL1);
bomb_load_img(bomb, 1, IMG_BOMB_TTL2);
bomb_load_img(bomb, 2, IMG_BOMB_TTL3);
bomb_load_img(bomb, 3, IMG_BOMB_TTL4);
bomb->current_time = 3;
bomb->x=x;
bomb->y=y;
bomb->portee=portee;
bomb->joueur=joueur;
bomb->nxt=NULL;
if( list_bombs== NULL)
{
return bomb;
}
else
{
t_bomb temp=list_bombs;
while(temp->nxt != NULL)
{
temp = temp->nxt;
}
temp->nxt = bomb;
return list_bombs;
}
}
int bomb_get_x(t_bomb bomb) {
assert(bomb != NULL);
return bomb->x;
}
int bomb_get_y(t_bomb bomb) {
assert(bomb != NULL);
return bomb->y;
}
int bomb_get_joueur(t_bomb bomb) {
assert(bomb != NULL);
return bomb->joueur;
}
t_bomb get_next_bomb(t_bomb bomb){
return bomb->nxt;
}
void set_next_bomb(t_bomb bomb, t_bomb nxt_bomb){
bomb->nxt=nxt_bomb;
}
int bomb_get_portee(t_bomb bomb) {
assert(bomb != NULL);
return bomb->portee;
}
void bomb_set_current_time(t_bomb bomb, int time) {
assert(bomb);
bomb->current_time = time;
}
int bomb_get_current_time(t_bomb bomb) {
assert(bomb);
return(bomb->current_time);
}
void bomb_free(t_bomb bomb) {
if (bomb != NULL){
assert(bomb);
int i;
for (i = 0; i < 4; i++)
SDL_FreeSurface(bomb->time_image[i]);
free(bomb);
}
}
void bomb_display(t_bomb bomb, SDL_Surface *screen) {
assert(bomb->time_image[bomb->current_time]);
assert(screen);
SDL_Rect place;
place.x = bomb->x * SIZE_BLOC;
place.y = bomb->y * SIZE_BLOC;
SDL_BlitSurface(bomb->time_image[bomb->current_time], NULL, screen,&place);
}

313
src/editeur.c Normal file
View File

@@ -0,0 +1,313 @@
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include "SDL_image.h"
#include "../include/constant.h"
#include "../include/editeur.h"
#include "../include/map.h"
#include "../include/misc.h"
void editeur(SDL_Surface *screen, int niveau)
{
SDL_Surface *wall = NULL, *box = NULL, *goal = NULL, *player = NULL,
*player_2 = NULL, *bomb_range_inc= NULL, *menu_empty= NULL, *menu_lives= NULL,
*menu_bombs= NULL, *menu_range= NULL, *menu_player_1= NULL, *menu_player_2= NULL, *temp= NULL;
SDL_Rect place;
SDL_Rect place_joueur_1;
SDL_Rect place_mouse;
SDL_Event event;
int continuer = 1, clicGaucheEnCours = 0, clicDroitEnCours = 0;
int objetActuel = CELL_WALL;
int i, j, xp1, yp1, player1_ok=1, xp2, yp2, player2_ok=1;
FILE *level=fopen("data/niveaux.lvl","r");
t_map map = map_load_dynamic(level,niveau,2);
#ifdef HW_SCREEN_RESIZE
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, SIZE_BLOC * map_get_width(map),
SIZE_BLOC * map_get_height(map), WINDOW_BPP, 0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(SIZE_BLOC * map_get_width(map),
SIZE_BLOC * map_get_height(map), WINDOW_BPP,
SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
#endif //HW_SCREEN_RESIZE
// Chargement des objets et du niveau
wall = load_image(IMG_MAP_WALL);
box = load_image(IMG_MAP_BOX);
goal = load_image(IMG_MAP_GOAL);
player = load_image(IMG_PLAYER_DOWN);
player_2 = load_image(IMG_PLAYER_2_DOWN);
bomb_range_inc=load_image(IMG_BONUS_BOMB_RANGE_INC);
menu_empty=load_image(IMG_MENU_EMPTY);
menu_lives=load_image(IMG_MENU_LIVES_1);
menu_bombs=load_image(IMG_MENU_BOMBS_1);
menu_range=load_image(IMG_MENU_RANGE_1);
menu_player_1=load_image(IMG_MENU_PLAYER_1);
menu_player_2=load_image(IMG_MENU_PLAYER_2);
temp=wall;
// Boucle infinie de l'<27>diteur
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
{
// On met l'objet actuellement choisi (wall, box...) <20> l'endroit du clic
if (objetActuel==CELL_PLAYER){
if(player1_ok!=1){
map_set_cell_type(map, xp1 , yp1, CELL_EMPTY);
}
map_set_cell_type(map, event.button.x / SIZE_BLOC, event.button.y / SIZE_BLOC, objetActuel);
xp1=event.button.x / SIZE_BLOC; yp1=event.button.y / SIZE_BLOC;
player1_ok--;
}
else if (objetActuel==CELL_PLAYER_2){
if(player2_ok!=1){
map_set_cell_type(map, xp2 , yp2, CELL_EMPTY);
}
map_set_cell_type(map, event.button.x / SIZE_BLOC, event.button.y / SIZE_BLOC, objetActuel);
xp2=event.button.x / SIZE_BLOC; yp2=event.button.y / SIZE_BLOC;
player2_ok--;
}
else {
map_set_cell_type(map, event.button.x / SIZE_BLOC, event.button.y / SIZE_BLOC, objetActuel);
}
clicGaucheEnCours = 1; // On active un bool<6F>en pour retenir qu'un bouton est enfonc<6E>
}
else if (event.button.button == SDL_BUTTON_RIGHT) // Le clic droit sert <20> effacer
{
map_set_cell_type(map, event.button.x / SIZE_BLOC, event.button.y / SIZE_BLOC, CELL_EMPTY);
clicDroitEnCours = 1;
}
break;
case SDL_MOUSEBUTTONUP: // On d<>sactive le bool<6F>en qui disait qu'un bouton <20>tait enfonc<6E>
if (event.button.button == SDL_BUTTON_LEFT)
clicGaucheEnCours = 0;
else if (event.button.button == SDL_BUTTON_RIGHT)
clicDroitEnCours = 0;
break;
case SDL_MOUSEMOTION:
if (clicGaucheEnCours) // Si on d<>place la souris et que le bouton gauche de la souris est enfonc<6E>
{
if (objetActuel==CELL_PLAYER){
if(player1_ok!=1){
map_set_cell_type(map, xp1 , yp1, CELL_EMPTY);
}
map_set_cell_type(map, event.button.x / SIZE_BLOC, event.button.y / SIZE_BLOC, objetActuel);
xp1=event.button.x / SIZE_BLOC; yp1=event.button.y / SIZE_BLOC;
player1_ok--;
}
else if (objetActuel==CELL_PLAYER_2){
if(player2_ok!=1){
map_set_cell_type(map, xp2 , yp2, CELL_EMPTY);
}
map_set_cell_type(map, event.button.x / SIZE_BLOC, event.button.y / SIZE_BLOC, objetActuel);
xp2=event.button.x / SIZE_BLOC; yp2=event.button.y / SIZE_BLOC;
player2_ok--;
}
else {
map_set_cell_type(map, event.button.x / SIZE_BLOC, event.button.y / SIZE_BLOC, objetActuel);
}
}
else if (clicDroitEnCours) // Pareil pour le bouton droit de la souris
{
map_set_cell_type(map, event.motion.x / SIZE_BLOC, event.motion.y / SIZE_BLOC, CELL_EMPTY);
}
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
continuer = 0;
break;
case SDLK_s:
sauvegarderNiveau(map,niveau);
break;
case SDLK_KP1:
objetActuel = CELL_WALL;
temp=wall;
break;
case SDLK_KP2:
objetActuel = CELL_BOX;
temp=box;
break;
case SDLK_KP3:
objetActuel = CELL_GOAL;
temp=goal;
break;
case SDLK_KP4:
objetActuel = CELL_PLAYER;
temp=player;
break;
case SDLK_KP5:
objetActuel = (CELL_BONUS | (BONUS_BOMB_RANGE_INC << 4));
temp=bomb_range_inc;
break;
case SDLK_KP6:
objetActuel = CELL_MENU_EMPTY;
temp=menu_empty;
break;
case SDLK_KP7:
objetActuel = CELL_MENU_LIVES;
temp=menu_lives;
break;
case SDLK_KP8:
objetActuel = CELL_MENU_BOMBS;
temp=menu_bombs;
break;
case SDLK_KP9:
objetActuel = CELL_MENU_RANGE;
temp=menu_range;
break;
case SDLK_j:
objetActuel = CELL_PLAYER_2;
temp=player_2;
break;
case SDLK_k:
objetActuel = CELL_MENU_PLAYER_1;
temp=menu_player_1;
break;
case SDLK_l:
objetActuel = CELL_MENU_PLAYER_2;
temp=menu_player_2;
break;
default :
break;
}
break;
}
// Effacement de l'<27>cran
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
player1_ok=1;
player2_ok=1;
// Placement des objets <20> l'<27>cran
for (j = 0; j < map_get_height(map); j++) {
for (i = 0; i < map_get_width(map); i++) {
place.x = i * SIZE_BLOC;
place.y = j * SIZE_BLOC;
place_joueur_1.x=xp1* SIZE_BLOC;
place_joueur_1.y=yp1* SIZE_BLOC;
t_cell_type type = map_get_cell_type(map, i, j);
switch (type & 0x0f) {
case CELL_WALL:
SDL_BlitSurface(wall, NULL, screen, &place);
break;
case CELL_BOX:
SDL_BlitSurface(box, NULL, screen, &place);
break;
case CELL_GOAL:
SDL_BlitSurface(goal, NULL, screen, &place);
break;
case CELL_PLAYER:
if(player1_ok!=1){
map_set_cell_type(map, xp1 , yp1, CELL_EMPTY);
SDL_BlitSurface(player, NULL, screen, &place);
}
SDL_BlitSurface(player, NULL, screen, &place);
xp1=i; yp1=j;
player1_ok--;
break;
case CELL_PLAYER_2:
if(player2_ok!=1){
map_set_cell_type(map, xp2 , yp2, CELL_EMPTY);
SDL_BlitSurface(player, NULL, screen, &place);
}
SDL_BlitSurface(player_2, NULL, screen, &place);
xp2=i; yp2=j;
player2_ok--;
break;
case CELL_MENU_EMPTY:
SDL_BlitSurface(menu_empty, NULL, screen, &place);
break;
case CELL_MENU_PLAYER_1:
SDL_BlitSurface(menu_player_1, NULL, screen, &place);
break;
case CELL_MENU_PLAYER_2:
SDL_BlitSurface(menu_player_2, NULL, screen, &place);
break;
case CELL_MENU_LIVES:
SDL_BlitSurface(menu_lives, NULL, screen, &place);
break;
case CELL_MENU_BOMBS:
SDL_BlitSurface(menu_bombs, NULL, screen, &place);
break;
case CELL_MENU_RANGE:
SDL_BlitSurface(menu_range, NULL, screen, &place);
break;
case CELL_BONUS:
SDL_BlitSurface(bomb_range_inc, NULL, screen, &place);
break;
}
}
}
if(!map_is_inside(map, event.motion.x, event.motion.y)){
place_mouse.x = event.motion.x ;
place_mouse.y = event.motion.y ;
SDL_BlitSurface(temp, NULL, screen, &place_mouse);
}
// Mise <20> jour de l'<27>cran
#ifdef HW_SCREEN_RESIZE
SDL_FillRect(hw_screen, NULL, 0x000000);
flip_NNOptimized_AllowOutOfScreen(screen, hw_screen,
HW_SCREEN_WIDTH,
MIN(screen->h*HW_SCREEN_WIDTH/screen->w, HW_SCREEN_HEIGHT));
SDL_Flip(hw_screen);
#else //HW_SCREEN_RESIZE
SDL_Flip(screen);
#endif //HW_SCREEN_RESIZE
}
SDL_FreeSurface(wall);
SDL_FreeSurface(box);
SDL_FreeSurface(goal);
SDL_FreeSurface(player);
SDL_FreeSurface(bomb_range_inc);
SDL_FreeSurface(menu_empty);
SDL_FreeSurface(menu_lives);
SDL_FreeSurface(menu_bombs);
SDL_FreeSurface(menu_range);
}

191
src/flamme.c Normal file
View File

@@ -0,0 +1,191 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "SDL.h"
#include "SDL_image.h"
#include "../include/game.h"
struct s_flamme {
enum e_type_flamme type; // Dans l'ordre HAUT BAS GAUCHE DROITE HRZLEFT HRZRIGHT VERTUP VERTDOWN CENTER
int portee, x, y;
SDL_Surface * type_img[4];
int current_time; //contient 1 2 3 ou 4 selon l'etat d'avancement de la flamme
t_flamme nxt;
};
static void flamme_load_img(t_flamme flamme,int place_tableau, const char *filename) {
flamme->type_img[place_tableau] = IMG_Load(filename);
if (!flamme->type_img[place_tableau])
error("IMG_Load: %s\n", IMG_GetError());
}
t_flamme flamme_init(t_flamme list_flammes, int x, int y, int portee, enum e_type_flamme type) {
t_flamme flamme = malloc(sizeof(*flamme));
if (!flamme)
error("Memory error");
switch (type) {
case HAUT :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_HAUT);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_HAUT);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_HAUT);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_HAUT);
break;
case BAS :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_BAS);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_BAS);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_BAS);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_BAS);
break;
case GAUCHE :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_GAUCHE);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_GAUCHE);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_GAUCHE);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_GAUCHE);
break;
case DROITE :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_DROITE);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_DROITE);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_DROITE);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_DROITE);
break;
case HRZLEFT :
flamme_load_img(flamme,3, IMG_FLAMME_THIN_HRZ);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_HRZ);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_HRZ);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_HRZ);
break;
case HRZRIGHT :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_HRZ);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_HRZ);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_HRZ);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_HRZ);
break;
case VERTUP :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_VERT);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_VERT);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_VERT);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_VERT);
break;
case VERTDOWN :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_VERT);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_VERT);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_VERT);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_VERT);
break;
case CENTER :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_CENTER);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_CENTER);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_CENTER);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_CENTER);
break;
case M_BAS :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_MONSTER_BAS);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_MONSTER_BAS);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_MONSTER_BAS);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_MONSTER_BAS);
break;
case M_HAUT :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_MONSTER_HAUT);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_MONSTER_HAUT);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_MONSTER_HAUT);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_MONSTER_HAUT);
break;
case M_DROITE :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_MONSTER_DROITE);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_MONSTER_DROITE);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_MONSTER_DROITE);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_MONSTER_DROITE);
break;
case M_GAUCHE :
flamme_load_img(flamme,3 , IMG_FLAMME_THIN_MONSTER_GAUCHE);
flamme_load_img(flamme,2 , IMG_FLAMME_MEDIUM_MONSTER_GAUCHE);
flamme_load_img(flamme,1 , IMG_FLAMME_FIRE_MONSTER_GAUCHE);
flamme_load_img(flamme,0 , IMG_FLAMME_LARGE_MONSTER_GAUCHE);
break;
}
flamme->current_time = 3;
flamme->type = type;
flamme->x=x;
flamme->y=y;
flamme->portee=portee;
flamme->nxt=NULL;
if( list_flammes== NULL)
{
return flamme;
}
else
{
t_flamme temp=list_flammes;
while(temp->nxt != NULL)
{
temp = temp->nxt;
}
temp->nxt = flamme;
return list_flammes;
}
}
t_flamme get_next_flamme(t_flamme flamme){
return flamme->nxt;
}
void set_next_flamme(t_flamme flamme, t_flamme nxt_flamme){
flamme->nxt=nxt_flamme;
}
int flamme_get_x(t_flamme flamme) {
assert(flamme != NULL);
return flamme->x;
}
int flamme_get_y(t_flamme flamme) {
assert(flamme != NULL);
return flamme->y;
}
int flamme_get_portee(t_flamme flamme) {
assert(flamme != NULL);
return flamme->portee;
}
enum e_type_flamme flamme_get_type(t_flamme flamme) {
assert(flamme != NULL);
return flamme->type;
}
void flamme_decrease_current_time(t_flamme flamme) {
assert(flamme);
if (flamme->current_time >= 0)
flamme->current_time--;
}
int flamme_get_current_time(t_flamme flamme) {
assert(flamme);
return(flamme->current_time);
}
void flamme_free(t_flamme flamme) {
if (flamme != NULL){
assert(flamme);
int i;
for (i = 0; i < 36; i++)
SDL_FreeSurface(flamme->type_img[i]);
free(flamme);
}
}
void flamme_display(t_flamme flamme, SDL_Surface *screen) {
assert(screen);
SDL_Rect place;
place.x = flamme->x * SIZE_BLOC;
place.y = flamme->y * SIZE_BLOC;
SDL_BlitSurface(flamme->type_img[flamme->current_time], NULL, screen,&place);
}

1176
src/game.c Normal file

File diff suppressed because it is too large Load Diff

68
src/game_time.c Normal file
View File

@@ -0,0 +1,68 @@
/*
* time.c
*
* Created on: 16 mars 2010
* Author: reveille
*/
#include "../include/game_time.h"
#include "../include/constant.h"
static Uint32 timer1, timer2;
struct s_game_time {
// Number of cycles to process before displaying an image
Uint16 cycles_to_calculate;
// Current speed of the game
Uint16 speed;
// Duration of a cycle (in ms) ~ 14ms
Uint16 cycle_length;
};
static struct s_game_time the_game_time;
Uint16 game_time_get_cycles_to_calculate() {
return the_game_time.cycles_to_calculate;
}
Uint16 game_time_get_speed () {
return the_game_time.speed;
}
Uint16 game_time_get_cycles_length(){
return the_game_time.cycle_length;
}
void game_time_init() {
the_game_time.cycles_to_calculate = 0;
game_time_set_speed(DEFAULT_GAME_SPEED);
}
// Set the speed of the game (number of cycles per second)
void game_time_set_speed(Uint16 speed) {
if (speed == 0)
speed = 1;
the_game_time.speed = speed;
the_game_time.cycle_length = 1000 / speed;
timer1 = SDL_GetTicks();
}
void game_time_update() {
while (1) {
timer2 = SDL_GetTicks() - timer1;
if (timer2 >= the_game_time.cycle_length)
break;
else
SDL_Delay(3);
}
timer1 = SDL_GetTicks() - (timer2 % the_game_time.cycle_length);
the_game_time.cycles_to_calculate = timer2 / the_game_time.cycle_length;
//printf ("%d \n", the_game_time.cycles_to_calculate);
if (the_game_time.cycles_to_calculate > MAX_SKIPPED_FRAMES)
the_game_time.cycles_to_calculate = MAX_SKIPPED_FRAMES;
}

910
src/main.c Normal file
View File

@@ -0,0 +1,910 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h> // rand(), srand()
#include <time.h> // time()
#include <SDL.h>
#include <SDL_image.h>
#include "../include/constant.h"
#include "../include/game.h"
#include "../include/game_time.h"
#include "../include/editeur.h"
#include "../include/niveau.h"
#ifdef SOUND_FMOD_ACTIVATED
#include <FMOD/fmod.h>
#endif //SOUND_FMOD_ACTIVATED
#ifdef SOUND_SDL_ACTIVATED
#include "SDL_mixer.h"
#endif //SOUND_SDL_ACTIVATED
bool audio_init_ok = false;
#ifdef HW_SCREEN_RESIZE
SDL_Surface *hw_screen = NULL;
#endif //HW_SCREEN_RESIZE
SDL_Surface *screen=NULL;
//##################################################### input_update ####################################################################
//Gere les entrees clavier lors de la phase de jeu.
int input_update(t_game game, int nb_joueur) {
SDL_Event event;
t_player player1 = game_get_player1(game);
t_player player2 = NULL;
if (nb_joueur == 2)
player2 = game_get_player2(game);
t_map map = game_the_map(game);
int x, y;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
return 2;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
return 2;
case SDLK_UP:
player_set_current_way(player1, UP);
player_move(player1, map);
if (player_win(player1)==1){
return 1;
}
break;
case SDLK_DOWN:
player_set_current_way(player1, DOWN);
player_move(player1, map);
if (player_win(player1)==1){
return 1;
}
break;
case SDLK_RIGHT:
player_set_current_way(player1, RIGHT);
player_move(player1, map);
if (player_win(player1)==1){
return 1;
}
break;
case SDLK_LEFT:
player_set_current_way(player1, LEFT);
player_move(player1, map);
if (player_win(player1)==1){
return 1;
}
break;
case SDLK_END: //sert <20> poser une bombe pour le joueur 1 (cette touche sert
x= player_get_x(player1); //pour les ordinateurs portables qui n'ont pas forc<72>ment la touce 0 <20> c<>t<EFBFBD> des fl<66>ches directionnelles)
y= player_get_y(player1);
if (player_get_nb_bomb(player1)>0 && map_get_cell_type(map,x,y)!=CELL_BOMB){
int portee=player_portee_bomb(player1);
game_init_bomb(game,x,y,portee,1);
map_set_cell_type(map, x, y, CELL_BOMB);
player_decrease_nb_bomb(player1);
}
break;
case SDLK_KP0: //sert <20> poser une bombe pour le joueur 1
x= player_get_x(player1);
y= player_get_y(player1);
if (player_get_nb_bomb(player1)>0 && map_get_cell_type(map,x,y)!=CELL_BOMB){
int portee=player_portee_bomb(player1);
game_init_bomb(game,x,y,portee,1);
map_set_cell_type(map, x, y, CELL_BOMB);
player_decrease_nb_bomb(player1);
}
break;
// touches du joueur 2:
case SDLK_e:
if (nb_joueur == 2){
player_set_current_way(player2, UP);
player_move(player2, map);
if (player_win(player2)==1){
return 1;
}}
break;
case SDLK_d:
if (nb_joueur == 2){
player_set_current_way(player2, DOWN);
player_move(player2, map);
if (player_win(player2)==1){
return 1;
}}
break;
case SDLK_f:
if (nb_joueur == 2){
player_set_current_way(player2, RIGHT);
player_move(player2, map);
if (player_win(player2)==1){
return 1;
}}
break;
case SDLK_s:
if (nb_joueur == 2){
player_set_current_way(player2, LEFT);
player_move(player2, map);
if (player_win(player2)==1){
return 1;
}
}
break;
case SDLK_SPACE: //sert <20> poser une bombe pour le joueur 2
if (nb_joueur == 2){
x= player_get_x(player2);
y= player_get_y(player2);
if (player_get_nb_bomb(player2)>0 && map_get_cell_type(map,x,y)!=CELL_BOMB){
int portee=player_portee_bomb(player2);
game_init_bomb(game,x,y,portee,2);
map_set_cell_type(map, x, y, CELL_BOMB);
player_decrease_nb_bomb(player2);
}
}
break;
default: break;
}
break;
}
}
return 0;
}
//##################################################### Main_game ####################################################################
//Fonction principale une fois qu'une partie est lancee, gere l'actualisation du jeu.
//Cette fonction retourne un entier qui est le niveau <20> jouer ensuite (sert pour le mode 1 joueur)
int main_game(SDL_Surface *screen, int nb_joueur, int niveau, int mode, int kill_bomb, int game_over) {
//nb_joueur indique le nombre de joueurs
//niveau indique le niveau <20> charger
//mode indique si on est en mode jeu principal (dans ce cas mode=1) ou en mode "jouer aux niveaux <20>dit<69>s" (mode=2)
//kill_bomb indique si les bombes peuvent ou non s'exploser entre elles (non=0, oui(par d<>fault)=1)
//game_over indique combien de fois le joueur peut mourrir avant de faire game over. (3,2,1 ou 0)
srand(time(NULL));
SDL_Surface *menu = NULL;
SDL_Event event;
SDL_Rect positionMenu;
positionMenu.x = 0;
positionMenu.y = 0;
t_game game = game_new(nb_joueur,niveau,mode, kill_bomb); //on lance le jeu
#ifdef HW_SCREEN_RESIZE
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, SIZE_BLOC * map_get_width(game_the_map(game)),
SIZE_BLOC * map_get_height(game_the_map(game)), WINDOW_BPP,
0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(SIZE_BLOC * map_get_width(game_the_map(game)),
SIZE_BLOC * map_get_height(game_the_map(game)), WINDOW_BPP,
SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
#endif //HW_SCREEN_RESIZE
game_display(game, screen);
int done = 0; //variable qui indique quand doit s'arr<72>ter la boucle du jeu, elle peut prendre les valeurs 2,1, ou 0.
// La boucle s'arr<72>te pour les valeurs 2 et 1.
int k=1, l=1, m=1, n=1;
t_player player1 = game_get_player1(game);
t_player player2=NULL;
if (nb_joueur == 2)
player2 = game_get_player2(game);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
if (nb_joueur==1){ // boucle principale d'un jeu <20> 1 joueur:
while (done==0 && player_get_dead(player1)!=0) {
game_time_update();
if(player_get_lives(player1)!=0){
done = input_update(game, nb_joueur);
bombs_management(game,0);
if (k==BOMBS_SPEED){
k=1;
bombs_management(game,1); //Le management des bombes en jeu se fait tous les "BOMBS_SPEED"
// Le pam<61>tre 1 de cette fonction indique qu'elle doit g<>rer toutes les bombes en jeu
}
else k++;
if (l==MONSTER_SPEED){
l=1;
monsters_management(game,1); //Le management des monstres en jeu se fait tous les "MONSTER_SPEED"
// Le pam<61>tre 1 de cette fonction indique le nombre de joueurs
}
else l++;
}
else if (player_get_lives(player1)==0){
if (m==ANIMATION_SPEED){
m=1;
player_die(player1); // Quand le joueur meurt, le jeu s'arr<72>te et une animation se lance.
}
else m++;
}
flamme_management(game); // le management des flammes se fait toutes les actualisatons du jeu.
game_display(game, screen);
}
}
else if (nb_joueur==2){ // boucle principale d'un jeu <20> 2 joueurs:
while (done==0 && player_get_dead(player1)!=0 && player_get_dead(player2)!=0) {
game_time_update();
if( player_get_lives(player1)!=0 && player_get_lives(player2)!=0){
done = input_update(game,nb_joueur);
bombs_management(game,0);
if (k==BOMBS_SPEED){
k=1;
bombs_management(game,1);
}
else k++;
if (l==MONSTER_SPEED){
l=1;
monsters_management(game, 2);
}
else l++;
}
else{
if (player_get_lives(player1)==0){
if (m==ANIMATION_SPEED){
m=1;
player_die(player1);
}
else m++;
}
if (player_get_lives(player2)==0){
if (n==ANIMATION_SPEED){
n=1;
player_die(player2);
}
else n++;
}
}
flamme_management(game);
game_display(game, screen);
}
}
#ifdef HW_SCREEN_RESIZE
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, 480,480, WINDOW_BPP, 0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(480,480, WINDOW_BPP,SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
#endif //HW_SCREEN_RESIZE
int boucle=0; //cette variable autorise ou non l'affichage de messages comme "vous avez gagn<67>", "game over", ...
if (nb_joueur==1 && player_win(player1)==1){
menu = IMG_Load("sprite/you_win.png");
boucle=1;
}
if (nb_joueur==1 && player_get_dead(player1)==0 && mode!=2){
switch (game_over){
case 3:
menu = IMG_Load("sprite/you_die_3.png");
break;
case 2:
menu = IMG_Load("sprite/you_die_2.png");
break;
case 1:
menu = IMG_Load("sprite/you_die_1.png");
break;
case 0:
menu = IMG_Load("sprite/game_over.png");
break;
}
boucle=1;
}
else if (nb_joueur==2){
if(player_get_dead(player1)==0 && player_get_dead(player2)==0){
menu = IMG_Load("sprite/egalite.png");
boucle=1;}
else if (player_get_dead(player1)==0){
menu = IMG_Load("sprite/p_2_win.png");
boucle=1;
}
else if (player_get_dead(player2)==0){
menu = IMG_Load("sprite/p_1_win.png");
boucle=1;
}
else if(player_win(player1)){
menu = IMG_Load("sprite/p_1_win.png");
boucle=1;
}
else if(player_win(player2)){
menu = IMG_Load("sprite/p_2_win.png");
boucle=1;
}
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(menu, NULL, screen, &positionMenu);
#ifdef HW_SCREEN_RESIZE
SDL_FillRect(hw_screen, NULL, 0x000000);
flip_NNOptimized_AllowOutOfScreen(screen, hw_screen,
HW_SCREEN_WIDTH,
MIN(screen->h*HW_SCREEN_WIDTH/screen->w, HW_SCREEN_HEIGHT));
SDL_Flip(hw_screen);
#else //HW_SCREEN_RESIZE
SDL_Flip(screen);
#endif //HW_SCREEN_RESIZE
bool continu = false;
if (boucle==1){
while (!continu)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continu = 1;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_ESCAPE:
continu = 1;
break;
case SDLK_RETURN:
continu = 1;
break;
case SDLK_KP_ENTER:
continu = 1;
break;
default: break;
}
break;
}
}
}
SDL_FreeSurface(menu);
if (nb_joueur==1 && player_win(player1)==1){
game_free(game);
return niveau; //permet de passer au niveau suivant
}
else if(game_over==0){
game_over=NB_DECES;
game_free(game);
return 0; // s'il y a game over la valeur retourn<72>e est 0 afin de reprendre le jeu depuis le niveau 1
}
else if(done==2){
return -1; // la valeur -1 sert <20> indiquer que l'utilisateur a appuy<75> sur <20>chap ou quitter
// elle informe qu'il faudra rejouer le niveau sans d<>cr<63>menter la variable game_over
// (donc le nombre de d<>c<EFBFBD>s autoris<69>s avant d'avoir un game over)
}
else {
game_free(game);
return niveau-1; // lorsque le joueur meurt, informe qu'il faut
//rejouer le niveau pr<70>c<EFBFBD>dent et d<>cr<63>menter la variable game_over
}
}
//##################################################### Main ####################################################################
//Fonction principale du jeu
int main(int argc, char *argv[]) {
SDL_Surface *menu = NULL;
SDL_Event event;
SDL_Rect positionMenu;
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
error("Can't init SDL: %s\n", SDL_GetError());
}
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
/** Init Video */
#ifdef HW_SCREEN_RESIZE
hw_screen = SDL_SetVideoMode(HW_SCREEN_WIDTH,HW_SCREEN_HEIGHT, WINDOW_BPP, SDL_HWSURFACE|SDL_DOUBLEBUF);
if (hw_screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
}
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, 700,500, WINDOW_BPP, 0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(700,500, WINDOW_BPP,SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
}
#endif //HW_SCREEN_RESIZE
SDL_WM_SetIcon(IMG_Load(IMG_PLAYER_DOWN), NULL);
SDL_WM_SetCaption("[PG110] Projet 2010", NULL);
/** Load audio */
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_MODULE *musique_menu_p;
FMUSIC_MODULE *musique_editeur;
FMUSIC_MODULE *musique_2_p;
FMUSIC_MODULE *musique_1_p;
FMUSIC_MODULE *musique_p_e;
if(!FSOUND_Init(11025, 32, 0)){
printf("Cannot init audio with FSOUND_Init\n");
}
else{
audio_init_ok = true;
musique_menu_p = FMUSIC_LoadSong("audio/mm2titl2.mid");
musique_editeur = FMUSIC_LoadSong("audio/mm2crash.mid");
musique_2_p = FMUSIC_LoadSong("audio/mm2wy1.mid");
musique_1_p = FMUSIC_LoadSong("audio/mix_1_p.mid");
musique_p_e = FMUSIC_LoadSong("audio/mm2flash.mid");
FMUSIC_SetMasterVolume(musique_menu_p, 130);
FMUSIC_SetMasterVolume(musique_p_e, 130);
FMUSIC_SetMasterVolume(musique_editeur, 130);
FMUSIC_SetMasterVolume(musique_2_p, 130);
FMUSIC_SetMasterVolume(musique_1_p, 130);
}
#elif defined(SOUND_SDL_ACTIVATED)
// Setup audio mode
Mix_Music *musique_menu_p = NULL;
Mix_Music *musique_editeur = NULL;
Mix_Music *musique_2_p = NULL;
Mix_Music *musique_1_p = NULL;
Mix_Music *musique_p_e = NULL;
//if(Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 640)==-1){
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)==-1){
printf("Mix_OpenAudio: %s\n", Mix_GetError());
//exit(2);
}
else{
audio_init_ok = true;
musique_menu_p = Mix_LoadMUS("audio/mm2titl2.mid");
musique_editeur = Mix_LoadMUS("audio/mm2crash.mid");
musique_2_p = Mix_LoadMUS("audio/mm2wy1.mid");
musique_1_p = Mix_LoadMUS("audio/mix_1_p.mid");
musique_p_e = Mix_LoadMUS("audio/mm2flash.mid");
}
#endif //SOUND_SDL_ACTIVATED
/** Init vars for graphics and game */
menu = IMG_Load("sprite/menu_1_p.png");
positionMenu.x = 0;
positionMenu.y = 0;
bool done = false; //variable de la boucle principale du bomberman (false: le jeu continu, true: le jeu s'arr<72>te)
int niveau_reussi=0, ancien_niveau=1, choix_entrer_dans_niveau, choix_entrer_niveau_2p=1, choix_niveau=1,
choix_ancien_niveau, choix_option, nb_joueurs, sure, kill_bomb=1;
//niveau_reussi sert pour le mode 1 joueur <20> indiquer <20> quel niveau se trouve le joueur
//ancien_niveau informe <20> quel niveau se trouvait le joueur avant de lancer main_game
//choix_entrer_dans_niveau=0 si le joueur accepte de rentrer dans le niveau indiqu<71>, 1 sinon.
//choix_entrer_niveau_2p permet de choisir lequel des 6 niveaux disponibles en 2 joueurs l'utilisateur choisi.
//choix_niveau sert <20> la m<>me chose que choix_entrer_niveau_2p mais pour l'<27>diteur et le mode "jouer aux jeux <20>dit<69>s"
//idem pour choix_option sauf que c'est pour le mode option (1:jouer son, 2: arr<72>ter son, 3: les bombes s'explosent
//entres elles, 4: les bombes ne s'explosent pas entre elles)
//choix_ancien_niveau sert (en deux joueurs et en mode "jouer aux jeux <20>dit<69>s) <20> replacer automatiquement
//le curseur sur le niveau qui vien d'<27>tre jou<6F>.
//sure=1 si l'utilisateur est s<>r de quitter, 0 sinon.
//kill_bomb=0 si les bombes ne s'explosent pas entre elles, 1 sinon.
//audio sert pour le son (voir version Windows)
int choix_actuel=1; //indique la position du curseur dans le menu principal (1: jeu 1 joueur, 2: jeu 2 joueurs,
//3:editeur, 4:jouer aux niveaux edit<69>s, 5: options, 6: quitter)
int game_over=NB_DECES;
int resize=0; //Si resize=1 on remet l'<27>cran <20> la taille 700*500
int play_music=1;
int audio=1;
while (!done)
{
if (play_music==1){
if(audio_init_ok && audio==1){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_PlaySong(musique_menu_p);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_PlayMusic(musique_menu_p, -1);
#endif //SOUND_SDL_ACTIVATED
}
play_music=0;
}
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
//FMUSIC_StopSong(musique_menu_p);
done = 1;
break;
case SDL_KEYDOWN:
if(event.key.keysym.sym==SDLK_ESCAPE){ // Veut arr<72>ter le jeu
// FMUSIC_StopSong(musique_menu_p);
done = 1;
}
else if(event.key.keysym.sym==SDLK_UP){
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/menu_q.png");
choix_actuel=6;
break;
case 2:
menu = IMG_Load("sprite/menu_1_p.png");
choix_actuel=1;
break;
case 3:
menu = IMG_Load("sprite/menu_2_p.png");
choix_actuel=2;
break;
case 4:
menu = IMG_Load("sprite/menu_e.png");
choix_actuel=3;
break;
case 5:
menu = IMG_Load("sprite/menu_j_e.png");
choix_actuel=4;
break;
case 6:
menu = IMG_Load("sprite/menu_o.png");
choix_actuel=5;
break;
}
}
else if(event.key.keysym.sym==SDLK_DOWN){
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/menu_2_p.png");
choix_actuel=2;
break;
case 2:
menu = IMG_Load("sprite/menu_e.png");
choix_actuel=3;
break;
case 3:
menu = IMG_Load("sprite/menu_j_e.png");
choix_actuel=4;
break;
case 4:
menu = IMG_Load("sprite/menu_o.png");
choix_actuel=5;
break;
case 5:
menu = IMG_Load("sprite/menu_q.png");
choix_actuel=6;
break;
case 6:
menu = IMG_Load("sprite/menu_1_p.png");
choix_actuel=1;
break;
}
}
else if(event.key.keysym.sym==SDLK_RETURN || event.key.keysym.sym==SDLK_KP_ENTER){
switch(choix_actuel){
case 1: //on rentre dans le mode 1 joueur.
if(audio_init_ok && audio==1){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_StopSong(musique_menu_p);
FMUSIC_PlaySong(musique_1_p);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_HaltMusic();
Mix_PlayMusic(musique_1_p, -1);
#endif //SOUND_SDL_ACTIVATED
}
while (niveau_reussi< 10){ //En effet il n'y a que 10 niveaux dans ce jeu
if(game_over<0){
niveau_reussi=0; //apr<70>s game over le joueur repart du niveau 1;
game_over=NB_DECES;
}
choix_entrer_dans_niveau=niveau_1_joueur(screen,niveau_reussi+1);
if (choix_entrer_dans_niveau==0){
ancien_niveau=niveau_reussi;
niveau_reussi=main_game(screen,1,niveau_reussi+1,1, kill_bomb, game_over); // le jeu se lance,
//le r<>sultat retourn<72> est stock<63> dans la variable niveau_reussi.
if(ancien_niveau==niveau_reussi && ancien_niveau!=0){
game_over--; //on regarde si le joueur est mort, dans ce cas on d<>cr<63>mente game_over.
}
else if(niveau_reussi==-1) //si l'utilisateur a fait <20>chap ou quitter durant la partie.
niveau_reussi=ancien_niveau;
if (niveau_reussi>=10){ //si l'utilisateur <20> gagn<67> le mode 1 joueur
choix_entrer_dans_niveau=niveau_1_joueur(screen,0);
niveau_reussi=0;
game_over=NB_DECES;
// FMUSIC_StopSong(musique_1_p);
// play_music=1;
break;
}
}
else{
if(are_you_sure(screen)==1){ //si l'utilisateur veut quitter le mode 1 joueur
play_music=1;
if(audio_init_ok && audio==1){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_StopSong(musique_1_p);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_HaltMusic();
#endif //SOUND_SDL_ACTIVATED
}
break;
}
}
}
resize=1;
break;
case 2:
if(audio_init_ok && audio==1){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_StopSong(musique_menu_p);
FMUSIC_PlaySong(musique_2_p);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_HaltMusic();
Mix_PlayMusic(musique_2_p, -1);
#endif //SOUND_SDL_ACTIVATED
}
sure=2;
choix_entrer_niveau_2p=1;
choix_ancien_niveau=choix_entrer_niveau_2p;
while (sure!=1){
if (choix_entrer_niveau_2p!=0)
choix_ancien_niveau=choix_entrer_niveau_2p;
choix_entrer_niveau_2p=niveau_2_joueur(screen,choix_ancien_niveau);
if(choix_entrer_niveau_2p==0)
sure=are_you_sure(screen);
else
main_game(screen,2,choix_entrer_niveau_2p,1, kill_bomb, game_over);
}
play_music=1;
if(audio_init_ok && audio==1){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_StopSong(musique_2_p);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_HaltMusic();
#endif //SOUND_SDL_ACTIVATED
}
resize=1;
break;
case 3:
if(audio_init_ok && audio==1){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_StopSong(musique_menu_p);
FMUSIC_PlaySong(musique_editeur);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_HaltMusic();
Mix_PlayMusic(musique_editeur, -1);
#endif //SOUND_SDL_ACTIVATED
}
sure=2;
choix_niveau=1;
choix_ancien_niveau=choix_niveau;
while (sure!=1){
if (choix_niveau!=0)
choix_ancien_niveau=choix_niveau;
choix_niveau=niveau_2_joueur(screen,choix_ancien_niveau);
if(choix_niveau==0)
sure=are_you_sure(screen);
else if (niveau_1_joueur(screen,-1)==0)
editeur(screen,choix_niveau);
}
play_music=1;
if(audio_init_ok && audio==1){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_StopSong(musique_editeur);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_HaltMusic();
#endif //SOUND_SDL_ACTIVATED
}
resize=1;
break;
case 4:
if(audio_init_ok && audio==1){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_StopSong(musique_menu_p);
FMUSIC_PlaySong(musique_p_e);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_HaltMusic();
Mix_PlayMusic(musique_p_e, -1);
#endif //SOUND_SDL_ACTIVATED
}
sure=2;
choix_niveau=1;
choix_ancien_niveau=choix_niveau;
while (sure!=1){
if (choix_niveau!=0){
choix_ancien_niveau=choix_niveau;
}
choix_niveau=niveau_2_joueur(screen,choix_ancien_niveau);
if(choix_niveau==0){
sure=are_you_sure(screen);
}
else{
nb_joueurs=choix_nb_joueurs(screen);
if(nb_joueurs!=0 ){
main_game(screen,nb_joueurs,choix_niveau,2, kill_bomb, game_over);
}
}
}
play_music=1;
if(audio_init_ok && audio==1){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_StopSong(musique_p_e);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_HaltMusic();
#endif //SOUND_SDL_ACTIVATED
}
resize=1;
break;
case 5:
choix_option=options(screen);
switch(choix_option){
case 0:
break;
case 1:
if(audio_init_ok){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_PlaySong(musique_menu_p);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_PlayMusic(musique_menu_p, -1);
#endif //SOUND_SDL_ACTIVATED
}
audio=1;
break;
case 2:
if(audio_init_ok){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_StopSong(musique_menu_p);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_HaltMusic();
#endif //SOUND_SDL_ACTIVATED
}
audio=0;
break;
case 3:
kill_bomb=1;
break;
case 4:
kill_bomb=0;
break;
default: break;
}
resize=1;
break;
case 6:
if(are_you_sure(screen)==1){
if(audio_init_ok && audio==1)
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_StopSong(musique_menu_p);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_HaltMusic();
#endif //SOUND_FMOD_ACTIVATED
done=1;
resize=1;
break;
}
else
resize=1;
break;
default: break;
}
break;
}
default: break;
}
if (resize==1){
#ifdef HW_SCREEN_RESIZE
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, 700,500, WINDOW_BPP, 0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(700,500, WINDOW_BPP,SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
#endif //HW_SCREEN_RESIZE
resize=0;
}
// Effacement de l'<27>cran
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(menu, NULL, screen, &positionMenu);
#ifdef HW_SCREEN_RESIZE
SDL_FillRect(hw_screen, NULL, 0x000000);
flip_NNOptimized_AllowOutOfScreen(screen, hw_screen,
HW_SCREEN_WIDTH,
MIN(screen->h*HW_SCREEN_WIDTH/screen->w, HW_SCREEN_HEIGHT));
SDL_Flip(hw_screen);
#else //HW_SCREEN_RESIZE
SDL_Flip(screen);
#endif //HW_SCREEN_RESIZE
}
if(audio_init_ok){
#ifdef SOUND_FMOD_ACTIVATED
FMUSIC_FreeSong(musique_menu_p);
FMUSIC_FreeSong(musique_2_p);
FMUSIC_FreeSong(musique_1_p);
FMUSIC_FreeSong(musique_p_e);
FMUSIC_FreeSong(musique_editeur);
#elif defined(SOUND_SDL_ACTIVATED)
Mix_FreeMusic(musique_menu_p);
Mix_FreeMusic(musique_2_p);
Mix_FreeMusic(musique_1_p);
Mix_FreeMusic(musique_p_e);
Mix_FreeMusic(musique_editeur);
Mix_CloseAudio();
#endif //SOUND_SDL_ACTIVATED
}
SDL_FreeSurface(menu);
SDL_Quit();
return EXIT_SUCCESS;
}

597
src/map.c Normal file
View File

@@ -0,0 +1,597 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <SDL.h>
#include "SDL_image.h"
#include <time.h>
#include "../include/constant.h"
#include "../include/map.h"
#include "../include/misc.h"
#include "../include/game_time.h"
#include "../include/game.h"
struct t_map {
int width;
int height;
t_cell_type *grid;
SDL_Surface *img_wall;
SDL_Surface *img_box;
SDL_Surface *img_goal;
SDL_Surface *img_bonus_bomb_range_inc;
SDL_Surface *img_bonus_bomb_range_dec;
SDL_Surface *img_bonus_bomb_nb_inc;
SDL_Surface *img_bonus_bomb_nb_dec;
SDL_Surface *img_bonus_life_inc;
SDL_Surface *img_menu_lives_0;
SDL_Surface *img_menu_lives_1;
SDL_Surface *img_menu_lives_2;
SDL_Surface *img_menu_lives_3;
SDL_Surface *img_menu_lives_4;
SDL_Surface *img_menu_bombs_0;
SDL_Surface *img_menu_bombs_1;
SDL_Surface *img_menu_bombs_2;
SDL_Surface *img_menu_bombs_3;
SDL_Surface *img_menu_bombs_4;
SDL_Surface *img_menu_bombs_5;
SDL_Surface *img_menu_range_0;
SDL_Surface *img_menu_range_1;
SDL_Surface *img_menu_range_2;
SDL_Surface *img_menu_range_3;
SDL_Surface *img_menu_range_4;
SDL_Surface *img_menu_range_5;
SDL_Surface *img_menu_empty;
SDL_Surface *img_menu_player_1;
SDL_Surface *img_menu_player_2;
int nb_monsters;
};
#define CELL(i,j) (i + map->width * j)
t_map map_new(int width, int height) {
assert(width > 0 && height > 0);
t_map map = malloc(sizeof(struct t_map));
if (map == NULL)
exit(EXIT_FAILURE);
map->width = width;
map->height = height;
map->nb_monsters = 0;
map->grid = (t_cell_type *) malloc(height * width * sizeof(t_cell_type));
if (map->grid == NULL)
exit(EXIT_FAILURE);
// Grid cleaning
int i, j;
for (i = 0; i < width; i++) {
for (j = 0; j < height; j++) {
map->grid[CELL(i,j)] = CELL_EMPTY;
}
}
// Sprite loading
map->img_wall = load_image(IMG_MAP_WALL);
map->img_box = load_image(IMG_MAP_BOX);
map->img_goal = load_image(IMG_MAP_GOAL);
map->img_bonus_bomb_range_inc = load_image(IMG_BONUS_BOMB_RANGE_INC);
map->img_bonus_bomb_range_dec = load_image(IMG_BONUS_BOMB_RANGE_DEC);
map->img_bonus_bomb_nb_inc = load_image(IMG_BONUS_BOMB_NB_INC);
map->img_bonus_bomb_nb_dec = load_image(IMG_BONUS_BOMB_NB_DEC);
map->img_bonus_life_inc = load_image(IMG_BONUS_LIFE_INC);
map->img_menu_empty = load_image(IMG_MENU_EMPTY);
map->img_menu_lives_0 = load_image(IMG_MENU_LIVES_0);
map->img_menu_lives_1 = load_image(IMG_MENU_LIVES_1);
map->img_menu_lives_2 = load_image(IMG_MENU_LIVES_2);
map->img_menu_lives_3 = load_image(IMG_MENU_LIVES_3);
map->img_menu_lives_4 = load_image(IMG_MENU_LIVES_4);
map->img_menu_bombs_0 = load_image(IMG_MENU_BOMBS_0);
map->img_menu_bombs_1 = load_image(IMG_MENU_BOMBS_1);
map->img_menu_bombs_2 = load_image(IMG_MENU_BOMBS_2);
map->img_menu_bombs_3 = load_image(IMG_MENU_BOMBS_3);
map->img_menu_bombs_4 = load_image(IMG_MENU_BOMBS_4);
map->img_menu_bombs_5 = load_image(IMG_MENU_BOMBS_5);
map->img_menu_range_0 = load_image(IMG_MENU_RANGE_0);
map->img_menu_range_1 = load_image(IMG_MENU_RANGE_1);
map->img_menu_range_2 = load_image(IMG_MENU_RANGE_2);
map->img_menu_range_3 = load_image(IMG_MENU_RANGE_3);
map->img_menu_range_4 = load_image(IMG_MENU_RANGE_4);
map->img_menu_range_5 = load_image(IMG_MENU_RANGE_5);
map->img_menu_player_1 = load_image(IMG_MENU_PLAYER_1);
map->img_menu_player_2 = load_image(IMG_MENU_PLAYER_2);
return map;
}
int map_is_inside(t_map map, int x, int y) {
assert(map);
int width=map_get_width(map);
int height=map_get_height(map);
if (x<0 || x>=width)
return 0;
if (y<0 || y>=height)
return 0;
return 1;
}
void map_free(t_map map) {
if (map == NULL)
return;
free(map->grid);
SDL_FreeSurface(map->img_wall);
SDL_FreeSurface(map->img_box);
SDL_FreeSurface(map->img_goal);
SDL_FreeSurface(map->img_bonus_bomb_range_inc);
SDL_FreeSurface(map->img_bonus_bomb_range_dec);
SDL_FreeSurface(map->img_bonus_bomb_nb_inc);
SDL_FreeSurface(map->img_bonus_bomb_nb_dec);
SDL_FreeSurface(map->img_bonus_life_inc);
SDL_FreeSurface(map->img_menu_empty);
SDL_FreeSurface(map->img_menu_lives_0);
SDL_FreeSurface(map->img_menu_lives_1);
SDL_FreeSurface(map->img_menu_lives_2);
SDL_FreeSurface(map->img_menu_lives_3);
SDL_FreeSurface(map->img_menu_lives_4);
SDL_FreeSurface(map->img_menu_bombs_0);
SDL_FreeSurface(map->img_menu_bombs_1);
SDL_FreeSurface(map->img_menu_bombs_2);
SDL_FreeSurface(map->img_menu_bombs_3);
SDL_FreeSurface(map->img_menu_bombs_4);
SDL_FreeSurface(map->img_menu_bombs_5);
SDL_FreeSurface(map->img_menu_range_0);
SDL_FreeSurface(map->img_menu_range_1);
SDL_FreeSurface(map->img_menu_range_2);
SDL_FreeSurface(map->img_menu_range_3);
SDL_FreeSurface(map->img_menu_range_4);
SDL_FreeSurface(map->img_menu_range_5);
SDL_FreeSurface(map->img_menu_player_1);
SDL_FreeSurface(map->img_menu_player_2);
free(map);
}
int map_get_width(t_map map) {
assert(map != NULL);
return map->width;
}
int map_get_height(t_map map) {
assert(map != NULL);
return map->height;
}
t_cell_type map_get_cell_type(t_map map, int x, int y) {
assert(map && map_is_inside(map, x, y));
return map->grid[CELL(x,y)];
}
void map_set_cell_type(t_map map, int x, int y, t_cell_type type) {
assert(map && map_is_inside(map, x, y));
map->grid[CELL(x,y)] = type;
}
t_map map_load_dynamic(FILE *fd, int niveau,int nb_joueur) {
int width, height;
char a;int k=1, i=0, continuer=1;
int taille_tableau=0;
t_map map =NULL;
if (fd) {
while (continuer==1){
if (k==niveau){
fscanf (fd, "%d:%d", &height, &width);
taille_tableau= width*height;
map = map_new(width, height);
while (i<taille_tableau)
{
a=fgetc(fd);
if (a != '\n' && a != ' '&& a != '\r')
{
switch (a){
case '0':
map->grid[i] = CELL_EMPTY;
break;
case '1':
map->grid[i] = CELL_WALL;
break;
case '2':
map->grid[i] = CELL_BOX | (return_bonus() << 4);
break;
case '3':
map->grid[i] = CELL_GOAL;
break;
case '4':
map->grid[i] = CELL_PLAYER;
break;
case 'j':
if (nb_joueur==2)
map->grid[i] = CELL_PLAYER_2;
else
map->grid[i] = CELL_EMPTY;
break;
case '5':
map->grid[i] = (CELL_BONUS | (BONUS_BOMB_RANGE_INC << 4));
break;
case 'k':
map->grid[i] = (CELL_MENU_PLAYER_1);
break;
case 'l':
map->grid[i] = (CELL_MENU_PLAYER_2);
break;
case '6':
map->grid[i] = (CELL_MENU_EMPTY);
break;
case '7':
map->grid[i] = (CELL_MENU_LIVES);
break;
case '8':
map->grid[i] = (CELL_MENU_BOMBS);
break;
case '9':
map->grid[i] = (CELL_MENU_RANGE);
break;
default: exit(EXIT_FAILURE);
}
i++;
}
}
continuer=0;
}
a=fgetc(fd);
if (a=='-') k++;
}
fclose(fd);
return map;
}
exit(EXIT_FAILURE);
}
void sauvegarderNiveau(t_map map, int niveau){
FILE* fichier = NULL;
int i = 0, j=0, continuer=1,k=1;
char a;
fichier = fopen("data/niveaux.lvl", "r+");
while (continuer==1){
if (k==niveau) continuer=0;
a=fgetc(fichier);
if (a=='-') k++;
}
if (niveau==1) fseek(fichier, -1, SEEK_CUR);
else fseek(fichier, 0, SEEK_CUR);
fprintf(fichier, "%d:%d", map->height,map->width);
for (j = 0; j < map->height; j++) {
fprintf(fichier, "%s", "\n");
for (i = 0; i < map->width; i++) {
t_cell_type type = map->grid[CELL(i,j)];
switch (type & 0x0f) {
case CELL_EMPTY:
fprintf(fichier, "%d", 0);
break;
case CELL_WALL:
fprintf(fichier, "%d", 1);
break;
case CELL_BOX:
fprintf(fichier, "%d", 2);
break;
case CELL_GOAL:
fprintf(fichier, "%d", 3);
break;
case CELL_PLAYER:
fprintf(fichier, "%d", 4);
break;
case CELL_PLAYER_2:
fputc('j', fichier);
break;
case CELL_BONUS:
switch ((type & 0xf0) >> 4) {
case BONUS_BOMB_RANGE_INC:
fprintf(fichier, "%d", 5);
break;
default: fprintf(fichier, "%d", 0);
}
break;
case CELL_MENU_EMPTY:
fprintf(fichier, "%d", 6);
break;
case CELL_MENU_PLAYER_1:
fputc('k', fichier);
break;
case CELL_MENU_PLAYER_2:
fputc('l', fichier);
break;
case CELL_MENU_LIVES:
fprintf(fichier, "%d", 7);
break;
case CELL_MENU_BOMBS:
fprintf(fichier, "%d", 8);
break;
case CELL_MENU_RANGE:
fprintf(fichier, "%d", 9);
break;
default: fprintf(fichier, "%d", 0);
}
}
}
fprintf(fichier, "\n-\n");
fclose(fichier);
}
// t_map map_load_static(void) {
// t_map map = map_new(12, 12);
// t_cell_type
// themap[144] = { CELL_PLAYER, CELL_BOX, CELL_EMPTY, CELL_EMPTY,
// CELL_EMPTY, CELL_WALL, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY,
// CELL_BOX, CELL_BOX, CELL_EMPTY, CELL_EMPTY, CELL_WALL,
// CELL_WALL, CELL_EMPTY, CELL_EMPTY, CELL_BOX, CELL_EMPTY,
// CELL_WALL, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY,
// CELL_EMPTY, CELL_BOX, CELL_EMPTY, CELL_EMPTY, CELL_WALL,
// CELL_WALL, CELL_WALL, CELL_WALL, CELL_EMPTY, CELL_BOX,
// CELL_BOX, CELL_EMPTY, CELL_BOX, CELL_BOX, CELL_WALL,
// CELL_WALL, CELL_WALL, CELL_EMPTY, CELL_BOX, CELL_EMPTY,
// CELL_EMPTY, CELL_BOX, CELL_EMPTY, CELL_EMPTY, CELL_BOX,
// CELL_WALL, CELL_BOX, CELL_EMPTY, CELL_EMPTY, CELL_BOX,
// CELL_BOX, CELL_BOX, CELL_WALL, CELL_WALL, CELL_EMPTY,
// CELL_EMPTY, CELL_WALL, CELL_BOX, CELL_BOX, CELL_EMPTY,
// CELL_BOX, CELL_BOX, CELL_BOX, CELL_BOX, CELL_EMPTY,
// CELL_EMPTY, CELL_WALL, CELL_WALL, CELL_EMPTY, CELL_EMPTY,
// CELL_EMPTY, CELL_WALL, CELL_BOX, CELL_BOX, CELL_BOX,
// CELL_WALL, CELL_WALL, CELL_BOX, CELL_EMPTY, CELL_EMPTY,
// CELL_EMPTY, CELL_BOX, CELL_BOX, CELL_WALL, CELL_BOX,
// CELL_BOX, CELL_WALL, CELL_WALL, CELL_EMPTY, CELL_BOX,
// CELL_BOX, CELL_EMPTY, CELL_BOX, CELL_EMPTY, CELL_EMPTY,
// CELL_WALL, CELL_WALL, CELL_BOX, CELL_EMPTY, CELL_EMPTY,
// CELL_BOX, CELL_BOX, CELL_BOX, CELL_WALL, CELL_EMPTY,
// CELL_WALL, CELL_WALL, CELL_WALL, CELL_BOX, CELL_EMPTY,
// CELL_WALL, CELL_WALL, CELL_EMPTY, CELL_EMPTY, CELL_BOX,
// CELL_BOX, CELL_EMPTY, CELL_WALL, CELL_EMPTY, CELL_BOX,
// CELL_EMPTY, CELL_EMPTY, CELL_WALL, CELL_EMPTY, CELL_BOX,
// CELL_BOX, CELL_WALL, CELL_BOX, CELL_EMPTY, CELL_EMPTY,
// CELL_EMPTY, CELL_WALL, CELL_EMPTY, CELL_EMPTY, CELL_WALL,
// CELL_WALL, CELL_EMPTY, CELL_EMPTY, CELL_WALL, CELL_GOAL };
// for (int i = 0; i < 144; i++)
// map->grid[i] = themap[i];
// return map;
// }
void map_display(t_map map, int lives_player, int bombs_player, int range_player, int lives_player2, int bombs_player2, int range_player2, SDL_Surface *screen) {
int i, j, k=0, l=0, m=0;
assert(map != NULL);
assert(screen != NULL);
assert(map->height > 0 && map->width > 0);
assert(map->img_wall != NULL);
assert(map->img_box != NULL);
assert(map->img_goal != NULL);
for (j = 0; j < map->height; j++) {
for (i = 0; i < map->width; i++) {
SDL_Rect place;
place.x = i * SIZE_BLOC;
place.y = j * SIZE_BLOC;
t_cell_type type = map->grid[CELL(i,j)];
switch (type & 0x0f) {
case CELL_WALL:
SDL_BlitSurface(map->img_wall, NULL, screen, &place);
break;
case CELL_BOX:
SDL_BlitSurface(map->img_box, NULL, screen, &place);
break;
case CELL_GOAL:
SDL_BlitSurface(map->img_goal, NULL, screen, &place);
break;
case CELL_MENU_EMPTY:
SDL_BlitSurface(map->img_menu_empty, NULL, screen, &place);
break;
case CELL_MENU_PLAYER_1:
SDL_BlitSurface(map->img_menu_player_1, NULL, screen, &place);
break;
case CELL_MENU_PLAYER_2:
SDL_BlitSurface(map->img_menu_player_2, NULL, screen, &place);
break;
case CELL_MENU_LIVES:
if (k==0){
switch (lives_player){
case 0:
SDL_BlitSurface(map->img_menu_lives_0, NULL, screen, &place);
break;
case 1:
SDL_BlitSurface(map->img_menu_lives_1, NULL, screen, &place);
break;
case 2:
SDL_BlitSurface(map->img_menu_lives_2, NULL, screen, &place);
break;
case 3:
SDL_BlitSurface(map->img_menu_lives_3, NULL, screen, &place);
break;
case 4:
SDL_BlitSurface(map->img_menu_lives_4, NULL, screen, &place);
break;
}
k++;
}
else {
switch (lives_player2){
case 0:
SDL_BlitSurface(map->img_menu_lives_0, NULL, screen, &place);
break;
case 1:
SDL_BlitSurface(map->img_menu_lives_1, NULL, screen, &place);
break;
case 2:
SDL_BlitSurface(map->img_menu_lives_2, NULL, screen, &place);
break;
case 3:
SDL_BlitSurface(map->img_menu_lives_3, NULL, screen, &place);
break;
case 4:
SDL_BlitSurface(map->img_menu_lives_4, NULL, screen, &place);
break;
}
}
break;
case CELL_MENU_BOMBS:
if (l==0){
switch (bombs_player){
case 0:
SDL_BlitSurface(map->img_menu_bombs_0, NULL, screen, &place);
break;
case 1:
SDL_BlitSurface(map->img_menu_bombs_1, NULL, screen, &place);
break;
case 2:
SDL_BlitSurface(map->img_menu_bombs_2, NULL, screen, &place);
break;
case 3:
SDL_BlitSurface(map->img_menu_bombs_3, NULL, screen, &place);
break;
case 4:
SDL_BlitSurface(map->img_menu_bombs_4, NULL, screen, &place);
break;
case 5:
SDL_BlitSurface(map->img_menu_bombs_5, NULL, screen, &place);
break;
}
l++;
}
else {
switch (bombs_player2){
case 0:
SDL_BlitSurface(map->img_menu_bombs_0, NULL, screen, &place);
break;
case 1:
SDL_BlitSurface(map->img_menu_bombs_1, NULL, screen, &place);
break;
case 2:
SDL_BlitSurface(map->img_menu_bombs_2, NULL, screen, &place);
break;
case 3:
SDL_BlitSurface(map->img_menu_bombs_3, NULL, screen, &place);
break;
case 4:
SDL_BlitSurface(map->img_menu_bombs_4, NULL, screen, &place);
break;
case 5:
SDL_BlitSurface(map->img_menu_bombs_5, NULL, screen, &place);
break;
}
}
break;
case CELL_MENU_RANGE:
if (m==0){
switch (range_player){
case 0:
SDL_BlitSurface(map->img_menu_range_0, NULL, screen, &place);
break;
case 1:
SDL_BlitSurface(map->img_menu_range_1, NULL, screen, &place);
break;
case 2:
SDL_BlitSurface(map->img_menu_range_2, NULL, screen, &place);
break;
case 3:
SDL_BlitSurface(map->img_menu_range_3, NULL, screen, &place);
break;
case 4:
SDL_BlitSurface(map->img_menu_range_4, NULL, screen, &place);
break;
case 5:
SDL_BlitSurface(map->img_menu_range_5, NULL, screen, &place);
break;
}
m++;
}
else {
switch (range_player2){
case 0:
SDL_BlitSurface(map->img_menu_range_0, NULL, screen, &place);
break;
case 1:
SDL_BlitSurface(map->img_menu_range_1, NULL, screen, &place);
break;
case 2:
SDL_BlitSurface(map->img_menu_range_2, NULL, screen, &place);
break;
case 3:
SDL_BlitSurface(map->img_menu_range_3, NULL, screen, &place);
break;
case 4:
SDL_BlitSurface(map->img_menu_range_4, NULL, screen, &place);
break;
case 5:
SDL_BlitSurface(map->img_menu_range_5, NULL, screen, &place);
break;
}
}
break;
case CELL_BONUS:
switch ((type & 0xf0) >> 4) {
case BONUS_BOMB_RANGE_INC:
SDL_BlitSurface(map->img_bonus_bomb_range_inc, NULL,
screen, &place);
break;
case BONUS_BOMB_RANGE_DEC:
SDL_BlitSurface(map->img_bonus_bomb_range_dec, NULL,
screen, &place);
break;
case BONUS_BOMB_NB_INC:
SDL_BlitSurface(map->img_bonus_bomb_nb_inc, NULL, screen,
&place);
break;
case BONUS_BOMB_NB_DEC:
SDL_BlitSurface(map->img_bonus_bomb_nb_dec, NULL, screen,
&place);
break;
case BONUS_LIFE_INC:
SDL_BlitSurface(map->img_bonus_life_inc, NULL,
screen, &place);
break;
}
}
}
}
}

70
src/misc.c Normal file
View File

@@ -0,0 +1,70 @@
/*
* misc.c
*
* Created on: 15 mars 2010
* Author: reveille
*/
#include "../include/misc.h"
#include "../include/constant.h"
void error(const char *s, ...) {
va_list ap;
va_start(ap, s);
vfprintf(stderr, s, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
SDL_Surface *load_image(const char *filename) {
SDL_Surface *img = IMG_Load(filename);
if (!img) {
error("IMG_Load: %s\n", IMG_GetError());
}
return img;
}
/// Nearest neighboor optimized with possible out of screen coordinates (for cropping)
void flip_NNOptimized_AllowOutOfScreen(SDL_Surface *virtual_screen, SDL_Surface *hardware_screen, int new_w, int new_h){
int w1=virtual_screen->w;
//int h1=virtual_screen->h;
int w2=new_w;
int h2=new_h;
int x_ratio = (int)((virtual_screen->w<<16)/w2);
int y_ratio = (int)((virtual_screen->h<<16)/h2);
int x2, y2 ;
/// --- Compute padding for centering when out of bounds ---
int y_padding = (HW_SCREEN_HEIGHT-new_h)/2;
int x_padding = 0;
if(w2>HW_SCREEN_WIDTH){
x_padding = (w2-HW_SCREEN_WIDTH)/2 + 1;
}
int x_padding_ratio = x_padding*w1/w2;
//printf("virtual_screen->h=%d, h2=%d\n", virtual_screen->h, h2);
for (int i=0;i<h2;i++)
{
if(i>=HW_SCREEN_HEIGHT){
continue;
}
uint16_t* t = (uint16_t*)(hardware_screen->pixels+((i+y_padding)* ((w2>HW_SCREEN_WIDTH)?HW_SCREEN_WIDTH:w2) )*sizeof(uint16_t));
y2 = ((i*y_ratio)>>16);
uint16_t* p = (uint16_t*)(virtual_screen->pixels + (y2*w1 + x_padding_ratio) *sizeof(uint16_t));
int rat = 0;
for (int j=0;j<w2;j++)
{
if(j>=HW_SCREEN_WIDTH){
continue;
}
x2 = (rat>>16);
*t++ = p[x2];
rat += x_ratio;
//printf("y=%d, x=%d, y2=%d, x2=%d, (y2*virtual_screen->w)+x2=%d\n", i, j, y2, x2, (y2*virtual_screen->w)+x2);
}
}
}

352
src/monsters.c Normal file
View File

@@ -0,0 +1,352 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "SDL.h"
#include "SDL_image.h"
#include "../include/game.h"
struct s_monster {
int x,y,portee_monstre;
SDL_Surface * directed_img[4];
enum e_way current_way;
t_monster nxt;
};
static void monster_load_img(t_monster monster, enum e_way way,
const char *filename) {
monster->directed_img[way] = IMG_Load(filename);
if (!monster->directed_img[way])
error("IMG_Load: %s\n", IMG_GetError());
}
t_monster monster_init(t_monster list_monsters, int x, int y, int portee_monstre) {
t_monster monster = malloc(sizeof(*monster));
if (!monster)
error("Memory error");
monster_load_img(monster, LEFT, IMG_MONSTER_LEFT);
monster_load_img(monster, RIGHT, IMG_MONSTER_RIGHT);
monster_load_img(monster, UP, IMG_MONSTER_UP);
monster_load_img(monster, DOWN, IMG_MONSTER_DOWN);
monster->current_way = DOWN;
monster->x=x;
monster->y=y;
monster->portee_monstre=portee_monstre;
monster->nxt=NULL;
if( list_monsters== NULL)
{
return monster;
}
else
{
t_monster temp=list_monsters;
while(temp->nxt != NULL)
{
temp = temp->nxt;
}
temp->nxt = monster;
return list_monsters;
}
}
void monster_free(t_monster monster) {
if (monster != NULL){
assert(monster);
int i;
for (i = 0; i < 4; i++)
SDL_FreeSurface(monster->directed_img[i]);
free(monster);
}
}
t_monster get_next_monster(t_monster monster){
return monster->nxt;
}
void set_next_monster(t_monster monster, t_monster nxt_monster){
monster->nxt=nxt_monster;
}
int monster_get_x(t_monster monster) {
assert(monster != NULL);
return monster->x;
}
int monster_get_y(t_monster monster) {
assert(monster != NULL);
return monster->y;
}
int monster_portee(t_monster monster) {
assert(monster != NULL);
return monster->portee_monstre;
}
enum e_way monster_get_current_way(t_monster monster) {
assert(monster);
return(monster->current_way);
}
void monster_set_current_way(t_monster monster, enum e_way way) {
assert(monster);
monster->current_way = way;
}
void monster_increase_portee(t_monster monster) {
assert(monster);
if (monster->portee_monstre <BOMB_RANGE_MAX)
monster->portee_monstre++;
}
void monster_decrease_portee(t_monster monster) {
assert(monster);
if (monster->portee_monstre >1)
monster->portee_monstre--;
}
static int monster_move_aux(t_monster monster, t_player player, t_player player2, int nb_joueur, t_map map, int x1, int y1, int x2,
int y2) {
t_cell_type type;
if (!map_is_inside(map, x1, y1))
return 0;
type=map_get_cell_type(map, x1, y1);
switch (type & 0x0f) {
case CELL_WALL:
return 0;
break;
case CELL_BOMB:
return 0;
break;
case CELL_PLAYER:
if ( nb_joueur==1){
player_decrease_lives(player);
}
else{
if (player_get_x(player)==x1 && player_get_y(player)==y1) {
player_decrease_lives(player);
}
if (player_get_x(player2)==x1 && player_get_y(player2)==y1) {
player_decrease_lives(player2);
}
}
break;
case CELL_FLAMME:
return 0;;
break;
case CELL_MENU_EMPTY:
return 0;
break;
case CELL_MENU_LIVES:
return 0;
break;
case CELL_MENU_BOMBS:
return 0;
break;
case CELL_MENU_RANGE:
return 0;
break;
case CELL_BONUS:
switch ((type & 0xf0) >> 4) {
case BONUS_BOMB_RANGE_INC:
monster_increase_portee(monster);
break;
case BONUS_BOMB_RANGE_DEC:
monster_decrease_portee(monster);
break;
default:
break;
}
break;
case CELL_BOX:
if (!map_is_inside(map, x2, y2))
return 0;
switch (map_get_cell_type(map,x2,y2)& 0x0f){
case CELL_WALL:
return 0;
break;
case CELL_BOX:
return 0;
break;
case CELL_GOAL:
return 0;
break;
case CELL_MONSTER:
return 0;
break;
case CELL_PLAYER:
return 0;
break;
case CELL_PLAYER_2:
return 0;
break;
case CELL_BOMB:
return 0;
break;
case CELL_MENU_EMPTY:
return 0;
break;
case CELL_MENU_LIVES:
return 0;
break;
case CELL_MENU_BOMBS:
return 0;
break;
case CELL_MENU_RANGE:
return 0;
break;
case CELL_MENU_PLAYER_1:
return 0;
break;
case CELL_MENU_PLAYER_2:
return 0;
break;
default:
break;
}
map_set_cell_type(map, x1, y1, CELL_EMPTY);
switch ((type & 0xf0) >> 4) {
case BONUS_BOMB_RANGE_INC:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_BOMB_RANGE_INC << 4));
break;
case BONUS_BOMB_RANGE_DEC:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_BOMB_RANGE_DEC << 4));
break;
case BONUS_BOMB_NB_INC:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_BOMB_NB_INC << 4));
break;
case BONUS_BOMB_NB_DEC:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_BOMB_NB_DEC << 4));
break;
case BONUS_MONSTER:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_MONSTER << 4));
break;
case BONUS_LIFE_INC:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_LIFE_INC << 4));
break;
case NO_BONUS:
map_set_cell_type(map, x2, y2, CELL_BOX | (NO_BONUS << 4));
break;
default:
break;
}
return 1;
break;
case CELL_GOAL:
return 0;
break;
case CELL_MONSTER:
return 0;
break;
case CELL_MENU_PLAYER_1:
return 0;
break;
case CELL_MENU_PLAYER_2:
return 0;
break;
default:
break;
}
// monster has moved
return 1;
}
int monster_move(t_monster monster, t_map map, t_player player, t_player player2, int nb_joueur) {
int x = monster->x;
int y = monster->y;
int move = 0;
switch (monster->current_way) {
case UP:
if (monster_move_aux(monster, player, player2,nb_joueur, map, x, y - 1, x, y - 2)) {
monster->y--;
move = 1;
}
break;
case DOWN:
if (monster_move_aux(monster, player, player2, nb_joueur, map, x, y + 1, x, y + 2)) {
monster->y++;
move = 1;
}
break;
case LEFT:
if (monster_move_aux(monster, player, player2, nb_joueur, map, x - 1, y, x - 2, y)) {
monster->x--;
move = 1;
}
break;
case RIGHT:
if (monster_move_aux(monster, player, player2, nb_joueur, map, x + 1, y, x + 2, y)) {
monster->x++;
move = 1;
}
break;
}
if (move) {
map_set_cell_type(map, x, y, CELL_EMPTY);
map_set_cell_type(map, monster->x, monster->y, CELL_MONSTER);
}
return move;
}
void monster_display(t_monster monster, SDL_Surface *screen) {
assert(monster);
assert(monster->directed_img[monster->current_way]);
assert(screen);
SDL_Rect place;
place.x = monster->x * SIZE_BLOC;
place.y = monster->y * SIZE_BLOC;
SDL_BlitSurface(monster->directed_img[monster->current_way], NULL, screen,
&place);
}

687
src/niveau.c Normal file
View File

@@ -0,0 +1,687 @@
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include "SDL_image.h"
#include "../include/editeur.h"
#include "../include/misc.h"
#include "../include/constant.h"
#include "../include/niveau.h"
#ifdef HW_SCREEN_RESIZE
extern SDL_Surface *hw_screen;
#endif //HW_SCREEN_RESIZE
int niveau_1_joueur(SDL_Surface *screen, int niveau){
int continuer = 1;
SDL_Surface *menu = NULL;
SDL_Event event;
SDL_Rect positionMenu;
#ifdef HW_SCREEN_RESIZE
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, 480,480, WINDOW_BPP, 0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(480,480, WINDOW_BPP, SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
#endif //HW_SCREEN_RESIZE
switch (niveau){
case -1:
menu = IMG_Load("sprite/legende_editeur.png");
break;
case 0:
menu = IMG_Load("sprite/win_the_game.png");
break;
case 1:
menu = IMG_Load("sprite/niveau_1.png");
break;
case 2:
menu = IMG_Load("sprite/niveau_2.png");
break;
case 3:
menu = IMG_Load("sprite/niveau_3.png");
break;
case 4:
menu = IMG_Load("sprite/niveau_4.png");
break;
case 5:
menu = IMG_Load("sprite/niveau_5.png");
break;
case 6:
menu = IMG_Load("sprite/niveau_6.png");
break;
case 7:
menu = IMG_Load("sprite/niveau_7.png");
break;
case 8:
menu = IMG_Load("sprite/niveau_8.png");
break;
case 9:
menu = IMG_Load("sprite/niveau_9.png");
break;
case 10:
menu = IMG_Load("sprite/niveau_10.png");
break;
}
positionMenu.x = 0;
positionMenu.y = 0;
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
SDL_FreeSurface(menu);
return 1;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
SDL_FreeSurface(menu);
return 1;
break;
case SDLK_RETURN:
continuer = 0;
break;
case SDLK_KP_ENTER:
continuer = 0;
break;
default: break;
}
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(menu, NULL, screen, &positionMenu);
#ifdef HW_SCREEN_RESIZE
SDL_FillRect(hw_screen, NULL, 0x000000);
flip_NNOptimized_AllowOutOfScreen(screen, hw_screen,
HW_SCREEN_WIDTH,
MIN(screen->h*HW_SCREEN_WIDTH/screen->w, HW_SCREEN_HEIGHT));
SDL_Flip(hw_screen);
#else //HW_SCREEN_RESIZE
SDL_Flip(screen);
#endif //HW_SCREEN_RESIZE
}
SDL_FreeSurface(menu);
return 0;
}
int niveau_2_joueur(SDL_Surface *screen, int choix_niveau){
#ifdef HW_SCREEN_RESIZE
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, 480,480, WINDOW_BPP, 0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(480,480, WINDOW_BPP, SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
#endif //HW_SCREEN_RESIZE
int continuer = 1, choix_actuel=choix_niveau;
SDL_Surface *menu = NULL;
SDL_Event event;
SDL_Rect positionMenu;
positionMenu.x = 0;
positionMenu.y = 0;
switch (choix_actuel){
case 1:
menu = IMG_Load("sprite/niveau_2p_1.png");
break;
case 2:
menu = IMG_Load("sprite/niveau_2p_2.png");
break;
case 3:
menu = IMG_Load("sprite/niveau_2p_3.png");
break;
case 4:
menu = IMG_Load("sprite/niveau_2p_4.png");
break;
case 5:
menu = IMG_Load("sprite/niveau_2p_5.png");
break;
case 6:
menu = IMG_Load("sprite/niveau_2p_6.png");
break;
default: break;
}
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer=0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE: // Veut arr<72>ter le jeu
continuer=0;
break;
case SDLK_UP:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/niveau_2p_6.png");;
choix_actuel=6;
break;
case 2:
menu = IMG_Load("sprite/niveau_2p_1.png");
choix_actuel=1;
break;
case 3:
menu = IMG_Load("sprite/niveau_2p_2.png");
choix_actuel=2;
break;
case 4:
menu = IMG_Load("sprite/niveau_2p_3.png");;
choix_actuel=3;
break;
case 5:
menu = IMG_Load("sprite/niveau_2p_4.png");
choix_actuel=4;
break;
case 6:
menu = IMG_Load("sprite/niveau_2p_5.png");
choix_actuel=5;
break;
}
break;
case SDLK_DOWN:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/niveau_2p_2.png");;
choix_actuel=2;
break;
case 2:
menu = IMG_Load("sprite/niveau_2p_3.png");
choix_actuel=3;
break;
case 3:
menu = IMG_Load("sprite/niveau_2p_4.png");
choix_actuel=4;
break;
case 4:
menu = IMG_Load("sprite/niveau_2p_5.png");;
choix_actuel=5;
break;
case 5:
menu = IMG_Load("sprite/niveau_2p_6.png");
choix_actuel=6;
break;
case 6:
menu = IMG_Load("sprite/niveau_2p_1.png");
choix_actuel=1;
break;
}
break;
case SDLK_RETURN:
SDL_FreeSurface(menu);
return choix_actuel;
break;
case SDLK_KP_ENTER:
SDL_FreeSurface(menu);
return choix_actuel;
break;
default : break;
}
break;
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(menu, NULL, screen, &positionMenu);
#ifdef HW_SCREEN_RESIZE
SDL_FillRect(hw_screen, NULL, 0x000000);
flip_NNOptimized_AllowOutOfScreen(screen, hw_screen,
HW_SCREEN_WIDTH,
MIN(screen->h*HW_SCREEN_WIDTH/screen->w, HW_SCREEN_HEIGHT));
SDL_Flip(hw_screen);
#else //HW_SCREEN_RESIZE
SDL_Flip(screen);
#endif //HW_SCREEN_RESIZE
}
SDL_FreeSurface(menu);
return 0;
}
int editeur_choix_niveau(SDL_Surface *screen){
#ifdef HW_SCREEN_RESIZE
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, 480,480, WINDOW_BPP, 0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(480,480, WINDOW_BPP, SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
#endif //HW_SCREEN_RESIZE
int continuer = 1, choix_actuel=1;
SDL_Surface *menu = NULL;
SDL_Event event;
SDL_Rect positionMenu;
positionMenu.x = 0;
positionMenu.y = 0;
menu = IMG_Load("sprite/niveau_2p_1.png");
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer=0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE: // Veut arr<72>ter le jeu
continuer=0;
break;
case SDLK_UP:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/niveau_2p_6.png");;
choix_actuel=6;
break;
case 2:
menu = IMG_Load("sprite/niveau_2p_1.png");
choix_actuel=1;
break;
case 3:
menu = IMG_Load("sprite/niveau_2p_2.png");
choix_actuel=2;
break;
case 4:
menu = IMG_Load("sprite/niveau_2p_3.png");;
choix_actuel=3;
break;
case 5:
menu = IMG_Load("sprite/niveau_2p_4.png");
choix_actuel=4;
break;
case 6:
menu = IMG_Load("sprite/niveau_2p_5.png");
choix_actuel=5;
break;
}
break;
case SDLK_DOWN:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/niveau_2p_2.png");;
choix_actuel=2;
break;
case 2:
menu = IMG_Load("sprite/niveau_2p_3.png");
choix_actuel=3;
break;
case 3:
menu = IMG_Load("sprite/niveau_2p_4.png");
choix_actuel=4;
break;
case 4:
menu = IMG_Load("sprite/niveau_2p_5.png");;
choix_actuel=5;
break;
case 5:
menu = IMG_Load("sprite/niveau_2p_6.png");
choix_actuel=6;
break;
case 6:
menu = IMG_Load("sprite/niveau_2p_1.png");
choix_actuel=1;
break;
}
break;
case SDLK_RETURN:
SDL_FreeSurface(menu);
return choix_actuel;
break;
case SDLK_KP_ENTER:
SDL_FreeSurface(menu);
return choix_actuel;
break;
default : break;
}
break;
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(menu, NULL, screen, &positionMenu);
#ifdef HW_SCREEN_RESIZE
SDL_FillRect(hw_screen, NULL, 0x000000);
flip_NNOptimized_AllowOutOfScreen(screen, hw_screen,
HW_SCREEN_WIDTH,
MIN(screen->h*HW_SCREEN_WIDTH/screen->w, HW_SCREEN_HEIGHT));
SDL_Flip(hw_screen);
#else //HW_SCREEN_RESIZE
SDL_Flip(screen);
#endif //HW_SCREEN_RESIZE
}
SDL_FreeSurface(menu);
return 0;
}
int are_you_sure(SDL_Surface *screen){
#ifdef HW_SCREEN_RESIZE
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, 480,480, WINDOW_BPP, 0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(480,480, WINDOW_BPP, SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
#endif //HW_SCREEN_RESIZE
int continuer = 1, choix_actuel=1;
SDL_Surface *menu = NULL;
SDL_Event event;
SDL_Rect positionMenu;
positionMenu.x = 0;
positionMenu.y = 0;
menu = IMG_Load("sprite/sure_yes.png");
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer=0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE: // Veut arr<72>ter le jeu
continuer=0;
break;
case SDLK_LEFT:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/sure_no.png");;
choix_actuel=2;
break;
case 2:
menu = IMG_Load("sprite/sure_yes.png");
choix_actuel=1;
break;
}
break;
case SDLK_RIGHT:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/sure_no.png");;
choix_actuel=2;
break;
case 2:
menu = IMG_Load("sprite/sure_yes.png");
choix_actuel=1;
break;
}
break;
case SDLK_RETURN:
SDL_FreeSurface(menu);
return choix_actuel;
break;
case SDLK_KP_ENTER:
SDL_FreeSurface(menu);
return choix_actuel;
break;
default : break;
}
break;
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(menu, NULL, screen, &positionMenu);
#ifdef HW_SCREEN_RESIZE
SDL_FillRect(hw_screen, NULL, 0x000000);
flip_NNOptimized_AllowOutOfScreen(screen, hw_screen,
HW_SCREEN_WIDTH,
MIN(screen->h*HW_SCREEN_WIDTH/screen->w, HW_SCREEN_HEIGHT));
SDL_Flip(hw_screen);
#else //HW_SCREEN_RESIZE
SDL_Flip(screen);
#endif //HW_SCREEN_RESIZE
}
SDL_FreeSurface(menu);
return 0;
}
int choix_nb_joueurs(SDL_Surface *screen){
#ifdef HW_SCREEN_RESIZE
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, 480,480, WINDOW_BPP, 0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(480,480, WINDOW_BPP, SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
#endif //HW_SCREEN_RESIZE
int continuer = 1, choix_actuel=1;
SDL_Surface *menu = NULL;
SDL_Event event;
SDL_Rect positionMenu;
positionMenu.x = 0;
positionMenu.y = 0;
menu = IMG_Load("sprite/menu_nb_joueur_1.png");
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer=0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE: // Veut arr<72>ter le jeu
continuer=0;
break;
case SDLK_UP:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/menu_nb_joueur_2.png");;
choix_actuel=2;
break;
case 2:
menu = IMG_Load("sprite/menu_nb_joueur_1.png");
choix_actuel=1;
break;
}
break;
case SDLK_DOWN:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/menu_nb_joueur_2.png");;
choix_actuel=2;
break;
case 2:
menu = IMG_Load("sprite/menu_nb_joueur_1.png");
choix_actuel=1;
break;
}
break;
case SDLK_RETURN:
SDL_FreeSurface(menu);
return choix_actuel;
break;
case SDLK_KP_ENTER:
SDL_FreeSurface(menu);
return choix_actuel;
break;
default : break;
}
break;
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(menu, NULL, screen, &positionMenu);
#ifdef HW_SCREEN_RESIZE
SDL_FillRect(hw_screen, NULL, 0x000000);
flip_NNOptimized_AllowOutOfScreen(screen, hw_screen,
HW_SCREEN_WIDTH,
MIN(screen->h*HW_SCREEN_WIDTH/screen->w, HW_SCREEN_HEIGHT));
SDL_Flip(hw_screen);
#else //HW_SCREEN_RESIZE
SDL_Flip(screen);
#endif //HW_SCREEN_RESIZE
}
SDL_FreeSurface(menu);
return 0;
}
int options(SDL_Surface *screen){
#ifdef HW_SCREEN_RESIZE
//if(screen != NULL) SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(SDL_SWSURFACE, 480,480, WINDOW_BPP, 0, 0, 0, 0);
#else //HW_SCREEN_RESIZE
screen = SDL_SetVideoMode(480,480, WINDOW_BPP, SDL_HWSURFACE);
if (screen == NULL) {
error("Can't set video mode: %s\n", SDL_GetError());
exit(1);
}
#endif //HW_SCREEN_RESIZE
int continuer = 1, choix_actuel=1;
SDL_Surface *menu = NULL;
SDL_Event event;
SDL_Rect positionMenu;
positionMenu.x = 0;
positionMenu.y = 0;
menu = IMG_Load("sprite/options_s_o.png");
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer=0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE: // Veut arr<72>ter le jeu
continuer=0;
break;
case SDLK_LEFT:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/options_s_n.png");;
choix_actuel=2;
break;
case 2:
menu = IMG_Load("sprite/options_s_o.png");
choix_actuel=1;
break;
case 3:
menu = IMG_Load("sprite/options_b_n.png");
choix_actuel=4;
break;
case 4:
menu = IMG_Load("sprite/options_b_o.png");
choix_actuel=3;
break;
}
break;
case SDLK_RIGHT:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/options_s_n.png");;
choix_actuel=2;
break;
case 2:
menu = IMG_Load("sprite/options_s_o.png");
choix_actuel=1;
break;
case 3:
menu = IMG_Load("sprite/options_b_n.png");
choix_actuel=4;
break;
case 4:
menu = IMG_Load("sprite/options_b_o.png");
choix_actuel=3;
break;
}
break;
case SDLK_UP:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/options_b_o.png");;
choix_actuel=3;
break;
case 2:
menu = IMG_Load("sprite/options_b_o.png");
choix_actuel=3;
break;
case 3:
menu = IMG_Load("sprite/options_s_o.png");
choix_actuel=1;
break;
case 4:
menu = IMG_Load("sprite/options_s_o.png");
choix_actuel=1;
break;
}
break;
case SDLK_DOWN:
switch(choix_actuel){
case 1:
menu = IMG_Load("sprite/options_b_o.png");;
choix_actuel=3;
break;
case 2:
menu = IMG_Load("sprite/options_b_o.png");
choix_actuel=3;
break;
case 3:
menu = IMG_Load("sprite/options_s_o.png");
choix_actuel=1;
break;
case 4:
menu = IMG_Load("sprite/options_s_o.png");
choix_actuel=1;
break;
}
break;
case SDLK_RETURN:
SDL_FreeSurface(menu);
return choix_actuel;
break;
case SDLK_KP_ENTER:
SDL_FreeSurface(menu);
return choix_actuel;
break;
default : break;
}
break;
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(menu, NULL, screen, &positionMenu);
#ifdef HW_SCREEN_RESIZE
SDL_FillRect(hw_screen, NULL, 0x000000);
flip_NNOptimized_AllowOutOfScreen(screen, hw_screen,
HW_SCREEN_WIDTH,
MIN(screen->h*HW_SCREEN_WIDTH/screen->w, HW_SCREEN_HEIGHT));
SDL_Flip(hw_screen);
#else //HW_SCREEN_RESIZE
SDL_Flip(screen);
#endif //HW_SCREEN_RESIZE
}
SDL_FreeSurface(menu);
return 0;
}

455
src/player.c Normal file
View File

@@ -0,0 +1,455 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "SDL.h"
#include "SDL_image.h"
#include "../include/game.h"
struct s_player {
int x, y, portee_bomb, win;
SDL_Surface * directed_img[4];
enum e_way current_way;
int lives;
int nb_bomb;
int nb_bomb_max;//cette variable est utile pour les bonus. Elle sert <20> ce que, par exemple, si le joueur a pos<6F> toutes ses bombes (disons 2) et qu'avant qu'elles explosent le joueur prend un bonus decrease_nb_bombs, il ne sera autoris<69> qu'<27> poser une seule bombe apr<70>s l'explosion des deux sur la map.
int dead;
};
static void player_load_img(t_player player, enum e_way way,
const char *filename) {
player->directed_img[way] = IMG_Load(filename);
if (!player->directed_img[way])
error("IMG_Load: %s\n", IMG_GetError());
}
t_player player_init( int nb_bomb, int portee_bomb, int lives, int no_joueur) {
t_player player = malloc(sizeof(*player));
if (!player)
error("Memory error");
switch (no_joueur) {
case 1:
player_load_img(player, LEFT, IMG_PLAYER_LEFT);
player_load_img(player, RIGHT, IMG_PLAYER_RIGHT);
player_load_img(player, UP, IMG_PLAYER_UP);
player_load_img(player, DOWN, IMG_PLAYER_DOWN);
break;
case 2:
player_load_img(player, LEFT, IMG_PLAYER_2_LEFT);
player_load_img(player, RIGHT, IMG_PLAYER_2_RIGHT);
player_load_img(player, UP, IMG_PLAYER_2_UP);
player_load_img(player, DOWN, IMG_PLAYER_2_DOWN);
break;
default: break;
}
player->current_way = DOWN;
player->win= 0;
player->portee_bomb=portee_bomb;
player->nb_bomb_max=nb_bomb;
player->nb_bomb=nb_bomb;
player->lives=lives;
player->dead=6;
return player;
}
void player_free(t_player player) {
assert(player);
int i;
for (i = 0; i < 4; i++)
SDL_FreeSurface(player->directed_img[i]);
free(player);
}
int player_get_x(t_player player) {
assert(player != NULL);
return player->x;
}
int player_get_y(t_player player) {
assert(player != NULL);
return player->y;
}
int player_win(t_player player){
assert(player != NULL);
return player->win;
}
int player_portee_bomb(t_player player){
assert(player != NULL);
return player->portee_bomb;
}
void player_set_current_way(t_player player, enum e_way way) {
assert(player);
player->current_way = way;
}
void player_increase_lives(t_player player) {
assert(player);
if (player->lives <MAXIMUM_LIVES)
player->lives++;
}
void player_decrease_lives(t_player player) {
assert(player);
if (player->lives >0)
player->lives--;
}
int player_get_lives(t_player player) {
assert(player);
return player->lives;
}
int player_get_nb_bomb(t_player player) {
assert(player);
return player->nb_bomb;
}
int player_get_nb_bomb_max(t_player player) {
assert(player);
return player->nb_bomb_max;
}
void player_set_nb_bomb(t_player player, int bomb_number) {
assert(player);
player->nb_bomb= bomb_number;
}
void player_increase_nb_bomb_max(t_player player) {
assert(player);
if (player->nb_bomb <BOMB_NUMBER_MAX)
player->nb_bomb_max++;
}
void player_decrease_nb_bomb_max(t_player player) {
assert(player);
if (player->nb_bomb_max >1)
player->nb_bomb_max--;
}
void player_increase_nb_bomb(t_player player) {
assert(player);
if (player->nb_bomb <BOMB_NUMBER_MAX)
player->nb_bomb++;
}
void player_decrease_nb_bomb(t_player player) {
assert(player);
if (player->nb_bomb >0)
player->nb_bomb--;
}
void player_increase_portee(t_player player) {
assert(player);
if (player->portee_bomb <BOMB_RANGE_MAX)
player->portee_bomb++;
}
void player_decrease_portee(t_player player) {
assert(player);
if (player->portee_bomb >1)
player->portee_bomb--;
}
int player_from_map(t_player player, t_map map) {
int i, j;
for (i = 0; i < map_get_height(map); i++) {
for (j = 0; j < map_get_width(map); j++) {
if (map_get_cell_type(map, j, i) == CELL_PLAYER) {
player->x = j;
player->y = i;
return 0;
}
}
}
return 0;
}
void player2_from_map(t_player player, t_map map) {
int i, j;
for (i = 0; i < map_get_height(map); i++) {
for (j = 0; j < map_get_width(map); j++) {
if (map_get_cell_type(map, j, i) == CELL_PLAYER_2) {
player->x = j;
player->y = i;
}
}
}
}
void player_die(t_player player){
if (player->dead>0){
switch (player->current_way){
case UP:
player->current_way=RIGHT;
break;
case RIGHT:
player->current_way=DOWN;
break;
case DOWN:
player->current_way=LEFT;
break;
case LEFT:
player->current_way=UP;
break;
}
player->dead--;
}
}
int player_get_dead(t_player player) {
assert(player);
return player->dead;
}
static int player_move_aux(t_player player, t_map map, int x1, int y1, int x2, int y2) {
t_cell_type type;
if (!map_is_inside(map, x1, y1))
return 0;
type=map_get_cell_type(map, x1, y1);
switch (type & 0x0f) {
case CELL_WALL:
return 0;
break;
case CELL_BOMB:
return 0;
break;
case CELL_FLAMME:
player_decrease_lives(player);
break;
case CELL_MENU_EMPTY:
return 0;
break;
case CELL_MENU_LIVES:
return 0;
break;
case CELL_MENU_BOMBS:
return 0;
break;
case CELL_MENU_RANGE:
return 0;
break;
case CELL_BOX:
if (!map_is_inside(map, x2, y2))
return 0;
switch (map_get_cell_type(map,x2,y2)& 0x0f){
case CELL_WALL:
return 0;
break;
case CELL_BOX:
return 0;
break;
case CELL_GOAL:
return 0;
break;
case CELL_MONSTER:
return 0;
break;
case CELL_PLAYER:
return 0;
break;
case CELL_PLAYER_2:
return 0;
break;
case CELL_BOMB:
return 0;
break;
case CELL_MENU_EMPTY:
return 0;
break;
case CELL_MENU_LIVES:
return 0;
break;
case CELL_MENU_BOMBS:
return 0;
break;
case CELL_MENU_RANGE:
return 0;
break;
case CELL_MENU_PLAYER_1:
return 0;
break;
case CELL_MENU_PLAYER_2:
return 0;
break;
default:
break;
}
map_set_cell_type(map, x1, y1, CELL_EMPTY);
switch ((type & 0xf0) >> 4) {
case BONUS_BOMB_RANGE_INC:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_BOMB_RANGE_INC << 4));
break;
case BONUS_BOMB_RANGE_DEC:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_BOMB_RANGE_DEC << 4));
break;
case BONUS_BOMB_NB_INC:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_BOMB_NB_INC << 4));
break;
case BONUS_BOMB_NB_DEC:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_BOMB_NB_DEC << 4));
break;
case BONUS_MONSTER:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_MONSTER << 4));
break;
case BONUS_LIFE_INC:
map_set_cell_type(map, x2, y2, CELL_BOX | (BONUS_LIFE_INC << 4));
break;
case NO_BONUS:
map_set_cell_type(map, x2, y2, CELL_BOX | (NO_BONUS << 4));
break;
default:
break;
}
return 1;
break;
case CELL_BONUS:
switch ((type & 0xf0) >> 4) {
case BONUS_BOMB_RANGE_INC:
player_increase_portee(player);
break;
case BONUS_BOMB_RANGE_DEC:
player_decrease_portee(player);
break;
case BONUS_BOMB_NB_INC:
player_increase_nb_bomb_max(player);
player_increase_nb_bomb(player);
break;
case BONUS_BOMB_NB_DEC:
player_decrease_nb_bomb_max(player);
if (player->nb_bomb>1)
player_decrease_nb_bomb(player);
break;
case BONUS_LIFE_INC:
if (player->lives<MAXIMUM_LIVES)
player->lives++;
break;
default:
break;
}
break;
case CELL_GOAL:
player->win=1;
break;
case CELL_MONSTER:
player->lives--;
break;
case CELL_PLAYER:
return 0;
break;
case CELL_PLAYER_2:
return 0;
break;
case CELL_MENU_PLAYER_1:
return 0;
break;
case CELL_MENU_PLAYER_2:
return 0;
break;
default:
break;
}
// Player has moved
return 1;
}
int player_move(t_player player, t_map map) {
int x = player->x;
int y = player->y;
int move = 0;
switch (player->current_way) {
case UP:
if (player_move_aux(player, map, x, y - 1, x, y - 2)) {
player->y--;
move = 1;
}
break;
case DOWN:
if (player_move_aux(player, map, x, y + 1, x, y + 2)) {
player->y++;
move = 1;
}
break;
case LEFT:
if (player_move_aux(player, map, x - 1, y, x - 2, y)) {
player->x--;
move = 1;
}
break;
case RIGHT:
if (player_move_aux(player, map, x + 1, y, x + 2, y)) {
player->x++;
move = 1;
}
break;
}
if (move) {
if ( map_get_cell_type(map,x,y)==CELL_PLAYER || map_get_cell_type(map,x,y)==CELL_PLAYER_2){
map_set_cell_type(map, x, y, CELL_EMPTY);
}
map_set_cell_type(map, player->x, player->y, CELL_PLAYER);
}
return move;
}
void player_display(t_player player, SDL_Surface *screen) {
assert(player);
assert(player->directed_img[player->current_way]);
assert(screen);
SDL_Rect place;
place.x = player->x * SIZE_BLOC;
place.y = player->y * SIZE_BLOC;
SDL_BlitSurface(player->directed_img[player->current_way], NULL, screen,
&place);
}