main.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 BACKGROUND_COLOR 33,27,81,255
  22. const int WINDOW_X_POSITION = 450;
  23. const int WINDOW_Y_POSITION = 380;
  24. const int CLOCK_X_POSITION = 160;
  25. const int CLOCK_Y_POSITION = 130;
  26. const int BAR_X_POSITION = 50;
  27. const int BAR_Y_POSITION = 275;
  28. using namespace std;
  29. SDL_Point mouse;
  30. bool mouse_follow = false;
  31. SDL_Point mouse_offset;
  32. int auxiliar_songs = 0;
  33. #define MAX_AUX_VALUE 2
  34. int main(){
  35. if(SDL_Init(SDL_INIT_VIDEO) < 0)
  36. cout << "SDL not working " << SDL_GetError() << endl;
  37. SDL_Window * gWindow = SDL_CreateWindow("Ad contemplationem", SDL_WINDOWPOS_UNDEFINED,
  38. SDL_WINDOWPOS_UNDEFINED, WINDOW_X_POSITION, WINDOW_Y_POSITION, 0);
  39. if(gWindow == NULL) {
  40. cout << "Render did not start" << SDL_GetError() << endl;
  41. return 1;
  42. }
  43. SDL_Renderer * gRenderer = SDL_CreateRenderer(gWindow, -1,
  44. SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  45. if(gRenderer == NULL){
  46. cout << "Render did not start" << SDL_GetError() << endl;
  47. return 1;
  48. }
  49. SDL_Surface * icon_surface = IMG_Load("icons/icon.png");
  50. SDL_SetWindowIcon(gWindow, icon_surface);
  51. SDL_Event event;
  52. if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) == -1){
  53. cout << "Audio did not start " << SDL_GetError() << endl;
  54. return 1;
  55. }
  56. int flags = 0;
  57. int init = Mix_Init(flags);
  58. if((init&flags) != flags) {
  59. printf("Mix_Init: Failed to init Mixer!\n");
  60. printf("Mix_Init: %s\n", Mix_GetError());
  61. }
  62. string auxiliar_songs_name[] = {
  63. "sound/heartbeat.wav",
  64. "sound/fountain.wav" // Madrid fountain. Awesome :)
  65. };
  66. string auxiliar_images_name[] = {"icons/heart.png", "icons/fountain.png"};
  67. int running = 1;
  68. /* Start loading screen */
  69. SDL_SetRenderDrawColor(gRenderer, BACKGROUND_COLOR);
  70. SDL_RenderClear(gRenderer);
  71. SDL_Rect s1;
  72. s1.x = CLOCK_X_POSITION;
  73. s1.y = CLOCK_Y_POSITION;
  74. SDL_Surface * temp_surface;
  75. temp_surface = IMG_Load("icons/clock.png");
  76. if( temp_surface == NULL ){
  77. cout << "Icon (icons/clock.png) couldn't be loaded" << endl;
  78. return 1;
  79. }
  80. SDL_Texture * fillbar = SDL_CreateTextureFromSurface(gRenderer, temp_surface);
  81. if( fillbar == NULL ){
  82. cout << "Fillbar couldn't be created" << endl;
  83. return 1;
  84. }
  85. SDL_QueryTexture(fillbar, NULL, NULL, &s1.w, &s1.h);
  86. SDL_RenderCopy(gRenderer, fillbar, NULL, &s1);
  87. SDL_DestroyTexture(fillbar);
  88. SDL_FreeSurface(temp_surface);
  89. SDL_RenderPresent(gRenderer);
  90. /* Load audio on background */
  91. song rain("sound/rain.wav");
  92. song thunder("sound/thunder.wav");
  93. song waves("sound/waves.wav");
  94. song wind("sound/wind.wav");
  95. song fire("sound/fire.wav");
  96. song birds("sound/birds.wav");
  97. song crickets("sound/crickets.wav");
  98. song auxiliar(auxiliar_songs_name[0]);
  99. /* End load screen */
  100. SDL_SetRenderDrawColor(gRenderer, BACKGROUND_COLOR);
  101. SDL_RenderClear(gRenderer);
  102. /* Start sliders and icons */
  103. slider s_rain(gRenderer, BAR_X_POSITION, BAR_Y_POSITION);
  104. slider s_thunder(gRenderer, BAR_X_POSITION + 50, BAR_Y_POSITION);
  105. slider s_waves(gRenderer, BAR_X_POSITION + 100, BAR_Y_POSITION);
  106. slider s_wind(gRenderer, BAR_X_POSITION + 150, BAR_Y_POSITION);
  107. slider s_fire(gRenderer, BAR_X_POSITION + 200, BAR_Y_POSITION);
  108. slider s_birds(gRenderer, BAR_X_POSITION + 250, BAR_Y_POSITION);
  109. slider s_crickets(gRenderer, BAR_X_POSITION + 300, BAR_Y_POSITION);
  110. slider s_auxiliar(gRenderer, BAR_X_POSITION + 350, BAR_Y_POSITION);
  111. s_rain.set_image("icons/rain.png");
  112. s_thunder.set_image("icons/flash.png");
  113. s_waves.set_image("icons/waves.png");
  114. s_wind.set_image("icons/wind.png");
  115. s_fire.set_image("icons/fire.png");
  116. s_birds.set_image("icons/bird.png");
  117. s_crickets.set_image("icons/fountain.png");
  118. s_auxiliar.set_image("icons/heart.png");
  119. SDL_RenderPresent(gRenderer);
  120. bool minimized = false;
  121. while(running){
  122. SDL_GetMouseState(&mouse.x, &mouse.y);
  123. while(SDL_PollEvent(&event) != 0){
  124. if(event.type == SDL_QUIT)
  125. running = 0;
  126. if(event.button.button == SDL_BUTTON_LEFT){
  127. if(event.type == SDL_MOUSEBUTTONDOWN){
  128. mouse_follow = true;
  129. s_rain.test_mouse_follow(&mouse,&mouse_offset);
  130. s_thunder.test_mouse_follow(&mouse,&mouse_offset);
  131. s_waves.test_mouse_follow(&mouse,&mouse_offset);
  132. s_wind.test_mouse_follow(&mouse,&mouse_offset);
  133. s_fire.test_mouse_follow(&mouse,&mouse_offset);
  134. s_birds.test_mouse_follow(&mouse,&mouse_offset);
  135. s_crickets.test_mouse_follow(&mouse,&mouse_offset);
  136. s_auxiliar.test_mouse_follow(&mouse,&mouse_offset);
  137. }
  138. if(event.type == SDL_MOUSEBUTTONUP){
  139. mouse_follow = false;
  140. s_rain.mouse_follow_off();
  141. s_thunder.mouse_follow_off();
  142. s_waves.mouse_follow_off();
  143. s_wind.mouse_follow_off();
  144. s_fire.mouse_follow_off();
  145. s_birds.mouse_follow_off();
  146. s_crickets.mouse_follow_off();
  147. s_auxiliar.mouse_follow_off();
  148. }
  149. }
  150. if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESTORED){
  151. minimized = false;
  152. SDL_SetRenderDrawColor(gRenderer, BACKGROUND_COLOR);
  153. SDL_RenderClear(gRenderer);
  154. s_rain.draw();
  155. s_thunder.draw();
  156. s_waves.draw();
  157. s_wind.draw();
  158. s_fire.draw();
  159. s_birds.draw();
  160. s_crickets.draw();
  161. s_auxiliar.draw();
  162. SDL_RenderPresent(gRenderer);
  163. }
  164. if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_EXPOSED){
  165. minimized = false;
  166. SDL_SetRenderDrawColor(gRenderer, BACKGROUND_COLOR);
  167. SDL_RenderClear(gRenderer);
  168. s_rain.draw();
  169. s_thunder.draw();
  170. s_waves.draw();
  171. s_wind.draw();
  172. s_fire.draw();
  173. s_birds.draw();
  174. s_crickets.draw();
  175. s_auxiliar.draw();
  176. SDL_RenderPresent(gRenderer);
  177. }
  178. if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_MINIMIZED)
  179. minimized = true;
  180. if(event.type == SDL_KEYDOWN){
  181. switch(event.key.keysym.sym){
  182. case SDLK_ESCAPE: running = 0; break;
  183. case SDLK_m:{
  184. ++auxiliar_songs;
  185. auxiliar_songs = auxiliar_songs % MAX_AUX_VALUE;
  186. SDL_RenderClear(gRenderer);
  187. auxiliar.change_song(auxiliar_songs_name[auxiliar_songs], s_auxiliar.get_value());
  188. s_auxiliar.set_image(auxiliar_images_name[auxiliar_songs]);
  189. s_rain.draw();
  190. s_thunder.draw();
  191. s_waves.draw();
  192. s_wind.draw();
  193. s_fire.draw();
  194. s_birds.draw();
  195. s_crickets.draw();
  196. s_auxiliar.draw();
  197. SDL_RenderPresent(gRenderer);
  198. SDL_Delay(10);
  199. };break;
  200. default: break;
  201. }
  202. }
  203. }
  204. if(!minimized){
  205. if(mouse_follow){
  206. SDL_RenderClear(gRenderer);
  207. s_rain.update(mouse.y-mouse_offset.y);
  208. s_thunder.update(mouse.y-mouse_offset.y);
  209. s_waves.update(mouse.y-mouse_offset.y);
  210. s_wind.update(mouse.y-mouse_offset.y);
  211. s_fire.update(mouse.y-mouse_offset.y);
  212. s_birds.update(mouse.y-mouse_offset.y);
  213. s_crickets.update(mouse.y-mouse_offset.y);
  214. s_auxiliar.update(mouse.y-mouse_offset.y);
  215. s_rain.draw();
  216. s_thunder.draw();
  217. s_waves.draw();
  218. s_wind.draw();
  219. s_fire.draw();
  220. s_birds.draw();
  221. s_crickets.draw();
  222. s_auxiliar.draw();
  223. rain.set_volume(s_rain.get_value());
  224. thunder.set_volume(s_thunder.get_value());
  225. waves.set_volume(s_waves.get_value());
  226. wind.set_volume(s_wind.get_value());
  227. fire.set_volume(s_fire.get_value());
  228. birds.set_volume(s_birds.get_value());
  229. crickets.set_volume(s_crickets.get_value());
  230. auxiliar.set_volume(s_auxiliar.get_value());
  231. SDL_RenderPresent(gRenderer);
  232. SDL_Delay(10);
  233. }else
  234. SDL_Delay(50);
  235. }else{
  236. SDL_WaitEvent(&event);
  237. minimized = false;
  238. }
  239. }
  240. }