Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <c2d2\chien2d2.h>
- #include <c2d2\chien2d2primitivas.h>
- #include <c2d2\chien2d2mapa.h>
- #include <iostream>
- #pragma comment(lib, "SDL.lib")
- #pragma comment(lib, "SDLMain.lib")
- #pragma comment(lib, "SDL_image.lib")
- #pragma comment(lib, "SDL_mixer.lib")
- #pragma comment(lib, "opengl32.lib")
- #pragma comment(lib, "glu32.lib")
- #pragma comment(lib, "c2d2.lib")
- #pragma comment(lib, "ca2.lib")
- #define ALTURA 600
- #define LARGURA 800
- // Defines para o tamanho da janela do mapa
- #define MAPA_X 100
- #define MAPA_Y 100
- #define JANELA_MAPA_X 600
- #define JANELA_MAPA_Y 400
- extern SDL_Surface *screen;
- int larguraMapa, alturaMapa;
- // As coordenadas do mapa
- int xmapa = 0, ymapa = 0;
- bool quit;
- int heroi;
- int xHeroi;
- int yHeroi;
- int quadroHeroi;
- int indHeroi;
- int tempo;
- int fundo;
- int arial32;
- FILE* fp;
- int animacaoDireita[] = { 0, 1, 2, 1 };
- int animacaoEsquerda[] = { 3, 4, 5, 4 };
- int animacaoCima[] = { 6, 7, 8, 6 };
- int animacaoBaixo[] = { 9, 10, 11, 9 };
- int animacaoMorre[] = { 12 };
- // Vetor da visibilidade das camadas
- bool camadas[4] = { true, true, true, true };
- int replay;
- bool inicializar()
- {
- std::cout << "Para gravar seu jogo digite 0, para Ver seu ultimo jogo digite 1:";
- std::cin >> replay;
- if (replay == 0){
- fp = fopen("./replay.txt", "wb");
- }
- else{
- fp = fopen("./replay.txt", "rb");
- }
- if (!C2D2_Inicia(LARGURA, ALTURA, C2D2_JANELA, C2D2_DESENHO_OPENGL, "Game"))
- {
- return false;
- }
- fundo = C2D2_CarregaSpriteSet("mundo/imagens/mapa.png", 0, 0);
- heroi = C2D2_CarregaSpriteSet("mundo/imagens/darkphoenix.png", 32, 42);
- arial32 = C2D2_CarregaFonte("mundo/imagens/arial32.png", 32);
- if (fundo == 0 || heroi == 0 || arial32 == 0)
- {
- printf("Falhou ao carregar alguma coisa. Encerrando.\n");
- // Encerra a Chien2d2
- C2D2_Encerra();
- return 0;
- }
- xHeroi = 400;
- yHeroi = 300;
- quadroHeroi = 0;
- tempo = 0;
- indHeroi = 0;
- return true;
- }
- void atualizar(){
- C2D2_Botao* teclas = C2D2_PegaTeclas();
- if (teclas[C2D2_ESC].pressionado ||
- teclas[C2D2_ENCERRA].pressionado)
- {
- quit = true;
- }
- if (teclas[C2D2_DIREITA].ativo)
- {
- fprintf(fp, "1");
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- //5 pixels a cada quadro.
- //60 quadros por segundo
- //velocidade = 5 * 60 = 300 pixels por segundo
- xHeroi += 5;
- quadroHeroi = animacaoDireita[indHeroi];
- }
- else if (teclas[C2D2_ESQUERDA].ativo)
- {
- fprintf(fp, "2");
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- xHeroi -= 5;
- quadroHeroi = animacaoEsquerda[indHeroi];
- }
- if (teclas[C2D2_CIMA].ativo)
- {
- fprintf(fp, "3");
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- yHeroi -= 5;
- quadroHeroi = animacaoCima[indHeroi];
- }
- if (teclas[C2D2_BAIXO].ativo)
- {
- fprintf(fp, "4");
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- yHeroi += 5;
- quadroHeroi = animacaoBaixo[indHeroi];
- }
- else if (teclas[C2D2_ESPACO].ativo)
- {
- fprintf(fp, "5");
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- quadroHeroi = animacaoMorre[indHeroi];
- }
- else{
- fprintf(fp, "0");
- }
- }
- void desenhar()
- {
- C2D2_LimpaTela();
- C2D2_DesenhaSprite(fundo, 0, 0, 0);
- C2D2_DesenhaSprite(heroi, quadroHeroi, xHeroi, yHeroi);
- }
- void finalizar()
- {
- // Apaga a imagem carregada da memória
- // C2D2M_RemoveMapa(fundo);
- // C2D2_RemoveSpriteSet(heroi);
- // encerra a biblioteca de mapas
- // C2D2M_Encerra();
- // Encerra a Chien2D 2
- C2D2_Encerra();
- }
- int main(int argc, char* argv[])
- {
- quit = false;
- if (!inicializar())
- {
- return 1;
- }
- do
- {
- C2D2_Sincroniza(C2D2_FPS_PADRAO);
- if (replay == 0){
- atualizar();
- desenhar();
- }
- else if (replay == 1){
- int c;
- if (fp == NULL) {
- std::cout << "Arquivo vazio ou inexistente";
- exit(1);
- }
- do{
- c = fgetc(fp);
- desenhar();
- C2D2_DesenhaTexto(arial32, 200, 50, "REPLAY!!!", C2D2_TEXTO_CENTRALIZADO);
- if (c == '0')
- {
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- break;
- }
- else if (c == '1')
- {
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- xHeroi += 5;
- quadroHeroi = animacaoDireita[indHeroi];
- break;
- }
- else if (c == '2')
- {
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- xHeroi -= 5;
- quadroHeroi = animacaoEsquerda[indHeroi];
- break;
- }
- else if (c == '3')
- {
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- yHeroi -= 5;
- quadroHeroi = animacaoCima[indHeroi];
- break;
- }
- else if (c == '4')
- {
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- yHeroi += 5;
- quadroHeroi = animacaoBaixo[indHeroi];
- break;
- }
- else if (c == '5')
- {
- tempo++;
- if (tempo > 6)
- {
- tempo = 0;
- indHeroi = (indHeroi + 1) % 4;
- }
- quadroHeroi = animacaoMorre[indHeroi];
- break;
- }
- } while (c != EOF);
- }
- } while (!quit);
- finalizar();
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement