0

There's quite a bit of code, so I put it all on my github. (cpp files are on /src, hpp files are on /include)

This is probably really simple, but since I'm new to C++ I'm not realizing how to get there.

So I have a few textures on my project already, a background texture that occupies the whole application (640x480), some floor tiles (64x64) and a player texture, which is where I have my issue.

I wanted to make my player texture something other than 64x64, maybe 86x86 or something similar, but not 64x64.

The problem is that the tutorial basically taught me to hard code the texture size to 64x64, which you can see in this little snippet of Entity.cpp.

Entity::Entity(Vector2f p_pos, SDL_Texture* p_tex)
:pos(p_pos), tex(p_tex)
{
    currentFrame.x = 0;
    currentFrame.y = 0;
    currentFrame.w = 64;
    currentFrame.h = 64;
}

Is there any way I can let this accept different values? The problem really starts to show when I set my player texture to (for example) 86x86 on Photoshop and insert it into the code.

The player is that yellow thing, I added a face so you can understand that it's cut off, because the program is cropping it to a 64x64 square, which I do not want.

screenshot of the problem.

I've tried adding a new size variable to my Entity class (which already had the position variable), but due to me not being very good at the language, I didn't manage to make it work, as it gave me a lot of errors stating that it basically had no idea what that size variable was and its purpose.

Martim
  • 1
  • 1

1 Answers1

0

You are using Hardware Rendering for your application. To do this SDL uses a function called SDL_RenderCopy. One of the arguments for this function is a pointer to an SDL_Rect which defines what "slice" of the image will be rendered on the location specified. Currently, you are using a value of 64 for the width and height of this image. You can change your constructor to accept width and height values and use those for currentFrame.w and currentFrame.h.

Entity::Entity(Vector2f p_pos, SDL_Texture* p_tex, int w, int h)
:pos(p_pos), tex(p_tex)
{
    currentFrame.x = 0;
    currentFrame.y = 0;
    currentFrame.w = w;
    currentFrame.h = h;
}

As you have seen using values smaller than your texture can result in your texture having a cropped appearance. However, this has an advantage too. Use of slices this way allows the programmer to use tile-maps in their applications rather than having to use separate textures for each and every tile they may want to use in their game.

Alternatively, if you aren't using tile-maps and just want the ease of rendering the entire texture without having to figure out its height and width in pixels you can simply pass NULL to SDL_RenderCopy for the srcrect argument.

SDL_RenderCopy(renderer, texture, NULL, &dst);

This fills the area occupied by dst with the texture texture.

SafelyFast
  • 75
  • 1
  • 1
  • 10
  • Thank you for the answer, it helped me understand this subject a bit more, yet, when I modify my code to what you just sent (both the width and height parameters and the SDL_RenderCopy line), I get an error: src/entity.cpp:5:1: error: no declaration matches 'Entity::Entity(Vector2f, SDL_Texture*, int, int)' 5 | Entity::Entity(Vector2f p_pos, SDL_Texture* p_tex, int w, int h) | ^~~~~~ any idea what this could be? – Martim Jun 20 '23 at 15:18
  • @Martim You need to update the definition of your class in your header file to match the new definition in the source file. Of note, you don't need to update the `SDL_RenderCopy` line too. It was just something to ponder for a future project. To fix the problem you had in this post just make the change to the class. – SafelyFast Jun 20 '23 at 22:47