奇怪的基本 SDL
我刚刚开始学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SDL_Rect * hello;
创建一个指向SDL_Rect
的指针。它指向随机内存,因为您没有为其分配任何内容。修改其成员可能会导致任何事情发生。使用
SDL_Rect hello;
代替 - 这会创建一个实际的SDL_Rect
,您现在可以安全地执行hello.x = 200;
操作,而无需修改您不修改的内存不拥有。SDL_Rect * hello;
creates a pointer to aSDL_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 actualSDL_Rect
, you can now safely do e.g.hello.x = 200;
without modifying memory you don't own.