display SDL

This commit is contained in:
2024-12-14 13:05:38 +01:00
parent 19a05574ab
commit 54e7f38ff2
14 changed files with 182 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
# Variable # Variable
CC = gcc CC = gcc
CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2) CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2 SDL2_image)
OBJ = main.o function.o display.o read.o OBJ = main.o function.o display.o read.o
TARGET = sokoban TARGET = sokoban
@@ -15,7 +15,7 @@ main.o : main.c function.h display.h read.h
function.o : function.c function.h function.o : function.c function.h
$(CC) $(CFLAGS) -c function.c $(CC) $(CFLAGS) -c function.c
display.o: display.c display.h display.o: display.c display.h function.h
$(CC) $(CFLAGS) -c display.c $(CC) $(CFLAGS) -c display.c
read.o: read.c read.h function.h read.o: read.c read.h function.h

BIN
box.png LFS Normal file

Binary file not shown.

BIN
box_on_target.png LFS Normal file

Binary file not shown.

104
display.c
View File

@@ -1,4 +1,11 @@
#include "display.h" #include "display.h"
#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 <stdio.h> #include <stdio.h>
void void
@@ -15,3 +22,100 @@ screenDisplay (unsigned short int **tab, int size)
puts (""); puts ("");
} }
} }
void
screenDisplaySDL (unsigned short int **tab, dis *display_user)
{
int size = display_user->size_window / display_user->size_box;
int i, j;
for (i = 0; i < display_user->size_box; ++i)
{
for (j = 0; j < display_user->size_box; ++j)
{
SDL_Surface *img;
SDL_Texture *texture;
vect pos = { i*size, j *size};
switch(tab[j][i])
{
case EMPTY:
img = IMG_Load("empty.png") ;
break;
case WALL:
img = IMG_Load("wall.png") ;
break;
case PLAYER:
img = IMG_Load("player.png");
break;
case TARGET:
img = IMG_Load("target.png") ;
break;
case BOX:
img = IMG_Load("box.png");
break;
case BOX_ON_TARGET:
img = IMG_Load("box_on_target.png") ;
break;
case PLAYER_ON_TARGET:
img = IMG_Load("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);
}
}
SDL_RenderPresent(display_user->renderer);
}
int
getMaxSize ()
{
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();
return (result - 50); // margin
}
void
initSDL (dis *display_user)
{
SDL_Init (SDL_INIT_VIDEO);
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);
}
}
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);
}

View File

@@ -1,7 +1,15 @@
#ifndef DISPLAY_H #ifndef DISPLAY_H
#define DISPLAY_H #define DISPLAY_H
#include <SDL2/SDL.h>
#include "function.h"
void screenDisplay( unsigned short int **tab,int size); void screenDisplay( unsigned short int **tab,int size);
int getMaxSize ();
void displayImage(SDL_Renderer *renderer, SDL_Texture *texture, vect pos, int size);
void initSDL (dis *display_user);
void screenDisplaySDL (unsigned short int **tab, dis *display_user);
#endif // !DISPLAY_H #endif // !DISPLAY_H

BIN
empty.png LFS Normal file

Binary file not shown.

View File

@@ -1,5 +1,7 @@
#include "function.h" #include "function.h"
#include "display.h"
#include <SDL2/SDL_render.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -181,7 +183,7 @@ move (unsigned short int **tab, vect *playerPos, vect direction)
void void
inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets, inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
int nbr_targets) int nbr_targets, dis *display_user)
{ {
vect direction = { 0, 0 }; vect direction = { 0, 0 };
char input; char input;
@@ -236,6 +238,7 @@ inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
finish = true; finish = true;
} }
screenDisplay (tab2d, N); screenDisplay (tab2d, N);
screenDisplaySDL(tab2d, display_user);
} }
} }

View File

@@ -1,9 +1,12 @@
#ifndef FONCTION_H #ifndef FONCTION_H
#define FONCTION_H #define FONCTION_H
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <SDL2/SDL.h>
#define EMPTY 0 #define EMPTY 0
#define WALL 1 #define WALL 1
@@ -27,14 +30,23 @@ typedef struct Score
unsigned int move_box; unsigned int move_box;
} score; } score;
typedef struct essential_sdl
{
SDL_Window *window ;
SDL_Renderer *renderer;
unsigned int size_window;
unsigned int size_box;
}dis;
unsigned short int **creatArea2D (const unsigned int N); unsigned short int **creatArea2D (const unsigned int N);
void free2D (unsigned short int **tab, int N); void free2D (unsigned short int **tab, int N);
void screenDisplay (unsigned short int **tab, int size); void screenDisplay (unsigned short int **tab, int size);
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
vect *targets, int nbr_targets);
bool isWin (unsigned short int **tab2d, vect *targets, int nbr_targets); bool isWin (unsigned short int **tab2d, vect *targets, int nbr_targets);
bool islose (unsigned short int **tab2d, const int N); bool islose (unsigned short int **tab2d, const int N);
bool blockBox (unsigned short int **tab2d, vect box_coor); bool blockBox (unsigned short int **tab2d, vect box_coor);
vect plusVect (vect one, vect two); vect plusVect (vect one, vect two);
void
inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
int nbr_targets, dis *display_user);
#endif // FONCTION_H #endif // FONCTION_H

31
main.c
View File

@@ -1,29 +1,50 @@
#include "display.h" #include "display.h"
#include "function.h" #include "function.h"
#include "read.h" #include "read.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#define SIZE_PLAY 24
#define SIZE_PLAY 30
int int
main () main ()
{ {
dis display_user;
display_user.size_window = getMaxSize();
display_user.size_box = SIZE_PLAY;
initSDL(&display_user);
vect *playerPos = (vect *)malloc (sizeof (vect)); vect *playerPos = (vect *)malloc (sizeof (vect));
vect *targets; vect *targets;
int nbr_targets; int nbr_targets;
score playerScore; score playerScore;
playerScore.before = time (NULL);
unsigned short int **tab2d = creatArea2D (SIZE_PLAY); unsigned short int **tab2d = creatArea2D (SIZE_PLAY);
targets
= fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets); playerScore.before = time (NULL);
targets = fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets);
screenDisplaySDL(tab2d, &display_user);
screenDisplay (tab2d, SIZE_PLAY); screenDisplay (tab2d, SIZE_PLAY);
inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets); inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets, &display_user);
playerScore.after = time (NULL); playerScore.after = time (NULL);
printf ("%ld\n", playerScore.after - playerScore.before); printf ("%ld\n", playerScore.after - playerScore.before);
SDL_DestroyWindow(display_user.window);
SDL_DestroyRenderer(display_user.renderer);
free2D (tab2d, SIZE_PLAY); free2D (tab2d, SIZE_PLAY);
free (playerPos); free (playerPos);
free (targets); free (targets);
SDL_Quit();
return 0; return 0;
} }

BIN
player.png LFS Normal file

Binary file not shown.

BIN
player_on_target.png LFS Normal file

Binary file not shown.

BIN
target.png LFS Normal file

Binary file not shown.

BIN
test.png LFS Normal file

Binary file not shown.

BIN
wall.png LFS Normal file

Binary file not shown.