Lesson 2 done + little synopsis change
This commit is contained in:
parent
8a4fc241e2
commit
3f13f0f11d
BIN
testSDL/Lesson2/-.-
Executable file
BIN
testSDL/Lesson2/-.-
Executable file
Binary file not shown.
BIN
testSDL/Lesson2/background.bmp
Normal file
BIN
testSDL/Lesson2/background.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 225 KiB |
BIN
testSDL/Lesson2/hello.bmp
Normal file
BIN
testSDL/Lesson2/hello.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 900 KiB |
BIN
testSDL/Lesson2/image.bmp
Normal file
BIN
testSDL/Lesson2/image.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 117 KiB |
@ -2,52 +2,87 @@
|
||||
#include <iostream>
|
||||
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 )
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
|
||||
{
|
||||
cout << "SDL_Init error\n";
|
||||
cout << SDL_GetError() << endl;
|
||||
return 1;
|
||||
}
|
||||
SDL_Window* window = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
|
||||
if (window == nullptr)
|
||||
win = SDL_CreateWindow("Lesson 2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screen_width, scren_height, SDL_WINDOW_SHOWN);
|
||||
if (win == NULL)
|
||||
{
|
||||
cout << "Window create error\n";
|
||||
return 1;
|
||||
cout << SDL_GetError() << endl;
|
||||
return 2;
|
||||
}
|
||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED || SDL_RENDERER_PRESENTVSYNC);
|
||||
if (renderer == nullptr)
|
||||
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
if (ren == NULL)
|
||||
{
|
||||
cout << "Renderer error\n";
|
||||
return 1;
|
||||
cout << SDL_GetError() << endl;
|
||||
return 3;
|
||||
}
|
||||
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;
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user