I'm watching the tutorial for adding image in SDL game, but author uses Visual Studio. I'm on the XCode and I'm not sure where to add the assets folder. I tried with right click on root of application > Add files to "Project"
> destination of the assets
folder but image is not showing in the window.
main.cpp
#include "Game.hpp"
Game *game = nullptr;
int main() {
game = new Game();
game->init("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
while (game->running()) {
game->handleEvents();
game->update();
game->render();
}
game->clean();
return 0;
}
This is the initializing method where I add image at the end of the function:
Game.cpp
void Game::init(const char *title, int xpos, int ypos, int width, int height, bool fullscreen)
{
int flags = 0;
if (fullscreen) {
flags = SDL_WINDOW_FULLSCREEN;
}
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
std::cout << "Subsystems initalized." << std::endl;
window = SDL_CreateWindow(title, xpos, ypos, width, height, fullscreen);
if (window)
{
std::cout << "Window created!" << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer)
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
std::cout << "Renderer created!" << std::endl;
}
isRunning = true;
} else {
isRunning = false;
}
SDL_Surface *tempSurface = IMG_Load("assets/player.png");
playerTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
SDL_FreeSurface(tempSurface);
}
And this is the render method:
Game.cpp
void Game::render()
{
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, playerTexture, NULL, NULL);
SDL_RenderPresent(renderer);
}