main.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * This file is part of Soft Mood (https://notabug.org/alkeon/soft-mood).
  3. * Copyright (c) 2019 Alejandro "alkeon" Castilla
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <iostream>
  18. #include "SDL.h"
  19. #include "slider.h"
  20. #include "song.h"
  21. #define WHITE 255,255,255,255
  22. using namespace std;
  23. SDL_Point mouse;
  24. bool mouse_follow = false;
  25. SDL_Point mouse_offset;
  26. int auxiliar_songs = 0;
  27. #define MAX_AUX_VALUE 2
  28. int main(){
  29. if(SDL_Init(SDL_INIT_VIDEO) < 0)
  30. cout << "SDL not working " << SDL_GetError() << endl;
  31. SDL_Window * gWindow = SDL_CreateWindow("Soft mood", SDL_WINDOWPOS_UNDEFINED,
  32. SDL_WINDOWPOS_UNDEFINED, 700, 480, 0);
  33. if(gWindow == NULL){
  34. cout << "Render did not start" << SDL_GetError() << endl;
  35. return 1;
  36. }
  37. SDL_Renderer * gRenderer = SDL_CreateRenderer(gWindow, -1,
  38. SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  39. if(gRenderer == NULL){
  40. cout << "Render did not start" << SDL_GetError() << endl;
  41. return 1;
  42. }
  43. SDL_Event event;
  44. if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) == -1){
  45. cout << "Audio did not start " << SDL_GetError() << endl;
  46. return 1;
  47. }
  48. int flags = 0;
  49. int init = Mix_Init(flags);
  50. if((init&flags) != flags) {
  51. printf("Mix_Init: Failed to init Mixer!\n");
  52. printf("Mix_Init: %s\n", Mix_GetError());
  53. }
  54. string auxiliar_songs_name[] = {
  55. "sound/heartbeat.wav",
  56. "sound/fountain.wav" // Madrid fountain. Awesome :)
  57. };
  58. string auxiliar_images_name[] = {"icons/heart.png", "icons/fountain.png"};
  59. int running = 1;
  60. /* Start loading screen */
  61. SDL_SetRenderDrawColor(gRenderer, WHITE);
  62. SDL_RenderClear(gRenderer);
  63. SDL_Rect s1;
  64. s1.x = 286;
  65. s1.y = 176;
  66. SDL_Surface * temp_surface;
  67. temp_surface = IMG_Load("icons/clock.png");
  68. if( temp_surface == NULL ){
  69. cout << "Icon (icons/clock.png) couldn't be loaded" << endl;
  70. return 1;
  71. }
  72. SDL_Texture * fillbar = SDL_CreateTextureFromSurface(gRenderer, temp_surface);
  73. if( fillbar == NULL ){
  74. cout << "Fillbar couldn't be created" << endl;
  75. return 1;
  76. }
  77. SDL_QueryTexture(fillbar, NULL, NULL, &s1.w, &s1.h);
  78. SDL_RenderCopy(gRenderer, fillbar, NULL, &s1);
  79. SDL_DestroyTexture(fillbar);
  80. SDL_FreeSurface(temp_surface);
  81. SDL_RenderPresent(gRenderer);
  82. /* Load audio on background */
  83. song rain("sound/rain.wav");
  84. song thunder("sound/thunder.wav");
  85. song waves("sound/waves.wav");
  86. song wind("sound/wind.wav");
  87. song fire("sound/fire.wav");
  88. song birds("sound/birds.wav");
  89. song crickets("sound/crickets.wav");
  90. song auxiliar(auxiliar_songs_name[0]);
  91. /* End load screen */
  92. SDL_SetRenderDrawColor(gRenderer, WHITE);
  93. SDL_RenderClear(gRenderer);
  94. /* Start sliders and icons */
  95. slider s_rain(gRenderer, 50, 300);
  96. slider s_thunder(gRenderer, 100, 300);
  97. slider s_waves(gRenderer, 150, 300);
  98. slider s_wind(gRenderer, 200, 300);
  99. slider s_fire(gRenderer, 250, 300);
  100. slider s_birds(gRenderer, 300, 300);
  101. slider s_crickets(gRenderer, 350, 300);
  102. slider s_auxiliar(gRenderer, 400, 300);
  103. s_rain.set_image("icons/rain.png");
  104. s_thunder.set_image("icons/flash.png");
  105. s_waves.set_image("icons/waves.png");
  106. s_wind.set_image("icons/wind.png");
  107. s_fire.set_image("icons/fire.png");
  108. s_birds.set_image("icons/bird.png");
  109. s_crickets.set_image("icons/cricket.png"); // cricket player icon but sound is cricket bug
  110. s_auxiliar.set_image("icons/heart.png");
  111. SDL_RenderPresent(gRenderer);
  112. bool minimized = false;
  113. while(running){
  114. SDL_GetMouseState(&mouse.x, &mouse.y);
  115. while(SDL_PollEvent(&event) != 0){
  116. if(event.type == SDL_QUIT)
  117. running = 0;
  118. if(event.button.button == SDL_BUTTON_LEFT){
  119. if(event.type == SDL_MOUSEBUTTONDOWN){
  120. mouse_follow = true;
  121. s_rain.test_mouse_follow(&mouse,&mouse_offset);
  122. s_thunder.test_mouse_follow(&mouse,&mouse_offset);
  123. s_waves.test_mouse_follow(&mouse,&mouse_offset);
  124. s_wind.test_mouse_follow(&mouse,&mouse_offset);
  125. s_fire.test_mouse_follow(&mouse,&mouse_offset);
  126. s_birds.test_mouse_follow(&mouse,&mouse_offset);
  127. s_crickets.test_mouse_follow(&mouse,&mouse_offset);
  128. s_auxiliar.test_mouse_follow(&mouse,&mouse_offset);
  129. }
  130. if(event.type == SDL_MOUSEBUTTONUP){
  131. mouse_follow = false;
  132. s_rain.mouse_follow_off();
  133. s_thunder.mouse_follow_off();
  134. s_waves.mouse_follow_off();
  135. s_wind.mouse_follow_off();
  136. s_fire.mouse_follow_off();
  137. s_birds.mouse_follow_off();
  138. s_crickets.mouse_follow_off();
  139. s_auxiliar.mouse_follow_off();
  140. }
  141. }
  142. if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESTORED){
  143. minimized = false;
  144. SDL_SetRenderDrawColor(gRenderer, WHITE);
  145. SDL_RenderClear(gRenderer);
  146. s_rain.draw();
  147. s_thunder.draw();
  148. s_waves.draw();
  149. s_wind.draw();
  150. s_fire.draw();
  151. s_birds.draw();
  152. s_crickets.draw();
  153. s_auxiliar.draw();
  154. SDL_RenderPresent(gRenderer);
  155. }
  156. if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_MINIMIZED)
  157. minimized = true;
  158. if(event.type == SDL_KEYDOWN){
  159. switch(event.key.keysym.sym){
  160. case SDLK_ESCAPE: running = 0; break;
  161. case SDLK_m:{
  162. ++auxiliar_songs;
  163. auxiliar_songs = auxiliar_songs % MAX_AUX_VALUE;
  164. SDL_RenderClear(gRenderer);
  165. auxiliar.change_song(auxiliar_songs_name[auxiliar_songs], s_auxiliar.get_value());
  166. s_auxiliar.set_image(auxiliar_images_name[auxiliar_songs]);
  167. s_rain.draw();
  168. s_thunder.draw();
  169. s_waves.draw();
  170. s_wind.draw();
  171. s_fire.draw();
  172. s_birds.draw();
  173. s_crickets.draw();
  174. s_auxiliar.draw();
  175. SDL_RenderPresent(gRenderer);
  176. SDL_Delay(10);
  177. };break;
  178. default: break;
  179. }
  180. }
  181. }
  182. if(!minimized){
  183. if(mouse_follow){
  184. SDL_RenderClear(gRenderer);
  185. s_rain.update(mouse.y-mouse_offset.y);
  186. s_thunder.update(mouse.y-mouse_offset.y);
  187. s_waves.update(mouse.y-mouse_offset.y);
  188. s_wind.update(mouse.y-mouse_offset.y);
  189. s_fire.update(mouse.y-mouse_offset.y);
  190. s_birds.update(mouse.y-mouse_offset.y);
  191. s_crickets.update(mouse.y-mouse_offset.y);
  192. s_auxiliar.update(mouse.y-mouse_offset.y);
  193. s_rain.draw();
  194. s_thunder.draw();
  195. s_waves.draw();
  196. s_wind.draw();
  197. s_fire.draw();
  198. s_birds.draw();
  199. s_crickets.draw();
  200. s_auxiliar.draw();
  201. rain.set_volume(s_rain.get_value());
  202. thunder.set_volume(s_thunder.get_value());
  203. waves.set_volume(s_waves.get_value());
  204. wind.set_volume(s_wind.get_value());
  205. fire.set_volume(s_fire.get_value());
  206. birds.set_volume(s_birds.get_value());
  207. crickets.set_volume(s_crickets.get_value());
  208. auxiliar.set_volume(s_auxiliar.get_value());
  209. SDL_RenderPresent(gRenderer);
  210. SDL_Delay(10);
  211. }else
  212. SDL_Delay(50);
  213. }else{
  214. SDL_WaitEvent(&event);
  215. minimized = false;
  216. }
  217. }
  218. }