奇怪的基本SDL

Weird basic SDL

本文关键字:SDL      更新时间:2023-10-16

我刚开始学习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;不修改内存你不拥有。

相关文章:
  • 没有找到相关文章