简体   繁体   English

SDL2 / C ++无法加载图像

[英]SDL2/C++ could not load image

I get no errors and the console windows closes immediately without displaying my bmp and png files. 我没有收到任何错误,控制台窗口立即关闭,而没有显示我的bmp和png文件。 Also all DLL files and the pics are in correct folders ( pictures are in my Debug folder where the exe is located) I'm using visual studio 2013 此外,所有DLL文件和图片都位于正确的文件夹中(图片位于exe所在的Debug文件夹中)我正在使用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;
}

After you create the window, and test whether it was successful, you have the following line (line 23): 创建窗口并测试它是否成功之后,您将获得以下行(第23行):

return 0;

Remove that, and your program works fine. 删除它,您的程序运行正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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