Carte/src/main.c

123 lines
6.0 KiB
C
Raw Permalink Normal View History

2020-04-18 23:33:56 +02:00
#include <gint/display.h>
#include <gint/keyboard.h>
2020-04-21 14:01:45 +02:00
typedef struct entity entity;
2020-04-18 23:33:56 +02:00
int main(void)
{
2020-04-21 14:01:45 +02:00
//Chargement des images
2020-04-19 13:43:36 +02:00
extern image_t img_grass_1;
extern image_t img_grass_2;
extern image_t img_grass_3;
extern image_t img_player;
2020-04-18 23:33:56 +02:00
2020-04-21 14:01:45 +02:00
//Création des structures
struct map {
int width;
int height;
uint16_t palette;
uint8_t data;
};
struct entity {
int x;
int y;
image_t sprite;
};
//Initialisation de la map "world"
static const uint8_t world_data[1000] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1};
static const uint16_t world_palette[3] = {0, 1, 2};
//Création de la map "world"
struct map world = {50, 20, world_palette, world_data};
//Création du l'entité "joueur"
entity player = {0, 0, img_player};
2020-04-19 13:43:36 +02:00
//Initialisation de la camera
int xCamera, yCamera;
int xCameraOffset = 12;
int yCameraOffset = 7;
2020-04-21 14:01:45 +02:00
bool xCameraMax = false;
2020-04-18 23:33:56 +02:00
2020-04-19 13:43:36 +02:00
//Boucle de jeu
2020-04-21 14:01:45 +02:00
while(1) {
2020-04-19 13:43:36 +02:00
//Positionement de la caméra par rapport au joueur
2020-04-21 14:01:45 +02:00
if (player.x-xCameraOffset < 0) {
2020-04-19 13:43:36 +02:00
xCamera = 0;
2020-04-21 14:01:45 +02:00
xCameraMax = false;
} else if (player.x+xCameraOffset > world.width-1) {
xCamera = world.width-1-xCameraOffset*2;
xCameraMax = true;
}else {
xCamera = player.x-xCameraOffset;
xCameraMax = false;
2020-04-19 13:43:36 +02:00
}
2020-04-21 14:01:45 +02:00
if (player.y-yCameraOffset < 0) {
2020-04-19 13:43:36 +02:00
yCamera = 0;
2020-04-21 14:01:45 +02:00
} else if (player.y+yCameraOffset > world.height) {
yCamera = world.height-yCameraOffset*2;
}else {
yCamera = player.y-yCameraOffset;
2020-04-19 13:43:36 +02:00
}
//Chargement de la partie de la map à afficher
dclear(C_WHITE);
int i, j;
2020-04-21 14:01:45 +02:00
for(j=0;j<14;j++) {
for(i=0; i<25; i++) {
switch(map[j+yCamera][i+xCamera]) {
2020-04-19 13:43:36 +02:00
case 0:
dimage(i*16-(4*xCameraMax), j*16, &img_grass_1);
2020-04-19 13:43:36 +02:00
break;
case 1:
dimage(i*16-(4*xCameraMax), j*16, &img_grass_2);
2020-04-19 13:43:36 +02:00
break;
case 2:
dimage(i*16-(4*xCameraMax), j*16, &img_grass_3);
2020-04-19 13:43:36 +02:00
break;
}
}
}
//Affichage de l'écran
2020-04-21 14:01:45 +02:00
dimage((player.x-xCamera)*16-(4*xCameraMax), (player.y-yCamera)*16, &img_player);
2020-04-18 23:33:56 +02:00
dupdate();
2020-04-19 13:43:36 +02:00
//Gestion du clavier
2020-04-21 14:01:45 +02:00
switch(getkey().key) {
2020-04-18 23:33:56 +02:00
case KEY_UP:
2020-04-21 14:01:45 +02:00
player.y--;
if (player.y < 0) {
player.y = 0;
}
2020-04-18 23:33:56 +02:00
break;
case KEY_DOWN:
2020-04-21 14:01:45 +02:00
player.y++;
if (player.y > world.height-1) {
player.y = world.height-1;
}
2020-04-18 23:33:56 +02:00
break;
case KEY_LEFT:
2020-04-21 14:01:45 +02:00
player.x--;
if (player.x < 0) {
player.x = 0;
}
2020-04-18 23:33:56 +02:00
break;
case KEY_RIGHT:
2020-04-21 14:01:45 +02:00
player.x++;
if (player.x > world.width-1) {
player.x = world.width-1;
}
break;
2020-04-19 13:43:36 +02:00
case KEY_EXE:
2020-04-18 23:33:56 +02:00
return 0;
}
}
return 0;
}