-
2007-03-14
移动图片 - [C++]
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://lwyf.blogbus.com/logs/4767702.html
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif#include <iostream>
#include <stdlib.h>
#include "SDL.h"using namespace std;
int main(int argc, char *argv[])
{
SDL_Surface *screen;
SDL_Surface *image;
SDL_Event event;
SDL_Rect dest;
int ncolors, i;
SDL_Color *colors;
int x = 0,y = 0;
int deltax = 0,deltay = 0;
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0){
printf("Could not initializing SDL: %s.\n",SDL_GetError());
exit(0);
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
if(screen == NULL){
fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n", SDL_GetError());
exit(-1);
}
image = SDL_LoadBMP("card_back.bmp");
if ( image == NULL ) {
fprintf(stderr, "Can't load : %s!\n", SDL_GetError());
SDL_Delay(2000);
return -1;
}
dest.x = 0;
dest.y = 0;
dest.w = image->w;
dest.h = image->h;
SDL_BlitSurface(image,NULL,screen,&dest);
SDL_UpdateRects(screen,1,&dest);
while(1)
{
while(SDL_PollEvent(&event))
{
switch(event.type){
case SDL_MOUSEBUTTONDOWN:
x = event.motion.x; y = event.motion.y;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_UP:
deltay = -4;
break;
case SDLK_DOWN:
deltay = 4;
break;
case SDLK_LEFT:
deltax = -4;
break;
case SDLK_RIGHT:
deltax = 4;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym){
case SDLK_UP:
if(deltay < 0)
deltay = 0;
break;
case SDLK_DOWN:
if(deltay > 0)
deltay = 0;
break;
case SDLK_LEFT:
if(deltax < 0)
deltax = 0;
break;
case SDLK_RIGHT:
if(deltax > 0)
deltax = 0;
break;
}
break;
case SDL_QUIT:
exit(0);
break;
}
}
x += deltax;
y += deltay;
if(x < 0) x = 0;if(x > screen->w - image->w) x = screen->w - image->w;
if(y < 0) y = 0;
if(y > screen->h - image->h) y = screen->h - image->h;
dest.x = x;
dest.y = y;
SDL_FillRect(screen, NULL, 0);
if(SDL_BlitSurface(image, NULL, screen, &dest) < 0)
fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
SDL_Flip(screen);
}
SDL_FreeSurface(image);
return 1;
}
收藏到:Del.icio.us








评论
不过,SDL是个什么东西?
注释呢