4

I'm having a problem with drawing my game map in Raylib.

As far as I'm seeing the site's examples, everything is correct... However, when I run it, it doesn't show anything about the game, just the screen in the color I put (RAYWHITE).

I'm trying to initialize the map first, and then I'm going to move characters within it.

The game will have more maps and they are being read from .txt files W K D P J M are the important characters of the map, which are in the files. They will tell each character's starting position and map wall

Can anyone help me with this problem?

Here's the code:

#include "raylib.h"
#include <stdio.h>

#define LARGURA 1200
#define ALTURA 500
#define LINE 100
#define PAREDE 'W'
#define PAREDEDESTRUTIVEL 'D'
#define POCAO 'P'
#define POSINICIALMAGO 'J'
#define POSISINICIALMONSTRO 'M'
#define POSINICIALCRIATURA 'K'
#define TAMANHO 40

int main(void){
    FILE *file;
    char path[] = "Mapa/mapa1.txt";
    int x,y, posInicialMago[2], posMago[2], posInicialCriatura[2], posCriatura[2], posInicialMonstro[2], posMonstro[2];
    char caracter;

    file = fopen(path,"r");

    if(file == NULL)
        printf("Erro na abertura do arquivo! \n");
    else
    {
        InitWindow(LARGURA, ALTURA, "Jogo");
        SetTargetFPS(60);

        x = y = 0;

        Texture2D mage = LoadTexture("./Texturas/mage (1).png");
        Texture2D fairy = LoadTexture("./Texturas/fairy (1).png");
        Texture2D potion = LoadTexture("./Texturas/potion (1).png");
        Texture2D monster = LoadTexture("./Texturas/monster (1).png");
        Texture2D wall = LoadTexture("./Texturas/wall (1).png");
        Texture2D indestructibleWall = LoadTexture("./Texturas/wall (1).png");

        while (!WindowShouldClose())
        {
            BeginDrawing();

                ClearBackground(RAYWHITE);

                do
                {
                    caracter = fgetc(file);
                    switch(caracter)
                    {
                        case PAREDE:
                            DrawTexture(indestructibleWall, x,y, BROWN);
                            break;
                        case PAREDEDESTRUTIVEL:
                            DrawTexture(wall, x,y, BROWN);
                            break;
                        case POCAO:
                            DrawTexture(potion, x,y, BLUE);
                            break;
                        case POSINICIALMAGO:
                            DrawTexture(mage, x,y, RAYWHITE);
                            posInicialMago[0] = posMago[0] = x;
                            posInicialMago[1] = posMago[1] = y;
                            break;
                        case POSINICIALCRIATURA:
                            DrawTexture(fairy, x,y, PURPLE);
                            posInicialCriatura[0] = x;
                            posInicialCriatura[1] = y;
                            break;

                        case POSISINICIALMONSTRO:
                            DrawTexture(monster, x,y, BLACK);
                            posInicialMonstro[0] = x;
                            posInicialMonstro[1] = y;
                            break;
                        case '\n':
                            break;
                        default:
                             DrawRectangle(LARGURA, ALTURA, x, y, DARKGRAY);
                            break;
                    }

                    x += TAMANHO;

                    if(caracter == '\n')
                    {
                        y += TAMANHO;
                        x = 0;
                    }

                }while(caracter != EOF);

            EndDrawing();

            if (IsKeyPressed(KEY_RIGHT)) posMago[0] += TAMANHO;
            if (IsKeyPressed(KEY_LEFT)) posMago[0] -= TAMANHO;
            if (IsKeyPressed(KEY_UP)) posMago[1] -= TAMANHO;
            if (IsKeyPressed(KEY_DOWN)) posMago[1] += TAMANHO;
        }
    }

    fclose(file);
    CloseWindow();
    return 0;
}
Mateus
  • 64
  • 4
  • Good description of what you are doing, and to some extent of what is not working right, but have you done any of the standard debugging things that would be expected? i.e. run in debug mode, use break points in places of interest to step through variable to see if they contain expected values?. If necessary (eg if you do not have a debug environment.) insert some `printf()` statements to show values of variables of interest. It would be very interesting to step through starting at this point: `caracter = fgetc(file);`, i.e. look at the value returned, and follow it through the `switch()` – ryyker Aug 08 '22 at 18:26
  • yea! I tried to print all the characters on the screen, and it drew the whole map. So, the while loop with the char variable is working. – Mateus Aug 08 '22 at 19:34
  • I don't know the library you are using, but graphics libraries usually have a separate "drawing" and "rendering" calls, are you sure that you don't need to call some sort of render function? If so, have you tried to remove the `ClearBackground` call and see if anything is being drawn? Perhaps you are drawing and then immediately clearing the screen. – aulven Aug 08 '22 at 21:25
  • Also you probably should check if `LoadTexture` succeeds. The API must have provided some sort of error checking whether the given file is successfully read and converted. – aulven Aug 08 '22 at 21:30
  • if I remove ClearBackground it draws, but it doesn't stop blinking. As he draws (but blinking), I imagine the textures are working. – Mateus Aug 09 '22 at 02:39

0 Answers0