2014-10-01 2 views
0

저는 Visual Studio 2013에서 SDL로 C++을 배우고 있습니다. Lazy Foo의 튜토리얼 (특히 : http://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php)을 따르고 있습니다.내 이미지가 렌더링되지 않는 이유는 무엇입니까?

내 이미지가 렌더링되지 않습니다. 튜토리얼 (작동)과 달리 전역 표면 변수가 없습니다. 대신에 나는 그들을 중심으로 선언하고 주위에 포인터를 전달했다.

코드 : main 함수 내에서 포인터가 초기화 하지이기 때문에

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

const int SCREEN_WIDTH = 600; 
const int SCREEN_HEIGHT = 480; 

//SDL init. 
bool initialiseSDL(SDL_Window* gWindow, SDL_Surface* gWindowSurface) 
{ 
    bool success = true; 

    /*SDL_Init() initisalises SDL library. Returning 0 or less signifies an error. 
     SDL_INIT_VIDEO flag refers to graphics sub system.*/ 
    if (SDL_Init(SDL_INIT_VIDEO) < 0) 
    { 
     printf("SDL could not initialise. SDL Error: %s\n", SDL_GetError()); 
     success = false; 
    } 
    else 
    { 
     //Create the window 
     gWindow = SDL_CreateWindow("Asteroids", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); 
     if (gWindow == NULL) 
     { 
      printf("Could not create window. SDL Error: %s\n", SDL_GetError()); 
      success = false; 
     } 
     else 
     { 
      //Get the window surface. 
      gWindowSurface = SDL_GetWindowSurface(gWindow); 
     } 
    } 
    return success; 
} 

bool loadMedia(SDL_Surface* surface, std::string path) 
{ 
    //Success flag 
    bool success = true; 

    surface = SDL_LoadBMP(path.c_str()); 

    if (surface == NULL) 
    { 
     printf("SDL surface failed to load. SDL Error: %s\n", SDL_GetError()); 
     success = false; 
    } 

    return success; 
} 

void close() 
{ 

} 

int main(int argc, char* argv[]) 
{ 
    SDL_Window* gWindow = NULL; 
    SDL_Surface* gWindowSurface = NULL; 
    SDL_Surface* gImageSurface = NULL; 

    if (!initialiseSDL(gWindow, gWindowSurface)) 
    { 
     printf("Failed to initialise.\n"); 
    } 
    else 
    { 

     if (!loadMedia(gImageSurface, "hw.bmp")) 
     { 
      printf("Failed to load inital media."); 
     } 
     else 
     { 
      //Apply the image 
      SDL_BlitSurface(gImageSurface, NULL, gWindowSurface, NULL); 

      //Update the surface 
      SDL_UpdateWindowSurface(gWindow); 

      //Wait two seconds 
      SDL_Delay(2000); 
     } 
    } 

    return 0; 
} 

답변

2

프로그램이, undefined behavior을 나타낸다.

initialiseSDL 함수에 전달하면 값으로 전달됩니다. 즉, 값이 복사되고 함수는 복사 후에 만 ​​작동하며 원래 포인터는 작동하지 않고 호출 후에도 초기화되지 않습니다. 당신이해야 할 일은

대신 참조 포인터를 전달하는 것입니다 : 물론

bool initialiseSDL(SDL_Window*& gWindow, SDL_Surface*& gWindowSurface) 

당신은 loadMedia 기능과 동일한 기능을 수행 할 필요가있다.

+0

@Pileborg 당신은 신사이고 학자입니다, 대단히 감사합니다! 나는 포인터에 관해 더 많이 읽을 것이다. – Cian

관련 문제