Skip to content
Snippets Groups Projects
GAME.c 10.40 KiB

/*!
 * \file GAME.c
 * \brief Game Source File
 * \authors Tin
 * \version 1
 * \date 03/03/2021
 *
 * Explanation.
 *
 */

#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "GAME.h"
#include "prof.h"
#include "personnage.h"
#include "map.h"
#include "DS.h"

/*!
 * \fn SDL_Texture * query_hero(Personnage * hero, SDL_Rect * hero_rect, SDL_Renderer * renderer, SDL_Window * window)
 * \brief This function query a picture into a texture for hero
 *
 * \param [in] hero the hero pointer
 * \param [in] hero_rect the pointer to the structure rect of hero
 * \param [in] renderer the renderer pointer
 * \param [in] window the window pointer
 */
SDL_Texture * query_hero(Personnage * hero, SDL_Rect * hero_rect, SDL_Renderer * renderer, SDL_Window * window){

    SDL_Texture * texture_hero;

    // load the image data into the graphics hardware's memory
    texture_hero = IMG_LoadTexture(renderer, hero->image_pers);

    if (!texture_hero)
    {
        printf("Cannot create texture for persson: %s\n", SDL_GetError());
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return NULL;
    }

    // get and scale the dimensions of texture
    SDL_QueryTexture(texture_hero, NULL, NULL, &hero_rect->w, &hero_rect->h);
    hero_rect->x =  hero->x_pers;
    hero_rect->y =  hero->y_pers;
    hero_rect->w =  hero->larg_pers;
    hero_rect->h =  hero->long_pers;

    return texture_hero;
}

/*!
 * \fn SDL_Texture * query_heart(SDL_Rect * heart_rect, SDL_Renderer * renderer, SDL_Window * window, char * path)
 * \brief This function query a picture into a texture for heart's life
 *
 * \param [in] path the path of 1, 2 or 3 heart
 * \param [in] heart_rect the pointer to the structure rect of heart
 * \param [in] renderer the renderer pointer
 * \param [in] window the window pointer
 */
SDL_Texture * query_heart(SDL_Rect * heart_rect, SDL_Renderer * renderer, SDL_Window * window, char * path){
    SDL_Texture * texture_heart;

    // load the image data into the graphics hardware's memory
    texture_heart = IMG_LoadTexture(renderer, path);

    if (!texture_heart)
    {
        printf("Cannot create texture for heart: %s\n", SDL_GetError());
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return NULL;
    }

    // get and scale the dimensions of texture
    SDL_QueryTexture(texture_heart, NULL, NULL, &heart_rect->w, &heart_rect->h);
    heart_rect->w =  75;
    heart_rect->h =  50;
    heart_rect->x =  620;
    heart_rect->y =  -5;
    return texture_heart;
}



/*!
 * \fn SDL_Texture * query_teacher(Prof * teacher, SDL_Rect * teacher_rect, SDL_Renderer * renderer, SDL_Window * window )
 * \brief This function query a picture into a texture for teacher
 *
 * \param [in] teacher the teacher pointer
 * \param [in] teacher_rect the pointer to the structure rect of teacher
 * \param [in] renderer the renderer pointer
 * \param [in] window the window pointer
 */
 
SDL_Texture * query_teacher(Prof * teacher, SDL_Rect * teacher_rect, SDL_Renderer * renderer, SDL_Window * window ){

    SDL_Texture * texture_teacher;


    // load the image data into the graphics hardware's memory
    texture_teacher = IMG_LoadTexture(renderer, teacher->image_prof);

    if (!texture_teacher)
    {
        printf("Cannot create texture for prof: %s\n", SDL_GetError());
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return NULL;
    }

// get and scale the dimensions of texture
    SDL_QueryTexture(texture_teacher, NULL, NULL, &teacher_rect->w, &teacher_rect->h);
    teacher_rect->w = teacher->larg_prof;
    teacher_rect->h = teacher->long_prof;
    teacher_rect->x = teacher->x_prof;
    teacher_rect->y = teacher->y_prof;

    return texture_teacher;
}

/*!
 * \fn SDL_Texture * query_exam(DS * exam, SDL_Rect * exam_rect, SDL_Renderer * renderer, SDL_Window * window)
 * \brief This function query a picture into a texture for exam
 *
 * \param [in] exam the exam pointer
 * \param [in] exam_rect the pointer to the structure rect of exam
 * \param [in] renderer the renderer pointer
 * \param [in] window the window pointer
 */
 
SDL_Texture * query_exam(DS * exam, SDL_Rect * exam_rect, SDL_Renderer * renderer, SDL_Window * window){

    SDL_Texture * texture_exam;

    // load the image data into the graphics hardware's memory
    texture_exam = IMG_LoadTexture(renderer, exam->image_DS);

    if (!texture_exam)
    {
        printf("error creating texture: %s\n", SDL_GetError());
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return NULL;
    }

    SDL_QueryTexture(texture_exam, NULL, NULL, &exam_rect->w, &exam_rect->h);
    exam_rect->w /= 30;
    exam_rect->h /= 35;
    exam_rect->x=exam->x_DS;
    exam_rect->y=exam->y_DS;

    return texture_exam;
}


/*!
 * \fn SDL_Texture * query_map(MAP * map_game, SDL_Renderer * renderer, SDL_Window * window)
 * \brief This function query a picture into a texture for map
 *
 * \param [in] map_game the map of game's pointer
 * \param [in] renderer the renderer pointer
 * \param [in] window the window pointer
 */
SDL_Texture * query_map(MAP * map_game, SDL_Renderer * renderer, SDL_Window * window){
    init_map(map_game,"../ressource/colored.bmp","../ressource/mapvis.bmp");
    map_game->functional = maptomatrix(map_game);

    //Create a texture for map
    SDL_Texture * texture_map = IMG_LoadTexture(renderer, map_game->visual_path);

    if (!texture_map)
    {
        printf("error creating texture for map: %s\n", SDL_GetError());
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return NULL;
    }

    return texture_map;
}


/*!
 * \fn SDL_Texture * query_score(SDL_Renderer *renderer, SDL_Rect* score_rect, SDL_Window * window, int* score)
 * \brief This function query a score texture
 *
 * \param [in] score_rect the SDL rect of score
 * \param [in] renderer the renderer pointer
 * \param [in] window the window pointer
 * \param [in] score the score pointer
 */
SDL_Texture * query_score(SDL_Renderer *renderer, SDL_Rect* score_rect, SDL_Window * window, int* score){
	
	char scoreString[10];
	
    SDL_Color white = {255, 255, 255};  //  text's color

    itoa(*score, scoreString, 10);
	
    TTF_Font* Sans = TTF_OpenFont("../ressource/OpenSans-Semibold.ttf", 8); //font style and sets a size
	
    TTF_SetFontStyle(Sans, 2);
    SDL_Surface* score_surface = TTF_RenderText_Solid(Sans, scoreString , white);
    SDL_Texture* texture_score = SDL_CreateTextureFromSurface(renderer, score_surface);
    SDL_FreeSurface(score_surface);
	
	
    // draw the image to the window
    if (!texture_score)
    {
        printf("error creating texture: %s\n", SDL_GetError());
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return NULL;
    }

    score_rect->x = 330;  //controls the rect's x coordinate
    score_rect->y = 394; // controls the rect's y coordinte
    score_rect->w = 100; // controls the width of the rect
    score_rect->h = 128; // controls the height of the rect

    

    return texture_score;
}


/*!
 * \fn int show_image_in_time(SDL_Renderer* renderer, SDL_Window * window, char * path, int time)
 * \brief This function show an image in some duration
 *
 * \param [in] renderer the renderer pointer
 * \param [in] window the window pointer
 * \param [in] path the path of image
 * \param [in] time the duration
 */

int show_image_in_time(SDL_Renderer* renderer, SDL_Window * window, char * path, int time) {
//texture for beginning page
    SDL_Texture *texture_begin_page;
    texture_begin_page = IMG_LoadTexture(renderer, path);
    if (!texture_begin_page) {
        printf("error creating texture: %s\n", SDL_GetError());
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }
    SDL_RenderCopy(renderer, texture_begin_page, NULL, NULL);
    SDL_RenderPresent(renderer);
    SDL_Delay(time);
    SDL_DestroyTexture(texture_begin_page);
    return 0;
}
/*!
 * \fn int calculer_score(Personnage * hero,  int * time_remaining)
 * \brief This function test and calculate the result of the game
 *
 * \param [in] hero the hero pointer
 * \param [in] time_remaining the time remaining pointer

 */

int calculer_score(Personnage * hero,  int * time_remaining){
    if(hero->result==WIN){
		
		return 1000*hero->DS_took + 1000*hero->life + 50*(*time_remaining);
		
	}
	else{
		
		return 1000*hero->DS_took + 1000*hero->life;
	}
}


/*!
 * \fn void test_event_hero(Personnage * hero, SDL_Event * event, int * close)
 * \brief This function test the events of keyboard and change the model of hero and close_request if necessary
 *
 * \param [in] hero the hero pointer
 * \param [in] event the SDL event
 * \param [in] close the close pointer
 */

void test_event_hero(Personnage * hero, SDL_Event * event, int * close){
    while (SDL_PollEvent(event))
    {
        switch (event->type)
        {
            case SDL_QUIT:
                *close = 1;
                hero->result=LOSE;
                break;
            case SDL_KEYDOWN:
                switch (event->key.keysym.scancode)
                {
                    case SDL_SCANCODE_UP:
                        hero->direction_personnage.up = 1;
                        break;
                    case SDL_SCANCODE_LEFT:
                        hero->direction_personnage.left = 1;
                        break;
                    case SDL_SCANCODE_DOWN:
                        hero->direction_personnage.down = 1;
                        break;
                    case SDL_SCANCODE_RIGHT:
                        hero->direction_personnage.right = 1;
                        break;
                }
                break;
            case SDL_KEYUP:
                switch (event->key.keysym.scancode)
                {
                    case SDL_SCANCODE_UP:
                        hero->direction_personnage.up = 0;
                        break;
                    case SDL_SCANCODE_LEFT:
                        hero->direction_personnage.left = 0;
                        break;
                    case SDL_SCANCODE_DOWN:
                        hero->direction_personnage.down = 0;
                        break;
                    case SDL_SCANCODE_RIGHT:
                        hero->direction_personnage.right = 0;
                        break;
                }
                break;
        }
    }
}