我刚刚开始学习SDL,我发现如果初始化SDL_Rect变量,则SDL_Delay不起作用。然后,如果我在SDL_Rect中设置值之一,则图像甚至不会显示(或暂停)。我不明白。我从lazyfoo的教程中获得了这段代码,目前只是在弄乱它

#include <SDL/SDL.h>
#include <iostream>

using namespace std;
int main( int argc, char* args[] ){

    int width = 512;
    int height = 512;

    //The images
    SDL_Surface* source = NULL;
    SDL_Surface* screen = NULL;
    //Start SDL
    //SDL_Init( SDL_INIT_EVERYTHING );
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) {
        return 1;
    }
    //Set up screen
    screen = SDL_SetVideoMode( width, height, 24, SDL_SWSURFACE );

    //Load imagec
    source = SDL_LoadBMP( "image.bmp");
    //Apply image to screen

    SDL_Rect * hello;                    //here is where it messes up the program
    //for(int a = 0; a < width; a++){    // i was trying to make the image move around the screen/window
    //hello -> x = 0;
    //now -> w = 200;
    //now -> h = 200;
    //for(int b = 0; b < height; b++){
    //now -> y = 0;
    //SDL_WM_SetCaption( "ajsncnsc", NULL );
    SDL_BlitSurface( source, NULL, screen, NULL );
    //Update Screen
    SDL_Flip( screen );
    SDL_Delay( 2000 );
    //    }
    //}

    //Free the loaded image
    SDL_FreeSurface( source );
    //Quit SDL
    SDL_Quit();
    return 0;
}

最佳答案

SDL_Rect * hello;创建一个指向SDL_Rect的指针。它指向随机内存,因为您没有为其分配任何内容。修改其成员可能导致任何事情发生。

请改用SDL_Rect hello;-这样会创建一个实际的SDL_Rect,您现在可以放心地进行例如hello.x = 200;,无需修改您不拥有的内存。

10-07 19:46
查看更多