Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Snooping through the files are we?
- // Compile With: g++ main.cpp loadImage.h playSound.h -lSDL2 -lSDL2_image -lSDL2_mixer -o "Butter Game"
- #include <iostream>
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_image.h>
- #include "loadImage.h"
- #include "playSound.h"
- #define CENTER 0
- SDL_Window* window;
- SDL_Renderer* renderer;
- bool running;
- bool fullscreen = false;
- bool loadedScenes[2] = {false, false};
- bool buttons = false;
- bool touchingButton1 = false;
- bool touchingButton2 = false;
- bool touchingButton3 = false;
- int buttonX[6];
- int buttonY[6];
- int mouseX;
- int mouseY;
- int buttonIndex = 0;
- const char* mainMenuBackground = "Assets/Backgrounds/main_menu.png";
- const char* playButton = "Assets/Sprites/play_button.png";
- const char* wimpButton = "Assets/Sprites/wimp_button.png";
- const char* mainMenuMusic = "Assets/Music/mainMenuMusic.mp3";
- void createHDWindow()
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- IMG_Init(IMG_INIT_PNG);
- Mix_Init(MIX_INIT_MP3);
- Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
- window = SDL_CreateWindow("Butter Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_SHOWN);
- renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
- }
- int quit()
- {
- SDL_DestroyTexture(imageSlot1);
- SDL_DestroyTexture(imageSlot2);
- SDL_DestroyTexture(imageSlot3);
- SDL_DestroyTexture(imageSlot4);
- SDL_DestroyTexture(imageSlot5);
- SDL_DestroyTexture(imageSlot6);
- SDL_DestroyTexture(imageSlot7);
- SDL_DestroyTexture(imageSlot8);
- SDL_DestroyTexture(imageSlot9);
- SDL_DestroyTexture(imageSlot10);
- Mix_CloseAudio();
- SDL_DestroyWindow(window);
- SDL_DestroyRenderer(renderer);
- return -1;
- }
- void createButton(int oneX, int twoX, int oneY, int twoY)
- {
- buttonX[buttonIndex] = oneX;
- buttonX[buttonIndex++] = twoX;
- buttonY[buttonIndex] = oneY;
- buttonY[buttonIndex++] = twoY;
- buttonIndex += 2;
- buttons = true;
- }
- void deleteButtons()
- {
- buttonX[0] == NULL;
- buttonX[1] == NULL;
- buttonX[2] == NULL;
- buttonX[3] == NULL;
- buttonX[4] == NULL;
- buttonX[5] == NULL;
- buttons = false;
- }
- void getInput()
- {
- SDL_Event event;
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_MOUSEMOTION:
- SDL_GetMouseState(&mouseX, &mouseY);
- if(buttons)
- {
- if(mouseX > buttonX[0] && mouseX < buttonX[1] && mouseY > buttonY[0] && mouseY < buttonY[1])
- {
- touchingButton1 = true;
- std::cout << "Touching button one!" << std::endl;
- }
- else
- {
- touchingButton1 = false;
- }
- if(mouseX > buttonX[2] && mouseX < buttonX[3] && mouseY > buttonY[2] && mouseY < buttonY[3])
- {
- touchingButton2 = true;
- std::cout << "Touching button two!" << std::endl;
- }
- else
- {
- touchingButton2 = false;
- }
- if(mouseX > buttonX[4] && mouseX < buttonX[5] && mouseY > buttonY[4] && mouseY < buttonY[5])
- {
- touchingButton3 = true;
- std::cout << "Touching button three!" << std::endl;
- }
- else
- {
- touchingButton3 = false;
- }
- }
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_F4:
- if(fullscreen)
- {
- SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
- std::cout << "Not Fullscreen" << std::endl;
- fullscreen = false;
- }
- else if(!fullscreen)
- {
- SDL_SetWindowFullscreen(window, 0);
- std::cout << "Fullscreen" << std::endl;
- fullscreen = true;
- }
- break;
- }
- break;
- case SDL_QUIT:
- quit();
- break;
- break;
- }
- }
- }
- void queueVisualEvents()
- {
- if(loadedScenes[1] != true)
- {
- playMusic(mainMenuMusic, -1);
- loadImage(mainMenuBackground, 0, 0, 1920, 1080);
- loadImage(playButton, 1400, 270, 340, 170);
- loadImage(wimpButton, 1400, 540, 340, 170);
- createButton(1400, 1740, 270, 440);
- loadedScenes[1] = true;
- }
- }
- void renderScreen()
- {
- SDL_RenderClear(renderer);
- SDL_RenderCopy(renderer, imageSlot1, NULL, &imageRect1);
- SDL_RenderCopy(renderer, imageSlot2, NULL, &imageRect2);
- SDL_RenderCopy(renderer, imageSlot3, NULL, &imageRect3);
- SDL_RenderCopy(renderer, imageSlot4, NULL, &imageRect4);
- SDL_RenderCopy(renderer, imageSlot5, NULL, &imageRect5);
- SDL_RenderCopy(renderer, imageSlot6, NULL, &imageRect6);
- SDL_RenderCopy(renderer, imageSlot7, NULL, &imageRect7);
- SDL_RenderCopy(renderer, imageSlot8, NULL, &imageRect8);
- SDL_RenderCopy(renderer, imageSlot9, NULL, &imageRect9);
- SDL_RenderCopy(renderer, imageSlot10, NULL, &imageRect10);
- SDL_RenderPresent(renderer);
- }
- int main()
- {
- running = true;
- createHDWindow();
- while(running)
- {
- getInput();
- queueVisualEvents();
- renderScreen();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement