找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3668|回复: 0
收起左侧

基于ARM平台的钢琴游戏 08实现音乐钢琴效果

[复制链接]
ID:222305 发表于 2017-7-25 15:41 | 显示全部楼层 |阅读模式
//////////////////////////////////////////////////////////////////
//
//  Copyright(C), 2013-2016, GEC Tech. Co., Ltd.
//
//  File name: GPLE/bmp.c
//
//  Author: Vincent Lin (林世霖)  微信公众号:秘籍酷
//
//  Date: 2016-11
//  
//  Description: 处理BMP格式图像数据
//
//  GitHub: github.com/vincent040   Bug Report: 2437231462@qq.com
//
//////////////////////////////////////////////////////////////////

#include <errno.h>
#include "bmp.h"
#include "ts.h"

char * load_bmp(const char *bmpfile, struct image_info *minfo)
{
        int fd = open(bmpfile, O_RDONLY);
        if(fd == -1)
        {
                fprintf(stderr, "opening \"%s\" failed: %s\n",
                                        bmpfile, strerror(errno));
                exit(0);
        }

        // 获得文件大小,并分配内存
        struct stat fileinfo;
        fstat(fd, &fileinfo);

        int   rgb_size = fileinfo.st_size;
        char *rgb_buf  = calloc(1, rgb_size);

        // 读取BMP内容到内存中
        struct bitmap_header header;
        struct bitmap_info info;
        struct rgb_quad quad;
        read(fd, &header, sizeof(header));
        read(fd, &info, sizeof(info));
        if(info.compression != 0)
        {
                read(fd, &quad, sizeof(quad));
                fprintf(stderr, "read quad! \n");
        }
        read(fd, rgb_buf, rgb_size);

        minfo->width = info.width;
        minfo->height= info.height;
        minfo->pixel_size = info.bit_count/8;

        #ifdef DEBUG
        printf("width: %d\n", minfo->width);
        printf("height: %d\n", minfo->height);
        printf("pixel_size: %d\n", minfo->pixel_size);
        #endif

        close(fd);
        return rgb_buf;
}

void bmp2lcd(char *bmpfile, char *FB,
                         struct fb_var_screeninfo *vinfo,
                         int xoffset, int yoffset)
{
        xoffset = xoffset>(65*12+10) ? (65*10+10) : xoffset;

        struct image_info *minfo = calloc(1, sizeof(struct image_info));
        char *rgb_buf = load_bmp(bmpfile, minfo);
        char *tmp = rgb_buf;

        // 从最后一行开始显示BMP图像
        int pad = ((4-( minfo->width * minfo->pixel_size ) % 4)) % 4; // 0-3
        rgb_buf += (minfo->width * minfo->pixel_size + pad) * (minfo->height-1);        

        FB += (yoffset * vinfo->xres + xoffset) * 4;
        int lcd_w = vinfo->xres - xoffset;
        int lcd_h = vinfo->yres - yoffset;

        int x, y;
        for(x=0; x<lcd_h && x<minfo->height; x++)
        {
                for(y=0; y<lcd_w && y<minfo->width; y++)
                {
                        unsigned long lcd_offset = (vinfo->xres*x + y) * 4;
                        rgb_buf += minfo->pixel_size;

                        memcpy(FB + lcd_offset + vinfo->red.offset/8,   rgb_buf + 2, 1);
                        memcpy(FB + lcd_offset + vinfo->green.offset/8, rgb_buf + 1, 1);
                        memcpy(FB + lcd_offset + vinfo->blue.offset/8,  rgb_buf + 0, 1);
                }

                rgb_buf += pad;
                rgb_buf -= (minfo->width * minfo->pixel_size + pad) * 2;
        }

        free(tmp);
}


08实现音乐钢琴效果.rar

474.99 KB, 下载次数: 23, 下载积分: 黑币 -5

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表