SDL2 / C ++无法加载图像

时间:2015-03-29 22:48:57

标签: c++ sdl

我没有错误,控制台窗口立即关闭而不显示我的bmp和png文件。此外,所有DLL文件和图片都在正确的文件夹中(图片在我的Debug文件夹中,exe所在的位置) 我正在使用visual studio 2013

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


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

bool quit = false;

SDL_Init(SDL_INIT_VIDEO);

SDL_Window* window = NULL;
window = SDL_CreateWindow("My First RPG", 100, 100, 600, 400, SDL_WINDOW_SHOWN);

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

return 0;

SDL_Renderer* renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event* mainEvent = new SDL_Event();

SDL_Texture* background_image = NULL;
background_image = IMG_LoadTexture(renderer, "background.bmp");

if (background_image == NULL)
{
    std::cout << "Could not load background" << std::endl;
}

SDL_Rect background_rect;
background_rect.x = 0;
background_rect.y = 0;
background_rect.w = 600;
background_rect.h = 400;

SDL_Texture* player_image = NULL;
player_image = IMG_LoadTexture(renderer, "player.png");

if (player_image == NULL)
{
    std::cout << "Could not load Player" << std::endl;
}

SDL_Rect player_rect;
player_rect.x = 300;
player_rect.y = 250;
player_rect.w = 100;
player_rect.h = 100;


while (!quit && mainEvent->type != SDL_QUIT)
{

    SDL_PollEvent(mainEvent);
    SDL_RenderClear(renderer);

    SDL_RenderCopy(renderer, background_image, NULL, &background_rect);
    SDL_RenderCopy(renderer, player_image, NULL, &player_rect);

    SDL_RenderPresent(renderer);
}

SDL_DestroyTexture(background_image);
SDL_DestroyTexture(player_image);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete mainEvent;

return 0;
}

1 个答案:

答案 0 :(得分:5)

创建窗口并测试它是否成功后,您有以下行(第23行):

return 0;

删除它,你的程序运行正常。