C++ SDL2 C++;-多图像渲染错误

C++ SDL2 C++;-多图像渲染错误,c++,sdl,codeblocks,sdl-2,C++,Sdl,Codeblocks,Sdl 2,在开始之前,我使用的IDE是Code::Blocks 我打算在另一个独立于我的学习项目的项目中练习我在网上学到的关于SDL的知识。我想在背景上加载一个球的图像。在我的学习项目中效果很好。我将代码复制到我的测试项目中,球消失了。只有背景可见 我删除了涉及背景的代码,效果很好 我的代码: #include <iostream> #include <cstdlib> #include <SDL.h> #include <SDL_image.h> #und

在开始之前,我使用的IDE是Code::Blocks

我打算在另一个独立于我的学习项目的项目中练习我在网上学到的关于SDL的知识。我想在背景上加载一个球的图像。在我的学习项目中效果很好。我将代码复制到我的测试项目中,球消失了。只有背景可见

我删除了涉及背景的代码,效果很好

我的代码:

#include <iostream>
#include <cstdlib>
#include <SDL.h>
#include <SDL_image.h>
#undef main

using namespace std;

SDL_Texture *LoadTexture (string filepath, SDL_Renderer *renderTarget){
    SDL_Texture *texture = 0;
    SDL_Surface *surface = IMG_Load(filepath.c_str());

    texture = SDL_CreateTextureFromSurface(renderTarget, surface);

    SDL_FreeSurface(surface);

    return texture;
}

int main () {

    SDL_Window *window = 0;
    SDL_Texture *ball = 0;
    SDL_Texture *background_sky = 0;

    SDL_Renderer *renderTarget = 0;

    int frameW, frameH;
    int textureW, textureH;

    //--Crop Instructions--//
    frameW = textureW / 1; //row division
    frameH = textureH / 1; //column division

    SDL_Rect ballcrop;
    ballcrop.x = ballcrop.y = 0;
    ballcrop.w = frameW; //length of selected area
    ballcrop.h = frameH; //height of selected area

    SDL_Rect ballpos;
    ballpos.x = ballpos.y = 10; //ball position
    ballpos.w = ballpos.h = 64; //size of crop
    //---------------------//

    SDL_Init(SDL_INIT_EVERYTHING);
    IMG_Init(IMG_INIT_PNG);

    window = SDL_CreateWindow("Ball", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 500, SDL_WINDOW_SHOWN);
    renderTarget = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    ball = LoadTexture("ball.png", renderTarget);
    background_sky = LoadTexture("bg_sky.png", renderTarget);


    SDL_QueryTexture(ball, NULL, NULL, &textureW, &textureH); //crop action

    bool Active = true;
    SDL_Event ev;

    while (Active) {
        while (SDL_PollEvent(&ev)!=0) {
            if (ev.type == SDL_QUIT)
                Active = false;

            else if (ev.type == SDL_KEYDOWN) {
                switch (ev.key.keysym.sym) {
                case SDLK_UP:
                    ballpos.y -= 5;
                    break;
                case SDLK_DOWN:
                    ballpos.y += 5;
                    break;
                }
            }
        }
        SDL_RenderClear(renderTarget);

        SDL_RenderCopy(renderTarget, background_sky, NULL, NULL);
        SDL_RenderCopy(renderTarget, ball, &ballcrop, &ballpos);

        SDL_RenderPresent(renderTarget);
    }

    SDL_DestroyWindow(window);
    SDL_DestroyTexture(background_sky);
    SDL_DestroyTexture(ball);

    SDL_DestroyRenderer(renderTarget);

    window = 0;
    ball = background_sky = 0;
    renderTarget = 0;

    IMG_Quit();
    SDL_Quit();

    return 0;
}
#包括
#包括
#包括
#包括
#未定义主
使用名称空间std;
SDL_纹理*LoadTexture(字符串文件路径,SDL_渲染器*renderTarget){
SDL_纹理*纹理=0;
SDL_Surface*Surface=IMG_Load(filepath.c_str());
纹理=SDL_CreateTextureFromSurface(渲染目标,曲面);
SDL_自由曲面(曲面);
返回纹理;
}
int main(){
SDL_窗口*窗口=0;
SDL_纹理*球=0;
SDL_纹理*背景_天空=0;
SDL_渲染器*renderTarget=0;
int frameW,frameH;
int-textureW,textureH;
//--作物说明--//
frameW=textureW/1;//行划分
frameH=textureH/1;//列划分
SDL__Rect ballcrop;
ballcrop.x=ballcrop.y=0;
ballcrop.w=frameW;//所选区域的长度
ballcrop.h=frameH;//所选区域的高度
SDL__Rect ballpos;
ballpos.x=ballpos.y=10;//球位置
ballpos.w=ballpos.h=64;//作物大小
//---------------------//
SDL_Init(SDL_Init_EVERYTHING);
IMG_Init(IMG_Init_PNG);
window=SDL_CreateWindow(“球”,SDL_WINDOWPOS_居中,SDL_WINDOWPOS_居中,500,显示SDL_window_);
renderTarget=SDL_CreateRenderer(窗口,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
ball=LoadTexture(“ball.png”,renderTarget);
background_sky=LoadTexture(“bg_sky.png”,renderTarget);
SDL_QueryTexture(ball、NULL、NULL、&textureW和&textureH);//裁剪操作
bool Active=true;
SDL_事件ev;
while(活动){
while(SDL_PollEvent(&ev)!=0){
如果(ev.type==SDL_退出)
主动=假;
否则如果(ev.type==SDL\U键控){
开关(电动钥匙钥匙符号符号符号){
案例SDLK\U UP:
球位y-=5;
打破
案例SDLK_向下:
球位y+=5;
打破
}
}
}
SDL_RenderClear(渲染目标);
SDL_渲染复制(渲染目标、背景_天空、空、空);
SDL_渲染复制(渲染目标、球和球裁剪以及球位置);
SDL_RenderPresent(渲染目标);
}
SDL_窗口(窗口);
SDL_纹理(背景_天空);
SDL_(球);
SDL_渲染器(renderTarget);
窗口=0;
球=背景\天空=0;
renderTarget=0;
IMG_Quit();
SDL_退出();
返回0;
}
我不想展示我研究项目的源代码,因为它非常凌乱


任何帮助都将不胜感激:)

您在计算
frameW
frameH
时出错;计算的代码

//--Crop Instructions--//
frameW = textureW / 1; //row division
frameH = textureH / 1; //column division

在初始化之前使用
textureW
textureH
。移动整个“裁剪说明”块,直到调用
SDL\u QueryTexture

后,如果只注释掉与背景关联的
SDL\u渲染副本
,它是否仍然有效?当您颠倒
SDL_RenderCopy
s?
frameW=textureW/1的顺序时,会遇到同样的问题除以1有什么意义?还有,你从哪里得到textureW的值。。。在发布的代码中,它没有显示。@MicroVirus:是的,它显示了。背景消失了,但球不在那里。我必须注释掉所有相关的bgcode@zammalad:这是一件细致的事情^^;我认为最好是使用调试器逐行检查它,看看哪一行实际出现故障:是
SDL_RenderCopy
失败了,还是球纹理一开始没有(正确)加载?顺便说一下,我想指出,您可以使用调试器自己轻松地解决这个问题。如果你还不知道如何使用,现在是学习基础知识的好时机。使用调试器,看看是否可以找出使用它时出现的问题。我不确定情况是否如此。我在问题的评论中确实提出了这一点。事实上,如果所有背景画的东西都被移除,它就可以工作,这表明他确实将它设置在了某个地方(但这并不是他所说的完整代码)。