SDL,如何渲染背景图片?

时间:2014-06-26 04:24:21

标签: c++ background rendering sdl sdl-2

我有渲染问题,但我解决了这个问题。问题是,图片实际上是不会出现的。我知道图片被加载,因为如果没有加载,我有一个错误处理程序。我知道代码很乱,但我只需要显示图片。

#include <SDL/SDL.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int fps = 60;

SDL_Window *window;


int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    window = SDL_CreateWindow("", 300, 100, 1024, 800, SDL_WINDOW_OPENGL);

    if (window == NULL)
    {
        cout << ("could not create window: %s/n", SDL_GetError());
        return 1;
    }
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
    int x = 0, y = 0;
    SDL_Surface  *screen;
    SDL_Surface *background = SDL_LoadBMP("hockeyrink.bmp");
    if(background == NULL)
    {
        SDL_ShowSimpleMessageBox(0, "Background init error",         SDL_GetError(), window);
    }

    if(renderer == NULL)
    {
        SDL_ShowSimpleMessageBox(0, "Renderer init error", SDL_GetError(), window);
    }
    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,background);
    if(texture == NULL)
    {
        SDL_ShowSimpleMessageBox(0, "Texture init error", SDL_GetError(), window);
    }

    const int speed = 5;
    SDL_Rect camera;
    camera.x = 0; //Don't worry about this camera, I need this after i get the background working.
    camera.y = 0;
    camera.w = 800;
    camera.h = 600;
    bool b[2] = {0,0};
    Uint32 start;

    bool running = true;
    while (running)
    {
        start = SDL_GetTicks();
        SDL_Event event;

        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
                running = false;
                SDL_DestroyTexture(texture);
                SDL_FreeSurface(background);
                SDL_DestroyRenderer(renderer);
                SDL_DestroyWindow(window);
                SDL_Quit();
                break;
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                case SDLK_UP:
                    b[0]=1;
                    break;
                case SDLK_LEFT:
                    b[1]=1;
                    break;
                }
                break;
            case SDL_KEYUP:
                switch(event.key.keysym.sym)
                {
                case SDLK_UP:
                    b[0]=0;
                    break;
                case SDLK_LEFT:
                    b[1]=0;
                    break;
                }
                break;
            }
        }

        if(b[0])
        {
            x+=speed;
            camera.y+=speed;
            if (camera.y > 3000-800)
            {
                camera.y=0;
            }

        }
        else if(b[1])
        {
            x-=speed;
            camera.y-=speed;

            if (camera.y <= 0)
            {
                camera.y = 2000-800;
            }


            SDL_RenderCopy(renderer, texture, NULL, NULL);
            SDL_RenderPresent(renderer);
        }
        if(1000/fps>SDL_GetTicks()-start)
        {
            SDL_Delay(1000/fps-(SDL_GetTicks() - start));
        }
    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

您已将b[1]初始化为0,并且仅在代码中b[1]1时呈现纹理。
你应该放行:

SDL_RenderCopy(renderer, texture, NULL, NULL);
 SDL_RenderPresent(renderer);

循环之前,
或按键盘左键(即SDLK_LEFT
在屏幕上呈现和呈现图像。

它适用于我的系统,所以它也可以在你的系统中工作。