我刚开始学习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;
}
答案 0 :(得分:2)
SDL_Rect * hello;
为SDL_Rect
创建指针。它指向随机内存,因为你没有为它分配任何东西。修改其成员可能会导致任何事情发生。
使用SDL_Rect hello;
代替 - 这会创建一个实际的SDL_Rect
,您现在可以安全地执行此操作hello.x = 200;
无需修改您不拥有的内存。