Today I worked on creating a new cursor image. The new image is a hand cursor that will be displayed when the mouse hovers over interactive objects. This is a good way to let the user know that the object they are hovering over is interactive.
To add the new image, I had to edit the Player
object.
First, I created a new variable for the image.
SURFACE *interatacbleCursorImage;
Then, I updated the LoadPlayImages()
method to load the new image.
template<>
bool Player<std::string, SDL_Surface, SDL_Renderer>::LoadPlayerImages( std::string locationImage1,
std::string locationImage2)
{
defaultCursorImage = IMG_Load(locationImage1.c_str());
interatacbleCursorImage = IMG_Load(locationImage2.c_str());
if (defaultCursorImage == nullptr || interatacbleCursorImage == nullptr)
{
return false;
}
return true;
}
To display the new Image, I added a boolean argument to the ShowCursor()
method and reformed its logic to display the correct image based on the new argument.
template<>
void Player<std::string, SDL_Surface, SDL_Renderer>::ShowCursor(SDL_Renderer *renderer, bool interactive)
{
SDL_Surface *convertImage = nullptr;
int imageSizeDivisionValue = 0;
if (!interactive)
{
convertImage = SDL_ConvertSurface(defaultCursorImage, defaultCursorImage->format);
imageSizeDivisionValue = 50;
}else
{
convertImage = SDL_ConvertSurface(interatacbleCursorImage, defaultCursorImage->format);
imageSizeDivisionValue = 40;
}
if (convertImage == nullptr)
{
std::cout << "Failed to create default cursor ";
std::cout << SDL_GetError() << std::endl;
exit(-1);
}
SDL_Texture *defaultCursorTexture = SDL_CreateTextureFromSurface(renderer, convertImage);
const SDL_FRect defaultCursorHolder = { static_cast<float>(x),
static_cast<float>(y),
static_cast<float>(convertImage->w/imageSizeDivisionValue),
static_cast<float>(convertImage->h/imageSizeDivisionValue)};
SDL_RenderTexture(renderer, defaultCursorTexture, nullptr, &defaultCursorHolder);
SDL_DestroyTexture(defaultCursorTexture);
SDL_DestroySurface(convertImage);
}
I updated the CMakeLists.txt file to include the new image in the .app container.
file(GLOB INTERACTABLE_CURSOR "graphics/Player/interactable_cursor.png")
# Create Executable
add_executable( ${PROJECT_NAME} MACOSX_BUNDLE
./src/main.cpp ./src/game.h
./src/game.cpp ./src/gameobject.h
./src/player.h ./src/thief.h
./src/gatherer.h ./src/fighter.h
./src/menuoptionsstructure.h ./src/audioplayer.h
./src/audioplayer.cpp
${FONT_SOURCES} ${MENU_LIGHT_UP_SOUND}
${MENU_MUSIC} ${DEFAULT_CURSOR} ${ATTACK_CURSOR}
${ENVIROMENT_BACKGROUND} ${LEAF_IMAGE} ${INTERACTABLE_CURSOR})
#Add Interactable Cursor
file(RELATIVE_PATH GET_REL_PATH "${CMAKE_CURRENT_SOURCE_DIR}" ${INTERACTABLE_CURSOR} )
get_filename_component(FILE_PATH "${GET_REL_PATH}" DIRECTORY)
set_property(SOURCE "${INTERACTABLE_CURSOR}" PROPERTY MACOSX_PACKAGE_LOCATION "Resources/${FILE_PATH}")