1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include <iostream>
#include <SFML/Graphics.hpp>
#include "Mario.hpp"
#include <cstdlib>
#define LARGEUR_TILE 60
#define HAUTEUR_TILE 60
#define NOMBRE_TILES_HAUTEUR 14
#define NOMBRE_TILES_LARGEUR 25
using namespace std;
using namespace sf;
const Vector2f WINDOW_SIZE(800, 600);
char* table [] ={
"00000000000000000000000000",
"00000000000000000000000000",
"00000000000000000000000000",
"00000000000000000000000000",
"10000000011111000000000000",
"00000000000000000000000000",
"00000000000000000000000000",
"00000000000000000000000000",
"00000002222002222000000000",
"00340000000000000000000000",
"00560000000000000000077777",
"00560000000000000000777777",
"77777777777777777007777777",
"77777777777777777007777777"};
void afficher(RenderWindow &App, Image &tileset, char** table)
{
App.Clear();
Sprite tileSource;
tileSource.SetImage(tileset);
for (int i = 0; i < NOMBRE_TILES_LARGEUR; i++)
{
for (int j = 0; j < NOMBRE_TILES_HAUTEUR; j++)
{
tileSource.SetSubRect(IntRect((table[j][i] - '0') * LARGEUR_TILE, 0, (table[j][i] - '0')*LARGEUR_TILE+LARGEUR_TILE, HAUTEUR_TILE));
tileSource.SetPosition(i * LARGEUR_TILE, j * HAUTEUR_TILE);
App.Draw(tileSource);
}
}
}
int main(int argc, char *argv[])
{
RenderWindow App(VideoMode(WINDOW_SIZE.x,WINDOW_SIZE.y, 32), "TileMapping");
App.SetFramerateLimit(60);
Image tileset;
if (!tileset.LoadFromFile("tilesetMario.bmp"))
{
cerr <<"Erreur lors du chargement de tilesetMario.bmp" << endl;
}
Mario supMario (200, 640, 9) ;
Vector2f demiTaille(WINDOW_SIZE.x /2, WINDOW_SIZE.y /2);
Vector2f centre (supMario.GetX(), supMario.GetY());
View vue (centre, demiTaille);
App.SetView(vue);
while (App.IsOpened())
{
Event event;
while (App.GetEvent(event))
{
if((event.Type == Event::KeyPressed && event.Key.Code == Key::Escape)
|| event.Type == Event::Closed)
{
App.Close();
}
}
vue.SetCenter(supMario.GetX(), supMario.GetY()); // La vue est centrée sur mario, mais ça ne fonctionne pas. Problème de récupération de la position de mario?
const Input& input = App.GetInput();
if (input.IsKeyDown(Key::Left))
{
supMario.Deplacement(Mario::GAUCHE);
}
if (input.IsKeyDown(Key::Right))
{
supMario.Deplacement(Mario::DROITE);
}
App.Clear();
tileset.SetSmooth(false);
afficher(App, tileset, table);
App.Draw(supMario.GetSprite());
App.Display();
}
return EXIT_SUCCESS;
} |