用SDL在Linux实现图像的移动

  SDL(Simple DirectMedia Layer)是一套很底层的图形API, 支持Linux, *BSD, MacOS, Win32 and BeOS等平台. 下面, 我在Linux用C语言和SDL编写一个移动图像的程序. 移动图像是2D游戏的基础.

首先. 你需要安装SDL, 也就是下面的包 libsdl, libsdl-dev, libsdl-image, libsdl-image-dev. libsdl-image是一套操作图形文件的的库, 使用它, 你只需要一个函数就能把png, jpg, bmp, gif等文件读入内存, 而且支持png的透明键.

下面是程序:

//author: ideawu
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

#define TICK_INTERVAL    15

int main(int argc, char *argv[]){
    SDL_Surface *screen;
    SDL_Surface *image;
    SDL_Event event;
    SDL_Rect r = {0,0,0,0};
    int x=0,y=0;
    char *file_name = "logofish.png";//使用的图像文件名, 放在当前目录下. 你可以使用png,jpg,bmp,等格式, SDL支持它们.
    int timepass=0, timeold=0;
   
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        printf("Could not initializing SDL: %s./n",SDL_GetError());
        exit(-1);
    }
    atexit(SDL_Quit);
    screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    if(screen == NULL){
        fprintf(stderr, "Couldn't set 800x600x32 video mode: %s/n", SDL_GetError());
        exit(1);
    }
   
    image = IMG_Load(file_name);
    if (image == NULL) {
        fprintf(stderr, "Couldn't load %s: %s/n", file_name, SDL_GetError());
        return 1;
    }
    while(1){
        while(SDL_PollEvent(&event)){
            switch(event.type){
                case SDL_MOUSEMOTION:
                    x = event.motion.x; y = event.motion.y;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym){
                        case SDLK_UP:
                            y -= 4;
                            break;
                        case SDLK_DOWN:
                            y += 4;
                            break;
                        case SDLK_LEFT:
                            x -= 4;
                            break;
                        case SDLK_RIGHT:
                            x += 4;
                            break;
                    }
                    break;
                case SDL_QUIT:
                    exit(0);
                    break;
            }
            if(x < -image->w + 2) x = -image->w + 2;
            if(y < -image->h + 2) y = -image->h + 2;
            if(x > screen->w + image->w - 2) x = screen->w + image->w - 2;
            if(y > screen->h + image->h - 2) y = screen->h + image->h - 2;
            r.x = x;
            r.y = y;

            timeold = SDL_GetTicks();
           
            SDL_FillRect(screen, NULL, 0);
            if(SDL_BlitSurface(image, NULL, screen, &r) < 0)
                fprintf(stderr, "BlitSurface error: %s/n", SDL_GetError());
            SDL_Flip(screen);
            timepass = SDL_GetTicks() - timeold;
            //printf("%d  ", timepass);
            if(timepass < TICK_INTERVAL){
                //SDL_Delay(TICK_INTERVAL - timepass);
            }
        }
    }
   
    return 1;
}

使用命令: gcc -lSDL_image -lSDL filename.c 编译. 然后 ./a.out 运行, 确保当前目录下有一个名为 fish.png的文件.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值