Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Compile with g++ main.cpp -lSDL2 -lSDL2_image -o 'Morshu Game'
- #include <iostream>
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_image.h>
- #define FAILURE 1
- #define SUCCESS 0
- #define CENTER_X 1920/2
- #define CENTER_Y 1080/2
- #define morshuRoom 0
- #define mainMenuText 1
- #define startGameText 2
- #define cityBackground 3
- #define whatWillYouDoText 4
- #define mainMenu 0
- #define roadScene 1
- int images[5];
- int imageIndex = 0;
- bool running = true;
- bool fullscreen = false;
- SDL_Window* window = nullptr;
- SDL_Renderer* renderer = nullptr;
- SDL_Texture* backgroundTexture = nullptr;
- SDL_Texture* imageSlot1 = nullptr;
- SDL_Texture* imageSlot2 = nullptr;
- SDL_Texture* imageSlot3 = nullptr;
- SDL_Texture* imageSlot4 = nullptr;
- SDL_Texture* imageSlot5 = nullptr;
- SDL_Rect imageRect1;
- SDL_Rect imageRect2;
- SDL_Rect imageRect3;
- SDL_Rect imageRect4;
- SDL_Rect imageRect5;
- SDL_Event event;
- int scene = mainMenu;
- int createWindow()
- {
- if(SDL_Init(SDL_INIT_EVERYTHING) != 0){return FAILURE;}
- if(IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG){return FAILURE;}
- window = SDL_CreateWindow("Morshu Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1920, 1080, SDL_WINDOW_SHOWN);
- if(!window){return FAILURE;}
- renderer = SDL_CreateRenderer(window, -1, 0);
- if(!renderer){return FAILURE;}
- return SUCCESS;
- }
- void quit()
- {
- SDL_DestroyTexture(imageSlot1);
- SDL_DestroyTexture(imageSlot2);
- SDL_DestroyTexture(imageSlot3);
- SDL_DestroyTexture(imageSlot4);
- SDL_DestroyTexture(imageSlot5);
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- IMG_Quit();
- SDL_Quit();
- running = false;
- }
- void getInput()
- {
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- quit();
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- quit();
- break;
- case SDLK_F4:
- if(fullscreen == false)
- {
- SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
- fullscreen = true;
- }
- else if(fullscreen == true)
- {
- SDL_SetWindowFullscreen(window, 0);
- fullscreen = false;
- }
- break;
- case SDLK_RETURN:
- scene = roadScene;
- break;
- default:
- break;
- }
- }
- }
- }
- void loadImage(int sprite, int posX, int posY, int sizeX, int sizeY)
- {
- SDL_Surface* image1;
- SDL_Surface* image2;
- SDL_Surface* image3;
- SDL_Surface* image4;
- SDL_Surface* image5;
- switch(imageIndex)
- {
- case 0:
- switch(sprite)
- {
- case mainMenuText:
- imageRect1.w = sizeX;
- imageRect1.h = sizeY;
- imageRect1.x = posX+sizeX/2;
- imageRect1.y = posY-sizeY/2;
- image1 = IMG_Load("./Assets/Sprites/mainMenuText.png");
- images[0] = mainMenuText;
- imageSlot1 = SDL_CreateTextureFromSurface(renderer, image1);
- SDL_DestroyTexture(imageSlot1);
- imageIndex++;
- break;
- case startGameText:
- imageRect1.w = sizeX;
- imageRect1.h = sizeY;
- imageRect1.x = posX+sizeX/2;
- imageRect1.y = posY-sizeY/2;
- image1 = IMG_Load("./Assets/Sprites/mainMenuText.png");
- images[0] = mainMenuText;
- imageSlot1 = SDL_CreateTextureFromSurface(renderer, image1);
- SDL_DestroyTexture(imageSlot1);
- imageIndex++;
- break;
- case whatWillYouDoText:
- imageRect1.w = sizeX;
- imageRect1.h = sizeY;
- imageRect1.x = posX+sizeX/2;
- imageRect1.y = posY-sizeY/2;
- image1 = IMG_Load("./Assets/Sprites/whatWillYouDoText.png");
- images[0] = mainMenuText;
- imageSlot1 = SDL_CreateTextureFromSurface(renderer, image1);
- SDL_DestroyTexture(imageSlot1);
- imageIndex++;
- break;
- default:
- break;
- }
- break;
- case 1:
- switch(sprite)
- {
- case mainMenuText:
- imageRect2.w = sizeX;
- imageRect2.h = sizeY;
- imageRect2.x = posX+sizeX/2;
- imageRect2.y = posY-sizeY/2;
- image2 = IMG_Load("./Assets/Sprites/mainMenuText.png");
- images[1] = mainMenuText;
- imageSlot2 = SDL_CreateTextureFromSurface(renderer, image2);
- SDL_DestroyTexture(imageSlot2);
- imageIndex++;
- break;
- case startGameText:
- imageRect2.w = sizeX;
- imageRect2.h = sizeY;
- imageRect2.x = posX+sizeX/2;
- imageRect2.y = posY-sizeY/2;
- image2 = IMG_Load("./Assets/Sprites/startGameText.png");
- images[0] = mainMenuText;
- imageSlot2 = SDL_CreateTextureFromSurface(renderer, image2);
- SDL_DestroyTexture(imageSlot2);
- imageIndex++;
- break;
- case whatWillYouDoText:
- imageRect2.w = sizeX;
- imageRect2.h = sizeY;
- imageRect2.x = posX+sizeX/2;
- imageRect2.y = posY-sizeY/2;
- image2 = IMG_Load("./Assets/Sprites/whatWillYouDoText.png");
- images[1] = mainMenuText;
- imageSlot2 = SDL_CreateTextureFromSurface(renderer, image2);
- SDL_DestroyTexture(imageSlot2);
- imageIndex++;
- break;
- default:
- break;
- }
- break;
- case 2:
- switch(sprite)
- {
- case mainMenuText:
- imageRect3.w = sizeX;
- imageRect3.h = sizeY;
- imageRect3.x = posX+sizeX/2;
- imageRect3.y = posY-sizeY/2;
- image3 = IMG_Load("./Assets/Sprites/mainMenuText.png");
- images[2] = mainMenuText;
- imageSlot3 = SDL_CreateTextureFromSurface(renderer, image3);
- SDL_DestroyTexture(imageSlot3);
- imageIndex++;
- break;
- case startGameText:
- imageRect3.w = sizeX;
- imageRect3.h = sizeY;
- imageRect3.x = posX+sizeX/2;
- imageRect3.y = posY-sizeY/2;
- image3 = IMG_Load("./Assets/Sprites/startGameText.png");
- images[2] = mainMenuText;
- imageSlot3 = SDL_CreateTextureFromSurface(renderer, image3);
- SDL_DestroyTexture(imageSlot3);
- imageIndex++;
- break;
- case whatWillYouDoText:
- imageRect3.w = sizeX;
- imageRect3.h = sizeY;
- imageRect3.x = posX+sizeX/2;
- imageRect3.y = posY-sizeY/2;
- image3 = IMG_Load("./Assets/Sprites/whatWillYouDoText.png");
- images[2] = mainMenuText;
- imageSlot3 = SDL_CreateTextureFromSurface(renderer, image3);
- SDL_DestroyTexture(imageSlot3);
- imageIndex++;
- break;
- default:
- break;
- }
- break;
- case 3:
- switch(sprite)
- {
- case mainMenuText:
- imageRect4.w = sizeX;
- imageRect4.h = sizeY;
- imageRect4.x = posX+sizeX/2;
- imageRect4.y = posY-sizeY/2;
- image4 = IMG_Load("./Assets/Sprites/mainMenuText.png");
- images[3] = mainMenuText;
- imageSlot4 = SDL_CreateTextureFromSurface(renderer, image4);
- SDL_DestroyTexture(imageSlot4);
- imageIndex++;
- break;
- case startGameText:
- imageRect4.w = sizeX;
- imageRect4.h = sizeY;
- imageRect4.x = posX+sizeX/2;
- imageRect4.y = posY-sizeY/2;
- image1 = IMG_Load("./Assets/Sprites/startGameText.png");
- images[3] = mainMenuText;
- imageSlot4 = SDL_CreateTextureFromSurface(renderer, image4);
- SDL_DestroyTexture(imageSlot4);
- imageIndex++;
- break;
- case whatWillYouDoText:
- imageRect4.w = sizeX;
- imageRect4.h = sizeY;
- imageRect4.x = posX+sizeX/2;
- imageRect4.y = posY-sizeY/2;
- image4 = IMG_Load("./Assets/Sprites/whatWillYouDoText.png");
- images[3] = mainMenuText;
- imageSlot4 = SDL_CreateTextureFromSurface(renderer, image4);
- SDL_DestroyTexture(imageSlot4);
- imageIndex++;
- break;
- default:
- break;
- }
- break;
- case 4:
- switch(sprite)
- {
- case mainMenuText:
- imageRect5.w = sizeX;
- imageRect5.h = sizeY;
- imageRect5.x = posX+sizeX/2;
- imageRect5.y = posY-sizeY/2;
- image5 = IMG_Load("./Assets/Sprites/mainMenuText.png");
- images[4] = mainMenuText;
- imageSlot5 = SDL_CreateTextureFromSurface(renderer, image5);
- SDL_DestroyTexture(imageSlot5);
- imageIndex++;
- break;
- case startGameText:
- imageRect5.w = sizeX;
- imageRect5.h = sizeY;
- imageRect5.x = posX+sizeX/2;
- imageRect5.y = posY-sizeY/2;
- image5 = IMG_Load("./Assets/Sprites/startGameText.png");
- images[4] = mainMenuText;
- imageSlot5 = SDL_CreateTextureFromSurface(renderer, image5);
- SDL_DestroyTexture(imageSlot5);
- imageIndex++;
- break;
- case whatWillYouDoText:
- imageRect5.w = sizeX;
- imageRect5.h = sizeY;
- imageRect5.x = posX+sizeX/2;
- imageRect5.y = posY-sizeY/2;
- image5 = IMG_Load("./Assets/Sprites/whatWillYouDoText.png");
- images[4] = mainMenuText;
- imageSlot5 = SDL_CreateTextureFromSurface(renderer, image5);
- SDL_DestroyTexture(imageSlot5);
- imageIndex++;
- break;
- default:
- break;
- }
- break;
- }
- }
- void renderScreen()
- {
- SDL_RenderClear(renderer);
- SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
- 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_RenderPresent(renderer);
- }
- void changeBackground(int sprite)
- {
- SDL_Surface* image;
- switch(sprite)
- {
- case morshuRoom:
- image = IMG_Load("./Assets/Backgrounds/morshuRoom.png");
- break;
- case cityBackground:
- image = IMG_Load("./Assets/Backgrounds/cityBackground.png");
- break;
- default:
- break;
- }
- backgroundTexture = SDL_CreateTextureFromSurface(renderer, image);
- }
- void clearScene()
- {
- SDL_DestroyTexture(imageSlot1);
- SDL_DestroyTexture(imageSlot2);
- SDL_DestroyTexture(imageSlot3);
- SDL_DestroyTexture(imageSlot4);
- SDL_DestroyTexture(imageSlot5);
- imageSlot1 = nullptr;
- imageSlot2 = nullptr;
- imageSlot3 = nullptr;
- imageSlot4 = nullptr;
- imageSlot5 = nullptr;
- backgroundTexture = nullptr;
- }
- void updateScreen()
- {
- changeBackground(morshuRoom);
- loadImage(mainMenuText, 210, 175, 800, 200);
- loadImage(startGameText, 300, 700, 700, 175);
- if(scene == roadScene)
- {
- clearScene();
- changeBackground(cityBackground);
- loadImage(whatWillYouDoText, 210, 175, 900, 275);
- }
- }
- int main()
- {
- if(FAILURE == createWindow()){quit();}
- while(running)
- {
- getInput();
- updateScreen();
- renderScreen();
- }
- return SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement