开发者

Weird basic SDL

Im just starting to learn SDL, and I found that if i initialize a SDL_Rect variable, SDL_Delay doesnt work. then, if i set one of the value in SDL_Rec开发者_开发技巧t, the image doesnt even show (or pause). i dont get it. i got this code from lazyfoo's tutorial and am currently just messing with it

#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; creates a pointer to a SDL_Rect. It points to random memory, since you didn't allocate anything for it. Modifying its members can cause anything to happen.

Use SDL_Rect hello; instead - this creates an actual SDL_Rect, you can now safely do e.g. hello.x = 200; without modifying memory you don't own.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜