首页 > 编程语言 >c语言编译器IDE的6键钢琴程序代码

c语言编译器IDE的6键钢琴程序代码

时间:2024-09-02 17:22:41浏览次数:12  
标签:loadMusic musics Mix 编译器 SDL buttonRects IDE event 程序代码

#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>

// Function for loading music to Mix_Music
static Mix_Music *loadMusic(const char *path) {
Mix_Music *music = Mix_LoadMUS(path);
if (music == NULL) {
fprintf(stderr, “Mix_LoadMUS Error: %s\n”, Mix_GetError());
return NULL;
}
return music;
}

int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING)!= 0) {
fprintf(stderr, “SDL_Init Error: %s\n”, SDL_GetError());
return 1;
}

SDL_Window *window = SDL_CreateWindow("Hello SDL2-mixer with Buttons", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 220, 280, 0);
if (window == NULL) {
    fprintf(stderr, "SDL_CreateWindow Error: %s\n", SDL_GetError());
    return 1;
}

SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer == NULL) {
    fprintf(stderr, "SDL_CreateRenderer Error: %s\n", SDL_GetError());
    return 1;
}

if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) == -1) {
    fprintf(stderr, "Mix_OpenAudio Error: %s\n", Mix_GetError());
    return 1;
}

Mix_Music *musics[6];
musics[0] = loadMusic("s1.wav");
musics[1] = loadMusic("s2.wav");
musics[2] = loadMusic("s3.wav");
musics[3] = loadMusic("s4.wav");
musics[4] = loadMusic("s5.wav");
musics[5] = loadMusic("s6.wav");

SDL_Rect buttonRects[6];
buttonRects[0].x = 60;
buttonRects[0].y = 320;
buttonRects[0].w = 80;
buttonRects[0].h = 300;

buttonRects[1].x = 160;
buttonRects[1].y = 320;
buttonRects[1].w = 80;
buttonRects[1].h = 300;

buttonRects[2].x = 260;
buttonRects[2].y = 320;
buttonRects[2].w = 80;
buttonRects[2].h = 300;

buttonRects[3].x = 360;
buttonRects[3].y = 320;
buttonRects[3].w = 80;
buttonRects[3].h = 300;

buttonRects[4].x = 460;
buttonRects[4].y = 320;
buttonRects[4].w = 80;
buttonRects[4].h = 300;

buttonRects[5].x = 560;
buttonRects[5].y = 320;
buttonRects[5].w = 80;
buttonRects[5].h = 300;

SDL_Event event;

while (1) {
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT) {
            break;
        } else if (event.type == SDL_MOUSEBUTTONDOWN) {
            int mouseX = event.button.x;
            int mouseY = event.button.y;
            for (int i = 0; i < 6; i++) {
                if (mouseX >= buttonRects[i].x && mouseX <= buttonRects[i].x + buttonRects[i].w &&
                        mouseY >= buttonRects[i].y && mouseY <= buttonRects[i].y + buttonRects[i].h) {
                    if (musics[i]!= NULL) {
                        Mix_PlayMusic(musics[i], 0);
                    }
                }
            }
        }
    }

    if (event.type == SDL_QUIT) {
        break;
    }

    SDL_RenderClear(renderer);
    for (int i = 0; i < 6; i++) {
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
        SDL_RenderFillRect(renderer, &buttonRects[i]);
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderDrawRect(renderer, &buttonRects[i]);
    }
    SDL_RenderPresent(renderer);
}

for (int i = 0; i < 6; i++) {
    if (musics[i]!= NULL) {
        Mix_FreeMusic(musics[i]);
    }
}
Mix_CloseAudio();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;

}

标签:loadMusic,musics,Mix,编译器,SDL,buttonRects,IDE,event,程序代码
From: https://blog.csdn.net/qq_32257509/article/details/141825215

相关文章

  • c语言编译器IDE小钢琴程序代码
    #include<stdio.h>#include<SDL2/SDL.h>#include<SDL2/SDL_mixer.h>//FunctionforloadingmusictoMix_MusicstaticMix_Music*loadMusic(constchar*path){Mix_Music*music=Mix_LoadMUS(path);if(music==NULL){fprintf(stderr,“M......
  • 【树莓派开发】树莓派GeanyIDE和控制台下C/C++中文乱码问题解决方法
    文章目录情况说明1.设置VS,将文件保存为UTF8编码2.更改GeanyIDE编码设置3.更改树莓派系统设置情况说明之前使用树莓派的时候,遇到了中文乱码的问题。VS2019编译器下写的.c文件,里面的中文注释在树莓派ide上乱码树莓派控制台上,C语言代码输出中文时乱码这里需要调整三个设置来解决该......
  • 【树莓派开发】gcc编译器中出现warning: #pragma once in main file
    众所周知,#pragmaonce语句是防止头文件重复包含非常常用的一条语句VS编译器在创建.h文件的时候会自动帮你在开头添加这个语句但是在gcc编译器下,这个语句就可能会出现一些问题所使用编译器:VS2019(windows10)树莓派(linux-gcc)warning:#pragmaonceinmainfile在我尝试在linux环境......
  • https://www.bilibili.com/video/BV1Bg41167W5/ 突破英语听力口语瓶颈20|掌握5种弱读,不
    functionwordsArticles(the,a/an)Auxiliaries(can,must,might,will)Demonstratives(this,these,that,those)Quantifiers(many,few,little,some)Prepositions(on,with,to,from)Pronouns(he,she,they,we)Conjunctions(and,but,or,but) 1.ReducingConjunction弱读连词......
  • Idea如何提交本地项目到Gitee或Gitlab等远程仓库
    1、确定远程仓库地址 2、确认自己的Git账号密码(提交代码时需要登录)如果是自己的Gitee码云则是自己登录码云的账号密码;如果是公司的GitLab则以公司分给自己的Git账号密码为准。3、确定自己本地安装了Git,然后用Idea创建或者开发好代码4、初始化本地项目的Git(以实际项目位......
  • goland idea中debug程序报错-debugger could not patch runtime.mallogc
    idea中debugGo程序报错errorlayer=debuggercouldnotpatchruntime.mallogc一、问题场景在idea中配置了Go编程环境,可以运行Go程序,但是无法debug,报错errorlayer=debuggercouldnotpatchruntime.mallogc:notypeentryfound,use‘types’foralistofvalidtypes......
  • Vue, Avoided redundant navigation to current location: "/login".
    VueAvoidedredundantnavigationtocurrentlocation:"/login".=================================报错解释:这个错误是在使用Vue.js框架时,发生的一个警告,表示尝试进行一个冗余的导航到当前位置(即“/login”路径)。这通常发生在VueRouter中,当你尝试通过编程方式导航到当前正......
  • Redis集群搭建以及用idea连接集群
    一、redis的集群搭建:判断一个是集群中的节点是否可用,是集群中的所用主节点选举过程,如果半数以上的节点认为当前节点挂掉,那么当前节点就是挂掉了,所以搭建redis集群时建议节点数最好为奇数,搭建集群至少需要三个主节点,三个从节点,至少需要6个节点。1、安装ruby#1.准备环......
  • Hausdorff Distance 和 Euclidean Distance Mean欧氏距离
    importtorchimporttorch.nnasnnclassHausdorffDistanceLoss(nn.Module):def__init__(self):super(HausdorffDistanceLoss,self).__init__()defforward(self,pred,target):#扩展为(B,N,1,D)和(B,1,M,D)pred=pred......
  • IDEA 使用教程
    概述视频教程:【尚硅谷IDEA安装idea实战教程(百万播放,新版来袭)】jetbrains中文官网IDEA官网IDEA从IDEA2022.1版本开始支持JDK17,也就是说如果想要使用JDK17,那么就要下载IDEA2022.1或者之后的版本。Jetbrains公司旗下还有其它产品,比如:WebStorm:用于开发JavaScri......