diff --git a/testSDL/Lesson2/-.- b/testSDL/Lesson2/-.- new file mode 100755 index 0000000..8e933d7 Binary files /dev/null and b/testSDL/Lesson2/-.- differ diff --git a/testSDL/Lesson2/background.bmp b/testSDL/Lesson2/background.bmp new file mode 100644 index 0000000..a8cf3c8 Binary files /dev/null and b/testSDL/Lesson2/background.bmp differ diff --git a/testSDL/Lesson2/hello.bmp b/testSDL/Lesson2/hello.bmp new file mode 100644 index 0000000..da8883e Binary files /dev/null and b/testSDL/Lesson2/hello.bmp differ diff --git a/testSDL/Lesson2/image.bmp b/testSDL/Lesson2/image.bmp new file mode 100644 index 0000000..340cdac Binary files /dev/null and b/testSDL/Lesson2/image.bmp differ diff --git a/testSDL/Lesson2/main.cpp b/testSDL/Lesson2/main.cpp index b5d62a0..23ee648 100644 --- a/testSDL/Lesson2/main.cpp +++ b/testSDL/Lesson2/main.cpp @@ -2,52 +2,87 @@ #include using namespace std; -const int screen_width ; -const int scren_height ; +const int screen_width = 640; +const int scren_height = 480; SDL_Window* win = NULL; -SDL_Renderer* renderer = NULL; +SDL_Renderer* ren = NULL; -SDL_Texture* LoadImage(char* path) +SDL_Texture* LoadImage(const char path[]) { SDL_Surface* loadedImage = NULL; SDL_Texture* texture = NULL; loadedImage = SDL_LoadBMP(path); if (loadedImage != 0) { - texture = SDL_CreateTextureFromSurface(renderer, loadedImage); + texture = SDL_CreateTextureFromSurface(ren, loadedImage); SDL_FreeSurface(loadedImage); } else - cout << SDL_GetError << endl; + 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) != 0 ) - { - cout << "SDL_Init error\n"; - return 1; - } - SDL_Window* window = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN); - if (window == nullptr) - { - cout << "Window create error\n"; - return 1; - } - SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED || SDL_RENDERER_PRESENTVSYNC); - if (renderer == nullptr) - { - cout << "Renderer error\n"; - return 1; - } - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, LoadImage("./hello.bmp"), NULL, NULL); - SDL_RenderPresent(renderer); - SDL_Delay(3000); - SDL_DestroyRenderer(renderer); - SDL_DestroyTexture(LoadImage("./hello.bmp")); - SDL_DestroyWindow(window); - SDL_Quit; + 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; } \ No newline at end of file