123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- /*
- * This file is part of Soft Mood (https://notabug.org/alkeon/soft-mood).
- * Copyright (c) 2019 Alejandro "alkeon" Castilla
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, version 3.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <iostream>
- #include "SDL.h"
- #include "slider.h"
- #include "song.h"
- #define BACKGROUND_COLOR 33,27,81,255
- const int WINDOW_X_POSITION = 450;
- const int WINDOW_Y_POSITION = 380;
- const int CLOCK_X_POSITION = 160;
- const int CLOCK_Y_POSITION = 130;
- const int BAR_X_POSITION = 50;
- const int BAR_Y_POSITION = 275;
- using namespace std;
- SDL_Point mouse;
- bool mouse_follow = false;
- SDL_Point mouse_offset;
- int auxiliar_songs = 0;
- #define MAX_AUX_VALUE 2
- int main(){
- if(SDL_Init(SDL_INIT_VIDEO) < 0)
- cout << "SDL not working " << SDL_GetError() << endl;
-
- SDL_Window * gWindow = SDL_CreateWindow("Ad contemplationem", SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED, WINDOW_X_POSITION, WINDOW_Y_POSITION, 0);
- if(gWindow == NULL) {
- cout << "Render did not start" << SDL_GetError() << endl;
- return 1;
- }
- SDL_Renderer * gRenderer = SDL_CreateRenderer(gWindow, -1,
- SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- if(gRenderer == NULL){
- cout << "Render did not start" << SDL_GetError() << endl;
- return 1;
- }
-
- SDL_Surface * icon_surface = IMG_Load("icons/icon.png");
- SDL_SetWindowIcon(gWindow, icon_surface);
-
- SDL_Event event;
- if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) == -1){
- cout << "Audio did not start " << SDL_GetError() << endl;
- return 1;
- }
- int flags = 0;
- int init = Mix_Init(flags);
- if((init&flags) != flags) {
- printf("Mix_Init: Failed to init Mixer!\n");
- printf("Mix_Init: %s\n", Mix_GetError());
- }
- string auxiliar_songs_name[] = {
- "sound/heartbeat.wav",
- "sound/fountain.wav" // Madrid fountain. Awesome :)
- };
- string auxiliar_images_name[] = {"icons/heart.png", "icons/fountain.png"};
- int running = 1;
- /* Start loading screen */
- SDL_SetRenderDrawColor(gRenderer, BACKGROUND_COLOR);
- SDL_RenderClear(gRenderer);
- SDL_Rect s1;
- s1.x = CLOCK_X_POSITION;
- s1.y = CLOCK_Y_POSITION;
-
- SDL_Surface * temp_surface;
- temp_surface = IMG_Load("icons/clock.png");
- if( temp_surface == NULL ){
- cout << "Icon (icons/clock.png) couldn't be loaded" << endl;
- return 1;
- }
- SDL_Texture * fillbar = SDL_CreateTextureFromSurface(gRenderer, temp_surface);
- if( fillbar == NULL ){
- cout << "Fillbar couldn't be created" << endl;
- return 1;
- }
- SDL_QueryTexture(fillbar, NULL, NULL, &s1.w, &s1.h);
- SDL_RenderCopy(gRenderer, fillbar, NULL, &s1);
- SDL_DestroyTexture(fillbar);
- SDL_FreeSurface(temp_surface);
- SDL_RenderPresent(gRenderer);
- /* Load audio on background */
- song rain("sound/rain.wav");
- song thunder("sound/thunder.wav");
- song waves("sound/waves.wav");
- song wind("sound/wind.wav");
- song fire("sound/fire.wav");
- song birds("sound/birds.wav");
- song crickets("sound/crickets.wav");
- song auxiliar(auxiliar_songs_name[0]);
- /* End load screen */
- SDL_SetRenderDrawColor(gRenderer, BACKGROUND_COLOR);
- SDL_RenderClear(gRenderer);
- /* Start sliders and icons */
- slider s_rain(gRenderer, BAR_X_POSITION, BAR_Y_POSITION);
- slider s_thunder(gRenderer, BAR_X_POSITION + 50, BAR_Y_POSITION);
- slider s_waves(gRenderer, BAR_X_POSITION + 100, BAR_Y_POSITION);
- slider s_wind(gRenderer, BAR_X_POSITION + 150, BAR_Y_POSITION);
- slider s_fire(gRenderer, BAR_X_POSITION + 200, BAR_Y_POSITION);
- slider s_birds(gRenderer, BAR_X_POSITION + 250, BAR_Y_POSITION);
- slider s_crickets(gRenderer, BAR_X_POSITION + 300, BAR_Y_POSITION);
- slider s_auxiliar(gRenderer, BAR_X_POSITION + 350, BAR_Y_POSITION);
- s_rain.set_image("icons/rain.png");
- s_thunder.set_image("icons/flash.png");
- s_waves.set_image("icons/waves.png");
- s_wind.set_image("icons/wind.png");
- s_fire.set_image("icons/fire.png");
- s_birds.set_image("icons/bird.png");
- s_crickets.set_image("icons/fountain.png");
- s_auxiliar.set_image("icons/heart.png");
- SDL_RenderPresent(gRenderer);
- bool minimized = false;
- while(running){
-
- SDL_GetMouseState(&mouse.x, &mouse.y);
- while(SDL_PollEvent(&event) != 0){
- if(event.type == SDL_QUIT)
- running = 0;
- if(event.button.button == SDL_BUTTON_LEFT){
- if(event.type == SDL_MOUSEBUTTONDOWN){
- mouse_follow = true;
- s_rain.test_mouse_follow(&mouse,&mouse_offset);
- s_thunder.test_mouse_follow(&mouse,&mouse_offset);
- s_waves.test_mouse_follow(&mouse,&mouse_offset);
- s_wind.test_mouse_follow(&mouse,&mouse_offset);
- s_fire.test_mouse_follow(&mouse,&mouse_offset);
- s_birds.test_mouse_follow(&mouse,&mouse_offset);
- s_crickets.test_mouse_follow(&mouse,&mouse_offset);
- s_auxiliar.test_mouse_follow(&mouse,&mouse_offset);
- }
- if(event.type == SDL_MOUSEBUTTONUP){
- mouse_follow = false;
- s_rain.mouse_follow_off();
- s_thunder.mouse_follow_off();
- s_waves.mouse_follow_off();
- s_wind.mouse_follow_off();
- s_fire.mouse_follow_off();
- s_birds.mouse_follow_off();
- s_crickets.mouse_follow_off();
- s_auxiliar.mouse_follow_off();
- }
- }
- if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESTORED){
- minimized = false;
- SDL_SetRenderDrawColor(gRenderer, BACKGROUND_COLOR);
- SDL_RenderClear(gRenderer);
- s_rain.draw();
- s_thunder.draw();
- s_waves.draw();
- s_wind.draw();
- s_fire.draw();
- s_birds.draw();
- s_crickets.draw();
- s_auxiliar.draw();
- SDL_RenderPresent(gRenderer);
- }
-
- if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_EXPOSED){
- minimized = false;
- SDL_SetRenderDrawColor(gRenderer, BACKGROUND_COLOR);
- SDL_RenderClear(gRenderer);
- s_rain.draw();
- s_thunder.draw();
- s_waves.draw();
- s_wind.draw();
- s_fire.draw();
- s_birds.draw();
- s_crickets.draw();
- s_auxiliar.draw();
- SDL_RenderPresent(gRenderer);
- }
- if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_MINIMIZED)
- minimized = true;
- if(event.type == SDL_KEYDOWN){
- switch(event.key.keysym.sym){
- case SDLK_ESCAPE: running = 0; break;
- case SDLK_m:{
- ++auxiliar_songs;
- auxiliar_songs = auxiliar_songs % MAX_AUX_VALUE;
- SDL_RenderClear(gRenderer);
- auxiliar.change_song(auxiliar_songs_name[auxiliar_songs], s_auxiliar.get_value());
- s_auxiliar.set_image(auxiliar_images_name[auxiliar_songs]);
- s_rain.draw();
- s_thunder.draw();
- s_waves.draw();
- s_wind.draw();
- s_fire.draw();
- s_birds.draw();
- s_crickets.draw();
- s_auxiliar.draw();
- SDL_RenderPresent(gRenderer);
- SDL_Delay(10);
- };break;
- default: break;
- }
- }
- }
- if(!minimized){
- if(mouse_follow){
- SDL_RenderClear(gRenderer);
-
- s_rain.update(mouse.y-mouse_offset.y);
- s_thunder.update(mouse.y-mouse_offset.y);
- s_waves.update(mouse.y-mouse_offset.y);
- s_wind.update(mouse.y-mouse_offset.y);
- s_fire.update(mouse.y-mouse_offset.y);
- s_birds.update(mouse.y-mouse_offset.y);
- s_crickets.update(mouse.y-mouse_offset.y);
- s_auxiliar.update(mouse.y-mouse_offset.y);
- s_rain.draw();
- s_thunder.draw();
- s_waves.draw();
- s_wind.draw();
- s_fire.draw();
- s_birds.draw();
- s_crickets.draw();
- s_auxiliar.draw();
- rain.set_volume(s_rain.get_value());
- thunder.set_volume(s_thunder.get_value());
- waves.set_volume(s_waves.get_value());
- wind.set_volume(s_wind.get_value());
- fire.set_volume(s_fire.get_value());
- birds.set_volume(s_birds.get_value());
- crickets.set_volume(s_crickets.get_value());
- auxiliar.set_volume(s_auxiliar.get_value());
-
- SDL_RenderPresent(gRenderer);
- SDL_Delay(10);
- }else
- SDL_Delay(50);
- }else{
-
- SDL_WaitEvent(&event);
- minimized = false;
- }
- }
- }
|