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 ?