Carol Cody
Carol Cody

Reputation: 15

SDL_Texture is not being set

I am having troubles with SDL_Texture

RPGTutorial.cpp

#include "stdafx.h"

int main(int argc, char *argv[])
{

    bool quit = false;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = NULL;
    window = SDL_CreateWindow("RPG Tutorial!", 100, 100, 600, 400, SDL_WINDOW_SHOWN);

    if (window == NULL)
    {
        std::cout << "Window couldn't be created" << std::endl;
        return 0;
    }

    SDL_Renderer* renderer = NULL;
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == NULL)
    {
        std::cout << "Renderer is not being created!" << std::endl;

        SDL_DestroyWindow(window);
        system("PAUSE");
        return 0;

    }

    SDL_Event* mainEvent = new SDL_Event();

    SDL_Texture* grass = NULL;
    grass = IMG_LoadTexture(renderer, "Grass.bmp");
    if (grass == NULL)
    {
        std::cout << "Grass Image was not found!" << std::endl;

        SDL_DestroyWindow(window);
        SDL_DestroyRenderer(renderer);
        delete mainEvent;
        system("PAUSE");
        return 0;
    }

    SDL_Rect grass_rect;
    grass_rect.x = 0;
    grass_rect.y = 0;
    grass_rect.w = 64 * 2;
    grass_rect.h = 64 * 2;

    while (!quit && mainEvent->type != SDL_QUIT)
    {
        SDL_PollEvent(mainEvent);
        SDL_RenderClear(renderer);

        SDL_RenderCopy(renderer, grass, NULL, &grass_rect);

        SDL_RenderPresent(renderer);
    }

    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    delete mainEvent;

    return 0;
}

stdafx.h

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <iostream>

I have the Grass.bmp in the RPGTutorial->RPGTutorial->Grass.bmp When I compile it, it is successful. It runs through the code til I get to the part where it checks if(grass == NULL) and it goes through that and exits. Can someone help me know why my grass is not being set to the image when I have the image in the same folder that the .cpp files are in? I even tried adding an Image folder to hold it in, and it did not work either.

Upvotes: 0

Views: 111

Answers (1)

wizebin
wizebin

Reputation: 730

If you have the time, I recommend you take some time to go through the Lazyfoo tutorials they are fantastic. He mentions this issue in the second tutorial, "Getting an Image on the Screen."

Visual Studio changes your current working directory to the place where your .vcxproj file is. That will be the directory you want to place your resources in. If you're not sure where that is, you can use the _getcwd() function in the direct.h header MSDN Source For getcwd

Upvotes: 1

Related Questions