Files
Sokoban/script/display.c
2024-12-24 15:51:26 +01:00

241 lines
7.0 KiB
C

/**
* \file display.c
*
*/
#include "../include/display.h"
#include "../include/function.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mutex.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <stdlib.h>
/**
* \brief La fonction permet d'afficher simplement le plateau de jeu dans le
* terminal.
* \param tab Le tableau 2d du plateau.
* \param x Le mombre de ligne.
* \param y Le nombre de colonnes.
* \return Void
*/
void screenDisplay (char **tab, int x ,int y )
{
// puts("0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 ");
char element[9] = { '#', '_', 'S', '.', '*', '@', '+' , 'o', 'O'};
for (int i = 0; i < x; ++i)
{
for (int j = 0; j < y; ++j)
{
printf ("%c ", element[(int)tab[i][j]]);
}
puts ("");
}
}
/**
* \brief La fonction affiche a l'aide de SDL la zone de jeu.
* \param tab Le tableau 2d de la zone de jeu.
* \param display_user La structure qui possede tous ce qu'il faut pour
* l'affichage SDL
* \param player_pos la position du joueur
* \param fov Le fov de la vision du joueur, -1 si desactiver.
* \return Void
*/
void screenDisplayGameSDL (char **tab, dis *display_user, vect *player_pos, int fov)
{
unsigned int display_game
= display_user->size_window - display_user->size_menu;
int size = display_game / display_user->size_box;
unsigned int marge = display_user->size_menu / 2;
unsigned int i, j, x = 0, y = 0;
int start_i = 0, start_j = 0;
unsigned int limit_i = display_user->size_box, limit_j = display_user->size_box;
if (fov >= 0 )
{
start_i = player_pos->y - fov+1;
if(start_i < 0) start_i = 0;
start_j = player_pos->x - fov +1 ;
if(start_j < 0) start_j = 0;
limit_i = player_pos->y + fov;
if(limit_i > display_user->size_box) limit_i = display_user->size_box;
limit_j = player_pos->x + fov;
if(limit_j > display_user->size_box) limit_j = display_user->size_box;
size = display_game / (fov*2 - 1);
}
SDL_Surface *img;
SDL_Texture *texture;
for (i = start_i; i < limit_i; ++i)
{
for (j = start_j; j < limit_j; ++j)
{
vect pos = { x * size + marge, y * size};
switch (tab[j][i])
{
case EMPTY:
img = IMG_Load ("image/empty.png");
break;
case WALL:
img = IMG_Load ("image/wall.png");
break;
case PLAYER:
img = IMG_Load ("image/player.png");
break;
case TARGET:
img = IMG_Load ("image/target.png");
break;
case BOX:
img = IMG_Load ("image/box.png");
break;
case BOX_ON_TARGET:
img = IMG_Load ("image/box_on_target.png");
break;
case PLAYER_ON_TARGET:
img = IMG_Load ("image/player_on_target.png");
break;
}
texture
= SDL_CreateTextureFromSurface (display_user->renderer, img);
displayImage (display_user->renderer, texture, pos, size);
SDL_FreeSurface (img);
SDL_DestroyTexture (texture);
y++;
}
y = 0;
x++;
}
SDL_RenderPresent (display_user->renderer);
}
/**
* \brief Taille de l'ecrant carre en fonction de l'ecrant du joueur avec une
* marge.
* \param display_user Qui serra modifier pour stoquer les information.
* \return La taille max pour la fenetre de l'utilisateur.
*/
int getMaxSize (dis display_user)
{
SDL_Init (SDL_INIT_VIDEO); // init if error
SDL_DisplayMode display;
SDL_GetCurrentDisplayMode (0, &display); // get dim display user
int result = 0;
if (display.w <= display.h)
{
result = display.w;
}
else
{
result = display.h;
}
SDL_Quit ();
int minus_mod = (result - 50) % display_user.size_box;
// printf("DIS :%d, size : %d ,mod : %d\n",result, size_box,minus_mod);
return (result - 50 - minus_mod); // margin
}
/**
* \brief Initialise SDL.
* \param display_user Stockage d'éléments SDL.
* \return Void
*/
void initSDL (dis *display_user)
{
SDL_Init (SDL_INIT_VIDEO);
TTF_Init ();
display_user->window
= SDL_CreateWindow ("Sokoman", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, display_user->size_window,
display_user->size_window, SDL_WINDOW_SHOWN);
if (!display_user->window)
{
SDL_Quit ();
perror ("Window null");
exit (-1);
}
display_user->renderer
= SDL_CreateRenderer (display_user->window, -1, SDL_RENDERER_SOFTWARE);
if (!display_user->renderer)
{
SDL_Quit ();
perror ("Renderer null");
exit (-1);
}
}
/**
* \brief Cette fonction affiche l'image dans la fenetre de l'utilisateur.
* \param renderer Le renderer de l'utilisateur.
* \param texture La texture de l'image à appliquer.
* \param pos La position de l'image à afficher.
* \param size La taile de l'image.
* \return Void
**/
void displayImage (SDL_Renderer *renderer, SDL_Texture *texture, vect pos,
int size)
{
SDL_Rect rect = { pos.x, pos.y, size, size };
SDL_RenderCopy (renderer, texture, NULL, &rect);
}
/**
* \brief Cette fonction affiche du texte dans la fenetre de l'utilisateur.
* \param display_user Tous les éléments SDL de l'utilisateur.
* \param text Le string à afficher.
* \param coor Les coordonnée du texte.
* \param size La taille du texte.
* \param font_size La taille de la font.
* \return Void
*/
void displayTextSDL (dis *display_user, char *text, vect coor, vect size,
int font_size)
{
TTF_Font *Sans = TTF_OpenFont ("Roboto-Regular.ttf", font_size);
SDL_Color white = { 255, 255, 255, 255 };
int text_width, text_height;
SDL_Rect background;
SDL_Surface *surface_text = TTF_RenderText_Solid (Sans, text, white);
TTF_SizeText (Sans, text, &text_height, &text_width);
SDL_Rect message_rect;
message_rect.x = coor.x;
message_rect.y = coor.y;
message_rect.w = text_height;
message_rect.h = text_width;
background.x = coor.x;
background.y = coor.y;
background.w = text_height;
background.h = text_width;
if (message_rect.w > size.y)
{
message_rect.w = size.y;
background.w = size.y;
}
if (message_rect.h > size.x)
{
message_rect.h = size.x;
background.h = size.x;
}
SDL_Texture *message
= SDL_CreateTextureFromSurface (display_user->renderer, surface_text);
SDL_SetRenderDrawColor (display_user->renderer, 0, 0, 0, 255);
SDL_RenderFillRect (display_user->renderer, &background);
SDL_RenderCopy (display_user->renderer, message, NULL, &message_rect);
SDL_RenderPresent (display_user->renderer);
SDL_FreeSurface (surface_text);
SDL_DestroyTexture (message);
TTF_CloseFont (Sans);
}