display SDL
This commit is contained in:
4
Makefile
4
Makefile
@@ -1,6 +1,6 @@
|
||||
# Variable
|
||||
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
|
||||
TARGET = sokoban
|
||||
|
||||
@@ -15,7 +15,7 @@ main.o : main.c function.h display.h read.h
|
||||
function.o : function.c function.h
|
||||
$(CC) $(CFLAGS) -c function.c
|
||||
|
||||
display.o: display.c display.h
|
||||
display.o: display.c display.h function.h
|
||||
$(CC) $(CFLAGS) -c display.c
|
||||
|
||||
read.o: read.c read.h function.h
|
||||
|
||||
BIN
box_on_target.png
LFS
Normal file
BIN
box_on_target.png
LFS
Normal file
Binary file not shown.
104
display.c
104
display.c
@@ -1,4 +1,11 @@
|
||||
#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>
|
||||
|
||||
void
|
||||
@@ -15,3 +22,100 @@ screenDisplay (unsigned short int **tab, int size)
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
#ifndef DISPLAY_H
|
||||
#define DISPLAY_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "function.h"
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "function.h"
|
||||
#include "display.h"
|
||||
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -181,7 +183,7 @@ move (unsigned short int **tab, vect *playerPos, vect direction)
|
||||
|
||||
void
|
||||
inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
|
||||
int nbr_targets)
|
||||
int nbr_targets, dis *display_user)
|
||||
{
|
||||
vect direction = { 0, 0 };
|
||||
char input;
|
||||
@@ -236,6 +238,7 @@ inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
|
||||
finish = true;
|
||||
}
|
||||
screenDisplay (tab2d, N);
|
||||
screenDisplaySDL(tab2d, display_user);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
16
function.h
16
function.h
@@ -1,9 +1,12 @@
|
||||
#ifndef FONCTION_H
|
||||
#define FONCTION_H
|
||||
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#define EMPTY 0
|
||||
#define WALL 1
|
||||
@@ -27,14 +30,23 @@ typedef struct Score
|
||||
unsigned int move_box;
|
||||
} 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);
|
||||
void free2D (unsigned short int **tab, int N);
|
||||
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 islose (unsigned short int **tab2d, const int N);
|
||||
bool blockBox (unsigned short int **tab2d, vect box_coor);
|
||||
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
|
||||
|
||||
31
main.c
31
main.c
@@ -1,29 +1,50 @@
|
||||
#include "display.h"
|
||||
#include "function.h"
|
||||
#include "read.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
#define SIZE_PLAY 24
|
||||
|
||||
#define SIZE_PLAY 30
|
||||
|
||||
int
|
||||
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 *targets;
|
||||
int nbr_targets;
|
||||
score playerScore;
|
||||
playerScore.before = time (NULL);
|
||||
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);
|
||||
inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets);
|
||||
inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets, &display_user);
|
||||
|
||||
playerScore.after = time (NULL);
|
||||
printf ("%ld\n", playerScore.after - playerScore.before);
|
||||
|
||||
SDL_DestroyWindow(display_user.window);
|
||||
SDL_DestroyRenderer(display_user.renderer);
|
||||
free2D (tab2d, SIZE_PLAY);
|
||||
free (playerPos);
|
||||
free (targets);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
player.png
LFS
Normal file
BIN
player.png
LFS
Normal file
Binary file not shown.
BIN
player_on_target.png
LFS
Normal file
BIN
player_on_target.png
LFS
Normal file
Binary file not shown.
BIN
target.png
LFS
Normal file
BIN
target.png
LFS
Normal file
Binary file not shown.
Reference in New Issue
Block a user