-2

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 ?

sockyone
  • 3
  • 3
  • 1
    Get a good C++ book and read how static variables work. –  Feb 09 '18 at 16:13
  • my english is not good that I can't read well. and C++ books in vietnamese are terrible, can't find any good one. – sockyone Feb 09 '18 at 16:23
  • can u have me with my problem, tell me something about static variable please ? – sockyone Feb 09 '18 at 16:24
  • This link may help: [SO: static and extern global variables in C and C++](https://stackoverflow.com/a/11056156/7478597). If you need more, please, google e.g. "stackoverflow C++ static extern" (or even without "stackoverflow"). Storage classes are something really fundamental in C and C++ (with slight differences in both languages). Even Vietnamese C++ books should mention this. ;-) – Scheff's Cat Feb 09 '18 at 16:28
  • Global variables are in most cases bad design. You could insert `SDL_Renderer *renderer` as parameter in your `loadTexture()` function to solve your issue. – Scheff's Cat Feb 09 '18 at 16:31
  • `_CORE_H_INCLUDED` name is reserved to the implementation. By defining it, the behaviour of the program is undefined. Choose another name for the include guard. – eerorika Feb 09 '18 at 16:32
  • @Scheff : thank you so much. I got it. – sockyone Feb 09 '18 at 16:35

1 Answers1

0

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, I.E. 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.