lesson 3
This commit is contained in:
parent
df3fa60b10
commit
da3c47e65b
Binary file not shown.
@ -7,7 +7,7 @@ const int scren_height = 480;
|
|||||||
SDL_Window* win = NULL;
|
SDL_Window* win = NULL;
|
||||||
SDL_Renderer* ren = NULL;
|
SDL_Renderer* ren = NULL;
|
||||||
|
|
||||||
SDL_Texture* LoadImage(const char path[])
|
SDL_Texture* LoadImage(const char* path)
|
||||||
{
|
{
|
||||||
SDL_Surface* loadedImage = NULL;
|
SDL_Surface* loadedImage = NULL;
|
||||||
SDL_Texture* texture = NULL;
|
SDL_Texture* texture = NULL;
|
||||||
@ -62,7 +62,7 @@ int main()
|
|||||||
|
|
||||||
int bW, bH;
|
int bW, bH;
|
||||||
SDL_QueryTexture(background, NULL, NULL, &bW, &bH);
|
SDL_QueryTexture(background, NULL, NULL, &bW, &bH);
|
||||||
//cout << bW << " " << bH << endl;
|
cout << bW << " " << bH << endl;
|
||||||
ApplySurfaces( 0, 0, background, ren);
|
ApplySurfaces( 0, 0, background, ren);
|
||||||
ApplySurfaces( bW, 0, background, ren);
|
ApplySurfaces( bW, 0, background, ren);
|
||||||
ApplySurfaces( 0, bH, background, ren);
|
ApplySurfaces( 0, bH, background, ren);
|
||||||
|
BIN
testSDL/lesson3/-.-
Executable file
BIN
testSDL/lesson3/-.-
Executable file
Binary file not shown.
BIN
testSDL/lesson3/background.bmp
Normal file
BIN
testSDL/lesson3/background.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 68 KiB |
161
testSDL/lesson3/main.cpp
Normal file
161
testSDL/lesson3/main.cpp
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_image.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
SDL_Renderer* ren;
|
||||||
|
|
||||||
|
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
|
||||||
|
std::cout << SDL_GetError() << std::endl;
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_UP (SDL_Renderer* ren, SDL_Texture* tex, SDL_Rect &destrect, int offset = 5)
|
||||||
|
{
|
||||||
|
destrect.y -= offset;
|
||||||
|
SDL_RenderClear(ren);
|
||||||
|
SDL_RenderCopy(ren, tex, NULL, &destrect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_DOWN (SDL_Renderer* ren, SDL_Texture* tex, SDL_Rect &destrect, int offset = 5)
|
||||||
|
{
|
||||||
|
destrect.y += offset;
|
||||||
|
SDL_RenderClear(ren);
|
||||||
|
SDL_RenderCopy(ren, tex, NULL, &destrect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_LEFT (SDL_Renderer* ren, SDL_Texture* tex, SDL_Rect &destrect, int offset = 5)
|
||||||
|
{
|
||||||
|
destrect.x -= offset;
|
||||||
|
SDL_RenderClear(ren);
|
||||||
|
SDL_RenderCopy(ren, tex, NULL, &destrect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_RIGHT (SDL_Renderer* ren, SDL_Texture* tex, SDL_Rect &destrect, int offset = 5)
|
||||||
|
{
|
||||||
|
destrect.x += offset;
|
||||||
|
SDL_RenderClear;
|
||||||
|
SDL_RenderCopy(ren, tex, NULL, &destrect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void render_UPDATE (SDL_Renderer* ren, SDL_Texture* tex[], SDL_Rect* destrect[], int states[])
|
||||||
|
{
|
||||||
|
SDL_RenderClear(ren);
|
||||||
|
if (states[0])
|
||||||
|
SDL_RenderCopy(ren, tex[0], NULL, destrect[0]);
|
||||||
|
if (states[1])
|
||||||
|
SDL_RenderCopy(ren, tex[1], NULL, destrect[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
SDL_DisplayMode displayMode;
|
||||||
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
||||||
|
{
|
||||||
|
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int request = SDL_GetDesktopDisplayMode(0,&displayMode);
|
||||||
|
SDL_Window* win = SDL_CreateWindow("Hello World!", 0, 0, 1280, 720, SDL_WINDOW_SHOWN);
|
||||||
|
if (win == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||||
|
if (ren == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const int player_wight = 333;
|
||||||
|
const int player_height = 227;
|
||||||
|
double texture_scale = 1.0;
|
||||||
|
SDL_Rect player_rect;
|
||||||
|
player_rect.x = 0;
|
||||||
|
player_rect.y = 0;
|
||||||
|
player_rect.w = player_wight;
|
||||||
|
player_rect.h = player_height;
|
||||||
|
SDL_Rect background_rect;
|
||||||
|
background_rect.x = 0;
|
||||||
|
background_rect.y = 0;
|
||||||
|
background_rect.h = displayMode.h;
|
||||||
|
background_rect.w = displayMode.w;
|
||||||
|
SDL_Texture* player = IMG_LoadTexture(ren, "player1.png");
|
||||||
|
SDL_Texture* background = IMG_LoadTexture(ren, "background.bmp");
|
||||||
|
SDL_RenderClear(ren);
|
||||||
|
SDL_RenderCopy(ren, background, NULL, &background_rect);
|
||||||
|
SDL_RenderCopy(ren, player, NULL, &player_rect);
|
||||||
|
SDL_RenderPresent(ren);
|
||||||
|
int bW, bH;
|
||||||
|
SDL_QueryTexture(background, NULL, NULL, &bW, &bH);
|
||||||
|
std::cout << bW << " " << bH << std::endl;
|
||||||
|
SDL_Event event;
|
||||||
|
const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
|
||||||
|
bool quit = false;
|
||||||
|
SDL_Texture* texture_array[2] = {background, player};
|
||||||
|
SDL_Rect* rect_array[2] = {&background_rect, &player_rect};
|
||||||
|
int texturesState_array[2] = {1,1};
|
||||||
|
|
||||||
|
while (!quit)
|
||||||
|
{
|
||||||
|
while(SDL_PollEvent(&event))
|
||||||
|
{
|
||||||
|
SDL_PumpEvents(); // обработчик событий.
|
||||||
|
|
||||||
|
if ((keyboardState[SDL_SCANCODE_UP])||(keyboardState[SDL_SCANCODE_W]))
|
||||||
|
{
|
||||||
|
move_UP(ren, player, player_rect);
|
||||||
|
}
|
||||||
|
if ((keyboardState[SDL_SCANCODE_DOWN])||keyboardState[SDL_SCANCODE_S])
|
||||||
|
{
|
||||||
|
move_DOWN(ren, player, player_rect);
|
||||||
|
}
|
||||||
|
if ((keyboardState[SDL_SCANCODE_LEFT])||keyboardState[SDL_SCANCODE_A])
|
||||||
|
{
|
||||||
|
move_LEFT(ren, player, player_rect);
|
||||||
|
}
|
||||||
|
if ((keyboardState[SDL_SCANCODE_RIGHT])||keyboardState[SDL_SCANCODE_D])
|
||||||
|
{
|
||||||
|
move_RIGHT(ren, player, player_rect);
|
||||||
|
}
|
||||||
|
if (keyboardState[SDL_SCANCODE_KP_PLUS])
|
||||||
|
{
|
||||||
|
texture_scale += 0.2;
|
||||||
|
player_rect.w = player_wight * texture_scale;
|
||||||
|
player_rect.h = player_height * texture_scale;
|
||||||
|
}
|
||||||
|
if (keyboardState[SDL_SCANCODE_KP_MINUS])
|
||||||
|
{
|
||||||
|
texture_scale -= 0.2;
|
||||||
|
player_rect.w = player_wight * texture_scale;
|
||||||
|
player_rect.h = player_height * texture_scale;
|
||||||
|
}
|
||||||
|
if (keyboardState[SDL_SCANCODE_ESCAPE])
|
||||||
|
{
|
||||||
|
quit = true;
|
||||||
|
}
|
||||||
|
render_UPDATE(ren, texture_array, rect_array, texturesState_array);
|
||||||
|
if (player_rect.x <= 0)
|
||||||
|
player_rect.x = 0;
|
||||||
|
if (player_rect.y <= 0)
|
||||||
|
player_rect.y = 0;
|
||||||
|
SDL_RenderPresent(ren);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SDL_DestroyTexture(player);
|
||||||
|
SDL_DestroyTexture(background);
|
||||||
|
SDL_DestroyWindow(win);
|
||||||
|
SDL_DestroyRenderer(ren);
|
||||||
|
SDL_Quit();
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
testSDL/lesson3/player.png
Normal file
BIN
testSDL/lesson3/player.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
BIN
testSDL/lesson3/player1.png
Normal file
BIN
testSDL/lesson3/player1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 229 KiB |
@ -18,3 +18,4 @@ SDL_DestroyRenderer |указатель на рендерер
|
|||||||
SDL_DestroyWindow |указатель на окно | - |уничтожает окно
|
SDL_DestroyWindow |указатель на окно | - |уничтожает окно
|
||||||
SDL_Quit() | - | - |выходил из систем SDL
|
SDL_Quit() | - | - |выходил из систем SDL
|
||||||
SDL_Delay |время в мс | - |просто некоторая задержка
|
SDL_Delay |время в мс | - |просто некоторая задержка
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user