2018-12-04 00:29:47 +03:00
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2018-12-04 01:56:38 +03:00
|
|
|
|
const int screen_width = 640;
|
|
|
|
|
const int scren_height = 480;
|
2018-12-04 00:29:47 +03:00
|
|
|
|
SDL_Window* win = NULL;
|
2018-12-04 01:56:38 +03:00
|
|
|
|
SDL_Renderer* ren = NULL;
|
2018-12-04 00:29:47 +03:00
|
|
|
|
|
2018-12-04 01:56:38 +03:00
|
|
|
|
SDL_Texture* LoadImage(const char path[])
|
2018-12-04 00:29:47 +03:00
|
|
|
|
{
|
|
|
|
|
SDL_Surface* loadedImage = NULL;
|
|
|
|
|
SDL_Texture* texture = NULL;
|
|
|
|
|
loadedImage = SDL_LoadBMP(path);
|
|
|
|
|
if (loadedImage != 0)
|
|
|
|
|
{
|
2018-12-04 01:56:38 +03:00
|
|
|
|
texture = SDL_CreateTextureFromSurface(ren, loadedImage);
|
2018-12-04 00:29:47 +03:00
|
|
|
|
SDL_FreeSurface(loadedImage);
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-12-04 01:56:38 +03:00
|
|
|
|
cout << SDL_GetError() << endl;
|
2018-12-04 00:29:47 +03:00
|
|
|
|
return texture;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 01:56:38 +03:00
|
|
|
|
void ApplySurfaces(int x, int y, SDL_Texture* tex, SDL_Renderer* ren)
|
|
|
|
|
{
|
|
|
|
|
SDL_Rect pos;
|
|
|
|
|
pos.x = x;
|
|
|
|
|
pos.y = y;
|
|
|
|
|
SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h); //Функция заполнит pos.w и pos.h для формата текстуры 1:1
|
|
|
|
|
SDL_RenderCopy(ren, tex, NULL, &pos); //В нужной точке рисуем текстуру с оригинальной высотой и шириной
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 00:29:47 +03:00
|
|
|
|
int main()
|
|
|
|
|
{
|
2018-12-04 01:56:38 +03:00
|
|
|
|
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
|
|
|
|
|
{
|
|
|
|
|
cout << SDL_GetError() << endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
win = SDL_CreateWindow("Lesson 2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screen_width, scren_height, SDL_WINDOW_SHOWN);
|
|
|
|
|
if (win == NULL)
|
|
|
|
|
{
|
|
|
|
|
cout << SDL_GetError() << endl;
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
|
|
|
if (ren == NULL)
|
|
|
|
|
{
|
|
|
|
|
cout << SDL_GetError() << endl;
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
SDL_Texture *background = NULL, *image = nullptr;
|
|
|
|
|
image = LoadImage("./image.bmp");
|
|
|
|
|
background = LoadImage("./background.bmp");
|
|
|
|
|
if (image == NULL || background == NULL)
|
|
|
|
|
{
|
|
|
|
|
cout << "bad LoadImage\n";
|
|
|
|
|
return 4;
|
|
|
|
|
}
|
|
|
|
|
SDL_RenderClear(ren);
|
|
|
|
|
|
|
|
|
|
int bW, bH;
|
|
|
|
|
SDL_QueryTexture(background, NULL, NULL, &bW, &bH);
|
|
|
|
|
//cout << bW << " " << bH << endl;
|
|
|
|
|
ApplySurfaces( 0, 0, background, ren);
|
|
|
|
|
ApplySurfaces( bW, 0, background, ren);
|
|
|
|
|
ApplySurfaces( 0, bH, background, ren);
|
|
|
|
|
ApplySurfaces( bW, bH, background, ren);
|
|
|
|
|
|
|
|
|
|
int iW, iH;
|
|
|
|
|
SDL_QueryTexture(image, NULL, NULL, &iW, &iH);
|
|
|
|
|
int x = (screen_width - iW) / 2;
|
|
|
|
|
int y = (scren_height - iH) / 2;
|
|
|
|
|
|
|
|
|
|
ApplySurfaces(x, y, image, ren);
|
|
|
|
|
SDL_RenderPresent(ren);
|
|
|
|
|
|
|
|
|
|
SDL_Delay(10000);
|
|
|
|
|
|
|
|
|
|
SDL_DestroyTexture(background);
|
|
|
|
|
SDL_DestroyTexture(image);
|
|
|
|
|
SDL_DestroyRenderer(ren);
|
|
|
|
|
SDL_DestroyWindow(win);
|
|
|
|
|
|
|
|
|
|
SDL_Quit();
|
2018-12-04 00:29:47 +03:00
|
|
|
|
return 0;
|
|
|
|
|
}
|