Skip to content
Snippets Groups Projects
button.c 1.74 KiB
Newer Older
llebasca's avatar
llebasca committed
/*
// Created by Lucile on 04/04/2021.
*/

/*!
 * \file button.c
 * \brief Button Source File
 * \authors Lucile
 * \version 2
 * \date 04/04/2021
 *
 * Button functions implementation.
 *
 */

#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "button.h"

/*!
 * \fn void init_button(Button * button, int x, int y, int length, int width, char * path)
 * \brief This function initializes a button : position of the button's top left corner (x,y), size (length, width), state (OFF by default) and also the path to the button's image.
 *
 * \param [in] button a pointer on a Button
 * \param [in] x the button's abscissa
 * \param [in] y the button's ordinate
 * \param [in] length the button's length
 * \param [in] width the button's width
 * \param [in] path the path to the Teacher's image
 */

void init_button(Button * button, int x, int y, int length, int width, char * path){
    button->x_button=x;
    button->y_button=y;
    button->long_button=length;
    button->larg_button=width;
    button->state=0;
    button->image_button = malloc(strlen(path));
    strcpy(button->image_button, path);
}

/*!
 * \fn int click(int x, int y, Button * button)
 * \brief Sets the button's state on ON if the mouse is clicking on it, stays at OFF otherwise.
 *
 * \param [in] x Mouse's abscissa
 * \param [in] y Mouse's ordinate
 * \param [in] button a pointer on a button
 * \return The button's state : 1 if the mouse is on the button, 0 otherwise
 */

int click(int x, int y, Button * button) {
    if (button->x_button < x && x < (button->x_button + button->long_button)
        && button->y_button < y && y < (button->y_button + button->larg_button)) {
        button->state=1;
llebasca's avatar
llebasca committed
    } else {
        button->state=0;
llebasca's avatar
llebasca committed
    }
    return button->state;
}