Code works fine but it doesn't display the window

Greetings everyone! Im having one issue here. This is the code:

#include “raylib.h”
#include
#include <stdio.h>
#include

int main(){

const int WindowWidth = 512;
const int WindowHeight = 380;
//const int RectangleWidth = 50;
//const int RectangleHeight = 80;
const int gravity = 1;                      //acceleration due to gravity (pixels/frame)/frame 
const int JumpVelocity = -15;

//int posY=WindowHeight - RectangleHeight;   //Y postion of the rectangle;
int velocity = 0;
bool IsInAir = false;                       //bool to detect if the rectangle is in the air

Texture2D scarfy = LoadTexture("textures/scarfy.png");    //We call the picture that we want to display
Rectangle scarfyRec;                                      //Rectagle of the picture
scarfyRec.width = scarfy.width/6;
scarfyRec.height = scarfy.height;
scarfyRec.x = 0;
scarfyRec.y = 0;
Vector2 scarfyPos;                                      //Position of the rectangle's picture   
scarfyPos.x=WindowWidth/2-scarfyRec.width/2;            //X Center in the screen - center of the rectangle picture in X  
scarfyPos.y=WindowHeight - scarfyRec.height;            //Y Center in the screen - center of the rectangle picture in Y


InitWindow(WindowWidth, WindowHeight, "NO WUON!");
SetTargetFPS(60);
while (!WindowShouldClose()){
    
    BeginDrawing();
    ClearBackground(WHITE);

    if (scarfyPos.y >= WindowHeight - scarfyRec.height){ //Ground check prevents that rectangle falls from the bottom
        velocity =0;
        IsInAir = false;
    
    }else{  //rectangle is in the air
        velocity += gravity;    //Apply gravity    
        IsInAir = true;
    }

    if (IsKeyPressed(KEY_SPACE) && !IsInAir){   //If we press the space key then we jump
        velocity += JumpVelocity;               //velocity of the jump
    }

    scarfyPos.y += velocity;       //update the rectangle position
    //DrawRectangle(WindowWidth/2, posY, RectangleWidth,RectangleHeight,GREEN);
    DrawTextureRec(scarfy, scarfyRec, scarfyPos, WHITE);
    EndDrawing();
} 
UnloadTexture(scarfy);
CloseWindow();

}

When I ran in the terminal with the powershell typing the name of the code, it loads the picture and the code but i can’t see the window with the picture of the code. What could it be?

image

Apparently is due to the “segmentation fault”

Ok now I made to display the window but I still see no picture. I change the path where I’m working on the folder but it stills not showing me the picture.

Ok problem solved. Apparently, “InitWindow()” function has to be at the beggining of the code before calling the “Texture 2D” command. :smiley:

1 Like

Good job detailing the steps you took and finding out the solution.

To explain a bit why you need to call InitWindow() before calling LoadTexture():

One of the things that InitWindow() does is prepare the GPU (aka, Video Card or Graphics Card) to take in data for your program. Once that preparation is done, you can safely load data into the memory of the GPU via LoadTexture() and related functions.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms