read file
This commit is contained in:
7
Makefile
7
Makefile
@@ -1,7 +1,7 @@
|
|||||||
# Variable
|
# Variable
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2)
|
CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2)
|
||||||
OBJ = main.o function.o display.o
|
OBJ = main.o function.o display.o read.o
|
||||||
TARGET = sokoban
|
TARGET = sokoban
|
||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
@@ -9,7 +9,7 @@ all: $(TARGET)
|
|||||||
$(TARGET): $(OBJ)
|
$(TARGET): $(OBJ)
|
||||||
$(CC) $(CFLAGS) $(OBJ) -o $(TARGET)
|
$(CC) $(CFLAGS) $(OBJ) -o $(TARGET)
|
||||||
|
|
||||||
main.o : main.c function.h
|
main.o : main.c function.h display.h read.h
|
||||||
$(CC) $(CFLAGS) -c main.c
|
$(CC) $(CFLAGS) -c main.c
|
||||||
|
|
||||||
function.o : function.c function.h
|
function.o : function.c function.h
|
||||||
@@ -18,6 +18,9 @@ function.o : function.c function.h
|
|||||||
display.o: display.c display.h
|
display.o: display.c display.h
|
||||||
$(CC) $(CFLAGS) -c display.c
|
$(CC) $(CFLAGS) -c display.c
|
||||||
|
|
||||||
|
read.o: read.c read.h function.h
|
||||||
|
$(CC) $(CFLAGS) -c read.c
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
rm -f $(OBJ) $(TARGET)
|
rm -f $(OBJ) $(TARGET)
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,3 @@ void screenDisplay( unsigned short int **tab,int size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ unsigned short int **creatArea2D(const unsigned int N)
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool fail = false;
|
bool fail = false;
|
||||||
int i;
|
unsigned int i;
|
||||||
for (i = 0; i < N && !fail; ++i)
|
for (i = 0; i < N && !fail; ++i)
|
||||||
{
|
{
|
||||||
tab2d[i] = calloc(N, sizeof(unsigned short int));
|
tab2d[i] = calloc(N, sizeof(unsigned short int));
|
||||||
@@ -27,7 +27,7 @@ unsigned short int **creatArea2D(const unsigned int N)
|
|||||||
|
|
||||||
if (fail)
|
if (fail)
|
||||||
{
|
{
|
||||||
int j;
|
unsigned int j;
|
||||||
for (j = 0; j < i; ++j)
|
for (j = 0; j < i; ++j)
|
||||||
{
|
{
|
||||||
free(tab2d[j]);
|
free(tab2d[j]);
|
||||||
|
|||||||
6
main.c
6
main.c
@@ -1,11 +1,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "function.h"
|
#include "function.h"
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
|
#include "read.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
unsigned short int **tab2d = creatArea2D(5);
|
unsigned short int **tab2d = creatArea2D(32);
|
||||||
screenDisplay(tab2d, 5);
|
fileToTab2D("test.txt", tab2d, 32);
|
||||||
|
screenDisplay(tab2d, 32);
|
||||||
printf("main\n");
|
printf("main\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
40
read.c
Normal file
40
read.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "function.h"
|
||||||
|
|
||||||
|
void fileToTab2D(const char* name_file, unsigned short int **tab, const unsigned N)
|
||||||
|
{
|
||||||
|
FILE *file = fopen(name_file, "r");
|
||||||
|
unsigned int x = 0, y = 0;
|
||||||
|
while(!feof(file))
|
||||||
|
{
|
||||||
|
char current =fgetc(file);
|
||||||
|
switch (current) {
|
||||||
|
case ' ':
|
||||||
|
tab[x][y] = EMPTY;
|
||||||
|
break;
|
||||||
|
case '#':
|
||||||
|
tab[x][y] = WALL;
|
||||||
|
break;
|
||||||
|
case '$':
|
||||||
|
tab[x][y] = BOX;
|
||||||
|
break;
|
||||||
|
case '.':
|
||||||
|
tab[x][y] = TARGET;
|
||||||
|
break;
|
||||||
|
case '@':
|
||||||
|
tab[x][y] = PLAYER;
|
||||||
|
break;
|
||||||
|
case '\n':
|
||||||
|
y = 0;
|
||||||
|
++x;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++y;
|
||||||
|
if (x >= N || y >= N)
|
||||||
|
{
|
||||||
|
perror("Level out of range !");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
read.h
Normal file
5
read.h
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#ifndef READ_H
|
||||||
|
#define READ_H
|
||||||
|
|
||||||
|
void fileToTab2D(const char* name_file, unsigned short int **tab, const unsigned N);
|
||||||
|
#endif // !READ_H
|
||||||
Reference in New Issue
Block a user