I created a game using raylib and C++ but I am getting the pointer issue when I load the texture
This is the code below:
#ifndef USER_HPP
#define USER_HPP
#include "raylib.h"
#include "raymath.h"
#include <iostream>
#include <vector>
#define rotation_speed 360
using namespace std;
class User{
public:
Vector2 position;
int velocity;
Vector2 direction;
Color color;
int life;
float hitTimer;
Image image;
Texture2D timage;
User(){
position = {0,0};
velocity = 0;
direction = Vector2Zero();
color = BLUE;
life = 3;
hitTimer = 0.0f;
timage = LoadTexture("Imagesource/Normal.png");
}
User(Vector2 set_position, int set_velocity, Vector2 set_direction){
position = set_position;
velocity = set_velocity;
direction = set_direction;
color = BLUE;
life = 3;
hitTimer = 0.0f;
timage = LoadTexture("Imagesource/Normal.png");
}
~User(){
UnloadTexture(timage);
}
void UserDraw(){
DrawTexture(timage, position.x, position.y, WHITE);
}
void setDirection(Vector2 newdirection){
direction = newdirection;
}
void UserUpdate(){
direction = Vector2Zero();
if(IsKeyDown(KEY_UP)){
direction.y -= 1;
}
if(IsKeyDown(KEY_DOWN)){
direction.y += 1;
}
if(IsKeyDown(KEY_RIGHT)){
direction.x+= 1;
}
if(IsKeyDown(KEY_LEFT)){
direction.x-= 1;
}
Vector2 newPosition = Vector2Add(position, Vector2Scale(direction, velocity/2));
if (newPosition.x >= 0 && newPosition.x <= 800 && newPosition.y >= 0 && newPosition.y <= 450) {
position = newPosition;
}
}
};
#endif
int main(void)
{
SetTargetFPS(60);
InitWindow(screenWidth, screenHeight, "Major Project");
ArrayList *arr_obstacle = new ArrayList();
ItemStack *usingitem = new ItemStack();
ItemStack *creatingitem = new ItemStack();
User main_user = User(screenCenter, 2, {0, 0});
main_user.timage.width = 15;
main_user.timage.height = 15;
while (!WindowShouldClose()) // Detect window close button or ESC key
{
UpdateDrawFrame(arr_obstacle, main_user, usingitem, creatingitem);
}
CloseWindow();
return 0;
}
In the UpdateDrawFrame function, it literally update the frame for obstacle, item, and user.
I tried loading the texture2d after InitWindow and setting that texture2d into timage of user but it also did not work.
Yehwan Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1