简体   繁体   中英

variable SDL_Renderer* seem to not work

Im trying to create something with SDL2 library. This is 2 functions in my main.cpp:

 bool Init()
{
    bool success = true;
    window = SDL_CreateWindow("SDL Program", SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
    if ( window == NULL )
    {
        cout << "Window could not be created. Error: \n" << SDL_GetError();
        success = false;
    }
    else
    {
        renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
        if (renderer == NULL)
        {
            cout << "Renderer could not be created. Error: \n" << SDL_GetError();
            success = false;
        }
        else
        SDL_SetRenderDrawColor(renderer,0xFF,0xFF,0xFF,0xFF);
    }
    return success;
}

SDL_Texture* loadTexture(string s)
{
    SDL_Texture* newTexture = NULL;
    SDL_Surface* loadedSurface = SDL_LoadBMP(s.c_str());
    if (loadedSurface == NULL)
    {
        cout << "Unable to load image. Error: \n " << SDL_GetError();
    }
    else
    {
        SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0x00, 0xFF, 0xFF ) );
        newTexture = SDL_CreateTextureFromSurface(renderer,loadedSurface);
        if (newTexture == NULL)
            cout << "Unable to create texture from " << s << " Error: \n" << SDL_GetError();
        SDL_FreeSurface(loadedSurface);
    }
    return newTexture;
}

Then i want to move loadTexture function to core.h file and core.cpp here is my core.h :

#ifndef _CORE_H_INCLUDED
#define _CORE_H_INCLUDED

#include <string>
#include <SDL.h>

using namespace std;

//SDL_Texture* loadTexture(string s);

const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 720;

static SDL_Window* window = NULL;
static SDL_Texture* texture = NULL;
static SDL_Renderer* renderer = NULL;
static SDL_Texture* current_render = NULL;
#endif

i set my SDL_renderer* as a static variable but when i move my function to core.cpp, my program run, but it didn't load the texture. I tried a lot then i think it because of SDL_Renderer* variable didn't work in "newTexture = SDL_CreateTextureFromSurface(renderer,loadedSurface);". What happened ? How can I do now ?

What your static declaration is doing in your core.h file is essentially saying that every .cpp file that includes core.h should have its own copy of that variable. This is likely not the behavior you want and what is causing your issue, IE Init or loadTexture are modifying one cpp's set of variables (core.cpp's) while main.cpp is using another. If you really want to use globals, what you should do is have the following in core.cpp:

SDL_Window* window = NULL;
SDL_Texture* texture = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* current_render = NULL;

and the following in core.h:

extern SDL_Window* window;
extern SDL_Texture* texture;
extern SDL_Renderer* renderer;
extern SDL_Texture* current_render;

What extern says is basically the opposite of static. It says "there is an SDL_Window* name window declared elsewhere in a .cpp file, so every .cpp including this should use that one". In this case, this would be core.cpp. So when main.cpp include's core.h, the window it uses will be the one declared in core.cpp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM