在窗口上绘制PNG图像 - C ++ SDL 2.0

时间:2015-04-13 16:59:59

标签: c++ sdl sdl-2

我已创建了一个窗口,但图片不会在其上呈现

我尝试了很多东西,但都没有用。

以下是该计划的一部分

janela1 = SDL_CreateWindow("Arkanoid", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, janx, jany, SDL_WINDOW_SHOWN);
rend = SDL_CreateRenderer(janela1, -1, SDL_RENDERER_ACCELERATED);
rwop = SDL_RWFromFile("bola.png", "rb");
textura = IMG_LoadTexture_RW(rend, rwop, NULL);
if (textura == nullptr || rwop == nullptr){
    cout << "erro";
}
SDL_Rect textura_retangulo;
textura_retangulo.h = 100;
textura_retangulo.w = 100;
textura_retangulo.x = 100;
textura_retangulo.y = 100;
SDL_RenderClear(rend);
SDL_RenderCopy(rend, textura, NULL, &textura_retangulo);
SDL_RenderPresent(rend);

我认为问题出现在RenderCopy中,我不知道是否需要进行其他的渲染图像,因为顺便说一下现在没有任何事情发生

1 个答案:

答案 0 :(得分:0)

首先,你应该在声明窗口之前声明janx和jany的int值。

#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <conio> 

int janx = 100; //change both of these into your preferred number
int jany = 100;

SDL_Window *janela1 = SDL_CreateWindow("Arkanoid", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, janx, jany, SDL_WINDOW_SHOWN);
SDL_Renderer* rend = SDL_CreateRenderer(janela1, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture *textura = IMG_LoadTexture_RW(rend, rwop, NULL);
SDL_RWops *rwop = SDL_RWFromFile("bola.png", "rb");
SDL_Event()* mainEvent = new SDL_Event();

if (textura == nullptr || rwop == nullptr){
    cout << "erro";
}
SDL_Rect textura_retangulo;
textura_retangulo.h = 100;
textura_retangulo.w = 100;
textura_retangulo.x = 100;
textura_retangulo.y = 100;


//I highly recommend you turn these into a loop, you'll find trouble once this expands.
void loop()
{
    while(mainEvent->type != SDL_QUIT)
    {
         SDL_PollEvent(mainEvent);
         SDL_RenderClear(rend);
         SDL_RenderCopy(rend, textura, NULL, &textura_retangulo);
         SDL_RenderPresent(rend);
    }
}