- #include <stdlib.h>
- #include <SDL/SDL.h>
- #include <SDL/SDL_image.h>
- #define MOVE_STEP 5
- #define SCREEN_LENGTH 640
- #define SCREEN_WIDTH 480
- #define SCREEN_DIPTH 24
- unsigned int global_pos_x = 0;
- void move_bmp(SDL_Surface* screen, SDL_Rect* dest, SDL_Surface* bmp, SDL_Rect* bmp_rect)
- {
- if (global_pos_x >= bmp->w+SCREEN_LENGTH)
- {
- global_pos_x = 0;
- }
- global_pos_x += MOVE_STEP;
- if (global_pos_x <= bmp->w)
- {
- //screen draw rect
- dest->x = 0;
- dest->y = 0;
- dest->w = global_pos_x;
- dest->h = bmp->h;
- //bmp rect to draw
- bmp_rect->x = bmp->w - global_pos_x;
- }
- else if (global_pos_x < SCREEN_LENGTH)
- {
- dest->x = global_pos_x - bmp->w;
- dest->y = 0;
- dest->w = bmp->w;
- dest->h = bmp->h;
- bmp_rect->x = 0;
- }
- else
- {
- dest->x = global_pos_x - bmp->w;
- dest->y = 0;
- dest->w = bmp->w + SCREEN_LENGTH - global_pos_x;
- dest->h = bmp->h;
- bmp_rect->x = 0;
- }
- bmp_rect->y = 0;
- bmp_rect->w = dest->w;
- bmp_rect->h = bmp->h;
- //use back color clear screen
- if ( SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)) < 0)
- {
- printf("clear screen failure:%s/n", SDL_GetError());
- exit(1);
- }
- SDL_BlitSurface(bmp, bmp_rect, screen, dest);
- //must be filp screen, can not UpdateRect, or the front rect will left
- SDL_Flip(screen);
- }
- int main(int argc, char* argv[])
- {
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- printf("can not init sdl:%s/n", SDL_GetError());
- exit(1);
- }
- atexit(SDL_Quit);
- SDL_Surface* screen = SDL_SetVideoMode(SCREEN_LENGTH, SCREEN_WIDTH, SCREEN_DIPTH, SDL_SWSURFACE|SDL_NOFRAME);
- if (screen == NULL)
- {
- printf("can not setvidemode:%s/n", SDL_GetError());
- exit(1);
- }
- SDL_Surface* bmp = SDL_LoadBMP("test.bmp");
- if (bmp == NULL)
- {
- printf("can not load test.bmp:%s/n", SDL_GetError());
- exit(1);
- }
- SDL_WM_SetCaption("yijunjun", "very good");
- SDL_Rect dest;
- SDL_Rect bmp_rect;
- SDL_Event event;
- SDL_Surface* jpg = IMG_Load("my_self.jpg");
- if (jpg == NULL)
- {
- printf("img load failure:%s/n", SDL_GetError());
- exit(1);
- }
- SDL_Rect jpg_rect;
- while(1)
- {
- //check have exit message
- while (SDL_PollEvent(&event))
- if (event.type == SDL_QUIT)
- exit(0);
- move_bmp(screen, &dest, jpg, &jpg_rect);
- SDL_Delay(18);
- }
- return 0;
- }
libSDL--初步使用
最新推荐文章于 2024-05-10 09:43:47 发布
2029

被折叠的 条评论
为什么被折叠?



