我正在尝试制作背景为曲棍球溜冰场的2D游戏。错误处理程序表示渲染器未初始化,因此使纹理未初始化。我知道BMP加载是因为没有错误。不用担心相机的东西。背景应该滚动。我只需要背景即可渲染。该代码可能不是最好的,但始终可以进行更正。

#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);
    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);
    }
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
    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;
    camera.y = 0;
    camera.w = 800;
    camera.h = 600;
    bool b[2] = {0,0};
    Uint32 start;
    window = SDL_CreateWindow("", 300, 100, 1024, 800, SDL_WINDOW_OPENGL);

    if (window == NULL)
    {
        cout << ("could not create window: %s/n", SDL_GetError());
        return 1;
    }
    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;
}

最佳答案

创建渲染器时,窗口指针不会指向任何内容(即未初始化)。
您必须在创建渲染器之前创建窗口。
所以把线:

    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);

关于c++ - 是什么导致SDL_Renderer无效?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24397231/

10-13 06:23