Advertisement
GreMendes

Replay - zoado pqsim

Oct 22nd, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <c2d2\chien2d2.h>
  3. #include <c2d2\chien2d2primitivas.h>
  4. #include <c2d2\chien2d2mapa.h>
  5. #include <iostream>
  6.  
  7. #pragma comment(lib, "SDL.lib")
  8. #pragma comment(lib, "SDLMain.lib")
  9. #pragma comment(lib, "SDL_image.lib")
  10. #pragma comment(lib, "SDL_mixer.lib")
  11. #pragma comment(lib, "opengl32.lib")
  12. #pragma comment(lib, "glu32.lib")
  13. #pragma comment(lib, "c2d2.lib")
  14. #pragma comment(lib, "ca2.lib")
  15.  
  16. #define ALTURA  600
  17. #define LARGURA 800
  18.  
  19. // Defines para o tamanho da janela do mapa
  20. #define MAPA_X  100
  21. #define MAPA_Y  100
  22. #define JANELA_MAPA_X   600
  23. #define JANELA_MAPA_Y   400
  24.  
  25. extern SDL_Surface *screen;
  26.  
  27. int larguraMapa, alturaMapa;
  28. // As coordenadas do mapa
  29. int xmapa = 0, ymapa = 0;
  30.  
  31. bool quit;
  32. int heroi;
  33. int xHeroi;
  34. int yHeroi;
  35. int quadroHeroi;
  36. int indHeroi;
  37. int tempo;
  38. int fundo;
  39. int arial32;
  40.  
  41. FILE* fp;
  42.  
  43. int animacaoDireita[] = { 0, 1, 2, 1 };
  44. int animacaoEsquerda[] = { 3, 4, 5, 4 };
  45. int animacaoCima[] = { 6, 7, 8, 6 };
  46. int animacaoBaixo[] = { 9, 10, 11, 9 };
  47. int animacaoMorre[] = { 12 };
  48.  
  49. // Vetor da visibilidade das camadas
  50. bool camadas[4] = { true, true, true, true };
  51.  
  52. int replay;
  53.  
  54.  
  55.  
  56.  
  57. bool inicializar()
  58. {
  59.    
  60.     std::cout << "Para gravar seu jogo digite 0, para Ver seu ultimo jogo digite 1:";
  61.     std::cin >> replay;
  62.     if (replay == 0){
  63.         fp = fopen("./replay.txt", "wb");
  64.     }
  65.     else{
  66.         fp = fopen("./replay.txt", "rb");
  67.     }
  68.    
  69.     if (!C2D2_Inicia(LARGURA, ALTURA, C2D2_JANELA, C2D2_DESENHO_OPENGL, "Game"))
  70.     {
  71.         return false;
  72.     }
  73.     fundo = C2D2_CarregaSpriteSet("mundo/imagens/mapa.png", 0, 0);
  74.     heroi = C2D2_CarregaSpriteSet("mundo/imagens/darkphoenix.png", 32, 42);
  75.     arial32 = C2D2_CarregaFonte("mundo/imagens/arial32.png", 32);
  76.    
  77.  
  78.     if (fundo == 0 || heroi == 0 || arial32 == 0)
  79.     {
  80.         printf("Falhou ao carregar alguma coisa. Encerrando.\n");
  81.         // Encerra a Chien2d2
  82.         C2D2_Encerra();
  83.         return 0;
  84.     }
  85.  
  86.     xHeroi = 400;
  87.     yHeroi = 300;
  88.     quadroHeroi = 0;
  89.     tempo = 0;
  90.     indHeroi = 0;
  91.    
  92.     return true;
  93. }
  94.  
  95. void atualizar(){
  96.     C2D2_Botao* teclas = C2D2_PegaTeclas();
  97.    
  98.  
  99.     if (teclas[C2D2_ESC].pressionado ||
  100.         teclas[C2D2_ENCERRA].pressionado)
  101.     {
  102.         quit = true;
  103.     }
  104.  
  105.     if (teclas[C2D2_DIREITA].ativo)
  106.     {
  107.         fprintf(fp, "1");
  108.         tempo++;
  109.         if (tempo > 6)
  110.         {
  111.            
  112.             tempo = 0;
  113.             indHeroi = (indHeroi + 1) % 4;
  114.            
  115.         }
  116.  
  117.         //5 pixels a cada quadro.
  118.         //60 quadros por segundo
  119.         //velocidade = 5 * 60 = 300 pixels por segundo
  120.         xHeroi += 5;
  121.         quadroHeroi = animacaoDireita[indHeroi];
  122.     }
  123.     else if (teclas[C2D2_ESQUERDA].ativo)
  124.     {
  125.         fprintf(fp, "2");
  126.         tempo++;
  127.         if (tempo > 6)
  128.         {
  129.            
  130.             tempo = 0;
  131.             indHeroi = (indHeroi + 1) % 4;
  132.            
  133.         }
  134.  
  135.         xHeroi -= 5;
  136.         quadroHeroi = animacaoEsquerda[indHeroi];
  137.     }
  138.  
  139.    
  140.     if (teclas[C2D2_CIMA].ativo)
  141.     {
  142.         fprintf(fp, "3");
  143.         tempo++;
  144.         if (tempo > 6)
  145.         {
  146.            
  147.             tempo = 0;
  148.             indHeroi = (indHeroi + 1) % 4;
  149.            
  150.         }
  151.  
  152.         yHeroi -= 5;
  153.         quadroHeroi = animacaoCima[indHeroi];
  154.     }
  155.  
  156.     if (teclas[C2D2_BAIXO].ativo)
  157.     {
  158.         fprintf(fp, "4");
  159.         tempo++;
  160.         if (tempo > 6)
  161.         {
  162.            
  163.             tempo = 0;
  164.             indHeroi = (indHeroi + 1) % 4;
  165.            
  166.         }
  167.  
  168.         yHeroi += 5;
  169.         quadroHeroi = animacaoBaixo[indHeroi];
  170.     }
  171.     else if (teclas[C2D2_ESPACO].ativo)
  172.     {
  173.         fprintf(fp, "5");
  174.         tempo++;
  175.         if (tempo > 6)
  176.         {
  177.            
  178.             tempo = 0;
  179.             indHeroi = (indHeroi + 1) % 4;
  180.            
  181.         }
  182.  
  183.  
  184.         quadroHeroi = animacaoMorre[indHeroi];
  185.     }
  186.     else{
  187.         fprintf(fp, "0");
  188.     }
  189. }
  190.  
  191.  
  192. void desenhar()
  193. {
  194.     C2D2_LimpaTela();
  195.     C2D2_DesenhaSprite(fundo, 0, 0, 0);
  196.     C2D2_DesenhaSprite(heroi, quadroHeroi, xHeroi, yHeroi);
  197.    
  198.  
  199. }
  200.  
  201. void finalizar()
  202. {
  203.         // Apaga a imagem carregada da memória
  204. //  C2D2M_RemoveMapa(fundo);
  205. //  C2D2_RemoveSpriteSet(heroi);
  206.     // encerra a biblioteca de mapas
  207. //  C2D2M_Encerra();
  208.     // Encerra a Chien2D 2
  209.     C2D2_Encerra();
  210. }
  211.  
  212.  
  213.  
  214. int main(int argc, char* argv[])
  215. {
  216.    
  217.     quit = false;
  218.     if (!inicializar())
  219.     {
  220.         return 1;
  221.     }
  222.  
  223.     do
  224.     {
  225.         C2D2_Sincroniza(C2D2_FPS_PADRAO);
  226.         if (replay == 0){
  227.  
  228.             atualizar();
  229.             desenhar();
  230.         }
  231.         else if (replay == 1){
  232.             int c;
  233.  
  234.  
  235.             if (fp == NULL) {
  236.                 std::cout << "Arquivo vazio ou inexistente";
  237.                 exit(1);
  238.             }
  239.  
  240.             do{
  241.                 c = fgetc(fp);
  242.  
  243.                 desenhar();
  244.                 C2D2_DesenhaTexto(arial32, 200, 50, "REPLAY!!!", C2D2_TEXTO_CENTRALIZADO);
  245.  
  246.  
  247.                 if (c == '0')
  248.                 {
  249.  
  250.  
  251.                     tempo++;
  252.                     if (tempo > 6)
  253.                     {
  254.  
  255.                         tempo = 0;
  256.                         indHeroi = (indHeroi + 1) % 4;
  257.  
  258.                     }
  259.  
  260.                     break;
  261.                 }
  262.  
  263.                 else if (c == '1')
  264.                 {
  265.  
  266.                     tempo++;
  267.                     if (tempo > 6)
  268.                     {
  269.                         tempo = 0;
  270.  
  271.                         indHeroi = (indHeroi + 1) % 4;
  272.  
  273.                     }
  274.  
  275.                     xHeroi += 5;
  276.                     quadroHeroi = animacaoDireita[indHeroi];
  277.                     break;
  278.                 }
  279.                 else  if (c == '2')
  280.                 {
  281.  
  282.                     tempo++;
  283.                     if (tempo > 6)
  284.                     {
  285.  
  286.                         tempo = 0;
  287.                         indHeroi = (indHeroi + 1) % 4;
  288.  
  289.                     }
  290.  
  291.                     xHeroi -= 5;
  292.                     quadroHeroi = animacaoEsquerda[indHeroi];
  293.                     break;
  294.                 }
  295.  
  296.  
  297.                 else if (c == '3')
  298.                 {
  299.  
  300.                     tempo++;
  301.                     if (tempo > 6)
  302.                     {
  303.  
  304.                         tempo = 0;
  305.                         indHeroi = (indHeroi + 1) % 4;
  306.  
  307.                     }
  308.  
  309.                     yHeroi -= 5;
  310.                     quadroHeroi = animacaoCima[indHeroi];
  311.                     break;
  312.                 }
  313.  
  314.                 else if (c == '4')
  315.                 {
  316.  
  317.                     tempo++;
  318.                     if (tempo > 6)
  319.                     {
  320.  
  321.                         tempo = 0;
  322.                         indHeroi = (indHeroi + 1) % 4;
  323.  
  324.                     }
  325.  
  326.                     yHeroi += 5;
  327.                     quadroHeroi = animacaoBaixo[indHeroi];
  328.                     break;
  329.                 }
  330.                 else if (c == '5')
  331.                 {
  332.  
  333.                     tempo++;
  334.                     if (tempo > 6)
  335.                     {
  336.  
  337.                         tempo = 0;
  338.                         indHeroi = (indHeroi + 1) % 4;
  339.  
  340.                     }
  341.  
  342.  
  343.                     quadroHeroi = animacaoMorre[indHeroi];
  344.                     break;
  345.                 }
  346.  
  347.  
  348.             } while (c != EOF);
  349.        
  350.            
  351.         }
  352.    
  353.                
  354.        
  355.        
  356.     } while (!quit);
  357.  
  358.     finalizar();
  359.     fclose(fp);
  360.     return 0;
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement