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 -Wall -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 buttons = false;
- SDL_Point mousePosition;
- SDL_Rect button1;
- SDL_Rect button2;
- SDL_Rect button3;
- bool loadedScenes[2] = {false, false};
- bool mouseInsideButton1 = false;
- bool mouseInsideButton2 = false;
- bool mouseInsideButton3 = false;
- bool playGrown = false;
- bool playShrunk = true;
- bool wimpGrown = false;
- bool wimpShrunk = true;
- bool leftMouseDown = false;
- int buttonIndex = 1;
- int scene = 1;
- int playerX = 300;
- int playerY = 653;
- int gravity = 1;
- bool gravityOn;
- const char* mainMenuBackground = "Assets/Backgrounds/mainMenuBackground.png";
- const char* bedroomBackground = "Assets/Backgrounds/bedroomBackground.png";
- const char* playButton = "Assets/Sprites/playButton.png";
- const char* wimpButton = "Assets/Sprites/wimpButton.png";
- const char* playButtonBig = "Assets/Sprites/playButtonBig.png";
- const char* wimpButtonBig = "Assets/Sprites/wimpButtonBig.png";
- const char* woodFloor = "Assets/Sprites/woodFloor.png";
- const char* bed = "Assets/Sprites/bed.png";
- const char* arthur = "Assets/Sprites/arthur.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);
- Mix_VolumeMusic(400);
- window = SDL_CreateWindow("Butter Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_FULLSCREEN);
- 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);
- exit(1);
- }
- void createButton(int width, int height, int posX, int posY)
- {
- if(buttonIndex == 1)
- {
- button1.x = posX;
- button1.y = posY;
- button1.w = width;
- button1.h = height;
- }
- else if(buttonIndex == 2)
- {
- button2.x = posX;
- button2.y = posY;
- button2.w = width;
- button2.h = height;
- }
- else if(buttonIndex == 3)
- {
- button3.x = posX;
- button3.y = posY;
- button3.w = width;
- button3.h = height;
- }
- buttons = true;
- buttonIndex++;
- }
- void deleteButtons()
- {
- buttonIndex = 1;
- button1 = {0, 0, 0, 0};
- button2 = {0, 0, 0, 0};
- button3 = {0, 0, 0, 0};
- }
- void getInput()
- {
- SDL_Event event;
- SDL_GetMouseState(&mousePosition.x, &mousePosition.y);
- mouseInsideButton1 = SDL_PointInRect(&mousePosition, &button1);
- mouseInsideButton2 = SDL_PointInRect(&mousePosition, &button2);
- mouseInsideButton3 = SDL_PointInRect(&mousePosition, &button3);
- while(SDL_PollEvent(&event))
- {
- if(event.button.button != SDL_BUTTON_LEFT)
- {
- leftMouseDown = false;
- }
- switch(event.type)
- {
- case SDL_MOUSEBUTTONDOWN:
- if(event.button.button == SDL_BUTTON_LEFT)
- {
- leftMouseDown = true;
- }
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- }
- break;
- case SDL_QUIT:
- quit();
- break;
- break;
- }
- }
- }
- void queueVisualEvents()
- {
- if(scene == 1 && loadedScenes[0] != true)
- {
- playMusic(mainMenuMusic, -1);
- loadImage(mainMenuBackground, 0, 0, 1920, 1080);
- loadImage(playButton, 1400, 270, 340, 170);
- loadImage(wimpButton, 1400, 540, 340, 170);
- createButton(340, 170, 1400, 270);
- createButton(340, 170, 1400, 540);
- loadedScenes[0] = true;
- }
- if(scene == 1)
- {
- if(mouseInsideButton1)
- {
- if(!playGrown)
- {
- freeImageSlot(2);
- loadSpecificImageSlot(playButtonBig, 1374, 244, 391, 196, 2);
- playGrown = true;
- playShrunk = false;
- }
- if(leftMouseDown)
- {
- clearScene();
- scene = 2;
- SDL_Delay(700);
- }
- }
- if(!mouseInsideButton1)
- {
- if(!playShrunk)
- {
- freeImageSlot(2);
- loadSpecificImageSlot(playButton, 1400, 270, 340, 170, 2);
- playShrunk = true;
- playGrown = false;
- }
- }
- if(mouseInsideButton2)
- {
- if(!wimpGrown)
- {
- freeImageSlot(3);
- loadSpecificImageSlot(wimpButtonBig, 1374, 514, 391, 196, 3);
- wimpGrown = true;
- wimpShrunk = false;
- }
- if(leftMouseDown)
- {
- SDL_Delay(700);
- quit();
- SDL_Delay(700);
- }
- }
- if(!mouseInsideButton2)
- {
- if(!wimpShrunk)
- {
- freeImageSlot(3);
- loadSpecificImageSlot(wimpButton, 1400, 540, 340, 170, 3);
- wimpShrunk = true;
- wimpGrown = false;
- }
- }
- }
- if(scene == 2)
- {
- if(loadedScenes[1] != true)
- {
- Mix_HaltMusic();
- loadImage(bedroomBackground, CENTER, CENTER, 1920, 1080);
- loadImage(woodFloor, CENTER, 1029, 1920, 51);
- loadImage(bed, 50, 546, 555, 503);
- loadedScenes[1] = true;
- }
- int imageIndexHolder;
- imageIndexHolder = loadImage(arthur, playerX, playerY, 118, 174);
- imageIndexHolder--;
- freeImageSlot(imageIndexHolder);
- playerY += gravity;
- gravity++;
- }
- }
- 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