Created
May 31, 2017 23:16
-
-
Save ander94lakx/814c021a2a5ab0b6deed8af0ca77f66b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_mixer.h> // Esta vez incluimos el SDL_mixer.h, que hara que todo sea mas facil | |
// OJO: para ello hay que instalar los paquetes | |
// libsdl2-mixer-2.0-0 | |
// libsdl2-mixer-dev | |
// Instalar en Ubuntu: sudo apt-get install libsdl2-mixer-2.0-0 libsdl2-mixer-dev | |
int main(int argc, char **argv) | |
{ | |
// Inicializamos SDL2 | |
SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO); | |
SDL_Window *window = SDL_CreateWindow("Hola!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0, SDL_WINDOW_OPENGL); | |
// Que? porque hemos puesto SDL_INIT_VIDEO? | |
// y poruq se separa con solo un palito vertical? No deberian ser dos? En plan asi ---> || | |
// Y que es eso de Window? | |
// Inicializamos el SDL mixer (es como un reproductor de musica, basicamente, en el que podemos añadir pistas) | |
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048); | |
// Cargamos el fichero de audio | |
Mix_Chunk* sonido = Mix_LoadWAV("soinuak/piano/DO.wav"); | |
SDL_Event event; // Variable que definimos donde guardaremos los eventos que ocurran | |
bool running = true; // Oh, un booleano!! Para usarlos hace falta incluir <stdbool.h> | |
while (running) // BUCLE INFINITO: Nuestro programa va a estar todo el rato escuchado eventos | |
{ | |
// Procesamos eventos (los eventos pueden ser muchas cosas, como por ejemplo pulsaciones de teclado) | |
while (SDL_PollEvent(&event)) | |
{ | |
if (event.type == SDL_QUIT) | |
{ | |
running = false; | |
} | |
else if (event.type == SDL_KEYDOWN) | |
{ | |
// Si pulsamos la Q reproducimos el sonido | |
if (event.key.keysym.sym == SDLK_q) | |
{ | |
Mix_PlayChannel(-1, sonido, 0); | |
} | |
} | |
} | |
} | |
Mix_FreeChunk(sonido); // Liberamos el sonido | |
Mix_CloseAudio(); // Cerramos el audio | |
SDL_Quit(); // Cerramos SDL2 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment