I am trying to implement sprite clips, or rendering a portion of a sprite sheet onto the screen. For now, I am least trying to get one sprite to display on the screen. Controller support and adding the other sprites when changing the character direction will come later. Here is what I have so far.
First, the class declaration:
class World1
{
public:
//other stuff
//screen resolution
int window_width = 0;
int window_height = 0;
//handles player
void renderPlayer(SDL_Renderer*&);
//other stuff
private:
//player graphic and rectangle structs/variables/etc.
SDL_Texture* pSpriteSheet = nullptr;
SDL_Rect pSpriteClips[3];
SDL_Rect* pSprite;
SDL_Rect pBase;
SDL_RendererFlip sFlip = SDL_FLIP_NONE;
//other stuff
};
Then, the constructor:
World1::World1(int SCREEN_WIDTH, int SCREEN_HEIGHT, SDL_Renderer*& renderer)
{
window_width = SCREEN_WIDTH;
window_height = SCREEN_HEIGHT;
*pSpriteSheet = IMG_LoadTexture(renderer, "male_base-test-anim.gif");
if (pSpriteSheet == nullptr)
{
cout << "Unable to load player Sprite sheet.";
}
//facing down sprite
pSpriteClips[0].x = 14;
pSpriteClips[0].y = 12;
pSpriteClips[0].w = 145;
pSpriteClips[0].h = 320;
//more code...
//player sprite
pBase.x = 0;
pBase.y = 0;
pBase.w = pSpriteClips[0].w;
pBase.h = pSpriteClips[0].h;
}
Then, the rendering
void World1::renderPlayer(SDL_Renderer*& renderer)
{
//render
SDL_RenderClear(renderer); //clears screen
SDL_RenderCopyEx(renderer, pSpriteSheet, pSprite, &pBase, 0.0, NULL, sFlip);
SDL_RenderPresent(renderer); //puts image on screen
}
So far, the program does not build and I obtain the following error:
C:\Users\Kevin\Documents\Codeblocks\KnightQuest\worlds.cpp|12|error: invalid use of incomplete type 'SDL_Texture {aka struct SDL_Texture}'|
Am I mistaken to assume that C++ will automatically assign a memory address to SDL_Rect* when I assign a value to it via the dereference operator? Or, perhaps there is another issue?
↧