奇怪的基本 SDL

发布于 2024-11-02 08:53:42 字数 1317 浏览 4 评论 0原文

我刚刚开始学习 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;
}

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_Rect, 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2024-11-09 08:53:42

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

使用 SDL_Rect hello; 代替 - 这会创建一个实际的 SDL_Rect,您现在可以安全地执行 hello.x = 200; 操作,而无需修改您不修改的内存不拥有。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文