Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /Using SDL, SDL_image, standard IO, and strings
- #include <SDL.h>
- #include <SDL_image.h>
- #include <stdio.h>
- #include <string>
- #include<iostream>
- #include <vector>
- #include "Regal.h"
- #include"Warzywa.h"
- #include"Bohater.h"
- #include"Produkty.h"
- //Screen dimension constants
- const int SCREEN_WIDTH = 1270;
- const int SCREEN_HEIGHT = 730;
- //Starts up SDL and creates window
- bool init();
- //Loads media
- //bool loadMedia();
- Bohater moj(610, 670);
- std::vector<Produkty>produkty;
- //Frees media and shuts down SDL
- void close();
- //Current displayed image
- SDL_Surface* gCurrentSurface = NULL;
- //Loads individual image as texture
- //SDL_Texture* loadTexture( std::string path );
- //The window we'll be rendering to
- SDL_Window* gWindow = NULL;
- //The window renderer
- SDL_Renderer* gRenderer = NULL;
- std::string student = "student.jpg";
- std::string chleb_1 = "chleb1.jpg";
- std::string chleb_2 = "chleb2.jpg";
- std::string woda_1 = "woda1.jpg";
- std::string woda_2 = "woda2.jpg";
- std::string mleko_1 = "mleko1.jpg";
- std::string mleko_2 = "mleko2.jpg";
- std::string pomidor = "pomidor.jpg";
- std::string rzodkiew = "rzodkiew.jpg";
- std::string salata = "salata.jpg";
- std::string brokul = "brokul";
- //SDL_Texture* gTexture = NULL;
- std::vector<Regal>regaly;
- bool init()
- {
- //Initialization flag
- bool success = true;
- //Initialize SDL
- if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
- {
- printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
- success = false;
- }
- else
- {
- //Set texture filtering to linear
- if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
- {
- printf( "Warning: Linear texture filtering not enabled!" );
- }
- //Create window
- gWindow = SDL_CreateWindow( "Studenckie zakupy", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
- if( gWindow == NULL )
- {
- printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
- success = false;
- }
- else
- {
- //Create renderer for window
- gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
- if( gRenderer == NULL )
- {
- printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
- success = false;
- }
- else
- {
- //Initialize renderer color
- SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
- //Initialize PNG loading
- int imgFlags = IMG_INIT_PNG;
- if( !( IMG_Init( imgFlags ) & imgFlags ) )
- {
- printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
- success = false;
- }
- }
- }
- }
- return success;
- }
- /*bool loadMedia()
- {
- //Loading success flag
- bool success = true;
- std::string t = "student.jpg";
- gTexture = loadTexture(t);
- if (gTexture == NULL)
- {
- printf("Failed to load texture image!\n");
- success = false;
- }
- return success;
- }*/
- void close()
- {
- //Free loaded image
- //SDL_DestroyTexture( moj.obraz );
- //gTexture = NULL;
- //Destroy window
- SDL_DestroyRenderer( gRenderer );
- SDL_DestroyWindow( gWindow );
- gWindow = NULL;
- gRenderer = NULL;
- //Quit SDL subsystems
- IMG_Quit();
- SDL_Quit();
- }
- int main( int argc, char* args[] )
- {
- moj.WczytajO(student, gRenderer);
- Produkty chleb1(5, 5);
- chleb1.WczytajO(chleb_1, gRenderer);
- produkty.push_back(chleb1);
- Produkty chleb2(1020, 115);
- chleb2.WczytajO(chleb_2, gRenderer);
- produkty.push_back(chleb2);
- Produkty woda1(820, 560);
- woda1.WczytajO(woda_1, gRenderer);
- produkty.push_back(woda1);
- Produkty woda2(380, 230);
- woda2.WczytajO(woda_2, gRenderer);
- produkty.push_back(woda2);
- Produkty mleko1(200, 450);
- mleko1.WczytajO(mleko_1, gRenderer);
- produkty.push_back(mleko1);
- Produkty mleko2(1200, 300);
- mleko2.WczytajO(mleko_2, gRenderer);
- produkty.push_back(mleko2);
- double x, y, w, h;
- x = 80;
- y = 60;
- w = 500;
- h = 55;
- Regal lewy(300, 70, 53, 600);
- regaly.push_back(lewy);
- for (int i = 0; i < 7; i++)
- {
- Regal nowy(x, y, w, h);
- regaly.push_back(nowy);
- y = y + 111;
- }
- x = 680;
- y = 60;
- w = 500;
- h = 55;
- Regal prawy(910, 70, 53, 600);
- regaly.push_back(prawy);
- for (int i = 0; i < 7; i++)
- {
- Regal nowy(x, y, w, h);
- regaly.push_back(nowy);
- y = y + 111;
- }
- //Start up SDL and create window
- if( !init() )
- {
- printf( "Failed to initialize!\n" );
- }
- else
- {
- //Load media
- if (!moj.WczytajO(student, gRenderer) || !chleb1.WczytajO(chleb_1, gRenderer) || !chleb2.WczytajO(chleb_2, gRenderer)
- || !woda1.WczytajO(woda_1, gRenderer) || !woda2.WczytajO(woda_2, gRenderer) || !mleko1.WczytajO(mleko_1, gRenderer)
- || !mleko2.WczytajO(mleko_2, gRenderer))
- {
- printf( "Failed to load media!\n" );
- }
- else
- {
- //Main loop flag
- bool quit = false;
- //Event handler
- SDL_Event e;
- //While application is running
- while( !quit )
- {
- //Handle events on queue
- while (SDL_PollEvent(&e) != 0)
- {
- //User requests quit
- if (e.type == SDL_QUIT)
- {
- quit = true;
- }
- else if (e.type == SDL_KEYDOWN)
- {
- //Select surfaces based on key press
- switch (e.key.keysym.sym)
- {
- int a;
- case SDLK_UP:
- {
- a = 24;
- moj.Ruch(a);
- break;
- }
- case SDLK_DOWN:
- {
- a = 25;
- moj.Ruch(a);
- break;
- }
- case SDLK_LEFT:
- {
- a = 26;
- moj.Ruch(a);
- break;
- }
- case SDLK_RIGHT:
- {
- a = 27;
- moj.Ruch(a);
- break;
- }
- }
- }
- }
- //Clear screen
- SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
- SDL_RenderClear(gRenderer);
- for (int i = 0; i < regaly.size() - 1; i++)
- {
- regaly[i].Rysuj(gRenderer);
- }
- //Render texture to screen
- //Clear screen
- /*SDL_Rect wielkosc;
- wielkosc.x = 610;
- wielkosc.y = 670;
- wielkosc.w = 50;
- wielkosc.h = 50;*/
- //Render texture to screen
- moj.Wyswietl(gRenderer);
- chleb1.Wyswietl(gRenderer);
- chleb2.Wyswietl(gRenderer);
- woda1.Wyswietl(gRenderer);
- woda2.Wyswietl(gRenderer);
- mleko1.Wyswietl(gRenderer);
- mleko2.Wyswietl(gRenderer);
- //Update screen
- SDL_RenderPresent(gRenderer);
- }
- }
- }
- //Free resources and close SDL
- close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement