_coursrach/testSDL/Lesson2/main.cpp
2018-12-05 00:35:13 +03:00

88 lines
2.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
const int screen_width = 640;
const int scren_height = 480;
SDL_Window* win = NULL;
SDL_Renderer* ren = NULL;
SDL_Texture* LoadImage(const char* path)
{
SDL_Surface* loadedImage = NULL;
SDL_Texture* texture = NULL;
loadedImage = SDL_LoadBMP(path);
if (loadedImage != 0)
{
texture = SDL_CreateTextureFromSurface(ren, loadedImage);
SDL_FreeSurface(loadedImage);
}
else
cout << SDL_GetError() << endl;
return texture;
}
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); //В нужной точке рисуем текстуру с оригинальной высотой и шириной
}
int main()
{
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();
return 0;
}