c++ - 当我使用 SDL_RECT 时表面消失

标签 c++ sdl 2d-games

我试图创建一个类,其中包含我的游戏 SHIP 的信息(目前的图像和位置),一切都按我预期的那样编译,但是如果我使用我在我的船中创建的 SDL_Rect 变量作为 SDL_blitsurface 中的参数方法,船没有出现。

这是我的代码:

主要.cpp

int main()
{
    init();
    Ship ship;
    ship.alive();
    Background bg;
    SDL_BlitSurface(bg.bg,NULL,screen,NULL);
    printf("%i",ship.rect.y);
    SDL_BlitSurface(ship.img,&ship.rect,screen,NULL);
    SDL_UpdateWindowSurface(window);
    SDL_Delay(5000);
    return 0;
}

发货.cpp

Ship::Ship(){
    std::string path = "img/arwing2.png";
    x = 50;
    y = 50;
    rect.x = x;
    rect.y = y;
    rect.w = 300;
    rect.y = 300;
    img = IMG_Load(path.c_str());
    if(img == NULL){
        printf("Unable to load image");
    }
    printf("Hey!!");
}


void Ship::alive(){
    printf("I'm alive!");
}

知道我做错了什么吗? 这就是我要的 enter image description here

这就是我得到的e

最佳答案

在您的代码中,您提供了源矩形。

这意味着它正在剪切您的图像以仅显示图像内部 (x,y) 的像素并扩展 (w,h)。

您可能想要提供目标矩形来告诉 SDL_BlitSurface 在哪里绘制它,即从屏幕内部的 (x,y)。如下:

SDL_BlitSurface(ship.img, NULL, screen, &ship.rect);
                          ^(src)        ^(dest)

关于c++ - 当我使用 SDL_RECT 时表面消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933790/

相关文章:

java - 这个颜色生成代码中的 "&"和 "|"有什么用?

c++ - 用c++清除文本文件中的数据

c++ - 如何在 Qt 项目中引用第三方 dll/库

java - 当玩家移动到屏幕边缘并将其传送回来时,如何使其背景为 "switches",就像穿越一个世界一样

Haskell SDL : Can't find mingw32. dll

c - 在 SDL 中旋转图像

react-native - react native 雪碧表

c++ - C++中的模板类

android - UnwindResume 等链接错误(安卓交叉编译)

c++ - 如何将 C++ 结构转换为数据库